[Python-checkins] bpo-45196: prevent unittest crash on address sanitizer builds (GH-28331)

miss-islington webhook-mailer at python.org
Tue Sep 14 14:54:35 EDT 2021


https://github.com/python/cpython/commit/9ccdc90488302b212bd3405d10dc5c22052e9b4c
commit: 9ccdc90488302b212bd3405d10dc5c22052e9b4c
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-09-14T11:54:27-07:00
summary:

bpo-45196: prevent unittest crash on address sanitizer builds (GH-28331)

(cherry picked from commit b668cdfa09e9bdfcfddaadd23dbd455d5f667383)

Co-authored-by: junyixie <xiejunyi.arch at bytedance.com>

files:
M Lib/test/test_decimal.py
M Lib/test/test_io.py

diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index d37c53da2cf4f..4404350504298 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -40,6 +40,17 @@
 import random
 import inspect
 import threading
+import sysconfig
+_cflags = sysconfig.get_config_var('CFLAGS') or ''
+_config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
+MEMORY_SANITIZER = (
+    '-fsanitize=memory' in _cflags or
+    '--with-memory-sanitizer' in _config_args
+)
+
+ADDRESS_SANITIZER = (
+    '-fsanitize=address' in _cflags
+)
 
 
 if sys.platform == 'darwin':
@@ -5486,6 +5497,8 @@ def __abs__(self):
     # Issue 41540:
     @unittest.skipIf(sys.platform.startswith("aix"),
                      "AIX: default ulimit: test is flaky because of extreme over-allocation")
+    @unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
+                     "instead of returning NULL for malloc failure.")
     def test_maxcontext_exact_arith(self):
 
         # Make sure that exact operations do not raise MemoryError due
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 8622939c4e619..49463e54e3c72 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -69,6 +69,10 @@ class EmptyStruct(ctypes.Structure):
     '--with-memory-sanitizer' in _config_args
 )
 
+ADDRESS_SANITIZER = (
+    '-fsanitize=address' in _cflags
+)
+
 # Does io.IOBase finalizer log the exception if the close() method fails?
 # The exception is ignored silently by default in release build.
 IOBASE_EMITS_UNRAISABLE = (hasattr(sys, "gettotalrefcount") or sys.flags.dev_mode)
@@ -1539,7 +1543,7 @@ def test_truncate_on_read_only(self):
 class CBufferedReaderTest(BufferedReaderTest, SizeofTest):
     tp = io.BufferedReader
 
-    @unittest.skipIf(MEMORY_SANITIZER, "MSan defaults to crashing "
+    @unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
                      "instead of returning NULL for malloc failure.")
     def test_constructor(self):
         BufferedReaderTest.test_constructor(self)
@@ -1888,7 +1892,7 @@ def test_slow_close_from_thread(self):
 class CBufferedWriterTest(BufferedWriterTest, SizeofTest):
     tp = io.BufferedWriter
 
-    @unittest.skipIf(MEMORY_SANITIZER, "MSan defaults to crashing "
+    @unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
                      "instead of returning NULL for malloc failure.")
     def test_constructor(self):
         BufferedWriterTest.test_constructor(self)
@@ -2387,7 +2391,7 @@ def test_interleaved_readline_write(self):
 class CBufferedRandomTest(BufferedRandomTest, SizeofTest):
     tp = io.BufferedRandom
 
-    @unittest.skipIf(MEMORY_SANITIZER, "MSan defaults to crashing "
+    @unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
                      "instead of returning NULL for malloc failure.")
     def test_constructor(self):
         BufferedRandomTest.test_constructor(self)



More information about the Python-checkins mailing list