llvm-project/mlir/test/get_darwin_real_python.py
Kasper Nielsen beff2bacae
[MLIR, Python] Make it easy to run tests with ASan on mac (#115524)
There are two things that make running MLIR tests with ASan on a mac
tedious:

1. The `DYLD_INSERT_LIBRARIES` environment variable needs to be set to
point to `libclang_rt.asan_osx_dynamic.dylib`
2. Mac is wrapping Python, which means that the `DYLD_INSERT_LIBRARIES`
environment variable is not being respected in the Python tests. The
solution is to find and use a non-wrapped Python binary.

With the above two changes, ASan works out of the box on mac's by
setting the `-DLLVM_USE_SANITIZER=Address` cmake flag.

I have stolen most of the code in this PR from other LLVM projects. It
may be a good idea to reconcile it somewhere.
2024-11-23 01:34:03 +01:00

17 lines
774 B
Python

# On macOS, system python binaries like /usr/bin/python and $(xcrun -f python3)
# are shims. They do some light validation work and then spawn the "real" python
# binary. Find the "real" python by asking dyld -- sys.executable reports the
# wrong thing more often than not. This is also useful when we're running under
# a Homebrew python3 binary, which also appears to be some kind of shim.
def getDarwinRealPythonExecutable():
import ctypes
dyld = ctypes.cdll.LoadLibrary("/usr/lib/system/libdyld.dylib")
namelen = ctypes.c_ulong(1024)
name = ctypes.create_string_buffer(b"\000", namelen.value)
dyld._NSGetExecutablePath(ctypes.byref(name), ctypes.byref(namelen))
return name.value.decode("utf-8").strip()
print(getDarwinRealPythonExecutable())