1 //===- ASanStackFrameLayoutTest.cpp - Tests for ComputeASanStackFrameLayout===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "gtest/gtest.h"
12 #include <sstream>
13
14 using namespace llvm;
15
16 static std::string
ShadowBytesToString(ArrayRef<uint8_t> ShadowBytes)17 ShadowBytesToString(ArrayRef<uint8_t> ShadowBytes) {
18 std::ostringstream os;
19 for (size_t i = 0, n = ShadowBytes.size(); i < n; i++) {
20 switch (ShadowBytes[i]) {
21 case kAsanStackLeftRedzoneMagic: os << "L"; break;
22 case kAsanStackRightRedzoneMagic: os << "R"; break;
23 case kAsanStackMidRedzoneMagic: os << "M"; break;
24 default: os << (unsigned)ShadowBytes[i];
25 }
26 }
27 return os.str();
28 }
29
TestLayout(SmallVector<ASanStackVariableDescription,10> Vars,size_t Granularity,size_t MinHeaderSize,const std::string & ExpectedDescr,const std::string & ExpectedShadow)30 static void TestLayout(SmallVector<ASanStackVariableDescription, 10> Vars,
31 size_t Granularity, size_t MinHeaderSize,
32 const std::string &ExpectedDescr,
33 const std::string &ExpectedShadow) {
34 ASanStackFrameLayout L;
35 ComputeASanStackFrameLayout(Vars, Granularity, MinHeaderSize, &L);
36 EXPECT_EQ(ExpectedDescr, L.DescriptionString);
37 EXPECT_EQ(ExpectedShadow, ShadowBytesToString(L.ShadowBytes));
38 }
39
TEST(ASanStackFrameLayout,Test)40 TEST(ASanStackFrameLayout, Test) {
41 #define VEC1(a) SmallVector<ASanStackVariableDescription, 10>(1, a)
42 #define VEC(a) \
43 SmallVector<ASanStackVariableDescription, 10>(a, a + sizeof(a) / sizeof(a[0]))
44
45 #define VAR(name, size, alignment) \
46 ASanStackVariableDescription name##size##_##alignment = { \
47 #name #size "_" #alignment, \
48 size, \
49 alignment, \
50 0, \
51 0 \
52 }
53
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);
66
67 TestLayout(VEC1(a1_1), 8, 16, "1 16 1 4 a1_1", "LL1R");
68 TestLayout(VEC1(a1_1), 64, 64, "1 64 1 4 a1_1", "L1");
69 TestLayout(VEC1(p1_32), 8, 32, "1 32 1 5 p1_32", "LLLL1RRR");
70 TestLayout(VEC1(p1_32), 8, 64, "1 64 1 5 p1_32", "LLLLLLLL1RRRRRRR");
71
72 TestLayout(VEC1(a1_1), 8, 32, "1 32 1 4 a1_1", "LLLL1RRR");
73 TestLayout(VEC1(a2_1), 8, 32, "1 32 2 4 a2_1", "LLLL2RRR");
74 TestLayout(VEC1(a3_1), 8, 32, "1 32 3 4 a3_1", "LLLL3RRR");
75 TestLayout(VEC1(a4_1), 8, 32, "1 32 4 4 a4_1", "LLLL4RRR");
76 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");
78 TestLayout(VEC1(a9_1), 8, 32, "1 32 9 4 a9_1", "LLLL01RR");
79 TestLayout(VEC1(a16_1), 8, 32, "1 32 16 5 a16_1", "LLLL00RR");
80 TestLayout(VEC1(p1_256), 8, 32, "1 256 1 6 p1_256",
81 "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1RRR");
82 TestLayout(VEC1(a41_1), 8, 32, "1 32 41 5 a41_1", "LLLL000001RRRRRR");
83 TestLayout(VEC1(a105_1), 8, 32, "1 32 105 6 a105_1",
84 "LLLL00000000000001RRRRRR");
85
86 {
87 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");
91 }
92
93 {
94 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 }
99 #undef VEC1
100 #undef VEC
101 #undef VAR
102 }
103