Skip to content

Commit e2263f1

Browse files
committed
[libc][NFC] Move code to sanitizer.h + more consistent naming
1 parent 90c98f8 commit e2263f1

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

libc/src/__support/FPUtil/x86_64/FEnvImpl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct X87StateDescriptor {
104104
LIBC_INLINE uint16_t get_x87_control_word() {
105105
uint16_t w;
106106
__asm__ __volatile__("fnstcw %0" : "=m"(w)::);
107-
SANITIZER_MEMORY_INITIALIZED(&w, sizeof(w));
107+
MSAN_UNPOISON(&w, sizeof(w));
108108
return w;
109109
}
110110

@@ -115,7 +115,7 @@ LIBC_INLINE void write_x87_control_word(uint16_t w) {
115115
LIBC_INLINE uint16_t get_x87_status_word() {
116116
uint16_t w;
117117
__asm__ __volatile__("fnstsw %0" : "=m"(w)::);
118-
SANITIZER_MEMORY_INITIALIZED(&w, sizeof(w));
118+
MSAN_UNPOISON(&w, sizeof(w));
119119
return w;
120120
}
121121

@@ -126,7 +126,7 @@ LIBC_INLINE void clear_x87_exceptions() {
126126
LIBC_INLINE uint32_t get_mxcsr() {
127127
uint32_t w;
128128
__asm__ __volatile__("stmxcsr %0" : "=m"(w)::);
129-
SANITIZER_MEMORY_INITIALIZED(&w, sizeof(w));
129+
MSAN_UNPOISON(&w, sizeof(w));
130130
return w;
131131
}
132132

@@ -136,7 +136,7 @@ LIBC_INLINE void write_mxcsr(uint32_t w) {
136136

137137
LIBC_INLINE void get_x87_state_descriptor(X87StateDescriptor &s) {
138138
__asm__ __volatile__("fnstenv %0" : "=m"(s));
139-
SANITIZER_MEMORY_INITIALIZED(&s, sizeof(s));
139+
MSAN_UNPOISON(&s, sizeof(s));
140140
}
141141

142142
LIBC_INLINE void write_x87_state_descriptor(const X87StateDescriptor &s) {

libc/src/__support/macros/sanitizer.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,20 @@
4949

5050
#if LIBC_HAVE_MEMORY_SANITIZER
5151
#include <sanitizer/msan_interface.h>
52-
#define SANITIZER_MEMORY_INITIALIZED(addr, size) __msan_unpoison(addr, size)
52+
#define MSAN_UNPOISON(addr, size) __msan_unpoison(addr, size)
5353
#else
54-
#define SANITIZER_MEMORY_INITIALIZED(ptr, size)
54+
#define MSAN_UNPOISON(ptr, size)
5555
#endif
56+
57+
#if LIBC_HAVE_ADDRESS_SANITIZER
58+
#include <sanitizer/asan_interface.h>
59+
#define ASAN_POISON_MEMORY_REGION(addr, size) \
60+
__asan_poison_memory_region((addr), (size))
61+
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
62+
__asan_unpoison_memory_region((addr), (size))
63+
#else
64+
#define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
65+
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
66+
#endif
67+
5668
#endif // LLVM_LIBC_SRC_SUPPORT_MACROS_SANITIZER_H

libc/test/src/string/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ function(add_libc_multi_impl_test name)
367367
${ARGN}
368368
DEPENDS
369369
${fq_config_name}
370+
libc.src.__support.sanitizer
370371
)
371372
get_fq_target_name(${fq_config_name}_test fq_target_name)
372373
else()

libc/test/src/string/memory_utils/memory_check_utils.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,13 @@
1111

1212
#include "src/__support/CPP/span.h"
1313
#include "src/__support/macros/compiler_features.h"
14+
#include "src/__support/macros/sanitizer.h"
1415
#include "src/string/memory_utils/utils.h"
1516
#include <assert.h> // assert
1617
#include <stddef.h> // size_t
1718
#include <stdint.h> // uintxx_t
1819
#include <stdlib.h> // malloc/free
1920

20-
#if LIBC_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
21-
#include <sanitizer/asan_interface.h>
22-
#define ASAN_POISON_MEMORY_REGION(addr, size) \
23-
__asan_poison_memory_region((addr), (size))
24-
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
25-
__asan_unpoison_memory_region((addr), (size))
26-
#else
27-
#define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
28-
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
29-
#endif
30-
3121
namespace __llvm_libc {
3222

3323
// Simple structure to allocate a buffer of a particular size.

utils/bazel/llvm-project-overlay/libc/test/src/string/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ cc_library(
111111
hdrs = ["memory_utils/memory_check_utils.h"],
112112
deps = [
113113
"//libc:__support_cpp_span",
114+
"//libc:__support_sanitizer",
114115
"//libc:string_memory_utils",
115116
],
116117
)

0 commit comments

Comments
 (0)