14fb7801bSKostya Serebryany //===-- ASanStackFrameLayout.cpp - helper for AddressSanitizer ------------===// 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 // 104fb7801bSKostya Serebryany // Definition of ComputeASanStackFrameLayout (see ASanStackFrameLayout.h). 114fb7801bSKostya Serebryany // 124fb7801bSKostya Serebryany //===----------------------------------------------------------------------===// 134fb7801bSKostya Serebryany #include "llvm/Transforms/Utils/ASanStackFrameLayout.h" 144fb7801bSKostya Serebryany #include "llvm/ADT/SmallString.h" 154fb7801bSKostya Serebryany #include "llvm/Support/raw_ostream.h" 164fb7801bSKostya Serebryany #include <algorithm> 174fb7801bSKostya Serebryany 184fb7801bSKostya Serebryany namespace llvm { 194fb7801bSKostya Serebryany 204fb7801bSKostya Serebryany // We sort the stack variables by alignment (largest first) to minimize 214fb7801bSKostya Serebryany // unnecessary large gaps due to alignment. 224fb7801bSKostya Serebryany // It is tempting to also sort variables by size so that larger variables 234fb7801bSKostya Serebryany // have larger redzones at both ends. But reordering will make report analysis 244fb7801bSKostya Serebryany // harder, especially when temporary unnamed variables are present. 254fb7801bSKostya Serebryany // So, until we can provide more information (type, line number, etc) 264fb7801bSKostya Serebryany // for the stack variables we avoid reordering them too much. 274fb7801bSKostya Serebryany static inline bool CompareVars(const ASanStackVariableDescription &a, 284fb7801bSKostya Serebryany const ASanStackVariableDescription &b) { 294fb7801bSKostya Serebryany return a.Alignment > b.Alignment; 304fb7801bSKostya Serebryany } 314fb7801bSKostya Serebryany 324fb7801bSKostya Serebryany // We also force minimal alignment for all vars to kMinAlignment so that vars 334fb7801bSKostya Serebryany // with e.g. alignment 1 and alignment 16 do not get reordered by CompareVars. 344fb7801bSKostya Serebryany static const size_t kMinAlignment = 16; 354fb7801bSKostya Serebryany 364fb7801bSKostya Serebryany static size_t RoundUpTo(size_t X, size_t RoundTo) { 374fb7801bSKostya Serebryany assert((RoundTo & (RoundTo - 1)) == 0); 384fb7801bSKostya Serebryany return (X + RoundTo - 1) & ~(RoundTo - 1); 394fb7801bSKostya Serebryany } 404fb7801bSKostya Serebryany 414fb7801bSKostya Serebryany // The larger the variable Size the larger is the redzone. 424fb7801bSKostya Serebryany // The resulting frame size is a multiple of Alignment. 434fb7801bSKostya Serebryany static size_t VarAndRedzoneSize(size_t Size, size_t Alignment) { 444fb7801bSKostya Serebryany size_t Res = 0; 454fb7801bSKostya Serebryany if (Size <= 4) Res = 16; 464fb7801bSKostya Serebryany else if (Size <= 16) Res = 32; 474fb7801bSKostya Serebryany else if (Size <= 128) Res = Size + 32; 484fb7801bSKostya Serebryany else if (Size <= 512) Res = Size + 64; 494fb7801bSKostya Serebryany else if (Size <= 4096) Res = Size + 128; 504fb7801bSKostya Serebryany else Res = Size + 256; 514fb7801bSKostya Serebryany return RoundUpTo(Res, Alignment); 524fb7801bSKostya Serebryany } 534fb7801bSKostya Serebryany 544fb7801bSKostya Serebryany void 554fb7801bSKostya Serebryany ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars, 564fb7801bSKostya Serebryany size_t Granularity, size_t MinHeaderSize, 574fb7801bSKostya Serebryany ASanStackFrameLayout *Layout) { 584fb7801bSKostya Serebryany assert(Granularity >= 8 && Granularity <= 64 && 594fb7801bSKostya Serebryany (Granularity & (Granularity - 1)) == 0); 604fb7801bSKostya Serebryany assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 && 614fb7801bSKostya Serebryany MinHeaderSize >= Granularity); 624fb7801bSKostya Serebryany size_t NumVars = Vars.size(); 634fb7801bSKostya Serebryany assert(NumVars > 0); 644fb7801bSKostya Serebryany for (size_t i = 0; i < NumVars; i++) 654fb7801bSKostya Serebryany Vars[i].Alignment = std::max(Vars[i].Alignment, kMinAlignment); 664fb7801bSKostya Serebryany 674fb7801bSKostya Serebryany std::stable_sort(Vars.begin(), Vars.end(), CompareVars); 68*e69170a1SAlp Toker SmallString<2048> StackDescriptionStorage; 69*e69170a1SAlp Toker raw_svector_ostream StackDescription(StackDescriptionStorage); 704fb7801bSKostya Serebryany StackDescription << NumVars; 714fb7801bSKostya Serebryany Layout->FrameAlignment = std::max(Granularity, Vars[0].Alignment); 724fb7801bSKostya Serebryany SmallVector<uint8_t, 64> &SB(Layout->ShadowBytes); 734fb7801bSKostya Serebryany SB.clear(); 744fb7801bSKostya Serebryany size_t Offset = std::max(std::max(MinHeaderSize, Granularity), 754fb7801bSKostya Serebryany Vars[0].Alignment); 764fb7801bSKostya Serebryany assert((Offset % Granularity) == 0); 774fb7801bSKostya Serebryany SB.insert(SB.end(), Offset / Granularity, kAsanStackLeftRedzoneMagic); 784fb7801bSKostya Serebryany for (size_t i = 0; i < NumVars; i++) { 794fb7801bSKostya Serebryany bool IsLast = i == NumVars - 1; 804fb7801bSKostya Serebryany size_t Alignment = std::max(Granularity, Vars[i].Alignment); 81152d48d3SKostya Serebryany (void)Alignment; // Used only in asserts. 824fb7801bSKostya Serebryany size_t Size = Vars[i].Size; 834fb7801bSKostya Serebryany const char *Name = Vars[i].Name; 844fb7801bSKostya Serebryany assert((Alignment & (Alignment - 1)) == 0); 854fb7801bSKostya Serebryany assert(Layout->FrameAlignment >= Alignment); 864fb7801bSKostya Serebryany assert((Offset % Alignment) == 0); 874fb7801bSKostya Serebryany assert(Size > 0); 884fb7801bSKostya Serebryany StackDescription << " " << Offset << " " << Size << " " << strlen(Name) 894fb7801bSKostya Serebryany << " " << Name; 904fb7801bSKostya Serebryany size_t NextAlignment = IsLast ? Granularity 914fb7801bSKostya Serebryany : std::max(Granularity, Vars[i + 1].Alignment); 924fb7801bSKostya Serebryany size_t SizeWithRedzone = VarAndRedzoneSize(Vars[i].Size, NextAlignment); 934fb7801bSKostya Serebryany SB.insert(SB.end(), Size / Granularity, 0); 944fb7801bSKostya Serebryany if (Size % Granularity) 954fb7801bSKostya Serebryany SB.insert(SB.end(), Size % Granularity); 964fb7801bSKostya Serebryany SB.insert(SB.end(), (SizeWithRedzone - Size) / Granularity, 974fb7801bSKostya Serebryany IsLast ? kAsanStackRightRedzoneMagic 984fb7801bSKostya Serebryany : kAsanStackMidRedzoneMagic); 994fb7801bSKostya Serebryany Vars[i].Offset = Offset; 1004fb7801bSKostya Serebryany Offset += SizeWithRedzone; 1014fb7801bSKostya Serebryany } 1024fb7801bSKostya Serebryany if (Offset % MinHeaderSize) { 1034fb7801bSKostya Serebryany size_t ExtraRedzone = MinHeaderSize - (Offset % MinHeaderSize); 1044fb7801bSKostya Serebryany SB.insert(SB.end(), ExtraRedzone / Granularity, 1054fb7801bSKostya Serebryany kAsanStackRightRedzoneMagic); 1064fb7801bSKostya Serebryany Offset += ExtraRedzone; 1074fb7801bSKostya Serebryany } 1084fb7801bSKostya Serebryany Layout->DescriptionString = StackDescription.str(); 1094fb7801bSKostya Serebryany Layout->FrameSize = Offset; 1104fb7801bSKostya Serebryany assert((Layout->FrameSize % MinHeaderSize) == 0); 1114fb7801bSKostya Serebryany assert(Layout->FrameSize / Granularity == Layout->ShadowBytes.size()); 1124fb7801bSKostya Serebryany } 1134fb7801bSKostya Serebryany 1144fb7801bSKostya Serebryany } // llvm namespace 115