Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit bb97c3d

Browse files
committed
[ubsan] Don't add a --dynamic-list for ubsan symbols when building a shared
library. That results in the linker resolving all references to weak symbols in the DSO to the definition from within that DSO. Ironically, this rarely causes observable problems, except that it causes ubsan's own dynamic type check to spuriously fail (because we fail to properly merge type_info object names). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@210220 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 028f7fd commit bb97c3d

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/Driver/Tools.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,17 +2033,23 @@ static void addLsanRT(const ToolChain &TC, const ArgList &Args,
20332033
static void addUbsanRT(const ToolChain &TC, const ArgList &Args,
20342034
ArgStringList &CmdArgs, bool IsCXX,
20352035
bool HasOtherSanitizerRt) {
2036+
// Export symbols if we're not building a shared library. This allows two
2037+
// models: either every DSO containing ubsan-sanitized code contains the
2038+
// ubsan runtime, or the main executable does (or both).
2039+
const bool ExportSymbols = !Args.hasArg(options::OPT_shared);
2040+
20362041
// Need a copy of sanitizer_common. This could come from another sanitizer
20372042
// runtime; if we're not including one, include our own copy.
20382043
if (!HasOtherSanitizerRt)
20392044
addSanitizerRTLinkFlags(TC, Args, CmdArgs, "san", true, false);
20402045

2041-
addSanitizerRTLinkFlags(TC, Args, CmdArgs, "ubsan", false);
2046+
addSanitizerRTLinkFlags(TC, Args, CmdArgs, "ubsan", false, ExportSymbols);
20422047

20432048
// Only include the bits of the runtime which need a C++ ABI library if
20442049
// we're linking in C++ mode.
20452050
if (IsCXX)
2046-
addSanitizerRTLinkFlags(TC, Args, CmdArgs, "ubsan_cxx", false);
2051+
addSanitizerRTLinkFlags(TC, Args, CmdArgs, "ubsan_cxx", false,
2052+
ExportSymbols);
20472053
}
20482054

20492055
static void addDfsanRT(const ToolChain &TC, const ArgList &Args,

test/Driver/sanitizer-ld.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,18 @@
189189

190190
// RUN: %clangxx -fsanitize=undefined %s -### -o %t.o 2>&1 \
191191
// RUN: -target i386-unknown-linux \
192+
// RUN: -resource-dir=%S/Inputs/resource_dir \
192193
// RUN: --sysroot=%S/Inputs/basic_linux_tree \
193194
// RUN: | FileCheck --check-prefix=CHECK-UBSAN-LINUX-CXX %s
194195
// CHECK-UBSAN-LINUX-CXX: "{{.*}}ld{{(.exe)?}}"
195196
// CHECK-UBSAN-LINUX-CXX-NOT: libclang_rt.asan
196197
// CHECK-UBSAN-LINUX-CXX: "-whole-archive" "{{.*}}libclang_rt.san-i386.a" "-no-whole-archive"
197198
// CHECK-UBSAN-LINUX-CXX-NOT: libclang_rt.asan
198199
// CHECK-UBSAN-LINUX-CXX: "-whole-archive" "{{.*}}libclang_rt.ubsan-i386.a" "-no-whole-archive"
200+
// CHECK-UBSAN-LINUX-CXX: "--dynamic-list={{.*}}libclang_rt.ubsan-i386.a.syms"
199201
// CHECK-UBSAN-LINUX-CXX: "-whole-archive" "{{.*}}libclang_rt.ubsan_cxx-i386.a" "-no-whole-archive"
200202
// CHECK-UBSAN-LINUX-CXX: "-lpthread"
203+
// CHECK-UBSAN-LINUX-CXX: "--dynamic-list={{.*}}libclang_rt.ubsan_cxx-i386.a.syms"
201204
// CHECK-UBSAN-LINUX-CXX: "-lstdc++"
202205

203206
// RUN: %clang -fsanitize=address,undefined %s -### -o %t.o 2>&1 \
@@ -228,11 +231,16 @@
228231

229232
// RUN: %clang -fsanitize=undefined %s -### -o %t.o 2>&1 \
230233
// RUN: -target i386-unknown-linux \
234+
// RUN: -resource-dir=%S/Inputs/resource_dir \
231235
// RUN: --sysroot=%S/Inputs/basic_linux_tree \
232236
// RUN: -shared \
233237
// RUN: | FileCheck --check-prefix=CHECK-UBSAN-LINUX-SHARED %s
234238
// CHECK-UBSAN-LINUX-SHARED: "{{.*}}ld{{(.exe)?}}"
239+
// CHECK-UBSAN-LINUX-SHARED-NOT: --export-dynamic
240+
// CHECK-UBSAN-LINUX-SHARED-NOT: --dynamic-list
235241
// CHECK-UBSAN-LINUX-SHARED: libclang_rt.ubsan-i386.a"
242+
// CHECK-UBSAN-LINUX-SHARED-NOT: --export-dynamic
243+
// CHECK-UBSAN-LINUX-SHARED-NOT: --dynamic-list
236244

237245
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
238246
// RUN: -target x86_64-unknown-linux -fsanitize=leak \

0 commit comments

Comments
 (0)