From: Mike Stump Date: Sat, 12 Dec 2009 01:27:46 +0000 (+0000) Subject: Implement runtime checks for undefined behavior. WIP. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9c276ae0f24d4cee8f7954069d4b8eae45d0447d;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@91198 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/LangOptions.h b/include/clang/Basic/LangOptions.h index 27a4cae2b7..e17279e266 100644 --- a/include/clang/Basic/LangOptions.h +++ b/include/clang/Basic/LangOptions.h @@ -92,6 +92,7 @@ public: unsigned ElideConstructors : 1; // Whether C++ copy constructors should be // elided if possible. + unsigned CatchUndefined :1; // Generate code to check for undefined ops. private: unsigned GC : 2; // Objective-C Garbage Collection modes. We // declare this enum as unsigned because MSVC @@ -160,6 +161,7 @@ public: CharIsSigned = 1; ShortWChar = 0; + CatchUndefined = 0; } GCMode getGCMode() const { return (GCMode) GC; } diff --git a/include/clang/Driver/CC1Options.td b/include/clang/Driver/CC1Options.td index a024a7c040..1a30a0fdb7 100644 --- a/include/clang/Driver/CC1Options.td +++ b/include/clang/Driver/CC1Options.td @@ -105,6 +105,8 @@ def disable_llvm_optzns : Flag<"-disable-llvm-optzns">, def disable_red_zone : Flag<"-disable-red-zone">, HelpText<"Do not emit code that uses the red zone.">; def g : Flag<"-g">, HelpText<"Generate source level debug information">; +def fcatch_undefined_behavior : Flag<"-fcatch-undefined-behavior">, + HelpText<"Generate runtime checks for undefined behavior.">; def fno_common : Flag<"-fno-common">, HelpText<"Compile common globals like normal definitions">; def no_implicit_float : Flag<"-no-implicit-float">, diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index db40ecb3d3..4f161ebc83 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -236,6 +236,8 @@ def fbootclasspath_EQ : Joined<"-fbootclasspath=">, Group; def fbuiltin_strcat : Flag<"-fbuiltin-strcat">, Group; def fbuiltin_strcpy : Flag<"-fbuiltin-strcpy">, Group; def fbuiltin : Flag<"-fbuiltin">, Group; +def fcatch_undefined_behavior : Flag<"-fcatch-undefined-behavior">, + Group, HelpText<"Generate runtime checks for undefined behavior.">; def fclasspath_EQ : Joined<"-fclasspath=">, Group; def fcolor_diagnostics : Flag<"-fcolor-diagnostics">, Group; def fcommon : Flag<"-fcommon">, Group; diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 70fa004de8..722ec868f5 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/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(E->getBase())) { + if (const DeclRefExpr *DRE = dyn_cast(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/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp index 26964529ea..db604f63b1 100644 --- a/lib/CodeGen/CodeGenFunction.cpp +++ b/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/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 1ddfa6d6c0..8820e6ec8b 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/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(); }; diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 1d82d82c04..4279db59c9 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -895,6 +895,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } // Forward -f (flag) options which we can pass directly. + Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior); Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); Args.AddLastArg(CmdArgs, options::OPT_ffreestanding); Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 1f64e1e7fa..3b9d5d07a0 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -447,6 +447,8 @@ static void LangOptsToArgs(const LangOptions &Opts, Res.push_back("-fno-operator-names"); if (Opts.PascalStrings) Res.push_back("-fpascal-strings"); + if (Opts.CatchUndefined) + Res.push_back("-fcatch-undefined-behavior"); if (Opts.WritableStrings) Res.push_back("-fwritable-strings"); if (!Opts.LaxVectorConversions) @@ -1151,6 +1153,7 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, Opts.ObjCConstantStringClass = getLastArgValue(Args, OPT_fconstant_string_class); Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi); + Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior); Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls); Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags); Opts.Static = Args.hasArg(OPT_static_define); diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp index aae201400d..58209c8701 100644 --- a/lib/Frontend/PCHReader.cpp +++ b/lib/Frontend/PCHReader.cpp @@ -116,6 +116,7 @@ PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) { diag::warn_pch_stack_protector); PARSE_LANGOPT_BENIGN(InstantiationDepth); PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl); + PARSE_LANGOPT_BENIGN(CatchUndefined); PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors); #undef PARSE_LANGOPT_IRRELEVANT #undef PARSE_LANGOPT_BENIGN @@ -1719,6 +1720,8 @@ bool PCHReader::ParseLanguageOptions( ++Idx; PARSE_LANGOPT(InstantiationDepth); PARSE_LANGOPT(OpenCL); + PARSE_LANGOPT(CatchUndefined); + // FIXME: Missing ElideConstructors?! #undef PARSE_LANGOPT return Listener->ReadLanguageOptions(LangOpts); diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp index a190013177..681c1ff32c 100644 --- a/lib/Frontend/PCHWriter.cpp +++ b/lib/Frontend/PCHWriter.cpp @@ -781,6 +781,7 @@ void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) { Record.push_back(LangOpts.getStackProtectorMode()); Record.push_back(LangOpts.InstantiationDepth); Record.push_back(LangOpts.OpenCL); + Record.push_back(LangOpts.CatchUndefined); Record.push_back(LangOpts.ElideConstructors); Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record); }