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" 12*8a8cd2baSChandler 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; 244fb7801bSKostya Serebryany default: os << (unsigned)ShadowBytes[i]; 254fb7801bSKostya Serebryany } 264fb7801bSKostya Serebryany } 274fb7801bSKostya Serebryany return os.str(); 284fb7801bSKostya Serebryany } 294fb7801bSKostya Serebryany 304fb7801bSKostya Serebryany static void TestLayout(SmallVector<ASanStackVariableDescription, 10> Vars, 314fb7801bSKostya Serebryany size_t Granularity, size_t MinHeaderSize, 324fb7801bSKostya Serebryany const std::string &ExpectedDescr, 334fb7801bSKostya Serebryany const std::string &ExpectedShadow) { 344fb7801bSKostya Serebryany ASanStackFrameLayout L; 354fb7801bSKostya Serebryany ComputeASanStackFrameLayout(Vars, Granularity, MinHeaderSize, &L); 364fb7801bSKostya Serebryany EXPECT_EQ(ExpectedDescr, L.DescriptionString); 374fb7801bSKostya Serebryany EXPECT_EQ(ExpectedShadow, ShadowBytesToString(L.ShadowBytes)); 384fb7801bSKostya Serebryany } 394fb7801bSKostya Serebryany 404fb7801bSKostya Serebryany TEST(ASanStackFrameLayout, Test) { 414fb7801bSKostya Serebryany #define VEC1(a) SmallVector<ASanStackVariableDescription, 10>(1, a) 424fb7801bSKostya Serebryany #define VEC(a) \ 434fb7801bSKostya Serebryany SmallVector<ASanStackVariableDescription, 10>(a, a + sizeof(a) / sizeof(a[0])) 444fb7801bSKostya Serebryany 454fb7801bSKostya Serebryany #define VAR(name, size, alignment) \ 464fb7801bSKostya Serebryany ASanStackVariableDescription name##size##_##alignment = { \ 474fb7801bSKostya Serebryany #name #size "_" #alignment, \ 484fb7801bSKostya Serebryany size, \ 494fb7801bSKostya Serebryany alignment, \ 504fb7801bSKostya Serebryany 0, \ 514fb7801bSKostya Serebryany 0 \ 524fb7801bSKostya Serebryany } 534fb7801bSKostya Serebryany 544fb7801bSKostya Serebryany VAR(a, 1, 1); 554fb7801bSKostya Serebryany VAR(p, 1, 32); 564fb7801bSKostya Serebryany VAR(p, 1, 256); 574fb7801bSKostya Serebryany VAR(a, 2, 1); 584fb7801bSKostya Serebryany VAR(a, 3, 1); 594fb7801bSKostya Serebryany VAR(a, 4, 1); 604fb7801bSKostya Serebryany VAR(a, 7, 1); 614fb7801bSKostya Serebryany VAR(a, 8, 1); 624fb7801bSKostya Serebryany VAR(a, 9, 1); 634fb7801bSKostya Serebryany VAR(a, 16, 1); 644fb7801bSKostya Serebryany VAR(a, 41, 1); 654fb7801bSKostya Serebryany VAR(a, 105, 1); 664fb7801bSKostya Serebryany 674fb7801bSKostya Serebryany TestLayout(VEC1(a1_1), 8, 16, "1 16 1 4 a1_1", "LL1R"); 684fb7801bSKostya Serebryany TestLayout(VEC1(a1_1), 64, 64, "1 64 1 4 a1_1", "L1"); 694fb7801bSKostya Serebryany TestLayout(VEC1(p1_32), 8, 32, "1 32 1 5 p1_32", "LLLL1RRR"); 704fb7801bSKostya Serebryany TestLayout(VEC1(p1_32), 8, 64, "1 64 1 5 p1_32", "LLLLLLLL1RRRRRRR"); 714fb7801bSKostya Serebryany 724fb7801bSKostya Serebryany TestLayout(VEC1(a1_1), 8, 32, "1 32 1 4 a1_1", "LLLL1RRR"); 734fb7801bSKostya Serebryany TestLayout(VEC1(a2_1), 8, 32, "1 32 2 4 a2_1", "LLLL2RRR"); 744fb7801bSKostya Serebryany TestLayout(VEC1(a3_1), 8, 32, "1 32 3 4 a3_1", "LLLL3RRR"); 754fb7801bSKostya Serebryany TestLayout(VEC1(a4_1), 8, 32, "1 32 4 4 a4_1", "LLLL4RRR"); 764fb7801bSKostya Serebryany TestLayout(VEC1(a7_1), 8, 32, "1 32 7 4 a7_1", "LLLL7RRR"); 774fb7801bSKostya Serebryany TestLayout(VEC1(a8_1), 8, 32, "1 32 8 4 a8_1", "LLLL0RRR"); 784fb7801bSKostya Serebryany TestLayout(VEC1(a9_1), 8, 32, "1 32 9 4 a9_1", "LLLL01RR"); 794fb7801bSKostya Serebryany TestLayout(VEC1(a16_1), 8, 32, "1 32 16 5 a16_1", "LLLL00RR"); 804fb7801bSKostya Serebryany TestLayout(VEC1(p1_256), 8, 32, "1 256 1 6 p1_256", 814fb7801bSKostya Serebryany "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1RRR"); 824fb7801bSKostya Serebryany TestLayout(VEC1(a41_1), 8, 32, "1 32 41 5 a41_1", "LLLL000001RRRRRR"); 834fb7801bSKostya Serebryany TestLayout(VEC1(a105_1), 8, 32, "1 32 105 6 a105_1", 844fb7801bSKostya Serebryany "LLLL00000000000001RRRRRR"); 854fb7801bSKostya Serebryany 864fb7801bSKostya Serebryany { 874fb7801bSKostya Serebryany ASanStackVariableDescription t[] = {a1_1, p1_256}; 884fb7801bSKostya Serebryany TestLayout(VEC(t), 8, 32, 894fb7801bSKostya Serebryany "2 256 1 6 p1_256 272 1 4 a1_1", 904fb7801bSKostya Serebryany "LLLLLLLL" "LLLLLLLL" "LLLLLLLL" "LLLLLLLL" "1M1R"); 914fb7801bSKostya Serebryany } 924fb7801bSKostya Serebryany 934fb7801bSKostya Serebryany { 944fb7801bSKostya Serebryany ASanStackVariableDescription t[] = {a1_1, a16_1, a41_1}; 954fb7801bSKostya Serebryany TestLayout(VEC(t), 8, 32, 964fb7801bSKostya Serebryany "3 32 1 4 a1_1 48 16 5 a16_1 80 41 5 a41_1", 974fb7801bSKostya Serebryany "LLLL" "1M00" "MM00" "0001" "RRRR"); 984fb7801bSKostya Serebryany } 994fb7801bSKostya Serebryany #undef VEC1 1004fb7801bSKostya Serebryany #undef VEC 1014fb7801bSKostya Serebryany #undef VAR 1024fb7801bSKostya Serebryany } 103