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