Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 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" |
Vitaly Buka | d88e520 | 2016-10-18 23:29:41 +0000 | [diff] [blame] | 15 | #include "llvm/IR/DebugInfo.h" |
Evgeniy Stepanov | 292acab | 2015-02-16 14:49:37 +0000 | [diff] [blame] | 16 | #include "llvm/Support/MathExtras.h" |
Vitaly Buka | 490fda3 | 2016-10-19 00:16:56 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ScopedPrinter.h" |
Weiming Zhao | 45d4cb9 | 2015-11-24 18:57:06 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
| 21 | namespace 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. |
| 30 | static 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. |
| 37 | static const size_t kMinAlignment = 16; |
| 38 | |
Walter Lee | 9abeecc | 2017-11-18 01:13:18 +0000 | [diff] [blame] | 39 | // We want to add a full redzone after every variable. |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 40 | // The larger the variable Size the larger is the redzone. |
| 41 | // The resulting frame size is a multiple of Alignment. |
Walter Lee | 9abeecc | 2017-11-18 01:13:18 +0000 | [diff] [blame] | 42 | static size_t VarAndRedzoneSize(size_t Size, size_t Granularity, |
| 43 | size_t Alignment) { |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 44 | size_t Res = 0; |
| 45 | if (Size <= 4) Res = 16; |
| 46 | else if (Size <= 16) Res = 32; |
| 47 | else if (Size <= 128) Res = Size + 32; |
| 48 | else if (Size <= 512) Res = Size + 64; |
| 49 | else if (Size <= 4096) Res = Size + 128; |
| 50 | else Res = Size + 256; |
Walter Lee | 9abeecc | 2017-11-18 01:13:18 +0000 | [diff] [blame] | 51 | return alignTo(std::max(Res, 2 * Granularity), Alignment); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 54 | ASanStackFrameLayout |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 55 | ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars, |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 56 | size_t Granularity, size_t MinHeaderSize) { |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 57 | assert(Granularity >= 8 && Granularity <= 64 && |
| 58 | (Granularity & (Granularity - 1)) == 0); |
| 59 | assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 && |
| 60 | MinHeaderSize >= Granularity); |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 61 | const size_t NumVars = Vars.size(); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 62 | assert(NumVars > 0); |
| 63 | for (size_t i = 0; i < NumVars; i++) |
| 64 | Vars[i].Alignment = std::max(Vars[i].Alignment, kMinAlignment); |
| 65 | |
| 66 | std::stable_sort(Vars.begin(), Vars.end(), CompareVars); |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 67 | |
| 68 | ASanStackFrameLayout Layout; |
| 69 | Layout.Granularity = Granularity; |
| 70 | Layout.FrameAlignment = std::max(Granularity, Vars[0].Alignment); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 71 | size_t Offset = std::max(std::max(MinHeaderSize, Granularity), |
| 72 | Vars[0].Alignment); |
| 73 | assert((Offset % Granularity) == 0); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 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); |
Kostya Serebryany | 152d48d | 2013-12-06 09:26:09 +0000 | [diff] [blame] | 77 | (void)Alignment; // Used only in asserts. |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 78 | size_t Size = Vars[i].Size; |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 79 | assert((Alignment & (Alignment - 1)) == 0); |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 80 | assert(Layout.FrameAlignment >= Alignment); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 81 | assert((Offset % Alignment) == 0); |
| 82 | assert(Size > 0); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 83 | size_t NextAlignment = IsLast ? Granularity |
| 84 | : std::max(Granularity, Vars[i + 1].Alignment); |
Walter Lee | 9abeecc | 2017-11-18 01:13:18 +0000 | [diff] [blame] | 85 | size_t SizeWithRedzone = VarAndRedzoneSize(Size, Granularity, |
| 86 | NextAlignment); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 87 | Vars[i].Offset = Offset; |
| 88 | Offset += SizeWithRedzone; |
| 89 | } |
| 90 | if (Offset % MinHeaderSize) { |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 91 | Offset += MinHeaderSize - (Offset % MinHeaderSize); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 92 | } |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 93 | Layout.FrameSize = Offset; |
| 94 | assert((Layout.FrameSize % MinHeaderSize) == 0); |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 95 | return Layout; |
| 96 | } |
| 97 | |
Vitaly Buka | 5910a92 | 2016-10-18 23:29:52 +0000 | [diff] [blame] | 98 | SmallString<64> ComputeASanStackFrameDescription( |
| 99 | const SmallVectorImpl<ASanStackVariableDescription> &Vars) { |
| 100 | SmallString<2048> StackDescriptionStorage; |
| 101 | raw_svector_ostream StackDescription(StackDescriptionStorage); |
| 102 | StackDescription << Vars.size(); |
| 103 | |
| 104 | for (const auto &Var : Vars) { |
| 105 | std::string Name = Var.Name; |
| 106 | if (Var.Line) { |
| 107 | Name += ":"; |
Vitaly Buka | 490fda3 | 2016-10-19 00:16:56 +0000 | [diff] [blame] | 108 | Name += to_string(Var.Line); |
Vitaly Buka | 5910a92 | 2016-10-18 23:29:52 +0000 | [diff] [blame] | 109 | } |
| 110 | StackDescription << " " << Var.Offset << " " << Var.Size << " " |
| 111 | << Name.size() << " " << Name; |
| 112 | } |
| 113 | return StackDescription.str(); |
| 114 | } |
| 115 | |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 116 | SmallVector<uint8_t, 64> |
| 117 | GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> &Vars, |
| 118 | const ASanStackFrameLayout &Layout) { |
Tim Northover | c10c334 | 2016-08-29 19:12:20 +0000 | [diff] [blame] | 119 | assert(Vars.size() > 0); |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 120 | SmallVector<uint8_t, 64> SB; |
| 121 | SB.clear(); |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 122 | const size_t Granularity = Layout.Granularity; |
| 123 | SB.resize(Vars[0].Offset / Granularity, kAsanStackLeftRedzoneMagic); |
| 124 | for (const auto &Var : Vars) { |
| 125 | SB.resize(Var.Offset / Granularity, kAsanStackMidRedzoneMagic); |
| 126 | |
| 127 | SB.resize(SB.size() + Var.Size / Granularity, 0); |
| 128 | if (Var.Size % Granularity) |
| 129 | SB.push_back(Var.Size % Granularity); |
| 130 | } |
| 131 | SB.resize(Layout.FrameSize / Granularity, kAsanStackRightRedzoneMagic); |
| 132 | return SB; |
| 133 | } |
| 134 | |
| 135 | SmallVector<uint8_t, 64> GetShadowBytesAfterScope( |
| 136 | const SmallVectorImpl<ASanStackVariableDescription> &Vars, |
| 137 | const ASanStackFrameLayout &Layout) { |
| 138 | SmallVector<uint8_t, 64> SB = GetShadowBytes(Vars, Layout); |
| 139 | const size_t Granularity = Layout.Granularity; |
| 140 | |
| 141 | for (const auto &Var : Vars) { |
Vitaly Buka | 5910a92 | 2016-10-18 23:29:52 +0000 | [diff] [blame] | 142 | assert(Var.LifetimeSize <= Var.Size); |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 143 | const size_t LifetimeShadowSize = |
| 144 | (Var.LifetimeSize + Granularity - 1) / Granularity; |
| 145 | const size_t Offset = Var.Offset / Granularity; |
| 146 | std::fill(SB.begin() + Offset, SB.begin() + Offset + LifetimeShadowSize, |
| 147 | kAsanStackUseAfterScopeMagic); |
| 148 | } |
| 149 | |
| 150 | return SB; |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 153 | } // llvm namespace |