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);
|
|
|
|
|
2020-05-29 06:36:17 +08:00
|
|
|
static cl::opt<int> StackSafetyPrint("stack-safety-print", cl::init(0),
|
|
|
|
cl::Hidden);
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// 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()) {
|
2020-05-29 05:25:44 +08:00
|
|
|
const 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)) {
|
2020-05-29 05:25:44 +08:00
|
|
|
if (const auto *AI = dyn_cast<AllocaInst>(&I)) {
|
2020-05-24 17:44:31 +08:00
|
|
|
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-28 16:07:31 +08:00
|
|
|
using GVToSSI = std::map<const GlobalValue *, FunctionInfo>;
|
|
|
|
|
2020-05-24 13:47:21 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
struct StackSafetyInfo::InfoTy {
|
|
|
|
FunctionInfo Info;
|
|
|
|
};
|
|
|
|
|
2020-05-28 16:07:31 +08:00
|
|
|
struct StackSafetyGlobalInfo::InfoTy {
|
|
|
|
GVToSSI Info;
|
|
|
|
};
|
2020-05-24 13:47:21 +08:00
|
|
|
|
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) {
|
2020-05-29 05:25:44 +08:00
|
|
|
if (const auto *MTI = dyn_cast<MemTransferInst>(MI)) {
|
2018-11-27 05:57:59 +08:00
|
|
|
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()) {
|
2020-05-29 05:25:44 +08:00
|
|
|
const auto *I = cast<const Instruction>(UI.getUser());
|
2018-11-27 05:57:59 +08:00
|
|
|
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-05-27 17:45:43 +08:00
|
|
|
int Found = 0;
|
2020-05-29 05:25:44 +08:00
|
|
|
for (size_t ArgNo = 0; ArgNo < CB.getNumArgOperands(); ++ArgNo) {
|
|
|
|
if (CB.getArgOperand(ArgNo) == V) {
|
2020-05-27 17:45:43 +08:00
|
|
|
++Found;
|
2020-05-29 05:25:44 +08:00
|
|
|
US.Calls.emplace_back(Callee, ArgNo, offsetFrom(UI, Ptr));
|
2018-11-27 05:57:59 +08:00
|
|
|
}
|
|
|
|
}
|
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)) {
|
2020-05-29 05:25:44 +08:00
|
|
|
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
|
|
|
|
|
|
|
public:
|
2020-05-28 16:07:31 +08:00
|
|
|
StackSafetyDataFlowAnalysis(uint32_t PointerBitWidth, FunctionMap Functions)
|
2020-05-28 13:21:39 +08:00
|
|
|
: Functions(std::move(Functions)),
|
2020-05-28 16:07:31 +08:00
|
|
|
UnknownRange(ConstantRange::getFull(PointerBitWidth)) {}
|
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-28 16:07:31 +08:00
|
|
|
const FunctionInfo &Summary = Iter->second;
|
2020-05-24 17:44:31 +08:00
|
|
|
size_t Pos = 0;
|
|
|
|
for (auto &I : instructions(F)) {
|
2020-05-29 05:25:44 +08:00
|
|
|
if (auto *AI = dyn_cast<AllocaInst>(&I)) {
|
2020-05-24 17:44:31 +08:00
|
|
|
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-29 05:25:44 +08:00
|
|
|
const Function *findCalleeInModule(const GlobalValue *GV) {
|
2020-05-28 13:21:39 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:25:44 +08:00
|
|
|
void resolveAllCalls(UseInfo &Use) {
|
2020-05-28 13:21:39 +08:00
|
|
|
ConstantRange FullSet(Use.Range.getBitWidth(), true);
|
|
|
|
for (auto &C : Use.Calls) {
|
2020-05-29 05:25:44 +08:00
|
|
|
const Function *F = findCalleeInModule(C.Callee);
|
2020-05-28 13:21:39 +08:00
|
|
|
if (F) {
|
|
|
|
C.Callee = F;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Use.updateRange(FullSet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:25:44 +08:00
|
|
|
void resolveAllCalls(SmallVectorImpl<UseInfo> &Values) {
|
2020-05-28 13:21:39 +08:00
|
|
|
for (auto &V : Values)
|
2020-05-29 05:25:44 +08:00
|
|
|
resolveAllCalls(V);
|
2020-05-28 13:21:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-05-29 05:25:44 +08:00
|
|
|
resolveAllCalls(FI.second.Params);
|
2020-05-28 13:21:39 +08:00
|
|
|
|
2020-05-28 16:07:31 +08:00
|
|
|
uint32_t PointerSize = Copy.begin()
|
|
|
|
->first->getParent()
|
|
|
|
->getDataLayout()
|
|
|
|
.getMaxPointerSizeInBits();
|
|
|
|
StackSafetyDataFlowAnalysis SSDFA(PointerSize, std::move(Copy));
|
2020-05-28 13:21:39 +08:00
|
|
|
|
|
|
|
for (auto &F : SSDFA.run()) {
|
|
|
|
auto FI = F.second;
|
|
|
|
size_t Pos = 0;
|
2020-05-28 16:07:31 +08:00
|
|
|
auto &SrcF = Functions[F.first];
|
2020-05-28 13:21:39 +08:00
|
|
|
for (auto &A : FI.Allocas) {
|
2020-05-29 05:25:44 +08:00
|
|
|
resolveAllCalls(A);
|
2020-05-28 13:21:39 +08:00
|
|
|
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.
|
2020-05-28 16:07:31 +08:00
|
|
|
A.Calls = SrcF.Allocas[Pos].Calls;
|
2020-05-28 13:21:39 +08:00
|
|
|
++Pos;
|
|
|
|
}
|
|
|
|
Pos = 0;
|
|
|
|
for (auto &P : FI.Params) {
|
2020-05-28 16:07:31 +08:00
|
|
|
P.Calls = SrcF.Params[Pos].Calls;
|
2020-05-28 13:21:39 +08:00
|
|
|
++Pos;
|
|
|
|
}
|
2020-05-28 16:07:31 +08:00
|
|
|
SSI[F.first] = std::move(FI);
|
2020-05-28 13:21:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return SSI;
|
|
|
|
}
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2020-05-28 16:07:31 +08:00
|
|
|
StackSafetyInfo::StackSafetyInfo() = default;
|
|
|
|
|
|
|
|
StackSafetyInfo::StackSafetyInfo(Function *F,
|
|
|
|
std::function<ScalarEvolution &()> GetSE)
|
|
|
|
: F(F), GetSE(GetSE) {}
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
StackSafetyInfo::StackSafetyInfo(StackSafetyInfo &&) = default;
|
|
|
|
|
2020-05-28 16:07:31 +08:00
|
|
|
StackSafetyInfo &StackSafetyInfo::operator=(StackSafetyInfo &&) = default;
|
2018-11-27 05:57:59 +08:00
|
|
|
|
|
|
|
StackSafetyInfo::~StackSafetyInfo() = default;
|
|
|
|
|
2020-05-28 16:07:31 +08:00
|
|
|
const StackSafetyInfo::InfoTy &StackSafetyInfo::getInfo() const {
|
|
|
|
if (!Info) {
|
|
|
|
StackSafetyLocalAnalysis SSLA(*F, GetSE());
|
|
|
|
Info.reset(new InfoTy{SSLA.run()});
|
|
|
|
}
|
|
|
|
return *Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyInfo::print(raw_ostream &O) const {
|
|
|
|
getInfo().Info.print(O, F->getName(), dyn_cast<Function>(F));
|
|
|
|
}
|
|
|
|
|
|
|
|
const StackSafetyGlobalInfo::InfoTy &StackSafetyGlobalInfo::getInfo() const {
|
|
|
|
if (!Info) {
|
|
|
|
std::map<const GlobalValue *, FunctionInfo> Functions;
|
|
|
|
for (auto &F : M->functions()) {
|
|
|
|
if (!F.isDeclaration()) {
|
|
|
|
auto FI = GetSSI(F).getInfo().Info;
|
|
|
|
Functions.emplace(&F, std::move(FI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Info.reset(new InfoTy{createGlobalStackSafetyInfo(std::move(Functions))});
|
2020-05-29 06:36:17 +08:00
|
|
|
if (StackSafetyPrint)
|
|
|
|
print(errs());
|
2020-05-28 16:07:31 +08:00
|
|
|
}
|
|
|
|
return *Info;
|
2020-05-24 17:44:31 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 16:07:31 +08:00
|
|
|
StackSafetyGlobalInfo::StackSafetyGlobalInfo() = default;
|
|
|
|
|
|
|
|
StackSafetyGlobalInfo::StackSafetyGlobalInfo(
|
|
|
|
Module *M, std::function<const StackSafetyInfo &(Function &F)> GetSSI)
|
2020-05-29 06:36:17 +08:00
|
|
|
: M(M), GetSSI(GetSSI) {
|
|
|
|
if (StackSafetyPrint > 1)
|
|
|
|
getInfo();
|
|
|
|
}
|
2020-05-28 16:07:31 +08:00
|
|
|
|
|
|
|
StackSafetyGlobalInfo::StackSafetyGlobalInfo(StackSafetyGlobalInfo &&) =
|
|
|
|
default;
|
|
|
|
|
|
|
|
StackSafetyGlobalInfo &
|
|
|
|
StackSafetyGlobalInfo::operator=(StackSafetyGlobalInfo &&) = default;
|
|
|
|
|
|
|
|
StackSafetyGlobalInfo::~StackSafetyGlobalInfo() = default;
|
|
|
|
|
2020-05-28 10:28:42 +08:00
|
|
|
bool StackSafetyGlobalInfo::setMetadata(Module &M) const {
|
2020-05-28 16:07:31 +08:00
|
|
|
return setStackSafetyMetadata(M, getInfo().Info);
|
2020-05-28 10:28:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyGlobalInfo::print(raw_ostream &O) const {
|
2020-05-28 16:07:31 +08:00
|
|
|
auto &SSI = getInfo().Info;
|
|
|
|
if (SSI.empty())
|
2020-05-28 10:28:42 +08:00
|
|
|
return;
|
2020-05-28 16:07:31 +08:00
|
|
|
const Module &M = *SSI.begin()->first->getParent();
|
2020-05-28 10:28:42 +08:00
|
|
|
for (auto &F : M.functions()) {
|
2020-05-24 17:44:31 +08:00
|
|
|
if (!F.isDeclaration()) {
|
2020-05-28 16:07:31 +08:00
|
|
|
SSI.find(&F)->second.print(O, F.getName(), &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) {
|
2020-05-28 16:07:31 +08:00
|
|
|
return StackSafetyInfo(&F, [&AM, &F]() -> ScalarEvolution & {
|
|
|
|
return AM.getResult<ScalarEvolutionAnalysis>(F);
|
|
|
|
});
|
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-28 16:07:31 +08:00
|
|
|
AM.getResult<StackSafetyAnalysis>(F).print(OS);
|
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 {
|
2020-05-28 16:07:31 +08:00
|
|
|
AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
|
2018-11-27 05:57:47 +08:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyInfoWrapperPass::print(raw_ostream &O, const Module *M) const {
|
2020-05-28 16:07:31 +08:00
|
|
|
SSI.print(O);
|
2018-11-27 05:57:47 +08:00
|
|
|
}
|
|
|
|
|
2018-11-27 05:57:59 +08:00
|
|
|
bool StackSafetyInfoWrapperPass::runOnFunction(Function &F) {
|
2020-05-28 16:07:31 +08:00
|
|
|
auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
|
|
|
|
SSI = {&F, [SE]() -> ScalarEvolution & { return *SE; }};
|
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 16:07:31 +08:00
|
|
|
return {&M, [&FAM](Function &F) -> const StackSafetyInfo & {
|
|
|
|
return FAM.getResult<StackSafetyAnalysis>(F);
|
|
|
|
}};
|
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());
|
|
|
|
}
|
|
|
|
|
2020-05-28 16:07:31 +08:00
|
|
|
StackSafetyGlobalInfoWrapperPass::~StackSafetyGlobalInfoWrapperPass() = default;
|
|
|
|
|
2018-11-27 07:05:48 +08:00
|
|
|
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 16:07:31 +08:00
|
|
|
SSGI = {&M, [this](Function &F) -> const StackSafetyInfo & {
|
|
|
|
return getAnalysis<StackSafetyInfoWrapperPass>(F).getResult();
|
|
|
|
}};
|
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)
|