15ca98fd9SDimitry Andric //===-- ASanStackFrameLayout.cpp - helper for AddressSanitizer ------------===//
25ca98fd9SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ca98fd9SDimitry Andric //
75ca98fd9SDimitry Andric //===----------------------------------------------------------------------===//
85ca98fd9SDimitry Andric //
95ca98fd9SDimitry Andric // Definition of ComputeASanStackFrameLayout (see ASanStackFrameLayout.h).
105ca98fd9SDimitry Andric //
115ca98fd9SDimitry Andric //===----------------------------------------------------------------------===//
125ca98fd9SDimitry Andric #include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
135ca98fd9SDimitry Andric #include "llvm/ADT/SmallString.h"
145a5ac124SDimitry Andric #include "llvm/Support/MathExtras.h"
15b915e9e0SDimitry Andric #include "llvm/Support/ScopedPrinter.h"
16dd58ef01SDimitry Andric #include "llvm/Support/raw_ostream.h"
175ca98fd9SDimitry Andric #include <algorithm>
185ca98fd9SDimitry Andric
195ca98fd9SDimitry Andric namespace llvm {
205ca98fd9SDimitry Andric
215ca98fd9SDimitry Andric // We sort the stack variables by alignment (largest first) to minimize
225ca98fd9SDimitry Andric // unnecessary large gaps due to alignment.
235ca98fd9SDimitry Andric // It is tempting to also sort variables by size so that larger variables
245ca98fd9SDimitry Andric // have larger redzones at both ends. But reordering will make report analysis
255ca98fd9SDimitry Andric // harder, especially when temporary unnamed variables are present.
265ca98fd9SDimitry Andric // So, until we can provide more information (type, line number, etc)
275ca98fd9SDimitry Andric // for the stack variables we avoid reordering them too much.
CompareVars(const ASanStackVariableDescription & a,const ASanStackVariableDescription & b)285ca98fd9SDimitry Andric static inline bool CompareVars(const ASanStackVariableDescription &a,
295ca98fd9SDimitry Andric const ASanStackVariableDescription &b) {
305ca98fd9SDimitry Andric return a.Alignment > b.Alignment;
315ca98fd9SDimitry Andric }
325ca98fd9SDimitry Andric
335ca98fd9SDimitry Andric // We also force minimal alignment for all vars to kMinAlignment so that vars
345ca98fd9SDimitry Andric // with e.g. alignment 1 and alignment 16 do not get reordered by CompareVars.
35c0981da4SDimitry Andric static const uint64_t kMinAlignment = 16;
365ca98fd9SDimitry Andric
37044eb2f6SDimitry Andric // We want to add a full redzone after every variable.
385ca98fd9SDimitry Andric // The larger the variable Size the larger is the redzone.
395ca98fd9SDimitry Andric // The resulting frame size is a multiple of Alignment.
VarAndRedzoneSize(uint64_t Size,uint64_t Granularity,uint64_t Alignment)40c0981da4SDimitry Andric static uint64_t VarAndRedzoneSize(uint64_t Size, uint64_t Granularity,
41c0981da4SDimitry Andric uint64_t Alignment) {
42c0981da4SDimitry Andric uint64_t Res = 0;
435ca98fd9SDimitry Andric if (Size <= 4) Res = 16;
445ca98fd9SDimitry Andric else if (Size <= 16) Res = 32;
455ca98fd9SDimitry Andric else if (Size <= 128) Res = Size + 32;
465ca98fd9SDimitry Andric else if (Size <= 512) Res = Size + 64;
475ca98fd9SDimitry Andric else if (Size <= 4096) Res = Size + 128;
485ca98fd9SDimitry Andric else Res = Size + 256;
49044eb2f6SDimitry Andric return alignTo(std::max(Res, 2 * Granularity), Alignment);
505ca98fd9SDimitry Andric }
515ca98fd9SDimitry Andric
52b915e9e0SDimitry Andric ASanStackFrameLayout
ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> & Vars,uint64_t Granularity,uint64_t MinHeaderSize)535ca98fd9SDimitry Andric ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars,
54c0981da4SDimitry Andric uint64_t Granularity, uint64_t MinHeaderSize) {
555ca98fd9SDimitry Andric assert(Granularity >= 8 && Granularity <= 64 &&
565ca98fd9SDimitry Andric (Granularity & (Granularity - 1)) == 0);
575ca98fd9SDimitry Andric assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 &&
585ca98fd9SDimitry Andric MinHeaderSize >= Granularity);
59b915e9e0SDimitry Andric const size_t NumVars = Vars.size();
605ca98fd9SDimitry Andric assert(NumVars > 0);
615ca98fd9SDimitry Andric for (size_t i = 0; i < NumVars; i++)
625ca98fd9SDimitry Andric Vars[i].Alignment = std::max(Vars[i].Alignment, kMinAlignment);
635ca98fd9SDimitry Andric
64e6d15924SDimitry Andric llvm::stable_sort(Vars, CompareVars);
65b915e9e0SDimitry Andric
66b915e9e0SDimitry Andric ASanStackFrameLayout Layout;
67b915e9e0SDimitry Andric Layout.Granularity = Granularity;
68b915e9e0SDimitry Andric Layout.FrameAlignment = std::max(Granularity, Vars[0].Alignment);
69c0981da4SDimitry Andric uint64_t Offset =
70c0981da4SDimitry Andric std::max(std::max(MinHeaderSize, Granularity), Vars[0].Alignment);
715ca98fd9SDimitry Andric assert((Offset % Granularity) == 0);
725ca98fd9SDimitry Andric for (size_t i = 0; i < NumVars; i++) {
735ca98fd9SDimitry Andric bool IsLast = i == NumVars - 1;
74c0981da4SDimitry Andric uint64_t Alignment = std::max(Granularity, Vars[i].Alignment);
755ca98fd9SDimitry Andric (void)Alignment; // Used only in asserts.
76c0981da4SDimitry Andric uint64_t Size = Vars[i].Size;
775ca98fd9SDimitry Andric assert((Alignment & (Alignment - 1)) == 0);
78b915e9e0SDimitry Andric assert(Layout.FrameAlignment >= Alignment);
795ca98fd9SDimitry Andric assert((Offset % Alignment) == 0);
805ca98fd9SDimitry Andric assert(Size > 0);
81c0981da4SDimitry Andric uint64_t NextAlignment =
82c0981da4SDimitry Andric IsLast ? Granularity : std::max(Granularity, Vars[i + 1].Alignment);
83c0981da4SDimitry Andric uint64_t SizeWithRedzone =
84c0981da4SDimitry Andric VarAndRedzoneSize(Size, Granularity, NextAlignment);
855ca98fd9SDimitry Andric Vars[i].Offset = Offset;
865ca98fd9SDimitry Andric Offset += SizeWithRedzone;
875ca98fd9SDimitry Andric }
885ca98fd9SDimitry Andric if (Offset % MinHeaderSize) {
89b915e9e0SDimitry Andric Offset += MinHeaderSize - (Offset % MinHeaderSize);
905ca98fd9SDimitry Andric }
91b915e9e0SDimitry Andric Layout.FrameSize = Offset;
92b915e9e0SDimitry Andric assert((Layout.FrameSize % MinHeaderSize) == 0);
93b915e9e0SDimitry Andric return Layout;
94b915e9e0SDimitry Andric }
95b915e9e0SDimitry Andric
ComputeASanStackFrameDescription(const SmallVectorImpl<ASanStackVariableDescription> & Vars)96b915e9e0SDimitry Andric SmallString<64> ComputeASanStackFrameDescription(
97b915e9e0SDimitry Andric const SmallVectorImpl<ASanStackVariableDescription> &Vars) {
98b915e9e0SDimitry Andric SmallString<2048> StackDescriptionStorage;
99b915e9e0SDimitry Andric raw_svector_ostream StackDescription(StackDescriptionStorage);
100b915e9e0SDimitry Andric StackDescription << Vars.size();
101b915e9e0SDimitry Andric
102b915e9e0SDimitry Andric for (const auto &Var : Vars) {
103b915e9e0SDimitry Andric std::string Name = Var.Name;
104b915e9e0SDimitry Andric if (Var.Line) {
105b915e9e0SDimitry Andric Name += ":";
106b915e9e0SDimitry Andric Name += to_string(Var.Line);
107b915e9e0SDimitry Andric }
108b915e9e0SDimitry Andric StackDescription << " " << Var.Offset << " " << Var.Size << " "
109b915e9e0SDimitry Andric << Name.size() << " " << Name;
110b915e9e0SDimitry Andric }
111b915e9e0SDimitry Andric return StackDescription.str();
112b915e9e0SDimitry Andric }
113b915e9e0SDimitry Andric
114b915e9e0SDimitry Andric SmallVector<uint8_t, 64>
GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> & Vars,const ASanStackFrameLayout & Layout)115b915e9e0SDimitry Andric GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> &Vars,
116b915e9e0SDimitry Andric const ASanStackFrameLayout &Layout) {
117b915e9e0SDimitry Andric assert(Vars.size() > 0);
118b915e9e0SDimitry Andric SmallVector<uint8_t, 64> SB;
119b915e9e0SDimitry Andric SB.clear();
120c0981da4SDimitry Andric const uint64_t Granularity = Layout.Granularity;
121b915e9e0SDimitry Andric SB.resize(Vars[0].Offset / Granularity, kAsanStackLeftRedzoneMagic);
122b915e9e0SDimitry Andric for (const auto &Var : Vars) {
123b915e9e0SDimitry Andric SB.resize(Var.Offset / Granularity, kAsanStackMidRedzoneMagic);
124b915e9e0SDimitry Andric
125b915e9e0SDimitry Andric SB.resize(SB.size() + Var.Size / Granularity, 0);
126b915e9e0SDimitry Andric if (Var.Size % Granularity)
127b915e9e0SDimitry Andric SB.push_back(Var.Size % Granularity);
128b915e9e0SDimitry Andric }
129b915e9e0SDimitry Andric SB.resize(Layout.FrameSize / Granularity, kAsanStackRightRedzoneMagic);
130b915e9e0SDimitry Andric return SB;
131b915e9e0SDimitry Andric }
132b915e9e0SDimitry Andric
GetShadowBytesAfterScope(const SmallVectorImpl<ASanStackVariableDescription> & Vars,const ASanStackFrameLayout & Layout)133b915e9e0SDimitry Andric SmallVector<uint8_t, 64> GetShadowBytesAfterScope(
134b915e9e0SDimitry Andric const SmallVectorImpl<ASanStackVariableDescription> &Vars,
135b915e9e0SDimitry Andric const ASanStackFrameLayout &Layout) {
136b915e9e0SDimitry Andric SmallVector<uint8_t, 64> SB = GetShadowBytes(Vars, Layout);
137c0981da4SDimitry Andric const uint64_t Granularity = Layout.Granularity;
138b915e9e0SDimitry Andric
139b915e9e0SDimitry Andric for (const auto &Var : Vars) {
140b915e9e0SDimitry Andric assert(Var.LifetimeSize <= Var.Size);
141c0981da4SDimitry Andric const uint64_t LifetimeShadowSize =
142b915e9e0SDimitry Andric (Var.LifetimeSize + Granularity - 1) / Granularity;
143c0981da4SDimitry Andric const uint64_t Offset = Var.Offset / Granularity;
144b915e9e0SDimitry Andric std::fill(SB.begin() + Offset, SB.begin() + Offset + LifetimeShadowSize,
145b915e9e0SDimitry Andric kAsanStackUseAfterScopeMagic);
146b915e9e0SDimitry Andric }
147b915e9e0SDimitry Andric
148b915e9e0SDimitry Andric return SB;
1495ca98fd9SDimitry Andric }
1505ca98fd9SDimitry Andric
1511a82d4c0SDimitry Andric } // llvm namespace
152