llvm.org GIT mirror llvm / b9b89d4
hwasan: Pad arrays with non-1 size correctly. Spotted by eugenis. Differential Revision: https://reviews.llvm.org/D64783 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366171 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 months ago
5 changed file(s) with 33 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
11071107 uint64_t AlignedSize = alignTo(Size, Mapping.getAllocaAlignment());
11081108 AI->setAlignment(std::max(AI->getAlignment(), 16u));
11091109 if (Size != AlignedSize) {
1110 Type *AllocatedType = AI->getAllocatedType();
1111 if (AI->isArrayAllocation()) {
1112 uint64_t ArraySize =
1113 cast(AI->getArraySize())->getZExtValue();
1114 AllocatedType = ArrayType::get(AllocatedType, ArraySize);
1115 }
11101116 Type *TypeWithPadding = StructType::get(
1111 AI->getAllocatedType(), ArrayType::get(Int8Ty, AlignedSize - Size));
1117 AllocatedType, ArrayType::get(Int8Ty, AlignedSize - Size));
11121118 auto *NewAI = new AllocaInst(
11131119 TypeWithPadding, AI->getType()->getAddressSpace(), nullptr, "", AI);
11141120 NewAI->takeName(AI);
11161122 NewAI->setUsedWithInAlloca(AI->isUsedWithInAlloca());
11171123 NewAI->setSwiftError(AI->isSwiftError());
11181124 NewAI->copyMetadata(*AI);
1119 Value *Zero = ConstantInt::get(Int32Ty, 0);
1120 auto *GEP = GetElementPtrInst::Create(TypeWithPadding, NewAI,
1121 {Zero, Zero}, "", AI);
1122 AI->replaceAllUsesWith(GEP);
1125 auto *Bitcast = new BitCastInst(NewAI, AI->getType(), "", AI);
1126 AI->replaceAllUsesWith(Bitcast);
11231127 AllocaToPaddedAllocaMap[AI] = NewAI;
11241128 }
11251129 }
0 ; RUN: opt < %s -hwasan -S | FileCheck %s
1
2 target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
3 target triple = "aarch64--linux-android"
4
5 declare void @use(i8*, i8*)
6
7 define void @test_alloca() sanitize_hwaddress {
8 ; CHECK: alloca { [4 x i8], [12 x i8] }, align 16
9 %x = alloca i8, i64 4
10 ; CHECK: alloca i8, i64 16, align 16
11 %y = alloca i8, i64 16
12 call void @use(i8* %x, i8* %y)
13 ret void
14 }
88
99 define void @test_alloca() sanitize_hwaddress {
1010 ; CHECK-LABEL: @test_alloca(
11 ; CHECK: %[[GEP:[^ ]*]] = getelementptr { i32, [12 x i8] }, { i32, [12 x i8] }* %x, i32 0, i32 0
11 ; CHECK: %[[BC:[^ ]*]] = bitcast { i32, [12 x i8] }* %x to i32*
1212 ; CHECK: %[[T1:[^ ]*]] = call i8 @__hwasan_generate_tag()
1313 ; CHECK: %[[A:[^ ]*]] = zext i8 %[[T1]] to i64
14 ; CHECK: %[[B:[^ ]*]] = ptrtoint i32* %[[GEP]] to i64
14 ; CHECK: %[[B:[^ ]*]] = ptrtoint i32* %[[BC]] to i64
1515 ; CHECK: %[[C:[^ ]*]] = shl i64 %[[A]], 56
1616 ; CHECK: or i64 %[[B]], %[[C]]
1717
1616 ; CHECK: %[[BASE_TAG:[^ ]*]] = xor i64 %[[A]], %[[B]]
1717
1818 ; CHECK: %[[X:[^ ]*]] = alloca { i32, [12 x i8] }, align 16
19 ; CHECK: %[[X_GEP:[^ ]*]] = getelementptr { i32, [12 x i8] }, { i32, [12 x i8] }* %[[X]], i32 0, i32 0
19 ; CHECK: %[[X_BC:[^ ]*]] = bitcast { i32, [12 x i8] }* %[[X]] to i32*
2020 ; CHECK: %[[X_TAG:[^ ]*]] = xor i64 %[[BASE_TAG]], 0
21 ; CHECK: %[[X1:[^ ]*]] = ptrtoint i32* %[[X_GEP]] to i64
21 ; CHECK: %[[X1:[^ ]*]] = ptrtoint i32* %[[X_BC]] to i64
2222 ; CHECK: %[[C:[^ ]*]] = shl i64 %[[X_TAG]], 56
2323 ; CHECK: %[[D:[^ ]*]] = or i64 %[[X1]], %[[C]]
2424 ; CHECK: %[[X_HWASAN:[^ ]*]] = inttoptr i64 %[[D]] to i32*
2525
2626 ; CHECK: %[[X_TAG2:[^ ]*]] = trunc i64 %[[X_TAG]] to i8
27 ; CHECK: %[[E:[^ ]*]] = ptrtoint i32* %[[X_GEP]] to i64
27 ; CHECK: %[[E:[^ ]*]] = ptrtoint i32* %[[X_BC]] to i64
2828 ; CHECK: %[[F:[^ ]*]] = lshr i64 %[[E]], 4
2929 ; DYNAMIC-SHADOW: %[[X_SHADOW:[^ ]*]] = getelementptr i8, i8* %.hwasan.shadow, i64 %[[F]]
3030 ; ZERO-BASED-SHADOW: %[[X_SHADOW:[^ ]*]] = inttoptr i64 %[[F]] to i8*
3131 ; CHECK: %[[X_SHADOW_GEP:[^ ]*]] = getelementptr i8, i8* %[[X_SHADOW]], i32 0
3232 ; CHECK: store i8 4, i8* %[[X_SHADOW_GEP]]
33 ; CHECK: %[[X_I8:[^ ]*]] = bitcast i32* %[[X_GEP]] to i8*
33 ; CHECK: %[[X_I8:[^ ]*]] = bitcast i32* %[[X_BC]] to i8*
3434 ; CHECK: %[[X_I8_GEP:[^ ]*]] = getelementptr i8, i8* %[[X_I8]], i32 15
3535 ; CHECK: store i8 %[[X_TAG2]], i8* %[[X_I8_GEP]]
3636 ; CHECK: call void @use32(i32* nonnull %[[X_HWASAN]])
3737
3838 ; UAR-TAGS: %[[BASE_TAG_COMPL:[^ ]*]] = xor i64 %[[BASE_TAG]], 255
3939 ; UAR-TAGS: %[[X_TAG_UAR:[^ ]*]] = trunc i64 %[[BASE_TAG_COMPL]] to i8
40 ; CHECK: %[[E2:[^ ]*]] = ptrtoint i32* %[[X_GEP]] to i64
40 ; CHECK: %[[E2:[^ ]*]] = ptrtoint i32* %[[X_BC]] to i64
4141 ; CHECK: %[[F2:[^ ]*]] = lshr i64 %[[E2]], 4
4242 ; DYNAMIC-SHADOW: %[[X_SHADOW2:[^ ]*]] = getelementptr i8, i8* %.hwasan.shadow, i64 %[[F2]]
4343 ; ZERO-BASED-SHADOW: %[[X_SHADOW2:[^ ]*]] = inttoptr i64 %[[F2]] to i8*
1414 ; CHECK: %[[BASE_TAG:[^ ]*]] = xor i64 %[[A]], %[[B]]
1515
1616 ; CHECK: %[[X:[^ ]*]] = alloca { i32, [12 x i8] }, align 16
17 ; CHECK: %[[X_GEP:[^ ]*]] = getelementptr { i32, [12 x i8] }, { i32, [12 x i8] }* %[[X]], i32 0, i32 0
17 ; CHECK: %[[X_BC:[^ ]*]] = bitcast { i32, [12 x i8] }* %[[X]] to i32*
1818 ; CHECK: %[[X_TAG:[^ ]*]] = xor i64 %[[BASE_TAG]], 0
19 ; CHECK: %[[X1:[^ ]*]] = ptrtoint i32* %[[X_GEP]] to i64
19 ; CHECK: %[[X1:[^ ]*]] = ptrtoint i32* %[[X_BC]] to i64
2020 ; CHECK: %[[C:[^ ]*]] = shl i64 %[[X_TAG]], 56
2121 ; CHECK: %[[D:[^ ]*]] = or i64 %[[C]], 72057594037927935
2222 ; CHECK: %[[E:[^ ]*]] = and i64 %[[X1]], %[[D]]