Files

73 lines
2.6 KiB
Java

// Assert that bit-address pointer constants have been converted to references.
// Serves as the failing test for FixPtrConstants.java.
//@category VT1200
import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.symbol.Reference;
public class AssertPtrFix extends GhidraScript {
// Known constant: 0x3F225CD8 at ROM+0x4A5E -> 'Connection request from user %s on node %s.'
private static final long CONST_OFF = 0x4A5EL;
private static final long CONST_VAL = 0x3F225CD8L;
private static final long TARGET_OFF = 0x4B9BL;
private int failures = 0;
private void check(boolean ok, String msg) {
println((ok ? "PASS: " : "FAIL: ") + msg);
if (!ok) {
failures++;
}
}
@Override
public void run() throws Exception {
MemoryBlock rom = null;
long best = -1;
for (MemoryBlock b : currentProgram.getMemory().getBlocks()) {
if (b.isInitialized()) {
long len = b.getEnd().getOffset() - b.getStart().getOffset();
if (len > best) {
best = len;
rom = b;
}
}
}
if (rom == null) {
throw new IllegalStateException("no initialized ROM block");
}
long base = rom.getStart().getOffset();
println(String.format("ROM block base: 0x%08X", base));
Address from = currentProgram.getAddressFactory().getDefaultAddressSpace()
.getAddress(base + CONST_OFF);
Address to = currentProgram.getAddressFactory().getDefaultAddressSpace()
.getAddress(base + TARGET_OFF);
byte[] raw = new byte[4];
rom.getBytes(from, raw);
long value = (raw[0] & 0xFFL) | ((raw[1] & 0xFFL) << 8)
| ((raw[2] & 0xFFL) << 16) | ((raw[3] & 0xFFL) << 24);
check(value == CONST_VAL,
String.format("constant at ROM+0x%X is 0x%08X (got 0x%08X)",
CONST_OFF, CONST_VAL, value));
boolean found = false;
for (Reference r : currentProgram.getReferenceManager().getReferencesFrom(from)) {
if (r.getToAddress().equals(to)) {
found = true;
break;
}
}
check(found, String.format("reference ROM+0x%X -> ROM+0x%X exists", CONST_OFF, TARGET_OFF));
if (failures > 0) {
throw new AssertionError(failures + " assertion(s) failed");
}
println("ASSERT: all pointer-fix checks passed");
}
}