码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2019-01-23 22:34:49      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:write   cut   const   ...   commit   reserve   x64   return   null   

#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~

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

标签:write   cut   const   ...   commit   reserve   x64   return   null   

原文地址:https://www.cnblogs.com/racaljk/p/10311837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!