Skip to content

Commit 9326445

Browse files
vstinnerpablogsal
andauthored
[3.10] bpo-46633: Skip tests on ASAN and/or MSAN builds (GH-31632) (GH-31634)
* Refactor sanitiser skip tests into test.support (GH-30889) * Refactor sanitizer skip tests into test.support (cherry picked from commit b1cb843) * Add skips to crashing tests under sanitizers instead of manually skipping them (GH-30897) (cherry picked from commit a275053) * bpo-46633: Skip tests on ASAN and/or MSAN builds (GH-31632) Skip tests on ASAN and/or MSAN builds: * multiprocessing tests * test___all__ * test_concurrent_futures * test_decimal * test_peg_generator * test_tools (cherry picked from commit 9204bb7) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
1 parent 7dbb2f8 commit 9326445

14 files changed

+109
-49
lines changed

Lib/test/_test_multiprocessing.py

+6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
msvcrt = None
7474

7575

76+
if support.check_sanitizer(address=True):
77+
# bpo-45200: Skip multiprocessing tests if Python is built with ASAN to
78+
# work around a libasan race condition: dead lock in pthread_create().
79+
raise unittest.SkipTest("libasan has a pthread_create() dead lock")
80+
81+
7682
def latin(s):
7783
return s.encode('latin')
7884

Lib/test/support/__init__.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"requires_IEEE_754", "requires_zlib",
4242
"anticipate_failure", "load_package_tests", "detect_api_mismatch",
4343
"check__all__", "skip_if_buggy_ucrt_strfptime",
44-
"check_disallow_instantiation",
44+
"check_disallow_instantiation", "check_sanitizer", "skip_if_sanitizer",
4545
# sys
4646
"is_jython", "is_android", "check_impl_detail", "unix_shell",
4747
"setswitchinterval",
@@ -367,6 +367,41 @@ def wrapper(*args, **kw):
367367
return decorator
368368

369369

370+
def check_sanitizer(*, address=False, memory=False, ub=False):
371+
"""Returns True if Python is compiled with sanitizer support"""
372+
if not (address or memory or ub):
373+
raise ValueError('At least one of address, memory, or ub must be True')
374+
375+
376+
_cflags = sysconfig.get_config_var('CFLAGS') or ''
377+
_config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
378+
memory_sanitizer = (
379+
'-fsanitize=memory' in _cflags or
380+
'--with-memory-sanitizer' in _config_args
381+
)
382+
address_sanitizer = (
383+
'-fsanitize=address' in _cflags or
384+
'--with-memory-sanitizer' in _config_args
385+
)
386+
ub_sanitizer = (
387+
'-fsanitize=undefined' in _cflags or
388+
'--with-undefined-behavior-sanitizer' in _config_args
389+
)
390+
return (
391+
(memory and memory_sanitizer) or
392+
(address and address_sanitizer) or
393+
(ub and ub_sanitizer)
394+
)
395+
396+
397+
def skip_if_sanitizer(reason=None, *, address=False, memory=False, ub=False):
398+
"""Decorator raising SkipTest if running with a sanitizer active."""
399+
if not reason:
400+
reason = 'not working with sanitizers active'
401+
skip = check_sanitizer(address=address, memory=memory, ub=ub)
402+
return unittest.skipIf(skip, reason)
403+
404+
370405
def system_must_validate_cert(f):
371406
"""Skip the test on TLS certificate validation failures."""
372407
@functools.wraps(f)

Lib/test/test___all__.py

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
import sys
66

77

8+
if support.check_sanitizer(address=True, memory=True):
9+
# bpo-46633: test___all__ is skipped because importing some modules
10+
# directly can trigger known problems with ASAN (like tk or crypt).
11+
raise unittest.SkipTest("workaround ASAN build issues on loading tests "
12+
"like tk or crypt")
13+
14+
815
class NoAll(RuntimeError):
916
pass
1017

Lib/test/test_concurrent_futures.py

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
import multiprocessing.util
3333

3434

35+
if support.check_sanitizer(address=True, memory=True):
36+
# bpo-46633: Skip the test because it is too slow when Python is built
37+
# with ASAN/MSAN: between 5 and 20 minutes on GitHub Actions.
38+
raise unittest.SkipTest("test too slow on ASAN/MSAN build")
39+
40+
3541
def create_future(state=PENDING, exception=None, result=None):
3642
f = Future()
3743
f._state = state

Lib/test/test_crypt.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import sys
22
import unittest
3+
from test.support import check_sanitizer
34

45

56
try:
7+
if check_sanitizer(address=True, memory=True):
8+
raise unittest.SkipTest("The crypt module SEGFAULTs on ASAN/MSAN builds")
69
import crypt
710
IMPORT_ERROR = None
811
except ImportError as ex:

