Skip to content

Commit

Permalink
[Driver] Link shared asan runtime lib with -z now on Solaris/x86
Browse files Browse the repository at this point in the history
As detailed in Issue #64126, several asan tests `FAIL` due to a cycle in
`AsanInitInternal`.  This can by avoided by disabling lazy binding with `ld
-z now`.

Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`.

Differential Revision: https://reviews.llvm.org/D156325
  • Loading branch information
rorth committed Jul 27, 2023
1 parent 2dcf051 commit 6b5149a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion clang/lib/Driver/ToolChains/Solaris.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,22 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("-lgcc");
CmdArgs.push_back("-lm");
}
const SanitizerArgs &SA = getToolChain().getSanitizerArgs(Args);
if (NeedsSanitizerDeps) {
linkSanitizerRuntimeDeps(getToolChain(), CmdArgs);

// Work around Solaris/amd64 ld bug when calling __tls_get_addr directly.
// However, ld -z relax=transtls is available since Solaris 11.2, but not
// in Illumos.
const SanitizerArgs &SA = getToolChain().getSanitizerArgs(Args);
if (getToolChain().getTriple().getArch() == llvm::Triple::x86_64 &&
(SA.needsAsanRt() || SA.needsStatsRt() ||
(SA.needsUbsanRt() && !SA.requiresMinimalRuntime())))
CmdArgs.push_back("-zrelax=transtls");
}
// Avoid AsanInitInternal cycle, Issue #64126.
if (getToolChain().getTriple().isX86() && SA.needsSharedRt() &&
SA.needsAsanRt())
CmdArgs.push_back("-znow");
}

if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
Expand Down
16 changes: 16 additions & 0 deletions clang/test/Driver/solaris-ld-sanitizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,19 @@
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_x86_tree \
// RUN: | FileCheck --check-prefix=CHECK-LD-X64-UBSAN %s
// CHECK-LD-X64-UBSAN: -zrelax=transtls

/// General tests that the ld -z now workaround is only applied on
/// Solaris/i386 with shared libclang_rt.asan.. Note that we use sysroot to
/// make these tests independent of the host system.

/// Check i386-pc-solaris2.11, 32bit, shared libclang_rt.asan
// RUN: %clang -fsanitize=address -shared-libasan --target=i386-pc-solaris2.11 %s -### 2>&1 \
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_x86_tree \
// RUN: | FileCheck --check-prefix=CHECK-LD-X32-ASAN-SHARED %s
// CHECK-LD-X32-ASAN-SHARED: -znow

/// Check i386-pc-solaris2.11, 32bit, static libclang_rt.asan
// RUN: %clang -fsanitize=address --target=i386-pc-solaris2.11 %s -### 2>&1 \
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_x86_tree \
// RUN: | FileCheck --check-prefix=CHECK-LD-X32-ASAN %s
// CHECK-LD-X32-ASAN-NOT: -znow

0 comments on commit 6b5149a

Please sign in to comment.