GP-2368_pcodecompile_npe

This commit is contained in:
James 2022-07-26 18:45:40 +00:00
parent 03b42fc6e4
commit 07f87bb190

View file

@ -15,8 +15,7 @@
*/ */
package ghidra.pcodeCPort.slgh_compile; package ghidra.pcodeCPort.slgh_compile;
import java.util.Arrays; import java.util.*;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -171,10 +170,9 @@ public abstract class PcodeCompile {
// Build temporary variable (with zerosize) // Build temporary variable (with zerosize)
public VarnodeTpl buildTemporary(Location location) { public VarnodeTpl buildTemporary(Location location) {
entry("buildTemporary", location); entry("buildTemporary", location);
VarnodeTpl res = VarnodeTpl res = new VarnodeTpl(location, new ConstTpl(getUniqueSpace()),
new VarnodeTpl(location, new ConstTpl(getUniqueSpace()), new ConstTpl( new ConstTpl(ConstTpl.const_type.real, allocateTemp()),
ConstTpl.const_type.real, allocateTemp()), new ConstTpl(ConstTpl.const_type.real, 0));
new ConstTpl(ConstTpl.const_type.real, 0));
res.setUnnamed(true); res.setUnnamed(true);
return res; return res;
} }
@ -197,8 +195,7 @@ public abstract class PcodeCompile {
labsym.setPlaced(); labsym.setPlaced();
VectorSTL<OpTpl> res = new VectorSTL<OpTpl>(); VectorSTL<OpTpl> res = new VectorSTL<OpTpl>();
OpTpl op = new OpTpl(location, OpCode.CPUI_PTRADD); OpTpl op = new OpTpl(location, OpCode.CPUI_PTRADD);
VarnodeTpl idvn = new VarnodeTpl(location, VarnodeTpl idvn = new VarnodeTpl(location, new ConstTpl(getConstantSpace()),
new ConstTpl(getConstantSpace()),
new ConstTpl(ConstTpl.const_type.real, labsym.getIndex()), new ConstTpl(ConstTpl.const_type.real, labsym.getIndex()),
new ConstTpl(ConstTpl.const_type.real, 4)); new ConstTpl(ConstTpl.const_type.real, 4));
op.addInput(idvn); op.addInput(idvn);
@ -239,9 +236,8 @@ public abstract class PcodeCompile {
if (size != 0) { if (size != 0) {
tmpvn.setSize(new ConstTpl(ConstTpl.const_type.real, size)); // Size was explicitly specified tmpvn.setSize(new ConstTpl(ConstTpl.const_type.real, size)); // Size was explicitly specified
} }
sym = sym = new VarnodeSymbol(location, varname, tmpvn.getSpace().getSpace(),
new VarnodeSymbol(location, varname, tmpvn.getSpace().getSpace(), tmpvn.getOffset().getReal(), (int) tmpvn.getSize().getReal());
tmpvn.getOffset().getReal(), (int) tmpvn.getSize().getReal());
addSymbol(sym); addSymbol(sym);
} }
@ -268,9 +264,8 @@ public abstract class PcodeCompile {
// cannot build the VarnodeSymbol with a placeholder constant // cannot build the VarnodeSymbol with a placeholder constant
rhs.setOutput(location, tmpvn); rhs.setOutput(location, tmpvn);
// Create new symbol regardless // Create new symbol regardless
sym = sym = new VarnodeSymbol(location, varname, tmpvn.getSpace().getSpace(),
new VarnodeSymbol(location, varname, tmpvn.getSpace().getSpace(), tmpvn.getOffset().getReal(), (int) tmpvn.getSize().getReal());
tmpvn.getOffset().getReal(), (int) tmpvn.getSize().getReal());
addSymbol(sym); addSymbol(sym);
if ((!usesLocalKey) && enforceLocalKey) { if ((!usesLocalKey) && enforceLocalKey) {
reportError(location, "Must use 'local' keyword to define symbol '" + varname + "'"); reportError(location, "Must use 'local' keyword to define symbol '" + varname + "'");
@ -369,9 +364,8 @@ public abstract class PcodeCompile {
public VectorSTL<OpTpl> createOpConst(Location location, OpCode opc, long val) { public VectorSTL<OpTpl> createOpConst(Location location, OpCode opc, long val) {
entry("createOpConst", location, opc, val); entry("createOpConst", location, opc, val);
VarnodeTpl vn = VarnodeTpl vn = new VarnodeTpl(location, new ConstTpl(getConstantSpace()),
new VarnodeTpl(location, new ConstTpl(getConstantSpace()), new ConstTpl( new ConstTpl(ConstTpl.const_type.real, val), new ConstTpl(ConstTpl.const_type.real, 4));
ConstTpl.const_type.real, val), new ConstTpl(ConstTpl.const_type.real, 4));
VectorSTL<OpTpl> res = new VectorSTL<OpTpl>(); VectorSTL<OpTpl> res = new VectorSTL<OpTpl>();
OpTpl op = new OpTpl(location, opc); OpTpl op = new OpTpl(location, opc);
op.addInput(vn); op.addInput(vn);
@ -384,9 +378,8 @@ public abstract class PcodeCompile {
entry("createLoad", location, qual, ptr); entry("createLoad", location, qual, ptr);
VarnodeTpl outvn = buildTemporary(location); VarnodeTpl outvn = buildTemporary(location);
OpTpl op = new OpTpl(location, OpCode.CPUI_LOAD); OpTpl op = new OpTpl(location, OpCode.CPUI_LOAD);
VarnodeTpl spcvn = VarnodeTpl spcvn = new VarnodeTpl(location, new ConstTpl(getConstantSpace()), qual.getId(),
new VarnodeTpl(location, new ConstTpl(getConstantSpace()), qual.getId(), new ConstTpl( new ConstTpl(ConstTpl.const_type.real, 8));
ConstTpl.const_type.real, 8));
op.addInput(spcvn); op.addInput(spcvn);
op.addInput(ptr.outvn); op.addInput(ptr.outvn);
op.setOutput(outvn); op.setOutput(outvn);
@ -406,9 +399,8 @@ public abstract class PcodeCompile {
res.appendAll(val.ops); res.appendAll(val.ops);
val.ops.clear(); val.ops.clear();
OpTpl op = new OpTpl(location, OpCode.CPUI_STORE); OpTpl op = new OpTpl(location, OpCode.CPUI_STORE);
VarnodeTpl spcvn = VarnodeTpl spcvn = new VarnodeTpl(location, new ConstTpl(getConstantSpace()), qual.getId(),
new VarnodeTpl(location, new ConstTpl(getConstantSpace()), qual.getId(), new ConstTpl( new ConstTpl(ConstTpl.const_type.real, 8));
ConstTpl.const_type.real, 8));
op.addInput(spcvn); op.addInput(spcvn);
op.addInput(ptr.outvn); op.addInput(ptr.outvn);
op.addInput(val.outvn); op.addInput(val.outvn);
@ -434,10 +426,9 @@ public abstract class PcodeCompile {
VectorSTL<ExprTree> param) { VectorSTL<ExprTree> param) {
entry("createUserOpNoOut", sym, param); entry("createUserOpNoOut", sym, param);
OpTpl op = new OpTpl(location, OpCode.CPUI_CALLOTHER); OpTpl op = new OpTpl(location, OpCode.CPUI_CALLOTHER);
VarnodeTpl vn = VarnodeTpl vn = new VarnodeTpl(sym.location, new ConstTpl(getConstantSpace()),
new VarnodeTpl(sym.location, new ConstTpl(getConstantSpace()), new ConstTpl( new ConstTpl(ConstTpl.const_type.real, sym.getIndex()),
ConstTpl.const_type.real, sym.getIndex()), new ConstTpl(ConstTpl.const_type.real, 4));
new ConstTpl(ConstTpl.const_type.real, 4));
op.addInput(vn); op.addInput(vn);
return ExprTree.appendParams(op, param); return ExprTree.appendParams(op, param);
} }
@ -493,9 +484,8 @@ public abstract class PcodeCompile {
// We put in the correct adjustment to offset assuming things are little endian // We put in the correct adjustment to offset assuming things are little endian
// We defer the correct big endian calculation until after the consistency check // We defer the correct big endian calculation until after the consistency check
// because we need to know the subtable export sizes // because we need to know the subtable export sizes
specialoff = specialoff = new ConstTpl(const_type.handle, basevn.getOffset().getHandleIndex(),
new ConstTpl(const_type.handle, basevn.getOffset().getHandleIndex(), v_field.v_offset_plus, byteoffset);
v_field.v_offset_plus, byteoffset);
} }
else { else {
if (basevn.getSize().getType() != const_type.real) { if (basevn.getSize().getType() != const_type.real) {
@ -510,9 +500,8 @@ public abstract class PcodeCompile {
} }
specialoff = new ConstTpl(const_type.real, basevn.getOffset().getReal() + plus); specialoff = new ConstTpl(const_type.real, basevn.getOffset().getReal() + plus);
} }
VarnodeTpl res = VarnodeTpl res = new VarnodeTpl(loc, basevn.getSpace(), specialoff,
new VarnodeTpl(loc, basevn.getSpace(), specialoff, new ConstTpl(const_type.real, new ConstTpl(const_type.real, numbytes));
numbytes));
return res; return res;
} }
@ -521,10 +510,9 @@ public abstract class PcodeCompile {
public void appendOp(Location location, OpCode opc, ExprTree res, long constval, int constsz) { public void appendOp(Location location, OpCode opc, ExprTree res, long constval, int constsz) {
entry("appendOp", location, opc, res, constval, constsz); entry("appendOp", location, opc, res, constval, constsz);
OpTpl op = new OpTpl(location, opc); OpTpl op = new OpTpl(location, opc);
VarnodeTpl constvn = VarnodeTpl constvn = new VarnodeTpl(location, new ConstTpl(getConstantSpace()),
new VarnodeTpl(location, new ConstTpl(getConstantSpace()), new ConstTpl( new ConstTpl(ConstTpl.const_type.real, constval),
ConstTpl.const_type.real, constval), new ConstTpl(ConstTpl.const_type.real, constsz));
new ConstTpl(ConstTpl.const_type.real, constsz));
VarnodeTpl outvn = buildTemporary(location); VarnodeTpl outvn = buildTemporary(location);
op.addInput(res.outvn); op.addInput(res.outvn);
op.addInput(constvn); op.addInput(constvn);
@ -708,15 +696,13 @@ public abstract class PcodeCompile {
if ((var.getOffset().getType() == ConstTpl.const_type.real) && if ((var.getOffset().getType() == ConstTpl.const_type.real) &&
(var.getSpace().getType() == ConstTpl.const_type.spaceid)) { (var.getSpace().getType() == ConstTpl.const_type.spaceid)) {
AddrSpace spc = var.getSpace().getSpace(); AddrSpace spc = var.getSpace().getSpace();
res = res = new VarnodeTpl(var.location, new ConstTpl(getConstantSpace()),
new VarnodeTpl(var.location, new ConstTpl(getConstantSpace()), new ConstTpl( new ConstTpl(ConstTpl.const_type.real, var.getOffset().getReal() >> spc.getScale()),
ConstTpl.const_type.real, var.getOffset().getReal() >> spc.getScale()), new ConstTpl(ConstTpl.const_type.real, size));
new ConstTpl(ConstTpl.const_type.real, size));
} }
else { else {
res = res = new VarnodeTpl(var.location, new ConstTpl(getConstantSpace()), var.getOffset(),
new VarnodeTpl(var.location, new ConstTpl(getConstantSpace()), var.getOffset(), new ConstTpl(ConstTpl.const_type.real, size));
new ConstTpl(ConstTpl.const_type.real, size));
} }
return res; return res;
} }
@ -898,7 +884,9 @@ public abstract class PcodeCompile {
public static void entry(String name, Object... args) { public static void entry(String name, Object... args) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(name).append("("); sb.append(name).append("(");
sb.append(Arrays.stream(args).map(Object::toString).collect(Collectors.joining(", "))); sb.append(Arrays.stream(args)
.map(x -> Objects.toString(x, "null"))
.collect(Collectors.joining(", ")));
sb.append(")"); sb.append(")");
log.trace(sb.toString()); log.trace(sb.toString());