2013-12-06 17:00:17 +08:00
|
|
|
//===-- ASanStackFrameLayout.cpp - helper for AddressSanitizer ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Definition of ComputeASanStackFrameLayout (see ASanStackFrameLayout.h).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2015-02-16 22:49:37 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2015-11-25 02:57:06 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-12-06 17:00:17 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
// We sort the stack variables by alignment (largest first) to minimize
|
|
|
|
// unnecessary large gaps due to alignment.
|
|
|
|
// It is tempting to also sort variables by size so that larger variables
|
|
|
|
// have larger redzones at both ends. But reordering will make report analysis
|
|
|
|
// harder, especially when temporary unnamed variables are present.
|
|
|
|
// So, until we can provide more information (type, line number, etc)
|
|
|
|
// for the stack variables we avoid reordering them too much.
|
|
|
|
static inline bool CompareVars(const ASanStackVariableDescription &a,
|
|
|
|
const ASanStackVariableDescription &b) {
|
|
|
|
return a.Alignment > b.Alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We also force minimal alignment for all vars to kMinAlignment so that vars
|
|
|
|
// with e.g. alignment 1 and alignment 16 do not get reordered by CompareVars.
|
|
|
|
static const size_t kMinAlignment = 16;
|
|
|
|
|
|
|
|
// The larger the variable Size the larger is the redzone.
|
|
|
|
// The resulting frame size is a multiple of Alignment.
|
|
|
|
static size_t VarAndRedzoneSize(size_t Size, size_t Alignment) {
|
|
|
|
size_t Res = 0;
|
|
|
|
if (Size <= 4) Res = 16;
|
|
|
|
else if (Size <= 16) Res = 32;
|
|
|
|
else if (Size <= 128) Res = Size + 32;
|
|
|
|
else if (Size <= 512) Res = Size + 64;
|
|
|
|
else if (Size <= 4096) Res = Size + 128;
|
|
|
|
else Res = Size + 256;
|
2016-01-15 05:06:47 +08:00
|
|
|
return alignTo(Res, Alignment);
|
2013-12-06 17:00:17 +08:00
|
|
|
}
|
|
|
|
|
2016-08-30 01:41:29 +08:00
|
|
|
ASanStackFrameLayout
|
2013-12-06 17:00:17 +08:00
|
|
|
ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars,
|
2016-08-30 01:41:29 +08:00
|
|
|
size_t Granularity, size_t MinHeaderSize) {
|
2013-12-06 17:00:17 +08:00
|
|
|
assert(Granularity >= 8 && Granularity <= 64 &&
|
|
|
|
(Granularity & (Granularity - 1)) == 0);
|
|
|
|
assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 &&
|
|
|
|
MinHeaderSize >= Granularity);
|
2016-08-30 01:41:29 +08:00
|
|
|
const size_t NumVars = Vars.size();
|
2013-12-06 17:00:17 +08:00
|
|
|
assert(NumVars > 0);
|
|
|
|
for (size_t i = 0; i < NumVars; i++)
|
|
|
|
Vars[i].Alignment = std::max(Vars[i].Alignment, kMinAlignment);
|
|
|
|
|
|
|
|
std::stable_sort(Vars.begin(), Vars.end(), CompareVars);
|
2014-06-27 06:52:05 +08:00
|
|
|
SmallString<2048> StackDescriptionStorage;
|
|
|
|
raw_svector_ostream StackDescription(StackDescriptionStorage);
|
2013-12-06 17:00:17 +08:00
|
|
|
StackDescription << NumVars;
|
2016-08-30 01:41:29 +08:00
|
|
|
|
|
|
|
ASanStackFrameLayout Layout;
|
|
|
|
Layout.Granularity = Granularity;
|
|
|
|
Layout.FrameAlignment = std::max(Granularity, Vars[0].Alignment);
|
2013-12-06 17:00:17 +08:00
|
|
|
size_t Offset = std::max(std::max(MinHeaderSize, Granularity),
|
|
|
|
Vars[0].Alignment);
|
|
|
|
assert((Offset % Granularity) == 0);
|
|
|
|
for (size_t i = 0; i < NumVars; i++) {
|
|
|
|
bool IsLast = i == NumVars - 1;
|
|
|
|
size_t Alignment = std::max(Granularity, Vars[i].Alignment);
|
2013-12-06 17:26:09 +08:00
|
|
|
(void)Alignment; // Used only in asserts.
|
2013-12-06 17:00:17 +08:00
|
|
|
size_t Size = Vars[i].Size;
|
|
|
|
const char *Name = Vars[i].Name;
|
|
|
|
assert((Alignment & (Alignment - 1)) == 0);
|
2016-08-30 01:41:29 +08:00
|
|
|
assert(Layout.FrameAlignment >= Alignment);
|
2013-12-06 17:00:17 +08:00
|
|
|
assert((Offset % Alignment) == 0);
|
|
|
|
assert(Size > 0);
|
2016-08-21 00:48:24 +08:00
|
|
|
assert(Vars[i].LifetimeSize <= Size);
|
2013-12-06 17:00:17 +08:00
|
|
|
StackDescription << " " << Offset << " " << Size << " " << strlen(Name)
|
|
|
|
<< " " << Name;
|
|
|
|
size_t NextAlignment = IsLast ? Granularity
|
|
|
|
: std::max(Granularity, Vars[i + 1].Alignment);
|
|
|
|
size_t SizeWithRedzone = VarAndRedzoneSize(Vars[i].Size, NextAlignment);
|
|
|
|
Vars[i].Offset = Offset;
|
|
|
|
Offset += SizeWithRedzone;
|
|
|
|
}
|
|
|
|
if (Offset % MinHeaderSize) {
|
2016-08-30 01:41:29 +08:00
|
|
|
Offset += MinHeaderSize - (Offset % MinHeaderSize);
|
|
|
|
}
|
|
|
|
Layout.DescriptionString = StackDescription.str();
|
|
|
|
Layout.FrameSize = Offset;
|
|
|
|
assert((Layout.FrameSize % MinHeaderSize) == 0);
|
|
|
|
|
|
|
|
return Layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<uint8_t, 64>
|
|
|
|
GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> &Vars,
|
|
|
|
const ASanStackFrameLayout &Layout) {
|
2016-08-30 03:12:20 +08:00
|
|
|
assert(Vars.size() > 0);
|
2016-08-30 01:41:29 +08:00
|
|
|
SmallVector<uint8_t, 64> SB;
|
|
|
|
SB.clear();
|
|
|
|
const size_t Granularity = Layout.Granularity;
|
|
|
|
SB.resize(Vars[0].Offset / Granularity, kAsanStackLeftRedzoneMagic);
|
|
|
|
for (const auto &Var : Vars) {
|
|
|
|
SB.resize(Var.Offset / Granularity, kAsanStackMidRedzoneMagic);
|
|
|
|
|
|
|
|
SB.resize(SB.size() + Var.Size / Granularity, 0);
|
|
|
|
if (Var.Size % Granularity)
|
|
|
|
SB.push_back(Var.Size % Granularity);
|
2013-12-06 17:00:17 +08:00
|
|
|
}
|
2016-08-30 01:41:29 +08:00
|
|
|
SB.resize(Layout.FrameSize / Granularity, kAsanStackRightRedzoneMagic);
|
|
|
|
return SB;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<uint8_t, 64> GetShadowBytesAfterScope(
|
|
|
|
const SmallVectorImpl<ASanStackVariableDescription> &Vars,
|
|
|
|
const ASanStackFrameLayout &Layout) {
|
|
|
|
SmallVector<uint8_t, 64> SB = GetShadowBytes(Vars, Layout);
|
|
|
|
const size_t Granularity = Layout.Granularity;
|
|
|
|
|
|
|
|
for (const auto &Var : Vars) {
|
|
|
|
const size_t LifetimeShadowSize =
|
|
|
|
(Var.LifetimeSize + Granularity - 1) / Granularity;
|
|
|
|
const size_t Offset = Var.Offset / Granularity;
|
|
|
|
std::fill(SB.begin() + Offset, SB.begin() + Offset + LifetimeShadowSize,
|
|
|
|
kAsanStackUseAfterScopeMagic);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SB;
|
2013-12-06 17:00:17 +08:00
|
|
|
}
|
|
|
|
|
2015-06-23 17:49:53 +08:00
|
|
|
} // llvm namespace
|