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