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

2018-10-02 Thread Leonard Chan via Phabricator via cfe-commits
leonardchan created this revision.
leonardchan added reviewers: mcgrathr, phosek, fedor.sergeev, philip.pfaffe, 
vitalybuka.
leonardchan added projects: clang, Sanitizers.
leonardchan added a dependency: D52739: [PassManager/Sanitizer] Port of 
AddresSanitizer pass from legacy to new PassManager.

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

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


Repository:
  rC Clang

https://reviews.llvm.org/D52814

Files:
  lib/CodeGen/BackendUtil.cpp
  test/CodeGen/asan-new-pm.ll


Index: test/CodeGen/asan-new-pm.ll
===
--- /dev/null
+++ test/CodeGen/asan-new-pm.ll
@@ -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:; :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:; :11: ; preds = %6
+; CHECK:  call void @__asan_report_load4(i64 %0)
+; CHECK:  call void asm sideeffect "", ""()
+; CHECK:  unreachable
+
+; CHECK:; :12: ; preds = %6, %entry
+
+  %tmp1 = load i32, i32* %a, align 4
+  ret i32 %tmp1
+}
Index: lib/CodeGen/BackendUtil.cpp
===
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -27,6 +27,7 @@
 #include "llvm/CodeGen/RegAllocRegistry.h"
 #include "llvm/CodeGen/SchedulerRegistry.h"
 #include "llvm/CodeGen/TargetSubtargetInfo.h"
+#include "llvm/IR/AddressSanitizerPass.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
@@ -902,6 +903,12 @@
   }
 }
 
+static FunctionPassManager buildAddressSanitizerPipeline(bool DebugLogging) {
+  FunctionPassManager FPM(DebugLogging);
+  FPM.addPass(AddressSanitizerPass());
+  return FPM;
+}
+
 /// A clean version of `EmitAssembly` that uses the new pass manager.
 ///
 /// Not all features are currently supported in this system, but where
@@ -1019,6 +1026,17 @@
CodeGenOpts.DebugPassManager);
   }
 }
+
+if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
+  MPM.addPass(AddressSanitizerModulePass());
+
+  // Add Function Pass
+  CGSCCPassManager MainCGPipeline(CodeGenOpts.DebugPassManager);
+  MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
+  buildAddressSanitizerPipeline(CodeGenOpts.DebugPassManager)));
+  MPM.addPass(
+  createModuleToPostOrderCGSCCPassAdaptor(std::move(MainCGPipeline)));
+}
   }
 
   // FIXME: We still use the legacy pass manager to do code generation. We


Index: test/CodeGen/asan-new-pm.ll
===
--- /dev/null
+++ test/CodeGen/asan-new-pm.ll
@@ -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:; :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:; :11: ; preds = %6
+; CHECK:  call void @__asan_report_load4(i64 %0)
+; CHECK:  call void asm sideeffect "", ""()
+; CHECK:  unreachable
+
+; CHECK:; :12: ; preds = %6, %entry
+
+  %tmp1 = load i32, i32* %a, align 4
+  ret i32 %tmp1
+}
Index: lib/CodeGen/BackendUtil.cpp
===
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -27,6 +27,7 @@
 #include "llvm/CodeGen/RegAllocRegistry.h"
 #include "llvm/CodeGen/SchedulerRegistry.h"
 #include "llvm/CodeGen/TargetSubtargetInfo.h"
+#include "llvm/IR/AddressSanitizerPass.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassMa

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

2018-10-04 Thread Philip Pfaffe via Phabricator via cfe-commits
philip.pfaffe added a comment.

Is this the right place in the pipeline to put the passes? The legacy PM 
inserts the asan passes at EP_OptimizerLast, why not do the same thing?


Repository:
  rC Clang

https://reviews.llvm.org/D52814



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2018-10-04 Thread Fedor Sergeev via Phabricator via cfe-commits
fedor.sergeev added inline comments.



