[llvm] 775a948 - [IR][sanitizer] Set nounwind on module ctor/dtor, additionally set uwtable if -fasynchronous-unwind-tables
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 21 15:58:27 PDT 2021
Author: Fangrui Song
Date: 2021-04-21T15:58:20-07:00
New Revision: 775a9483e55efb365691bc425ed107568ad6daf8
URL: https://github.com/llvm/llvm-project/commit/775a9483e55efb365691bc425ed107568ad6daf8
DIFF: https://github.com/llvm/llvm-project/commit/775a9483e55efb365691bc425ed107568ad6daf8.diff
LOG: [IR][sanitizer] Set nounwind on module ctor/dtor, additionally set uwtable if -fasynchronous-unwind-tables
On ELF targets, if a function has uwtable or personality, or does not have
nounwind (`needsUnwindTableEntry`), it marks that `.eh_frame` is needed in the module.
Then, a function gets `.eh_frame` if `needsUnwindTableEntry` or `-g[123]` is specified.
(i.e. If -g[123], every function gets `.eh_frame`.
This behavior is strange but that is the status quo on GCC and Clang.)
Let's take asan as an example. Other sanitizers are similar.
`asan.module_[cd]tor` has no attribute. `needsUnwindTableEntry` returns true,
so every function gets `.eh_frame` if `-g[123]` is specified.
This is the root cause that
`-fno-exceptions -fno-asynchronous-unwind-tables -g` produces .debug_frame
while
`-fno-exceptions -fno-asynchronous-unwind-tables -g -fsanitize=address` produces .eh_frame.
This patch
* sets the nounwind attribute on sanitizer module ctor/dtor.
* let Clang emit a module flag metadata "uwtable" for -fasynchronous-unwind-tables. If "uwtable" is set, sanitizer module ctor/dtor additionally get the uwtable attribute.
The "uwtable" mechanism is generic: synthesized functions not cloned/specialized
from existing ones should consider `Function::createWithDefaultAttr` instead of
`Function::create` if they want to get some default attributes which
have more of module semantics.
Other candidates: "frame-pointer" (https://github.com/ClangBuiltLinux/linux/issues/955
https://github.com/ClangBuiltLinux/linux/issues/1238), dso_local, etc.
Differential Revision: https://reviews.llvm.org/D100251
Added:
llvm/test/Instrumentation/AddressSanitizer/uwtable.ll
Modified:
clang/lib/CodeGen/CodeGenModule.cpp
clang/test/CodeGen/asan-globals.cpp
clang/test/CodeGen/asan-new-pm.ll
clang/test/CodeGen/asan-no-globals-no-comdat.cpp
llvm/include/llvm/IR/Function.h
llvm/include/llvm/IR/Module.h
llvm/lib/IR/Function.cpp
llvm/lib/IR/Module.cpp
llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
llvm/lib/Transforms/Utils/ModuleUtils.cpp
llvm/test/Instrumentation/AddressSanitizer/basic.ll
llvm/test/Instrumentation/AddressSanitizer/no-globals.ll
llvm/test/Instrumentation/HWAddressSanitizer/basic.ll
llvm/test/Instrumentation/HWAddressSanitizer/with-calls.ll
llvm/test/Instrumentation/MemorySanitizer/msan_basic.ll
llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard-inline-8bit-counters.ll
llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard-inline-bool-flag.ll
llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard.ll
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index a5229261bea7b..6b966e7ca1338 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -741,6 +741,8 @@ void CodeGenModule::Release() {
if (CodeGenOpts.NoPLT)
getModule().setRtLibUseGOT();
+ if (CodeGenOpts.UnwindTables)
+ getModule().setUwtable();
SimplifyPersonality();
diff --git a/clang/test/CodeGen/asan-globals.cpp b/clang/test/CodeGen/asan-globals.cpp
index cb6f2174c6a5c..f49a180c5868f 100644
--- a/clang/test/CodeGen/asan-globals.cpp
+++ b/clang/test/CodeGen/asan-globals.cpp
@@ -27,7 +27,8 @@ void func() {
// ASAN: @__special_global{{.*}} global { i32, [60 x i8] }{{.*}}, align 32
// KASAN: @__special_global{{.*}} global i32
-// CHECK-LABEL: define internal void @asan.module_ctor
+/// Without -fasynchronous-unwind-tables, ctor and dtor get the uwtable attribute.
+// CHECK-LABEL: define internal void @asan.module_ctor() #[[#ATTR:]] {
// ASAN-NEXT: call void @__asan_init
// ASAN-NEXT: call void @__asan_version_mismatch_check
// KASAN-NOT: call void @__asan_init
@@ -36,10 +37,19 @@ void func() {
// KASAN-NEXT: call void @__asan_register_globals({{.*}}, i{{32|64}} 5)
// CHECK-NEXT: ret void
-// CHECK-LABEL: define internal void @asan.module_dtor
+// CHECK: define internal void @asan.module_dtor() #[[#ATTR]] {
// CHECK-NEXT: call void @__asan_unregister_globals
// CHECK-NEXT: ret void
+// CHECK: attributes #[[#ATTR]] = { nounwind }
+
+/// If -fasynchronous-unwind-tables, set the module flag "uwtable". ctor/dtor
+/// will thus get the uwtable attribute.
+// RUN: %clang_cc1 -emit-llvm -fsanitize=address -munwind-tables -o - %s | FileCheck %s --check-prefixes=UWTABLE
+// UWTABLE: define internal void @asan.module_dtor() #[[#ATTR:]] {
+// UWTABLE: attributes #[[#ATTR]] = { nounwind uwtable }
+// UWTABLE: ![[#]] = !{i32 7, !"uwtable", i32 1}
+
// CHECK: !llvm.asan.globals = !{![[EXTRA_GLOBAL:[0-9]+]], ![[GLOBAL:[0-9]+]], ![[DYN_INIT_GLOBAL:[0-9]+]], ![[ATTR_GLOBAL:[0-9]+]], ![[BLACKLISTED_GLOBAL:[0-9]+]], ![[SECTIONED_GLOBAL:[0-9]+]], ![[SPECIAL_GLOBAL:[0-9]+]], ![[STATIC_VAR:[0-9]+]], ![[LITERAL:[0-9]+]]}
// CHECK: ![[EXTRA_GLOBAL]] = !{{{.*}} ![[EXTRA_GLOBAL_LOC:[0-9]+]], !"extra_global", i1 false, i1 false}
// CHECK: ![[EXTRA_GLOBAL_LOC]] = !{!"{{.*}}extra-source.cpp", i32 1, i32 5}
diff --git a/clang/test/CodeGen/asan-new-pm.ll b/clang/test/CodeGen/asan-new-pm.ll
index fd840c4e7d10d..e41bb58b232be 100644
--- a/clang/test/CodeGen/asan-new-pm.ll
+++ b/clang/test/CodeGen/asan-new-pm.ll
@@ -12,7 +12,7 @@ entry:
; CHECK: __asan_init
-; CHECK-DAG: define internal void @asan.module_ctor() {
+; CHECK-DAG: define internal void @asan.module_ctor() #[[#]] {
; CHECK: {{.*}} call void @__asan_init()
; CHECK: {{.*}} call void @__asan_version_mismatch_check_v8()
; CHECK: ret void
diff --git a/clang/test/CodeGen/asan-no-globals-no-comdat.cpp b/clang/test/CodeGen/asan-no-globals-no-comdat.cpp
index 4637346f9c417..b7a6b16159a5f 100644
--- a/clang/test/CodeGen/asan-no-globals-no-comdat.cpp
+++ b/clang/test/CodeGen/asan-no-globals-no-comdat.cpp
@@ -7,5 +7,5 @@
// RUN: %clang_cc1 -fsanitize=address -fsanitize-address-globals-dead-stripping -fno-integrated-as -emit-llvm -o - -triple x86_64-linux %s | FileCheck %s --check-prefix=WITHOUT-GC
// RUN: %clang_cc1 -fsanitize=address -fdata-sections -emit-llvm -o - -triple x86_64-linux %s | FileCheck %s --check-prefix=WITHOUT-GC
-// WITH-GC: define internal void @asan.module_ctor() comdat {
-// WITHOUT-GC: define internal void @asan.module_ctor() {
+// WITH-GC: define internal void @asan.module_ctor() #[[#]] comdat {
+// WITHOUT-GC: define internal void @asan.module_ctor() #[[#]] {
diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h
index 88e26705e9650..8bc5e12a5bc8c 100644
--- a/llvm/include/llvm/IR/Function.h
+++ b/llvm/include/llvm/IR/Function.h
@@ -153,6 +153,11 @@ class Function : public GlobalObject, public ilist_node<Function> {
static Function *Create(FunctionType *Ty, LinkageTypes Linkage,
const Twine &N, Module &M);
+ static Function *createWithDefaultAttr(FunctionType *Ty, LinkageTypes Linkage,
+ unsigned AddrSpace,
+ const Twine &N = "",
+ Module *M = nullptr);
+
// Provide fast operand accessors.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h
index 9bd8aa95a3a04..a4ad9d3f28e2d 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -886,6 +886,10 @@ class Module {
/// Set that PLT should be avoid for RTLib calls.
void setRtLibUseGOT();
+ /// Get/set whether synthesized functions should get the uwtable attribute.
+ bool getUwtable() const;
+ void setUwtable();
+
/// @name Utility functions for querying and setting the build SDK version
/// @{
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index 73fd32cedbdb0..32c7b876115ec 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -330,6 +330,16 @@ Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage,
return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M);
}
+Function *Function::createWithDefaultAttr(FunctionType *Ty,
+ LinkageTypes Linkage,
+ unsigned AddrSpace, const Twine &N,
+ Module *M) {
+ auto *F = new Function(Ty, Linkage, AddrSpace, N, M);
+ if (M->getUwtable())
+ F->addAttribute(AttributeList::FunctionIndex, Attribute::UWTable);
+ return F;
+}
+
void Function::removeFromParent() {
getParent()->getFunctionList().remove(getIterator());
}
diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp
index 41666e8274d42..b5108f7a3ebe9 100644
--- a/llvm/lib/IR/Module.cpp
+++ b/llvm/lib/IR/Module.cpp
@@ -669,6 +669,13 @@ void Module::setRtLibUseGOT() {
addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1);
}
+bool Module::getUwtable() const {
+ auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("uwtable"));
+ return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0);
+}
+
+void Module::setUwtable() { addModuleFlag(ModFlagBehavior::Max, "uwtable", 1); }
+
void Module::setSDKVersion(const VersionTuple &V) {
SmallVector<unsigned, 3> Entries;
Entries.push_back(V.getMajor());
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index 94e43c57b5da6..cb48da63275c4 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -2081,9 +2081,11 @@ ModuleAddressSanitizer::CreateMetadataGlobal(Module &M, Constant *Initializer,
}
Instruction *ModuleAddressSanitizer::CreateAsanModuleDtor(Module &M) {
- AsanDtorFunction =
- Function::Create(FunctionType::get(Type::getVoidTy(*C), false),
- GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
+ AsanDtorFunction = Function::createWithDefaultAttr(
+ FunctionType::get(Type::getVoidTy(*C), false),
+ GlobalValue::InternalLinkage, 0, kAsanModuleDtorName, &M);
+ AsanDtorFunction->addAttribute(AttributeList::FunctionIndex,
+ Attribute::NoUnwind);
BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
return ReturnInst::Create(*C, AsanDtorBB);
diff --git a/llvm/lib/Transforms/Utils/ModuleUtils.cpp b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
index 74d3239847f7d..4ad9837a0d35f 100644
--- a/llvm/lib/Transforms/Utils/ModuleUtils.cpp
+++ b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
@@ -122,9 +122,10 @@ llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
}
Function *llvm::createSanitizerCtor(Module &M, StringRef CtorName) {
- Function *Ctor = Function::Create(
+ Function *Ctor = Function::createWithDefaultAttr(
FunctionType::get(Type::getVoidTy(M.getContext()), false),
- GlobalValue::InternalLinkage, CtorName, &M);
+ GlobalValue::InternalLinkage, 0, CtorName, &M);
+ Ctor->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
ReturnInst::Create(M.getContext(), CtorBB);
return Ctor;
diff --git a/llvm/test/Instrumentation/AddressSanitizer/basic.ll b/llvm/test/Instrumentation/AddressSanitizer/basic.ll
index 32462bb730c6a..41ab493a0d1c2 100644
--- a/llvm/test/Instrumentation/AddressSanitizer/basic.ll
+++ b/llvm/test/Instrumentation/AddressSanitizer/basic.ll
@@ -225,8 +225,12 @@ define void @test_swifterror_3() sanitize_address {
ret void
}
-; CHECK: define internal void @asan.module_ctor()
+;; ctor/dtor have the nounwind attribute. See uwtable.ll, they additionally have
+;; the uwtable attribute with the module flag "uwtable".
+; CHECK: define internal void @asan.module_ctor() #[[#ATTR:]] {{(comdat )?}}{
; CHECK: call void @__asan_init()
+; CHECK: attributes #[[#ATTR]] = { nounwind }
+
; PROF
; CHECK: ![[PROF]] = !{!"branch_weights", i32 1, i32 100000}
diff --git a/llvm/test/Instrumentation/AddressSanitizer/no-globals.ll b/llvm/test/Instrumentation/AddressSanitizer/no-globals.ll
index ea84ac387a71f..f329da6cb5eb2 100644
--- a/llvm/test/Instrumentation/AddressSanitizer/no-globals.ll
+++ b/llvm/test/Instrumentation/AddressSanitizer/no-globals.ll
@@ -9,5 +9,5 @@ define void @f() {
; CHECK-NOT: @llvm.global_dtors
; CHECK: @llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 1, void ()* @asan.module_ctor, i8* bitcast (void ()* @asan.module_ctor to i8*) }]
; CHECK-NOT: @llvm.global_dtors
-; CHECK: define internal void @asan.module_ctor() comdat
+; CHECK: define internal void @asan.module_ctor() #[[#]] comdat
; CHECK-NOT: @llvm.global_dtors
diff --git a/llvm/test/Instrumentation/AddressSanitizer/uwtable.ll b/llvm/test/Instrumentation/AddressSanitizer/uwtable.ll
new file mode 100644
index 0000000000000..09b7c3608c5ee
--- /dev/null
+++ b/llvm/test/Instrumentation/AddressSanitizer/uwtable.ll
@@ -0,0 +1,21 @@
+; RUN: opt < %s -passes='asan-pipeline' -S | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+ at g = dso_local global i32 0, align 4
+
+define i32 @test_load() sanitize_address {
+entry:
+ %tmp = load i32, i32* @g, align 4
+ ret i32 %tmp
+}
+
+!llvm.module.flags = !{!0}
+
+;; Due to -fasynchronous-unwind-tables.
+!0 = !{i32 7, !"uwtable", i32 1}
+
+;; Set the uwtable attribute on ctor/dtor.
+; CHECK: define internal void @asan.module_ctor() #[[#ATTR:]]
+; CHECK: define internal void @asan.module_dtor() #[[#ATTR]]
+; CHECK: attributes #[[#ATTR]] = { nounwind uwtable }
diff --git a/llvm/test/Instrumentation/HWAddressSanitizer/basic.ll b/llvm/test/Instrumentation/HWAddressSanitizer/basic.ll
index e139e3c9cbafd..7307ca200e273 100644
--- a/llvm/test/Instrumentation/HWAddressSanitizer/basic.ll
+++ b/llvm/test/Instrumentation/HWAddressSanitizer/basic.ll
@@ -406,7 +406,9 @@ entry:
; CHECK: declare void @__hwasan_init()
-; CHECK: define internal void @hwasan.module_ctor() comdat {
+; CHECK: define internal void @hwasan.module_ctor() #[[#ATTR:]] comdat {
; CHECK-NEXT: call void @__hwasan_init()
; CHECK-NEXT: ret void
; CHECK-NEXT: }
+
+; CHECK: attributes #[[#ATTR]] = { nounwind }
diff --git a/llvm/test/Instrumentation/HWAddressSanitizer/with-calls.ll b/llvm/test/Instrumentation/HWAddressSanitizer/with-calls.ll
index f660fe25d9cc6..e0e7077ae9416 100644
--- a/llvm/test/Instrumentation/HWAddressSanitizer/with-calls.ll
+++ b/llvm/test/Instrumentation/HWAddressSanitizer/with-calls.ll
@@ -197,7 +197,7 @@ entry:
; CHECK: declare void @__hwasan_init()
-; CHECK: define internal void @hwasan.module_ctor() comdat {
+; CHECK: define internal void @hwasan.module_ctor() #[[#]] comdat {
; CHECK-NEXT: call void @__hwasan_init()
; CHECK-NEXT: ret void
; CHECK-NEXT: }
diff --git a/llvm/test/Instrumentation/MemorySanitizer/msan_basic.ll b/llvm/test/Instrumentation/MemorySanitizer/msan_basic.ll
index 6a33b398ef6e1..e9afb0febe3f6 100644
--- a/llvm/test/Instrumentation/MemorySanitizer/msan_basic.ll
+++ b/llvm/test/Instrumentation/MemorySanitizer/msan_basic.ll
@@ -1013,7 +1013,7 @@ define i8* @MismatchingCallMustTailCall(i32 %a) sanitize_memory {
; CHECK-NEXT: ret i8*
-; CHECK-LABEL: define internal void @msan.module_ctor() {
+; CHECK-LABEL: define internal void @msan.module_ctor() #[[#ATTR:]] {
; CHECK: call void @__msan_init()
; CHECK-CALLS: declare void @__msan_maybe_warning_1(i8 zeroext, i32 zeroext)
@@ -1024,3 +1024,5 @@ define i8* @MismatchingCallMustTailCall(i32 %a) sanitize_memory {
; CHECK-CALLS: declare void @__msan_maybe_store_origin_4(i32 zeroext, i8*, i32 zeroext)
; CHECK-CALLS: declare void @__msan_maybe_warning_8(i64 zeroext, i32 zeroext)
; CHECK-CALLS: declare void @__msan_maybe_store_origin_8(i64 zeroext, i8*, i32 zeroext)
+
+; CHECK: attributes #[[#ATTR]] = { nounwind }
diff --git a/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard-inline-8bit-counters.ll b/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard-inline-8bit-counters.ll
index 2cce6e76f4f38..0ccf158bb2fa6 100644
--- a/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard-inline-8bit-counters.ll
+++ b/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard-inline-8bit-counters.ll
@@ -4,8 +4,8 @@
; Module ctors should have stable names across modules, not something like
; @sancov.module_ctor.3 that may cause duplicate ctors after linked together.
-; CHECK: define internal void @sancov.module_ctor_trace_pc_guard() comdat {
-; CHECK: define internal void @sancov.module_ctor_8bit_counters() comdat {
+; CHECK: define internal void @sancov.module_ctor_trace_pc_guard() #[[#]] comdat {
+; CHECK: define internal void @sancov.module_ctor_8bit_counters() #[[#]] comdat {
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-unknown-linux-gnu"
diff --git a/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard-inline-bool-flag.ll b/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard-inline-bool-flag.ll
index 14198c77d90b2..5c807bc09c5b4 100644
--- a/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard-inline-bool-flag.ll
+++ b/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard-inline-bool-flag.ll
@@ -4,8 +4,8 @@
; Module ctors should have stable names across modules, not something like
; @sancov.module_ctor.3 that may cause duplicate ctors after linked together.
-; CHECK: define internal void @sancov.module_ctor_trace_pc_guard() comdat {
-; CHECK: define internal void @sancov.module_ctor_bool_flag() comdat {
+; CHECK: define internal void @sancov.module_ctor_trace_pc_guard() #[[#]] comdat {
+; CHECK: define internal void @sancov.module_ctor_bool_flag() #[[#]] comdat {
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-unknown-linux-gnu"
diff --git a/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard.ll b/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard.ll
index 6dbbbff4ef6fb..4e21374794f59 100644
--- a/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard.ll
+++ b/llvm/test/Instrumentation/SanitizerCoverage/trace-pc-guard.ll
@@ -60,5 +60,7 @@ entry:
ret void
}
-; ELF-LABEL: define internal void @sancov.module_ctor_trace_pc_guard() comdat {
-; MACHO-LABEL: define internal void @sancov.module_ctor_trace_pc_guard() {
+; ELF-LABEL: define internal void @sancov.module_ctor_trace_pc_guard() #2 comdat {
+; MACHO-LABEL: define internal void @sancov.module_ctor_trace_pc_guard() #2 {
+
+; CHECK: attributes #2 = { nounwind }
More information about the llvm-commits
mailing list