[llvm] r342582 - [InstCombine] Disable strcmp->memcmp transform for MSan.

Matt Morehouse via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 19 12:37:24 PDT 2018


Author: morehouse
Date: Wed Sep 19 12:37:24 2018
New Revision: 342582

URL: http://llvm.org/viewvc/llvm-project?rev=342582&view=rev
Log:
[InstCombine] Disable strcmp->memcmp transform for MSan.

Summary:
The strcmp->memcmp transform can make the resulting memcmp read
uninitialized data, which MSan doesn't like.

Resolves https://github.com/google/sanitizers/issues/993.

Reviewers: eugenis, xbolva00

Reviewed By: eugenis

Subscribers: hiraditya, llvm-commits

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

Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
    llvm/trunk/test/Transforms/InstCombine/strcmp-memcmp.ll

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=342582&r1=342581&r2=342582&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Wed Sep 19 12:37:24 2018
@@ -171,7 +171,10 @@ static bool canTransformToMemCmp(CallIns
 
   if (!isDereferenceableAndAlignedPointer(Str, 1, APInt(64, Len), DL))
     return false;
-    
+
+  if (CI->getFunction()->hasFnAttribute(Attribute::SanitizeMemory))
+    return false;
+
   return true;
 }
 

Modified: llvm/trunk/test/Transforms/InstCombine/strcmp-memcmp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/strcmp-memcmp.ll?rev=342582&r1=342581&r2=342582&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/strcmp-memcmp.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/strcmp-memcmp.ll Wed Sep 19 12:37:24 2018
@@ -542,4 +542,19 @@ define i32 @strncmp_memcmp_bad4([4 x i8]
   ret i32 0
 }
 
+define i32 @strcmp_memcmp_msan([12 x i8]* dereferenceable (12) %buf) sanitize_memory {
+; CHECK-LABEL: @strcmp_memcmp_msan(
+; CHECK-NEXT:    [[STRING:%.*]] = getelementptr inbounds [12 x i8], [12 x i8]* [[BUF:%.*]], i64 0, i64 0
+; CHECK-NEXT:    [[CALL:%.*]] = call i32 @strcmp(i8* nonnull [[STRING]], i8* getelementptr inbounds ([4 x i8], [4 x i8]* @key, i64 0, i64 0))
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[CALL]], 0
+; CHECK-NEXT:    [[CONV:%.*]] = zext i1 [[CMP]] to i32
+; CHECK-NEXT:    ret i32 [[CONV]]
+;
+  %string = getelementptr inbounds [12 x i8], [12 x i8]* %buf, i64 0, i64 0
+  %call = call i32 @strcmp(i8* nonnull %string, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @key, i64 0, i64 0))
+  %cmp = icmp eq i32 %call, 0
+  %conv = zext i1 %cmp to i32
+  ret i32 %conv
+}
+
 declare i32 @memcmp(i8* nocapture, i8* nocapture, i64)




More information about the llvm-commits mailing list