From 8cbf33322a220878bba45b8c7977e69b1ca348d4 Mon Sep 17 00:00:00 2001 From: Karl Schimpf Date: Wed, 19 Jun 2013 08:49:12 -0700 Subject: Insulate calling conventions in PNaCl bitcode files. Creates a bitcode-local enumeration for calling conventions for the PNaCl bitcode file, so that the bitcode file is better protected from (accidental) changes in LLVM code. Also removes invokes from the bitcode. BUG= https://code.google.com/p/nativeclient/issues/detail?id=3497 R=dschuff@chromium.org, mseaborn@chromium.org Review URL: https://codereview.chromium.org/17118002 --- lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp | 63 +++++---------------------- 1 file changed, 12 insertions(+), 51 deletions(-) (limited to 'lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp') diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp index 58ff9de3aa..2b2ca32ad7 100644 --- a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp +++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp @@ -194,6 +194,14 @@ static SynchronizationScope GetDecodedSynchScope(unsigned Val) { } } +static CallingConv::ID GetDecodedCallingConv(unsigned Val) { + switch (Val) { + default: + report_fatal_error("PNaCl bitcode contains invalid calling conventions."); + case naclbitc::C_CallingConv: return CallingConv::C; + } +} + namespace llvm { namespace { /// @brief A class for maintaining the slot number definition @@ -1746,7 +1754,7 @@ bool NaClBitcodeReader::ParseModule(bool Resume) { Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, "", TheModule); - Func->setCallingConv(static_cast(Record[1])); + Func->setCallingConv(GetDecodedCallingConv(Record[1])); bool isProto = Record[2]; Func->setLinkage(GetDecodedLinkage(Record[3])); Func->setAttributes(getAttributes(Record[4])); @@ -2447,55 +2455,9 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { break; } - case naclbitc::FUNC_CODE_INST_INVOKE: { - // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] - if (Record.size() < 4) return Error("Invalid INVOKE record"); - AttributeSet PAL = getAttributes(Record[0]); - unsigned CCInfo = Record[1]; - BasicBlock *NormalBB = getBasicBlock(Record[2]); - BasicBlock *UnwindBB = getBasicBlock(Record[3]); - - unsigned OpNum = 4; - Value *Callee; - if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) - return Error("Invalid INVOKE record"); - - PointerType *CalleeTy = dyn_cast(Callee->getType()); - FunctionType *FTy = !CalleeTy ? 0 : - dyn_cast(CalleeTy->getElementType()); - - // Check that the right number of fixed parameters are here. - if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 || - Record.size() < OpNum+FTy->getNumParams()) - return Error("Invalid INVOKE record"); - - SmallVector Ops; - for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { - Ops.push_back(getValue(Record, OpNum, NextValueNo, - FTy->getParamType(i))); - if (Ops.back() == 0) return Error("Invalid INVOKE record"); - } - - if (!FTy->isVarArg()) { - if (Record.size() != OpNum) - return Error("Invalid INVOKE record"); - } else { - // Read type/value pairs for varargs params. - while (OpNum != Record.size()) { - Value *Op; - if (getValueTypePair(Record, OpNum, NextValueNo, Op)) - return Error("Invalid INVOKE record"); - Ops.push_back(Op); - } - } - - I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops); - InstructionList.push_back(I); - cast(I)->setCallingConv( - static_cast(CCInfo)); - cast(I)->setAttributes(PAL); + case naclbitc::FUNC_CODE_INST_INVOKE: + return Error("Invoke is not allowed"); break; - } case naclbitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] unsigned Idx = 0; Value *Val = 0; @@ -2755,8 +2717,7 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { I = CallInst::Create(Callee, Args); InstructionList.push_back(I); - cast(I)->setCallingConv( - static_cast(CCInfo>>1)); + cast(I)->setCallingConv(GetDecodedCallingConv(CCInfo>>1)); cast(I)->setTailCall(CCInfo & 1); cast(I)->setAttributes(PAL); break; -- cgit v1.2.3-18-g5258