Valgrind 更改 CPUID 值

Valgrind changes CPUID value

Valgrind 正在更改 CPUID opcode instruction 返回的值。简单地说,如何让 Valgrind 遵守实际的 CPUID 指令?

供参考,这是在 运行 在我知道 没有 有 aes-ni 的旧计算机上检测 aes-ni 支持时出现奇怪错误时发现的指令系统。然而,这种行为显然正在改变多个值。

可以使用以下 C 代码 valgrind-3.10.1 观察此行为:

#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)

注意同样的二进制returns 0098e3fd正常,但是在valgrind下0298e3ff,这是错误的!

几天后没有任何答案,看来 Valgrind 无法允许正确的 CPUID 响应。

因为 Valgrind 本质上是 运行 在一个虚拟环境中,它会响应它所知道的 虚拟 处理器的 CPUID 信息,而不是系统的处理器。

感谢@Joe 的评论,以下 link 显示了可追溯到 2014 年的关于此的对话:https://sourceforge.net/p/valgrind/mailman/message/31960632/

简而言之,如果 Valgrind 可以选择将 CPUID 标志设置为运行时标志(正如 linked 线程中所建议的那样),但迄今为止(2018 年 2 月)还没有这样的选项标志存在。