Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MemorySanitizer build fixes.
These teach MSan about syscalls, asm behavior, or otherwise address
clang memory sanitizer flagged issues.
  • Loading branch information
gpshead committed Nov 12, 2018
commit ffaeb17bc760a059d22ffcdf0c35b3293eaacde0
11 changes: 11 additions & 0 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
#include <alloca.h>
#endif

#ifdef MEMORY_SANITIZER
#include <sanitizer/msan_interface.h>
#endif

#if defined(_DEBUG) || defined(__MINGW32__)
/* Don't use structured exception handling on Windows if this is defined.
MingW, AFAIK, doesn't support it.
Expand Down Expand Up @@ -1125,6 +1129,13 @@ PyObject *_ctypes_callproc(PPROC pProc,
rtype = _ctypes_get_ffi_type(restype);
resbuf = alloca(max(rtype->size, sizeof(ffi_arg)));

#ifdef MEMORY_SANITIZER
/* ffi_call actually initializes resbuf, but from asm, which
* MemorySanitizer can't detect. Avoid false positives from MSan. */
if (resbuf != NULL) {
__msan_unpoison(resbuf, max(rtype->size, sizeof(ffi_arg)));
}
#endif
avalues = (void **)alloca(sizeof(void *) * argcount);
atypes = (ffi_type **)alloca(sizeof(ffi_type *) * argcount);
if (!resbuf || !avalues || !atypes) {
Expand Down
7 changes: 7 additions & 0 deletions Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include <dirent.h>
#endif

#ifdef MEMORY_SANITIZER
# include <sanitizer/msan_interface.h>
#endif

#if defined(__ANDROID__) && __ANDROID_API__ < 21 && !defined(SYS_getdents64)
# include <sys/linux-syscalls.h>
# define SYS_getdents64 __NR_getdents64
Expand Down Expand Up @@ -287,6 +291,9 @@ _close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
sizeof(buffer))) > 0) {
struct linux_dirent64 *entry;
int offset;
#ifdef MEMORY_SANITIZER
__msan_unpoison(buffer, bytes);
#endif
for (offset = 0; offset < bytes; offset += entry->d_reclen) {
int fd;
entry = (struct linux_dirent64 *)(buffer + offset);
Expand Down
2 changes: 1 addition & 1 deletion Modules/faulthandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ void _PyFaulthandler_Fini(void)
#ifdef HAVE_SIGALTSTACK
if (stack.ss_sp != NULL) {
/* Fetch the current alt stack */
stack_t current_stack;
stack_t current_stack = {};
if (sigaltstack(NULL, &current_stack) == 0) {
if (current_stack.ss_sp == stack.ss_sp) {
/* The current alt stack is the one that we installed.
Expand Down
9 changes: 9 additions & 0 deletions Python/bootstrap_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# endif
#endif

#ifdef MEMORY_SANITIZER
# include <sanitizer/msan_interface.h>
#endif

#ifdef Py_DEBUG
int _Py_HashSecret_Initialized = 0;
#else
Expand Down Expand Up @@ -143,6 +147,11 @@ py_getrandom(void *buffer, Py_ssize_t size, int blocking, int raise)
else {
n = syscall(SYS_getrandom, dest, n, flags);
}
# ifdef MEMORY_SANITIZER
if (n > 0) {
__msan_unpoison(dest, n);
}
# endif
#endif

if (n < 0) {
Expand Down
4 changes: 3 additions & 1 deletion Python/pymath.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ double _Py_force_double(double x)

/* inline assembly for getting and setting the 387 FPU control word on
gcc/x86 */

#ifdef MEMORY_SANITIZER
__attribute__((no_sanitize_memory))
#endif
unsigned short _Py_get_387controlword(void) {
unsigned short cw;
__asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
Expand Down