1 //===- ASanStackFrameLayoutTest.cpp - Tests for ComputeASanStackFrameLayout===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
9 #include "llvm/ADT/ArrayRef.h"
10 #include "gtest/gtest.h"
11 #include <sstream>
12
13 using namespace llvm;
14
15 static std::string
ShadowBytesToString(ArrayRef<uint8_t> ShadowBytes)16 ShadowBytesToString(ArrayRef<uint8_t> ShadowBytes) {
17 std::ostringstream os;
18 for (size_t i = 0, n = ShadowBytes.size(); i < n; i++) {
19 switch (ShadowBytes[i]) {
20 case kAsanStackLeftRedzoneMagic: os << "L"; break;
21 case kAsanStackRightRedzoneMagic: os << "R"; break;
22 case kAsanStackMidRedzoneMagic: os << "M"; break;
23 case kAsanStackUseAfterScopeMagic:
24 os << "S";
25 break;
26 default: os << (unsigned)ShadowBytes[i];
27 }
28 }
29 return os.str();
30 }
31
32 // Use macro to preserve line information in EXPECT_EQ output.
33 #define TEST_LAYOUT(V, Granularity, MinHeaderSize, ExpectedDescr, \
34 ExpectedShadow, ExpectedShadowAfterScope) \
35 { \
36 SmallVector<ASanStackVariableDescription, 10> Vars = V; \
37 ASanStackFrameLayout L = \
38 ComputeASanStackFrameLayout(Vars, Granularity, MinHeaderSize); \
39 EXPECT_STREQ(ExpectedDescr, \
40 ComputeASanStackFrameDescription(Vars).c_str()); \
41 EXPECT_EQ(ExpectedShadow, ShadowBytesToString(GetShadowBytes(Vars, L))); \
42 EXPECT_EQ(ExpectedShadowAfterScope, \
43 ShadowBytesToString(GetShadowBytesAfterScope(Vars, L))); \
44 }
45
TEST(ASanStackFrameLayout,Test)46 TEST(ASanStackFrameLayout, Test) {
47 #define VAR(name, size, lifetime, alignment, line) \
48 ASanStackVariableDescription name##size##_##alignment = { \
49 #name #size "_" #alignment, \
50 size, \
51 lifetime, \
52 alignment, \
53 0, \
54 0, \
55 line, \
56 }
57
58 VAR(a, 1, 0, 1, 0);
59 VAR(p, 1, 0, 32, 15);
60 VAR(p, 1, 0, 256, 2700);
61 VAR(a, 2, 0, 1, 0);
62 VAR(a, 3, 0, 1, 0);
63 VAR(a, 4, 0, 1, 0);
64 VAR(a, 7, 0, 1, 0);
65 VAR(a, 8, 8, 1, 0);
66 VAR(a, 9, 0, 1, 0);
67 VAR(a, 16, 16, 1, 0);
68 VAR(a, 41, 9, 1, 7);
69 VAR(a, 105, 103, 1, 0);
70 VAR(a, 200, 97, 1, 0);
71
72 TEST_LAYOUT({a1_1}, 8, 16, "1 16 1 4 a1_1", "LL1R", "LL1R");
73 TEST_LAYOUT({a1_1}, 16, 16, "1 16 1 4 a1_1", "L1R", "L1R");
74 TEST_LAYOUT({a1_1}, 32, 32, "1 32 1 4 a1_1", "L1R", "L1R");
75 TEST_LAYOUT({a1_1}, 64, 64, "1 64 1 4 a1_1", "L1R", "L1R");
76 TEST_LAYOUT({p1_32}, 8, 32, "1 32 1 8 p1_32:15", "LLLL1RRR", "LLLL1RRR");
77 TEST_LAYOUT({p1_32}, 8, 64, "1 64 1 8 p1_32:15", "LLLLLLLL1RRRRRRR",
78 "LLLLLLLL1RRRRRRR");
79
80 TEST_LAYOUT({a1_1}, 8, 32, "1 32 1 4 a1_1", "LLLL1RRR", "LLLL1RRR");
81 TEST_LAYOUT({a2_1}, 8, 32, "1 32 2 4 a2_1", "LLLL2RRR", "LLLL2RRR");
82 TEST_LAYOUT({a3_1}, 8, 32, "1 32 3 4 a3_1", "LLLL3RRR", "LLLL3RRR");
83 TEST_LAYOUT({a4_1}, 8, 32, "1 32 4 4 a4_1", "LLLL4RRR", "LLLL4RRR");
84 TEST_LAYOUT({a7_1}, 8, 32, "1 32 7 4 a7_1", "LLLL7RRR", "LLLL7RRR");
85 TEST_LAYOUT({a8_1}, 8, 32, "1 32 8 4 a8_1", "LLLL0RRR", "LLLLSRRR");
86 TEST_LAYOUT({a9_1}, 8, 32, "1 32 9 4 a9_1", "LLLL01RR", "LLLL01RR");
87 TEST_LAYOUT({a16_1}, 8, 32, "1 32 16 5 a16_1", "LLLL00RR", "LLLLSSRR");
88 TEST_LAYOUT({p1_256}, 8, 32, "1 256 1 11 p1_256:2700",
89 "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1RRR",
90 "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1RRR");
91 TEST_LAYOUT({a41_1}, 8, 32, "1 32 41 7 a41_1:7", "LLLL000001RRRRRR",
92 "LLLLSS0001RRRRRR");
93 TEST_LAYOUT({a105_1}, 8, 32, "1 32 105 6 a105_1", "LLLL00000000000001RRRRRR",
94 "LLLLSSSSSSSSSSSSS1RRRRRR");
95
96 {
97 SmallVector<ASanStackVariableDescription, 10> t = {a1_1, p1_256};
98 TEST_LAYOUT(t, 8, 32, "2 256 1 11 p1_256:2700 272 1 4 a1_1",
99 "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1M1R",
100 "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1M1R");
101 }
102
103 {
104 SmallVector<ASanStackVariableDescription, 10> t = {a1_1, a16_1, a41_1};
105 TEST_LAYOUT(t, 8, 32, "3 32 1 4 a1_1 48 16 5 a16_1 80 41 7 a41_1:7",
106 "LLLL1M00MM000001RRRR", "LLLL1MSSMMSS0001RRRR");
107 }
108
109 TEST_LAYOUT({a2_1}, 32, 32, "1 32 2 4 a2_1", "L2R", "L2R");
110 TEST_LAYOUT({a9_1}, 32, 32, "1 32 9 4 a9_1", "L9R", "L9R");
111 TEST_LAYOUT({a16_1}, 32, 32, "1 32 16 5 a16_1", "L16R", "LSR");
112 TEST_LAYOUT({p1_256}, 32, 32, "1 256 1 11 p1_256:2700",
113 "LLLLLLLL1R", "LLLLLLLL1R");
114 TEST_LAYOUT({a41_1}, 32, 32, "1 32 41 7 a41_1:7", "L09R",
115 "LS9R");
116 TEST_LAYOUT({a105_1}, 32, 32, "1 32 105 6 a105_1", "L0009R",
117 "LSSSSR");
118 TEST_LAYOUT({a200_1}, 32, 32, "1 32 200 6 a200_1", "L0000008RR",
119 "LSSSS008RR");
120
121 {
122 SmallVector<ASanStackVariableDescription, 10> t = {a1_1, p1_256};
123 TEST_LAYOUT(t, 32, 32, "2 256 1 11 p1_256:2700 320 1 4 a1_1",
124 "LLLLLLLL1M1R", "LLLLLLLL1M1R");
125 }
126
127 {
128 SmallVector<ASanStackVariableDescription, 10> t = {a1_1, a16_1, a41_1};
129 TEST_LAYOUT(t, 32, 32, "3 32 1 4 a1_1 96 16 5 a16_1 160 41 7 a41_1:7",
130 "L1M16M09R", "L1MSMS9R");
131 }
132 #undef VAR
133 #undef TEST_LAYOUT
134 }
135