diff options
author | Mike Stump <mrs@apple.com> | 2009-12-12 01:27:46 +0000 |
---|---|---|
committer | Mike Stump <mrs@apple.com> | 2009-12-12 01:27:46 +0000 |
commit | d954638f02e77ed3ade66d60945f7ee60747e3b5 (patch) | |
tree | 197149147fb245fe77f0c5797c19569a16f0b07d /clang/lib/CodeGen | |
parent | 7cc35b76c3eb20b1eae194317bff8872e5119086 (diff) | |
download | bcm5719-llvm-d954638f02e77ed3ade66d60945f7ee60747e3b5.tar.gz bcm5719-llvm-d954638f02e77ed3ade66d60945f7ee60747e3b5.zip |
Implement runtime checks for undefined behavior. WIP.
This implements a new flag -fcatch-undefined-behavior. The flag turns
on additional runtime checks for:
T a[I];
a[i] abort when i < 0 or i >= I.
Future stuff includes shifts by >= bitwidth amounts.
llvm-svn: 91198
Diffstat (limited to 'clang/lib/CodeGen')
-rw-r--r-- | clang/lib/CodeGen/CGExpr.cpp | 61 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenFunction.cpp | 3 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenFunction.h | 6 |
3 files changed, 69 insertions, 1 deletions
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 70fa004de85..722ec868f59 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -1012,6 +1012,36 @@ LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { } } +static llvm::Constant *getAbortFn(CodeGenFunction &CGF) { + // void abort(); + + const llvm::FunctionType *FTy = + llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false); + + return CGF.CGM.CreateRuntimeFunction(FTy, "abort"); +} + +llvm::BasicBlock*CodeGenFunction::getAbortBB() { + if (AbortBB) + return AbortBB; + + llvm::BasicBlock *Cont = 0; + if (HaveInsertPoint()) { + Cont = createBasicBlock("cont"); + EmitBranch(Cont); + } + AbortBB = createBasicBlock("abort"); + EmitBlock(AbortBB); + llvm::CallInst *AbortCall = Builder.CreateCall(getAbortFn(*this)); + AbortCall->setDoesNotReturn(); + AbortCall->setDoesNotThrow(); + Builder.CreateUnreachable(); + + if (Cont) + EmitBlock(Cont); + return AbortBB; +} + LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { // The index must always be an integer, which is not an aggregate. Emit it. llvm::Value *Idx = EmitScalarExpr(E->getIdx()); @@ -1040,6 +1070,37 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { llvm::IntegerType::get(VMContext, LLVMPointerWidth), IdxSigned, "idxprom"); + if (CatchUndefined) { + if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E->getBase())) { + if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) { + if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) { + if (const ConstantArrayType *CAT + = getContext().getAsConstantArrayType(DRE->getType())) { + llvm::APInt Size = CAT->getSize(); + llvm::BasicBlock *Cont = createBasicBlock("cont"); + if (IdxSigned) { + Builder.CreateCondBr(Builder.CreateICmpSGE(Idx, + llvm::ConstantInt::get(Idx->getType(), 0)), + Cont, getAbortBB()); + EmitBlock(Cont); + Cont = createBasicBlock("cont"); + Builder.CreateCondBr(Builder.CreateICmpSLT(Idx, + llvm::ConstantInt::get(Idx->getType(), Size)), + Cont, getAbortBB()); + EmitBlock(Cont); + } else { + llvm::BasicBlock *Cont = createBasicBlock("cont"); + Builder.CreateCondBr(Builder.CreateICmpULT(Idx, + llvm::ConstantInt::get(Idx->getType(), Size)), + Cont, getAbortBB()); + EmitBlock(Cont); + } + } + } + } + } + } + // We know that the pointer points to a type of the correct size, unless the // size is a VLA or Objective-C interface. llvm::Value *Address = 0; diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 26964529ea6..db604f63b18 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -32,10 +32,11 @@ CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0), CXXThisDecl(0), CXXVTTDecl(0), ConditionalBranchLevel(0), TerminateHandler(0), - UniqueAggrDestructorCount(0) { + UniqueAggrDestructorCount(0), AbortBB(0) { LLVMIntTy = ConvertType(getContext().IntTy); LLVMPointerWidth = Target.getPointerWidth(0); Exceptions = getContext().getLangOptions().Exceptions; + CatchUndefined = getContext().getLangOptions().CatchUndefined; } ASTContext &CodeGenFunction::getContext() const { diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index 1ddfa6d6c02..8820e6ec8b7 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -108,6 +108,7 @@ public: uint32_t LLVMPointerWidth; bool Exceptions; + bool CatchUndefined; public: /// ObjCEHValueStack - Stack of Objective-C exception values, used for /// rethrows. @@ -1266,6 +1267,11 @@ private: ArgType)); } } + + llvm::BasicBlock *AbortBB; + /// getAbortBB - Create a basic block that will call abort. We'll generate + /// a branch around the created basic block as necessary. + llvm::BasicBlock* getAbortBB(); }; |