Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6973 - Variable annotation address handling update

dragonmacher99a75d13ba17
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableAnnotatedStringHandler.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableAnnotatedStringHandler.java
index 53c075754d..d33b8631cf 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableAnnotatedStringHandler.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableAnnotatedStringHandler.java
@@ -15,6 +15,7 @@
*/
package ghidra.app.util.viewer.field;
+import java.awt.FontMetrics;
import java.util.Arrays;
import java.util.stream.Collectors;
@@ -92,7 +93,7 @@ public class VariableAnnotatedStringHandler implements AnnotatedStringHandler {
String entry = text[2];
Function function = getFunctionAt(program, entry);
if (function == null) {
- throw new AnnotationException("Could not find function \"" + entry + "\"");
+ throw new AnnotationException("Could not find function '%s'".formatted(entry));
}
String address = text[1];
@@ -100,19 +101,18 @@ public class VariableAnnotatedStringHandler implements AnnotatedStringHandler {
if (var == null) {
String functionName = function.getName();
throw new AnnotationException(
- "Could not find variable at address %s in function %s".formatted(functionName,
- address));
+ "Could not find variable by address or name '%s' in function '%s'".formatted(
+ address, functionName));
}
return new AttributedString(var.getName(), prototypeString.getColor(0),
prototypeString.getFontMetrics(0), true, prototypeString.getColor(0));
}
- private AttributedString createPlaceholderString(AttributedString prototypeString,
- String[] text) {
+ private AttributedString createPlaceholderString(AttributedString prototype, String[] text) {
String joined = Arrays.stream(text).collect(Collectors.joining(" "));
- return new AttributedString(joined, Palette.LIGHT_GRAY,
- prototypeString.getFontMetrics(0));
+ FontMetrics fm = prototype.getFontMetrics(0);
+ return new AttributedString(joined, Palette.LIGHT_GRAY, fm);
}
private Function getFunction(Program program, String value) {
@@ -176,7 +176,11 @@ public class VariableAnnotatedStringHandler implements AnnotatedStringHandler {
VariableMatcher matcher = createVariableMatcher(program, value);
Variable var = findVariable(function, matcher);
if (var == null) {
- return null;
+ return new String[] {
+ "variable",
+ value, // leave the variable text untouched
+ function.getEntryPoint().toString()
+ };
}
return new String[] {
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/viewer/field/AnnotationTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/viewer/field/AnnotationTest.java
index 89619bed5f..4acfa79521 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/viewer/field/AnnotationTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/viewer/field/AnnotationTest.java
@@ -86,11 +86,15 @@ public class AnnotationTest extends AbstractGhidraHeadedIntegrationTest {
builder.createLabel("1001018", "mySym{0}"); // symbol with braces
builder.createLabel("1001022", "mySym\\{0\\}"); // symbol with braces escaped
- Parameter p = new ParameterImpl("deadbeef", UnsignedIntegerDataType.dataType, builder.getProgram());
+ // create a variable name that can also be interpreted as an address
+ String paramName = "deadbeef";
+ Parameter p =
+ new ParameterImpl(paramName, UnsignedIntegerDataType.dataType, builder.getProgram());
Function myFunction =
builder.createEmptyFunction("MyFunction", "1002000", 0x10, VoidDataType.dataType, p);
builder.createLocalVariable(myFunction, "myVariable", UnsignedIntegerDataType.dataType, 10);
- builder.createLocalVariable(myFunction, "ram:deadbeef", UnsignedIntegerDataType.dataType, 14);
+ builder.createLocalVariable(myFunction, "ram:" + paramName,
+ UnsignedIntegerDataType.dataType, 14);
return builder.getProgram();
}
@@ -743,14 +747,14 @@ public class AnnotationTest extends AbstractGhidraHeadedIntegrationTest {
}
@Test
- public void testVariableAnnotation_BasicModify() {
+ public void testVariableAnnotation() {
String rawComment = "{@variable myVariable MyFunction}";
String display = CommentUtils.fixupAnnotations(rawComment, program, Address.NO_ADDRESS);
assertEquals("{@variable Stack[0xa] 01002000}", display);
}
@Test
- public void testVariableAnnotation_BasicModify_NoFunction() {
+ public void testVariableAnnotation_NoFunction() {
String rawComment = "{@variable myVariable}";
String functionAddress = "01002000";
Address entryPoint = addr(functionAddress);
@@ -759,28 +763,62 @@ public class AnnotationTest extends AbstractGhidraHeadedIntegrationTest {
}
@Test
- public void testVariableAnnotation_BasicModify_ImplicitAddressName() {
- String rawComment = "{@variable deadbeef MyFunction}";
+ public void testVariableAnnotation_BadName() {
+ String rawComment = "{@variable noSuchName MyFunction}";
+ String functionAddress = "01002000";
+ Address entryPoint = addr(functionAddress);
+ String fixed = CommentUtils.fixupAnnotations(rawComment, program, entryPoint);
+ AttributedString prototype = prototype();
+ FieldElement element =
+ CommentUtils.parseTextForAnnotations(fixed, program, prototype, 0);
+ String displayString = element.getText();
+
+ assertContainsString("Could not find variable by address or name 'noSuchName'",
+ displayString);
+ }
+
+ @Test
+ public void testVariableAnnotation_BadName_BadFunction() {
+ String rawComment = "{@variable noSuchName NoSuchFunction}";
+ String functionAddress = "01002000";
+ Address entryPoint = addr(functionAddress);
+ String fixed = CommentUtils.fixupAnnotations(rawComment, program, entryPoint);
+ AttributedString prototype = prototype();
+ FieldElement element =
+ CommentUtils.parseTextForAnnotations(fixed, program, prototype, 0);
+ String displayString = element.getText();
+
+ assertContainsString("Could not find function 'NoSuchFunction'",
+ displayString);
+ }
+
+ @Test
+ public void testVariableAnnotation_HexName_AsName() {
+ // the variable name can also be interpreted as an address
+ String paramName = "deadbeef";
+ String rawComment = "{@variable %s MyFunction}".formatted(paramName);
String display = CommentUtils.fixupAnnotations(rawComment, program, Address.NO_ADDRESS);
assertEquals("{@variable register:1030 01002000}", display);
}
@Test
- public void testVariableAnnotation_BasicModify_ExplicitAddressName() {
- String rawComment = "{@variable ram:deadbeef MyFunction}";
+ public void testVariableAnnotation_HexName_AsAddress() {
+ // the variable address matches an existing param name
+ String paramAddress = "ram:deadbeef";
+ String rawComment = "{@variable %s MyFunction}".formatted(paramAddress);
String display = CommentUtils.fixupAnnotations(rawComment, program, Address.NO_ADDRESS);
assertEquals("{@variable Stack[0xe] 01002000}", display);
}
@Test
- public void testLocalAnnotation_VariableAddress() {
+ public void testVariableAnnotation_StackAddress() {
String rawComment = "{@variable Stack[0xa] MyFunction}";
String display = CommentUtils.fixupAnnotations(rawComment, program, Address.NO_ADDRESS);
assertEquals("{@variable Stack[0xa] 01002000}", display);
}
@Test
- public void testLocalAnnotation_VariableAddress_NoFunction() {
+ public void testVariableAnnotation_StackAddress_NoFunction() {
String rawComment = "{@variable Stack[0xa]}";
String functionAddress = "01002000";
Address entryPoint = addr(functionAddress);
@@ -789,7 +827,7 @@ public class AnnotationTest extends AbstractGhidraHeadedIntegrationTest {
}
@Test
- public void testLocalAnnotation_RegisterAddress() {
+ public void testVariableAnnotation_RegisterAddress() {
String rawComment = "{@variable register:1030 MyFunction}";
String display = CommentUtils.fixupAnnotations(rawComment, program, Address.NO_ADDRESS);
assertEquals("{@variable register:1030 01002000}", display);