From 6a8093b3200dafd6f97c3a8a84384b2d362aeca5 Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Tue, 28 May 2013 13:07:43 +0000 Subject: [PATCH] [msan] Fix argument shadow alignment. llvm-svn: 182771 --- .../Instrumentation/MemorySanitizer.cpp | 15 +++++-- .../MemorySanitizer/msan_basic.ll | 40 +++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/lib/Transforms/Instrumentation/MemorySanitizer.cpp index d4664a95636..58108290d8d 100644 --- a/lib/Transforms/Instrumentation/MemorySanitizer.cpp +++ b/lib/Transforms/Instrumentation/MemorySanitizer.cpp @@ -769,14 +769,21 @@ struct MemorySanitizerVisitor : public InstVisitor { if (AI->hasByValAttr()) { // ByVal pointer itself has clean shadow. We copy the actual // argument shadow to the underlying memory. + // Figure out maximal valid memcpy alignment. + unsigned ArgAlign = AI->getParamAlignment(); + if (ArgAlign == 0) { + Type *EltType = A->getType()->getPointerElementType(); + ArgAlign = MS.TD->getABITypeAlignment(EltType); + } + unsigned CopyAlign = std::min(ArgAlign, kShadowTLSAlignment); Value *Cpy = EntryIRB.CreateMemCpy( - getShadowPtr(V, EntryIRB.getInt8Ty(), EntryIRB), - Base, Size, AI->getParamAlignment()); + getShadowPtr(V, EntryIRB.getInt8Ty(), EntryIRB), Base, Size, + CopyAlign); DEBUG(dbgs() << " ByValCpy: " << *Cpy << "\n"); (void)Cpy; *ShadowPtr = getCleanShadow(V); } else { - *ShadowPtr = EntryIRB.CreateLoad(Base); + *ShadowPtr = EntryIRB.CreateAlignedLoad(Base, kShadowTLSAlignment); } DEBUG(dbgs() << " ARG: " << *AI << " ==> " << **ShadowPtr << "\n"); @@ -785,7 +792,7 @@ struct MemorySanitizerVisitor : public InstVisitor { setOrigin(A, EntryIRB.CreateLoad(OriginPtr)); } } - ArgOffset += DataLayout::RoundUpAlignment(Size, 8); + ArgOffset += DataLayout::RoundUpAlignment(Size, kShadowTLSAlignment); } assert(*ShadowPtr && "Could not find shadow for an argument"); return *ShadowPtr; diff --git a/test/Instrumentation/MemorySanitizer/msan_basic.ll b/test/Instrumentation/MemorySanitizer/msan_basic.ll index 320860ce77b..433b8bab4cc 100644 --- a/test/Instrumentation/MemorySanitizer/msan_basic.ll +++ b/test/Instrumentation/MemorySanitizer/msan_basic.ll @@ -1,5 +1,7 @@ ; RUN: opt < %s -msan -msan-check-access-address=0 -S | FileCheck %s ; RUN: opt < %s -msan -msan-check-access-address=0 -msan-track-origins=1 -S | FileCheck -check-prefix=CHECK-ORIGINS %s +; RUN: opt < %s -msan -msan-check-access-address=1 -S | FileCheck %s -check-prefix=CHECK-AA + target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" @@ -633,3 +635,41 @@ declare void @bar() ; CHECK: store {{.*}} @__msan_retval_tls ; CHECK-NOT: @__msan_warning ; CHECK: ret i32 + + +; Test argument shadow alignment + +define <2 x i64> @ArgumentShadowAlignment(i64 %a, <2 x i64> %b) sanitize_memory { +entry: + ret <2 x i64> %b +} + +; CHECK: @ArgumentShadowAlignment +; CHECK: load <2 x i64>* {{.*}} @__msan_param_tls {{.*}}, align 8 +; CHECK: store <2 x i64> {{.*}} @__msan_retval_tls {{.*}}, align 8 +; CHECK: ret <2 x i64> + + +; Test byval argument shadow alignment + +define <2 x i64> @ByValArgumentShadowLargeAlignment(<2 x i64>* byval %p) sanitize_memory { +entry: + %x = load <2 x i64>* %p + ret <2 x i64> %x +} + +; CHECK-AA: @ByValArgumentShadowLargeAlignment +; CHECK-AA: call void @llvm.memcpy.p0i8.p0i8.i64(i8* {{.*}}, i8* {{.*}}, i64 16, i32 8, i1 false) +; CHECK-AA: ret <2 x i64> + + +define i16 @ByValArgumentShadowSmallAlignment(i16* byval %p) sanitize_memory { +entry: + %x = load i16* %p + ret i16 %x +} + +; CHECK-AA: @ByValArgumentShadowSmallAlignment +; CHECK-AA: call void @llvm.memcpy.p0i8.p0i8.i64(i8* {{.*}}, i8* {{.*}}, i64 2, i32 2, i1 false) +; CHECK-AA: ret i16 +