优化大型地址空间上的循环
Optimizing for loop over a large address space
我正在尝试调试一个进程的虚拟内存,这需要我在其虚拟内存中搜索一系列已知和常量字节,但是由于偏移量一直在动态变化,我必须遍历整个地址空间以找到我需要的字节。目前我有这些循环,它们有效,但是它们平均需要很长时间才能运行~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;
}
}
相关文章:
- C++/地址空间:每个地址 2 个字节?
- 任务管理器 - C/C++ 应用程序 - 分配的地址空间与已用内存
- size_t ptrdiff_t和地址空间
- C++中是否有一个函数可以为您获取指向该节点的所有指针的地址空间
- ptrA = ptrB 是否等同于 ptrA = &*ptrB?,空值是否共享一个地址空间?
- 优化大型地址空间上的循环
- 如何表示指向地址空间开头的指针
- 从某个特定但很大的虚拟地址空间(例如,20GB以上)分配内存
- Do C和C++标准意味着地址空间中必须只存在一个特殊值来表示空指针的值
- 使用mprotect保护整个地址空间
- 关于取消引用和地址空间的基本C++指针问题
- 进程如何访问不属于其虚拟地址空间的内存映射 IO 地址?
- 在虚拟地址空间中使用内存映射文件
- 由 shmat 返回的指针位于地址空间的末尾,这给出了 SEG 错误
- 与地址空间无关的对象表示
- 以何种形式,可以通过管道将空指针发送到不同进程地址空间中的另一个进程
- 窗口中的虚拟地址空间
- 在Clang中使用指向成员和地址空间的指针
- 更新进程地址空间中的HTML内容
- 如何判断共享库在进程地址空间中的加载位置