xref: /llvm-project/llvm/lib/Transforms/Utils/ASanStackFrameLayout.cpp (revision db331d8be7f85cd976d4f63176cccd7d30aceacf)
1 //===-- ASanStackFrameLayout.cpp - helper for AddressSanitizer ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Definition of ComputeASanStackFrameLayout (see ASanStackFrameLayout.h).
11 //
12 //===----------------------------------------------------------------------===//
13 #include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Support/MathExtras.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <algorithm>
18 
19 namespace llvm {
20 
21 // We sort the stack variables by alignment (largest first) to minimize
22 // unnecessary large gaps due to alignment.
23 // It is tempting to also sort variables by size so that larger variables
24 // have larger redzones at both ends. But reordering will make report analysis
25 // harder, especially when temporary unnamed variables are present.
26 // So, until we can provide more information (type, line number, etc)
27 // for the stack variables we avoid reordering them too much.
28 static inline bool CompareVars(const ASanStackVariableDescription &a,
29                                const ASanStackVariableDescription &b) {
30   return a.Alignment > b.Alignment;
31 }
32 
33 // We also force minimal alignment for all vars to kMinAlignment so that vars
34 // with e.g. alignment 1 and alignment 16 do not get reordered by CompareVars.
35 static const size_t kMinAlignment = 16;
36 
37 // The larger the variable Size the larger is the redzone.
38 // The resulting frame size is a multiple of Alignment.
39 static size_t VarAndRedzoneSize(size_t Size, size_t Alignment) {
40   size_t Res = 0;
41   if (Size <= 4)  Res = 16;
42   else if (Size <= 16) Res = 32;
43   else if (Size <= 128) Res = Size + 32;
44   else if (Size <= 512) Res = Size + 64;
45   else if (Size <= 4096) Res = Size + 128;
46   else                   Res = Size + 256;
47   return alignTo(Res, Alignment);
48 }
49 
50 ASanStackFrameLayout
51 ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars,
52                             size_t Granularity, size_t MinHeaderSize) {
53   assert(Granularity >= 8 && Granularity <= 64 &&
54          (Granularity & (Granularity - 1)) == 0);
55   assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 &&
56          MinHeaderSize >= Granularity);
57   const size_t NumVars = Vars.size();
58   assert(NumVars > 0);
59   for (size_t i = 0; i < NumVars; i++)
60     Vars[i].Alignment = std::max(Vars[i].Alignment, kMinAlignment);
61 
62   std::stable_sort(Vars.begin(), Vars.end(), CompareVars);
63   SmallString<2048> StackDescriptionStorage;
64   raw_svector_ostream StackDescription(StackDescriptionStorage);
65   StackDescription << NumVars;
66 
67   ASanStackFrameLayout Layout;
68   Layout.Granularity = Granularity;
69   Layout.FrameAlignment = std::max(Granularity, Vars[0].Alignment);
70   size_t Offset = std::max(std::max(MinHeaderSize, Granularity),
71      Vars[0].Alignment);
72   assert((Offset % Granularity) == 0);
73   for (size_t i = 0; i < NumVars; i++) {
74     bool IsLast = i == NumVars - 1;
75     size_t Alignment = std::max(Granularity, Vars[i].Alignment);
76     (void)Alignment;  // Used only in asserts.
77     size_t Size = Vars[i].Size;
78     const char *Name = Vars[i].Name;
79     assert((Alignment & (Alignment - 1)) == 0);
80     assert(Layout.FrameAlignment >= Alignment);
81     assert((Offset % Alignment) == 0);
82     assert(Size > 0);
83     assert(Vars[i].LifetimeSize <= Size);
84     StackDescription << " " << Offset << " " << Size << " " << strlen(Name)
85                      << " " << Name;
86     size_t NextAlignment = IsLast ? Granularity
87                    : std::max(Granularity, Vars[i + 1].Alignment);
88     size_t SizeWithRedzone = VarAndRedzoneSize(Vars[i].Size, NextAlignment);
89     Vars[i].Offset = Offset;
90     Offset += SizeWithRedzone;
91   }
92   if (Offset % MinHeaderSize) {
93     Offset += MinHeaderSize - (Offset % MinHeaderSize);
94   }
95   Layout.DescriptionString = StackDescription.str();
96   Layout.FrameSize = Offset;
97   assert((Layout.FrameSize % MinHeaderSize) == 0);
98 
99   return Layout;
100 }
101 
102 SmallVector<uint8_t, 64>
103 GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> &Vars,
104                const ASanStackFrameLayout &Layout) {
105   SmallVector<uint8_t, 64> SB;
106   SB.clear();
107   const size_t NumVars = Vars.size();
108   assert(NumVars > 0);
109   const size_t Granularity = Layout.Granularity;
110   SB.resize(Vars[0].Offset / Granularity, kAsanStackLeftRedzoneMagic);
111   for (const auto &Var : Vars) {
112     SB.resize(Var.Offset / Granularity, kAsanStackMidRedzoneMagic);
113 
114     SB.resize(SB.size() + Var.Size / Granularity, 0);
115     if (Var.Size % Granularity)
116       SB.push_back(Var.Size % Granularity);
117   }
118   SB.resize(Layout.FrameSize / Granularity, kAsanStackRightRedzoneMagic);
119   return SB;
120 }
121 
122 SmallVector<uint8_t, 64> GetShadowBytesAfterScope(
123     const SmallVectorImpl<ASanStackVariableDescription> &Vars,
124     const ASanStackFrameLayout &Layout) {
125   SmallVector<uint8_t, 64> SB = GetShadowBytes(Vars, Layout);
126   const size_t Granularity = Layout.Granularity;
127 
128   for (const auto &Var : Vars) {
129     const size_t LifetimeShadowSize =
130         (Var.LifetimeSize + Granularity - 1) / Granularity;
131     const size_t Offset = Var.Offset / Granularity;
132     std::fill(SB.begin() + Offset, SB.begin() + Offset + LifetimeShadowSize,
133               kAsanStackUseAfterScopeMagic);
134   }
135 
136   return SB;
137 }
138 
139 } // llvm namespace
140