]> granicus.if.org Git - llvm/commitdiff
hwasan: Move ctor into a comdat.
authorPeter Collingbourne <peter@pcc.me.uk>
Mon, 17 Dec 2018 22:56:34 +0000 (22:56 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Mon, 17 Dec 2018 22:56:34 +0000 (22:56 +0000)
Differential Revision: https://reviews.llvm.org/D55733

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@349413 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
test/Instrumentation/HWAddressSanitizer/basic.ll
test/Instrumentation/HWAddressSanitizer/with-calls.ll

index 91021604169a6936e642567291202fe15ec31b50..042e8ea0d2945fea576259ec523aa97442ef75b0 100644 (file)
@@ -309,15 +309,24 @@ bool HWAddressSanitizer::doInitialization(Module &M) {
                                             kHwasanInitName,
                                             /*InitArgTypes=*/{},
                                             /*InitArgs=*/{});
-    appendToGlobalCtors(M, HwasanCtorFunction, 0);
-  }
+    Comdat *CtorComdat = M.getOrInsertComdat(kHwasanModuleCtorName);
+    HwasanCtorFunction->setComdat(CtorComdat);
+    appendToGlobalCtors(M, HwasanCtorFunction, 0, HwasanCtorFunction);
+
+    // Create a zero-length global in __hwasan_frame so that the linker will
+    // always create start and stop symbols.
+    //
+    // N.B. If we ever start creating associated metadata in this pass this
+    // global will need to be associated with the ctor.
+    Type *Int8Arr0Ty = ArrayType::get(Int8Ty, 0);
+    auto GV =
+        new GlobalVariable(M, Int8Arr0Ty, /*isConstantGlobal*/ true,
+                           GlobalVariable::PrivateLinkage,
+                           Constant::getNullValue(Int8Arr0Ty), "__hwasan");
+    GV->setSection(getFrameSection());
+    GV->setComdat(CtorComdat);
+    appendToCompilerUsed(M, GV);
 
-  // Create a call to __hwasan_init_frames.
-  if (HwasanCtorFunction) {
-    // Create a dummy frame description for the CTOR function.
-    // W/o it we would have to create the call to __hwasan_init_frames after
-    // all functions are instrumented (i.e. need to have a ModulePass).
-    createFrameGlobal(*HwasanCtorFunction, "");
     IRBuilder<> IRBCtor(HwasanCtorFunction->getEntryBlock().getTerminator());
     IRBCtor.CreateCall(
         declareSanitizerInitFunction(M, "__hwasan_init_frames",
@@ -742,10 +751,9 @@ void HWAddressSanitizer::createFrameGlobal(Function &F,
   GV->setSection(getFrameSection());
   appendToCompilerUsed(M, GV);
   // Put GV into the F's Comadat so that if F is deleted GV can be deleted too.
-  if (&F != HwasanCtorFunction)
-    if (auto Comdat =
-            GetOrCreateFunctionComdat(F, TargetTriple, CurModuleUniqueId))
-      GV->setComdat(Comdat);
+  if (auto Comdat =
+          GetOrCreateFunctionComdat(F, TargetTriple, CurModuleUniqueId))
+    GV->setComdat(Comdat);
 }
 
 Value *HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB,
index 8253016d97b4adff5d0c2f7b4c55822045f3a127..e02e5fc283b8406ee03851b48d20827caebb00a3 100644 (file)
@@ -5,6 +5,9 @@
 ; RUN: opt < %s -hwasan -hwasan-recover=0 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=CHECK,ABORT,ZERO-BASED-SHADOW
 ; RUN: opt < %s -hwasan -hwasan-recover=1 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=CHECK,RECOVER,ZERO-BASED-SHADOW
 
+; CHECK: @llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 0, void ()* @hwasan.module_ctor, i8* bitcast (void ()* @hwasan.module_ctor to i8*) }]
+; CHECK: @__hwasan = private constant [0 x i8] zeroinitializer, section "__hwasan_frames", comdat($hwasan.module_ctor)
+
 target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
 target triple = "aarch64--linux-android"
 
@@ -352,7 +355,7 @@ entry:
 
 ; CHECK: declare void @__hwasan_init()
 
-; CHECK:      define internal void @hwasan.module_ctor() {
+; CHECK:      define internal void @hwasan.module_ctor() comdat {
 ; CHECK-NEXT:   call void @__hwasan_init()
 ; CHECK-NEXT:   call void @__hwasan_init_frames(
 ; CHECK-NEXT:   ret void
index 8d6068c343894f890c61d972bb7cfa64d7c09cbf..9f5bc66bd4d3b0e0be006cc3cb7621a87c0713ad 100644 (file)
@@ -197,7 +197,7 @@ entry:
 
 ; CHECK: declare void @__hwasan_init()
 
-; CHECK:      define internal void @hwasan.module_ctor() {
+; CHECK:      define internal void @hwasan.module_ctor() comdat {
 ; CHECK-NEXT:   call void @__hwasan_init()
 ; CHECK-NEXT:   call void @__hwasan_init_frames(
 ; CHECK-NEXT:   ret void