Skip to content

Commit d088f9d

Browse files
committed
asan: Preload asan runtime into python interpreter
Same as llvm/llvm-project#123303 upstream
1 parent 481da8d commit d088f9d

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

projects/pt1/python/test/lit.cfg.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@
1818

1919
# Configuration file for the 'lit' test runner.
2020

21+
22+
# Find path to the ASan runtime required for the Python interpreter.
23+
def find_asan_runtime():
24+
if not "asan" in config.available_features or not "Linux" in config.host_os:
25+
return ""
26+
# Find the asan rt lib
27+
return (
28+
subprocess.check_output(
29+
[
30+
config.host_cxx.strip(),
31+
f"-print-file-name=libclang_rt.asan-{config.host_arch}.so",
32+
]
33+
)
34+
.decode("utf-8")
35+
.strip()
36+
)
37+
38+
2139
# name: The name of this test suite.
2240
config.name = "TORCH_MLIR_PYTHON"
2341

@@ -37,10 +55,15 @@
3755
# test_exec_root: The root path where tests should be run.
3856
config.test_exec_root = os.path.join(config.torch_mlir_obj_root, "test")
3957

58+
# Python configuration with sanitizer requires some magic preloading. This will only work on clang/linux.
59+
# TODO: detect Darwin/Windows situation (or mark these tests as unsupported on these platforms).
60+
if "asan" in config.available_features and "Linux" in config.host_os:
61+
_asan_rt = find_asan_runtime()
62+
config.python_executable = f"env LD_PRELOAD={_asan_rt} {config.python_executable}"
4063
# On Windows the path to python could contains spaces in which case it needs to
4164
# be provided in quotes. This is the equivalent of how %python is setup in
4265
# llvm/utils/lit/lit/llvm/config.py.
43-
if "Windows" in config.host_os:
66+
elif "Windows" in config.host_os:
4467
config.python_executable = '"%s"' % (config.python_executable)
4568

4669
config.substitutions.append(("%PATH%", config.environment["PATH"]))

0 commit comments

Comments
 (0)