From 7c0dda5f64d9a310443d14ff9487708f4b297163 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith [Google LLC]" Date: Sun, 30 Dec 2018 15:37:01 -0800 Subject: [PATCH 1/3] bpo-35214: MSan workarounds for socket, time, and test_faulthandler. 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. --- Lib/test/test_faulthandler.py | 5 +++++ .../2018-12-30-15-36-23.bpo-35214.GWDQcv.rst | 2 ++ Modules/socketmodule.c | 20 +++++++++++++++++++ Modules/timemodule.c | 7 +++++++ 4 files changed, 34 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-12-30-15-36-23.bpo-35214.GWDQcv.rst diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py index 59289d026e3919..5651dae8724882 100644 --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -5,6 +5,7 @@ import signal import subprocess import sys +import sysconfig from test import support from test.support import script_helper, is_android import tempfile @@ -252,6 +253,8 @@ def test_gil_released(self): 3, 'Segmentation fault') + @unittest.skipIf("--with-memory-sanitizer" in sysconfig.get_config_var("CONFIG_ARGS"), + "memory-sanizer builds change crashing process output.") @skip_segfault_on_android def test_enable_file(self): with temporary_filename() as filename: @@ -267,6 +270,8 @@ def test_enable_file(self): @unittest.skipIf(sys.platform == "win32", "subprocess doesn't support pass_fds on Windows") + @unittest.skipIf("--with-memory-sanitizer" in sysconfig.get_config_var("CONFIG_ARGS"), + "memory-sanizer builds change crashing process output.") @skip_segfault_on_android def test_enable_fd(self): with tempfile.TemporaryFile('wb+') as fp: diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-12-30-15-36-23.bpo-35214.GWDQcv.rst b/Misc/NEWS.d/next/Core and Builtins/2018-12-30-15-36-23.bpo-35214.GWDQcv.rst new file mode 100644 index 00000000000000..2e7e61864487fe --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-12-30-15-36-23.bpo-35214.GWDQcv.rst @@ -0,0 +1,2 @@ +Clang Memory Sanitizer build instrumentation was added to work around false +positives from socket, time, and test_faulthandler. diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 66e52f84eb91f6..0ae280f298818c 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -102,6 +102,10 @@ Local naming conventions: #include "Python.h" #include "structmember.h" +#ifdef _Py_MEMORY_SANITIZER +# include +#endif + /* Socket object documentation */ PyDoc_STRVAR(sock_doc, "socket(family=AF_INET, type=SOCK_STREAM, proto=0) -> socket object\n\ @@ -6571,7 +6575,23 @@ socket_if_nameindex(PyObject *self, PyObject *arg) return NULL; } +#ifdef _Py_MEMORY_SANITIZER + __msan_unpoison(ni, sizeof(ni)); + __msan_unpoison(&ni[0], sizeof(ni[0])); +#endif for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) { +#ifdef _Py_MEMORY_SANITIZER + /* This one isn't the end sentinel, the next one must exist. */ + __msan_unpoison(&ni[i+1], sizeof(ni[0])); + /* Otherwise Py_BuildValue internals are flagged by MSan when + they access the not-msan-tracked if_name string data. */ + { + char *to_sanitize = ni[i].if_name; + do { + __msan_unpoison(to_sanitize, 1); + } while (*to_sanitize++ != '\0'); + } +#endif PyObject *ni_tuple = Py_BuildValue("IO&", ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name); diff --git a/Modules/timemodule.c b/Modules/timemodule.c index cf6522927adaa2..43951d5623d3f4 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -34,6 +34,10 @@ #endif /* MS_WINDOWS */ #endif /* !__WATCOMC__ || __QNX__ */ +#ifdef _Py_MEMORY_SANITIZER +# include +#endif + #define SEC_TO_NS (1000 * 1000 * 1000) /* Forward declarations */ @@ -336,6 +340,9 @@ time_pthread_getcpuclockid(PyObject *self, PyObject *args) PyErr_SetFromErrno(PyExc_OSError); return NULL; } +#ifdef _Py_MEMORY_SANITIZER + __msan_unpoison(&clk_id, sizeof(clk_id)); +#endif return PyLong_FromLong(clk_id); } From d309ab769649dd14c345d24ca12a21258879133c Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith [Google LLC]" Date: Sun, 30 Dec 2018 15:52:34 -0800 Subject: [PATCH 2/3] Fix the skip on Windows. --- Lib/test/test_faulthandler.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py index 5651dae8724882..76dd6ab068053f 100644 --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -20,6 +20,10 @@ TIMEOUT = 0.5 MS_WINDOWS = (os.name == 'nt') +MEMORY_SANITIZER = ( + sysconfig.get_config_var("CONFIG_ARGS") and + ("--with-memory-sanitizer" in sysconfig.get_config_var("CONFIG_ARGS")) +) def expected_traceback(lineno1, lineno2, header, min_count=1): regex = header @@ -253,7 +257,7 @@ def test_gil_released(self): 3, 'Segmentation fault') - @unittest.skipIf("--with-memory-sanitizer" in sysconfig.get_config_var("CONFIG_ARGS"), + @unittest.skipIf(MEMORY_SANITIZER, "memory-sanizer builds change crashing process output.") @skip_segfault_on_android def test_enable_file(self): @@ -270,7 +274,7 @@ def test_enable_file(self): @unittest.skipIf(sys.platform == "win32", "subprocess doesn't support pass_fds on Windows") - @unittest.skipIf("--with-memory-sanitizer" in sysconfig.get_config_var("CONFIG_ARGS"), + @unittest.skipIf(MEMORY_SANITIZER, "memory-sanizer builds change crashing process output.") @skip_segfault_on_android def test_enable_fd(self): From 073d18bd79c95e986f2752866be1608cb769e4ad Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 30 Dec 2018 16:15:25 -0800 Subject: [PATCH 3/3] make appveyor run again --- .../Core and Builtins/2018-12-30-15-36-23.bpo-35214.GWDQcv.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-12-30-15-36-23.bpo-35214.GWDQcv.rst b/Misc/NEWS.d/next/Core and Builtins/2018-12-30-15-36-23.bpo-35214.GWDQcv.rst index 2e7e61864487fe..d2e5457842b748 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2018-12-30-15-36-23.bpo-35214.GWDQcv.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2018-12-30-15-36-23.bpo-35214.GWDQcv.rst @@ -1,2 +1,2 @@ -Clang Memory Sanitizer build instrumentation was added to work around false +clang Memory Sanitizer build instrumentation was added to work around false positives from socket, time, and test_faulthandler.