mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-26 21:32:10 +00:00
[asan] rewrite asan's stack frame layout
Summary: Rewrite asan's stack frame layout. First, most of the stack layout logic is moved into a separte file to make it more testable and (potentially) useful for other projects. Second, make the frames more compact by using adaptive redzones (smaller for small objects, larger for large objects). Third, try to minimized gaps due to large alignments (this is hypothetical since today we don't see many stack vars aligned by more than 32). The frames indeed become more compact, but I'll still need to run more benchmarks before committing, but I am sking for review now to get early feedback. This change will be accompanied by a trivial change in compiler-rt tests to match the new frame sizes. Reviewers: samsonov, dvyukov Reviewed By: samsonov CC: llvm-commits Differential Revision: http://llvm-reviews.chandlerc.com/D2324 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@196568 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
21c34931e8
commit
daa1bf3b74
64
include/llvm/Transforms/Utils/ASanStackFrameLayout.h
Normal file
64
include/llvm/Transforms/Utils/ASanStackFrameLayout.h
Normal file
@ -0,0 +1,64 @@
|
||||
//===- ASanStackFrameLayout.h - ComputeASanStackFrameLayout -----*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This header defines ComputeASanStackFrameLayout and auxilary data structs.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#ifndef LLVM_TRANSFORMS_UTILS_ASANSTACKFRAMELAYOUT_H
|
||||
#define LLVM_TRANSFORMS_UTILS_ASANSTACKFRAMELAYOUT_H
|
||||
#include "llvm/ADT/SmallString.h"
|
||||
#include "llvm/ADT/SmallVector.h"
|
||||
|
||||
namespace llvm {
|
||||
|
||||
class AllocaInst;
|
||||
|
||||
// These magic constants should be the same as in
|
||||
// in asan_internal.h from ASan runtime in compiler-rt.
|
||||
static const int kAsanStackLeftRedzoneMagic = 0xf1;
|
||||
static const int kAsanStackMidRedzoneMagic = 0xf2;
|
||||
static const int kAsanStackRightRedzoneMagic = 0xf3;
|
||||
|
||||
// Input/output data struct for ComputeASanStackFrameLayout.
|
||||
struct ASanStackVariableDescription {
|
||||
const char *Name; // Name of the variable that will be displayed by asan
|
||||
// if a stack-related bug is reported.
|
||||
size_t Size; // Size of the variable in bytes.
|
||||
size_t Alignment; // Alignment of the variable (power of 2).
|
||||
AllocaInst *AI; // The actual AllocaInst.
|
||||
size_t Offset; // Offset from the beginning of the frame;
|
||||
// set by ComputeASanStackFrameLayout.
|
||||
};
|
||||
|
||||
// Output data struct for ComputeASanStackFrameLayout.
|
||||
struct ASanStackFrameLayout {
|
||||
// Frame description, see DescribeAddressIfStack in ASan runtime.
|
||||
SmallString<64> DescriptionString;
|
||||
// The contents of the shadow memory for the stack frame that we need
|
||||
// to set at function entry.
|
||||
SmallVector<uint8_t, 64> ShadowBytes;
|
||||
size_t FrameAlignment; // Alignment for the entire frame.
|
||||
size_t FrameSize; // Size of the frame in bytes.
|
||||
};
|
||||
|
||||
void ComputeASanStackFrameLayout(
|
||||
// The array of stack variables. The elements may get reordered and changed.
|
||||
SmallVectorImpl<ASanStackVariableDescription> &Vars,
|
||||
// AddressSanitizer's shadow granularity. Usually 8, may also be 16, 32, 64.
|
||||
size_t Granularity,
|
||||
// The minimal size of the left-most redzone (header).
|
||||
// At least 4 pointer sizes, power of 2, and >= Granularity.
|
||||
// The resulting FrameSize should be multiple of MinHeaderSize.
|
||||
size_t MinHeaderSize,
|
||||
// The result is put here.
|
||||
ASanStackFrameLayout *Layout);
|
||||
|
||||
} // llvm namespace
|
||||
|
||||
#endif // LLVM_TRANSFORMS_UTILS_ASANSTACKFRAMELAYOUT_H
|
@ -41,8 +41,8 @@
|
||||
#include "llvm/Support/DataTypes.h"
|
||||
#include "llvm/Support/Debug.h"
|
||||
#include "llvm/Support/Endian.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include "llvm/Support/system_error.h"
|
||||
#include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
|
||||
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
||||
#include "llvm/Transforms/Utils/Cloning.h"
|
||||
#include "llvm/Transforms/Utils/Local.h"
|
||||
@ -93,11 +93,6 @@ static const char *const kAsanUnpoisonStackMemoryName =
|
||||
static const char *const kAsanOptionDetectUAR =
|
||||
"__asan_option_detect_stack_use_after_return";
|
||||
|
||||
// These constants must match the definitions in the run-time library.
|
||||
static const int kAsanStackLeftRedzoneMagic = 0xf1;
|
||||
static const int kAsanStackMidRedzoneMagic = 0xf2;
|
||||
static const int kAsanStackRightRedzoneMagic = 0xf3;
|
||||
static const int kAsanStackPartialRedzoneMagic = 0xf4;
|
||||
#ifndef NDEBUG
|
||||
static const int kAsanStackAfterReturnMagic = 0xf5;
|
||||
#endif
|
||||
@ -141,8 +136,9 @@ static cl::opt<bool> ClInitializers("asan-initialization-order",
|
||||
cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(false));
|
||||
static cl::opt<bool> ClMemIntrin("asan-memintrin",
|
||||
cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
|
||||
static cl::opt<bool> ClRealignStack("asan-realign-stack",
|
||||
cl::desc("Realign stack to 32"), cl::Hidden, cl::init(true));
|
||||
static cl::opt<unsigned> ClRealignStack("asan-realign-stack",
|
||||
cl::desc("Realign stack to the value of this flag (power of two)"),
|
||||
cl::Hidden, cl::init(32));
|
||||
static cl::opt<std::string> ClBlacklistFile("asan-blacklist",
|
||||
cl::desc("File containing the list of objects to ignore "
|
||||
"during instrumentation"), cl::Hidden);
|
||||
@ -376,7 +372,7 @@ class AddressSanitizerModule : public ModulePass {
|
||||
|
||||
bool ShouldInstrumentGlobal(GlobalVariable *G);
|
||||
void createInitializerPoisonCalls(Module &M, GlobalValue *ModuleName);
|
||||
size_t RedzoneSize() const {
|
||||
size_t MinRedzoneSizeForGlobal() const {
|
||||
return RedzoneSizeForScale(Mapping.Scale);
|
||||
}
|
||||
|
||||
@ -416,7 +412,6 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
|
||||
|
||||
SmallVector<AllocaInst*, 16> AllocaVec;
|
||||
SmallVector<Instruction*, 8> RetVec;
|
||||
uint64_t TotalStackSize;
|
||||
unsigned StackAlignment;
|
||||
|
||||
Function *AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
|
||||
@ -440,7 +435,7 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
|
||||
: F(F), ASan(ASan), DIB(*F.getParent()), C(ASan.C),
|
||||
IntptrTy(ASan.IntptrTy), IntptrPtrTy(PointerType::get(IntptrTy, 0)),
|
||||
Mapping(ASan.Mapping),
|
||||
TotalStackSize(0), StackAlignment(1 << Mapping.Scale) {}
|
||||
StackAlignment(1 << Mapping.Scale) {}
|
||||
|
||||
bool runOnFunction() {
|
||||
if (!ClStack) return false;
|
||||
@ -479,8 +474,6 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
|
||||
|
||||
StackAlignment = std::max(StackAlignment, AI.getAlignment());
|
||||
AllocaVec.push_back(&AI);
|
||||
uint64_t AlignedSize = getAlignedAllocaSize(&AI);
|
||||
TotalStackSize += AlignedSize;
|
||||
}
|
||||
|
||||
/// \brief Collect lifetime intrinsic calls to check for use-after-scope
|
||||
@ -514,31 +507,20 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
|
||||
|
||||
// Check if we want (and can) handle this alloca.
|
||||
bool isInterestingAlloca(AllocaInst &AI) const {
|
||||
return (!AI.isArrayAllocation() &&
|
||||
AI.isStaticAlloca() &&
|
||||
AI.getAlignment() <= RedzoneSize() &&
|
||||
AI.getAllocatedType()->isSized());
|
||||
return (!AI.isArrayAllocation() && AI.isStaticAlloca() &&
|
||||
AI.getAllocatedType()->isSized() &&
|
||||
// alloca() may be called with 0 size, ignore it.
|
||||
getAllocaSizeInBytes(&AI) > 0);
|
||||
}
|
||||
|
||||
size_t RedzoneSize() const {
|
||||
return RedzoneSizeForScale(Mapping.Scale);
|
||||
}
|
||||
uint64_t getAllocaSizeInBytes(AllocaInst *AI) const {
|
||||
Type *Ty = AI->getAllocatedType();
|
||||
uint64_t SizeInBytes = ASan.TD->getTypeAllocSize(Ty);
|
||||
return SizeInBytes;
|
||||
}
|
||||
uint64_t getAlignedSize(uint64_t SizeInBytes) const {
|
||||
size_t RZ = RedzoneSize();
|
||||
return ((SizeInBytes + RZ - 1) / RZ) * RZ;
|
||||
}
|
||||
uint64_t getAlignedAllocaSize(AllocaInst *AI) const {
|
||||
uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
|
||||
return getAlignedSize(SizeInBytes);
|
||||
}
|
||||
/// Finds alloca where the value comes from.
|
||||
AllocaInst *findAllocaForValue(Value *V);
|
||||
void poisonRedZones(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> &IRB,
|
||||
void poisonRedZones(const ArrayRef<uint8_t> ShadowBytes, IRBuilder<> &IRB,
|
||||
Value *ShadowBase, bool DoPoison);
|
||||
void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> &IRB, bool DoPoison);
|
||||
|
||||
@ -861,8 +843,8 @@ bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) {
|
||||
// - Need to poison all copies, not just the main thread's one.
|
||||
if (G->isThreadLocal())
|
||||
return false;
|
||||
// For now, just ignore this Alloca if the alignment is large.
|
||||
if (G->getAlignment() > RedzoneSize()) return false;
|
||||
// For now, just ignore this Global if the alignment is large.
|
||||
if (G->getAlignment() > MinRedzoneSizeForGlobal()) return false;
|
||||
|
||||
// Ignore all the globals with the names starting with "\01L_OBJC_".
|
||||
// Many of those are put into the .cstring section. The linker compresses
|
||||
@ -980,7 +962,7 @@ bool AddressSanitizerModule::runOnModule(Module &M) {
|
||||
PointerType *PtrTy = cast<PointerType>(G->getType());
|
||||
Type *Ty = PtrTy->getElementType();
|
||||
uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
|
||||
uint64_t MinRZ = RedzoneSize();
|
||||
uint64_t MinRZ = MinRedzoneSizeForGlobal();
|
||||
// MinRZ <= RZ <= kMaxGlobalRedzone
|
||||
// and trying to make RZ to be ~ 1/4 of SizeInBytes.
|
||||
uint64_t RZ = std::max(MinRZ,
|
||||
@ -1323,32 +1305,6 @@ bool AddressSanitizer::runOnFunction(Function &F) {
|
||||
return res;
|
||||
}
|
||||
|
||||
static uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) {
|
||||
if (ShadowRedzoneSize == 1) return PoisonByte;
|
||||
if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte;
|
||||
if (ShadowRedzoneSize == 4)
|
||||
return (PoisonByte << 24) + (PoisonByte << 16) +
|
||||
(PoisonByte << 8) + (PoisonByte);
|
||||
llvm_unreachable("ShadowRedzoneSize is either 1, 2 or 4");
|
||||
}
|
||||
|
||||
static void PoisonShadowPartialRightRedzone(uint8_t *Shadow,
|
||||
size_t Size,
|
||||
size_t RZSize,
|
||||
size_t ShadowGranularity,
|
||||
uint8_t Magic) {
|
||||
for (size_t i = 0; i < RZSize;
|
||||
i+= ShadowGranularity, Shadow++) {
|
||||
if (i + ShadowGranularity <= Size) {
|
||||
*Shadow = 0; // fully addressable
|
||||
} else if (i >= Size) {
|
||||
*Shadow = Magic; // unaddressable
|
||||
} else {
|
||||
*Shadow = Size - i; // first Size-i bytes are addressable
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Workaround for bug 11395: we don't want to instrument stack in functions
|
||||
// with large assembly blobs (32-bit only), otherwise reg alloc may crash.
|
||||
// FIXME: remove once the bug 11395 is fixed.
|
||||
@ -1378,65 +1334,32 @@ void FunctionStackPoisoner::initializeCallbacks(Module &M) {
|
||||
kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
|
||||
}
|
||||
|
||||
void FunctionStackPoisoner::poisonRedZones(
|
||||
const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> &IRB, Value *ShadowBase,
|
||||
bool DoPoison) {
|
||||
size_t ShadowRZSize = RedzoneSize() >> Mapping.Scale;
|
||||
assert(ShadowRZSize >= 1 && ShadowRZSize <= 4);
|
||||
Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8);
|
||||
Type *RZPtrTy = PointerType::get(RZTy, 0);
|
||||
|
||||
Value *PoisonLeft = ConstantInt::get(RZTy,
|
||||
ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize));
|
||||
Value *PoisonMid = ConstantInt::get(RZTy,
|
||||
ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize));
|
||||
Value *PoisonRight = ConstantInt::get(RZTy,
|
||||
ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize));
|
||||
|
||||
// poison the first red zone.
|
||||
IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy));
|
||||
|
||||
// poison all other red zones.
|
||||
uint64_t Pos = RedzoneSize();
|
||||
for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
|
||||
AllocaInst *AI = AllocaVec[i];
|
||||
uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
|
||||
uint64_t AlignedSize = getAlignedAllocaSize(AI);
|
||||
assert(AlignedSize - SizeInBytes < RedzoneSize());
|
||||
Value *Ptr = NULL;
|
||||
|
||||
Pos += AlignedSize;
|
||||
|
||||
assert(ShadowBase->getType() == IntptrTy);
|
||||
if (SizeInBytes < AlignedSize) {
|
||||
// Poison the partial redzone at right
|
||||
Ptr = IRB.CreateAdd(
|
||||
ShadowBase, ConstantInt::get(IntptrTy,
|
||||
(Pos >> Mapping.Scale) - ShadowRZSize));
|
||||
size_t AddressableBytes = RedzoneSize() - (AlignedSize - SizeInBytes);
|
||||
uint32_t Poison = 0;
|
||||
if (DoPoison) {
|
||||
PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes,
|
||||
RedzoneSize(),
|
||||
1ULL << Mapping.Scale,
|
||||
kAsanStackPartialRedzoneMagic);
|
||||
Poison =
|
||||
ASan.TD->isLittleEndian()
|
||||
? support::endian::byte_swap<uint32_t, support::little>(Poison)
|
||||
: support::endian::byte_swap<uint32_t, support::big>(Poison);
|
||||
}
|
||||
Value *PartialPoison = ConstantInt::get(RZTy, Poison);
|
||||
IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
|
||||
void
|
||||
FunctionStackPoisoner::poisonRedZones(const ArrayRef<uint8_t> ShadowBytes,
|
||||
IRBuilder<> &IRB, Value *ShadowBase,
|
||||
bool DoPoison) {
|
||||
size_t n = ShadowBytes.size();
|
||||
size_t LargeStoreSize = ASan.LongSize / 8;
|
||||
size_t i;
|
||||
for (i = 0; i + LargeStoreSize - 1 < n; i += LargeStoreSize) {
|
||||
uint64_t Val = 0;
|
||||
for (size_t j = 0; j < LargeStoreSize; j++) {
|
||||
if (ASan.TD->isLittleEndian())
|
||||
Val |= (uint64_t)ShadowBytes[i + j] << (8 * j);
|
||||
else
|
||||
Val = (Val << 8) | ShadowBytes[i + j];
|
||||
}
|
||||
|
||||
// Poison the full redzone at right.
|
||||
Ptr = IRB.CreateAdd(ShadowBase,
|
||||
ConstantInt::get(IntptrTy, Pos >> Mapping.Scale));
|
||||
bool LastAlloca = (i == AllocaVec.size() - 1);
|
||||
Value *Poison = LastAlloca ? PoisonRight : PoisonMid;
|
||||
IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
|
||||
|
||||
Pos += RedzoneSize();
|
||||
if (!Val) continue;
|
||||
Value *Ptr = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
|
||||
Value *Poison = ConstantInt::get(IntptrTy, DoPoison ? Val : 0);
|
||||
IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, IntptrPtrTy));
|
||||
}
|
||||
for (; i < n; i++) {
|
||||
uint8_t Val = ShadowBytes[i];
|
||||
if (!Val) continue;
|
||||
Value *Ptr = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
|
||||
Value *Poison = ConstantInt::get(IRB.getInt8Ty(), DoPoison ? Val : 0);
|
||||
IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, IRB.getInt8PtrTy()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1468,24 +1391,37 @@ void FunctionStackPoisoner::SetShadowToStackAfterReturnInlined(
|
||||
}
|
||||
|
||||
void FunctionStackPoisoner::poisonStack() {
|
||||
uint64_t LocalStackSize = TotalStackSize +
|
||||
(AllocaVec.size() + 1) * RedzoneSize();
|
||||
|
||||
bool DoStackMalloc = ASan.CheckUseAfterReturn
|
||||
&& LocalStackSize <= kMaxStackMallocSize;
|
||||
int StackMallocIdx = -1;
|
||||
|
||||
assert(AllocaVec.size() > 0);
|
||||
Instruction *InsBefore = AllocaVec[0];
|
||||
IRBuilder<> IRB(InsBefore);
|
||||
|
||||
SmallVector<ASanStackVariableDescription, 16> SVD;
|
||||
SVD.reserve(AllocaVec.size());
|
||||
for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
|
||||
AllocaInst *AI = AllocaVec[i];
|
||||
ASanStackVariableDescription D = { AI->getName().data(),
|
||||
getAllocaSizeInBytes(AI),
|
||||
AI->getAlignment(), AI, 0};
|
||||
SVD.push_back(D);
|
||||
}
|
||||
// Minimal header size (left redzone) is 4 pointers,
|
||||
// i.e. 32 bytes on 64-bit platforms and 16 bytes in 32-bit platforms.
|
||||
size_t MinHeaderSize = ASan.LongSize / 2;
|
||||
ASanStackFrameLayout L;
|
||||
ComputeASanStackFrameLayout(SVD, 1UL << Mapping.Scale, MinHeaderSize, &L);
|
||||
DEBUG(dbgs() << L.DescriptionString << " --- " << L.FrameSize << "\n");
|
||||
uint64_t LocalStackSize = L.FrameSize;
|
||||
bool DoStackMalloc =
|
||||
ASan.CheckUseAfterReturn && LocalStackSize <= kMaxStackMallocSize;
|
||||
|
||||
Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
|
||||
AllocaInst *MyAlloca =
|
||||
new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
|
||||
if (ClRealignStack && StackAlignment < RedzoneSize())
|
||||
StackAlignment = RedzoneSize();
|
||||
MyAlloca->setAlignment(StackAlignment);
|
||||
assert((ClRealignStack & (ClRealignStack - 1)) == 0);
|
||||
size_t FrameAlignment = std::max(L.FrameAlignment, (size_t)ClRealignStack);
|
||||
MyAlloca->setAlignment(FrameAlignment);
|
||||
assert(MyAlloca->isStaticAlloca());
|
||||
Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
|
||||
Value *LocalStackBase = OrigStackBase;
|
||||
@ -1515,11 +1451,6 @@ void FunctionStackPoisoner::poisonStack() {
|
||||
LocalStackBase = Phi;
|
||||
}
|
||||
|
||||
// This string will be parsed by the run-time (DescribeAddressIfStack).
|
||||
SmallString<2048> StackDescriptionStorage;
|
||||
raw_svector_ostream StackDescription(StackDescriptionStorage);
|
||||
StackDescription << AllocaVec.size() << " ";
|
||||
|
||||
// Insert poison calls for lifetime intrinsics for alloca.
|
||||
bool HavePoisonedAllocas = false;
|
||||
for (size_t i = 0, n = AllocaPoisonCallVec.size(); i < n; i++) {
|
||||
@ -1531,24 +1462,16 @@ void FunctionStackPoisoner::poisonStack() {
|
||||
HavePoisonedAllocas |= APC.DoPoison;
|
||||
}
|
||||
|
||||
uint64_t Pos = RedzoneSize();
|
||||
// Replace Alloca instructions with base+offset.
|
||||
for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
|
||||
AllocaInst *AI = AllocaVec[i];
|
||||
uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
|
||||
StringRef Name = AI->getName();
|
||||
StackDescription << Pos << " " << SizeInBytes << " "
|
||||
<< Name.size() << " " << Name << " ";
|
||||
uint64_t AlignedSize = getAlignedAllocaSize(AI);
|
||||
assert((AlignedSize % RedzoneSize()) == 0);
|
||||
for (size_t i = 0, n = SVD.size(); i < n; i++) {
|
||||
AllocaInst *AI = SVD[i].AI;
|
||||
Value *NewAllocaPtr = IRB.CreateIntToPtr(
|
||||
IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)),
|
||||
AI->getType());
|
||||
IRB.CreateAdd(LocalStackBase,
|
||||
ConstantInt::get(IntptrTy, SVD[i].Offset)),
|
||||
AI->getType());
|
||||
replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB);
|
||||
AI->replaceAllUsesWith(NewAllocaPtr);
|
||||
Pos += AlignedSize + RedzoneSize();
|
||||
}
|
||||
assert(Pos == LocalStackSize);
|
||||
|
||||
// The left-most redzone has enough space for at least 4 pointers.
|
||||
// Write the Magic value to redzone[0].
|
||||
@ -1560,7 +1483,7 @@ void FunctionStackPoisoner::poisonStack() {
|
||||
IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, ASan.LongSize/8)),
|
||||
IntptrPtrTy);
|
||||
GlobalVariable *StackDescriptionGlobal =
|
||||
createPrivateGlobalForString(*F.getParent(), StackDescription.str());
|
||||
createPrivateGlobalForString(*F.getParent(), L.DescriptionString);
|
||||
Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal,
|
||||
IntptrTy);
|
||||
IRB.CreateStore(Description, BasePlus1);
|
||||
@ -1573,7 +1496,7 @@ void FunctionStackPoisoner::poisonStack() {
|
||||
|
||||
// Poison the stack redzones at the entry.
|
||||
Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB);
|
||||
poisonRedZones(AllocaVec, IRB, ShadowBase, true);
|
||||
poisonRedZones(L.ShadowBytes, IRB, ShadowBase, true);
|
||||
|
||||
// Unpoison the stack before all ret instructions.
|
||||
for (size_t i = 0, n = RetVec.size(); i < n; i++) {
|
||||
@ -1583,7 +1506,7 @@ void FunctionStackPoisoner::poisonStack() {
|
||||
IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
|
||||
BasePlus0);
|
||||
// Unpoison the stack.
|
||||
poisonRedZones(AllocaVec, IRBRet, ShadowBase, false);
|
||||
poisonRedZones(L.ShadowBytes, IRBRet, ShadowBase, false);
|
||||
if (DoStackMalloc) {
|
||||
assert(StackMallocIdx >= 0);
|
||||
// In use-after-return mode, mark the whole stack frame unaddressable.
|
||||
|
113
lib/Transforms/Utils/ASanStackFrameLayout.cpp
Normal file
113
lib/Transforms/Utils/ASanStackFrameLayout.cpp
Normal file
@ -0,0 +1,113 @@
|
||||
//===-- 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"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#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;
|
||||
|
||||
static size_t RoundUpTo(size_t X, size_t RoundTo) {
|
||||
assert((RoundTo & (RoundTo - 1)) == 0);
|
||||
return (X + RoundTo - 1) & ~(RoundTo - 1);
|
||||
}
|
||||
|
||||
// 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;
|
||||
return RoundUpTo(Res, Alignment);
|
||||
}
|
||||
|
||||
void
|
||||
ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars,
|
||||
size_t Granularity, size_t MinHeaderSize,
|
||||
ASanStackFrameLayout *Layout) {
|
||||
assert(Granularity >= 8 && Granularity <= 64 &&
|
||||
(Granularity & (Granularity - 1)) == 0);
|
||||
assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 &&
|
||||
MinHeaderSize >= Granularity);
|
||||
size_t NumVars = Vars.size();
|
||||
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);
|
||||
SmallString<2048> StackDescriptionStorage;
|
||||
raw_svector_ostream StackDescription(StackDescriptionStorage);
|
||||
StackDescription << NumVars;
|
||||
Layout->FrameAlignment = std::max(Granularity, Vars[0].Alignment);
|
||||
SmallVector<uint8_t, 64> &SB(Layout->ShadowBytes);
|
||||
SB.clear();
|
||||
size_t Offset = std::max(std::max(MinHeaderSize, Granularity),
|
||||
Vars[0].Alignment);
|
||||
assert((Offset % Granularity) == 0);
|
||||
SB.insert(SB.end(), Offset / Granularity, kAsanStackLeftRedzoneMagic);
|
||||
for (size_t i = 0; i < NumVars; i++) {
|
||||
bool IsLast = i == NumVars - 1;
|
||||
size_t Alignment = std::max(Granularity, Vars[i].Alignment);
|
||||
size_t Size = Vars[i].Size;
|
||||
const char *Name = Vars[i].Name;
|
||||
assert((Alignment & (Alignment - 1)) == 0);
|
||||
assert(Layout->FrameAlignment >= Alignment);
|
||||
assert((Offset % Alignment) == 0);
|
||||
assert(Size > 0);
|
||||
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);
|
||||
SB.insert(SB.end(), Size / Granularity, 0);
|
||||
if (Size % Granularity)
|
||||
SB.insert(SB.end(), Size % Granularity);
|
||||
SB.insert(SB.end(), (SizeWithRedzone - Size) / Granularity,
|
||||
IsLast ? kAsanStackRightRedzoneMagic
|
||||
: kAsanStackMidRedzoneMagic);
|
||||
Vars[i].Offset = Offset;
|
||||
Offset += SizeWithRedzone;
|
||||
}
|
||||
if (Offset % MinHeaderSize) {
|
||||
size_t ExtraRedzone = MinHeaderSize - (Offset % MinHeaderSize);
|
||||
SB.insert(SB.end(), ExtraRedzone / Granularity,
|
||||
kAsanStackRightRedzoneMagic);
|
||||
Offset += ExtraRedzone;
|
||||
}
|
||||
Layout->DescriptionString = StackDescription.str();
|
||||
Layout->FrameSize = Offset;
|
||||
assert((Layout->FrameSize % MinHeaderSize) == 0);
|
||||
assert(Layout->FrameSize / Granularity == Layout->ShadowBytes.size());
|
||||
}
|
||||
|
||||
} // llvm namespace
|
@ -1,4 +1,5 @@
|
||||
add_llvm_library(LLVMTransformUtils
|
||||
ASanStackFrameLayout.cpp
|
||||
BasicBlockUtils.cpp
|
||||
BreakCriticalEdges.cpp
|
||||
BuildLibCalls.cpp
|
||||
|
@ -89,25 +89,6 @@ entry:
|
||||
; CHECK-NOT: = alloca
|
||||
; CHECK: ret void
|
||||
|
||||
; Check that asan does not touch allocas with alignment > 32.
|
||||
define void @alloca_alignment_test() sanitize_address {
|
||||
entry:
|
||||
%x = alloca [10 x i8], align 64
|
||||
%y = alloca [10 x i8], align 128
|
||||
%z = alloca [10 x i8], align 256
|
||||
call void @alloca_test_use([10 x i8]* %x)
|
||||
call void @alloca_test_use([10 x i8]* %y)
|
||||
call void @alloca_test_use([10 x i8]* %z)
|
||||
ret void
|
||||
}
|
||||
|
||||
; CHECK: define void @alloca_alignment_test()
|
||||
; CHECK: = alloca{{.*}} align 64
|
||||
; CHECK: = alloca{{.*}} align 128
|
||||
; CHECK: = alloca{{.*}} align 256
|
||||
; CHECK: ret void
|
||||
|
||||
|
||||
define void @LongDoubleTest(x86_fp80* nocapture %a) nounwind uwtable sanitize_address {
|
||||
entry:
|
||||
store x86_fp80 0xK3FFF8000000000000000, x86_fp80* %a, align 16
|
||||
|
49
test/Instrumentation/AddressSanitizer/stack_layout.ll
Normal file
49
test/Instrumentation/AddressSanitizer/stack_layout.ll
Normal file
@ -0,0 +1,49 @@
|
||||
; Test the ASan's stack layout.
|
||||
; More tests in tests/Transforms/Utils/ASanStackFrameLayoutTest.cpp
|
||||
; RUN: opt < %s -asan -S | FileCheck %s
|
||||
|
||||
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
|
||||
target triple = "x86_64-unknown-linux-gnu"
|
||||
|
||||
declare void @Use(i8*)
|
||||
|
||||
; CHECK: internal unnamed_addr constant{{.*}}3 32 10 3 XXX 64 20 3 YYY 128 30 3 ZZZ
|
||||
; CHECK: internal unnamed_addr constant{{.*}}3 32 5 3 AAA 64 55 3 BBB 160 555 3 CCC
|
||||
; CHECK: internal unnamed_addr constant{{.*}}3 256 128 3 CCC 448 128 3 BBB 608 128 3 AAA
|
||||
|
||||
define void @Func1() sanitize_address {
|
||||
entry:
|
||||
; CHECK-LABEL: Func1
|
||||
; CHECK: alloca [192 x i8]
|
||||
; CHECK-NOT: alloca
|
||||
; CHECK: ret void
|
||||
%XXX = alloca [10 x i8], align 1
|
||||
%YYY = alloca [20 x i8], align 1
|
||||
%ZZZ = alloca [30 x i8], align 1
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @Func2() sanitize_address {
|
||||
entry:
|
||||
; CHECK-LABEL: Func2
|
||||
; CHECK: alloca [864 x i8]
|
||||
; CHECK-NOT: alloca
|
||||
; CHECK: ret void
|
||||
%AAA = alloca [5 x i8], align 1
|
||||
%BBB = alloca [55 x i8], align 1
|
||||
%CCC = alloca [555 x i8], align 1
|
||||
ret void
|
||||
}
|
||||
|
||||
; Check that we reorder vars according to alignment and handle large alignments.
|
||||
define void @Func3() sanitize_address {
|
||||
entry:
|
||||
; CHECK-LABEL: Func3
|
||||
; CHECK: alloca [768 x i8]
|
||||
; CHECK-NOT: alloca
|
||||
; CHECK: ret void
|
||||
%AAA = alloca [128 x i8], align 16
|
||||
%BBB = alloca [128 x i8], align 64
|
||||
%CCC = alloca [128 x i8], align 256
|
||||
ret void
|
||||
}
|
102
unittests/Transforms/Utils/ASanStackFrameLayoutTest.cpp
Normal file
102
unittests/Transforms/Utils/ASanStackFrameLayoutTest.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
//===- ASanStackFrameLayoutTest.cpp - Tests for ComputeASanStackFrameLayout===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
|
||||
#include "llvm/ADT/ArrayRef.h"
|
||||
#include <sstream>
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
static std::string
|
||||
ShadowBytesToString(ArrayRef<uint8_t> ShadowBytes) {
|
||||
std::ostringstream os;
|
||||
for (size_t i = 0, n = ShadowBytes.size(); i < n; i++) {
|
||||
switch (ShadowBytes[i]) {
|
||||
case kAsanStackLeftRedzoneMagic: os << "L"; break;
|
||||
case kAsanStackRightRedzoneMagic: os << "R"; break;
|
||||
case kAsanStackMidRedzoneMagic: os << "M"; break;
|
||||
default: os << (unsigned)ShadowBytes[i];
|
||||
}
|
||||
}
|
||||
return os.str();
|
||||
}
|
||||
|
||||
static void TestLayout(SmallVector<ASanStackVariableDescription, 10> Vars,
|
||||
size_t Granularity, size_t MinHeaderSize,
|
||||
const std::string &ExpectedDescr,
|
||||
const std::string &ExpectedShadow) {
|
||||
ASanStackFrameLayout L;
|
||||
ComputeASanStackFrameLayout(Vars, Granularity, MinHeaderSize, &L);
|
||||
EXPECT_EQ(ExpectedDescr, L.DescriptionString);
|
||||
EXPECT_EQ(ExpectedShadow, ShadowBytesToString(L.ShadowBytes));
|
||||
}
|
||||
|
||||
TEST(ASanStackFrameLayout, Test) {
|
||||
#define VEC1(a) SmallVector<ASanStackVariableDescription, 10>(1, a)
|
||||
#define VEC(a) \
|
||||
SmallVector<ASanStackVariableDescription, 10>(a, a + sizeof(a) / sizeof(a[0]))
|
||||
|
||||
#define VAR(name, size, alignment) \
|
||||
ASanStackVariableDescription name##size##_##alignment = { \
|
||||
#name #size "_" #alignment, \
|
||||
size, \
|
||||
alignment, \
|
||||
0, \
|
||||
0 \
|
||||
}
|
||||
|
||||
VAR(a, 1, 1);
|
||||
VAR(p, 1, 32);
|
||||
VAR(p, 1, 256);
|
||||
VAR(a, 2, 1);
|
||||
VAR(a, 3, 1);
|
||||
VAR(a, 4, 1);
|
||||
VAR(a, 7, 1);
|
||||
VAR(a, 8, 1);
|
||||
VAR(a, 9, 1);
|
||||
VAR(a, 16, 1);
|
||||
VAR(a, 41, 1);
|
||||
VAR(a, 105, 1);
|
||||
|
||||
TestLayout(VEC1(a1_1), 8, 16, "1 16 1 4 a1_1", "LL1R");
|
||||
TestLayout(VEC1(a1_1), 64, 64, "1 64 1 4 a1_1", "L1");
|
||||
TestLayout(VEC1(p1_32), 8, 32, "1 32 1 5 p1_32", "LLLL1RRR");
|
||||
TestLayout(VEC1(p1_32), 8, 64, "1 64 1 5 p1_32", "LLLLLLLL1RRRRRRR");
|
||||
|
||||
TestLayout(VEC1(a1_1), 8, 32, "1 32 1 4 a1_1", "LLLL1RRR");
|
||||
TestLayout(VEC1(a2_1), 8, 32, "1 32 2 4 a2_1", "LLLL2RRR");
|
||||
TestLayout(VEC1(a3_1), 8, 32, "1 32 3 4 a3_1", "LLLL3RRR");
|
||||
TestLayout(VEC1(a4_1), 8, 32, "1 32 4 4 a4_1", "LLLL4RRR");
|
||||
TestLayout(VEC1(a7_1), 8, 32, "1 32 7 4 a7_1", "LLLL7RRR");
|
||||
TestLayout(VEC1(a8_1), 8, 32, "1 32 8 4 a8_1", "LLLL0RRR");
|
||||
TestLayout(VEC1(a9_1), 8, 32, "1 32 9 4 a9_1", "LLLL01RR");
|
||||
TestLayout(VEC1(a16_1), 8, 32, "1 32 16 5 a16_1", "LLLL00RR");
|
||||
TestLayout(VEC1(p1_256), 8, 32, "1 256 1 6 p1_256",
|
||||
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1RRR");
|
||||
TestLayout(VEC1(a41_1), 8, 32, "1 32 41 5 a41_1", "LLLL000001RRRRRR");
|
||||
TestLayout(VEC1(a105_1), 8, 32, "1 32 105 6 a105_1",
|
||||
"LLLL00000000000001RRRRRR");
|
||||
|
||||
{
|
||||
ASanStackVariableDescription t[] = {a1_1, p1_256};
|
||||
TestLayout(VEC(t), 8, 32,
|
||||
"2 256 1 6 p1_256 272 1 4 a1_1",
|
||||
"LLLLLLLL" "LLLLLLLL" "LLLLLLLL" "LLLLLLLL" "1M1R");
|
||||
}
|
||||
|
||||
{
|
||||
ASanStackVariableDescription t[] = {a1_1, a16_1, a41_1};
|
||||
TestLayout(VEC(t), 8, 32,
|
||||
"3 32 1 4 a1_1 48 16 5 a16_1 80 41 5 a41_1",
|
||||
"LLLL" "1M00" "MM00" "0001" "RRRR");
|
||||
}
|
||||
#undef VEC1
|
||||
#undef VEC
|
||||
#undef VAR
|
||||
}
|
@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS
|
||||
)
|
||||
|
||||
add_llvm_unittest(UtilsTests
|
||||
ASanStackFrameLayoutTest.cpp
|
||||
Cloning.cpp
|
||||
IntegerDivision.cpp
|
||||
Local.cpp
|
||||
|
Loading…
Reference in New Issue
Block a user