可变参数的汇编中的有效模式

A valid pattern in assembly for variadic arguments

本文关键字:有效 模式 汇编 参数 变参      更新时间:2023-10-16

我想我的问题可能看起来有点奇怪,但它就在这里;我正在尝试在C++中动态创建一个程序(主要是为了好玩,但也出于编程原因(,这并不像听起来那么难。为此,您必须在运行时使用程序集,如下所示:

byte * buffer = new byte[5];
*buffer = '0xE9'; // Code for 'jmp'
*(uint*)(buffer + 1) = 'address destination'; // Address to jump to

这比看起来容易得多,因为我只针对一个平台和编译器;使用 Linux 32 位的 GCC(也只有一个调用约定 cdecl(。所以我正在尝试创建一个动态程序集函数来重定向来自触发器的调用,这样我就可以使用类方法作为回调(即使使用 C API 库(当然使用 cdecl((。我只需要它来支持指针和本机类型(字符、整数、短等(。

ANYTHING MyRedirect(ANY AMOUNT ARGUMENTS)
{
    return MyClassFunc('this', ANY AMOUNT ARGUMENTS);
}

上面的函数是我想在纯汇编中创建的函数(在内存中带有 C++(。由于该函数非常简单,因此其ASM也很简单(取决于参数(。

55                      push   %ebp
89 e5                   mov    %esp,%ebp
83 ec 04                sub    $0x4,%esp
8b 45 08                mov    0x8(%ebp),%eax
89 04 24                mov    %eax,(%esp)
e8 00 00 00 00          call   <address>
c9                      leave
c3                      ret  

所以在我的程序中,我创建了一个ASM模式生成器(因为我不是特别了解ASM,所以我搜索模式(。此函数可以通过指定函数所需的参数量来生成汇编代码(以字节为单位,对于上述确切情况,即重定向和返回的函数(。这是我C++代码中的一个片段。

std::vector<byte> detourFunc(10 + stackSize, 0x90); // Base is 10 bytes + argument size
// This becomes 'push %ebp; move %esp, %ebp'
detourFunc.push_back(0x55);     // push %ebp
detourFunc.push_back(0x89);     // mov
detourFunc.push_back(0xE5);     // %esp, %ebp
// Check for arguments
if(stackSize != 0)
{
    detourFunc.push_back(0x83);     // sub
    detourFunc.push_back(0xEC);     // %esp
    detourFunc.push_back(stackSize);    // stack size required
    // If there are arguments, we want to push them
    // in the opposite direction (cdecl convention)
    for(int i = (argumentCount - 1); i >= 0; i--)
    {
        // This is what I'm trying to implement
        // ...
    }
    // Check if we need to add 'this'
    if(m_callbackClassPtr)
    {
    }
}
// This is our call operator
detourFunc.push_back(0xE8);     // call
// All nop, this will be replaced by an address
detourFunc.push_back(0x90);     // nop
detourFunc.push_back(0x90);     // nop
detourFunc.push_back(0x90);     // nop
detourFunc.push_back(0x90);     // nop
if(stackSize == 0)
{
    // In case of no arguments, just 'pop'
    detourFunc.push_back(0x5D); // pop %ebp
}
else 
{
    // Use 'leave' if we have arguments
    detourFunc.push_back(0xC9); // leave    
}
// Return function
detourFunc.push_back(0xC3);     // ret

如果我指定零作为stackSize这将是输出:

55                      push   %ebp
89 e5                   mov    %esp,%ebp
e8 90 90 90 90          call   <address>
5d                      pop    %ebp
c3                      ret   

如您所见,这是完全有效的 32 位 ASM,如果它没有参数并且不需要"this"指针,它将充当"MyRedirect"。问题是,我想实现它生成 ASM 代码的部分,具体取决于我指定的"重定向"函数将接收的参数数量。我已经在我的小C++程序中成功地做到了这一点(破解了模式(。

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
    int val = atoi(argv[1]);
    printf("tpush %%ebpn");
    printf("tmov %%esp,%%ebpn");
    if(val == 0)
    {
        printf("tcall <address>n");
        printf("tpop %%ebpn");
    }
    else
    {
        printf("tsub $0x%x,%%espn", val * sizeof(int));
        for(int i = val; i > 0; i--)
        {
            printf("tmov 0x%x(%%ebp),%%eaxn", i * sizeof(int) + sizeof(int));
            printf("tmov %%eax,0x%x(%%esp)n", i * sizeof(int) - sizeof(int));
        }
        printf("tcall <address>n");
        printf("tleaven");
    }
    printf("tretn");
    return 0;
}

此函数打印出与"objdump"生成的 ASM 代码完全相同的模式。所以我的问题是;如果我想要上述重定向函数,无论参数如何,如果它仅在 Linux 32 位下,或者是否有任何我需要了解的陷阱,这是否在所有情况下都有效?例如;生成的 ASM 是否会与"短裤"或"字符"不同,或者这会起作用(我只用整数测试过(,以及如果我调用一个返回"void"的函数(这将如何影响 ASM(?

我可能把所有事情解释得有点模糊,所以请问而不是任何误解:)

注意:我不想知道替代方案,我喜欢我目前的实现,并认为这是一个非常有趣的实现,我非常感谢您在这个问题上的帮助。

编辑:如果有兴趣,这里有一些转储上面的C++代码:链接

正如 Dan 建议的那样,您需要将内存标记为可执行文件。我写了一些你可以使用的代码。(它适用于GNU/Linux和Windows。如果您打算永远不支持 ARM、x86-64 或其他平台,那么我认为您的代码(添加了可执行部分(没有任何缺点,并且似乎它应该"始终有效"。(当然,假设其他一切都正常工作。

#include <sys/mman.h>
...
n = <size of code buffer>;
p = mmap(0, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, 0, 0);

"鱼"建议你使用ASMJIT。我必须同意这一点;它比您的方法更便携。但是,您说您对替代方案不感兴趣。

你可能对一种叫做"Thunking"(有点(的东西感兴趣。它基本上尝试完成"用C++方法替换 C 回调"。这实际上非常有用,但对于您的应用程序来说并不是一个好的设计。

希望有帮助。