Skip to content

Commit 01b9664

Browse files
bpo-35214: MSan workarounds for socket, time, and test_faulthandler. (GH-11375) (GH-11378)
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. (cherry picked from commit b474e67) Co-authored-by: Gregory P. Smith <[email protected]>
1 parent c74061d commit 01b9664

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
@@ -100,6 +100,10 @@ Local naming conventions:
100100
#include "Python.h"
101101
#include "structmember.h"
102102

103+
#ifdef _Py_MEMORY_SANITIZER
104+
# include <sanitizer/msan_interface.h>
105+
#endif
106+
103107
/* Socket object documentation */
104108
PyDoc_STRVAR(sock_doc,
105109
"socket(family=AF_INET, type=SOCK_STREAM, proto=0) -> socket object\n\
@@ -6463,7 +6467,23 @@ socket_if_nameindex(PyObject *self, PyObject *arg)
64636467
return NULL;
64646468
}
64656469

6470+
#ifdef _Py_MEMORY_SANITIZER
6471+
__msan_unpoison(ni, sizeof(ni));
6472+
__msan_unpoison(&ni[0], sizeof(ni[0]));
6473+
#endif
64666474
for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) {
6475+
#ifdef _Py_MEMORY_SANITIZER
6476+
/* This one isn't the end sentinel, the next one must exist. */
6477+
__msan_unpoison(&ni[i+1], sizeof(ni[0]));
6478+
/* Otherwise Py_BuildValue internals are flagged by MSan when
6479+
they access the not-msan-tracked if_name string data. */
6480+
{
6481+
char *to_sanitize = ni[i].if_name;
6482+
do {
6483+
__msan_unpoison(to_sanitize, 1);
6484+
} while (*to_sanitize++ != '\0');
6485+
}
6486+
#endif
64676487
PyObject *ni_tuple = Py_BuildValue("IO&",
64686488
ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name);
64696489

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 */
@@ -331,6 +335,9 @@ time_pthread_getcpuclockid(PyObject *self, PyObject *args)
331335
PyErr_SetFromErrno(PyExc_OSError);
332336
return NULL;
333337
}
338+
#ifdef _Py_MEMORY_SANITIZER
339+
__msan_unpoison(&clk_id, sizeof(clk_id));
340+
#endif
334341
return PyLong_FromLong(clk_id);
335342
}
336343

0 commit comments

Comments
 (0)