Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCode: Add edge label information to XML encoding and expose it in Ghidra. #7308

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Ghidra/Features/Decompiler/src/decompile/cpp/block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ AttributeId ATTRIB_DEPTH = AttributeId("depth",76);
AttributeId ATTRIB_END = AttributeId("end",77);
AttributeId ATTRIB_OPCODE = AttributeId("opcode",78);
AttributeId ATTRIB_REV = AttributeId("rev",79);
AttributeId ATTRIB_EDGE_LABEL = AttributeId("edgelabel",151);

ElementId ELEM_BHEAD = ElementId("bhead",102);
ElementId ELEM_BLOCK = ElementId("block",103);
Expand All @@ -36,9 +37,9 @@ void BlockEdge::encode(Encoder &encoder) const

{
encoder.openElement(ELEM_EDGE);
// We are not saving label currently
encoder.writeSignedInteger(ATTRIB_END, point->getIndex()); // Reference to other end of edge
encoder.writeSignedInteger(ATTRIB_REV, reverse_index); // Position within other blocks edgelist
encoder.writeUnsignedInteger(ATTRIB_EDGE_LABEL, label);
encoder.closeElement(ELEM_EDGE);
}

Expand All @@ -49,12 +50,12 @@ void BlockEdge::decode(Decoder &decoder,BlockMap &resolver)

{
uint4 elemId = decoder.openElement(ELEM_EDGE);
label = 0; // Tag does not currently contain info about label
int4 endIndex = decoder.readSignedInteger(ATTRIB_END);
point = resolver.findLevelBlock(endIndex);
if (point == (FlowBlock *)0)
throw LowlevelError("Bad serialized edge in block graph");
reverse_index = decoder.readSignedInteger(ATTRIB_REV);
label = decoder.readUnsignedInteger(ATTRIB_EDGE_LABEL);
decoder.closeElement(elemId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,8 @@ public record AttributeId(String name, int id) {
public static final AttributeId ATTRIB_STORAGE = new AttributeId("storage", 149);
public static final AttributeId ATTRIB_STACKSPILL = new AttributeId("stackspill", 150);

public static final AttributeId ATTRIB_UNKNOWN = new AttributeId("XMLunknown", 151);
// edge
public static final AttributeId ATTRIB_EDGE_LABEL = new AttributeId("edgelabel", 151);

public static final AttributeId ATTRIB_UNKNOWN = new AttributeId("XMLunknown", 152);
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public void encode(Encoder encoder) throws IOException {
encoder.writeSignedInteger(ATTRIB_END, point.getIndex());
// Position within other blocks edgelist
encoder.writeSignedInteger(ATTRIB_REV, reverse_index);
encoder.writeUnsignedInteger(ATTRIB_EDGE_LABEL, label);
encoder.closeElement(ELEM_EDGE);
}

Expand All @@ -170,26 +171,26 @@ public void encode(Encoder encoder) throws IOException {
*/
public void decode(Decoder decoder, BlockMap resolver) throws DecoderException {
int el = decoder.openElement(ELEM_EDGE);
label = 0; // Tag does not currently contain info about label
int endIndex = (int) decoder.readSignedInteger(ATTRIB_END);
point = resolver.findLevelBlock(endIndex);
if (point == null) {
throw new DecoderException("Bad serialized edge in block graph");
}
reverse_index = (int) decoder.readSignedInteger(ATTRIB_REV);
label = (int) decoder.readUnsignedInteger(ATTRIB_EDGE_LABEL);
decoder.closeElement(el);
}

public void decode(Decoder decoder, ArrayList<? extends PcodeBlock> blockList)
throws DecoderException {
int el = decoder.openElement(ELEM_EDGE);
label = 0; // Tag does not currently contain info about label
int endIndex = (int) decoder.readSignedInteger(ATTRIB_END);
point = blockList.get(endIndex);
if (point == null) {
throw new DecoderException("Bad serialized edge in block list");
}
reverse_index = (int) decoder.readSignedInteger(ATTRIB_REV);
label = (int) decoder.readUnsignedInteger(ATTRIB_EDGE_LABEL);
decoder.closeElement(el);
}

Expand Down Expand Up @@ -257,7 +258,7 @@ protected void decodeNextInEdge(Decoder decoder, BlockMap resolver) throws Decod
while (inEdge.point.outofthis.size() <= inEdge.reverse_index) {
inEdge.point.outofthis.add(null);
}
BlockEdge outEdge = new BlockEdge(this, 0, intothis.size() - 1);
BlockEdge outEdge = new BlockEdge(this, inEdge.label, intothis.size() - 1);
inEdge.point.outofthis.set(inEdge.reverse_index, outEdge);
}

Expand All @@ -275,18 +276,26 @@ protected void decodeNextInEdge(Decoder decoder, ArrayList<? extends PcodeBlock>
while (inEdge.point.outofthis.size() <= inEdge.reverse_index) {
inEdge.point.outofthis.add(null);
}
BlockEdge outEdge = new BlockEdge(this, 0, intothis.size() - 1);
BlockEdge outEdge = new BlockEdge(this, inEdge.label, intothis.size() - 1);
inEdge.point.outofthis.set(inEdge.reverse_index, outEdge);
}

public PcodeBlock getIn(int i) {
return intothis.get(i).point;
}

public int getInEdgeLabel(int i) {
return intothis.get(i).label;
}

public PcodeBlock getOut(int i) {
return outofthis.get(i).point;
}

public int getOutEdgeLabel(int i) {
return outofthis.get(i).label;
}

/**
* Get reverse index of the i-th outgoing block. I.e this.getOut(i).getIn(reverse_index) == this
* @param i is the outgoing block to request reverse index from
Expand Down