[PATCH] D101335: [ASan][Darwin] Fix `TestCases/Darwin/init_for_dlopen.cpp` when running in a standalone build.

Dan Liew via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 26 17:26:52 PDT 2021


delcypher created this revision.
delcypher added reviewers: yln, aralisza, kubamracek.
delcypher requested review of this revision.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.

When compiler-rt is configured for a standalone build the
`config.compiler_rt_libdir` path isn't correct because the Clang used
for testing won't use the ASan runtime from this path.

During testing it seems that this bug only actually matters on Darwin for
the `init_for_dlopen.cpp` test case. The test fails because
the `%shared_libasan` expands to a path that doesn't exist. It doesn't
exist in this particular scenario because the build hasn't been run.
Even if the build were run `%shared_libasan` would still point to the
wrong path.

To fix this we query clang during lit setup to find out where the
clang being used for testing will get its ASan runtime from instead
of using `config.compiler_rt_libdir`.

Although this change could be made for other platforms we only make
it for Darwin for now to avoid accidently breaking testing on other
platforms.

This change should have no effect for in tree builds (the common case)
because `config.compiler_rt_libdir` and the output for `clang
-print-file-name=lib` agree.

rdar://77182297O


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101335

Files:
  compiler-rt/test/asan/lit.cfg.py


Index: compiler-rt/test/asan/lit.cfg.py
===================================================================
--- compiler-rt/test/asan/lit.cfg.py
+++ compiler-rt/test/asan/lit.cfg.py
@@ -53,6 +53,41 @@
       (new_path, config.environment.get(dynamic_library_lookup_var, '')))
     config.environment[dynamic_library_lookup_var] = new_ld_library_path_64
 
+def find_clang_compiler_rt_libdir(config):
+  import subprocess
+  clang_cmd = [
+    config.clang.strip(),
+    '-print-file-name=lib',
+  ]
+  try:
+    result = subprocess.run(
+      clang_cmd,
+      stdout=subprocess.PIPE,
+      stderr=subprocess.PIPE,
+      check=True
+    )
+  except subprocess.CalledProcessError as e:
+    msg = 'Failed to run {}\nrc:{}\nstdout:{}\nstderr:{}\n'.format(
+        clang_cmd,
+        e.returncode,
+        e.stdout,
+        e.stderr)
+    lit_config.fatal(msg)
+  path = result.stdout.decode().strip()
+  if not os.path.exists(path):
+    lit_config.fatal('Path reported by clang does not exist: {}'.format(path))
+  path = os.path.realpath(path)
+  if config.host_os == 'Darwin':
+    path = os.path.join(path, 'darwin')
+
+  config_crt_libdir_real = os.path.realpath(config.compiler_rt_libdir)
+  if path != config_crt_libdir_real:
+    # Note it's possible for this mismatch to occur when doing a
+    # Standalone build of compiler-rt.
+    lit_config.warning('Path reported by clang ({}) != configured path ({})'.format(
+      path, config_crt_libdir_real))
+  return path
+
 # Setup config name.
 config.name = 'AddressSanitizer' + config.name_suffix
 
@@ -134,7 +169,9 @@
   if config.host_os in ['Linux', 'FreeBSD', 'NetBSD', 'SunOS']:
     shared_libasan_path = os.path.join(config.compiler_rt_libdir, "libclang_rt.asan{}.so".format(config.target_suffix))
   elif config.host_os == 'Darwin':
-    shared_libasan_path = os.path.join(config.compiler_rt_libdir, 'libclang_rt.asan_{}_dynamic.dylib'.format(config.apple_platform))
+    shared_libasan_path = os.path.join(
+      find_clang_compiler_rt_libdir(config),
+      'libclang_rt.asan_{}_dynamic.dylib'.format(config.apple_platform))
   else:
     lit_config.warning('%shared_libasan substitution not set but dynamic ASan is available.')
     shared_libasan_path = None


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101335.340698.patch
Type: text/x-patch
Size: 2236 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210427/67f42df3/attachment-0001.bin>


More information about the llvm-commits mailing list