栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何动态生成和运行本机代码?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何动态生成和运行本机代码?

不确定linux,但这适用于x86 / windows。
更新:http://prepad.org/sQoF6kR8

#include <stdio.h>#include <windows.h>typedef unsigned char byte;int arg1;int arg2;int res1;typedef void (*pfunc)(void);union funcptr {  pfunc x;  byte* y;};int main( void ) {  byte* buf = (byte*)VirtualAllocEx( GetCurrentProcess(), 0, 1<<16, MEM_COMMIT, PAGE_EXECUTE_READWRITE );  if( buf==0 ) return 0;  byte* p = buf;  *p++ = 0x50; // push eax  *p++ = 0x52; // push edx  *p++ = 0xA1; // mov eax, [arg2]  (int*&)p[0] = &arg2; p+=sizeof(int*);  *p++ = 0x92; // xchg edx,eax  *p++ = 0xA1; // mov eax, [arg1]  (int*&)p[0] = &arg1; p+=sizeof(int*);  *p++ = 0xF7; *p++ = 0xEA; // imul edx  *p++ = 0xA3; // mov [res1],eax  (int*&)p[0] = &res1; p+=sizeof(int*);  *p++ = 0x5A; // pop edx  *p++ = 0x58; // pop eax  *p++ = 0xC3; // ret  funcptr func;  func.y = buf;  arg1 = 123; arg2 = 321; res1 = 0;  func.x(); // call generated pre  printf( "arg1=%i arg2=%i arg1*arg2=%i func(arg1,arg2)=%in", arg1,arg2,arg1*arg2,res1 );}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399089.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号