xref: /llvm-project/llvm/lib/Transforms/Utils/ASanStackFrameLayout.cpp (revision d88e52012bd8e73f06621658b33cecb7ffa150a6)
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   SmallString<2048> StackDescriptionStorage;
65   raw_svector_ostream StackDescription(StackDescriptionStorage);
66   StackDescription << NumVars;
67 
68   ASanStackFrameLayout Layout;
69   Layout.Granularity = Granularity;
70   Layout.FrameAlignment = std::max(Granularity, Vars[0].Alignment);
71   size_t Offset = std::max(std::max(MinHeaderSize, Granularity),
72      Vars[0].Alignment);
73   assert((Offset % Granularity) == 0);
74   for (size_t i = 0; i < NumVars; i++) {
75     bool IsLast = i == NumVars - 1;
76     size_t Alignment = std::max(Granularity, Vars[i].Alignment);
77     (void)Alignment;  // Used only in asserts.
78     size_t Size = Vars[i].Size;
79     std::string Name = Vars[i].Name;
80     assert((Alignment & (Alignment - 1)) == 0);
81     assert(Layout.FrameAlignment >= Alignment);
82     assert((Offset % Alignment) == 0);
83     assert(Size > 0);
84     assert(Vars[i].LifetimeSize <= Size);
85     if (Vars[i].Line) {
86       Name += ":";
87       Name += std::to_string(Vars[i].Line);
88     }
89     StackDescription << " " << Offset << " " << Size << " " << Name.size()
90                      << " " << Name;
91     size_t NextAlignment = IsLast ? Granularity
92                    : std::max(Granularity, Vars[i + 1].Alignment);
93     size_t SizeWithRedzone = VarAndRedzoneSize(Vars[i].Size, NextAlignment);
94     Vars[i].Offset = Offset;
95     Offset += SizeWithRedzone;
96   }
97   if (Offset % MinHeaderSize) {
98     Offset += MinHeaderSize - (Offset % MinHeaderSize);
99   }
100   Layout.DescriptionString = StackDescription.str();
101   Layout.FrameSize = Offset;
102   assert((Layout.FrameSize % MinHeaderSize) == 0);
103 
104   return Layout;
105 }
106 
107 SmallVector<uint8_t, 64>
108 GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> &Vars,
109                const ASanStackFrameLayout &Layout) {
110   assert(Vars.size() > 0);
111   SmallVector<uint8_t, 64> SB;
112   SB.clear();
113   const size_t Granularity = Layout.Granularity;
114   SB.resize(Vars[0].Offset / Granularity, kAsanStackLeftRedzoneMagic);
115   for (const auto &Var : Vars) {
116     SB.resize(Var.Offset / Granularity, kAsanStackMidRedzoneMagic);
117 
118     SB.resize(SB.size() + Var.Size / Granularity, 0);
119     if (Var.Size % Granularity)
120       SB.push_back(Var.Size % Granularity);
121   }
122   SB.resize(Layout.FrameSize / Granularity, kAsanStackRightRedzoneMagic);
123   return SB;
124 }
125 
126 SmallVector<uint8_t, 64> GetShadowBytesAfterScope(
127     const SmallVectorImpl<ASanStackVariableDescription> &Vars,
128     const ASanStackFrameLayout &Layout) {
129   SmallVector<uint8_t, 64> SB = GetShadowBytes(Vars, Layout);
130   const size_t Granularity = Layout.Granularity;
131 
132   for (const auto &Var : Vars) {
133     const size_t LifetimeShadowSize =
134         (Var.LifetimeSize + Granularity - 1) / Granularity;
135     const size_t Offset = Var.Offset / Granularity;
136     std::fill(SB.begin() + Offset, SB.begin() + Offset + LifetimeShadowSize,
137               kAsanStackUseAfterScopeMagic);
138   }
139 
140   return SB;
141 }
142 
143 } // llvm namespace
144