From d088f9d92710c8fb57b687cc3b03026634e60750 Mon Sep 17 00:00:00 2001 From: Matthias Gehre Date: Mon, 24 Feb 2025 14:39:38 +0100 Subject: [PATCH] asan: Preload asan runtime into python interpreter Same as https://github.com/llvm/llvm-project/pull/123303 upstream --- projects/pt1/python/test/lit.cfg.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/projects/pt1/python/test/lit.cfg.py b/projects/pt1/python/test/lit.cfg.py index 0e6d132faa00..ddac7b7dc596 100644 --- a/projects/pt1/python/test/lit.cfg.py +++ b/projects/pt1/python/test/lit.cfg.py @@ -18,6 +18,24 @@ # Configuration file for the 'lit' test runner. + +# Find path to the ASan runtime required for the Python interpreter. +def find_asan_runtime(): + if not "asan" in config.available_features or not "Linux" in config.host_os: + return "" + # Find the asan rt lib + return ( + subprocess.check_output( + [ + config.host_cxx.strip(), + f"-print-file-name=libclang_rt.asan-{config.host_arch}.so", + ] + ) + .decode("utf-8") + .strip() + ) + + # name: The name of this test suite. config.name = "TORCH_MLIR_PYTHON" @@ -37,10 +55,15 @@ # test_exec_root: The root path where tests should be run. config.test_exec_root = os.path.join(config.torch_mlir_obj_root, "test") +# Python configuration with sanitizer requires some magic preloading. This will only work on clang/linux. +# TODO: detect Darwin/Windows situation (or mark these tests as unsupported on these platforms). +if "asan" in config.available_features and "Linux" in config.host_os: + _asan_rt = find_asan_runtime() + config.python_executable = f"env LD_PRELOAD={_asan_rt} {config.python_executable}" # On Windows the path to python could contains spaces in which case it needs to # be provided in quotes. This is the equivalent of how %python is setup in # llvm/utils/lit/lit/llvm/config.py. -if "Windows" in config.host_os: +elif "Windows" in config.host_os: config.python_executable = '"%s"' % (config.python_executable) config.substitutions.append(("%PATH%", config.environment["PATH"]))