Skip to content

Commit

Permalink
pythongh-90791: test.pythoninfo logs ASAN_OPTIONS env var (python#108289
Browse files Browse the repository at this point in the history
)

* Cleanup libregrtest code logging ASAN_OPTIONS.
* Fix a typo on "ASAN_OPTIONS" vs "MSAN_OPTIONS".
  • Loading branch information
vstinner authored Aug 22, 2023
1 parent a0bb4a3 commit 3a1ac87
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
41 changes: 24 additions & 17 deletions Lib/test/libregrtest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,26 +526,33 @@ def display_header(self):
print("== CPU count:", cpu_count)
print("== encodings: locale=%s, FS=%s"
% (locale.getencoding(), sys.getfilesystemencoding()))
self.display_sanitizers()

def display_sanitizers(self):
# This makes it easier to remember what to set in your local
# environment when trying to reproduce a sanitizer failure.
asan = support.check_sanitizer(address=True)
msan = support.check_sanitizer(memory=True)
ubsan = support.check_sanitizer(ub=True)
# This makes it easier to remember what to set in your local
# environment when trying to reproduce a sanitizer failure.
if asan or msan or ubsan:
names = [n for n in (asan and "address",
msan and "memory",
ubsan and "undefined behavior")
if n]
print(f"== sanitizers: {', '.join(names)}")
a_opts = os.environ.get("ASAN_OPTIONS")
if asan and a_opts is not None:
print(f"== ASAN_OPTIONS={a_opts}")
m_opts = os.environ.get("ASAN_OPTIONS")
if msan and m_opts is not None:
print(f"== MSAN_OPTIONS={m_opts}")
ub_opts = os.environ.get("UBSAN_OPTIONS")
if ubsan and ub_opts is not None:
print(f"== UBSAN_OPTIONS={ub_opts}")
sanitizers = []
if asan:
sanitizers.append("address")
if msan:
sanitizers.append("memory")
if ubsan:
sanitizers.append("undefined behavior")
if not sanitizers:
return

print(f"== sanitizers: {', '.join(sanitizers)}")
for sanitizer, env_var in (
(asan, "ASAN_OPTIONS"),
(msan, "MSAN_OPTIONS"),
(ubsan, "UBSAN_OPTIONS"),
):
options= os.environ.get(env_var)
if sanitizer and options is not None:
print(f"== {env_var}={options!r}")

def no_tests_run(self):
return not any((self.good, self.bad, self.skipped, self.interrupted,
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/pythoninfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ def format_groups(groups):
"_PYTHON_PROJECT_BASE",
"_PYTHON_SYSCONFIGDATA_NAME",
"__PYVENV_LAUNCHER__",

# Sanitizer options
"ASAN_OPTIONS",
"LSAN_OPTIONS",
"MSAN_OPTIONS",
"TSAN_OPTIONS",
"UBSAN_OPTIONS",
))
for name, value in os.environ.items():
uname = name.upper()
Expand Down
16 changes: 8 additions & 8 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,19 +407,19 @@ def check_sanitizer(*, address=False, memory=False, ub=False):
raise ValueError('At least one of address, memory, or ub must be True')


_cflags = sysconfig.get_config_var('CFLAGS') or ''
_config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
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
'-fsanitize=memory' in cflags or
'--with-memory-sanitizer' in config_args
)
address_sanitizer = (
'-fsanitize=address' in _cflags or
'--with-address-sanitizer' in _config_args
'-fsanitize=address' in cflags or
'--with-address-sanitizer' in config_args
)
ub_sanitizer = (
'-fsanitize=undefined' in _cflags or
'--with-undefined-behavior-sanitizer' in _config_args
'-fsanitize=undefined' in cflags or
'--with-undefined-behavior-sanitizer' in config_args
)
return (
(memory and memory_sanitizer) or
Expand Down

0 comments on commit 3a1ac87

Please sign in to comment.