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..f80b806596b 100644 --- a/build_tools/github_actions/tests/pytest_runner_test.py +++ b/build_tools/github_actions/tests/pytest_runner_test.py @@ -261,12 +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)) + 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 —