r344699 - [PassManager/Sanitizer] Enable usage of ported AddressSanitizer passes with -fsanitize=address

Leonard Chan via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 17 08:38:22 PDT 2018


Author: leonardchan
Date: Wed Oct 17 08:38:22 2018
New Revision: 344699

URL: http://llvm.org/viewvc/llvm-project?rev=344699&view=rev
Log:
[PassManager/Sanitizer] Enable usage of ported AddressSanitizer passes with -fsanitize=address

Enable usage of `AddressSanitizer` and `AddressModuleSanitizer` ported from the
legacy to the new PassManager.

This patch depends on https://reviews.llvm.org/D52739.

Differential Revision: https://reviews.llvm.org/D52814

Added:
    cfe/trunk/test/CodeGen/asan-new-pm.ll
Modified:
    cfe/trunk/lib/CodeGen/BackendUtil.cpp

Modified: cfe/trunk/lib/CodeGen/BackendUtil.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/BackendUtil.cpp?rev=344699&r1=344698&r2=344699&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/BackendUtil.cpp (original)
+++ cfe/trunk/lib/CodeGen/BackendUtil.cpp Wed Oct 17 08:38:22 2018
@@ -52,6 +52,7 @@
 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
 #include "llvm/Transforms/InstCombine/InstCombine.h"
 #include "llvm/Transforms/Instrumentation.h"
+#include "llvm/Transforms/Instrumentation/AddressSanitizerPass.h"
 #include "llvm/Transforms/Instrumentation/BoundsChecking.h"
 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
 #include "llvm/Transforms/ObjCARC.h"
@@ -1022,6 +1023,16 @@ void EmitAssemblyHelper::EmitAssemblyWit
                                                CodeGenOpts.DebugPassManager);
       }
     }
+
+    if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
+      bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Address);
+      MPM.addPass(createModuleToFunctionPassAdaptor(
+          AddressSanitizerPass(/*CompileKernel=*/false, Recover,
+                               CodeGenOpts.SanitizeAddressUseAfterScope)));
+      bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
+      MPM.addPass(AddressSanitizerPass(/*CompileKernel=*/false, Recover,
+                                       ModuleUseAfterScope));
+    }
   }
 
   // FIXME: We still use the legacy pass manager to do code generation. We

Added: cfe/trunk/test/CodeGen/asan-new-pm.ll
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/asan-new-pm.ll?rev=344699&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/asan-new-pm.ll (added)
+++ cfe/trunk/test/CodeGen/asan-new-pm.ll Wed Oct 17 08:38:22 2018
@@ -0,0 +1,31 @@
+; RUN: %clang_cc1 -S -emit-llvm -o - -fexperimental-new-pass-manager -fsanitize=address %s | FileCheck %s
+
+; CHECK: @llvm.global_ctors = {{.*}}@asan.module_ctor
+
+define i32 @test_load(i32* %a) sanitize_address {
+entry:
+; CHECK:  %0 = ptrtoint i32* %a to i64
+; CHECK:  %1 = lshr i64 %0, 3
+; CHECK:  %2 = add i64 %1, 2147450880
+; CHECK:  %3 = inttoptr i64 %2 to i8*
+; CHECK:  %4 = load i8, i8* %3
+; CHECK:  %5 = icmp ne i8 %4, 0
+; CHECK:  br i1 %5, label %6, label %12, !prof !0
+
+; CHECK:; <label>:6:                                      ; preds = %entry
+; CHECK:  %7 = and i64 %0, 7
+; CHECK:  %8 = add i64 %7, 3
+; CHECK:  %9 = trunc i64 %8 to i8
+; CHECK:  %10 = icmp sge i8 %9, %4
+; CHECK:  br i1 %10, label %11, label %12
+
+; CHECK:; <label>:11:                                     ; preds = %6
+; CHECK:  call void @__asan_report_load4(i64 %0)
+; CHECK:  call void asm sideeffect "", ""()
+; CHECK:  unreachable
+
+; CHECK:; <label>:12:                                     ; preds = %6, %entry
+
+  %tmp1 = load i32, i32* %a, align 4
+  ret i32 %tmp1
+}




More information about the cfe-commits mailing list