2018-11-27 05:57:47 +08:00
|
|
|
//===- StackSafetyAnalysis.cpp - Stack memory safety analysis -------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-11-27 05:57:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/StackSafetyAnalysis.h"
|
2020-05-27 07:53:02 +08:00
|
|
|
#include "llvm/ADT/APInt.h"
|
2018-11-27 05:57:47 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2020-05-27 07:53:02 +08:00
|
|
|
#include "llvm/IR/ConstantRange.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/GlobalValue.h"
|
2018-11-27 05:57:59 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2020-05-27 07:53:02 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2018-11-27 05:57:59 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2020-05-24 17:44:31 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2019-11-15 07:15:48 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2018-11-27 05:57:47 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2020-05-27 07:53:02 +08:00
|
|
|
#include <algorithm>
|
2020-05-24 13:47:21 +08:00
|
|
|
#include <memory>
|
2018-11-27 05:57:47 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "stack-safety"
|
|
|
|
|
2018-11-27 07:05:58 +08:00
|
|
|
static cl::opt<int> StackSafetyMaxIterations("stack-safety-max-iterations",
|
|
|
|
cl::init(20), cl::Hidden);
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
namespace {
|
|
|
|
|
2020-05-28 10:28:42 +08:00
|
|
|
using GVToSSI = StackSafetyGlobalInfo::GVToSSI;
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
/// Rewrite an SCEV expression for a memory access address to an expression that
|
|
|
|
/// represents offset from the given alloca.
|
|
|
|
class AllocaOffsetRewriter : public SCEVRewriteVisitor<AllocaOffsetRewriter> {
|
|
|
|
const Value *AllocaPtr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AllocaOffsetRewriter(ScalarEvolution &SE, const Value *AllocaPtr)
|
|
|
|
: SCEVRewriteVisitor(SE), AllocaPtr(AllocaPtr) {}
|
|
|
|
|
|
|
|
const SCEV *visitUnknown(const SCEVUnknown *Expr) {
|
|
|
|
// FIXME: look through one or several levels of definitions?
|
|
|
|
// This can be inttoptr(AllocaPtr) and SCEV would not unwrap
|
|
|
|
// it for us.
|
|
|
|
if (Expr->getValue() == AllocaPtr)
|
|
|
|
return SE.getZero(Expr->getType());
|
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Describes use of address in as a function call argument.
|
|
|
|
struct PassAsArgInfo {
|
|
|
|
/// Function being called.
|
|
|
|
const GlobalValue *Callee = nullptr;
|
|
|
|
/// Index of argument which pass address.
|
|
|
|
size_t ParamNo = 0;
|
|
|
|
// Offset range of address from base address (alloca or calling function
|
|
|
|
// argument).
|
|
|
|
// Range should never set to empty-set, that is an invalid access range
|
|
|
|
// that can cause empty-set to be propagated with ConstantRange::add
|
|
|
|
ConstantRange Offset;
|
|
|
|
PassAsArgInfo(const GlobalValue *Callee, size_t ParamNo, ConstantRange Offset)
|
|
|
|
: Callee(Callee), ParamNo(ParamNo), Offset(Offset) {}
|
|
|
|
|
|
|
|
StringRef getName() const { return Callee->getName(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const PassAsArgInfo &P) {
|
|
|
|
return OS << "@" << P.getName() << "(arg" << P.ParamNo << ", " << P.Offset
|
|
|
|
<< ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Describe uses of address (alloca or parameter) inside of the function.
|
|
|
|
struct UseInfo {
|
|
|
|
// Access range if the address (alloca or parameters).
|
|
|
|
// It is allowed to be empty-set when there are no known accesses.
|
|
|
|
ConstantRange Range;
|
|
|
|
|
|
|
|
// List of calls which pass address as an argument.
|
|
|
|
SmallVector<PassAsArgInfo, 4> Calls;
|
|
|
|
|
|
|
|
explicit UseInfo(unsigned PointerSize) : Range{PointerSize, false} {}
|
|
|
|
|
2020-05-28 04:22:16 +08:00
|
|
|
void updateRange(const ConstantRange &R) {
|
|
|
|
assert(!R.isUpperSignWrapped());
|
|
|
|
Range = Range.unionWith(R);
|
|
|
|
assert(!Range.isUpperSignWrapped());
|
|
|
|
}
|
2018-11-27 05:57:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const UseInfo &U) {
|
|
|
|
OS << U.Range;
|
|
|
|
for (auto &Call : U.Calls)
|
|
|
|
OS << ", " << Call;
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2020-05-28 08:41:17 +08:00
|
|
|
// Check if we should bailout for such ranges.
|
|
|
|
bool isUnsafe(const ConstantRange &R) {
|
|
|
|
return R.isEmptySet() || R.isFullSet() || R.isUpperSignWrapped();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculate the allocation size of a given alloca. Returns empty range
|
|
|
|
// in case of confution.
|
|
|
|
ConstantRange getStaticAllocaSizeRange(const AllocaInst &AI) {
|
|
|
|
const DataLayout &DL = AI.getModule()->getDataLayout();
|
|
|
|
TypeSize TS = DL.getTypeAllocSize(AI.getAllocatedType());
|
|
|
|
unsigned PointerSize = DL.getMaxPointerSizeInBits();
|
|
|
|
// Fallback to empty range for alloca size.
|
|
|
|
ConstantRange R = ConstantRange::getEmpty(PointerSize);
|
2020-01-25 08:36:13 +08:00
|
|
|
if (TS.isScalable())
|
2020-05-28 08:41:17 +08:00
|
|
|
return R;
|
|
|
|
APInt APSize(PointerSize, TS.getFixedSize(), true);
|
|
|
|
if (APSize.isNonPositive())
|
|
|
|
return R;
|
|
|
|
if (AI.isArrayAllocation()) {
|
|
|
|
auto C = dyn_cast<ConstantInt>(AI.getArraySize());
|
2018-11-27 05:57:59 +08:00
|
|
|
if (!C)
|
2020-05-28 08:41:17 +08:00
|
|
|
return R;
|
|
|
|
bool Overflow = false;
|
|
|
|
APInt Mul = C->getValue();
|
|
|
|
if (Mul.isNonPositive())
|
|
|
|
return R;
|
|
|
|
Mul = Mul.sextOrTrunc(PointerSize);
|
|
|
|
APSize = APSize.smul_ov(Mul, Overflow);
|
|
|
|
if (Overflow)
|
|
|
|
return R;
|
2018-11-27 05:57:59 +08:00
|
|
|
}
|
2020-05-28 08:41:17 +08:00
|
|
|
R = ConstantRange(APInt::getNullValue(PointerSize), APSize);
|
|
|
|
assert(!isUnsafe(R));
|
|
|
|
return R;
|
2018-11-27 05:57:59 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 13:47:21 +08:00
|
|
|
struct FunctionInfo {
|
2020-05-24 17:44:31 +08:00
|
|
|
SmallVector<UseInfo, 4> Allocas;
|
|
|
|
SmallVector<UseInfo, 4> Params;
|
2018-11-27 05:57:59 +08:00
|
|
|
// TODO: describe return value as depending on one or more of its arguments.
|
|
|
|
|
2018-11-27 07:05:58 +08:00
|
|
|
// StackSafetyDataFlowAnalysis counter stored here for faster access.
|
|
|
|
int UpdateCount = 0;
|
|
|
|
|
2020-05-24 17:44:31 +08:00
|
|
|
void print(raw_ostream &O, StringRef Name, const Function *F) const {
|
2018-11-27 07:05:58 +08:00
|
|
|
// TODO: Consider different printout format after
|
|
|
|
// StackSafetyDataFlowAnalysis. Calls and parameters are irrelevant then.
|
2020-05-28 13:21:39 +08:00
|
|
|
O << " @" << Name << ((F && F->isDSOLocal()) ? "" : " dso_preemptable")
|
|
|
|
<< ((F && F->isInterposable()) ? " interposable" : "") << "\n";
|
2020-05-24 17:44:31 +08:00
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
O << " args uses:\n";
|
2020-05-24 17:44:31 +08:00
|
|
|
size_t Pos = 0;
|
|
|
|
for (auto &P : Params) {
|
|
|
|
StringRef Name = "<N/A>";
|
|
|
|
if (F)
|
|
|
|
Name = F->getArg(Pos)->getName();
|
|
|
|
O << " " << Name << "[]: " << P << "\n";
|
|
|
|
++Pos;
|
|
|
|
}
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
O << " allocas uses:\n";
|
2020-05-24 17:44:31 +08:00
|
|
|
if (F) {
|
|
|
|
size_t Pos = 0;
|
|
|
|
for (auto &I : instructions(F)) {
|
|
|
|
if (auto AI = dyn_cast<AllocaInst>(&I)) {
|
|
|
|
auto &AS = Allocas[Pos];
|
|
|
|
O << " " << AI->getName() << "["
|
2020-05-28 08:41:17 +08:00
|
|
|
<< getStaticAllocaSizeRange(*AI).getUpper() << "]: " << AS << "\n";
|
2020-05-24 17:44:31 +08:00
|
|
|
++Pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(Allocas.empty());
|
|
|
|
}
|
2018-11-27 05:57:59 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-24 13:47:21 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
struct StackSafetyInfo::InfoTy {
|
|
|
|
FunctionInfo Info;
|
|
|
|
};
|
|
|
|
|
|
|
|
StackSafetyInfo makeSSI(FunctionInfo Info) {
|
|
|
|
return StackSafetyInfo(StackSafetyInfo::InfoTy{std::move(Info)});
|
|
|
|
}
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class StackSafetyLocalAnalysis {
|
2020-02-28 07:43:53 +08:00
|
|
|
Function &F;
|
2018-11-27 05:57:59 +08:00
|
|
|
const DataLayout &DL;
|
|
|
|
ScalarEvolution &SE;
|
|
|
|
unsigned PointerSize = 0;
|
|
|
|
|
|
|
|
const ConstantRange UnknownRange;
|
|
|
|
|
2020-05-27 07:12:08 +08:00
|
|
|
ConstantRange offsetFrom(Value *Addr, Value *Base);
|
|
|
|
ConstantRange getAccessRange(Value *Addr, Value *Base,
|
2020-01-25 08:36:13 +08:00
|
|
|
ConstantRange SizeRange);
|
2020-05-27 07:12:08 +08:00
|
|
|
ConstantRange getAccessRange(Value *Addr, Value *Base, TypeSize Size);
|
2018-11-27 05:57:59 +08:00
|
|
|
ConstantRange getMemIntrinsicAccessRange(const MemIntrinsic *MI, const Use &U,
|
2020-05-27 07:12:08 +08:00
|
|
|
Value *Base);
|
2018-11-27 05:57:47 +08:00
|
|
|
|
2020-05-27 07:12:08 +08:00
|
|
|
bool analyzeAllUses(Value *Ptr, UseInfo &AS);
|
2018-11-27 05:57:59 +08:00
|
|
|
|
|
|
|
public:
|
2020-02-28 07:43:53 +08:00
|
|
|
StackSafetyLocalAnalysis(Function &F, ScalarEvolution &SE)
|
2018-11-27 05:57:59 +08:00
|
|
|
: F(F), DL(F.getParent()->getDataLayout()), SE(SE),
|
|
|
|
PointerSize(DL.getPointerSizeInBits()),
|
|
|
|
UnknownRange(PointerSize, true) {}
|
|
|
|
|
|
|
|
// Run the transformation on the associated function.
|
2020-05-24 13:47:21 +08:00
|
|
|
FunctionInfo run();
|
2018-11-27 05:57:59 +08:00
|
|
|
};
|
|
|
|
|
2020-05-27 07:12:08 +08:00
|
|
|
ConstantRange StackSafetyLocalAnalysis::offsetFrom(Value *Addr, Value *Base) {
|
2018-11-27 05:57:59 +08:00
|
|
|
if (!SE.isSCEVable(Addr->getType()))
|
|
|
|
return UnknownRange;
|
|
|
|
|
2020-05-27 07:12:08 +08:00
|
|
|
AllocaOffsetRewriter Rewriter(SE, Base);
|
2018-11-27 05:57:59 +08:00
|
|
|
const SCEV *Expr = Rewriter.visit(SE.getSCEV(Addr));
|
2020-05-27 13:05:41 +08:00
|
|
|
ConstantRange Offset = SE.getSignedRange(Expr);
|
2020-05-28 04:22:16 +08:00
|
|
|
if (isUnsafe(Offset))
|
2020-05-27 08:04:09 +08:00
|
|
|
return UnknownRange;
|
2020-05-27 13:05:41 +08:00
|
|
|
return Offset.sextOrTrunc(PointerSize);
|
2018-11-27 05:57:59 +08:00
|
|
|
}
|
|
|
|
|
2020-01-25 08:36:13 +08:00
|
|
|
ConstantRange
|
2020-05-27 07:12:08 +08:00
|
|
|
StackSafetyLocalAnalysis::getAccessRange(Value *Addr, Value *Base,
|
2020-01-25 08:36:13 +08:00
|
|
|
ConstantRange SizeRange) {
|
2020-01-25 08:36:24 +08:00
|
|
|
// Zero-size loads and stores do not access memory.
|
|
|
|
if (SizeRange.isEmptySet())
|
|
|
|
return ConstantRange::getEmpty(PointerSize);
|
2020-05-28 04:22:16 +08:00
|
|
|
assert(!isUnsafe(SizeRange));
|
|
|
|
|
2020-05-28 04:32:58 +08:00
|
|
|
ConstantRange Offsets = offsetFrom(Addr, Base);
|
|
|
|
if (isUnsafe(Offsets))
|
2020-05-28 04:22:16 +08:00
|
|
|
return UnknownRange;
|
2020-01-25 08:36:24 +08:00
|
|
|
|
2020-05-28 04:32:58 +08:00
|
|
|
if (Offsets.signedAddMayOverflow(SizeRange) !=
|
2020-05-28 04:22:16 +08:00
|
|
|
ConstantRange::OverflowResult::NeverOverflows)
|
|
|
|
return UnknownRange;
|
2020-05-28 04:32:58 +08:00
|
|
|
Offsets = Offsets.add(SizeRange);
|
|
|
|
if (isUnsafe(Offsets))
|
2020-05-28 04:22:16 +08:00
|
|
|
return UnknownRange;
|
2020-05-28 04:32:58 +08:00
|
|
|
return Offsets;
|
2018-11-27 05:57:59 +08:00
|
|
|
}
|
|
|
|
|
2020-05-27 07:12:08 +08:00
|
|
|
ConstantRange StackSafetyLocalAnalysis::getAccessRange(Value *Addr, Value *Base,
|
2020-01-25 08:36:13 +08:00
|
|
|
TypeSize Size) {
|
2020-05-28 04:22:16 +08:00
|
|
|
if (Size.isScalable())
|
|
|
|
return UnknownRange;
|
2020-05-28 08:41:17 +08:00
|
|
|
APInt APSize(PointerSize, Size.getFixedSize(), true);
|
|
|
|
if (APSize.isNegative())
|
|
|
|
return UnknownRange;
|
|
|
|
return getAccessRange(
|
|
|
|
Addr, Base, ConstantRange(APInt::getNullValue(PointerSize), APSize));
|
2020-01-25 08:36:13 +08:00
|
|
|
}
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
ConstantRange StackSafetyLocalAnalysis::getMemIntrinsicAccessRange(
|
2020-05-27 07:12:08 +08:00
|
|
|
const MemIntrinsic *MI, const Use &U, Value *Base) {
|
2018-11-27 05:57:59 +08:00
|
|
|
if (auto MTI = dyn_cast<MemTransferInst>(MI)) {
|
|
|
|
if (MTI->getRawSource() != U && MTI->getRawDest() != U)
|
2020-05-27 16:42:28 +08:00
|
|
|
return ConstantRange::getEmpty(PointerSize);
|
2018-11-27 05:57:59 +08:00
|
|
|
} else {
|
|
|
|
if (MI->getRawDest() != U)
|
2020-05-27 16:42:28 +08:00
|
|
|
return ConstantRange::getEmpty(PointerSize);
|
2018-11-27 05:57:59 +08:00
|
|
|
}
|
2020-05-28 08:41:17 +08:00
|
|
|
|
2020-05-27 14:20:12 +08:00
|
|
|
auto *CalculationTy = IntegerType::getIntNTy(SE.getContext(), PointerSize);
|
|
|
|
if (!SE.isSCEVable(MI->getLength()->getType()))
|
2018-11-27 05:57:59 +08:00
|
|
|
return UnknownRange;
|
2020-05-27 14:20:12 +08:00
|
|
|
|
|
|
|
const SCEV *Expr =
|
|
|
|
SE.getTruncateOrZeroExtend(SE.getSCEV(MI->getLength()), CalculationTy);
|
2020-05-28 04:32:58 +08:00
|
|
|
ConstantRange Sizes = SE.getSignedRange(Expr);
|
2020-05-28 08:41:17 +08:00
|
|
|
if (Sizes.getUpper().isNegative() || isUnsafe(Sizes))
|
2020-05-27 14:20:12 +08:00
|
|
|
return UnknownRange;
|
2020-05-28 04:32:58 +08:00
|
|
|
Sizes = Sizes.sextOrTrunc(PointerSize);
|
2020-05-27 14:20:12 +08:00
|
|
|
ConstantRange SizeRange(APInt::getNullValue(PointerSize),
|
2020-05-28 04:32:58 +08:00
|
|
|
Sizes.getUpper() - 1);
|
2020-05-27 14:20:12 +08:00
|
|
|
return getAccessRange(U, Base, SizeRange);
|
2018-11-27 05:57:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The function analyzes all local uses of Ptr (alloca or argument) and
|
|
|
|
/// calculates local access range and all function calls where it was used.
|
2020-05-27 07:12:08 +08:00
|
|
|
bool StackSafetyLocalAnalysis::analyzeAllUses(Value *Ptr, UseInfo &US) {
|
2018-11-27 05:57:59 +08:00
|
|
|
SmallPtrSet<const Value *, 16> Visited;
|
|
|
|
SmallVector<const Value *, 8> WorkList;
|
|
|
|
WorkList.push_back(Ptr);
|
|
|
|
|
|
|
|
// A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc.
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
const Value *V = WorkList.pop_back_val();
|
|
|
|
for (const Use &UI : V->uses()) {
|
|
|
|
auto I = cast<const Instruction>(UI.getUser());
|
|
|
|
assert(V == UI.get());
|
|
|
|
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case Instruction::Load: {
|
|
|
|
US.updateRange(
|
|
|
|
getAccessRange(UI, Ptr, DL.getTypeStoreSize(I->getType())));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Instruction::VAArg:
|
|
|
|
// "va-arg" from a pointer is safe.
|
|
|
|
break;
|
|
|
|
case Instruction::Store: {
|
|
|
|
if (V == I->getOperand(0)) {
|
|
|
|
// Stored the pointer - conservatively assume it may be unsafe.
|
|
|
|
US.updateRange(UnknownRange);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
US.updateRange(getAccessRange(
|
|
|
|
UI, Ptr, DL.getTypeStoreSize(I->getOperand(0)->getType())));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Instruction::Ret:
|
|
|
|
// Information leak.
|
|
|
|
// FIXME: Process parameters correctly. This is a leak only if we return
|
|
|
|
// alloca.
|
|
|
|
US.updateRange(UnknownRange);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Instruction::Call:
|
|
|
|
case Instruction::Invoke: {
|
2020-04-24 01:35:10 +08:00
|
|
|
const auto &CB = cast<CallBase>(*I);
|
2018-11-27 05:57:59 +08:00
|
|
|
|
2018-12-22 05:49:40 +08:00
|
|
|
if (I->isLifetimeStartOrEnd())
|
|
|
|
break;
|
2018-11-27 05:57:59 +08:00
|
|
|
|
|
|
|
if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
|
|
|
|
US.updateRange(getMemIntrinsicAccessRange(MI, UI, Ptr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: consult devirt?
|
|
|
|
// Do not follow aliases, otherwise we could inadvertently follow
|
|
|
|
// dso_preemptable aliases or aliases with interposable linkage.
|
2019-08-23 03:56:14 +08:00
|
|
|
const GlobalValue *Callee =
|
2020-04-28 11:15:59 +08:00
|
|
|
dyn_cast<GlobalValue>(CB.getCalledOperand()->stripPointerCasts());
|
2018-11-27 05:57:59 +08:00
|
|
|
if (!Callee) {
|
|
|
|
US.updateRange(UnknownRange);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(isa<Function>(Callee) || isa<GlobalAlias>(Callee));
|
|
|
|
|
2020-04-24 01:35:10 +08:00
|
|
|
auto B = CB.arg_begin(), E = CB.arg_end();
|
2020-05-27 17:45:43 +08:00
|
|
|
int Found = 0;
|
2020-04-24 01:35:10 +08:00
|
|
|
for (auto A = B; A != E; ++A) {
|
2018-11-27 05:57:59 +08:00
|
|
|
if (A->get() == V) {
|
2020-05-27 17:45:43 +08:00
|
|
|
++Found;
|
2020-05-27 07:12:08 +08:00
|
|
|
ConstantRange Offset = offsetFrom(UI, Ptr);
|
2018-11-27 05:57:59 +08:00
|
|
|
US.Calls.emplace_back(Callee, A - B, Offset);
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 17:45:43 +08:00
|
|
|
if (!Found) {
|
|
|
|
US.updateRange(UnknownRange);
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-27 05:57:59 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (Visited.insert(I).second)
|
|
|
|
WorkList.push_back(cast<const Instruction>(I));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-24 13:47:21 +08:00
|
|
|
FunctionInfo StackSafetyLocalAnalysis::run() {
|
2020-05-28 13:21:39 +08:00
|
|
|
FunctionInfo Info;
|
2018-11-27 05:57:59 +08:00
|
|
|
assert(!F.isDeclaration() &&
|
|
|
|
"Can't run StackSafety on a function declaration");
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "[StackSafety] " << F.getName() << "\n");
|
|
|
|
|
|
|
|
for (auto &I : instructions(F)) {
|
|
|
|
if (auto AI = dyn_cast<AllocaInst>(&I)) {
|
2020-05-24 17:44:31 +08:00
|
|
|
Info.Allocas.emplace_back(PointerSize);
|
|
|
|
UseInfo &AS = Info.Allocas.back();
|
|
|
|
analyzeAllUses(AI, AS);
|
2018-11-27 05:57:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 07:12:08 +08:00
|
|
|
for (Argument &A : make_range(F.arg_begin(), F.arg_end())) {
|
2020-05-24 17:44:31 +08:00
|
|
|
Info.Params.emplace_back(PointerSize);
|
|
|
|
UseInfo &PS = Info.Params.back();
|
|
|
|
analyzeAllUses(&A, PS);
|
2018-11-27 05:57:59 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:44:31 +08:00
|
|
|
LLVM_DEBUG(Info.print(dbgs(), F.getName(), &F));
|
2018-11-27 05:57:59 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "[StackSafety] done\n");
|
2020-05-24 13:47:21 +08:00
|
|
|
return Info;
|
2018-11-27 05:57:59 +08:00
|
|
|
}
|
|
|
|
|
2018-11-27 07:05:58 +08:00
|
|
|
class StackSafetyDataFlowAnalysis {
|
2020-05-24 13:47:21 +08:00
|
|
|
using FunctionMap = std::map<const GlobalValue *, FunctionInfo>;
|
2018-11-27 07:05:58 +08:00
|
|
|
|
|
|
|
FunctionMap Functions;
|
2020-05-28 13:21:39 +08:00
|
|
|
const ConstantRange UnknownRange;
|
|
|
|
|
2018-11-27 07:05:58 +08:00
|
|
|
// Callee-to-Caller multimap.
|
|
|
|
DenseMap<const GlobalValue *, SmallVector<const GlobalValue *, 4>> Callers;
|
|
|
|
SetVector<const GlobalValue *> WorkList;
|
|
|
|
|
|
|
|
|
|
|
|
bool updateOneUse(UseInfo &US, bool UpdateToFullSet);
|
2020-05-24 13:47:21 +08:00
|
|
|
void updateOneNode(const GlobalValue *Callee, FunctionInfo &FS);
|
2018-11-27 07:05:58 +08:00
|
|
|
void updateOneNode(const GlobalValue *Callee) {
|
|
|
|
updateOneNode(Callee, Functions.find(Callee)->second);
|
|
|
|
}
|
|
|
|
void updateAllNodes() {
|
|
|
|
for (auto &F : Functions)
|
|
|
|
updateOneNode(F.first, F.second);
|
|
|
|
}
|
|
|
|
void runDataFlow();
|
2019-03-02 01:15:21 +08:00
|
|
|
#ifndef NDEBUG
|
2018-11-27 07:05:58 +08:00
|
|
|
void verifyFixedPoint();
|
2019-03-02 01:15:21 +08:00
|
|
|
#endif
|
2018-11-27 07:05:58 +08:00
|
|
|
|
2020-05-28 13:21:39 +08:00
|
|
|
uint32_t findPointerWidth() const {
|
|
|
|
for (auto &F : Functions)
|
|
|
|
for (auto &P : F.second.Params)
|
|
|
|
return P.Range.getBitWidth();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-11-27 07:05:58 +08:00
|
|
|
public:
|
2020-05-28 13:21:39 +08:00
|
|
|
explicit StackSafetyDataFlowAnalysis(FunctionMap Functions)
|
|
|
|
: Functions(std::move(Functions)),
|
|
|
|
UnknownRange(ConstantRange::getFull(findPointerWidth())) {}
|
2018-11-27 07:05:58 +08:00
|
|
|
|
2020-05-28 13:21:39 +08:00
|
|
|
const FunctionMap &run();
|
|
|
|
|
|
|
|
ConstantRange getArgumentAccessRange(const GlobalValue *Callee,
|
2020-05-29 04:01:02 +08:00
|
|
|
unsigned ParamNo,
|
|
|
|
const ConstantRange &Offsets) const;
|
2020-05-28 13:21:39 +08:00
|
|
|
};
|
2018-11-27 07:05:58 +08:00
|
|
|
|
2020-05-29 04:01:02 +08:00
|
|
|
ConstantRange StackSafetyDataFlowAnalysis::getArgumentAccessRange(
|
|
|
|
const GlobalValue *Callee, unsigned ParamNo,
|
|
|
|
const ConstantRange &Offsets) const {
|
2018-11-27 07:05:58 +08:00
|
|
|
auto IT = Functions.find(Callee);
|
|
|
|
// Unknown callee (outside of LTO domain or an indirect call).
|
|
|
|
if (IT == Functions.end())
|
|
|
|
return UnknownRange;
|
2020-05-24 13:47:21 +08:00
|
|
|
const FunctionInfo &FS = IT->second;
|
2018-11-27 07:05:58 +08:00
|
|
|
if (ParamNo >= FS.Params.size()) // possibly vararg
|
|
|
|
return UnknownRange;
|
2020-05-29 04:01:02 +08:00
|
|
|
auto &Access = FS.Params[ParamNo].Range;
|
|
|
|
if (Access.isEmptySet())
|
|
|
|
return Access;
|
|
|
|
if (Access.isFullSet() || Offsets.isFullSet())
|
|
|
|
return UnknownRange;
|
|
|
|
if (Offsets.signedAddMayOverflow(Access) !=
|
|
|
|
ConstantRange::OverflowResult::NeverOverflows)
|
|
|
|
return UnknownRange;
|
|
|
|
return Access.add(Offsets);
|
2018-11-27 07:05:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StackSafetyDataFlowAnalysis::updateOneUse(UseInfo &US,
|
|
|
|
bool UpdateToFullSet) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (auto &CS : US.Calls) {
|
2018-11-27 09:56:35 +08:00
|
|
|
assert(!CS.Offset.isEmptySet() &&
|
|
|
|
"Param range can't be empty-set, invalid offset range");
|
2018-11-27 07:05:58 +08:00
|
|
|
|
2020-05-29 04:01:02 +08:00
|
|
|
ConstantRange CalleeRange =
|
|
|
|
getArgumentAccessRange(CS.Callee, CS.ParamNo, CS.Offset);
|
2018-11-27 07:05:58 +08:00
|
|
|
if (!US.Range.contains(CalleeRange)) {
|
|
|
|
Changed = true;
|
|
|
|
if (UpdateToFullSet)
|
|
|
|
US.Range = UnknownRange;
|
|
|
|
else
|
|
|
|
US.Range = US.Range.unionWith(CalleeRange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2020-05-24 13:47:21 +08:00
|
|
|
void StackSafetyDataFlowAnalysis::updateOneNode(const GlobalValue *Callee,
|
|
|
|
FunctionInfo &FS) {
|
2018-11-27 07:05:58 +08:00
|
|
|
bool UpdateToFullSet = FS.UpdateCount > StackSafetyMaxIterations;
|
|
|
|
bool Changed = false;
|
|
|
|
for (auto &PS : FS.Params)
|
2020-05-24 17:44:31 +08:00
|
|
|
Changed |= updateOneUse(PS, UpdateToFullSet);
|
2018-11-27 07:05:58 +08:00
|
|
|
|
|
|
|
if (Changed) {
|
|
|
|
LLVM_DEBUG(dbgs() << "=== update [" << FS.UpdateCount
|
2020-05-24 17:44:31 +08:00
|
|
|
<< (UpdateToFullSet ? ", full-set" : "") << "] " << &FS
|
|
|
|
<< "\n");
|
2018-11-27 07:05:58 +08:00
|
|
|
// Callers of this function may need updating.
|
|
|
|
for (auto &CallerID : Callers[Callee])
|
|
|
|
WorkList.insert(CallerID);
|
|
|
|
|
|
|
|
++FS.UpdateCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyDataFlowAnalysis::runDataFlow() {
|
|
|
|
Callers.clear();
|
|
|
|
WorkList.clear();
|
|
|
|
|
|
|
|
SmallVector<const GlobalValue *, 16> Callees;
|
|
|
|
for (auto &F : Functions) {
|
|
|
|
Callees.clear();
|
2020-05-24 13:47:21 +08:00
|
|
|
FunctionInfo &FS = F.second;
|
2018-11-27 07:05:58 +08:00
|
|
|
for (auto &PS : FS.Params)
|
2020-05-24 17:44:31 +08:00
|
|
|
for (auto &CS : PS.Calls)
|
2018-11-27 07:05:58 +08:00
|
|
|
Callees.push_back(CS.Callee);
|
|
|
|
|
|
|
|
llvm::sort(Callees);
|
|
|
|
Callees.erase(std::unique(Callees.begin(), Callees.end()), Callees.end());
|
|
|
|
|
|
|
|
for (auto &Callee : Callees)
|
|
|
|
Callers[Callee].push_back(F.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAllNodes();
|
|
|
|
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
const GlobalValue *Callee = WorkList.back();
|
|
|
|
WorkList.pop_back();
|
|
|
|
updateOneNode(Callee);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 01:15:21 +08:00
|
|
|
#ifndef NDEBUG
|
2018-11-27 07:05:58 +08:00
|
|
|
void StackSafetyDataFlowAnalysis::verifyFixedPoint() {
|
|
|
|
WorkList.clear();
|
|
|
|
updateAllNodes();
|
|
|
|
assert(WorkList.empty());
|
|
|
|
}
|
2019-03-02 01:15:21 +08:00
|
|
|
#endif
|
2018-11-27 07:05:58 +08:00
|
|
|
|
2020-05-28 13:21:39 +08:00
|
|
|
const StackSafetyDataFlowAnalysis::FunctionMap &
|
|
|
|
StackSafetyDataFlowAnalysis::run() {
|
2018-11-27 07:05:58 +08:00
|
|
|
runDataFlow();
|
|
|
|
LLVM_DEBUG(verifyFixedPoint());
|
2020-05-28 13:21:39 +08:00
|
|
|
return Functions;
|
2018-11-27 07:05:58 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 10:28:42 +08:00
|
|
|
bool setStackSafetyMetadata(Module &M, const GVToSSI &SSGI) {
|
2020-05-16 04:24:08 +08:00
|
|
|
bool Changed = false;
|
|
|
|
for (auto &F : M.functions()) {
|
|
|
|
if (F.isDeclaration() || F.hasOptNone())
|
|
|
|
continue;
|
|
|
|
auto Iter = SSGI.find(&F);
|
|
|
|
if (Iter == SSGI.end())
|
|
|
|
continue;
|
2020-05-24 13:47:21 +08:00
|
|
|
const FunctionInfo &Summary = Iter->second.getInfo().Info;
|
2020-05-24 17:44:31 +08:00
|
|
|
size_t Pos = 0;
|
|
|
|
for (auto &I : instructions(F)) {
|
|
|
|
if (auto AI = dyn_cast<AllocaInst>(&I)) {
|
|
|
|
auto &AS = Summary.Allocas[Pos];
|
2020-05-28 08:41:17 +08:00
|
|
|
if (getStaticAllocaSizeRange(*AI).contains(AS.Range)) {
|
2020-05-24 17:44:31 +08:00
|
|
|
AI->setMetadata(M.getMDKindID("stack-safe"),
|
|
|
|
MDNode::get(M.getContext(), None));
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
++Pos;
|
2020-05-16 04:24:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:21:39 +08:00
|
|
|
const Function *FindCalleeInModule(const GlobalValue *GV) {
|
|
|
|
while (GV) {
|
|
|
|
if (GV->isInterposable() || !GV->isDSOLocal())
|
|
|
|
return nullptr;
|
|
|
|
if (const Function *F = dyn_cast<Function>(GV))
|
|
|
|
return F;
|
|
|
|
const GlobalAlias *A = dyn_cast<GlobalAlias>(GV);
|
|
|
|
if (!A)
|
|
|
|
return nullptr;
|
|
|
|
GV = A->getBaseObject();
|
|
|
|
if (GV == A)
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResolveAllCalls(UseInfo &Use) {
|
|
|
|
ConstantRange FullSet(Use.Range.getBitWidth(), true);
|
|
|
|
for (auto &C : Use.Calls) {
|
|
|
|
const Function *F = FindCalleeInModule(C.Callee);
|
|
|
|
if (F) {
|
|
|
|
C.Callee = F;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Use.updateRange(FullSet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResolveAllCalls(SmallVectorImpl<UseInfo> &Values) {
|
|
|
|
for (auto &V : Values)
|
|
|
|
ResolveAllCalls(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
GVToSSI createGlobalStackSafetyInfo(
|
|
|
|
std::map<const GlobalValue *, FunctionInfo> Functions) {
|
|
|
|
GVToSSI SSI;
|
|
|
|
if (Functions.empty())
|
|
|
|
return SSI;
|
|
|
|
|
|
|
|
// FIXME: Simplify printing and remove copying here.
|
|
|
|
auto Copy = Functions;
|
|
|
|
|
|
|
|
for (auto &FI : Copy)
|
|
|
|
ResolveAllCalls(FI.second.Params);
|
|
|
|
|
|
|
|
StackSafetyDataFlowAnalysis SSDFA(std::move(Copy));
|
|
|
|
|
|
|
|
for (auto &F : SSDFA.run()) {
|
|
|
|
auto FI = F.second;
|
|
|
|
size_t Pos = 0;
|
|
|
|
for (auto &A : FI.Allocas) {
|
|
|
|
ResolveAllCalls(A);
|
|
|
|
for (auto &C : A.Calls) {
|
2020-05-29 04:01:02 +08:00
|
|
|
A.updateRange(
|
|
|
|
SSDFA.getArgumentAccessRange(C.Callee, C.ParamNo, C.Offset));
|
2020-05-28 13:21:39 +08:00
|
|
|
}
|
|
|
|
// FIXME: This is needed only to preserve calls in print() results.
|
|
|
|
A.Calls = Functions[F.first].Allocas[Pos].Calls;
|
|
|
|
++Pos;
|
|
|
|
}
|
|
|
|
Pos = 0;
|
|
|
|
for (auto &P : FI.Params) {
|
|
|
|
P.Calls = Functions[F.first].Params[Pos].Calls;
|
|
|
|
++Pos;
|
|
|
|
}
|
|
|
|
SSI.emplace(F.first, makeSSI(std::move(FI)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return SSI;
|
|
|
|
}
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
StackSafetyInfo::StackSafetyInfo(StackSafetyInfo &&) = default;
|
|
|
|
StackSafetyInfo &StackSafetyInfo::operator=(StackSafetyInfo &&) = default;
|
|
|
|
|
2020-05-24 13:47:21 +08:00
|
|
|
StackSafetyInfo::StackSafetyInfo(InfoTy Info)
|
|
|
|
: Info(new InfoTy(std::move(Info))) {}
|
2018-11-27 05:57:59 +08:00
|
|
|
|
|
|
|
StackSafetyInfo::~StackSafetyInfo() = default;
|
|
|
|
|
2020-05-24 17:44:31 +08:00
|
|
|
void StackSafetyInfo::print(raw_ostream &O, const GlobalValue &F) const {
|
|
|
|
Info->Info.print(O, F.getName(), dyn_cast<Function>(&F));
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:28:42 +08:00
|
|
|
bool StackSafetyGlobalInfo::setMetadata(Module &M) const {
|
|
|
|
return setStackSafetyMetadata(M, SSGI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyGlobalInfo::print(raw_ostream &O) const {
|
|
|
|
if (SSGI.empty())
|
|
|
|
return;
|
|
|
|
const Module &M = *SSGI.begin()->first->getParent();
|
|
|
|
for (auto &F : M.functions()) {
|
2020-05-24 17:44:31 +08:00
|
|
|
if (!F.isDeclaration()) {
|
2020-05-28 10:28:42 +08:00
|
|
|
SSGI.find(&F)->second.print(O, F);
|
2020-05-24 17:44:31 +08:00
|
|
|
O << "\n";
|
|
|
|
}
|
2020-05-28 10:28:42 +08:00
|
|
|
}
|
2020-05-24 17:44:31 +08:00
|
|
|
}
|
2018-11-27 05:57:59 +08:00
|
|
|
|
2020-05-28 10:28:42 +08:00
|
|
|
LLVM_DUMP_METHOD void StackSafetyGlobalInfo::dump() const { print(dbgs()); }
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
AnalysisKey StackSafetyAnalysis::Key;
|
2018-11-27 05:57:47 +08:00
|
|
|
|
|
|
|
StackSafetyInfo StackSafetyAnalysis::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
2018-11-27 05:57:59 +08:00
|
|
|
StackSafetyLocalAnalysis SSLA(F, AM.getResult<ScalarEvolutionAnalysis>(F));
|
2020-05-24 13:47:21 +08:00
|
|
|
return makeSSI(SSLA.run());
|
2018-11-27 05:57:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses StackSafetyPrinterPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
|
|
|
OS << "'Stack Safety Local Analysis' for function '" << F.getName() << "'\n";
|
2020-05-24 17:44:31 +08:00
|
|
|
AM.getResult<StackSafetyAnalysis>(F).print(OS, F);
|
2018-11-27 05:57:47 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
char StackSafetyInfoWrapperPass::ID = 0;
|
|
|
|
|
|
|
|
StackSafetyInfoWrapperPass::StackSafetyInfoWrapperPass() : FunctionPass(ID) {
|
|
|
|
initializeStackSafetyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<ScalarEvolutionWrapperPass>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyInfoWrapperPass::print(raw_ostream &O, const Module *M) const {
|
2020-05-24 17:44:31 +08:00
|
|
|
SSI->print(O, *F);
|
2018-11-27 05:57:47 +08:00
|
|
|
}
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
bool StackSafetyInfoWrapperPass::runOnFunction(Function &F) {
|
|
|
|
StackSafetyLocalAnalysis SSLA(
|
|
|
|
F, getAnalysis<ScalarEvolutionWrapperPass>().getSE());
|
2020-05-24 13:47:21 +08:00
|
|
|
SSI = makeSSI(SSLA.run());
|
2020-05-24 17:44:31 +08:00
|
|
|
this->F = &F;
|
2018-11-27 05:57:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-27 05:57:47 +08:00
|
|
|
|
2018-11-27 07:05:48 +08:00
|
|
|
AnalysisKey StackSafetyGlobalAnalysis::Key;
|
|
|
|
|
|
|
|
StackSafetyGlobalInfo
|
|
|
|
StackSafetyGlobalAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
|
2018-11-27 07:05:58 +08:00
|
|
|
FunctionAnalysisManager &FAM =
|
|
|
|
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
|
|
|
|
|
2020-05-28 13:21:39 +08:00
|
|
|
// FIXME: Lookup Module Summary.
|
|
|
|
std::map<const GlobalValue *, FunctionInfo> Functions;
|
|
|
|
|
|
|
|
for (auto &F : M.functions()) {
|
|
|
|
if (!F.isDeclaration()) {
|
|
|
|
auto FI = FAM.getResult<StackSafetyAnalysis>(F).getInfo().Info;
|
|
|
|
Functions.emplace(&F, std::move(FI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return createGlobalStackSafetyInfo(std::move(Functions));
|
2018-11-27 07:05:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses StackSafetyGlobalPrinterPass::run(Module &M,
|
|
|
|
ModuleAnalysisManager &AM) {
|
|
|
|
OS << "'Stack Safety Analysis' for module '" << M.getName() << "'\n";
|
2020-05-28 10:28:42 +08:00
|
|
|
AM.getResult<StackSafetyGlobalAnalysis>(M).print(OS);
|
2018-11-27 07:05:48 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:43:53 +08:00
|
|
|
PreservedAnalyses
|
|
|
|
StackSafetyGlobalAnnotatorPass::run(Module &M, ModuleAnalysisManager &AM) {
|
|
|
|
auto &SSGI = AM.getResult<StackSafetyGlobalAnalysis>(M);
|
2020-05-28 10:28:42 +08:00
|
|
|
SSGI.setMetadata(M);
|
2020-02-28 07:43:53 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
2018-11-27 07:05:48 +08:00
|
|
|
char StackSafetyGlobalInfoWrapperPass::ID = 0;
|
|
|
|
|
2020-05-28 11:35:06 +08:00
|
|
|
StackSafetyGlobalInfoWrapperPass::StackSafetyGlobalInfoWrapperPass()
|
|
|
|
: ModulePass(ID) {
|
2018-11-27 07:05:48 +08:00
|
|
|
initializeStackSafetyGlobalInfoWrapperPassPass(
|
|
|
|
*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyGlobalInfoWrapperPass::print(raw_ostream &O,
|
|
|
|
const Module *M) const {
|
2020-05-28 10:28:42 +08:00
|
|
|
SSGI.print(O);
|
2018-11-27 07:05:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyGlobalInfoWrapperPass::getAnalysisUsage(
|
|
|
|
AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<StackSafetyInfoWrapperPass>();
|
|
|
|
}
|
|
|
|
|
2018-11-27 07:05:58 +08:00
|
|
|
bool StackSafetyGlobalInfoWrapperPass::runOnModule(Module &M) {
|
2020-05-28 13:21:39 +08:00
|
|
|
std::map<const GlobalValue *, FunctionInfo> Functions;
|
|
|
|
for (auto &F : M.functions()) {
|
|
|
|
if (!F.isDeclaration()) {
|
|
|
|
auto FI =
|
|
|
|
getAnalysis<StackSafetyInfoWrapperPass>(F).getResult().getInfo().Info;
|
|
|
|
Functions.emplace(&F, std::move(FI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SSGI = createGlobalStackSafetyInfo(std::move(Functions));
|
2020-05-28 11:35:06 +08:00
|
|
|
return SSGI.setMetadata(M);
|
2020-02-28 07:43:53 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 11:35:06 +08:00
|
|
|
ModulePass *llvm::createStackSafetyGlobalInfoWrapperPass() {
|
|
|
|
return new StackSafetyGlobalInfoWrapperPass();
|
2018-11-27 07:05:58 +08:00
|
|
|
}
|
2018-11-27 07:05:48 +08:00
|
|
|
|
2018-11-27 05:57:47 +08:00
|
|
|
static const char LocalPassArg[] = "stack-safety-local";
|
|
|
|
static const char LocalPassName[] = "Stack Safety Local Analysis";
|
|
|
|
INITIALIZE_PASS_BEGIN(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName,
|
|
|
|
false, true)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName,
|
|
|
|
false, true)
|
2018-11-27 07:05:48 +08:00
|
|
|
|
|
|
|
static const char GlobalPassName[] = "Stack Safety Analysis";
|
|
|
|
INITIALIZE_PASS_BEGIN(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE,
|
|
|
|
GlobalPassName, false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE,
|
|
|
|
GlobalPassName, false, false)
|