Skip to content

Commit e6e38dd

Browse files
committed
ghidra: fix early handling of exceptions
1 parent a27a9db commit e6e38dd

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

scripts/ghidra/PatchestryDecompileFunctions.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -853,17 +853,22 @@ private Address convertAddressToRamSpace(Address address) throws Exception {
853853
}
854854

855855
try {
856-
// If already in RAM space, just return it
857-
if (address.getAddressSpace().getName().equals("ram")) {
856+
// Note: Function converts address to ramspace only if it belongs to
857+
// constant space; if address space is not constant, return
858+
if (!address.getAddressSpace().isConstantSpace()) {
858859
return address;
859860
}
860861

861862
// Get the numeric offset and create a new address in RAM space
862863
long offset = address.getOffset();
863864
return program.getAddressFactory().getDefaultAddressSpace().getAddress(offset);
864-
} catch (Exception e) {
865-
println(String.format("Failed to convert address %s to RAM space: %s", address, e.getMessage()));
865+
866+
} catch (AddressOutOfBoundsException e) {
867+
println(String.format("Error converting address %s to RAM space: %s",
868+
address, e.getMessage()));
866869
return null;
870+
} catch (Exception e) {
871+
throw new RuntimeException("Failed converting address to RAM space", e);
867872
}
868873
}
869874

@@ -924,19 +929,12 @@ private Data getDataReferencedAsConstant(Varnode node) throws Exception {
924929

925930
// Ghidra sometime fail to resolve references to Data and show it as const.
926931
// Check if it is referencing Data as constant from `ram` addresspace.
927-
try {
928-
// Convert the constant value to a potential RAM address
929-
Address ram_address = convertAddressToRamSpace(node.getAddress());
930-
if (ram_address == null) {
931-
return null;
932-
}
933-
return getDataAt(ram_address);
934-
935-
} catch (AddressOutOfBoundsException e) {
936-
println("Address conversion out of bounds for constant: " + e.getMessage());
932+
// Convert the constant value to a potential RAM address
933+
Address ram_address = convertAddressToRamSpace(node.getAddress());
934+
if (ram_address == null) {
935+
return null;
937936
}
938-
939-
return null;
937+
return getDataAt(ram_address);
940938
}
941939

942940
// Serialize an input or output varnode.

0 commit comments

Comments
 (0)