【问题标题】:Why my code can only run on windows xp 32bit system?[code generation]为什么我的代码只能在windows xp 32bit系统上运行?[代码生成]
【发布时间】:2015-02-07 06:33:01
【问题描述】:

我正在尝试编写一个生成x86机器代码的程序,但代码只能在32位winxp上运行。它说“tiny.exe 中 0x776315ee 处的未处理异常:0xC0000005:访问冲突。” win7 64bit,为什么?

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

int main()
{
    char *code = (char *)malloc(1024);
    memset(code, 0, 1024);

    char opcode = 0xB8;
    memcpy(code, &opcode, sizeof(char));

    int oprand = 2;
    memcpy(code + sizeof(char), &oprand, sizeof(int));

    char opret = 0xC3;
    memcpy(code + sizeof(char) + sizeof(int), &opret, sizeof(char));

    typedef void (* func_t)(void);
    func_t func = (func_t)code;

    func();

    return 0;
}

【问题讨论】:

    标签: c visual-c++ compiler-construction compiler-optimization


    【解决方案1】:

    在堆上分配的内存受到DEP 的保护,无法执行。它可能在您的 XP 机器上被禁用,或者可能是其他原因,但无论原因是什么:您必须调用 VirtualAlloc() 和(至少)PAGE_EXECUTE_READWRITE 作为 flProtect 来动态分配内存,而不是 malloc()可以从中执行代码。

    【讨论】:

    • 但请注意,与malloc 不同,VirtualAlloc 会将大小向上舍入到页面边界(可能是 4 KiB)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多