Ghidra is a software reverse engineering (SRE) framework
GP-587: Testing that delay slots can be assembled.
Dan669b14a7cfb2
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/Assembler.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/Assembler.javaindex 8038db8620..e0bc082847 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/Assembler.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/Assembler.java@@ -28,6 +28,7 @@ import ghidra.program.model.mem.MemoryAccessException;/*** The primary interface for performing assembly in Ghidra.*+ * <p>* Use the {@link Assemblers} class to obtain a suitable implementation for a given program or* language.*/@@ -35,10 +36,12 @@ public interface Assembler {/*** Assemble a sequence of instructions and place them at the given address.*+ * <p>* This method is only valid if the assembler is bound to a program. An instance may optionally* implement this method without a program binding. In that case, the returned instruction block* will refer to pseudo instructions.*+ * <p>* NOTE: There must be an active transaction on the bound program for this method to succeed.** @param at the location where the resulting instructions should be placed@@ -56,6 +59,7 @@ public interface Assembler {/*** Assemble a line instruction at the given address.*+ * <p>* This method is valid with or without a bound program. Even if bound, the program is not* modified; however, the appropriate context information is taken from the bound program.* Without a program, the language's default context is taken at the given location.@@ -72,6 +76,7 @@ public interface Assembler {/*** Assemble a line instruction at the given address, assuming the given context.*+ * <p>* This method works like {@link #assembleLine(Address, String)} except that it allows you to* override the assumed context at that location.*@@ -88,11 +93,13 @@ public interface Assembler {/*** Parse a line instruction.*+ * <p>* Generally, you should just use {@link #assembleLine(Address, String)}, but if you'd like* access to the parse trees outside of an {@link AssemblySelector}, then this may be an* acceptable option. Most notably, this is an excellent way to obtain suggestions for* auto-completion.*+ * <p>* Each item in the returned collection is either a complete parse tree, or a syntax error* Because all parse paths are attempted, it's possible to get many mixed results. For example,* The input line may be a valid instruction; however, there may be suggestions to continue the@@ -106,10 +113,12 @@ public interface Assembler {/*** Resolve a given parse tree at the given address, assuming the given context*+ * <p>* Each item in the returned collection is either a completely resolved instruction, or a* semantic error. Because all resolutions are attempted, it's possible to get many mixed* results.*+ * <p>* NOTE: The resolved instructions are given as masks and values. Where the mask does not cover,* you can choose any value.*@@ -124,10 +133,12 @@ public interface Assembler {/*** Resolve a given parse tree at the given address.*+ * <p>* Each item in the returned collection is either a completely resolved instruction, or a* semantic error. Because all resolutions are attempted, it's possible to get many mixed* results.*+ * <p>* NOTE: The resolved instructions are given as masks and values. Where the mask does not cover,* you can choose any value.*@@ -140,6 +151,7 @@ public interface Assembler {/*** Assemble a line instruction at the given address.*+ * <p>* This method works like {@link #resolveLine(Address, String, AssemblyPatternBlock)}, except* that it derives the context using {@link #getContextAt(Address)}.*@@ -154,6 +166,7 @@ public interface Assembler {/*** Assemble a line instruction at the given address, assuming the given context.*+ * <p>* This method works like {@link #assembleLine(Address, String, AssemblyPatternBlock)}, except* that it returns all possible resolutions for the parse trees that pass the* {@link AssemblySelector}.@@ -170,6 +183,7 @@ public interface Assembler {/*** Place a resolved (and fully-masked) instruction into the bound program.*+ * <p>* This method is not valid without a program binding. Also, this method must be called during a* program database transaction.*@@ -184,6 +198,7 @@ public interface Assembler {/*** Place an instruction into the bound program.*+ * <p>* This method is not valid without a program binding. Also, this method must be called during a* program database transaction.*@@ -198,6 +213,7 @@ public interface Assembler {/*** Get the context at a given address*+ * <p>* If there is a program binding, this will extract the actual context at the given address.* Otherwise, it will obtain the default context at the given address for the language.*diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/Assemblers.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/Assemblers.javaindex 0780eb9936..d0cb43af4d 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/Assemblers.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/Assemblers.java@@ -28,16 +28,15 @@ import ghidra.program.model.listing.Program;/*** The primary class for obtaining an {@link Assembler} for a Ghidra-supported language.*+ * <p>* The general flow is: First, obtain an assembler for a language or program. Second, call its* {@link Assembler#assemble(Address, String...)} and related methods to perform assembly. More* advanced uses pass a {@link AssemblySelector} to control certain aspects of assembly instruction* selection, and to obtain advanced diagnostics, like detailed errors and code completion.** <pre>- * {@code* Assembler asm = Assemblers.getAssembler(currentProgram);* asm.assemble(currentAddress, "ADD ...");- * }* </pre>*/public final class Assemblers {@@ -45,6 +44,7 @@ public final class Assemblers {/*** Get a builder for the given language, possibly using a cached one.+ ** @param lang the language* @return the builder for that language, if successful*/@@ -64,10 +64,11 @@ public final class Assemblers {/*** Get an assembler for the given program.*- * Provides an assembler suitable for the program's language, and bound to the program. Calls- * to its Assembler#assemble() function will cause modifications to the bound program. If this- * is the first time an assembler for the program's language has been requested, this function- * may take some time to build the assembler.+ * <p>+ * Provides an assembler suitable for the program's language, and bound to the program. Calls to+ * its Assembler#assemble() function will cause modifications to the bound program. If this is+ * the first time an assembler for the program's language has been requested, this function may+ * take some time to build the assembler.** @param selector a method to select a single result from many* @param program the program for which an assembler is requested@@ -81,6 +82,7 @@ public final class Assemblers {/*** Get an assembler for the given language.*+ * <p>* Provides a suitable assembler for the given language. Only calls to its* Assembler#assembleLine() method are valid. If this is the first time a language has been* requested, this function may take some time to build the assembler. Otherwise, it returns adiff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/sleigh/SleighAssembler.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/sleigh/SleighAssembler.javaindex 410117c287..a02cca1e2d 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/sleigh/SleighAssembler.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/sleigh/SleighAssembler.java@@ -111,7 +111,6 @@ public class SleighAssembler implements Assembler {listing.clearCodeUnits(at, end, false);memory.setBytes(at, insbytes);dis.disassemble(at, new AddressSet(at));- List<Instruction> result = new ArrayList<>();return listing.getInstructions(new AddressSet(at, end), true);}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/ProgramTransaction.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/ProgramTransaction.javaindex 866a8e2547..4a4ad41de9 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/ProgramTransaction.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/ProgramTransaction.java@@ -24,12 +24,10 @@ import ghidra.program.model.listing.Program;* This is meant to be used as an idiom in a try-with-resources block:** <pre>- * {@code* try (ProgramTransaction t = ProgramTransaction.open(program, "Demo")) {* program.getMemory().....* t.commit();* }- * }* </pre>** <p>diff --git a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/app/plugin/assembler/sleigh/PublicAPITest.java b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/app/plugin/assembler/sleigh/PublicAPITest.javaindex 6776c32f47..721568c64f 100644--- a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/app/plugin/assembler/sleigh/PublicAPITest.java+++ b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/app/plugin/assembler/sleigh/PublicAPITest.java@@ -15,36 +15,86 @@*/package ghidra.app.plugin.assembler.sleigh;-import org.junit.Before;-import org.junit.Test;+import static org.junit.Assert.assertEquals;+import static org.junit.Assert.assertNotEquals;++import java.util.ArrayList;+import java.util.List;++import org.junit.*;import generic.test.AbstractGenericTest;import ghidra.app.plugin.assembler.*;import ghidra.app.plugin.processors.sleigh.SleighLanguageProvider;+import ghidra.program.database.ProgramDB;+import ghidra.program.database.util.ProgramTransaction;+import ghidra.program.model.address.Address;+import ghidra.program.model.address.AddressOverflowException;import ghidra.program.model.lang.Language;import ghidra.program.model.lang.LanguageID;+import ghidra.program.model.listing.*;+import ghidra.util.exception.CancelledException;+import ghidra.util.task.TaskMonitor;public class PublicAPITest extends AbstractGenericTest {- private Language x86;+ Language x86;+ Language toy;++ Program program;@Beforepublic void setUp() throws Exception {SleighLanguageProvider provider = new SleighLanguageProvider();x86 = provider.getLanguage(new LanguageID("x86:LE:64:default"));+ toy = provider.getLanguage(new LanguageID("Toy:BE:64:default"));+ }++ @After+ public void tearDown() throws Exception {+ if (program != null) {+ program.release(this);+ }}@Testpublic void testADD0() throws AssemblySyntaxException, AssemblySemanticException {+ // Mostly just test that it doesn't crashAssembler asm = Assemblers.getAssembler(x86);byte[] b =asm.assembleLine(x86.getDefaultSpace().getAddress(0x40000000), "ADD byte ptr [RBX],BL");- printArray(b);+ assertNotEquals(0, b.length);+ }++ protected Address addr(long offset) {+ return program.getAddressFactory().getDefaultAddressSpace().getAddress(offset);}- public static void printArray(byte[] arr) {- for (int i = 0; i < arr.length; i++) {- System.out.printf("%02x", arr[i]);+ @Test+ public void testAssembleWithDelaySlot() throws Exception,+ AddressOverflowException, CancelledException {+ program = new ProgramDB("test", toy, toy.getDefaultCompilerSpec(), this);++ InstructionIterator it;+ try (ProgramTransaction tid = ProgramTransaction.open(program, "Test")) {+ program.getMemory()+ .createInitializedBlock(".text", addr(0x00400000), 0x1000, (byte) 0,+ TaskMonitor.DUMMY, false);+ Assembler asm = Assemblers.getAssembler(program);++ it = asm.assemble(addr(0x00400000),+ "brds 0x00400004",+ "add r0, #6");++ tid.commit();}- System.out.println();++ List<Instruction> result = new ArrayList<>();+ while (it.hasNext()) {+ result.add(it.next());+ }++ assertEquals(2, result.size());+ assertEquals("brds", result.get(0).getMnemonicString());+ assertEquals("_add", result.get(1).getMnemonicString());}}