Skip to content

Commit b474e67

Browse files
authored
bpo-35214: MSan workarounds for socket, time, and test_faulthandler. (GH-11375)
Add Clang Memory Sanitizer build instrumentation to work around false positives from the socket and time modules as well as skipping a couple test_faulthandler tests.
1 parent 387512c commit b474e67

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

Lib/test/test_faulthandler.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import signal
66
import subprocess
77
import sys
8+
import sysconfig
89
from test import support
910
from test.support import script_helper, is_android
1011
import tempfile
@@ -19,6 +20,10 @@
1920

2021
TIMEOUT = 0.5
2122
MS_WINDOWS = (os.name == 'nt')
23+
MEMORY_SANITIZER = (
24+
sysconfig.get_config_var("CONFIG_ARGS") and
25+
("--with-memory-sanitizer" in sysconfig.get_config_var("CONFIG_ARGS"))
26+
)
2227

2328
def expected_traceback(lineno1, lineno2, header, min_count=1):
2429
regex = header
@@ -252,6 +257,8 @@ def test_gil_released(self):
252257
3,
253258
'Segmentation fault')
254259

260+
@unittest.skipIf(MEMORY_SANITIZER,
261+
"memory-sanizer builds change crashing process output.")
255262
@skip_segfault_on_android
256263
def test_enable_file(self):
257264
with temporary_filename() as filename:
@@ -267,6 +274,8 @@ def test_enable_file(self):
267274

268275
@unittest.skipIf(sys.platform == "win32",
269276
"subprocess doesn't support pass_fds on Windows")
277+
@unittest.skipIf(MEMORY_SANITIZER,
278+
"memory-sanizer builds change crashing process output.")
270279
@skip_segfault_on_android
271280
def test_enable_fd(self):
272281
with tempfile.TemporaryFile('wb+') as fp:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
clang Memory Sanitizer build instrumentation was added to work around false
2+
positives from socket, time, and test_faulthandler.

Modules/socketmodule.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ Local naming conventions:
102102
#include "Python.h"
103103
#include "structmember.h"
104104

105+
#ifdef _Py_MEMORY_SANITIZER
106+
# include <sanitizer/msan_interface.h>
107+
#endif
108+
105109
/* Socket object documentation */
106110
PyDoc_STRVAR(sock_doc,
107111
"socket(family=AF_INET, type=SOCK_STREAM, proto=0) -> socket object\n\
@@ -6571,7 +6575,23 @@ socket_if_nameindex(PyObject *self, PyObject *arg)
65716575
return NULL;
65726576
}
65736577

6578+
#ifdef _Py_MEMORY_SANITIZER
6579+
__msan_unpoison(ni, sizeof(ni));
6580+
__msan_unpoison(&ni[0], sizeof(ni[0]));
6581+
#endif
65746582
for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) {
6583+
#ifdef _Py_MEMORY_SANITIZER
6584+
/* This one isn't the end sentinel, the next one must exist. */
6585+
__msan_unpoison(&ni[i+1], sizeof(ni[0]));
6586+
/* Otherwise Py_BuildValue internals are flagged by MSan when
6587+
they access the not-msan-tracked if_name string data. */
6588+
{
6589+
char *to_sanitize = ni[i].if_name;
6590+
do {
6591+
__msan_unpoison(to_sanitize, 1);
6592+
} while (*to_sanitize++ != '\0');
6593+
}
6594+
#endif
65756595
PyObject *ni_tuple = Py_BuildValue("IO&",
65766596
ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name);
65776597

Modules/timemodule.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
#endif /* MS_WINDOWS */
3535
#endif /* !__WATCOMC__ || __QNX__ */
3636

37+
#ifdef _Py_MEMORY_SANITIZER
38+
# include <sanitizer/msan_interface.h>
39+
#endif
40+
3741
#define SEC_TO_NS (1000 * 1000 * 1000)
3842

3943
/* Forward declarations */
@@ -336,6 +340,9 @@ time_pthread_getcpuclockid(PyObject *self, PyObject *args)
336340
PyErr_SetFromErrno(PyExc_OSError);
337341
return NULL;
338342
}
343+
#ifdef _Py_MEMORY_SANITIZER
344+
__msan_unpoison(&clk_id, sizeof(clk_id));
345+
#endif
339346
return PyLong_FromLong(clk_id);
340347
}
341348

0 commit comments

Comments
 (0)