Lib/test/test_decimal.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import locale
3535
from test.support import (run_unittest, run_doctest, is_resource_enabled,
3636
requires_IEEE_754, requires_docstrings,
37-
requires_legacy_unicode_capi)
37+
requires_legacy_unicode_capi, check_sanitizer)
3838
from test.support import (TestFailed,
3939
run_with_locale, cpython_only,
4040
darwin_malloc_err_warning)
@@ -43,17 +43,6 @@
4343
import random
4444
import inspect
4545
import threading
46-
import sysconfig
47-
_cflags = sysconfig.get_config_var('CFLAGS') or ''
48-
_config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
49-
MEMORY_SANITIZER = (
50-
'-fsanitize=memory' in _cflags or
51-
'--with-memory-sanitizer' in _config_args
52-
)
53-
54-
ADDRESS_SANITIZER = (
55-
'-fsanitize=address' in _cflags
56-
)
5746

5847

5948
if sys.platform == 'darwin':
@@ -5511,7 +5500,8 @@ def __abs__(self):
55115500
# Issue 41540:
55125501
@unittest.skipIf(sys.platform.startswith("aix"),
55135502
"AIX: default ulimit: test is flaky because of extreme over-allocation")
5514-
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
5503+
@unittest.skipIf(check_sanitizer(address=True, memory=True),
5504+
"ASAN/MSAN sanitizer defaults to crashing "
55155505
"instead of returning NULL for malloc failure.")
55165506
def test_maxcontext_exact_arith(self):
55175507

Lib/test/test_faulthandler.py

+5-15
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import signal
77
import subprocess
88
import sys
9-
import sysconfig
109
from test import support
1110
from test.support import os_helper
1211
from test.support import script_helper, is_android
12+
from test.support import skip_if_sanitizer
1313
import tempfile
1414
import unittest
1515
from textwrap import dedent
@@ -21,16 +21,6 @@
2121

2222
TIMEOUT = 0.5
2323
MS_WINDOWS = (os.name == 'nt')
24-
_cflags = sysconfig.get_config_var('CFLAGS') or ''
25-
_config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
26-
UB_SANITIZER = (
27-
'-fsanitize=undefined' in _cflags or
28-
'--with-undefined-behavior-sanitizer' in _config_args
29-
)
30-
MEMORY_SANITIZER = (
31-
'-fsanitize=memory' in _cflags or
32-
'--with-memory-sanitizer' in _config_args
33-
)
3424

3525

3626
def expected_traceback(lineno1, lineno2, header, min_count=1):
@@ -310,8 +300,8 @@ def test_gil_released(self):
310300
3,
311301
'Segmentation fault')
312302

313-
@unittest.skipIf(UB_SANITIZER or MEMORY_SANITIZER,
314-
"sanitizer builds change crashing process output.")
303+
@skip_if_sanitizer(memory=True, ub=True, reason="sanitizer "
304+
"builds change crashing process output.")
315305
@skip_segfault_on_android
316306
def test_enable_file(self):
317307
with temporary_filename() as filename:
@@ -327,8 +317,8 @@ def test_enable_file(self):
327317

328318
@unittest.skipIf(sys.platform == "win32",
329319
"subprocess doesn't support pass_fds on Windows")
330-
@unittest.skipIf(UB_SANITIZER or MEMORY_SANITIZER,
331-
"sanitizer builds change crashing process output.")
320+
@skip_if_sanitizer(memory=True, ub=True, reason="sanitizer "
321+
"builds change crashing process output.")
332322
@skip_segfault_on_android
333323
def test_enable_fd(self):
334324
with tempfile.TemporaryFile('wb+') as fp:

Lib/test/test_idle.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import unittest
22
from test.support.import_helper import import_module
3+
from test.support import check_sanitizer
4+
5+
if check_sanitizer(address=True, memory=True):
6+
raise unittest.SkipTest("Tests involvin libX11 can SEGFAULT on ASAN/MSAN builds")
37

48
# Skip test_idle if _tkinter wasn't built, if tkinter is missing,
59
# if tcl/tk is not the 8.5+ needed for ttk widgets,

Lib/test/test_io.py

+7-18
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import random
2929
import signal
3030
import sys
31-
import sysconfig
3231
import textwrap
3332
import threading
3433
import time
@@ -44,6 +43,7 @@
4443
from test.support import os_helper
4544
from test.support import threading_helper
4645
from test.support import warnings_helper
46+
from test.support import skip_if_sanitizer
4747
from test.support.os_helper import FakePath
4848

4949
import codecs
@@ -66,17 +66,6 @@ def byteslike(*pos, **kw):
6666
class EmptyStruct(ctypes.Structure):
6767
pass
6868

