* Add calls to failure template so that it is actually possible to debug

why bytecode parsing is failing.  Just put a breakpoint in the failure
  templates.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@323 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2001-07-28 17:50:18 +00:00
parent 81e29632f7
commit 3d3f289eac
4 changed files with 134 additions and 124 deletions

View File

@ -19,7 +19,7 @@ bool BytecodeParser::parseTypeConstant(const uchar *&Buf, const uchar *EndBuf,
const Type *Val = 0;
unsigned PrimType;
if (read_vbr(Buf, EndBuf, PrimType)) return true;
if (read_vbr(Buf, EndBuf, PrimType)) return failure(true);
if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType))) {
V = new ConstPoolType(Val); // It's just a primitive ID.
@ -29,18 +29,18 @@ bool BytecodeParser::parseTypeConstant(const uchar *&Buf, const uchar *EndBuf,
switch (PrimType) {
case Type::MethodTyID: {
unsigned Typ;
if (read_vbr(Buf, EndBuf, Typ)) return true;
if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
const Type *RetType = getType(Typ);
if (RetType == 0) return true;
if (RetType == 0) return failure(true);
unsigned NumParams;
if (read_vbr(Buf, EndBuf, NumParams)) return true;
if (read_vbr(Buf, EndBuf, NumParams)) return failure(true);
MethodType::ParamTypes Params;
while (NumParams--) {
if (read_vbr(Buf, EndBuf, Typ)) return true;
if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
const Type *Ty = getType(Typ);
if (Ty == 0) return true;
if (Ty == 0) return failure(true);
Params.push_back(Ty);
}
@ -49,12 +49,12 @@ bool BytecodeParser::parseTypeConstant(const uchar *&Buf, const uchar *EndBuf,
}
case Type::ArrayTyID: {
unsigned ElTyp;
if (read_vbr(Buf, EndBuf, ElTyp)) return true;
if (read_vbr(Buf, EndBuf, ElTyp)) return failure(true);
const Type *ElementType = getType(ElTyp);
if (ElementType == 0) return true;
if (ElementType == 0) return failure(true);
int NumElements;
if (read_vbr(Buf, EndBuf, NumElements)) return true;
if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
Val = ArrayType::getArrayType(ElementType, NumElements);
break;
}
@ -62,13 +62,13 @@ bool BytecodeParser::parseTypeConstant(const uchar *&Buf, const uchar *EndBuf,
unsigned Typ;
StructType::ElementTypes Elements;
if (read_vbr(Buf, EndBuf, Typ)) return true;
if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
while (Typ) { // List is terminated by void/0 typeid
const Type *Ty = getType(Typ);
if (Ty == 0) return true;
if (Ty == 0) return failure(true);
Elements.push_back(Ty);
if (read_vbr(Buf, EndBuf, Typ)) return true;
if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
}
Val = StructType::getStructType(Elements);
@ -76,9 +76,9 @@ bool BytecodeParser::parseTypeConstant(const uchar *&Buf, const uchar *EndBuf,
}
case Type::PointerTyID: {
unsigned ElTyp;
if (read_vbr(Buf, EndBuf, ElTyp)) return true;
if (read_vbr(Buf, EndBuf, ElTyp)) return failure(true);
const Type *ElementType = getType(ElTyp);
if (ElementType == 0) return true;
if (ElementType == 0) return failure(true);
Val = PointerType::getPointerType(ElementType);
break;
}
@ -86,7 +86,7 @@ bool BytecodeParser::parseTypeConstant(const uchar *&Buf, const uchar *EndBuf,
default:
cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to deserialize"
<< " primitive Type " << PrimType << "\n";
return true;
return failure(true);
}
V = new ConstPoolType(Val);
@ -99,8 +99,8 @@ bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
switch (Ty->getPrimitiveID()) {
case Type::BoolTyID: {
unsigned Val;
if (read_vbr(Buf, EndBuf, Val)) return true;
if (Val != 0 && Val != 1) return true;
if (read_vbr(Buf, EndBuf, Val)) return failure(true);
if (Val != 0 && Val != 1) return failure(true);
V = new ConstPoolBool(Val == 1);
break;
}
@ -109,15 +109,15 @@ bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
case Type::UShortTyID:
case Type::UIntTyID: {
unsigned Val;
if (read_vbr(Buf, EndBuf, Val)) return true;
if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return true;
if (read_vbr(Buf, EndBuf, Val)) return failure(true);
if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return failure(true);
V = new ConstPoolUInt(Ty, Val);
break;
}
case Type::ULongTyID: {
uint64_t Val;
if (read_vbr(Buf, EndBuf, Val)) return true;
if (read_vbr(Buf, EndBuf, Val)) return failure(true);
V = new ConstPoolUInt(Ty, Val);
break;
}
@ -126,35 +126,35 @@ bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
case Type::ShortTyID:
case Type::IntTyID: {
int Val;
if (read_vbr(Buf, EndBuf, Val)) return true;
if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return 0;
if (read_vbr(Buf, EndBuf, Val)) return failure(true);
if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return failure(true);
V = new ConstPoolSInt(Ty, Val);
break;
}
case Type::LongTyID: {
int64_t Val;
if (read_vbr(Buf, EndBuf, Val)) return true;
if (read_vbr(Buf, EndBuf, Val)) return failure(true);
V = new ConstPoolSInt(Ty, Val);
break;
}
case Type::FloatTyID: {
float F;
if (input_data(Buf, EndBuf, &F, &F+1)) return true;
if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
V = new ConstPoolFP(Ty, F);
break;
}
case Type::DoubleTyID: {
double Val;
if (input_data(Buf, EndBuf, &Val, &Val+1)) return true;
if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
V = new ConstPoolFP(Ty, Val);
break;
}
case Type::TypeTyID:
if (parseTypeConstant(Buf, EndBuf, V)) return true;
if (parseTypeConstant(Buf, EndBuf, V)) return failure(true);
break;
case Type::ArrayTyID: {
@ -163,15 +163,14 @@ bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
if (AT->isSized()) // Sized array, # elements stored in type!
NumElements = (unsigned)AT->getNumElements();
else // Unsized array, # elements stored in stream!
if (read_vbr(Buf, EndBuf, NumElements)) return true;
if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
vector<ConstPoolVal *> Elements;
while (NumElements--) { // Read all of the elements of the constant.
unsigned Slot;
if (read_vbr(Buf, EndBuf, Slot)) return true;
if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Value *V = getValue(AT->getElementType(), Slot, false);
if (!V || !V->isConstant())
return true;
if (!V || !V->isConstant()) return failure(true);
Elements.push_back((ConstPoolVal*)V);
}
V = new ConstPoolArray(AT, Elements);
@ -185,10 +184,10 @@ bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
vector<ConstPoolVal *> Elements;
for (unsigned i = 0; i < ET.size(); ++i) {
unsigned Slot;
if (read_vbr(Buf, EndBuf, Slot)) return true;
if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Value *V = getValue(ET[i], Slot, false);
if (!V || !V->isConstant())
return true;
return failure(true);
Elements.push_back((ConstPoolVal*)V);
}
@ -200,7 +199,7 @@ bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
cerr << __FILE__ << ":" << __LINE__
<< ": Don't know how to deserialize constant value of type '"
<< Ty->getName() << "'\n";
return true;
return failure(true);
}
return false;
}
@ -212,13 +211,13 @@ bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
unsigned NumEntries, Typ;
if (read_vbr(Buf, EndBuf, NumEntries) ||
read_vbr(Buf, EndBuf, Typ)) return true;
read_vbr(Buf, EndBuf, Typ)) return failure(true);
const Type *Ty = getType(Typ);
if (Ty == 0) return true;
if (Ty == 0) return failure(true);
for (unsigned i = 0; i < NumEntries; i++) {
ConstPoolVal *I;
if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return true;
if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return failure(true);
#if 0
cerr << " Read const value: <" << I->getType()->getName()
<< ">: " << I->getStrValue() << endl;
@ -228,5 +227,6 @@ bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
}
}
return Buf > EndBuf;
if (Buf > EndBuf) return failure(true);
return false;
}

View File

@ -20,7 +20,7 @@
bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf,
RawInst &Result) {
unsigned Op, Typ;
if (read(Buf, EndBuf, Op)) return true;
if (read(Buf, EndBuf, Op)) return failure(true);
Result.NumOperands = Op >> 30;
Result.Opcode = (Op >> 24) & 63;
@ -45,38 +45,38 @@ bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf,
break;
case 0:
Buf -= 4; // Hrm, try this again...
if (read_vbr(Buf, EndBuf, Result.Opcode)) return true;
if (read_vbr(Buf, EndBuf, Typ)) return true;
if (read_vbr(Buf, EndBuf, Result.Opcode)) return failure(true);
if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
Result.Ty = getType(Typ);
if (read_vbr(Buf, EndBuf, Result.NumOperands)) return true;
if (read_vbr(Buf, EndBuf, Result.NumOperands)) return failure(true);
switch (Result.NumOperands) {
case 0:
cerr << "Zero Arg instr found!\n";
return true; // This encoding is invalid!
return failure(true); // This encoding is invalid!
case 1:
if (read_vbr(Buf, EndBuf, Result.Arg1)) return true;
if (read_vbr(Buf, EndBuf, Result.Arg1)) return failure(true);
break;
case 2:
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
read_vbr(Buf, EndBuf, Result.Arg2)) return true;
read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
break;
case 3:
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
read_vbr(Buf, EndBuf, Result.Arg2) ||
read_vbr(Buf, EndBuf, Result.Arg3)) return true;
read_vbr(Buf, EndBuf, Result.Arg3)) return failure(true);
break;
default:
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
read_vbr(Buf, EndBuf, Result.Arg2)) return true;
read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
// Allocate a vector to hold arguments 3, 4, 5, 6 ...
Result.VarArgs = new vector<unsigned>(Result.NumOperands-2);
for (unsigned a = 0; a < Result.NumOperands-2; a++)
if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return true;
if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return failure(true);
break;
}
if (align32(Buf, EndBuf)) return true;
if (align32(Buf, EndBuf)) return failure(true);
break;
}
@ -92,7 +92,7 @@ bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf,
bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
Instruction *&Res) {
RawInst Raw;
if (ParseRawInst(Buf, EndBuf, Raw)) return true;;
if (ParseRawInst(Buf, EndBuf, Raw)) return failure(true);
if (Raw.Opcode >= Instruction::FirstUnaryOp &&
Raw.Opcode < Instruction::NumUnaryOps && Raw.NumOperands == 1) {
@ -120,7 +120,7 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
case 1:
case 3: cerr << "Invalid phi node encountered!\n";
delete PN;
return true;
return failure(true);
case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
(BasicBlock*)getValue(Type::LabelTy, Raw.Arg2));
break;
@ -130,7 +130,7 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
if (Raw.VarArgs->size() & 1) {
cerr << "PHI Node with ODD number of arguments!\n";
delete PN;
return true;
return failure(true);
} else {
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0; i < args.size(); i+=2)
@ -180,7 +180,7 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
cerr << "Switch statement with odd number of arguments!\n";
delete I;
return true;
return failure(true);
}
vector<unsigned> &args = *Raw.VarArgs;
@ -194,7 +194,7 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
case Instruction::Call: {
Method *M = (Method*)getValue(Raw.Ty, Raw.Arg1);
if (M == 0) return true;
if (M == 0) return failure(true);
vector<Value *> Params;
const MethodType::ParamTypes &PL = M->getMethodType()->getParamTypes();
@ -204,37 +204,36 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
switch (Raw.NumOperands) {
case 0: cerr << "Invalid call instruction encountered!\n";
return true;
return failure(true);
case 1: break;
case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
case 3: Params.push_back(getValue(*It++, Raw.Arg2));
if (It == PL.end()) return true;
if (It == PL.end()) return failure(true);
Params.push_back(getValue(*It++, Raw.Arg3)); break;
default:
Params.push_back(getValue(*It++, Raw.Arg2));
{
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0; i < args.size(); i++) {
if (It == PL.end()) return true;
if (It == PL.end()) return failure(true);
// TODO: Check getValue for null!
Params.push_back(getValue(*It++, args[i]));
}
}
delete Raw.VarArgs;
}
if (It != PL.end()) return true;
if (It != PL.end()) return failure(true);
} else {
// The first parameter does not have a type specifier... because there
// must be at least one concrete argument to a vararg type...
Params.push_back(getValue(PL.front(), Raw.Arg2));
vector<unsigned> &args = *Raw.VarArgs;
if ((args.size() & 1) != 0) return true; // Must be pairs of type/value
if ((args.size() & 1) != 0)
return failure(true); // Must be pairs of type/value
for (unsigned i = 0; i < args.size(); i+=2) {
Value *Ty = getValue(Type::TypeTy, args[i]);
if (!Ty) return true;
// TODO: Check getValue for null!
Params.push_back(getValue(Ty->castTypeAsserting(), args[i+1]));
Params.push_back(getValue(getType(args[i]), args[i+1]));
}
delete Raw.VarArgs;
}
@ -243,20 +242,20 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
return false;
}
case Instruction::Malloc:
if (Raw.NumOperands > 2) return true;
if (Raw.NumOperands > 2) return failure(true);
V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
Res = new MallocInst(Raw.Ty, V);
return false;
case Instruction::Alloca:
if (Raw.NumOperands > 2) return true;
if (Raw.NumOperands > 2) return failure(true);
V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
Res = new AllocaInst(Raw.Ty, V);
return false;
case Instruction::Free:
V = getValue(Raw.Ty, Raw.Arg1);
if (!V->getType()->isPointerType()) return true;
if (!V->getType()->isPointerType()) return failure(true);
Res = new FreeInst(V);
return false;
@ -264,27 +263,27 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
case Instruction::GetElementPtr: {
vector<ConstPoolVal*> Idx;
switch (Raw.NumOperands) {
case 0: cerr << "Invalid load encountered!\n"; return true;
case 0: cerr << "Invalid load encountered!\n"; return failure(true);
case 1: break;
case 2: V = getValue(Type::UByteTy, Raw.Arg2);
if (!V->isConstant()) return true;
if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
break;
case 3: V = getValue(Type::UByteTy, Raw.Arg2);
if (!V->isConstant()) return true;
if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
V = getValue(Type::UByteTy, Raw.Arg3);
if (!V->isConstant()) return true;
if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
break;
default:
V = getValue(Type::UByteTy, Raw.Arg2);
if (!V->isConstant()) return true;
if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0, E = args.size(); i != E; ++i) {
V = getValue(Type::UByteTy, args[i]);
if (!V->isConstant()) return true;
if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
}
delete Raw.VarArgs;
@ -302,17 +301,17 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
vector<ConstPoolVal*> Idx;
switch (Raw.NumOperands) {
case 0:
case 1: cerr << "Invalid store encountered!\n"; return true;
case 1: cerr << "Invalid store encountered!\n"; return failure(true);
case 2: break;
case 3: V = getValue(Type::UByteTy, Raw.Arg3);
if (!V->isConstant()) return true;
if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
break;
default:
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0, E = args.size(); i != E; ++i) {
V = getValue(Type::UByteTy, args[i]);
if (!V->isConstant()) return true;
if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
}
delete Raw.VarArgs;
@ -320,7 +319,7 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
}
const Type *ElType = StoreInst::getIndexedType(Raw.Ty, Idx);
if (ElType == 0) return true;
if (ElType == 0) return failure(true);
Res = new StoreInst(getValue(ElType, Raw.Arg1), getValue(Raw.Ty, Raw.Arg2),
Idx);
return false;
@ -329,5 +328,5 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
cerr << "Unrecognized instruction! " << Raw.Opcode
<< " ADDR = 0x" << (void*)Buf << endl;
return true;
return failure(true);
}

