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