mirror of
https://github.com/NationalSecurityAgency/ghidra
synced 2024-08-27 13:03:12 +00:00
Merge remote-tracking branch
'origin/GP-2913_ghidracadabra_PR-4595_kkaempf_improve_sleigh_error_reporting' (Closes #4595)
This commit is contained in:
commit
93d3b41d01
|
@ -479,7 +479,7 @@ inline bool Range::contains(const Address &addr) const {
|
|||
|
||||
/// \param size is the desired size in bytes
|
||||
/// \return a value appropriate for masking off the first \e size bytes
|
||||
inline uintb calc_mask(int4 size) { return uintbmasks[(size<8)? size : 8]; }
|
||||
inline uintb calc_mask(int4 size) { return uintbmasks[((uint4)size) < 8 ? size : 8]; }
|
||||
|
||||
/// Perform a CPUI_INT_RIGHT on the given val
|
||||
/// \param val is the value to shift
|
||||
|
|
|
@ -1945,8 +1945,10 @@ void SleighCompile::buildPatterns(void)
|
|||
errors += 1;
|
||||
}
|
||||
for(int4 i=0;i<tables.size();++i) {
|
||||
if (tables[i]->isError())
|
||||
if (tables[i]->isError()) {
|
||||
reportError(getLocation(tables[i]), "Problem in table '"+tables[i]->getName() + "':" + msg.str());
|
||||
errors += 1;
|
||||
}
|
||||
if (tables[i]->getPattern() == (TokenPattern *)0) {
|
||||
reportWarning(getLocation(tables[i]), "Unreferenced table '"+tables[i]->getName() + "'");
|
||||
}
|
||||
|
@ -2133,8 +2135,11 @@ void SleighCompile::checkCaseSensitivity(void)
|
|||
SleighSymbol *oldsym = (*check.first).second;
|
||||
ostringstream s;
|
||||
s << "Name collision: " << sym->getName() << " --- ";
|
||||
s << "Duplicate symbol " << oldsym->getName();
|
||||
const Location *oldLocation = getLocation(oldsym);
|
||||
s << "Duplicate symbol " << oldsym->getName() << " defined at " << oldLocation->format();
|
||||
if (oldLocation != (Location *) 0x0) {
|
||||
s << " defined at " << oldLocation->format();
|
||||
}
|
||||
const Location *location = getLocation(sym);
|
||||
reportError(location,s.str());
|
||||
}
|
||||
|
@ -2188,11 +2193,15 @@ const Location *SleighCompile::getLocation(Constructor *ctor) const
|
|||
}
|
||||
|
||||
/// \param sym is the given symbol
|
||||
/// \return the filename and line number
|
||||
/// \return the filename and line number or null if location not found
|
||||
const Location *SleighCompile::getLocation(SleighSymbol *sym) const
|
||||
|
||||
{
|
||||
return &symbolLocationMap.at(sym);
|
||||
try {
|
||||
return &symbolLocationMap.at(sym);
|
||||
} catch (const out_of_range &e) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
/// The current filename and line number are placed into a Location object
|
||||
|
@ -2241,7 +2250,7 @@ void SleighCompile::reportError(const Location* loc, const string &msg)
|
|||
void SleighCompile::reportError(const string &msg)
|
||||
|
||||
{
|
||||
cerr << "ERROR " << msg << endl;
|
||||
cerr << filename.back() << ":" << lineno.back() << " - ERROR " << msg << endl;
|
||||
errors += 1;
|
||||
if (errors > 1000000) {
|
||||
cerr << "Too many errors: Aborting" << endl;
|
||||
|
@ -2670,7 +2679,10 @@ void SleighCompile::attachValues(vector<SleighSymbol *> *symlist,vector<intb> *n
|
|||
if (sym == (ValueSymbol *)0) continue;
|
||||
PatternValue *patval = sym->getPatternValue();
|
||||
if (patval->maxValue() + 1 != numlist->size()) {
|
||||
reportError(getCurrentLocation(), "Attach value '" + sym->getName() + "' is wrong size for list");
|
||||
ostringstream msg;
|
||||
msg << "Attach value '" + sym->getName();
|
||||
msg << "' (range 0.." << patval->maxValue() << ") is wrong size for list (of " << numlist->size() << " entries)";
|
||||
reportError(getCurrentLocation(), msg.str());
|
||||
}
|
||||
symtab.replaceSymbol(sym, new ValueMapSymbol(sym->getName(),patval,*numlist));
|
||||
}
|
||||
|
@ -2695,7 +2707,10 @@ void SleighCompile::attachNames(vector<SleighSymbol *> *symlist,vector<string> *
|
|||
if (sym == (ValueSymbol *)0) continue;
|
||||
PatternValue *patval = sym->getPatternValue();
|
||||
if (patval->maxValue() + 1 != names->size()) {
|
||||
reportError(getCurrentLocation(), "Attach name '" + sym->getName() + "' is wrong size for list");
|
||||
ostringstream msg;
|
||||
msg << "Attach name '" + sym->getName();
|
||||
msg << "' (range 0.." << patval->maxValue() << ") is wrong size for list (of " << names->size() << " entries)";
|
||||
reportError(getCurrentLocation(), msg.str());
|
||||
}
|
||||
symtab.replaceSymbol(sym,new NameSymbol(sym->getName(),patval,*names));
|
||||
}
|
||||
|
@ -2720,7 +2735,10 @@ void SleighCompile::attachVarnodes(vector<SleighSymbol *> *symlist,vector<Sleigh
|
|||
if (sym == (ValueSymbol *)0) continue;
|
||||
PatternValue *patval = sym->getPatternValue();
|
||||
if (patval->maxValue() + 1 != varlist->size()) {
|
||||
reportError(getCurrentLocation(), "Attach varnode '" + sym->getName() + "' is wrong size for list");
|
||||
ostringstream msg;
|
||||
msg << "Attach varnode '" + sym->getName();
|
||||
msg << "' (range 0.." << patval->maxValue() << ") is wrong size for list (of " << varlist->size() << " entries)";
|
||||
reportError(getCurrentLocation(), msg.str());
|
||||
}
|
||||
int4 sz = 0;
|
||||
for(int4 j=0;j<varlist->size();++j) {
|
||||
|
|
|
@ -488,6 +488,8 @@ public class SleighCompile extends SleighBase {
|
|||
}
|
||||
for (int i = 0; i < tables.size(); ++i) {
|
||||
if (tables.get(i).isError()) {
|
||||
reportError(tables.get(i).getLocation(),
|
||||
"Problem in table: '" + tables.get(i).getName());
|
||||
errors += 1;
|
||||
}
|
||||
if (tables.get(i).getPattern() == null) {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
/* ###
|
||||
* IP: GHIDRA
|
||||
* REVIEWED: YES
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,31 +15,29 @@
|
|||
*/
|
||||
package ghidra.pcodeCPort.utils;
|
||||
|
||||
import ghidra.pcodeCPort.context.*;
|
||||
import ghidra.pcodeCPort.context.SleighError;
|
||||
import ghidra.sleigh.grammar.Location;
|
||||
|
||||
public class Utils {
|
||||
|
||||
public static final String endl = System.getProperty( "line.separator" );
|
||||
|
||||
private static long[] uintbmasks = {
|
||||
0, 0xff, 0xffff, 0xffffff, 0xffffffffL, 0xffffffffffL, 0xffffffffffffL,
|
||||
0xffffffffffffffL, 0xffffffffffffffffL
|
||||
};
|
||||
public static final String endl = System.getProperty("line.separator");
|
||||
|
||||
public static long calc_mask( int size ) {
|
||||
return uintbmasks[(size < 8) ? size : 8];
|
||||
private static long[] uintbmasks = { 0, 0xff, 0xffff, 0xffffff, 0xffffffffL, 0xffffffffffL,
|
||||
0xffffffffffffL, 0xffffffffffffffL, 0xffffffffffffffffL };
|
||||
|
||||
public static long calc_mask(int size) {
|
||||
return uintbmasks[(Integer.compareUnsigned(size, 8) < 0) ? size : 8];
|
||||
}
|
||||
|
||||
public static long pcode_right( long val, int sa ) {
|
||||
if ( sa >= 64 ) {
|
||||
public static long pcode_right(long val, int sa) {
|
||||
if (sa >= 64) {
|
||||
return 0;
|
||||
}
|
||||
return val >>> sa;
|
||||
}
|
||||
|
||||
public static long pcode_left( long val, int sa ) {
|
||||
if ( sa >= 64 ) {
|
||||
public static long pcode_left(long val, int sa) {
|
||||
if (sa >= 64) {
|
||||
return 0;
|
||||
}
|
||||
return val << sa;
|
||||
|
@ -61,30 +58,30 @@ public class Utils {
|
|||
// return s;
|
||||
// }
|
||||
|
||||
public static boolean signbit_negative( long val, int size ) { // Return true if signbit is set
|
||||
// (negative)
|
||||
public static boolean signbit_negative(long val, int size) { // Return true if signbit is set
|
||||
// (negative)
|
||||
long mask = 0x80;
|
||||
mask <<= 8 * (size - 1);
|
||||
return ((val & mask) != 0);
|
||||
}
|
||||
|
||||
public static long uintb_negate( long in, int size ) { // Invert bits
|
||||
return ((~in) & calc_mask( size ));
|
||||
public static long uintb_negate(long in, int size) { // Invert bits
|
||||
return ((~in) & calc_mask(size));
|
||||
}
|
||||
|
||||
public static long sign_extend( long in, int sizein, int sizeout )
|
||||
public static long sign_extend(long in, int sizein, int sizeout)
|
||||
|
||||
{
|
||||
int signbit;
|
||||
long mask;
|
||||
|
||||
signbit = sizein * 8 - 1;
|
||||
in &= calc_mask( sizein );
|
||||
if ( sizein >= sizeout ) {
|
||||
in &= calc_mask(sizein);
|
||||
if (sizein >= sizeout) {
|
||||
return in;
|
||||
}
|
||||
if ( (in >>> signbit) != 0 ) {
|
||||
mask = calc_mask( sizeout );
|
||||
if ((in >>> signbit) != 0) {
|
||||
mask = calc_mask(sizeout);
|
||||
long tmp = mask << signbit; // Split shift into two pieces
|
||||
tmp = (tmp << 1) & mask; // In case, everything is shifted out
|
||||
in |= tmp;
|
||||
|
@ -93,22 +90,23 @@ public class Utils {
|
|||
}
|
||||
|
||||
// this used to void and changed the parameter val - can't do it in java
|
||||
public static long zzz_sign_extend( long val, int bit )
|
||||
public static long zzz_sign_extend(long val, int bit)
|
||||
|
||||
{ // Sign extend -val- above -bit-
|
||||
long mask = 0;
|
||||
mask = (~mask) << bit;
|
||||
if ( ((val >>> bit) & 1) != 0 ) {
|
||||
if (((val >>> bit) & 1) != 0) {
|
||||
val |= mask;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
val &= (~mask);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
// this used to void and changed the parameter val - can't do it in java
|
||||
public static long zzz_zero_extend( long val, int bit ) { // Clear all bits in -val- above
|
||||
// -bit-
|
||||
public static long zzz_zero_extend(long val, int bit) { // Clear all bits in -val- above
|
||||
// -bit-
|
||||
long mask = 0;
|
||||
mask = (~mask) << bit;
|
||||
mask <<= 1;
|
||||
|
@ -117,9 +115,9 @@ public class Utils {
|
|||
}
|
||||
|
||||
// this used to void and changed the parameter val - can't do it in java
|
||||
public static long byte_swap( long val, int size ) { // Swap the least sig -size- bytes in val
|
||||
public static long byte_swap(long val, int size) { // Swap the least sig -size- bytes in val
|
||||
long res = 0;
|
||||
while ( size > 0 ) {
|
||||
while (size > 0) {
|
||||
res <<= 8;
|
||||
res |= (val & 0xff);
|
||||
val >>>= 8;
|
||||
|
@ -128,9 +126,9 @@ public class Utils {
|
|||
return res;
|
||||
}
|
||||
|
||||
long byte_swap( int val ) { // Swap the bytes for the whole machine int
|
||||
long byte_swap(int val) { // Swap the bytes for the whole machine int
|
||||
long res = 0;
|
||||
for ( int i = 0; i < 4; ++i ) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
res <<= 8;
|
||||
res |= (val & 0xff);
|
||||
val >>>= 8;
|
||||
|
@ -138,21 +136,21 @@ public class Utils {
|
|||
return res;
|
||||
}
|
||||
|
||||
public static boolean isprint(int c) {
|
||||
if (c >= 32 && c <= 126)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
public static boolean isprint(int c) {
|
||||
if (c >= 32 && c <= 126)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isascii(int c) {
|
||||
if (c >= 0 && c <= 127)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
public static boolean isascii(int c) {
|
||||
if (c >= 0 && c <= 127)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static int leastsigbit_set( long val ) { // Return bit number (0=lsb)
|
||||
public static int leastsigbit_set(long val) { // Return bit number (0=lsb)
|
||||
// of the least signifigant bit set or -1 if none set
|
||||
if ( val == 0 ) {
|
||||
if (val == 0) {
|
||||
return -1;
|
||||
}
|
||||
int res = 0;
|
||||
|
@ -160,18 +158,19 @@ public class Utils {
|
|||
long mask = -1;
|
||||
do {
|
||||
mask >>>= sz;
|
||||
if ( (mask & val) == 0 ) {
|
||||
if ((mask & val) == 0) {
|
||||
res += sz;
|
||||
val >>>= sz;
|
||||
}
|
||||
sz >>= 1;
|
||||
} while ( sz != 0 );
|
||||
}
|
||||
while (sz != 0);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static int mostsigbit_set( long val ) { // Return bit number (0=lsb)
|
||||
public static int mostsigbit_set(long val) { // Return bit number (0=lsb)
|
||||
// of the most signifigant bit set or -1 if none set
|
||||
if ( val == 0 ) {
|
||||
if (val == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -180,103 +179,108 @@ public class Utils {
|
|||
long mask = -1;
|
||||
do {
|
||||
mask <<= sz;
|
||||
if ( (mask & val) == 0 ) {
|
||||
if ((mask & val) == 0) {
|
||||
res -= sz;
|
||||
val <<= sz;
|
||||
}
|
||||
sz >>= 1;
|
||||
} while ( sz != 0 );
|
||||
}
|
||||
while (sz != 0);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static long coveringmask( long val )
|
||||
public static long coveringmask(long val)
|
||||
|
||||
{ // Return smallest number of form 2^n-1, bigger or equal to val
|
||||
long res = val;
|
||||
int sz = 1;
|
||||
while ( sz < 64 ) {
|
||||
while (sz < 64) {
|
||||
res = res | (res >>> sz);
|
||||
sz <<= 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static String paddedHexString( long value, int padLength ) {
|
||||
String decodedString = Long.toString( value, 16 );
|
||||
if ( decodedString.length() >= padLength ) {
|
||||
public static String paddedHexString(long value, int padLength) {
|
||||
String decodedString = Long.toString(value, 16);
|
||||
if (decodedString.length() >= padLength) {
|
||||
return decodedString;
|
||||
}
|
||||
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
int missingLength = padLength - decodedString.length();
|
||||
for ( int i = 0; i < missingLength; i++ ) {
|
||||
buffer.append( "0" );
|
||||
for (int i = 0; i < missingLength; i++) {
|
||||
buffer.append("0");
|
||||
}
|
||||
buffer.append( decodedString );
|
||||
buffer.append(decodedString);
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static int unsignedCompare( long v1, long v2 ) {
|
||||
if ( v1 == v2 ) {
|
||||
public static int unsignedCompare(long v1, long v2) {
|
||||
if (v1 == v2) {
|
||||
return 0;
|
||||
}
|
||||
if ( v1 >= 0 && v2 >= 0 ) {
|
||||
if (v1 >= 0 && v2 >= 0) {
|
||||
return v1 < v2 ? -1 : 1;
|
||||
}
|
||||
if ( v1 < 0 && v2 < 0 ) {
|
||||
if (v1 < 0 && v2 < 0) {
|
||||
return v1 < v2 ? -1 : 1;
|
||||
}
|
||||
if ( v1 < 0 ) {
|
||||
if (v1 < 0) {
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public static int unsignedCompare( int v1, int v2 ) {
|
||||
if ( v1 == v2 ) {
|
||||
|
||||
public static int unsignedCompare(int v1, int v2) {
|
||||
if (v1 == v2) {
|
||||
return 0;
|
||||
}
|
||||
if ( v1 >= 0 && v2 >= 0 ) {
|
||||
if (v1 >= 0 && v2 >= 0) {
|
||||
return v1 < v2 ? -1 : 1;
|
||||
}
|
||||
if ( v1 < 0 && v2 < 0 ) {
|
||||
if (v1 < 0 && v2 < 0) {
|
||||
return v1 < v2 ? -1 : 1;
|
||||
}
|
||||
if ( v1 < 0 ) {
|
||||
if (v1 < 0) {
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public static void calc_maskword( Location location, int sbit, int ebit, MutableInt num, MutableInt shift, MutableInt mask ) {
|
||||
num.set(unsignedDivide(sbit, 8 * 4));
|
||||
if(num.get() != unsignedDivide(ebit, 8 * 4)) {
|
||||
throw new SleighError( "Context field not contained within one machine int", location );
|
||||
}
|
||||
sbit -= (int) (unsignedInt(num.get()) * 8 * 4);
|
||||
ebit -= (int) (unsignedInt(num.get()) * 8 * 4);
|
||||
|
||||
shift.set(8 * 4 - ebit - 1);
|
||||
int m = (-1) >>> (sbit + shift.get());
|
||||
m <<= shift.get();
|
||||
mask.set(m);
|
||||
}
|
||||
public static int bytesToInt( byte[] bytes, boolean bigEndian ) {
|
||||
|
||||
public static void calc_maskword(Location location, int sbit, int ebit, MutableInt num,
|
||||
MutableInt shift, MutableInt mask) {
|
||||
num.set(unsignedDivide(sbit, 8 * 4));
|
||||
if (num.get() != unsignedDivide(ebit, 8 * 4)) {
|
||||
throw new SleighError("Context field not contained within one machine int", location);
|
||||
}
|
||||
sbit -= (int) (unsignedInt(num.get()) * 8 * 4);
|
||||
ebit -= (int) (unsignedInt(num.get()) * 8 * 4);
|
||||
|
||||
shift.set(8 * 4 - ebit - 1);
|
||||
int m = (-1) >>> (sbit + shift.get());
|
||||
m <<= shift.get();
|
||||
mask.set(m);
|
||||
}
|
||||
|
||||
public static int bytesToInt(byte[] bytes, boolean bigEndian) {
|
||||
int result = 0;
|
||||
if (bigEndian) {
|
||||
for(int i=0;i<4;i++) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
result <<= 8;
|
||||
result |= (bytes[i] & 0xFF);
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(int i=3;i>=0;i--) {
|
||||
for (int i = 3; i >= 0; i--) {
|
||||
result <<= 8;
|
||||
result |= (bytes[i] & 0xFF);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static long shiftLeft(long a, long b) {
|
||||
b &= 0xff;
|
||||
if (b >= 64) {
|
||||
|
@ -284,6 +288,7 @@ public class Utils {
|
|||
}
|
||||
return a << b;
|
||||
}
|
||||
|
||||
public static long ashiftRight(long a, long b) {
|
||||
b &= 0xff;
|
||||
if (b >= 64) {
|
||||
|
@ -291,6 +296,7 @@ public class Utils {
|
|||
}
|
||||
return a >> b;
|
||||
}
|
||||
|
||||
public static long lshiftRight(long a, long b) {
|
||||
b &= 0xff;
|
||||
if (b >= 64) {
|
||||
|
@ -298,36 +304,41 @@ public class Utils {
|
|||
}
|
||||
return a >>> b;
|
||||
}
|
||||
|
||||
public static long unsignedInt(int a) {
|
||||
long result = a;
|
||||
if (result < 0) {
|
||||
result += 4294967296L;
|
||||
}
|
||||
return result;
|
||||
long result = a;
|
||||
if (result < 0) {
|
||||
result += 4294967296L;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public static int unsignedDivide(int a, int b) {
|
||||
long la = unsignedInt(a);
|
||||
long lb = unsignedInt(b);
|
||||
long result = la/lb;
|
||||
return (int) result;
|
||||
}
|
||||
public static int unsignedModulo(int a, int b) {
|
||||
long la = unsignedInt(a);
|
||||
long lb = unsignedInt(b);
|
||||
long result = la%lb;
|
||||
return (int) result;
|
||||
}
|
||||
|
||||
public static int unsignedDivide(int a, int b) {
|
||||
long la = unsignedInt(a);
|
||||
long lb = unsignedInt(b);
|
||||
long result = la / lb;
|
||||
return (int) result;
|
||||
}
|
||||
|
||||
public static int unsignedModulo(int a, int b) {
|
||||
long la = unsignedInt(a);
|
||||
long lb = unsignedInt(b);
|
||||
long result = la % lb;
|
||||
return (int) result;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(unsignedDivide(-8, 32));
|
||||
System.out.println(unsignedModulo(-8, 32));
|
||||
System.out.println(unsignedDivide(-8, 32));
|
||||
System.out.println(unsignedModulo(-8, 32));
|
||||
}
|
||||
|
||||
public static String toUnsignedIntHex(int n) {
|
||||
return Long.toHexString(unsignedInt(n));
|
||||
return Long.toHexString(unsignedInt(n));
|
||||
}
|
||||
|
||||
public static long bytesToLong(byte[] byteBuf) {
|
||||
long value = 0;
|
||||
for(int i=0;i<8;i++) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
value = value << 8 | byteBuf[i];
|
||||
}
|
||||
return value;
|
||||
|
|
Loading…
Reference in a new issue