14fb7801bSKostya Serebryany //===- ASanStackFrameLayoutTest.cpp - Tests for ComputeASanStackFrameLayout===// 24fb7801bSKostya Serebryany // 34fb7801bSKostya Serebryany // The LLVM Compiler Infrastructure 44fb7801bSKostya Serebryany // 54fb7801bSKostya Serebryany // This file is distributed under the University of Illinois Open Source 64fb7801bSKostya Serebryany // License. See LICENSE.TXT for details. 74fb7801bSKostya Serebryany // 84fb7801bSKostya Serebryany //===----------------------------------------------------------------------===// 94fb7801bSKostya Serebryany #include "llvm/Transforms/Utils/ASanStackFrameLayout.h" 104fb7801bSKostya Serebryany #include "llvm/ADT/ArrayRef.h" 114fb7801bSKostya Serebryany #include "gtest/gtest.h" 128a8cd2baSChandler Carruth #include <sstream> 134fb7801bSKostya Serebryany 144fb7801bSKostya Serebryany using namespace llvm; 154fb7801bSKostya Serebryany 164fb7801bSKostya Serebryany static std::string 174fb7801bSKostya Serebryany ShadowBytesToString(ArrayRef<uint8_t> ShadowBytes) { 184fb7801bSKostya Serebryany std::ostringstream os; 194fb7801bSKostya Serebryany for (size_t i = 0, n = ShadowBytes.size(); i < n; i++) { 204fb7801bSKostya Serebryany switch (ShadowBytes[i]) { 214fb7801bSKostya Serebryany case kAsanStackLeftRedzoneMagic: os << "L"; break; 224fb7801bSKostya Serebryany case kAsanStackRightRedzoneMagic: os << "R"; break; 234fb7801bSKostya Serebryany case kAsanStackMidRedzoneMagic: os << "M"; break; 24*f9fd63adSVitaly Buka case kAsanStackUseAfterScopeMagic: 25*f9fd63adSVitaly Buka os << "S"; 26*f9fd63adSVitaly Buka break; 274fb7801bSKostya Serebryany default: os << (unsigned)ShadowBytes[i]; 284fb7801bSKostya Serebryany } 294fb7801bSKostya Serebryany } 304fb7801bSKostya Serebryany return os.str(); 314fb7801bSKostya Serebryany } 324fb7801bSKostya Serebryany 334fb7801bSKostya Serebryany static void TestLayout(SmallVector<ASanStackVariableDescription, 10> Vars, 344fb7801bSKostya Serebryany size_t Granularity, size_t MinHeaderSize, 354fb7801bSKostya Serebryany const std::string &ExpectedDescr, 364fb7801bSKostya Serebryany const std::string &ExpectedShadow) { 374fb7801bSKostya Serebryany ASanStackFrameLayout L; 384fb7801bSKostya Serebryany ComputeASanStackFrameLayout(Vars, Granularity, MinHeaderSize, &L); 394fb7801bSKostya Serebryany EXPECT_EQ(ExpectedDescr, L.DescriptionString); 404fb7801bSKostya Serebryany EXPECT_EQ(ExpectedShadow, ShadowBytesToString(L.ShadowBytes)); 414fb7801bSKostya Serebryany } 424fb7801bSKostya Serebryany 434fb7801bSKostya Serebryany TEST(ASanStackFrameLayout, Test) { 444fb7801bSKostya Serebryany #define VEC1(a) SmallVector<ASanStackVariableDescription, 10>(1, a) 454fb7801bSKostya Serebryany #define VEC(a) \ 464fb7801bSKostya Serebryany SmallVector<ASanStackVariableDescription, 10>(a, a + sizeof(a) / sizeof(a[0])) 474fb7801bSKostya Serebryany 48*f9fd63adSVitaly Buka #define VAR(name, size, lifetime, alignment) \ 494fb7801bSKostya Serebryany ASanStackVariableDescription name##size##_##alignment = { \ 504fb7801bSKostya Serebryany #name #size "_" #alignment, \ 514fb7801bSKostya Serebryany size, \ 52*f9fd63adSVitaly Buka lifetime, \ 534fb7801bSKostya Serebryany alignment, \ 544fb7801bSKostya Serebryany 0, \ 554fb7801bSKostya Serebryany 0 \ 564fb7801bSKostya Serebryany } 574fb7801bSKostya Serebryany 58*f9fd63adSVitaly Buka VAR(a, 1, 0, 1); 59*f9fd63adSVitaly Buka VAR(p, 1, 0, 32); 60*f9fd63adSVitaly Buka VAR(p, 1, 0, 256); 61*f9fd63adSVitaly Buka VAR(a, 2, 0, 1); 62*f9fd63adSVitaly Buka VAR(a, 3, 0, 1); 63*f9fd63adSVitaly Buka VAR(a, 4, 0, 1); 64*f9fd63adSVitaly Buka VAR(a, 7, 0, 1); 65*f9fd63adSVitaly Buka VAR(a, 8, 8, 1); 66*f9fd63adSVitaly Buka VAR(a, 9, 0, 1); 67*f9fd63adSVitaly Buka VAR(a, 16, 0, 1); 68*f9fd63adSVitaly Buka VAR(a, 41, 9, 1); 69*f9fd63adSVitaly Buka VAR(a, 105, 103, 1); 704fb7801bSKostya Serebryany 714fb7801bSKostya Serebryany TestLayout(VEC1(a1_1), 8, 16, "1 16 1 4 a1_1", "LL1R"); 724fb7801bSKostya Serebryany TestLayout(VEC1(a1_1), 64, 64, "1 64 1 4 a1_1", "L1"); 734fb7801bSKostya Serebryany TestLayout(VEC1(p1_32), 8, 32, "1 32 1 5 p1_32", "LLLL1RRR"); 744fb7801bSKostya Serebryany TestLayout(VEC1(p1_32), 8, 64, "1 64 1 5 p1_32", "LLLLLLLL1RRRRRRR"); 754fb7801bSKostya Serebryany 764fb7801bSKostya Serebryany TestLayout(VEC1(a1_1), 8, 32, "1 32 1 4 a1_1", "LLLL1RRR"); 774fb7801bSKostya Serebryany TestLayout(VEC1(a2_1), 8, 32, "1 32 2 4 a2_1", "LLLL2RRR"); 784fb7801bSKostya Serebryany TestLayout(VEC1(a3_1), 8, 32, "1 32 3 4 a3_1", "LLLL3RRR"); 794fb7801bSKostya Serebryany TestLayout(VEC1(a4_1), 8, 32, "1 32 4 4 a4_1", "LLLL4RRR"); 804fb7801bSKostya Serebryany TestLayout(VEC1(a7_1), 8, 32, "1 32 7 4 a7_1", "LLLL7RRR"); 81*f9fd63adSVitaly Buka TestLayout(VEC1(a8_1), 8, 32, "1 32 8 4 a8_1", "LLLLSRRR"); 824fb7801bSKostya Serebryany TestLayout(VEC1(a9_1), 8, 32, "1 32 9 4 a9_1", "LLLL01RR"); 834fb7801bSKostya Serebryany TestLayout(VEC1(a16_1), 8, 32, "1 32 16 5 a16_1", "LLLL00RR"); 844fb7801bSKostya Serebryany TestLayout(VEC1(p1_256), 8, 32, "1 256 1 6 p1_256", 854fb7801bSKostya Serebryany "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1RRR"); 86*f9fd63adSVitaly Buka TestLayout(VEC1(a41_1), 8, 32, "1 32 41 5 a41_1", "LLLLSS0001RRRRRR"); 874fb7801bSKostya Serebryany TestLayout(VEC1(a105_1), 8, 32, "1 32 105 6 a105_1", 88*f9fd63adSVitaly Buka "LLLLSSSSSSSSSSSSS1RRRRRR"); 894fb7801bSKostya Serebryany 904fb7801bSKostya Serebryany { 914fb7801bSKostya Serebryany ASanStackVariableDescription t[] = {a1_1, p1_256}; 92*f9fd63adSVitaly Buka TestLayout(VEC(t), 8, 32, "2 256 1 6 p1_256 272 1 4 a1_1", 93*f9fd63adSVitaly Buka "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1M1R"); 944fb7801bSKostya Serebryany } 954fb7801bSKostya Serebryany 964fb7801bSKostya Serebryany { 974fb7801bSKostya Serebryany ASanStackVariableDescription t[] = {a1_1, a16_1, a41_1}; 98*f9fd63adSVitaly Buka TestLayout(VEC(t), 8, 32, "3 32 1 4 a1_1 48 16 5 a16_1 80 41 5 a41_1", 99*f9fd63adSVitaly Buka "LLLL1M00MMSS0001RRRR"); 1004fb7801bSKostya Serebryany } 1014fb7801bSKostya Serebryany #undef VEC1 1024fb7801bSKostya Serebryany #undef VEC 1034fb7801bSKostya Serebryany #undef VAR 1044fb7801bSKostya Serebryany } 105