[compiler-rt] d7a47a9 - Desist from passing function location to __msan_set_alloca_origin4.

Kevin Athey via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 10 09:02:58 PDT 2022


Author: Kevin Athey
Date: 2022-08-10T09:02:53-07:00
New Revision: d7a47a9bb5bd9f9b292e4f11184942936e2b1fc0

URL: https://github.com/llvm/llvm-project/commit/d7a47a9bb5bd9f9b292e4f11184942936e2b1fc0
DIFF: https://github.com/llvm/llvm-project/commit/d7a47a9bb5bd9f9b292e4f11184942936e2b1fc0.diff

LOG: Desist from passing function location to __msan_set_alloca_origin4.

This is done by calling __msan_set_alloca_origin and providing the location of the variable by using the call stack.
This is prepatory work for dropping variable names when track-origins is enabled.

Reviewed By: vitalybuka

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

Added: 
    

Modified: 
    compiler-rt/lib/msan/msan.cpp
    compiler-rt/test/msan/chained_origin.cpp
    compiler-rt/test/msan/chained_origin_memcpy.cpp
    compiler-rt/test/msan/chained_origin_memmove.cpp
    compiler-rt/test/msan/msan_print_shadow.cpp
    compiler-rt/test/msan/report-demangling.cpp
    compiler-rt/test/msan/stack-origin.cpp
    compiler-rt/test/msan/stack-origin2.cpp
    compiler-rt/test/msan/unaligned_read_origin.cpp
    llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
    llvm/test/Instrumentation/MemorySanitizer/alloca.ll

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/msan/msan.cpp b/compiler-rt/lib/msan/msan.cpp
index 2c0f849b999a3..87b0e1f87303a 100644
--- a/compiler-rt/lib/msan/msan.cpp
+++ b/compiler-rt/lib/msan/msan.cpp
@@ -585,7 +585,10 @@ void __msan_set_origin(const void *a, uptr size, u32 origin) {
 // When we see descr for the first time we replace '----' with a uniq id
 // and set the origin to (id | (31-th bit)).
 void __msan_set_alloca_origin(void *a, uptr size, char *descr) {
-  __msan_set_alloca_origin4(a, size, descr, 0);
+  __msan_set_alloca_origin4(
+      a, size, descr,
+      StackTrace::GetPreviousInstructionPc(
+          reinterpret_cast<uptr>(__builtin_return_address(0))));
 }
 
 void __msan_set_alloca_origin4(void *a, uptr size, char *descr, uptr pc) {

diff  --git a/compiler-rt/test/msan/chained_origin.cpp b/compiler-rt/test/msan/chained_origin.cpp
index c16b8792251d1..3889edf7fb1aa 100644
--- a/compiler-rt/test/msan/chained_origin.cpp
+++ b/compiler-rt/test/msan/chained_origin.cpp
@@ -61,7 +61,7 @@ int main(int argc, char *argv[]) {
 // CHECK-SHORT-STACK: {{#0 .* in fn_g.*chained_origin.cpp:}}[[@LINE-37]]
 
 // CHECK-STACK: Uninitialized value was created by an allocation of 'z' in the stack frame of function 'main'
-// CHECK-STACK: {{#0 .* in main.*chained_origin.cpp:}}[[@LINE-27]]
+// CHECK-STACK: {{#0 .* in main.*chained_origin.cpp:}}[[@LINE-22]]
 
 // CHECK-HEAP: Uninitialized value was created by a heap allocation
 // CHECK-HEAP: {{#1 .* in main.*chained_origin.cpp:}}[[@LINE-28]]

diff  --git a/compiler-rt/test/msan/chained_origin_memcpy.cpp b/compiler-rt/test/msan/chained_origin_memcpy.cpp
index 47a0effb3ccde..c5c526e653afc 100644
--- a/compiler-rt/test/msan/chained_origin_memcpy.cpp
+++ b/compiler-rt/test/msan/chained_origin_memcpy.cpp
@@ -59,4 +59,5 @@ int main(int argc, char *argv[]) {
 
 // CHECK-Z1: Uninitialized value was created by an allocation of 'z1' in the stack frame of function 'main'
 // CHECK-Z2: Uninitialized value was created by an allocation of 'z2' in the stack frame of function 'main'
-// CHECK: {{#0 .* in main.*chained_origin_memcpy.cpp:}}[[@LINE-22]]
+// CHECK-Z1: {{#0 .* in main.*chained_origin_memcpy.cpp:}}[[@LINE-21]]
+// CHECK-Z2: {{#0 .* in main.*chained_origin_memcpy.cpp:}}[[@LINE-21]]

diff  --git a/compiler-rt/test/msan/chained_origin_memmove.cpp b/compiler-rt/test/msan/chained_origin_memmove.cpp
index af6935767bc97..580e28c32f68f 100644
--- a/compiler-rt/test/msan/chained_origin_memmove.cpp
+++ b/compiler-rt/test/msan/chained_origin_memmove.cpp
@@ -54,4 +54,5 @@ int main(int argc, char *argv[]) {
 
 // CHECK-Z1: Uninitialized value was created by an allocation of 'z1' in the stack frame of function 'main'
 // CHECK-Z2: Uninitialized value was created by an allocation of 'z2' in the stack frame of function 'main'
-// CHECK: {{#0 .* in main.*chained_origin_memmove.cpp:}}[[@LINE-22]]
+// CHECK-Z1: {{#0 .* in main.*chained_origin_memmove.cpp:}}[[@LINE-21]]
+// CHECK-Z2: {{#0 .* in main.*chained_origin_memmove.cpp:}}[[@LINE-21]]

diff  --git a/compiler-rt/test/msan/msan_print_shadow.cpp b/compiler-rt/test/msan/msan_print_shadow.cpp
index 2782fcd83ed7d..1a72f5dd0e6a7 100644
--- a/compiler-rt/test/msan/msan_print_shadow.cpp
+++ b/compiler-rt/test/msan/msan_print_shadow.cpp
@@ -107,7 +107,7 @@ int main(void) {
 // CHECK-ORIGINS-2:  Uninitialized value was stored to memory at
 // CHECK-ORIGINS-2:    #0 {{.*}} in main{{.*}}msan_print_shadow.cpp:48
 // CHECK-ORIGINS:   Uninitialized value was created by an allocation of 'x' in the stack frame of function 'main'
-// CHECK-ORIGINS:     #0 {{.*}} in main{{.*}}msan_print_shadow.cpp:12
+// CHECK-ORIGINS:     #0 {{.*}} in main{{.*}}msan_print_shadow.cpp:13
 
 // CHECK-ORIGINS: Origin D (origin_id {{.*}}):
 // CHECK-ORIGINS:   Memory was marked as uninitialized

diff  --git a/compiler-rt/test/msan/report-demangling.cpp b/compiler-rt/test/msan/report-demangling.cpp
index 9ef6113cfd0c6..e77e4b08ac004 100644
--- a/compiler-rt/test/msan/report-demangling.cpp
+++ b/compiler-rt/test/msan/report-demangling.cpp
@@ -15,5 +15,5 @@ int main(int argc, char **argv) {
   return f();
   // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
   // CHECK: Uninitialized value was created by an allocation of 'x' in the stack frame of function '_Z1fv'
-  // CHECK: #0 {{.*}} in f{{.*}} {{.*}}report-demangling.cpp:[[@LINE-10]]
+  // CHECK: #0 {{.*}} in f{{.*}} {{.*}}report-demangling.cpp:[[@LINE-9]]
 }

diff  --git a/compiler-rt/test/msan/stack-origin.cpp b/compiler-rt/test/msan/stack-origin.cpp
index 3e315260ada81..8bc485af750a0 100644
--- a/compiler-rt/test/msan/stack-origin.cpp
+++ b/compiler-rt/test/msan/stack-origin.cpp
@@ -25,7 +25,7 @@ int main(int argc, char **argv) {
   // CHECK: {{#0 0x.* in main .*stack-origin.cpp:}}[[@LINE-2]]
 
   // CHECK-ORIGINS: Uninitialized value was created by an allocation of 'x' in the stack frame of function 'main'
-  // CHECK-ORIGINS: {{#0 0x.* in main .*stack-origin.cpp:}}[[@LINE-8]]
+  // CHECK-ORIGINS: {{#0 0x.* in main .*stack-origin.cpp:}}[[@LINE-7]]
 
   // CHECK: SUMMARY: MemorySanitizer: use-of-uninitialized-value {{.*stack-origin.cpp:.* main}}
 }

diff  --git a/compiler-rt/test/msan/stack-origin2.cpp b/compiler-rt/test/msan/stack-origin2.cpp
index 2ef3e180c4a19..14b899bb679ba 100644
--- a/compiler-rt/test/msan/stack-origin2.cpp
+++ b/compiler-rt/test/msan/stack-origin2.cpp
@@ -35,7 +35,7 @@ int main(int argc, char **argv) {
   // CHECK: {{#0 0x.* in main .*stack-origin2.cpp:}}[[@LINE-2]]
 
   // CHECK-ORIGINS: Uninitialized value was created by an allocation of 'x' in the stack frame of function 'f'
-  // CHECK-ORIGINS: {{#0 0x.* in f .*stack-origin2.cpp:}}[[@LINE-14]]
+  // CHECK-ORIGINS: {{#0 0x.* in f .*stack-origin2.cpp:}}[[@LINE-11]]
 
   // CHECK: SUMMARY: MemorySanitizer: use-of-uninitialized-value {{.*stack-origin2.cpp:.* main}}
 }

diff  --git a/compiler-rt/test/msan/unaligned_read_origin.cpp b/compiler-rt/test/msan/unaligned_read_origin.cpp
index cf25122ad31ae..bdccaf9104c53 100644
--- a/compiler-rt/test/msan/unaligned_read_origin.cpp
+++ b/compiler-rt/test/msan/unaligned_read_origin.cpp
@@ -12,5 +12,5 @@ int main(int argc, char **argv) {
   // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
   // CHECK: {{#0 0x.* in main .*unaligned_read_origin.cpp:}}[[@LINE-2]]
   // CHECK: Uninitialized value was created by an allocation of 'x' in the stack frame of function 'main'
-  // CHECK: {{#0 0x.* in main .*unaligned_read_origin.cpp:}}[[@LINE-7]]
+  // CHECK: {{#0 0x.* in main .*unaligned_read_origin.cpp:}}[[@LINE-6]]
 }

diff  --git a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
index 36537c014c2c4..8f8b1f5d4ef15 100644
--- a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -579,7 +579,7 @@ class MemorySanitizer {
 
   /// Run-time helper that generates a new origin value for a stack
   /// allocation.
-  FunctionCallee MsanSetAllocaOrigin4Fn;
+  FunctionCallee MsanSetAllocaOriginFn;
 
   /// Run-time helper that poisons stack on function entry.
   FunctionCallee MsanPoisonStackFn;
@@ -825,9 +825,9 @@ void MemorySanitizer::createUserspaceApi(Module &M) {
         IRB.getInt32Ty());
   }
 
-  MsanSetAllocaOrigin4Fn = M.getOrInsertFunction(
-    "__msan_set_alloca_origin4", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy,
-    IRB.getInt8PtrTy(), IntptrTy);
+  MsanSetAllocaOriginFn = M.getOrInsertFunction(
+    "__msan_set_alloca_origin", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy,
+    IRB.getInt8PtrTy());
   MsanPoisonStackFn =
       M.getOrInsertFunction("__msan_poison_stack", IRB.getVoidTy(),
                             IRB.getInt8PtrTy(), IntptrTy);
@@ -3895,10 +3895,9 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
 
     if (PoisonStack && MS.TrackOrigins) {
       Value *Descr = getLocalVarDescription(I);
-      IRB.CreateCall(MS.MsanSetAllocaOrigin4Fn,
+      IRB.CreateCall(MS.MsanSetAllocaOriginFn,
                      {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len,
-                      IRB.CreatePointerCast(Descr, IRB.getInt8PtrTy()),
-                      IRB.CreatePointerCast(&F, MS.IntptrTy)});
+                      IRB.CreatePointerCast(Descr, IRB.getInt8PtrTy())});
     }
   }
 
@@ -3918,6 +3917,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
     if (!InsPoint)
       InsPoint = &I;
     IRBuilder<> IRB(InsPoint->getNextNode());
+    IRB.SetCurrentDebugLocation(InsPoint->getDebugLoc());
     const DataLayout &DL = F.getParent()->getDataLayout();
     uint64_t TypeSize = DL.getTypeAllocSize(I.getAllocatedType());
     Value *Len = ConstantInt::get(MS.IntptrTy, TypeSize);

diff  --git a/llvm/test/Instrumentation/MemorySanitizer/alloca.ll b/llvm/test/Instrumentation/MemorySanitizer/alloca.ll
index 10c1796ac6047..a3a807ad9f5c6 100644
--- a/llvm/test/Instrumentation/MemorySanitizer/alloca.ll
+++ b/llvm/test/Instrumentation/MemorySanitizer/alloca.ll
@@ -23,7 +23,7 @@ entry:
 ; CHECK-LABEL: define void @static(
 ; INLINE: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 4, i1 false)
 ; CALL: call void @__msan_poison_stack(i8* {{.*}}, i64 4)
-; ORIGIN: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 4,
+; ORIGIN: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 4,
 ; KMSAN: call void @__msan_poison_alloca(i8* {{.*}}, i64 4,
 ; CHECK: ret void
 
@@ -39,7 +39,7 @@ l:
 ; CHECK-LABEL: define void @dynamic(
 ; INLINE: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 4, i1 false)
 ; CALL: call void @__msan_poison_stack(i8* {{.*}}, i64 4)
-; ORIGIN: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 4,
+; ORIGIN: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 4,
 ; KMSAN: call void @__msan_poison_alloca(i8* {{.*}}, i64 4,
 ; CHECK: ret void
 
@@ -52,7 +52,7 @@ entry:
 ; CHECK-LABEL: define void @array(
 ; INLINE: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 20, i1 false)
 ; CALL: call void @__msan_poison_stack(i8* {{.*}}, i64 20)
-; ORIGIN: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 20,
+; ORIGIN: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 20,
 ; KMSAN: call void @__msan_poison_alloca(i8* {{.*}}, i64 20,
 ; CHECK: ret void
 
@@ -66,7 +66,7 @@ entry:
 ; CHECK: %[[A:.*]] = mul i64 4, %cnt
 ; INLINE: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 %[[A]], i1 false)
 ; CALL: call void @__msan_poison_stack(i8* {{.*}}, i64 %[[A]])
-; ORIGIN: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 %[[A]],
+; ORIGIN: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 %[[A]],
 ; KMSAN: call void @__msan_poison_alloca(i8* {{.*}}, i64 %[[A]],
 ; CHECK: ret void
 
@@ -80,7 +80,7 @@ entry:
 ; CHECK-LABEL: define void @unpoison_local(
 ; INLINE: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 0, i64 20, i1 false)
 ; CALL: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 0, i64 20, i1 false)
-; ORIGIN-NOT: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 20,
+; ORIGIN-NOT: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 20,
 ; KMSAN: call void @__msan_unpoison_alloca(i8* {{.*}}, i64 20)
 ; CHECK: ret void
 
@@ -109,13 +109,13 @@ another_bb:
 ; CHECK: call void @llvm.lifetime.start
 ; INLINE: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 4, i1 false)
 ; CALL: call void @__msan_poison_stack(i8* {{.*}}, i64 4)
-; ORIGIN: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 4,
+; ORIGIN: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 4,
 ; KMSAN: call void @__msan_poison_alloca(i8* {{.*}}, i64 4,
 
 ; CHECK: call void @llvm.lifetime.start
 ; INLINE: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 4, i1 false)
 ; CALL: call void @__msan_poison_stack(i8* {{.*}}, i64 4)
-; ORIGIN: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 4,
+; ORIGIN: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 4,
 ; KMSAN: call void @__msan_poison_alloca(i8* {{.*}}, i64 4,
 ; CHECK: ret void
 
@@ -136,7 +136,7 @@ entry:
 ; CHECK: %[[A:.*]] = mul i64 4, %cnt
 ; INLINE: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 %[[A]], i1 false)
 ; CALL: call void @__msan_poison_stack(i8* {{.*}}, i64 %[[A]])
-; ORIGIN: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 %[[A]],
+; ORIGIN: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 %[[A]],
 ; KMSAN: call void @__msan_poison_alloca(i8* {{.*}}, i64 %[[A]],
 ; CHECK: call void @llvm.lifetime.end
 ; CHECK: ret void
@@ -176,36 +176,36 @@ another_bb:
 ; CHECK: %x = alloca i32
 ; INLINE: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 4, i1 false)
 ; CALL: call void @__msan_poison_stack(i8* {{.*}}, i64 4)
-; ORIGIN: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 4,
+; ORIGIN: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 4,
 ; KMSAN: call void @__msan_poison_alloca(i8* {{.*}}, i64 4,
 ; CHECK: %y = alloca i32
 ; INLINE: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 4, i1 false)
 ; CALL: call void @__msan_poison_stack(i8* {{.*}}, i64 4)
-; ORIGIN: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 4,
+; ORIGIN: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 4,
 ; KMSAN: call void @__msan_poison_alloca(i8* {{.*}}, i64 4,
 ; CHECK: %z = alloca i32
 ; INLINE: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 4, i1 false)
 ; CALL: call void @__msan_poison_stack(i8* {{.*}}, i64 4)
-; ORIGIN: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 4,
+; ORIGIN: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 4,
 ; KMSAN: call void @__msan_poison_alloca(i8* {{.*}}, i64 4,
 
 ; There're two lifetime intrinsics for %z, but we must instrument it only once.
 ; INLINE-NOT: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 4, i1 false)
 ; CALL-NOT: call void @__msan_poison_stack(i8* {{.*}}, i64 4)
-; ORIGIN-NOT: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 4,
+; ORIGIN-NOT: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 4,
 ; KMSAN-NOT: call void @__msan_poison_alloca(i8* {{.*}}, i64 4,
 ; CHECK-LABEL: another_bb:
 
 ; CHECK: call void @llvm.lifetime.start
 ; INLINE-NOT: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 4, i1 false)
 ; CALL-NOT: call void @__msan_poison_stack(i8* {{.*}}, i64 4)
-; ORIGIN-NOT: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 4,
+; ORIGIN-NOT: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 4,
 ; KMSAN-NOT: call void @__msan_poison_alloca(i8* {{.*}}, i64 4,
 ; CHECK: call void @llvm.lifetime.end
 ; CHECK: call void @llvm.lifetime.start
 ; INLINE-NOT: call void @llvm.memset.p0i8.i64(i8* align 4 {{.*}}, i8 -1, i64 4, i1 false)
 ; CALL-NOT: call void @__msan_poison_stack(i8* {{.*}}, i64 4)
-; ORIGIN-NOT: call void @__msan_set_alloca_origin4(i8* {{.*}}, i64 4,
+; ORIGIN-NOT: call void @__msan_set_alloca_origin(i8* {{.*}}, i64 4,
 ; KMSAN-NOT: call void @__msan_poison_alloca(i8* {{.*}}, i64 4,
 ; CHECK: call void @llvm.lifetime.end
 


        


More information about the llvm-commits mailing list