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