8000 MemorySanitizer build fixes. · python/cpython@ffaeb17 · GitHub
[go: up one dir, main page]

Skip to content

Commit ffaeb17

Browse files
committed
MemorySanitizer build fixes.
These teach MSan about syscalls, asm behavior, or otherwise address clang memory sanitizer flagged issues.
1 parent dcf57d4 commit ffaeb17

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

Modules/_ctypes/callproc.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
#include <alloca.h>
7676
#endif
7777

78+
#ifdef MEMORY_SANITIZER
79+
#include <sanitizer/msan_interface.h>
80+
#endif
81+
7882
#if defined(_DEBUG) || defined(__MINGW32__)
7983
/* Don't use structured exception handling on Windows if this is defined.
8084
MingW, AFAIK, doesn't support it.
@@ -1125,6 +1129,13 @@ PyObject *_ctypes_callproc(PPROC pProc,
11251129
rtype = _ctypes_get_ffi_type(restype);
11261130
resbuf = alloca(max(rtype->size, sizeof(ffi_arg)));
11271131

1132+
#ifdef MEMORY_SANITIZER
1133+
/* ffi_call actually initializes resbuf, but from asm, which
1134+
* MemorySanitizer can't detect. Avoid false positives from MSan. */
1135+
if (resbuf != NULL) {
1136+
__msan_unpoison(resbuf, max(rtype->size, sizeof(ffi_arg)));
1137+
}
1138+
#endif
11281139
avalues = (void **)alloca(sizeof(void *) * argcount);
11291140
atypes = (ffi_type **)alloca(sizeof(ffi_type *) * argcount);
11301141
if (!resbuf || !avalues || !atypes) {

Modules/_posixsubprocess.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include <dirent.h>
2222
#endif
2323

24+
#ifdef MEMORY_SANITIZER
25+
# include <sanitizer/msan_interface.h>
26+
#endif
27+
2428
#if defined(__ANDROID__) && __ANDROID_API__ < 21 && !defined(SYS_getdents64)
2529
# include <sys/linux-syscalls.h>
2630
# define SYS_getdents64 __NR_getdents64
@@ -287,6 +291,9 @@ _close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
287291
sizeof(buffer))) > 0) {
288292
struct linux_dirent64 *entry;
289293
int offset;
294+
#ifdef MEMORY_SANITIZER
295+
__msan_unpoison(buffer, bytes);
296+
#endif
290297
for (offset = 0; offset < bytes; offset += entry->d_reclen) {
291298
int fd;
292299
entry = (struct linux_dirent64 *)(buffer + offset);

Modules/faulthandler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ void _PyFaulthandler_Fini(void)
13701370
#ifdef HAVE_SIGALTSTACK
13711371
if (stack.ss_sp != NULL) {
13721372
/* Fetch the current alt stack */
1373-
stack_t current_stack;
1373+
stack_t current_stack = {};
13741374
if (sigaltstack(NULL, &current_stack) == 0) {
13751375
if (current_stack.ss_sp == stack.ss_sp) {
13761376
/* The current alt stack is the one that we installed.

Python/bootstrap_hash.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
# endif
2121
#endif
2222

23+
#ifdef MEMORY_SANITIZER
24+
# include <sanitizer/msan_interface.h>
25+
#endif
26+
2327
#ifdef Py_DEBUG
2428
int _Py_HashSecret_Initialized = 0;
2529
#else
@@ -143,6 +147,11 @@ py_getrandom(void *buffer, Py_ssize_t size, int blocking, int raise)
143147
else {
144148
n = syscall(SYS_getrandom, dest, n, flags);
145149
}
150+
# ifdef MEMORY_SANITIZER
151+
if (n > 0) {
152+
__msan_unpoison(dest, n);
153+
}
154+
# endif
146155
#endif
147156

148157
if (n < 0) {

Python/pymath.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ double _Py_force_double(double x)
1717

1818
/* inline assembly for getting and setting the 387 FPU control word on
1919
gcc/x86 */
20-
20+
#ifdef MEMORY_SANITIZER
21+
__attribute__((no_sanitize_memory))
22+
#endif
2123
unsigned short _Py_get_387controlword(void) {
2224
unsigned short cw;
2325
__asm__ __volatile__ ("fnstcw %0" : "=m" (cw));

0 commit comments

Comments
 (0)
0