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