调用code存放在由VC堆++ [英] calling code stored in the heap from vc++

查看:127
本文介绍了调用code存放在由VC堆++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我做这样的事情:

Imagine I am doing something like this:

void *p = malloc (1000);
*((char*)p) = some_opcode;
*((char*)p+1) = another_opcode; // for the sake of the example: the opcodes are ok

....
etc...

我如何定义一个函数指针调用数p作为,如果它是一个功能? (我使用VC ++ 2008的前preSS)。

How can I define a function pointer to call p as if it was a function? (i'm using VC++ 2008 express).

感谢

推荐答案

一个评论是没有足够的空间。 Joe_Muc是正确的。你不应该的东西code到由的malloc 获得内存。你会遇到的问题,如果你更改页面的页面属性是Windows分配。

A comment wasn't enough space. Joe_Muc is correct. You should not stuff code into memory obtained by malloc or new. You will run into problems if you change the page properties of pages that Windows allocates.

这是不是监守使用的VirtualAlloc()和相关的Win32 API的一个问题是每个简单:拨打 VirtualAlloc的(),并设置了 flProtect [PAGE_EXECUTE_READWRITE] [2]

This isn't a problem becuase using VirtualAlloc() and the related WIn32 APIs is every easy: call VirtualAlloc() and set the flProtect to [PAGE_EXECUTE_READWRITE][2]

请注意,你应该做三分配,一是保护页,你需要为你code中的页面,然后又保护页。这会给你从坏code有点保护。

Note, you should probably do three allocations, one guard page, the pages you need for your code, then another guard page. This will give you a little protection from bad code.

也换到你产生code调用与结构化异常处理

Also wrap calls to your generated code with structured exception handling.

接下来,在Windows X86 ABI(调用约定)都有详细的记载(我知道,我已经看了)。有一些信息这里,的here 这里最好的办法,看看如何事情的工作是看编译器生成code。这是很容易与 \\ FA 切换(有他们四个)。

Next, the Windows X86 ABI (calling conventions) are not well documented (I know, I've looked). There is some info here, here, here The best way to see how things work is to look at code generated by the compiler. This is easy to do with the \FA switches ( there are four of them).

您可以找到64位调用约定这里

You can find the 64-bit calling conventions here.

此外,你仍然可以得到微软的宏汇编<一个href=\"http://www.microsoft.com/downloads/details.aspx?familyid=7A1C9DA0-0510-44A2-B042-7EF370530C64&displaylang=en\">MASM这里。我建议在MASM写你的机器code,并期待在其输出端,然后让你的机器code发电机做类似的事情。

Also, you can still obtain Microsoft's Macro Assembler MASM here. I recommend writing your machine code in MASM and look at its output, then have your machine code generator do similar things.

英特尔并的 AMD的处理器手册是很好的参考 - 让他们,如果你没有他们

Intel's and AMD's processor manuals are good references - get them if you don't have them.

这篇关于调用code存放在由VC堆++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