Skip to content

Commit d5ec149

Browse files
committedAug 18, 2016
[asan] Add support of lifetime poisoning into ComputeASanStackFrameLayout
Summary: We are going to combine poisoning of red zones and scope poisoning. PR27453 Reviewers: kcc, eugenis Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D23623 llvm-svn: 279020
1 parent 1396b9f commit d5ec149

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed
 

Diff for: ‎llvm/include/llvm/Transforms/Utils/ASanStackFrameLayout.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@ class AllocaInst;
2424
static const int kAsanStackLeftRedzoneMagic = 0xf1;
2525
static const int kAsanStackMidRedzoneMagic = 0xf2;
2626
static const int kAsanStackRightRedzoneMagic = 0xf3;
27+
static const int kAsanStackUseAfterScopeMagic = 0xf8;
2728

2829
// Input/output data struct for ComputeASanStackFrameLayout.
2930
struct ASanStackVariableDescription {
30-
const char *Name; // Name of the variable that will be displayed by asan
31-
// if a stack-related bug is reported.
32-
uint64_t Size; // Size of the variable in bytes.
33-
size_t Alignment; // Alignment of the variable (power of 2).
34-
AllocaInst *AI; // The actual AllocaInst.
35-
size_t Offset; // Offset from the beginning of the frame;
36-
// set by ComputeASanStackFrameLayout.
31+
const char *Name; // Name of the variable that will be displayed by asan
32+
// if a stack-related bug is reported.
33+
uint64_t Size; // Size of the variable in bytes.
34+
size_t LifetimeSize; // Size in bytes to use for lifetime analysis check.
35+
// Will be rounded up to Granularity.
36+
size_t Alignment; // Alignment of the variable (power of 2).
37+
AllocaInst *AI; // The actual AllocaInst.
38+
size_t Offset; // Offset from the beginning of the frame;
39+
// set by ComputeASanStackFrameLayout.
3740
};
3841

3942
// Output data struct for ComputeASanStackFrameLayout.

Diff for: ‎llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -2079,7 +2079,10 @@ void FunctionStackPoisoner::poisonStack() {
20792079
for (AllocaInst *AI : AllocaVec) {
20802080
ASanStackVariableDescription D = {AI->getName().data(),
20812081
ASan.getAllocaSizeInBytes(*AI),
2082-
AI->getAlignment(), AI, 0};
2082+
0,
2083+
AI->getAlignment(),
2084+
AI,
2085+
0};
20832086
SVD.push_back(D);
20842087
}
20852088
// Minimal header size (left redzone) is 4 pointers,

Diff for: ‎llvm/lib/Transforms/Utils/ASanStackFrameLayout.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,26 @@ ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars,
8181
assert(Layout->FrameAlignment >= Alignment);
8282
assert((Offset % Alignment) == 0);
8383
assert(Size > 0);
84+
assert(Vars[i].LifetimeSize <= Size);
8485
StackDescription << " " << Offset << " " << Size << " " << strlen(Name)
8586
<< " " << Name;
8687
size_t NextAlignment = IsLast ? Granularity
8788
: std::max(Granularity, Vars[i + 1].Alignment);
8889
size_t SizeWithRedzone = VarAndRedzoneSize(Vars[i].Size, NextAlignment);
89-
SB.insert(SB.end(), Size / Granularity, 0);
90-
if (Size % Granularity)
91-
SB.insert(SB.end(), Size % Granularity);
90+
size_t LifetimeShadowSize =
91+
(Vars[i].LifetimeSize + Granularity - 1) / Granularity;
92+
SB.insert(SB.end(), LifetimeShadowSize, kAsanStackUseAfterScopeMagic);
93+
if (Size / Granularity >= LifetimeShadowSize) {
94+
SB.insert(SB.end(), Size / Granularity - LifetimeShadowSize, 0);
95+
if (Size % Granularity)
96+
SB.insert(SB.end(), Size % Granularity);
97+
}
9298
SB.insert(SB.end(), (SizeWithRedzone - Size) / Granularity,
9399
IsLast ? kAsanStackRightRedzoneMagic
94100
: kAsanStackMidRedzoneMagic);
95101
Vars[i].Offset = Offset;
96102
Offset += SizeWithRedzone;
103+
assert(Offset == SB.size() * Granularity);
97104
}
98105
if (Offset % MinHeaderSize) {
99106
size_t ExtraRedzone = MinHeaderSize - (Offset % MinHeaderSize);

Diff for: ‎llvm/unittests/Transforms/Utils/ASanStackFrameLayoutTest.cpp

+24-22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ ShadowBytesToString(ArrayRef<uint8_t> ShadowBytes) {
2121
case kAsanStackLeftRedzoneMagic: os << "L"; break;
2222
case kAsanStackRightRedzoneMagic: os << "R"; break;
2323
case kAsanStackMidRedzoneMagic: os << "M"; break;
24+
case kAsanStackUseAfterScopeMagic:
25+
os << "S";
26+
break;
2427
default: os << (unsigned)ShadowBytes[i];
2528
}
2629
}
@@ -42,27 +45,28 @@ TEST(ASanStackFrameLayout, Test) {
4245
#define VEC(a) \
4346
SmallVector<ASanStackVariableDescription, 10>(a, a + sizeof(a) / sizeof(a[0]))
4447

45-
#define VAR(name, size, alignment) \
48+
#define VAR(name, size, lifetime, alignment) \
4649
ASanStackVariableDescription name##size##_##alignment = { \
4750
#name #size "_" #alignment, \
4851
size, \
52+
lifetime, \
4953
alignment, \
5054
0, \
5155
0 \
5256
}
5357

