For PR797:

Make the Bytecode Reader use setjmp/longjump instead of exceptions to handle
errors. The alternative was even uglier than setjmp/longjump as it would
impact the interface and workings of nearly every function in the reader.

llvm-svn: 29819
This commit is contained in:
Reid Spencer 2006-08-22 16:09:19 +00:00
parent 88b3207059
commit d00c37651a
2 changed files with 118 additions and 117 deletions

View File

@ -51,13 +51,10 @@ namespace {
} }
// Provide some details on error // Provide some details on error
inline void BytecodeReader::error(std::string err) { inline void BytecodeReader::error(const std::string& err) {
err += " (Vers=" ; ErrorMsg = err + " (Vers=" + itostr(RevisionNum) + ", Pos="
err += itostr(RevisionNum) ; + itostr(At-MemStart) + ")";
err += ", Pos=" ; longjmp(context,1);
err += itostr(At-MemStart);
err += ")";
throw err;
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -470,7 +467,8 @@ Value * BytecodeReader::getValue(unsigned type, unsigned oNum, bool Create) {
ForwardReferences.insert(I, std::make_pair(KeyValue, Val)); ForwardReferences.insert(I, std::make_pair(KeyValue, Val));
return Val; return Val;
} }
throw "Can't create placeholder for value of type slot #" + utostr(type); error("Can't create placeholder for value of type slot #" + utostr(type));
return 0; // just silence warning, error calls longjmp
} }
/// This is just like getValue, but when a compaction table is in use, it /// This is just like getValue, but when a compaction table is in use, it
@ -718,12 +716,12 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
} }
case Instruction::ExtractElement: { case Instruction::ExtractElement: {
if (Oprnds.size() != 2) if (Oprnds.size() != 2)
throw std::string("Invalid extractelement instruction!"); error("Invalid extractelement instruction!");
Value *V1 = getValue(iType, Oprnds[0]); Value *V1 = getValue(iType, Oprnds[0]);
Value *V2 = getValue(Type::UIntTyID, Oprnds[1]); Value *V2 = getValue(Type::UIntTyID, Oprnds[1]);
if (!ExtractElementInst::isValidOperands(V1, V2)) if (!ExtractElementInst::isValidOperands(V1, V2))
throw std::string("Invalid extractelement instruction!"); error("Invalid extractelement instruction!");
Result = new ExtractElementInst(V1, V2); Result = new ExtractElementInst(V1, V2);
break; break;
@ -731,28 +729,28 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
case Instruction::InsertElement: { case Instruction::InsertElement: {
const PackedType *PackedTy = dyn_cast<PackedType>(InstTy); const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
if (!PackedTy || Oprnds.size() != 3) if (!PackedTy || Oprnds.size() != 3)
throw std::string("Invalid insertelement instruction!"); error("Invalid insertelement instruction!");
Value *V1 = getValue(iType, Oprnds[0]); Value *V1 = getValue(iType, Oprnds[0]);
Value *V2 = getValue(getTypeSlot(PackedTy->getElementType()), Oprnds[1]); Value *V2 = getValue(getTypeSlot(PackedTy->getElementType()), Oprnds[1]);
Value *V3 = getValue(Type::UIntTyID, Oprnds[2]); Value *V3 = getValue(Type::UIntTyID, Oprnds[2]);
if (!InsertElementInst::isValidOperands(V1, V2, V3)) if (!InsertElementInst::isValidOperands(V1, V2, V3))
throw std::string("Invalid insertelement instruction!"); error("Invalid insertelement instruction!");
Result = new InsertElementInst(V1, V2, V3); Result = new InsertElementInst(V1, V2, V3);
break; break;
} }
case Instruction::ShuffleVector: { case Instruction::ShuffleVector: {
const PackedType *PackedTy = dyn_cast<PackedType>(InstTy); const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
if (!PackedTy || Oprnds.size() != 3) if (!PackedTy || Oprnds.size() != 3)
throw std::string("Invalid shufflevector instruction!"); error("Invalid shufflevector instruction!");
Value *V1 = getValue(iType, Oprnds[0]); Value *V1 = getValue(iType, Oprnds[0]);
Value *V2 = getValue(iType, Oprnds[1]); Value *V2 = getValue(iType, Oprnds[1]);
const PackedType *EltTy = const PackedType *EltTy =
PackedType::get(Type::UIntTy, PackedTy->getNumElements()); PackedType::get(Type::UIntTy, PackedTy->getNumElements());
Value *V3 = getValue(getTypeSlot(EltTy), Oprnds[2]); Value *V3 = getValue(getTypeSlot(EltTy), Oprnds[2]);
if (!ShuffleVectorInst::isValidOperands(V1, V2, V3)) if (!ShuffleVectorInst::isValidOperands(V1, V2, V3))
throw std::string("Invalid shufflevector instruction!"); error("Invalid shufflevector instruction!");
Result = new ShuffleVectorInst(V1, V2, V3); Result = new ShuffleVectorInst(V1, V2, V3);
break; break;
} }
@ -2403,10 +2401,28 @@ void BytecodeReader::ParseModule() {
/// This function completely parses a bytecode buffer given by the \p Buf /// This function completely parses a bytecode buffer given by the \p Buf
/// and \p Length parameters. /// and \p Length parameters.
void BytecodeReader::ParseBytecode(BufPtr Buf, unsigned Length, bool BytecodeReader::ParseBytecode(BufPtr Buf, unsigned Length,
const std::string &ModuleID) { const std::string &ModuleID,
std::string* ErrMsg) {
/// We handle errors by
if (setjmp(context)) {
// Cleanup after error
if (Handler) Handler->handleError(ErrorMsg);
freeState();
delete TheModule;
TheModule = 0;
if (decompressedBlock != 0 ) {
::free(decompressedBlock);
decompressedBlock = 0;
}
// Set caller's error message, if requested
if (ErrMsg)
*ErrMsg = ErrorMsg;
// Indicate an error occurred
return true;
}
try {
RevisionNum = 0; RevisionNum = 0;
At = MemStart = BlockStart = Buf; At = MemStart = BlockStart = Buf;
MemEnd = BlockEnd = Buf + Length; MemEnd = BlockEnd = Buf + Length;
@ -2490,28 +2506,8 @@ void BytecodeReader::ParseBytecode(BufPtr Buf, unsigned Length,
// Tell the handler we're finished the parse // Tell the handler we're finished the parse
if (Handler) Handler->handleFinish(); if (Handler) Handler->handleFinish();
} catch (std::string& errstr) { return false;
if (Handler) Handler->handleError(errstr);
freeState();
delete TheModule;
TheModule = 0;
if (decompressedBlock != 0 ) {
::free(decompressedBlock);
decompressedBlock = 0;
}
throw;
} catch (...) {
std::string msg("Unknown Exception Occurred");
if (Handler) Handler->handleError(msg);
freeState();
delete TheModule;
TheModule = 0;
if (decompressedBlock != 0) {
::free(decompressedBlock);
decompressedBlock = 0;
}
throw msg;
}
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@ -24,6 +24,7 @@
#include "llvm/Bytecode/Analyzer.h" #include "llvm/Bytecode/Analyzer.h"
#include <utility> #include <utility>
#include <map> #include <map>
#include <setjmp.h>
namespace llvm { namespace llvm {
@ -136,11 +137,13 @@ public:
/// @name Methods /// @name Methods
/// @{ /// @{
public: public:
/// @returns true if an error occurred
/// @brief Main interface to parsing a bytecode buffer. /// @brief Main interface to parsing a bytecode buffer.
void ParseBytecode( bool ParseBytecode(
const unsigned char *Buf, ///< Beginning of the bytecode buffer const unsigned char *Buf, ///< Beginning of the bytecode buffer
unsigned Length, ///< Length of the bytecode buffer unsigned Length, ///< Length of the bytecode buffer
const std::string &ModuleID ///< An identifier for the module constructed. const std::string &ModuleID, ///< An identifier for the module constructed.
std::string* ErrMsg = 0 ///< Optional place for error message
); );
/// @brief Parse all function bodies /// @brief Parse all function bodies
@ -260,6 +263,8 @@ protected:
/// @name Data /// @name Data
/// @{ /// @{
private: private:
std::string ErrorMsg; ///< A place to hold an error message through longjmp
jmp_buf context; ///< Where to return to if an error occurs.
char* decompressedBlock; ///< Result of decompression char* decompressedBlock; ///< Result of decompression
BufPtr MemStart; ///< Start of the memory buffer BufPtr MemStart; ///< Start of the memory buffer
BufPtr MemEnd; ///< End of the memory buffer BufPtr MemEnd; ///< End of the memory buffer
@ -487,7 +492,7 @@ private:
} }
} }
inline void error(std::string errmsg); inline void error(const std::string& errmsg);
BytecodeReader(const BytecodeReader &); // DO NOT IMPLEMENT BytecodeReader(const BytecodeReader &); // DO NOT IMPLEMENT
void operator=(const BytecodeReader &); // DO NOT IMPLEMENT void operator=(const BytecodeReader &); // DO NOT IMPLEMENT