diff options
author | MITSUNARI Shigeo <[email protected]> | 2018-08-28 18:31:43 +0900 |
---|---|---|
committer | MITSUNARI Shigeo <[email protected]> | 2018-08-28 18:31:43 +0900 |
commit | 93579ee65d53700250c90b56c13b8e74176425e6 (patch) | |
tree | d4755f677d4b5155e964f2626d09cacfd309d51a | |
parent | 60004b5c77badf2a397ee5495f8a3e76f57c2327 (diff) | |
download | xbyak-93579ee65d53700250c90b56c13b8e74176425e6.tar.gz xbyak-93579ee65d53700250c90b56c13b8e74176425e6.zip |
add protect-re.cpp
-rw-r--r-- | sample/protect-re.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/sample/protect-re.cpp b/sample/protect-re.cpp new file mode 100644 index 0000000..6eaa863 --- /dev/null +++ b/sample/protect-re.cpp @@ -0,0 +1,70 @@ +#define XBYAK_NO_OP_NAMES +#include <xbyak/xbyak.h> + +struct Code1 : Xbyak::CodeGenerator { + Code1() + : Xbyak::CodeGenerator(4096, Xbyak::DontSetProtectRWE) + { + mov(eax, 123); + ret(); + } + void update() + { + db(0); + } +}; + +void test1(bool updateCode) +{ + Code1 c; + c.setProtectModeRE(); + if (updateCode) c.update(); // segmentation fault + int (*f)() = c.getCode<int (*)()>(); + printf("f=%d\n", f()); + + c.setProtectModeRW(); + c.update(); + puts("ok"); +} + +struct Code2 : Xbyak::CodeGenerator { + Code2() + : Xbyak::CodeGenerator(4096, Xbyak::AutoGrow) + { + mov(eax, 123); + ret(); + } + void update() + { + db(0); + } +}; + +void test2(bool updateCode) +{ + Code2 c; + c.readyRE(); + if (updateCode) c.update(); // segmentation fault + int (*f)() = c.getCode<int (*)()>(); + printf("f=%d\n", f()); + + c.setProtectModeRW(); + c.update(); + puts("ok"); +} + +int main(int argc, char *argv[]) +{ + if (argc < 2) { + fprintf(stderr, "%s <testNum> [update]\n", argv[0]); + return 0; + } + bool update = argc == 3; + int n = atoi(argv[1]); + printf("n=%d update=%d\n", n, update); + switch (n) { + case 1: test1(update); break; + case 2: test2(update); break; + default: fprintf(stderr, "no test %d\n", n); break; + } +} |