View File

@ -30,7 +30,7 @@ bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
Slot = Ty->getPrimitiveID();
} else {
TypeMapType::iterator I = TypeMap.find(Ty);
if (I == TypeMap.end()) return true; // Didn't find type!
if (I == TypeMap.end()) return failure(true); // Didn't find type!
Slot = I->second;
}
//cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << endl;
@ -44,7 +44,7 @@ const Type *BytecodeParser::getType(unsigned ID) {
//cerr << "Looking up Type ID: " << ID << endl;
const Value *D = getValue(Type::TypeTy, ID, false);
if (D == 0) return 0;
if (D == 0) return failure<const Type*>(0);
assert(D->getType() == Type::TypeTy);
return ((const ConstPoolType*)D->castConstantAsserting())->getValue();
@ -52,7 +52,7 @@ const Type *BytecodeParser::getType(unsigned ID) {
bool BytecodeParser::insertValue(Value *Def, vector<ValueList> &ValueTab) {
unsigned type;
if (getTypeSlot(Def->getType(), type)) return true;
if (getTypeSlot(Def->getType(), type)) return failure(true);
if (ValueTab.size() <= type)
ValueTab.resize(type+1, ValueList());
@ -80,7 +80,7 @@ Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
unsigned Num = oNum;
unsigned type; // The type plane it lives in...
if (getTypeSlot(Ty, type)) return 0; // TODO: true
if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
if (type == Type::TypeTyID) { // The 'type' plane has implicit values
const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
@ -99,7 +99,7 @@ Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
if (Values.size() > type && Values[type].size() > Num)
return Values[type][Num];
if (!Create) return 0; // Do not create a placeholder?
if (!Create) return failure<Value*>(0); // Do not create a placeholder?
Value *d = 0;
switch (Ty->getPrimitiveID()) {
@ -114,7 +114,7 @@ Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
}
assert(d != 0 && "How did we not make something?");
if (insertValue(d, LateResolveValues)) return 0;
if (insertValue(d, LateResolveValues)) return failure<Value*>(0);
return d;
}
@ -156,11 +156,11 @@ bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
Instruction *Def;
if (ParseInstruction(Buf, EndBuf, Def)) {
delete BB;
return true;
return failure(true);
}
if (Def == 0) { delete BB; return true; }
if (insertValue(Def, Values)) { delete BB; return true; }
if (Def == 0) { delete BB; return failure(true); }
if (insertValue(Def, Values)) { delete BB; return failure(true); }
BB->getInstList().push_back(Def);
}
@ -173,25 +173,26 @@ bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf) {
// Symtab block header: [num entries][type id number]
unsigned NumEntries, Typ;
if (read_vbr(Buf, EndBuf, NumEntries) ||
read_vbr(Buf, EndBuf, Typ)) return true;
read_vbr(Buf, EndBuf, Typ)) return failure(true);
const Type *Ty = getType(Typ);
if (Ty == 0) return true;
if (Ty == 0) return failure(true);
for (unsigned i = 0; i < NumEntries; ++i) {
// Symtab entry: [def slot #][name]
unsigned slot;
if (read_vbr(Buf, EndBuf, slot)) return true;
if (read_vbr(Buf, EndBuf, slot)) return failure(true);
string Name;
if (read(Buf, EndBuf, Name, false)) // Not aligned...
return true;
return failure(true);
Value *D = getValue(Ty, slot, false); // Find mapping...
if (D == 0) return true;
if (D == 0) return failure(true);
D->setName(Name);
}
}
return Buf > EndBuf;
if (Buf > EndBuf) return failure(true);
return false;
}
@ -199,7 +200,7 @@ bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
Module *C) {
// Clear out the local values table...
Values.clear();
if (MethodSignatureList.empty()) return true; // Unexpected method!
if (MethodSignatureList.empty()) return failure(true); // Unexpected method!
const MethodType *MTy = MethodSignatureList.front().first;
unsigned MethSlot = MethodSignatureList.front().second;
@ -210,20 +211,20 @@ bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
for (MethodType::ParamTypes::const_iterator It = Params.begin();
It != Params.end(); ++It) {
MethodArgument *MA = new MethodArgument(*It);
if (insertValue(MA, Values)) { delete M; return true; }
if (insertValue(MA, Values)) { delete M; return failure(true); }
M->getArgumentList().push_back(MA);
}
while (Buf < EndBuf) {
unsigned Type, Size;
const uchar *OldBuf = Buf;
if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return true; }
if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return failure(true); }
switch (Type) {
case BytecodeFormat::ConstantPool:
if (ParseConstantPool(Buf, Buf+Size, M->getConstantPool(), Values)) {
cerr << "Error reading constant pool!\n";
delete M; return true;
delete M; return failure(true);
}
break;
@ -232,7 +233,7 @@ bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
if (ParseBasicBlock(Buf, Buf+Size, BB) ||
insertValue(BB, Values)) {
cerr << "Error parsing basic block!\n";
delete M; return true; // Parse error... :(
delete M; return failure(true); // Parse error... :(
}
M->getBasicBlocks().push_back(BB);
@ -242,24 +243,24 @@ bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
case BytecodeFormat::SymbolTable:
if (ParseSymbolTable(Buf, Buf+Size)) {
cerr << "Error reading method symbol table!\n";
delete M; return true;
delete M; return failure(true);
}
break;
default:
Buf += Size;
if (OldBuf > Buf) return true; // Wrap around!
if (OldBuf > Buf) return failure(true); // Wrap around!
break;
}
if (align32(Buf, EndBuf)) {
delete M; // Malformed bc file, read past end of block.
return true;
return failure(true);
}
}
if (postResolveValues(LateResolveValues) ||
postResolveValues(LateResolveModuleValues)) {
delete M; return true; // Unresolvable references!
delete M; return failure(true); // Unresolvable references!
}
Value *MethPHolder = getValue(MTy, MethSlot, false);
@ -287,18 +288,19 @@ bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Module *C) {
if (!MethodSignatureList.empty()) return true; // Two ModuleGlobal blocks?
if (!MethodSignatureList.empty())
return failure(true); // Two ModuleGlobal blocks?
// Read the method signatures for all of the methods that are coming, and
// create fillers in the Value tables.
unsigned MethSignature;
if (read_vbr(Buf, End, MethSignature)) return true;
if (read_vbr(Buf, End, MethSignature)) return failure(true);
while (MethSignature != Type::VoidTyID) { // List is terminated by Void
const Type *Ty = getType(MethSignature);
if (!Ty || !Ty->isMethodType()) {
cerr << "Method not meth type! ";
if (Ty) cerr << Ty->getName(); else cerr << MethSignature; cerr << endl;
return true;
return failure(true);
}
// When the ModuleGlobalInfo section is read, we load the type of each method
@ -312,7 +314,7 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
// Figure out which entry of its typeslot it went into...
unsigned TypeSlot;
if (getTypeSlot(Def->getType(), TypeSlot)) return true;
if (getTypeSlot(Def->getType(), TypeSlot)) return failure(true);
unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
@ -320,10 +322,10 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
// methods are loaded...
//
MethodSignatureList.push_back(make_pair((const MethodType*)Ty, SlotNo));
if (read_vbr(Buf, End, MethSignature)) return true;
if (read_vbr(Buf, End, MethSignature)) return failure(true);
}
if (align32(Buf, End)) return true;
if (align32(Buf, End)) return failure(true);
// This is for future proofing... in the future extra fields may be added that
// we don't understand, so we transparently ignore them.
@ -336,39 +338,39 @@ bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
Module *&C) {
unsigned Type, Size;
if (readBlock(Buf, EndBuf, Type, Size)) return true;
if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
return true; // Hrm, not a class?
return failure(true); // Hrm, not a class?
MethodSignatureList.clear(); // Just in case...
// Read into instance variables...
if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true;
if (align32(Buf, EndBuf)) return true;
if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
if (align32(Buf, EndBuf)) return failure(true);
C = new Module();
while (Buf < EndBuf) {
const uchar *OldBuf = Buf;
if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return true; }
if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
switch (Type) {
case BytecodeFormat::ModuleGlobalInfo:
if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
cerr << "Error reading class global info section!\n";
delete C; return true;
delete C; return failure(true);
}
break;
case BytecodeFormat::ConstantPool:
if (ParseConstantPool(Buf, Buf+Size, C->getConstantPool(), ModuleValues)) {
cerr << "Error reading class constant pool!\n";
delete C; return true;
delete C; return failure(true);
}
break;
case BytecodeFormat::Method: {
if (ParseMethod(Buf, Buf+Size, C)) {
delete C; return true; // Error parsing method
delete C; return failure(true); // Error parsing method
}
break;
}
@ -376,21 +378,21 @@ bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
case BytecodeFormat::SymbolTable:
if (ParseSymbolTable(Buf, Buf+Size)) {
cerr << "Error reading class symbol table!\n";
delete C; return true;
delete C; return failure(true);
}
break;
default:
cerr << "Unknown class block: " << Type << endl;
Buf += Size;
if (OldBuf > Buf) return true; // Wrap around!
if (OldBuf > Buf) return failure(true); // Wrap around!
break;
}
if (align32(Buf, EndBuf)) { delete C; return true; }
if (align32(Buf, EndBuf)) { delete C; return failure(true); }
}
if (!MethodSignatureList.empty()) // Expected more methods!
return true;
return failure(true);
return false;
}
@ -400,7 +402,7 @@ Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
// Read and check signature...
if (read(Buf, EndBuf, Sig) ||
Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
return 0; // Invalid signature!
return failure<Module*>(0); // Invalid signature!
Module *Result;
if (ParseModule(Buf, EndBuf, Result)) return 0;
@ -421,15 +423,15 @@ Module *ParseBytecodeFile(const string &Filename) {
if (Filename != string("-")) { // Read from a file...
int FD = open(Filename.c_str(), O_RDONLY);
if (FD == -1) return 0;
if (FD == -1) return failure<Module*>(0);
if (fstat(FD, &StatBuf) == -1) { close(FD); return 0; }
if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
int Length = StatBuf.st_size;
if (Length == 0) { close(FD); return 0; }
if (Length == 0) { close(FD); return failure<Module*>(0); }
uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
MAP_PRIVATE, FD, 0);
if (Buffer == (uchar*)-1) { close(FD); return 0; }
if (Buffer == (uchar*)-1) { close(FD); return failure<Module*>(0); }
BytecodeParser Parser;
Result = Parser.ParseBytecode(Buffer, Buffer+Length);
@ -441,14 +443,14 @@ Module *ParseBytecodeFile(const string &Filename) {
int BlockSize;
uchar Buffer[4096], *FileData = 0;
while ((BlockSize = read(0, Buffer, 4))) {
if (BlockSize == -1) { free(FileData); return 0; }
if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
memcpy(FileData+FileSize, Buffer, BlockSize);
FileSize += BlockSize;
}
if (FileSize == 0) { free(FileData); return 0; }
if (FileSize == 0) { free(FileData); return failure<Module*>(0); }
#define ALIGN_PTRS 1
#if ALIGN_PTRS

View File

@ -136,4 +136,13 @@ static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
#endif
}
// failure Template - This template function is used as a place to put
// breakpoints in to debug failures of the bytecode parser.
//
template <typename X>
static X failure(X Value) {
return Value;
}
#endif