Skip to content

Commit 0bf1fe8

Browse files
authored
Merge pull request #647 from tkoeppe/msan
[cpuid-x86.h] Add MSAN annotations to mark memory as initialized To be used with clang -fsanitize=memory (which currently requires additional flags, see llvm/llvm-project#77393)
2 parents b6fb7d2 + a9e5336 commit 0bf1fe8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

include/private/cpuid-x86.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
#ifndef HWLOC_PRIVATE_CPUID_X86_H
1212
#define HWLOC_PRIVATE_CPUID_X86_H
1313

14+
/* A macro for annotating memory as uninitialized when building with MSAN
15+
* (and otherwise having no effect). See below for why this is used with
16+
* our custom assembly.
17+
*/
18+
#ifdef __has_feature
19+
#define HWLOC_HAS_FEATURE(name) __has_feature(name)
20+
#else
21+
#define HWLOC_HAS_FEATURE(name) 0
22+
#endif
23+
#if HWLOC_HAS_FEATURE(memory_sanitizer) || defined(MEMORY_SANITIZER)
24+
#include <sanitizer/msan_interface.h>
25+
#define HWLOC_ANNOTATE_MEMORY_IS_INITIALIZED(ptr, len) __msan_unpoison(ptr, len)
26+
#else
27+
#define HWLOC_ANNOTATE_MEMORY_IS_INITIALIZED(ptr, len)
28+
#endif
29+
1430
#if (defined HWLOC_X86_32_ARCH) && (!defined HWLOC_HAVE_MSVC_CPUIDEX)
1531
static __hwloc_inline int hwloc_have_x86_cpuid(void)
1632
{
@@ -71,12 +87,18 @@ static __hwloc_inline void hwloc_x86_cpuid(unsigned *eax, unsigned *ebx, unsigne
7187
"movl %k2,%1\n\t"
7288
: "+a" (*eax), "=m" (*ebx), "=&r"(sav_rbx),
7389
"+c" (*ecx), "=&d" (*edx));
90+
/* MSAN does not recognize the effect of the above assembly on the memory operand
91+
* (`"=m"(*ebx)`). This may get improved in MSAN at some point in the future, e.g.
92+
* see https://github.com/llvm/llvm-project/pull/77393. */
93+
HWLOC_ANNOTATE_MEMORY_IS_INITIALIZED(ebx, sizeof *ebx);
7494
#elif defined(HWLOC_X86_32_ARCH)
7595
__asm__(
7696
"mov %%ebx,%1\n\t"
7797
"cpuid\n\t"
7898
"xchg %%ebx,%1\n\t"
7999
: "+a" (*eax), "=&SD" (*ebx), "+c" (*ecx), "=&d" (*edx));
100+
/* See above. */
101+
HWLOC_ANNOTATE_MEMORY_IS_INITIALIZED(ebx, sizeof *ebx);
80102
#else
81103
#error unknown architecture
82104
#endif

0 commit comments

Comments
 (0)