Comment at: lib/CodeGen/BackendUtil.cpp:1031-1038
+  MPM.addPass(AddressSanitizerModulePass());
+
+  // Add Function Pass
+  CGSCCPassManager MainCGPipeline(CodeGenOpts.DebugPassManager);
+  MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
+  buildAddressSanitizerPipeline(CodeGenOpts.DebugPassManager)));
+  MPM.addPass(

I dont believe CGSCC is appropriate here.

I would expect to see a simple ModuleToFunction adapter, something like this:

MPM.addPass(AddressSanitizerModulePass());
MPM.addPass(createModuleToFunctionPassAdaptor(AddressSanitizerFunctionPass())

Also, it seems that legacy runs Function sanitizer first and then Module 
sanitizer after it.
This sequence does it backwards. Is it intentional?


Repository:
  rC Clang

https://reviews.llvm.org/D52814



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2018-10-04 Thread Leonard Chan via Phabricator via cfe-commits
leonardchan added a comment.

In https://reviews.llvm.org/D52814#1254901, @philip.pfaffe wrote:

> Is this the right place in the pipeline to put the passes? The legacy PM 
> inserts the asan passes at EP_OptimizerLast, why not do the same thing?


I noticed this also and thought adding this using 
`registerScalarOptimizerLateEPCallback` was the initial correct way to add this 
function pass, but it seems that the vector of callbacks this gets added to 
(`ScalarOptimizerLateEPCallbacks` in `buildFunctionSimplificationPipeline`) 
doesn't get called unless some sort of optimization is requested (that is I run 
with `-fexperimental-new-pass-manager -fsanitize=address -O1`) when the 
sanitizer should still be applied even without optimization.

We could force this pipeline to be run if we run 
`PB.buildPerModuleDefaultPipeline()`regardless of the optimization level, but I 
don't know why it's only called if optimization is zero.




Comment at: lib/CodeGen/BackendUtil.cpp:1031-1038
+  MPM.addPass(AddressSanitizerModulePass());
+
+  // Add Function Pass
+  CGSCCPassManager MainCGPipeline(CodeGenOpts.DebugPassManager);
+  MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
+  buildAddressSanitizerPipeline(CodeGenOpts.DebugPassManager)));
+  MPM.addPass(

fedor.sergeev wrote:
> I dont believe CGSCC is appropriate here.
> 
> I would expect to see a simple ModuleToFunction adapter, something like this:
> 
> MPM.addPass(AddressSanitizerModulePass());
> MPM.addPass(createModuleToFunctionPassAdaptor(AddressSanitizerFunctionPass())
> 
> Also, it seems that legacy runs Function sanitizer first and then Module 
> sanitizer after it.
> This sequence does it backwards. Is it intentional?
My bad. I didn't see there was a `createModuleToFunctionPassAdaptor` earlier 
and used CGSCC since that seemed to be how other function passes were added to 
the module.

I also wasn't sure if the order mattered since it seems both passes can be run 
independently of each other and provide different instrumentation. I think 
@vitalybuka might be able to answer that better, Regardless, I switched them 
still so Module runs after Function.


Repository:
  rC Clang

https://reviews.llvm.org/D52814



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2018-10-04 Thread Leonard Chan via Phabricator via cfe-commits
leonardchan updated this revision to Diff 168348.
leonardchan marked an inline comment as done.

Repository:
  rC Clang

https://reviews.llvm.org/D52814

Files:
  lib/CodeGen/BackendUtil.cpp
  test/CodeGen/asan-new-pm.ll


Index: test/CodeGen/asan-new-pm.ll
===
--- /dev/null
+++ test/CodeGen/asan-new-pm.ll
@@ -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:; :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:; :11: ; preds = %6
+; CHECK:  call void @__asan_report_load4(i64 %0)
+; CHECK:  call void asm sideeffect "", ""()
+; CHECK:  unreachable
+
+; CHECK:; :12: ; preds = %6, %entry
+
+  %tmp1 = load i32, i32* %a, align 4
+  ret i32 %tmp1
+}
Index: lib/CodeGen/BackendUtil.cpp
===
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -27,6 +27,7 @@
 #include "llvm/CodeGen/RegAllocRegistry.h"
 #include "llvm/CodeGen/SchedulerRegistry.h"
 #include "llvm/CodeGen/TargetSubtargetInfo.h"
+#include "llvm/IR/AddressSanitizerPass.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
@@ -1019,6 +1020,11 @@
CodeGenOpts.DebugPassManager);
   }
 }
+
+if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
+  MPM.addPass(createModuleToFunctionPassAdaptor(AddressSanitizerPass()));
+  MPM.addPass(AddressSanitizerModulePass());
+}
   }
 
   // FIXME: We still use the legacy pass manager to do code generation. We


Index: test/CodeGen/asan-new-pm.ll
===
--- /dev/null
+++ test/CodeGen/asan-new-pm.ll
@@ -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:; :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:; :11: ; preds = %6
+; CHECK:  call void @__asan_report_load4(i64 %0)
+; CHECK:  call void asm sideeffect "", ""()
+; CHECK:  unreachable
+
+; CHECK:; :12: ; preds = %6, %entry
+
+  %tmp1 = load i32, i32* %a, align 4
+  ret i32 %tmp1
+}
Index: lib/CodeGen/BackendUtil.cpp
===
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -27,6 +27,7 @@
 #include "llvm/CodeGen/RegAllocRegistry.h"
 #include "llvm/CodeGen/SchedulerRegistry.h"
 #include "llvm/CodeGen/TargetSubtargetInfo.h"
+#include "llvm/IR/AddressSanitizerPass.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
@@ -1019,6 +1020,11 @@
CodeGenOpts.DebugPassManager);
   }
 }
+
+if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
+  MPM.addPass(createModuleToFunctionPassAdaptor(AddressSanitizerPass()));
+  MPM.addPass(AddressSanitizerModulePass());
+}
   }
 
   // FIXME: We still use the legacy pass manager to do code generation. We
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2018-10-11 Thread Philip Pfaffe via Phabricator via cfe-commits
philip.pfaffe added a comment.

