Valgrind is changing the values returned by the CPUID opcode instruction. Simply put, how can I make Valgrind respect the actual CPUID instruction?
For reference, this was discovered when running into strange errors when detecting aes-ni support on an old computer which I know does not have the aes-ni instruction set. This behavior, however, is clearly changing multiple values.
This behavior can be observed with valgrind-3.10.1, using the following C code:
#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;
}
Run Code Online (Sandbox Code Playgroud)
Which compiles and runs as such:
$ 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)
Run Code Online (Sandbox Code Playgroud)
请注意,相同的二进制文件通常返回0098e3fd,但在 valgrind 下返回 0298e3ff,这是错误的!
几天后没有得到任何答复,看来 Valgrind 无法允许正确的 CPUID 响应。
因为 Valgrind 本质上是在虚拟环境中运行,所以它将响应有关它所识别的虚拟处理器(而不是系统处理器)的 CPUID 信息。
感谢 @Joe 的评论,以下链接显示了可追溯到 2014 年的有关此问题的对话:https://sourceforge.net/p/valgrind/mailman/message/31960632/
简而言之,如果 Valgrind 能够选择将 CPUID 标志设置为运行时标志(如链接线程中所建议的那样),那就太好了,但迄今为止(2018 年 2 月)尚不存在这样的标志。
| 归档时间: |
|
| 查看次数: |
638 次 |
| 最近记录: |