Skip to content

Commit

Permalink
ghidra: move null type check to label method
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarak committed Oct 24, 2024
1 parent 3fef34f commit b8629f9
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions scripts/ghidra/PatchestryDecompileFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ private static String label(PcodeOp op) throws Exception {
}

private String label(DataType type) throws Exception {
// In type is null, assign VoidDataType in all cases.
// We assume it as void type.
if (type == null) {
type = VoidDataType.dataType;
}
String name = type.getName();
CategoryPath category = type.getCategoryPath();
String concat_type = category.toString() + name + Integer.toString(type.getLength());
Expand Down Expand Up @@ -209,41 +214,22 @@ private void serializePointerType(Pointer ptr) throws Exception {
name("name").value(ptr.getDisplayName());
name("kind").value("pointer");
name("size").value(ptr.getLength());
DataType elem_type = ptr.getDataType();
// Pointer element type could be null; assign void type to
// element in such case
if (elem_type == null) {
elem_type = VoidDataType.dataType;
}

name("element_type").value(label(elem_type));
name("element_type").value(label(ptr.getDataType()));
}

private void serializeTypedefType(TypeDef typedef) throws Exception {
name("name").value(typedef.getDisplayName());
name("kind").value("typedef");
name("size").value(typedef.getLength());

DataType base_type = typedef.getBaseDataType();
// If base type is null, raise an exception.
// TODO(kumarak): if see cases where base type is null, handle it
// by assigning default `void` type.
if (base_type == null) {
throw new Exception("Typedef base type is null: " + typedef.toString());
}

name("base_type").value(label(base_type));
name("base_type").value(label(typedef.getBaseDataType()));
}

private void serializeArrayType(Array arr) throws Exception {
name("name").value(arr.getDisplayName());
name("kind").value("array");
name("size").value(arr.getLength());
name("num_elements").value(arr.getNumElements());
DataType elem_type = arr.getDataType();
if (elem_type != null) {
name("element_type").value(label(elem_type));
}
name("element_type").value(label(arr.getDataType()));
}

private void serializeBuiltinType(DataType data_type, String kind) throws Exception {
Expand All @@ -266,7 +252,7 @@ private void serializeCompositeType(Composite data_type, String kind) throws Exc
name("offset").value(dtc.getOffset());

if (dtc.getFieldName() != null) {
name("name").value(dtc.getFieldName().replaceAll(" ", ""));
name("name").value(dtc.getFieldName());
}
endObject();
}
Expand Down

0 comments on commit b8629f9

Please sign in to comment.