You're right, my bad; I missed the fact that it's EP_OptimizerLast, which has 
no equivalent in the new PM. Your implementation is actually correct here.

Did you run your test in a debug build?


Repository:
  rC Clang

https://reviews.llvm.org/D52814



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2018-10-12 Thread Leonard Chan via Phabricator via cfe-commits
leonardchan added a comment.

In https://reviews.llvm.org/D52814#1262550, @philip.pfaffe wrote:

> You're right, my bad; I missed the fact that it's EP_OptimizerLast, which has 
> no equivalent in the new PM. Your implementation is actually correct here.
>
> Did you run your test in a debug build?


Yes, and I tested it on release also in case there were any differences between 
either builds.


Repository:
  rC Clang

https://reviews.llvm.org/D52814



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2018-10-12 Thread Leonard Chan via Phabricator via cfe-commits
leonardchan updated this revision to Diff 169516.

Repository:
  rC Clang

https://reviews.llvm.org/D52814

Files:
  lib/CodeGen/BackendUtil.cpp
  test/CodeGen/asan-new-pm.ll


Index: test/CodeGen/asan-new-pm.ll
===
--- /dev/null
+++ test/CodeGen/asan-new-pm.ll
@@ -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:; :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:; :11: ; preds = %6
+; CHECK:  call void @__asan_report_load4(i64 %0)
+; CHECK:  call void asm sideeffect "", ""()
+; CHECK:  unreachable
+
+; CHECK:; :12: ; preds = %6, %entry
+
+  %tmp1 = load i32, i32* %a, align 4
+  ret i32 %tmp1
+}
Index: lib/CodeGen/BackendUtil.cpp
===
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -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 @@
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


Index: test/CodeGen/asan-new-pm.ll
===
--- /dev/null
+++ test/CodeGen/asan-new-pm.ll
@@ -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:; :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:; :11: ; preds = %6
+; CHECK:  call void @__asan_report_load4(i64 %0)
+; CHECK:  call void asm sideeffect "", ""()
+; CHECK:  unreachable
+
+; CHECK:; :12: ; preds = %6, %entry
+
+  %tmp1 = load i32, i32* %a, align 4
+  ret i32 %tmp1
+}
Index: lib/CodeGen/BackendUtil.cpp
===
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -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 @@
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 =

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

2018-10-16 Thread Leonard Chan via Phabricator via cfe-commits
leonardchan added a comment.

@philip.pfaffe @fedor.sergeev Do you have any more comments on this patch?


Repository:
  rC Clang

https://reviews.llvm.org/D52814



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2018-10-17 Thread Philip Pfaffe via Phabricator via cfe-commits
philip.pfaffe accepted this revision.
philip.pfaffe added a comment.
This revision is now accepted and ready to land.

LGTM, thank you!


Repository:
  rC Clang

https://reviews.llvm.org/D52814



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2018-10-17 Thread Fedor Sergeev via Phabricator via cfe-commits
fedor.sergeev accepted this revision.
fedor.sergeev added a comment.

LGTM.


Repository:
  rC Clang

https://reviews.llvm.org/D52814



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2018-10-17 Thread Leonard Chan via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL344699: [PassManager/Sanitizer] Enable usage of ported 
AddressSanitizer passes with… (authored by leonardchan, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D52814?vs=169516&id=170016#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D52814

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


Index: cfe/trunk/test/CodeGen/asan-new-pm.ll
===
--- cfe/trunk/test/CodeGen/asan-new-pm.ll
+++ cfe/trunk/test/CodeGen/asan-new-pm.ll
@@ -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:; :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:; :11: ; preds = %6
+; CHECK:  call void @__asan_report_load4(i64 %0)
+; CHECK:  call void asm sideeffect "", ""()
+; CHECK:  unreachable
+
+; CHECK:; :12: ; preds = %6, %entry
+
+  %tmp1 = load i32, i32* %a, align 4
+  ret i32 %tmp1
+}
Index: cfe/trunk/lib/CodeGen/BackendUtil.cpp
===
--- cfe/trunk/lib/CodeGen/BackendUtil.cpp
+++ cfe/trunk/lib/CodeGen/BackendUtil.cpp
@@ -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 @@
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


Index: cfe/trunk/test/CodeGen/asan-new-pm.ll
===
--- cfe/trunk/test/CodeGen/asan-new-pm.ll
+++ cfe/trunk/test/CodeGen/asan-new-pm.ll
@@ -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:; :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:; :11: ; preds = %6
+; CHECK:  call void @__asan_report_load4(i64 %0)
+; CHECK:  call void asm sideeffect "", ""()
+; CHECK:  unreachable
+
+; CHECK:; :12: ; preds = %6, %entry
+
+  %tmp1 = load i32, i32* %a, align 4
+  ret i32 %tmp1
+}
Index: cfe/trunk/lib/CodeGen/BackendUtil.cpp
===
--- cfe/trunk/lib/CodeGen/BackendUtil.cpp
+++ cfe/trunk/lib/CodeGen/BackendUtil.cpp
@@ -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