10b57cec5SDimitry Andric //===-- ASanStackFrameLayout.cpp - helper for AddressSanitizer ------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Definition of ComputeASanStackFrameLayout (see ASanStackFrameLayout.h).
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric #include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
130b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
140b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
150b57cec5SDimitry Andric #include "llvm/Support/ScopedPrinter.h"
160b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
170b57cec5SDimitry Andric #include <algorithm>
180b57cec5SDimitry Andric
190b57cec5SDimitry Andric namespace llvm {
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric // We sort the stack variables by alignment (largest first) to minimize
220b57cec5SDimitry Andric // unnecessary large gaps due to alignment.
230b57cec5SDimitry Andric // It is tempting to also sort variables by size so that larger variables
240b57cec5SDimitry Andric // have larger redzones at both ends. But reordering will make report analysis
250b57cec5SDimitry Andric // harder, especially when temporary unnamed variables are present.
260b57cec5SDimitry Andric // So, until we can provide more information (type, line number, etc)
270b57cec5SDimitry Andric // for the stack variables we avoid reordering them too much.
CompareVars(const ASanStackVariableDescription & a,const ASanStackVariableDescription & b)280b57cec5SDimitry Andric static inline bool CompareVars(const ASanStackVariableDescription &a,
290b57cec5SDimitry Andric const ASanStackVariableDescription &b) {
300b57cec5SDimitry Andric return a.Alignment > b.Alignment;
310b57cec5SDimitry Andric }
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric // We also force minimal alignment for all vars to kMinAlignment so that vars
340b57cec5SDimitry Andric // with e.g. alignment 1 and alignment 16 do not get reordered by CompareVars.
35*349cc55cSDimitry Andric static const uint64_t kMinAlignment = 16;
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric // We want to add a full redzone after every variable.
380b57cec5SDimitry Andric // The larger the variable Size the larger is the redzone.
390b57cec5SDimitry Andric // The resulting frame size is a multiple of Alignment.
VarAndRedzoneSize(uint64_t Size,uint64_t Granularity,uint64_t Alignment)40*349cc55cSDimitry Andric static uint64_t VarAndRedzoneSize(uint64_t Size, uint64_t Granularity,
41*349cc55cSDimitry Andric uint64_t Alignment) {
42*349cc55cSDimitry Andric uint64_t Res = 0;
430b57cec5SDimitry Andric if (Size <= 4) Res = 16;
440b57cec5SDimitry Andric else if (Size <= 16) Res = 32;
450b57cec5SDimitry Andric else if (Size <= 128) Res = Size + 32;
460b57cec5SDimitry Andric else if (Size <= 512) Res = Size + 64;
470b57cec5SDimitry Andric else if (Size <= 4096) Res = Size + 128;
480b57cec5SDimitry Andric else Res = Size + 256;
490b57cec5SDimitry Andric return alignTo(std::max(Res, 2 * Granularity), Alignment);
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric ASanStackFrameLayout
ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> & Vars,uint64_t Granularity,uint64_t MinHeaderSize)530b57cec5SDimitry Andric ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars,
54*349cc55cSDimitry Andric uint64_t Granularity, uint64_t MinHeaderSize) {
550b57cec5SDimitry Andric assert(Granularity >= 8 && Granularity <= 64 &&
560b57cec5SDimitry Andric (Granularity & (Granularity - 1)) == 0);
570b57cec5SDimitry Andric assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 &&
580b57cec5SDimitry Andric MinHeaderSize >= Granularity);
590b57cec5SDimitry Andric const size_t NumVars = Vars.size();
600b57cec5SDimitry Andric assert(NumVars > 0);
610b57cec5SDimitry Andric for (size_t i = 0; i < NumVars; i++)
620b57cec5SDimitry Andric Vars[i].Alignment = std::max(Vars[i].Alignment, kMinAlignment);
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric llvm::stable_sort(Vars, CompareVars);
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric ASanStackFrameLayout Layout;
670b57cec5SDimitry Andric Layout.Granularity = Granularity;
680b57cec5SDimitry Andric Layout.FrameAlignment = std::max(Granularity, Vars[0].Alignment);
69*349cc55cSDimitry Andric uint64_t Offset =
70*349cc55cSDimitry Andric std::max(std::max(MinHeaderSize, Granularity), Vars[0].Alignment);
710b57cec5SDimitry Andric assert((Offset % Granularity) == 0);
720b57cec5SDimitry Andric for (size_t i = 0; i < NumVars; i++) {
730b57cec5SDimitry Andric bool IsLast = i == NumVars - 1;
74*349cc55cSDimitry Andric uint64_t Alignment = std::max(Granularity, Vars[i].Alignment);
750b57cec5SDimitry Andric (void)Alignment; // Used only in asserts.
76*349cc55cSDimitry Andric uint64_t Size = Vars[i].Size;
770b57cec5SDimitry Andric assert((Alignment & (Alignment - 1)) == 0);
780b57cec5SDimitry Andric assert(Layout.FrameAlignment >= Alignment);
790b57cec5SDimitry Andric assert((Offset % Alignment) == 0);
800b57cec5SDimitry Andric assert(Size > 0);
81*349cc55cSDimitry Andric uint64_t NextAlignment =
82*349cc55cSDimitry Andric IsLast ? Granularity : std::max(Granularity, Vars[i + 1].Alignment);
83*349cc55cSDimitry Andric uint64_t SizeWithRedzone =
84*349cc55cSDimitry Andric VarAndRedzoneSize(Size, Granularity, NextAlignment);
850b57cec5SDimitry Andric Vars[i].Offset = Offset;
860b57cec5SDimitry Andric Offset += SizeWithRedzone;
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric if (Offset % MinHeaderSize) {
890b57cec5SDimitry Andric Offset += MinHeaderSize - (Offset % MinHeaderSize);
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric Layout.FrameSize = Offset;
920b57cec5SDimitry Andric assert((Layout.FrameSize % MinHeaderSize) == 0);
930b57cec5SDimitry Andric return Layout;
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric
ComputeASanStackFrameDescription(const SmallVectorImpl<ASanStackVariableDescription> & Vars)960b57cec5SDimitry Andric SmallString<64> ComputeASanStackFrameDescription(
970b57cec5SDimitry Andric const SmallVectorImpl<ASanStackVariableDescription> &Vars) {
980b57cec5SDimitry Andric SmallString<2048> StackDescriptionStorage;
990b57cec5SDimitry Andric raw_svector_ostream StackDescription(StackDescriptionStorage);
1000b57cec5SDimitry Andric StackDescription << Vars.size();
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric for (const auto &Var : Vars) {
1030b57cec5SDimitry Andric std::string Name = Var.Name;
1040b57cec5SDimitry Andric if (Var.Line) {
1050b57cec5SDimitry Andric Name += ":";
1060b57cec5SDimitry Andric Name += to_string(Var.Line);
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric StackDescription << " " << Var.Offset << " " << Var.Size << " "
1090b57cec5SDimitry Andric << Name.size() << " " << Name;
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric return StackDescription.str();
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric SmallVector<uint8_t, 64>
GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> & Vars,const ASanStackFrameLayout & Layout)1150b57cec5SDimitry Andric GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> &Vars,
1160b57cec5SDimitry Andric const ASanStackFrameLayout &Layout) {
1170b57cec5SDimitry Andric assert(Vars.size() > 0);
1180b57cec5SDimitry Andric SmallVector<uint8_t, 64> SB;
1190b57cec5SDimitry Andric SB.clear();
120*349cc55cSDimitry Andric const uint64_t Granularity = Layout.Granularity;
1210b57cec5SDimitry Andric SB.resize(Vars[0].Offset / Granularity, kAsanStackLeftRedzoneMagic);
1220b57cec5SDimitry Andric for (const auto &Var : Vars) {
1230b57cec5SDimitry Andric SB.resize(Var.Offset / Granularity, kAsanStackMidRedzoneMagic);
1240b57cec5SDimitry Andric
1250b57cec5SDimitry Andric SB.resize(SB.size() + Var.Size / Granularity, 0);
1260b57cec5SDimitry Andric if (Var.Size % Granularity)
1270b57cec5SDimitry Andric SB.push_back(Var.Size % Granularity);
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric SB.resize(Layout.FrameSize / Granularity, kAsanStackRightRedzoneMagic);
1300b57cec5SDimitry Andric return SB;
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric
GetShadowBytesAfterScope(const SmallVectorImpl<ASanStackVariableDescription> & Vars,const ASanStackFrameLayout & Layout)1330b57cec5SDimitry Andric SmallVector<uint8_t, 64> GetShadowBytesAfterScope(
1340b57cec5SDimitry Andric const SmallVectorImpl<ASanStackVariableDescription> &Vars,
1350b57cec5SDimitry Andric const ASanStackFrameLayout &Layout) {
1360b57cec5SDimitry Andric SmallVector<uint8_t, 64> SB = GetShadowBytes(Vars, Layout);
137*349cc55cSDimitry Andric const uint64_t Granularity = Layout.Granularity;
1380b57cec5SDimitry Andric
1390b57cec5SDimitry Andric for (const auto &Var : Vars) {
1400b57cec5SDimitry Andric assert(Var.LifetimeSize <= Var.Size);
141*349cc55cSDimitry Andric const uint64_t LifetimeShadowSize =
1420b57cec5SDimitry Andric (Var.LifetimeSize + Granularity - 1) / Granularity;
143*349cc55cSDimitry Andric const uint64_t Offset = Var.Offset / Granularity;
1440b57cec5SDimitry Andric std::fill(SB.begin() + Offset, SB.begin() + Offset + LifetimeShadowSize,
1450b57cec5SDimitry Andric kAsanStackUseAfterScopeMagic);
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric return SB;
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric } // llvm namespace
152