54-
VAR(a, 1, 1);
55-
VAR(p, 1, 32);
56-
VAR(p, 1, 256);
57-
VAR(a, 2, 1);
58-
VAR(a, 3, 1);
59-
VAR(a, 4, 1);
60-
VAR(a, 7, 1);
61-
VAR(a, 8, 1);
62-
VAR(a, 9, 1);
63-
VAR(a, 16, 1);
64-
VAR(a, 41, 1);
65-
VAR(a, 105, 1);
58+
VAR(a, 1, 0, 1);
59+
VAR(p, 1, 0, 32);
60+
VAR(p, 1, 0, 256);
61+
VAR(a, 2, 0, 1);
62+
VAR(a, 3, 0, 1);
63+
VAR(a, 4, 0, 1);
64+
VAR(a, 7, 0, 1);
65+
VAR(a, 8, 8, 1);
66+
VAR(a, 9, 0, 1);
67+
VAR(a, 16, 0, 1);
68+
VAR(a, 41, 9, 1);
69+
VAR(a, 105, 103, 1);
6670

6771
TestLayout(VEC1(a1_1), 8, 16, "1 16 1 4 a1_1", "LL1R");
6872
TestLayout(VEC1(a1_1), 64, 64, "1 64 1 4 a1_1", "L1");
@@ -74,27 +78,25 @@ TEST(ASanStackFrameLayout, Test) {
7478
TestLayout(VEC1(a3_1), 8, 32, "1 32 3 4 a3_1", "LLLL3RRR");
7579
TestLayout(VEC1(a4_1), 8, 32, "1 32 4 4 a4_1", "LLLL4RRR");
7680
TestLayout(VEC1(a7_1), 8, 32, "1 32 7 4 a7_1", "LLLL7RRR");
77-
TestLayout(VEC1(a8_1), 8, 32, "1 32 8 4 a8_1", "LLLL0RRR");
81+
TestLayout(VEC1(a8_1), 8, 32, "1 32 8 4 a8_1", "LLLLSRRR");
7882
TestLayout(VEC1(a9_1), 8, 32, "1 32 9 4 a9_1", "LLLL01RR");
7983
TestLayout(VEC1(a16_1), 8, 32, "1 32 16 5 a16_1", "LLLL00RR");
8084
TestLayout(VEC1(p1_256), 8, 32, "1 256 1 6 p1_256",
8185
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1RRR");
82-
TestLayout(VEC1(a41_1), 8, 32, "1 32 41 5 a41_1", "LLLL000001RRRRRR");
86+
TestLayout(VEC1(a41_1), 8, 32, "1 32 41 5 a41_1", "LLLLSS0001RRRRRR");
8387
TestLayout(VEC1(a105_1), 8, 32, "1 32 105 6 a105_1",
84-
"LLLL00000000000001RRRRRR");
88+
"LLLLSSSSSSSSSSSSS1RRRRRR");
8589

8690
{
8791
ASanStackVariableDescription t[] = {a1_1, p1_256};
88-
TestLayout(VEC(t), 8, 32,
89-
"2 256 1 6 p1_256 272 1 4 a1_1",
90-
"LLLLLLLL" "LLLLLLLL" "LLLLLLLL" "LLLLLLLL" "1M1R");
92+
TestLayout(VEC(t), 8, 32, "2 256 1 6 p1_256 272 1 4 a1_1",
93+
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1M1R");
9194
}
9295

9396
{
9497
ASanStackVariableDescription t[] = {a1_1, a16_1, a41_1};
95-
TestLayout(VEC(t), 8, 32,
96-
"3 32 1 4 a1_1 48 16 5 a16_1 80 41 5 a41_1",
97-
"LLLL" "1M00" "MM00" "0001" "RRRR");
98+
TestLayout(VEC(t), 8, 32, "3 32 1 4 a1_1 48 16 5 a16_1 80 41 5 a41_1",
99+
"LLLL1M00MMSS0001RRRR");
98100
}
99101
#undef VEC1
100102
#undef VEC

0 commit comments

Comments
 (0)
Please sign in to comment.