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