From a6759c35e2113fd6144ae0e561504aed26583d9b Mon Sep 17 00:00:00 2001 From: Sahil Faizal Date: Sun, 4 Oct 2026 11:38:40 -0700 Subject: [PATCH 1/2] fix(ci): preload ASan before starting pytest --- .../test_executable_scripts/pytest_runner.py | 14 +++++ .../tests/pytest_runner_test.py | 54 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/build_tools/github_actions/test_executable_scripts/pytest_runner.py b/build_tools/github_actions/test_executable_scripts/pytest_runner.py index a0bf7a19e26..508a23f38f2 100644 --- a/build_tools/github_actions/test_executable_scripts/pytest_runner.py +++ b/build_tools/github_actions/test_executable_scripts/pytest_runner.py @@ -47,6 +47,10 @@ from pathlib import Path import yaml +sys.path.append(str(Path(__file__).resolve().parent.parent)) +from amdgpu_family_matrix import is_asan +from configure_asan_env import get_asan_runtime_path + logging.basicConfig(level=logging.INFO, format="%(message)s") # Map job name -> install location, relative to the ROCm prefix (THEROCK_BIN_DIR @@ -242,6 +246,16 @@ def build_environment(rocm_path, component_name): env = os.environ.copy() component_root = resolve_component_path(component_name, rocm_path) + # Initialize ASan before Python allocates objects that instrumented imports + # may later free. Merely exposing the runtime on LD_LIBRARY_PATH loads it + # too late when rocisa imports instrumented ROCm libraries. + if is_asan(): + runtime = str(get_asan_runtime_path(rocm_path)) + existing_preload = env.get("LD_PRELOAD", "") + env["LD_PRELOAD"] = ( + f"{runtime}:{existing_preload}" if existing_preload else runtime + ) + existing_pythonpath = env.get("PYTHONPATH") env["PYTHONPATH"] = ( f"{component_root}{os.pathsep}{existing_pythonpath}" diff --git a/build_tools/github_actions/tests/pytest_runner_test.py b/build_tools/github_actions/tests/pytest_runner_test.py index 38178aaae3f..d490847fa5c 100644 --- a/build_tools/github_actions/tests/pytest_runner_test.py +++ b/build_tools/github_actions/tests/pytest_runner_test.py @@ -268,6 +268,60 @@ def test_prepends_to_existing_values(self): self.assertTrue(env["PYTHONPATH"].endswith("/pre/existing")) self.assertIn("/pre/ld", env["LD_LIBRARY_PATH"].split(os.pathsep)) + def test_asan_runtime_precedes_existing_preload(self): + with tempfile.TemporaryDirectory() as tmp: + runtime = Path(tmp) / "libclang_rt.asan.so" + runtime.touch() + with mock.patch.dict( + os.environ, + { + "BUILD_VARIANT": "host-asan", + "ASAN_RUNTIME_PATH": str(runtime), + "LD_PRELOAD": "/existing/library.so", + "ASAN_OPTIONS": "detect_leaks=1", + }, + clear=True, + ): + env = pytest_runner.build_environment(Path(tmp), "tensilelite") + self.assertEqual( + env["LD_PRELOAD"], f"{runtime.resolve()}:/existing/library.so" + ) + self.assertEqual(env["ASAN_OPTIONS"], "detect_leaks=1") + + def test_sanitizer_preload_is_scoped_to_child_environment(self): + with tempfile.TemporaryDirectory() as tmp: + runtime = Path(tmp) / "libclang_rt.asan.so" + runtime.touch() + for variant in ("asan", "host-asan", "asan-debug", "host-asan-debug"): + with ( + self.subTest(variant=variant), + mock.patch.dict( + os.environ, + { + "BUILD_VARIANT": variant, + "ASAN_RUNTIME_PATH": str(runtime), + }, + clear=True, + ), + ): + parent_env = os.environ.copy() + env = pytest_runner.build_environment(Path(tmp), "tensilelite") + self.assertEqual(env["LD_PRELOAD"], str(runtime.resolve())) + self.assertEqual(dict(os.environ), parent_env) + + def test_release_does_not_resolve_or_change_preload(self): + with mock.patch.dict( + os.environ, + { + "BUILD_VARIANT": "release", + "ASAN_RUNTIME_PATH": "/missing/asan.so", + "LD_PRELOAD": "/existing/library.so", + }, + clear=True, + ): + env = pytest_runner.build_environment(Path("/opt/rocm"), "tensilelite") + self.assertEqual(env["LD_PRELOAD"], "/existing/library.so") + def test_ld_library_path_includes_all_llvm_triple_dirs(self): # All triple subdirs must be on LD_LIBRARY_PATH in a deterministic order — # not an arbitrary glob[0] that breaks when a new triple is added. From 6823d66836a2b5ab258d403a8e885716c1f178b0 Mon Sep 17 00:00:00 2001 From: Sahil Faizal Date: Wed, 7 Oct 2026 21:24:22 -0700 Subject: [PATCH 2/2] Reuse existing environment test for ASan pytest preload --- .../tests/pytest_runner_test.py | 65 +++---------------- 1 file changed, 10 insertions(+), 55 deletions(-) diff --git a/build_tools/github_actions/tests/pytest_runner_test.py b/build_tools/github_actions/tests/pytest_runner_test.py index d490847fa5c..f80b806596b 100644 --- a/build_tools/github_actions/tests/pytest_runner_test.py +++ b/build_tools/github_actions/tests/pytest_runner_test.py @@ -261,66 +261,21 @@ def test_paths_and_ld_library_path(self): def test_prepends_to_existing_values(self): os.environ["PYTHONPATH"] = "/pre/existing" os.environ["LD_LIBRARY_PATH"] = "/pre/ld" + os.environ["LD_PRELOAD"] = "/pre/preload" rocm = Path("/opt/rocm") - env = pytest_runner.build_environment(rocm, "tensilelite") + with ( + mock.patch.object(pytest_runner, "is_asan", return_value=True), + mock.patch.object( + pytest_runner, "get_asan_runtime_path", return_value=Path("/asan.so") + ), + ): + env = pytest_runner.build_environment(rocm, "tensilelite") self.assertTrue(env["PYTHONPATH"].endswith("/pre/existing")) self.assertIn("/pre/ld", env["LD_LIBRARY_PATH"].split(os.pathsep)) - - def test_asan_runtime_precedes_existing_preload(self): - with tempfile.TemporaryDirectory() as tmp: - runtime = Path(tmp) / "libclang_rt.asan.so" - runtime.touch() - with mock.patch.dict( - os.environ, - { - "BUILD_VARIANT": "host-asan", - "ASAN_RUNTIME_PATH": str(runtime), - "LD_PRELOAD": "/existing/library.so", - "ASAN_OPTIONS": "detect_leaks=1", - }, - clear=True, - ): - env = pytest_runner.build_environment(Path(tmp), "tensilelite") - self.assertEqual( - env["LD_PRELOAD"], f"{runtime.resolve()}:/existing/library.so" - ) - self.assertEqual(env["ASAN_OPTIONS"], "detect_leaks=1") - - def test_sanitizer_preload_is_scoped_to_child_environment(self): - with tempfile.TemporaryDirectory() as tmp: - runtime = Path(tmp) / "libclang_rt.asan.so" - runtime.touch() - for variant in ("asan", "host-asan", "asan-debug", "host-asan-debug"): - with ( - self.subTest(variant=variant), - mock.patch.dict( - os.environ, - { - "BUILD_VARIANT": variant, - "ASAN_RUNTIME_PATH": str(runtime), - }, - clear=True, - ), - ): - parent_env = os.environ.copy() - env = pytest_runner.build_environment(Path(tmp), "tensilelite") - self.assertEqual(env["LD_PRELOAD"], str(runtime.resolve())) - self.assertEqual(dict(os.environ), parent_env) - - def test_release_does_not_resolve_or_change_preload(self): - with mock.patch.dict( - os.environ, - { - "BUILD_VARIANT": "release", - "ASAN_RUNTIME_PATH": "/missing/asan.so", - "LD_PRELOAD": "/existing/library.so", - }, - clear=True, - ): - env = pytest_runner.build_environment(Path("/opt/rocm"), "tensilelite") - self.assertEqual(env["LD_PRELOAD"], "/existing/library.so") + self.assertEqual(env["LD_PRELOAD"], f"{Path('/asan.so')}:/pre/preload") + self.assertEqual(os.environ["LD_PRELOAD"], "/pre/preload") def test_ld_library_path_includes_all_llvm_triple_dirs(self): # All triple subdirs must be on LD_LIBRARY_PATH in a deterministic order —