blob: df9d5da9e26e6037559ec179a3cb78d4ead9d0f7 [file] [log] [blame]
Kostya Serebryany4fb78012013-12-06 09:00:17 +00001//===-- 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"
Vitaly Bukad88e5202016-10-18 23:29:41 +000015#include "llvm/IR/DebugInfo.h"
Evgeniy Stepanov292acab2015-02-16 14:49:37 +000016#include "llvm/Support/MathExtras.h"
Vitaly Buka490fda32016-10-19 00:16:56 +000017#include "llvm/Support/ScopedPrinter.h"
Weiming Zhao45d4cb92015-11-24 18:57:06 +000018#include "llvm/Support/raw_ostream.h"
Kostya Serebryany4fb78012013-12-06 09:00:17 +000019#include <algorithm>
20
21namespace llvm {
22
23// We sort the stack variables by alignment (largest first) to minimize
24// unnecessary large gaps due to alignment.
25// It is tempting to also sort variables by size so that larger variables
26// have larger redzones at both ends. But reordering will make report analysis
27// harder, especially when temporary unnamed variables are present.
28// So, until we can provide more information (type, line number, etc)
29// for the stack variables we avoid reordering them too much.
30static inline bool CompareVars(const ASanStackVariableDescription &a,
31 const ASanStackVariableDescription &b) {
32 return a.Alignment > b.Alignment;
33}
34
35// We also force minimal alignment for all vars to kMinAlignment so that vars
36// with e.g. alignment 1 and alignment 16 do not get reordered by CompareVars.
37static const size_t kMinAlignment = 16;
38
Kostya Serebryany4fb78012013-12-06 09:00:17 +000039// The larger the variable Size the larger is the redzone.
40// The resulting frame size is a multiple of Alignment.
41static size_t VarAndRedzoneSize(size_t Size, size_t Alignment) {
42 size_t Res = 0;
43 if (Size <= 4) Res = 16;
44 else if (Size <= 16) Res = 32;
45 else if (Size <= 128) Res = Size + 32;
46 else if (Size <= 512) Res = Size + 64;
47 else if (Size <= 4096) Res = Size + 128;
48 else Res = Size + 256;
Rui Ueyamada00f2f2016-01-14 21:06:47 +000049 return alignTo(Res, Alignment);
Kostya Serebryany4fb78012013-12-06 09:00:17 +000050}
51
Vitaly Bukadb331d82016-08-29 17:41:29 +000052ASanStackFrameLayout
Kostya Serebryany4fb78012013-12-06 09:00:17 +000053ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars,
Vitaly Bukadb331d82016-08-29 17:41:29 +000054 size_t Granularity, size_t MinHeaderSize) {
Kostya Serebryany4fb78012013-12-06 09:00:17 +000055 assert(Granularity >= 8 && Granularity <= 64 &&
56 (Granularity & (Granularity - 1)) == 0);
57 assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 &&
58 MinHeaderSize >= Granularity);
Vitaly Bukadb331d82016-08-29 17:41:29 +000059 const size_t NumVars = Vars.size();
Kostya Serebryany4fb78012013-12-06 09:00:17 +000060 assert(NumVars > 0);
61 for (size_t i = 0; i < NumVars; i++)
62 Vars[i].Alignment = std::max(Vars[i].Alignment, kMinAlignment);
63
64 std::stable_sort(Vars.begin(), Vars.end(), CompareVars);
Vitaly Bukadb331d82016-08-29 17:41:29 +000065
66 ASanStackFrameLayout Layout;
67 Layout.Granularity = Granularity;
68 Layout.FrameAlignment = std::max(Granularity, Vars[0].Alignment);
Kostya Serebryany4fb78012013-12-06 09:00:17 +000069 size_t Offset = std::max(std::max(MinHeaderSize, Granularity),
70 Vars[0].Alignment);
71 assert((Offset % Granularity) == 0);
Kostya Serebryany4fb78012013-12-06 09:00:17 +000072 for (size_t i = 0; i < NumVars; i++) {
73 bool IsLast = i == NumVars - 1;
74 size_t Alignment = std::max(Granularity, Vars[i].Alignment);
Kostya Serebryany152d48d2013-12-06 09:26:09 +000075 (void)Alignment; // Used only in asserts.
Kostya Serebryany4fb78012013-12-06 09:00:17 +000076 size_t Size = Vars[i].Size;
Kostya Serebryany4fb78012013-12-06 09:00:17 +000077 assert((Alignment & (Alignment - 1)) == 0);
Vitaly Bukadb331d82016-08-29 17:41:29 +000078 assert(Layout.FrameAlignment >= Alignment);
Kostya Serebryany4fb78012013-12-06 09:00:17 +000079 assert((Offset % Alignment) == 0);
80 assert(Size > 0);
Kostya Serebryany4fb78012013-12-06 09:00:17 +000081 size_t NextAlignment = IsLast ? Granularity
82 : std::max(Granularity, Vars[i + 1].Alignment);
Vitaly Buka5910a922016-10-18 23:29:52 +000083 size_t SizeWithRedzone = VarAndRedzoneSize(Size, NextAlignment);
Kostya Serebryany4fb78012013-12-06 09:00:17 +000084 Vars[i].Offset = Offset;
85 Offset += SizeWithRedzone;
86 }
87 if (Offset % MinHeaderSize) {
Vitaly Bukadb331d82016-08-29 17:41:29 +000088 Offset += MinHeaderSize - (Offset % MinHeaderSize);
Kostya Serebryany4fb78012013-12-06 09:00:17 +000089 }
Vitaly Bukadb331d82016-08-29 17:41:29 +000090 Layout.FrameSize = Offset;
91 assert((Layout.FrameSize % MinHeaderSize) == 0);
Vitaly Bukadb331d82016-08-29 17:41:29 +000092 return Layout;
93}
94
Vitaly Buka5910a922016-10-18 23:29:52 +000095SmallString<64> ComputeASanStackFrameDescription(
96 const SmallVectorImpl<ASanStackVariableDescription> &Vars) {
97 SmallString<2048> StackDescriptionStorage;
98 raw_svector_ostream StackDescription(StackDescriptionStorage);
99 StackDescription << Vars.size();
100
101 for (const auto &Var : Vars) {
102 std::string Name = Var.Name;
103 if (Var.Line) {
104 Name += ":";
Vitaly Buka490fda32016-10-19 00:16:56 +0000105 Name += to_string(Var.Line);
Vitaly Buka5910a922016-10-18 23:29:52 +0000106 }
107 StackDescription << " " << Var.Offset << " " << Var.Size << " "
108 << Name.size() << " " << Name;
109 }
110 return StackDescription.str();
111}
112
Vitaly Bukadb331d82016-08-29 17:41:29 +0000113SmallVector<uint8_t, 64>
114GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> &Vars,
115 const ASanStackFrameLayout &Layout) {
Tim Northoverc10c3342016-08-29 19:12:20 +0000116 assert(Vars.size() > 0);
Vitaly Bukadb331d82016-08-29 17:41:29 +0000117 SmallVector<uint8_t, 64> SB;
118 SB.clear();
Vitaly Bukadb331d82016-08-29 17:41:29 +0000119 const size_t Granularity = Layout.Granularity;
120 SB.resize(Vars[0].Offset / Granularity, kAsanStackLeftRedzoneMagic);
121 for (const auto &Var : Vars) {
122 SB.resize(Var.Offset / Granularity, kAsanStackMidRedzoneMagic);
123
124 SB.resize(SB.size() + Var.Size / Granularity, 0);
125 if (Var.Size % Granularity)
126 SB.push_back(Var.Size % Granularity);
127 }
128 SB.resize(Layout.FrameSize / Granularity, kAsanStackRightRedzoneMagic);
129 return SB;
130}
131
132SmallVector<uint8_t, 64> GetShadowBytesAfterScope(
133 const SmallVectorImpl<ASanStackVariableDescription> &Vars,
134 const ASanStackFrameLayout &Layout) {
135 SmallVector<uint8_t, 64> SB = GetShadowBytes(Vars, Layout);
136 const size_t Granularity = Layout.Granularity;
137
138 for (const auto &Var : Vars) {
Vitaly Buka5910a922016-10-18 23:29:52 +0000139 assert(Var.LifetimeSize <= Var.Size);
Vitaly Bukadb331d82016-08-29 17:41:29 +0000140 const size_t LifetimeShadowSize =
141 (Var.LifetimeSize + Granularity - 1) / Granularity;
142 const size_t Offset = Var.Offset / Granularity;
143 std::fill(SB.begin() + Offset, SB.begin() + Offset + LifetimeShadowSize,
144 kAsanStackUseAfterScopeMagic);
145 }
146
147 return SB;
Kostya Serebryany4fb78012013-12-06 09:00:17 +0000148}
149
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000150} // llvm namespace