|
@@ -33,7 +33,6 @@
|
|
#include "llvm/Pass.h"
|
|
#include "llvm/Pass.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
#include "llvm/Target/TargetLowering.h"
|
|
#include "llvm/Target/TargetLowering.h"
|
|
-#include <cstdlib>
|
|
|
|
using namespace llvm;
|
|
using namespace llvm;
|
|
|
|
|
|
STATISTIC(NumFunProtected, "Number of functions protected");
|
|
STATISTIC(NumFunProtected, "Number of functions protected");
|
|
@@ -54,10 +53,6 @@ namespace {
|
|
|
|
|
|
DominatorTree *DT;
|
|
DominatorTree *DT;
|
|
|
|
|
|
- /// \brief The minimum size of buffers that will receive stack smashing
|
|
|
|
- /// protection when -fstack-protection is used.
|
|
|
|
- unsigned SSPBufferSize;
|
|
|
|
-
|
|
|
|
/// VisitedPHIs - The set of PHI nodes visited when determining
|
|
/// VisitedPHIs - The set of PHI nodes visited when determining
|
|
/// if a variable's reference has been taken. This set
|
|
/// if a variable's reference has been taken. This set
|
|
/// is maintained to ensure we don't visit the same PHI node multiple
|
|
/// is maintained to ensure we don't visit the same PHI node multiple
|
|
@@ -90,12 +85,11 @@ namespace {
|
|
bool RequiresStackProtector();
|
|
bool RequiresStackProtector();
|
|
public:
|
|
public:
|
|
static char ID; // Pass identification, replacement for typeid.
|
|
static char ID; // Pass identification, replacement for typeid.
|
|
- StackProtector() : FunctionPass(ID), TM(0), TLI(0), SSPBufferSize(0) {
|
|
|
|
|
|
+ StackProtector() : FunctionPass(ID), TM(0), TLI(0) {
|
|
initializeStackProtectorPass(*PassRegistry::getPassRegistry());
|
|
initializeStackProtectorPass(*PassRegistry::getPassRegistry());
|
|
}
|
|
}
|
|
StackProtector(const TargetMachine *TM)
|
|
StackProtector(const TargetMachine *TM)
|
|
- : FunctionPass(ID), TM(TM), TLI(0), Trip(TM->getTargetTriple()),
|
|
|
|
- SSPBufferSize(8) {
|
|
|
|
|
|
+ : FunctionPass(ID), TM(TM), TLI(0), Trip(TM->getTargetTriple()) {
|
|
initializeStackProtectorPass(*PassRegistry::getPassRegistry());
|
|
initializeStackProtectorPass(*PassRegistry::getPassRegistry());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -123,12 +117,6 @@ bool StackProtector::runOnFunction(Function &Fn) {
|
|
|
|
|
|
if (!RequiresStackProtector()) return false;
|
|
if (!RequiresStackProtector()) return false;
|
|
|
|
|
|
- Attribute Attr =
|
|
|
|
- Fn.getAttributes().getAttribute(AttributeSet::FunctionIndex,
|
|
|
|
- "ssp-buffer-size");
|
|
|
|
- if (Attr.isStringAttribute())
|
|
|
|
- SSPBufferSize = atoi(Attr.getValueAsString().data());
|
|
|
|
-
|
|
|
|
++NumFunProtected;
|
|
++NumFunProtected;
|
|
return InsertStackProtectors();
|
|
return InsertStackProtectors();
|
|
}
|
|
}
|
|
@@ -144,6 +132,7 @@ bool StackProtector::ContainsProtectableArray(Type *Ty, bool Strong,
|
|
// protector
|
|
// protector
|
|
if (Strong)
|
|
if (Strong)
|
|
return true;
|
|
return true;
|
|
|
|
+ const TargetMachine &TM = TLI->getTargetMachine();
|
|
if (!AT->getElementType()->isIntegerTy(8)) {
|
|
if (!AT->getElementType()->isIntegerTy(8)) {
|
|
// If we're on a non-Darwin platform or we're inside of a structure, don't
|
|
// If we're on a non-Darwin platform or we're inside of a structure, don't
|
|
// add stack protectors unless the array is a character array.
|
|
// add stack protectors unless the array is a character array.
|
|
@@ -153,7 +142,7 @@ bool StackProtector::ContainsProtectableArray(Type *Ty, bool Strong,
|
|
|
|
|
|
// If an array has more than SSPBufferSize bytes of allocated space, then we
|
|
// If an array has more than SSPBufferSize bytes of allocated space, then we
|
|
// emit stack protectors.
|
|
// emit stack protectors.
|
|
- if (SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT))
|
|
|
|
|
|
+ if (TM.Options.SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT))
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -241,14 +230,13 @@ bool StackProtector::RequiresStackProtector() {
|
|
|
|
|
|
if (const ConstantInt *CI =
|
|
if (const ConstantInt *CI =
|
|
dyn_cast<ConstantInt>(AI->getArraySize())) {
|
|
dyn_cast<ConstantInt>(AI->getArraySize())) {
|
|
- if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize)
|
|
|
|
|
|
+ unsigned BufferSize = TLI->getTargetMachine().Options.SSPBufferSize;
|
|
|
|
+ if (CI->getLimitedValue(BufferSize) >= BufferSize)
|
|
// A call to alloca with size >= SSPBufferSize requires
|
|
// A call to alloca with size >= SSPBufferSize requires
|
|
// stack protectors.
|
|
// stack protectors.
|
|
return true;
|
|
return true;
|
|
- } else {
|
|
|
|
- // A call to alloca with a variable size requires protectors.
|
|
|
|
|
|
+ } else // A call to alloca with a variable size requires protectors.
|
|
return true;
|
|
return true;
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
if (ContainsProtectableArray(AI->getAllocatedType(), Strong))
|
|
if (ContainsProtectableArray(AI->getAllocatedType(), Strong))
|