diff options
author | Karl Schimpf <kschimpf@google.com> | 2013-06-19 08:49:12 -0700 |
---|---|---|
committer | Karl Schimpf <kschimpf@google.com> | 2013-06-19 08:49:12 -0700 |
commit | 8cbf33322a220878bba45b8c7977e69b1ca348d4 (patch) | |
tree | b83a2db80fd363a9e538d2f77d7d71818a3ac159 /lib | |
parent | 139165f35e841945b94685bdd91243f5b14d0a7a (diff) |
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
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp | 63 | ||||
-rw-r--r-- | lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp | 38 |
2 files changed, 24 insertions, 77 deletions
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<CallingConv::ID>(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<PointerType>(Callee->getType()); - FunctionType *FTy = !CalleeTy ? 0 : - dyn_cast<FunctionType>(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<Value*, 16> 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<InvokeInst>(I)->setCallingConv( - static_cast<CallingConv::ID>(CCInfo)); - cast<InvokeInst>(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<CallInst>(I)->setCallingConv( - static_cast<CallingConv::ID>(CCInfo>>1)); + cast<CallInst>(I)->setCallingConv(GetDecodedCallingConv(CCInfo>>1)); cast<CallInst>(I)->setTailCall(CCInfo & 1); cast<CallInst>(I)->setAttributes(PAL); break; diff --git a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp index e286d2852a..c5c469255b 100644 --- a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp +++ b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp @@ -181,6 +181,13 @@ static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) { llvm_unreachable("Invalid synch scope"); } +static unsigned GetEncodedCallingConv(CallingConv::ID conv) { + switch (conv) { + case CallingConv::C: return naclbitc::C_CallingConv; + } + report_fatal_error("Calling convention not supported by PNaCL bitcode"); +} + static void WriteStringRecord(unsigned Code, StringRef Str, unsigned AbbrevToUse, NaClBitstreamWriter &Stream) { @@ -610,7 +617,7 @@ static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE, // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment, // section, visibility, gc, unnamed_addr] Vals.push_back(VE.getTypeID(F->getType())); - Vals.push_back(F->getCallingConv()); + Vals.push_back(GetEncodedCallingConv(F->getCallingConv())); Vals.push_back(F->isDeclaration()); Vals.push_back(getEncodedLinkage(F)); Vals.push_back(VE.getAttributeID(F->getAttributes())); @@ -1374,31 +1381,9 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, Vals.push_back(VE.getValueID(I.getOperand(i))); break; - case Instruction::Invoke: { - const InvokeInst *II = cast<InvokeInst>(&I); - const Value *Callee(II->getCalledValue()); - PointerType *PTy = cast<PointerType>(Callee->getType()); - FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); - Code = naclbitc::FUNC_CODE_INST_INVOKE; - - Vals.push_back(VE.getAttributeID(II->getAttributes())); - Vals.push_back(II->getCallingConv()); - Vals.push_back(VE.getValueID(II->getNormalDest())); - Vals.push_back(VE.getValueID(II->getUnwindDest())); - PushValueAndType(Callee, InstID, Vals, VE); - - // Emit value #'s for the fixed parameters. - for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) - pushValue(I.getOperand(i), InstID, Vals, VE); // fixed param. - - // Emit type/value pairs for varargs params. - if (FTy->isVarArg()) { - for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3; - i != e; ++i) - PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg - } + case Instruction::Invoke: + report_fatal_error("Invoke is not allowed in PNaCl bitcode"); break; - } case Instruction::Resume: Code = naclbitc::FUNC_CODE_INST_RESUME; PushValueAndType(I.getOperand(0), InstID, Vals, VE); @@ -1516,7 +1501,8 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, Code = naclbitc::FUNC_CODE_INST_CALL; Vals.push_back(VE.getAttributeID(CI.getAttributes())); - Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall())); + Vals.push_back((GetEncodedCallingConv(CI.getCallingConv()) << 1) + | unsigned(CI.isTailCall())); PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee // Emit value #'s for the fixed parameters. |