优化大型地址空间上的循环

Optimizing for loop over a large address space

本文关键字:循环 地址空间 大型 优化      更新时间:2023-10-16

我正在尝试调试一个进程的虚拟内存,这需要我在其虚拟内存中搜索一系列已知和常量字节,但是由于偏移量一直在动态变化,我必须遍历整个地址空间以找到我需要的字节。目前我有这些循环,它们有效,但是它们平均需要很长时间才能运行~15-30秒才能完成。

BYTE target[15] = { 0xFG, 0x93, 0x32, 0x1A, 0xB0, 0x9F, 0xC7, 0x00, 0x11, 0x00,
                        0x3F, 0x01, 0x00, 0x00, 0x00 };
    BYTE buff[15];
    int count = 0;
    DWORD target_addr = 0;
    for (DWORD i = 0x401000; i <= 0x2000000; i++)
    {
        count = 0;
        ReadProcessMemory(procName, (LPCVOID)i, (LPVOID)buff, 15, &real);
        for (int j = 0; j < 15; j++)
        {
            if (target[j] == buff[j])
                count++;
            else
                break;
        }
        if (count == 15)
        {
            target_addr = i;
            break;
        }
    }

我怎样才能大大加快这个过程?

这是我第一次使用低级内存函数和Windows的调试API,所以我很可能错过了更好的方法。

假设您在从 i 开始的每次迭代中读取 15 个字节,如果内存不相等,则从 i+1 开始读取接下来的 15 个字节,因此您不考虑您可能知道其他 14 个字节相等的事实。如果向后看 15 个字节,则可以知道,如果缓冲区不相等,如果最后k个字节相等,这允许直接向前15-k字节,并且不验证下一个迭代中的第一个字节k

我还没有测试下面的代码,但我认为有了这个总体思路,它将允许更快地执行。

BYTE target[15] = { 0xFG, 0x93, 0x32, 0x1A, 0xB0, 0x9F, 0xC7, 0x00, 0x11, 0x00,
                    0x3F, 0x01, 0x00, 0x00, 0x00 };
BYTE buff[15];
DWORD target_addr = 0;
DWORD current = 0x401000;
DWORD max = 0x2000000;
int previousEqual = 0;
while (!target_addr && current < max)
{
    int count = 15;
    ReadProcessMemory(procName, (LPCVOID)i, (LPVOID)buff, 15, &real);
    while (target[count-1] == buff[count-1])
    {
         --count;
    }
    if (count == previousEqual)
    {
        target_addr = current;
    }
    else
    {
        current += count;
        previousEqual = 15-count;
    }
}