[PATCH] D108377: [asan] Implemented flag to emit intrinsics to optimize ASan callbacks.

Kirill Stoimenov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 24 12:41:14 PDT 2021


kstoimenov updated this revision to Diff 368432.
kstoimenov added a comment.

Updating after pushing D107850 <https://reviews.llvm.org/D107850>.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108377/new/

https://reviews.llvm.org/D108377

Files:
  llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
  llvm/test/Instrumentation/AddressSanitizer/asan-optimize-callbacks.ll


Index: llvm/test/Instrumentation/AddressSanitizer/asan-optimize-callbacks.ll
===================================================================
--- /dev/null
+++ llvm/test/Instrumentation/AddressSanitizer/asan-optimize-callbacks.ll
@@ -0,0 +1,14 @@
+; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -S | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @load1(i8* %p1, i16* %p2, i32* %p4, i64* %p8, i128* %p16)
+  sanitize_address {
+  %n1 = load i8, i8* %p1
+  %n2 = load i16, i16* %p2
+  %n4 = load i32, i32* %p4
+  %n8 = load i64, i64* %p8
+  %n16 = load i128, i128* %p16
+;  store i32 0, i32* %p4, align 8
+  ret void
+}
Index: llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -356,6 +356,10 @@
 static cl::opt<bool> ClOpt("asan-opt", cl::desc("Optimize instrumentation"),
                            cl::Hidden, cl::init(true));
 
+static cl::opt<bool> ClOptimizeCallbacks("asan-optimize-callbacks",
+                                         cl::desc("Optimize callbacks"),
+                                         cl::Hidden, cl::init(false));
+
 static cl::opt<bool> ClOptSameTemp(
     "asan-opt-same-temp", cl::desc("Instrument the same temp just once"),
     cl::Hidden, cl::init(true));
@@ -657,6 +661,7 @@
     C = &(M.getContext());
     LongSize = M.getDataLayout().getPointerSizeInBits();
     IntptrTy = Type::getIntNTy(*C, LongSize);
+    Int8PtrTy = Type::getInt8PtrTy(*C);
     TargetTriple = Triple(M.getTargetTriple());
 
     Mapping = getShadowMapping(TargetTriple, LongSize, this->CompileKernel);
@@ -747,6 +752,7 @@
   bool UseAfterScope;
   AsanDetectStackUseAfterReturnMode UseAfterReturn;
   Type *IntptrTy;
+  Type *Int8PtrTy;
   ShadowMapping Mapping;
   FunctionCallee AsanHandleNoReturnFunc;
   FunctionCallee AsanPtrCmpFunction, AsanPtrSubFunction;
@@ -1768,14 +1774,23 @@
   IRBuilder<> IRB(InsertBefore);
   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
   size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
+  const ASanAccessInfo AccessInfo(IsWrite, CompileKernel, AccessSizeIndex);
 
   if (UseCalls) {
-    if (Exp == 0)
-      IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][0][AccessSizeIndex],
-                     AddrLong);
-    else
-      IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][1][AccessSizeIndex],
-                     {AddrLong, ConstantInt::get(IRB.getInt32Ty(), Exp)});
+    if (ClOptimizeCallbacks) {
+      Value *Ptr8 = IRB.CreatePointerCast(Addr, Int8PtrTy);
+      Module *M = IRB.GetInsertBlock()->getParent()->getParent();
+      IRB.CreateCall(
+          Intrinsic::getDeclaration(M, Intrinsic::asan_check_memaccess),
+          {Ptr8, ConstantInt::get(IRB.getInt32Ty(), AccessInfo.Packed)});
+    } else {
+      if (Exp == 0)
+        IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][0][AccessSizeIndex],
+                       AddrLong);
+      else
+        IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][1][AccessSizeIndex],
+                       {AddrLong, ConstantInt::get(IRB.getInt32Ty(), Exp)});
+    }
     return;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108377.368432.patch
Type: text/x-patch
Size: 3257 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210824/bbee02e8/attachment.bin>


More information about the cfe-commits mailing list