Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/GT-3174_SleighErrorMessages'
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmkurtz committed Oct 28, 2019
2 parents 90854a0 + 688264f commit 07d3d4d
Show file tree
Hide file tree
Showing 25 changed files with 656 additions and 443 deletions.
24 changes: 15 additions & 9 deletions Ghidra/Features/Decompiler/src/decompile/cpp/pcodecompile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
*/
#include "pcodecompile.hh"

ExprTree::ExprTree(VarnodeTpl *vn)
string Location::format(void) const

{
ostringstream s;
s << filename << ":" << dec << lineno;
return s.str();
}

ExprTree::ExprTree(VarnodeTpl *vn)

{
outvn = vn;
Expand Down Expand Up @@ -305,9 +313,7 @@ vector<OpTpl *> *PcodeCompile::placeLabel(LabelSymbol *labsym)

{ // Create placeholder OpTpl for a label
if (labsym->isPlaced()) {
string errmsg = "Label " + labsym->getName();
errmsg += " is placed more than once";
reportError(errmsg);
reportError(getLocation(labsym), "Label '" + labsym->getName() + "' is placed more than once");
}
labsym->setPlaced();
vector<OpTpl *> *res = new vector<OpTpl *>;
Expand Down Expand Up @@ -335,7 +341,7 @@ vector<OpTpl *> *PcodeCompile::newOutput(bool usesLocalKey,ExprTree *rhs,string
sym = new VarnodeSymbol(*varname,tmpvn->getSpace().getSpace(),tmpvn->getOffset().getReal(),tmpvn->getSize().getReal()); // Create new symbol regardless
addSymbol(sym);
if ((!usesLocalKey) && enforceLocalKey)
reportError("Must use 'local' keyword to define symbol: "+*varname);
reportError(getLocation(sym), "Must use 'local' keyword to define symbol '"+*varname + "'");
delete varname;
return ExprTree::toVector(rhs);
}
Expand Down Expand Up @@ -633,7 +639,7 @@ vector<OpTpl *> *PcodeCompile::assignBitRange(VarnodeTpl *vn,uint4 bitoffset,uin
}

if (errmsg.size()>0) { // Was there an error condition
reportError(errmsg); // Report the error
reportError((const Location *)0, errmsg); // Report the error
delete vn; // Clean up
vector<OpTpl *> *resops = rhs->ops; // Passthru old expression
rhs->ops = (vector<OpTpl *> *)0;
Expand All @@ -647,6 +653,7 @@ vector<OpTpl *> *PcodeCompile::assignBitRange(VarnodeTpl *vn,uint4 bitoffset,uin
ExprTree *res;
VarnodeTpl *finalout = buildTruncatedVarnode(vn,bitoffset,numbits);
if (finalout != (VarnodeTpl *)0) {
delete vn; // Don't keep the original Varnode object
res = createOpOutUnary(finalout,CPUI_COPY,rhs);
}
else {
Expand All @@ -663,7 +670,7 @@ vector<OpTpl *> *PcodeCompile::assignBitRange(VarnodeTpl *vn,uint4 bitoffset,uin
res = createOpOut(finalout,CPUI_INT_OR,res,rhs);
}
if (errmsg.size() > 0)
reportError(errmsg);
reportError((const Location *)0, errmsg);
vector<OpTpl *> *resops = res->ops;
res->ops = (vector<OpTpl *> *)0;
delete res;
Expand Down Expand Up @@ -737,7 +744,7 @@ ExprTree *PcodeCompile::createBitRange(SpecificSymbol *sym,uint4 bitoffset,uint4
ExprTree *res = new ExprTree(vn);

if (errmsg.size()>0) { // Check for error condition
reportError(errmsg);
reportError(getLocation(sym), errmsg);
return res;
}

Expand Down Expand Up @@ -774,4 +781,3 @@ VarnodeTpl *PcodeCompile::addressOf(VarnodeTpl *var,uint4 size)
delete var;
return res;
}

15 changes: 14 additions & 1 deletion Ghidra/Features/Decompiler/src/decompile/cpp/pcodecompile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@

#include "slghsymbol.hh"

class Location {
string filename;
int4 lineno;
public:
Location(void) {}
Location(const string &fname, const int4 line) { filename = fname; lineno = line; }
string getFilename(void) const { return filename; }
int4 getLineno(void) const { return lineno; }
string format(void) const;
};

struct StarQuality {
ConstTpl id;
uint4 size;
Expand Down Expand Up @@ -53,7 +64,9 @@ public:
PcodeCompile(void) { defaultspace=(AddrSpace *)0; constantspace=(AddrSpace *)0;
uniqspace=(AddrSpace *)0; local_labelcount=0; enforceLocalKey=false; }
virtual ~PcodeCompile(void) {}
virtual void reportError(const string &msg)=0;
virtual const Location *getLocation(SleighSymbol *sym) const=0;
virtual void reportError(const Location *loc, const string &msg)=0;
virtual void reportWarning(const Location *loc, const string &msg)=0;
void resetLabelCount(void) { local_labelcount=0; }
void setDefaultSpace(AddrSpace *spc) { defaultspace = spc; }
void setConstantSpace(AddrSpace *spc) { constantspace = spc; }
Expand Down
4 changes: 3 additions & 1 deletion Ghidra/Features/Decompiler/src/decompile/cpp/pcodeparse.hh
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public:
void setResult(ConstructTpl *res) { result = res; }
ConstructTpl *releaseResult(void) { ConstructTpl *res = result; result = (ConstructTpl *)0; return res; }
virtual ~PcodeSnippet(void);
virtual void reportError(const string &msg);
virtual const Location *getLocation(SleighSymbol *sym) const { return (const Location *)0; }
virtual void reportError(const Location *loc, const string &msg);
virtual void reportWarning(const Location *loc, const string &msg) {}
bool hasErrors(void) const { return (errorcount != 0); }
const string getErrorMessage(void) const { return firsterror; }
void setUniqueBase(uintb val) { tempbase = val; }
Expand Down
10 changes: 5 additions & 5 deletions Ghidra/Features/Decompiler/src/decompile/cpp/pcodeparse.y
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ void PcodeSnippet::addSymbol(SleighSymbol *sym)

res = tree.insert( sym );
if (!res.second) {
reportError("Duplicate symbol name: "+sym->getName());
reportError((const Location *)0,"Duplicate symbol name: "+sym->getName());
delete sym; // Symbol is unattached to anything else
}
}
Expand Down Expand Up @@ -712,7 +712,7 @@ PcodeSnippet::~PcodeSnippet(void)
}
}

void PcodeSnippet::reportError(const string &msg)
void PcodeSnippet::reportError(const Location *loc, const string &msg)

{
if (errorcount == 0)
Expand Down Expand Up @@ -779,11 +779,11 @@ int4 PcodeSnippet::lex(void)
pcode = this; // Setup global object for yyparse
int4 res = yyparse();
if (res != 0) {
reportError("Syntax error");
reportError((const Location *)0,"Syntax error");
return false;
}
if (!PcodeCompile::propagateSize(result)) {
reportError("Could not resolve at least 1 variable size");
reportError((const Location *)0,"Could not resolve at least 1 variable size");
return false;
}
return true;
Expand All @@ -803,6 +803,6 @@ int yylex(void) {
int yyerror(const char *s)

{
pcode->reportError(s);
pcode->reportError((const Location *)0,s);
return 0;
}
15 changes: 7 additions & 8 deletions Ghidra/Features/Decompiler/src/decompile/cpp/sleighbase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ SleighBase::SleighBase(void)

/// Assuming the symbol table is populated, iterate through the table collecting
/// registers (for the map), user-op names, and context fields.
void SleighBase::buildXrefs(void)
void SleighBase::buildXrefs(vector<string> &errorPairs)

{
SymbolScope *glb = symtab.getGlobalScope();
SymbolTree::const_iterator iter;
SleighSymbol *sym;
int4 errors = 0;
ostringstream s;

for(iter=glb->begin();iter!=glb->end();++iter) {
Expand All @@ -43,9 +42,8 @@ void SleighBase::buildXrefs(void)
pair<VarnodeData,string> ins(((VarnodeSymbol *)sym)->getFixedVarnode(),sym->getName());
pair<map<VarnodeData,string>::iterator,bool> res = varnode_xref.insert(ins);
if (!res.second) {
s << "Duplicate (offset,size) pair for registers: ";
s << sym->getName() << " and " << (*(res.first)).second << '\n';
errors += 1;
errorPairs.push_back(sym->getName());
errorPairs.push_back((*(res.first)).second);
}
}
else if (sym->getType() == SleighSymbol::userop_symbol) {
Expand All @@ -62,8 +60,6 @@ void SleighBase::buildXrefs(void)
registerContext(csym->getName(),startbit,endbit);
}
}
if (errors > 0)
throw SleighError(s.str());
}

/// If \b this SleighBase is being reused with a new program, the context
Expand Down Expand Up @@ -239,5 +235,8 @@ void SleighBase::restoreXml(const Element *el)
iter++;
symtab.restoreXml(*iter,this);
root = (SubtableSymbol *)symtab.getGlobalScope()->findSymbol("instruction");
buildXrefs();
vector<string> errorPairs;
buildXrefs(errorPairs);
if (!errorPairs.empty())
throw SleighError("Duplicate register pairs");
}
2 changes: 1 addition & 1 deletion Ghidra/Features/Decompiler/src/decompile/cpp/sleighbase.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected:
uint4 maxdelayslotbytes; ///< Maximum number of bytes in a delay-slot directive
uint4 unique_allocatemask; ///< Bits that are guaranteed to be zero in the unique allocation scheme
uint4 numSections; ///< Number of \e named sections
void buildXrefs(void); ///< Build register map. Collect user-ops and context-fields.
void buildXrefs(vector<string> &errorPairs); ///< Build register map. Collect user-ops and context-fields.
void reregisterContext(void); ///< Reregister context fields for a new executable
void restoreXml(const Element *el); ///< Read a SLEIGH specification from XML
public:
Expand Down
Loading

0 comments on commit 07d3d4d

Please sign in to comment.