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

Kirill Stoimenov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 19 12:09:35 PDT 2021


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

Removed unused encodeMemToShadowInfo.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108377

Files:
  clang/test/CodeGen/asan-use-callbacks.cpp
  llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp


Index: llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -348,6 +348,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));
@@ -634,6 +638,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);
@@ -724,6 +729,7 @@
   bool UseAfterScope;
   AsanDetectStackUseAfterReturnMode UseAfterReturn;
   Type *IntptrTy;
+  Type *Int8PtrTy;
   ShadowMapping Mapping;
   FunctionCallee AsanHandleNoReturnFunc;
   FunctionCallee AsanPtrCmpFunction, AsanPtrSubFunction;
@@ -1753,12 +1759,21 @@
   size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
 
   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.getInt8Ty(), IsWrite),
+           ConstantInt::get(IRB.getInt8Ty(), AccessSizeIndex)});
+    } 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;
   }
 
Index: clang/test/CodeGen/asan-use-callbacks.cpp
===================================================================
--- clang/test/CodeGen/asan-use-callbacks.cpp
+++ clang/test/CodeGen/asan-use-callbacks.cpp
@@ -1,12 +1,18 @@
-// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -fsanitize=address \
-// RUN:     -o - %s \
+// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - \
+// RUN:     -fsanitize=address %s \
 // RUN:     | FileCheck %s --check-prefixes=CHECK-NO-OUTLINE
 // RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - \
 // RUN:     -fsanitize=address %s -fsanitize-address-outline-instrumentation \
 // RUN:     | FileCheck %s --check-prefixes=CHECK-OUTLINE
+// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - \
+// RUN:     -fsanitize=address %s -fsanitize-address-outline-instrumentation \
+// RUN:     -mllvm -asan-optimize-callbacks \
+// RUN:     | FileCheck %s --check-prefixes=CHECK-OPTIMIZED
+
 
 // CHECK-NO-OUTLINE-NOT: call{{.*}}@__asan_load4
 // CHECK-OUTLINE: call{{.*}}@__asan_load4
+// CHECK-OPTIMIZED: call{{.*}}@llvm.asan.check.memaccess(i8*{{.*}}, i64{{.*}}, i32{{.*}})
 
 int deref(int *p) {
   return *p;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108377.367585.patch
Type: text/x-patch
Size: 3648 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210819/9b18cad4/attachment.bin>


More information about the llvm-commits mailing list