1*4fb7801bSKostya Serebryany //===-- ASanStackFrameLayout.cpp - helper for AddressSanitizer ------------===// 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 // 10*4fb7801bSKostya Serebryany // Definition of ComputeASanStackFrameLayout (see ASanStackFrameLayout.h). 11*4fb7801bSKostya Serebryany // 12*4fb7801bSKostya Serebryany //===----------------------------------------------------------------------===// 13*4fb7801bSKostya Serebryany #include "llvm/Transforms/Utils/ASanStackFrameLayout.h" 14*4fb7801bSKostya Serebryany #include "llvm/ADT/SmallString.h" 15*4fb7801bSKostya Serebryany #include "llvm/Support/raw_ostream.h" 16*4fb7801bSKostya Serebryany #include <algorithm> 17*4fb7801bSKostya Serebryany 18*4fb7801bSKostya Serebryany namespace llvm { 19*4fb7801bSKostya Serebryany 20*4fb7801bSKostya Serebryany // We sort the stack variables by alignment (largest first) to minimize 21*4fb7801bSKostya Serebryany // unnecessary large gaps due to alignment. 22*4fb7801bSKostya Serebryany // It is tempting to also sort variables by size so that larger variables 23*4fb7801bSKostya Serebryany // have larger redzones at both ends. But reordering will make report analysis 24*4fb7801bSKostya Serebryany // harder, especially when temporary unnamed variables are present. 25*4fb7801bSKostya Serebryany // So, until we can provide more information (type, line number, etc) 26*4fb7801bSKostya Serebryany // for the stack variables we avoid reordering them too much. 27*4fb7801bSKostya Serebryany static inline bool CompareVars(const ASanStackVariableDescription &a, 28*4fb7801bSKostya Serebryany const ASanStackVariableDescription &b) { 29*4fb7801bSKostya Serebryany return a.Alignment > b.Alignment; 30*4fb7801bSKostya Serebryany } 31*4fb7801bSKostya Serebryany 32*4fb7801bSKostya Serebryany // We also force minimal alignment for all vars to kMinAlignment so that vars 33*4fb7801bSKostya Serebryany // with e.g. alignment 1 and alignment 16 do not get reordered by CompareVars. 34*4fb7801bSKostya Serebryany static const size_t kMinAlignment = 16; 35*4fb7801bSKostya Serebryany 36*4fb7801bSKostya Serebryany static size_t RoundUpTo(size_t X, size_t RoundTo) { 37*4fb7801bSKostya Serebryany assert((RoundTo & (RoundTo - 1)) == 0); 38*4fb7801bSKostya Serebryany return (X + RoundTo - 1) & ~(RoundTo - 1); 39*4fb7801bSKostya Serebryany } 40*4fb7801bSKostya Serebryany 41*4fb7801bSKostya Serebryany // The larger the variable Size the larger is the redzone. 42*4fb7801bSKostya Serebryany // The resulting frame size is a multiple of Alignment. 43*4fb7801bSKostya Serebryany static size_t VarAndRedzoneSize(size_t Size, size_t Alignment) { 44*4fb7801bSKostya Serebryany size_t Res = 0; 45*4fb7801bSKostya Serebryany if (Size <= 4) Res = 16; 46*4fb7801bSKostya Serebryany else if (Size <= 16) Res = 32; 47*4fb7801bSKostya Serebryany else if (Size <= 128) Res = Size + 32; 48*4fb7801bSKostya Serebryany else if (Size <= 512) Res = Size + 64; 49*4fb7801bSKostya Serebryany else if (Size <= 4096) Res = Size + 128; 50*4fb7801bSKostya Serebryany else Res = Size + 256; 51*4fb7801bSKostya Serebryany return RoundUpTo(Res, Alignment); 52*4fb7801bSKostya Serebryany } 53*4fb7801bSKostya Serebryany 54*4fb7801bSKostya Serebryany void 55*4fb7801bSKostya Serebryany ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars, 56*4fb7801bSKostya Serebryany size_t Granularity, size_t MinHeaderSize, 57*4fb7801bSKostya Serebryany ASanStackFrameLayout *Layout) { 58*4fb7801bSKostya Serebryany assert(Granularity >= 8 && Granularity <= 64 && 59*4fb7801bSKostya Serebryany (Granularity & (Granularity - 1)) == 0); 60*4fb7801bSKostya Serebryany assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 && 61*4fb7801bSKostya Serebryany MinHeaderSize >= Granularity); 62*4fb7801bSKostya Serebryany size_t NumVars = Vars.size(); 63*4fb7801bSKostya Serebryany assert(NumVars > 0); 64*4fb7801bSKostya Serebryany for (size_t i = 0; i < NumVars; i++) 65*4fb7801bSKostya Serebryany Vars[i].Alignment = std::max(Vars[i].Alignment, kMinAlignment); 66*4fb7801bSKostya Serebryany 67*4fb7801bSKostya Serebryany std::stable_sort(Vars.begin(), Vars.end(), CompareVars); 68*4fb7801bSKostya Serebryany SmallString<2048> StackDescriptionStorage; 69*4fb7801bSKostya Serebryany raw_svector_ostream StackDescription(StackDescriptionStorage); 70*4fb7801bSKostya Serebryany StackDescription << NumVars; 71*4fb7801bSKostya Serebryany Layout->FrameAlignment = std::max(Granularity, Vars[0].Alignment); 72*4fb7801bSKostya Serebryany SmallVector<uint8_t, 64> &SB(Layout->ShadowBytes); 73*4fb7801bSKostya Serebryany SB.clear(); 74*4fb7801bSKostya Serebryany size_t Offset = std::max(std::max(MinHeaderSize, Granularity), 75*4fb7801bSKostya Serebryany Vars[0].Alignment); 76*4fb7801bSKostya Serebryany assert((Offset % Granularity) == 0); 77*4fb7801bSKostya Serebryany SB.insert(SB.end(), Offset / Granularity, kAsanStackLeftRedzoneMagic); 78*4fb7801bSKostya Serebryany for (size_t i = 0; i < NumVars; i++) { 79*4fb7801bSKostya Serebryany bool IsLast = i == NumVars - 1; 80*4fb7801bSKostya Serebryany size_t Alignment = std::max(Granularity, Vars[i].Alignment); 81*4fb7801bSKostya Serebryany size_t Size = Vars[i].Size; 82*4fb7801bSKostya Serebryany const char *Name = Vars[i].Name; 83*4fb7801bSKostya Serebryany assert((Alignment & (Alignment - 1)) == 0); 84*4fb7801bSKostya Serebryany assert(Layout->FrameAlignment >= Alignment); 85*4fb7801bSKostya Serebryany assert((Offset % Alignment) == 0); 86*4fb7801bSKostya Serebryany assert(Size > 0); 87*4fb7801bSKostya Serebryany StackDescription << " " << Offset << " " << Size << " " << strlen(Name) 88*4fb7801bSKostya Serebryany << " " << Name; 89*4fb7801bSKostya Serebryany size_t NextAlignment = IsLast ? Granularity 90*4fb7801bSKostya Serebryany : std::max(Granularity, Vars[i + 1].Alignment); 91*4fb7801bSKostya Serebryany size_t SizeWithRedzone = VarAndRedzoneSize(Vars[i].Size, NextAlignment); 92*4fb7801bSKostya Serebryany SB.insert(SB.end(), Size / Granularity, 0); 93*4fb7801bSKostya Serebryany if (Size % Granularity) 94*4fb7801bSKostya Serebryany SB.insert(SB.end(), Size % Granularity); 95*4fb7801bSKostya Serebryany SB.insert(SB.end(), (SizeWithRedzone - Size) / Granularity, 96*4fb7801bSKostya Serebryany IsLast ? kAsanStackRightRedzoneMagic 97*4fb7801bSKostya Serebryany : kAsanStackMidRedzoneMagic); 98*4fb7801bSKostya Serebryany Vars[i].Offset = Offset; 99*4fb7801bSKostya Serebryany Offset += SizeWithRedzone; 100*4fb7801bSKostya Serebryany } 101*4fb7801bSKostya Serebryany if (Offset % MinHeaderSize) { 102*4fb7801bSKostya Serebryany size_t ExtraRedzone = MinHeaderSize - (Offset % MinHeaderSize); 103*4fb7801bSKostya Serebryany SB.insert(SB.end(), ExtraRedzone / Granularity, 104*4fb7801bSKostya Serebryany kAsanStackRightRedzoneMagic); 105*4fb7801bSKostya Serebryany Offset += ExtraRedzone; 106*4fb7801bSKostya Serebryany } 107*4fb7801bSKostya Serebryany Layout->DescriptionString = StackDescription.str(); 108*4fb7801bSKostya Serebryany Layout->FrameSize = Offset; 109*4fb7801bSKostya Serebryany assert((Layout->FrameSize % MinHeaderSize) == 0); 110*4fb7801bSKostya Serebryany assert(Layout->FrameSize / Granularity == Layout->ShadowBytes.size()); 111*4fb7801bSKostya Serebryany } 112*4fb7801bSKostya Serebryany 113*4fb7801bSKostya Serebryany } // llvm namespace 114