【发布时间】:2018-07-19 17:41:01
【问题描述】:
Valgrind 正在更改 CPUID opcode instruction 返回的值。简单地说,如何让 Valgrind 尊重实际的 CPUID 指令?
作为参考,这是在我知道 没有 具有 aes-ni 指令集的旧计算机上检测到 aes-ni 支持时遇到奇怪错误时发现的。然而,这种行为显然会改变多个值。
可以通过valgrind-3.10.1 观察到这种行为,使用以下 C 代码:
#include <stdio.h>
int main() {
unsigned eax, ebx, ecx, edx;
eax = 1;
__asm__ volatile("cpuid"
: "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx)
: "0" (eax), "2" (ecx)
);
if(ecx & (1<<25)) {
printf("aes-ni enabled (ecx=%08x)n", ecx);
} else {
printf("no aes-ni support (ecx=%08x)\n", ecx);
}
return 1;
}
这样编译和运行:
$ gcc -o test test.c
$ ./test
no aes-ni support (ecx=0098e3fd)
$ valgrind ./test
==25361== Memcheck, a memory error detector
==25361== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==25361== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==25361== Command: ./test
==25361==
aes-ni enabled (ecx=0298e3ff)
==25361==
==25361== HEAP SUMMARY:
==25361== in use at exit: 0 bytes in 0 blocks
==25361== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==25361==
==25361== All heap blocks were freed -- no leaks are possible
==25361==
==25361== For counts of detected and suppressed errors, rerun with: -v
==25361== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
注意,同样的二进制正常返回0098e3fd,但是在valgrind下0298e3ff,这是错误的!
【问题讨论】:
-
@cegfault,请注意 valgrind 本质上是一个 CPU 仿真器。与一些分析工具不同,它读取并执行机器代码,收集数据(计数指令而不是时间)。我不确定你能否在 valgrind 上测试 CPU 特定的代码,但我可能错了。
-
@Myst 是的,Valgrind 基本上是一个虚拟接口(如果没有虚拟化和控制内存库就找不到内存泄漏)。也就是说,如果 Valgrind 的虚拟系统是硬编码的,那么它就不能用于测试回退代码(与 aes-ni 一样;我有支持它的系统的 aesni 内在函数,原始 C 代码作为回退代码) .如果不重写代码的调用方式,我无法测试该备用代码是否存在内存泄漏,如果 Valgrind 需要这样做,我会感到失望。
-
此外,正是因为 Valgrind 是虚拟的,我认为它的反馈和标志可以被修改。修改它(尤其是禁用一项功能)比修改裸机设置更容易。
-
@cegfault 我并不是说它不能完成...我不是 valgrind 专家...但是,我会考虑(作为一种解决方法)使用编译标志编译代码这会强制使用后备代码。无论您在何种 CPU 下运行,这都将允许您测试代码。我知道这很麻烦,但这是我唯一能提供帮助的想法。