Fix ASAN execution for the MLIR Python tests
authorMehdi Amini <joker.eph@gmail.com>
Sun, 3 Oct 2021 01:24:07 +0000 (01:24 +0000)
committerMehdi Amini <joker.eph@gmail.com>
Sun, 3 Oct 2021 05:07:32 +0000 (05:07 +0000)
First the leak sanitizer has to be disabled, as even an empty script
leads to leak detection with Python.
Then we need to preload the ASAN runtime, as the main binary (python)
won't be linked against it. This will only work on Linux right now.

Differential Revision: https://reviews.llvm.org/D111004

mlir/test/lit.cfg.py
mlir/test/python/lit.local.cfg

index f6b8be3..2307192 100644 (file)
@@ -68,7 +68,6 @@ tools = [
 
 # The following tools are optional
 tools.extend([
-    ToolSubst('%PYTHON', config.python_executable, unresolved='ignore'),
     ToolSubst('toy-ch1', unresolved='ignore'),
     ToolSubst('toy-ch2', unresolved='ignore'),
     ToolSubst('toy-ch3', unresolved='ignore'),
@@ -80,6 +79,16 @@ tools.extend([
     ToolSubst('%vulkan_wrapper_library_dir', config.vulkan_wrapper_library_dir, unresolved='ignore'),
     ToolSubst('%mlir_integration_test_dir', config.mlir_integration_test_dir, unresolved='ignore'),
 ])
+
+python_executable = config.python_executable
+# 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:
+  python_executable = f"LD_PRELOAD=$({config.host_cxx} -print-file-name=libclang_rt.asan-{config.host_arch}.so) {config.python_executable}"
+tools.extend([
+  ToolSubst('%PYTHON', python_executable, unresolved='ignore'),
+])
+
 llvm_config.add_tool_substitutions(tools, tool_dirs)
 
 
index fc4c987..8a98474 100644 (file)
@@ -1,8 +1,4 @@
-# Disable with sanitizers for now, this require some more setup apparently.
-for san in ['asan', 'msan', 'ubsan']:
-   if (san in config.available_features):
-      config.unsupported = True
-
+config.environment['ASAN_OPTIONS'] = 'detect_leaks=0'
 if not config.enable_bindings_python:
   config.unsupported = True
 config.excludes.add('python_test_ops.td')