Skip to content

Commit 81101de

Browse files
committedJan 16, 2019
[MSan] Apply the ctor creation scheme of TSan
Summary: To avoid adding an extern function to the global ctors list, apply the changes of D56538 also to MSan. Reviewers: chandlerc, vitalybuka, fedor.sergeev, leonardchan Subscribers: hiraditya, bollu, llvm-commits Differential Revision: https://reviews.llvm.org/D56734 llvm-svn: 351322
1 parent 1fe469a commit 81101de

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed
 

‎llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ static cl::opt<unsigned long long> ClOriginBase("msan-origin-base",
321321
cl::desc("Define custom MSan OriginBase"),
322322
cl::Hidden, cl::init(0));
323323

324+
static const char *const kMsanModuleCtorName = "msan.module_ctor";
324325
static const char *const kMsanInitName = "__msan_init";
325326

326327
namespace {
@@ -586,6 +587,8 @@ class MemorySanitizer {
586587

587588
/// An empty volatile inline asm that prevents callback merge.
588589
InlineAsm *EmptyAsm;
590+
591+
Function *MsanCtorFunction;
589592
};
590593

591594
/// A legacy function pass for msan instrumentation.
@@ -839,6 +842,8 @@ Value *MemorySanitizer::getKmsanShadowOriginAccessFn(bool isStore, int size) {
839842
}
840843

841844
/// Module-level initialization.
845+
///
846+
/// inserts a call to __msan_init to the module's constructor list.
842847
void MemorySanitizer::initializeModule(Module &M) {
843848
auto &DL = M.getDataLayout();
844849

@@ -913,7 +918,22 @@ void MemorySanitizer::initializeModule(Module &M) {
913918
OriginStoreWeights = MDBuilder(*C).createBranchWeights(1, 1000);
914919

915920
if (!CompileKernel) {
916-
getOrCreateInitFunction(M, kMsanInitName);
921+
std::tie(MsanCtorFunction, std::ignore) =
922+
getOrCreateSanitizerCtorAndInitFunctions(
923+
M, kMsanModuleCtorName, kMsanInitName,
924+
/*InitArgTypes=*/{},
925+
/*InitArgs=*/{},
926+
// This callback is invoked when the functions are created the first
927+
// time. Hook them into the global ctors list in that case:
928+
[&](Function *Ctor, Function *) {
929+
if (!ClWithComdat) {
930+
appendToGlobalCtors(M, Ctor, 0);
931+
return;
932+
}
933+
Comdat *MsanCtorComdat = M.getOrInsertComdat(kMsanModuleCtorName);
934+
Ctor->setComdat(MsanCtorComdat);
935+
appendToGlobalCtors(M, Ctor, 0, Ctor);
936+
});
917937

918938
if (TrackOrigins)
919939
M.getOrInsertGlobal("__msan_track_origins", IRB.getInt32Ty(), [&] {
@@ -4458,6 +4478,8 @@ static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
44584478
}
44594479

44604480
bool MemorySanitizer::sanitizeFunction(Function &F, TargetLibraryInfo &TLI) {
4481+
if (!CompileKernel && (&F == MsanCtorFunction))
4482+
return false;
44614483
MemorySanitizerVisitor Visitor(F, *this, TLI);
44624484

44634485
// Clear out readonly/readnone attributes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; MSan converts 2-element global_ctors to 3-element when adding the new entry.
2+
; RUN: opt < %s -msan-with-comdat -S -passes=msan 2>&1 | FileCheck %s
3+
; RUN: opt < %s -msan -msan-with-comdat -S | FileCheck %s
4+
5+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
6+
target triple = "x86_64-unknown-linux-gnu"
7+
8+
; CHECK: $msan.module_ctor = comdat any
9+
; CHECK: @llvm.global_ctors = appending global [2 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @f, i8* null }, { i32, void ()*, i8* } { i32 0, void ()* @msan.module_ctor, i8* bitcast (void ()* @msan.module_ctor to i8*) }]
10+
11+
@llvm.global_ctors = appending global [1 x { i32, void ()* }] [{ i32, void ()* } { i32 65535, void ()* @f }]
12+
13+
define internal void @f() {
14+
entry:
15+
ret void
16+
}
17+
18+
; CHECK: define internal void @msan.module_ctor() comdat {

‎llvm/test/Instrumentation/MemorySanitizer/msan_basic.ll

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
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-S128"
1010
target triple = "x86_64-unknown-linux-gnu"
1111

12-
; CHECK: @llvm.global_ctors {{.*}} { i32 0, void ()* @__msan_init, i8* null }
12+
; CHECK: @llvm.global_ctors {{.*}} { i32 0, void ()* @msan.module_ctor, i8* null }
1313

1414
; Check the presence and the linkage type of __msan_track_origins and
1515
; other interface symbols.
@@ -991,4 +991,5 @@ define i8* @MismatchingCallMustTailCall(i32 %a) sanitize_memory {
991991
; CHECK-NEXT: ret i8*
992992

993993

994-
; CHECK: declare void @__msan_init()
994+
; CHECK-LABEL: define internal void @msan.module_ctor() {
995+
; CHECK: call void @__msan_init()

‎llvm/test/Instrumentation/MemorySanitizer/msan_llvm_is_constant.ll

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
; Make sure MSan doesn't insert shadow checks for @llvm.is.constant.* arguments.
22

3+
; RUN: opt < %s -msan-kernel=1 -S -passes=msan 2>&1 | FileCheck \
4+
; RUN: -check-prefixes=CHECK %s
35
; RUN: opt < %s -msan -msan-kernel=1 -S | FileCheck -check-prefixes=CHECK %s
6+
; RUN: opt < %s -S -passes=msan 2>&1 | FileCheck -check-prefixes=CHECK %s
47
; RUN: opt < %s -msan -S | FileCheck -check-prefixes=CHECK %s
58

69
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"

0 commit comments

Comments
 (0)
Please sign in to comment.