写个好玩的JIT demo,仅限Intel x86+ windows+msvc

2019-01-28 17:19:32 浏览数 (2)

代码语言:javascript复制
#include <Windows.h>
#include <cstdint>
#include <cstring>
#define BACK_FILL (0)

int main() {
    char* native = (char*)VirtualAlloc(NULL, 1024, MEM_COMMIT | MEM_RESERVE,
                                       PAGE_EXECUTE_READWRITE);
    char code[] = {0x68, 0x65, 0x6c, 0x6c,      0x6f,      0x20,      0x77,
                   0x6f, 0x72, 0x6c, 0x64,      0x00,      0xff,      0xf5,
                   0x89, 0xe5, 0x68, BACK_FILL, BACK_FILL, BACK_FILL, BACK_FILL,
                   0xff, 0x95, 0x08, 0x00,      0x00,      0x00,      0x81,
                   0xc4, 0x04, 0x00, 0x00,      0x00,      0x8f,      0xc5,
                   0xc3, 0x00};
    memcpy(native, code, sizeof(code) / sizeof(char));
    *(int32_t*)(native   17) = (int32_t)native;
    ((void (*)(int (*)(const char*, ...)))(native   12))(&printf);
    VirtualFree(native, 0, MEM_RELEASE);
    getchar();
    return 0;
}

have fun~

0 人点赞