69-
_cflags = sysconfig.get_config_var('CFLAGS') or ''
70-
_config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
71-
MEMORY_SANITIZER = (
72-
'-fsanitize=memory' in _cflags or
73-
'--with-memory-sanitizer' in _config_args
74-
)
75-
76-
ADDRESS_SANITIZER = (
77-
'-fsanitize=address' in _cflags
78-
)
79-
8069
# Does io.IOBase finalizer log the exception if the close() method fails?
8170
# The exception is ignored silently by default in release build.
8271
IOBASE_EMITS_UNRAISABLE = (hasattr(sys, "gettotalrefcount") or sys.flags.dev_mode)
@@ -1550,8 +1539,8 @@ def test_truncate_on_read_only(self):
15501539
class CBufferedReaderTest(BufferedReaderTest, SizeofTest):
15511540
tp = io.BufferedReader
15521541

1553-
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
1554-
"instead of returning NULL for malloc failure.")
1542+
@skip_if_sanitizer(memory=True, address=True, reason= "sanitizer defaults to crashing "
1543+
"instead of returning NULL for malloc failure.")
15551544
def test_constructor(self):
15561545
BufferedReaderTest.test_constructor(self)
15571546
# The allocation can succeed on 32-bit builds, e.g. with more
@@ -1915,8 +1904,8 @@ def test_slow_close_from_thread(self):
19151904
class CBufferedWriterTest(BufferedWriterTest, SizeofTest):
19161905
tp = io.BufferedWriter
19171906

1918-
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
1919-
"instead of returning NULL for malloc failure.")
1907+
@skip_if_sanitizer(memory=True, address=True, reason= "sanitizer defaults to crashing "
1908+
"instead of returning NULL for malloc failure.")
19201909
def test_constructor(self):
19211910
BufferedWriterTest.test_constructor(self)
19221911
# The allocation can succeed on 32-bit builds, e.g. with more
@@ -2414,8 +2403,8 @@ def test_interleaved_readline_write(self):
24142403
class CBufferedRandomTest(BufferedRandomTest, SizeofTest):
24152404
tp = io.BufferedRandom
24162405

2417-
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
2418-
"instead of returning NULL for malloc failure.")
2406+
@skip_if_sanitizer(memory=True, address=True, reason= "sanitizer defaults to crashing "
2407+
"instead of returning NULL for malloc failure.")
24192408
def test_constructor(self):
24202409
BufferedRandomTest.test_constructor(self)
24212410
# The allocation can succeed on 32-bit builds, e.g. with more
+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import os
2-
1+
import os.path
2+
import unittest
3+
from test import support
34
from test.support import load_package_tests
45

6+
7+
if support.check_sanitizer(address=True, memory=True):
8+
# bpo-46633: Skip the test because it is too slow when Python is built
9+
# with ASAN/MSAN: between 5 and 20 minutes on GitHub Actions.
10+
raise unittest.SkipTest("test too slow on ASAN/MSAN build")
11+
12+
513
# Load all tests in package
614
def load_tests(*args):
715
return load_package_tests(os.path.dirname(__file__), *args)

Lib/test/test_tix.py

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
import unittest
33
from test import support
44
from test.support import import_helper
5+
from test.support import check_sanitizer
6+
7+
if check_sanitizer(address=True, memory=True):
8+
raise unittest.SkipTest("Tests involvin libX11 can SEGFAULT on ASAN/MSAN builds")
9+
510

611
# Skip this test if the _tkinter module wasn't built.
712
_tkinter = import_helper.import_module('_tkinter')

Lib/test/test_tk.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import unittest
12
from test import support
23
from test.support import import_helper
4+
from test.support import check_sanitizer
5+
6+
if check_sanitizer(address=True, memory=True):
7+
raise unittest.SkipTest("Tests involvin libX11 can SEGFAULT on ASAN/MSAN builds")
8+
39
# Skip test if _tkinter wasn't built.
410
import_helper.import_module('_tkinter')
511

Lib/test/test_tools/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
from test import support
77
from test.support import import_helper
88

9+
10+
if support.check_sanitizer(address=True, memory=True):
11+
# bpo-46633: Skip the test because it is too slow when Python is built
12+
# with ASAN/MSAN: between 5 and 20 minutes on GitHub Actions.
13+
raise unittest.SkipTest("test too slow on ASAN/MSAN build")
14+
15+
916
basepath = os.path.normpath(
1017
os.path.dirname( # <src/install dir>
1118
os.path.dirname( # Lib

Lib/test/test_ttk_guionly.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import unittest
22
from test import support
33
from test.support import import_helper
4+
from test.support import check_sanitizer
5+
6+
if check_sanitizer(address=True, memory=True):
7+
raise unittest.SkipTest("Tests involvin libX11 can SEGFAULT on ASAN/MSAN builds")
48

59
# Skip this test if _tkinter wasn't built.
610
import_helper.import_module('_tkinter')

0 commit comments

Comments
 (0)