GP-901 added RegisterBuilder tests

This commit is contained in:
ghidra1 2021-04-28 13:16:27 -04:00
parent 8f9b067384
commit d46eb8e996
2 changed files with 88 additions and 59 deletions

View File

@ -53,13 +53,12 @@ public class CreateRelocationBasedOperandReferences extends GhidraScript {
Iterator<Relocation> relocations = relocationTable.getRelocations();
monitor.initialize(relocationTable.getSize());
int progress = 0;
int refCount = 0;
while (relocations.hasNext()) {
monitor.checkCanceled();
Relocation r = relocations.next();
monitor.setProgress(progress++);
monitor.incrementProgress(1);
Instruction instr = listing.getInstructionAt(r.getAddress());
if (instr == null) {
@ -137,11 +136,13 @@ public class CreateRelocationBasedOperandReferences extends GhidraScript {
}
if (obj instanceof Scalar) {
if (s != null) {
// more than one scalar found
return null;
}
s = (Scalar) obj;
}
else {
// non-scalar found
return null;
}
}

View File

@ -15,9 +15,11 @@
*/
package ghidra.program.model.lang;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;
import org.junit.*;
import java.util.Iterator;
import org.junit.Test;
import generic.test.AbstractGenericTest;
import ghidra.program.model.address.Address;
@ -25,8 +27,6 @@ import ghidra.program.model.address.AddressSpace;
public class RegisterManagerTest extends AbstractGenericTest {
private RegisterManager registerManager;
public RegisterManagerTest() {
super();
@ -36,24 +36,6 @@ public class RegisterManagerTest extends AbstractGenericTest {
return AddressSpace.DEFAULT_REGISTER_SPACE.getAddress(offset);
}
@Before
public void setUp() throws Exception {
RegisterBuilder builder = new RegisterBuilder();
builder.addRegister("L_0_8", "", addr(0), 8, false, 0);
builder.addRegister("L_0_4", "", addr(0), 4, false, 0);
builder.addRegister("L_4_4", "", addr(4), 4, false, 0);
builder.addRegister("L_0_2", "", addr(0), 2, false, 0);
builder.addRegister("L_2_2", "", addr(2), 2, false, 0);
builder.addRegister("B_10_8", "", addr(0x10), 8, true, 0);
builder.addRegister("B_10_4", "", addr(0x10), 4, true, 0);
builder.addRegister("B_14_4", "", addr(0x14), 4, true, 0);
builder.addRegister("B_16_2", "", addr(0x16), 2, true, 0);
builder.addRegister("B_12_2", "", addr(0x12), 2, true, 0);
registerManager = builder.getRegisterManager();
}
@Test
public void testLittle() {
RegisterBuilder builder = new RegisterBuilder();
@ -63,17 +45,17 @@ public class RegisterManagerTest extends AbstractGenericTest {
builder.addRegister("L_0_2", "", addr(0), 2, false, 0);
builder.addRegister("L_2_2", "", addr(2), 2, false, 0);
registerManager = builder.getRegisterManager();
RegisterManager registerManager = builder.getRegisterManager();
Assert.assertEquals("L_0_8", registerManager.getRegister(addr(0), 8).getName());
Assert.assertEquals("L_0_8", registerManager.getRegister(addr(0), 7).getName());
Assert.assertEquals("L_0_8", registerManager.getRegister(addr(0), 6).getName());
Assert.assertEquals("L_0_8", registerManager.getRegister(addr(0), 5).getName());
Assert.assertEquals("L_0_4", registerManager.getRegister(addr(0), 4).getName());
Assert.assertEquals("L_0_4", registerManager.getRegister(addr(0), 3).getName());
Assert.assertEquals("L_0_2", registerManager.getRegister(addr(0), 2).getName());
Assert.assertEquals("L_0_2", registerManager.getRegister(addr(0), 1).getName());
Assert.assertEquals("L_0_8", registerManager.getRegister(addr(0), 0).getName());
assertEquals("L_0_8", registerManager.getRegister(addr(0), 8).getName());
assertEquals("L_0_8", registerManager.getRegister(addr(0), 7).getName());
assertEquals("L_0_8", registerManager.getRegister(addr(0), 6).getName());
assertEquals("L_0_8", registerManager.getRegister(addr(0), 5).getName());
assertEquals("L_0_4", registerManager.getRegister(addr(0), 4).getName());
assertEquals("L_0_4", registerManager.getRegister(addr(0), 3).getName());
assertEquals("L_0_2", registerManager.getRegister(addr(0), 2).getName());
assertEquals("L_0_2", registerManager.getRegister(addr(0), 1).getName());
assertEquals("L_0_8", registerManager.getRegister(addr(0), 0).getName());
assertNull(registerManager.getRegister(addr(1), 8));
assertNull(registerManager.getRegister(addr(1), 7));
@ -91,9 +73,9 @@ public class RegisterManagerTest extends AbstractGenericTest {
assertNull(registerManager.getRegister(addr(2), 5));
assertNull(registerManager.getRegister(addr(2), 4));
assertNull(registerManager.getRegister(addr(2), 3));
Assert.assertEquals("L_2_2", registerManager.getRegister(addr(2), 2).getName());
Assert.assertEquals("L_2_2", registerManager.getRegister(addr(2), 1).getName());
Assert.assertEquals("L_2_2", registerManager.getRegister(addr(2), 0).getName());
assertEquals("L_2_2", registerManager.getRegister(addr(2), 2).getName());
assertEquals("L_2_2", registerManager.getRegister(addr(2), 1).getName());
assertEquals("L_2_2", registerManager.getRegister(addr(2), 0).getName());
assertNull(registerManager.getRegister(addr(3), 8));
assertNull(registerManager.getRegister(addr(3), 7));
@ -109,11 +91,11 @@ public class RegisterManagerTest extends AbstractGenericTest {
assertNull(registerManager.getRegister(addr(4), 7));
assertNull(registerManager.getRegister(addr(4), 6));
assertNull(registerManager.getRegister(addr(4), 5));
Assert.assertEquals("L_4_4", registerManager.getRegister(addr(4), 4).getName());
Assert.assertEquals("L_4_4", registerManager.getRegister(addr(4), 3).getName());
Assert.assertEquals("L_4_4", registerManager.getRegister(addr(4), 2).getName());
Assert.assertEquals("L_4_4", registerManager.getRegister(addr(4), 1).getName());
Assert.assertEquals("L_4_4", registerManager.getRegister(addr(4), 0).getName());
assertEquals("L_4_4", registerManager.getRegister(addr(4), 4).getName());
assertEquals("L_4_4", registerManager.getRegister(addr(4), 3).getName());
assertEquals("L_4_4", registerManager.getRegister(addr(4), 2).getName());
assertEquals("L_4_4", registerManager.getRegister(addr(4), 1).getName());
assertEquals("L_4_4", registerManager.getRegister(addr(4), 0).getName());
}
@ -127,64 +109,64 @@ public class RegisterManagerTest extends AbstractGenericTest {
builder.addRegister("B_6_2", "", addr(6), 2, true, 0);
builder.addRegister("B_2_2", "", addr(2), 2, true, 0);
registerManager = builder.getRegisterManager();
RegisterManager registerManager = builder.getRegisterManager();
Assert.assertEquals("B_0_8", registerManager.getRegister(addr(0), 8).getName());
assertEquals("B_0_8", registerManager.getRegister(addr(0), 8).getName());
assertNull(registerManager.getRegister(addr(0), 7));
assertNull(registerManager.getRegister(addr(0), 6));
assertNull(registerManager.getRegister(addr(0), 5));
Assert.assertEquals("B_0_4", registerManager.getRegister(addr(0), 4).getName());
assertEquals("B_0_4", registerManager.getRegister(addr(0), 4).getName());
assertNull(registerManager.getRegister(addr(0), 3));
assertNull(registerManager.getRegister(addr(0), 2));
assertNull(registerManager.getRegister(addr(0), 1));
Assert.assertEquals("B_0_8", registerManager.getRegister(addr(0), 0).getName());
assertEquals("B_0_8", registerManager.getRegister(addr(0), 0).getName());
assertNull(registerManager.getRegister(addr(1), 8));
Assert.assertEquals("B_0_8", registerManager.getRegister(addr(1), 7).getName());
assertEquals("B_0_8", registerManager.getRegister(addr(1), 7).getName());
assertNull(registerManager.getRegister(addr(1), 6));
assertNull(registerManager.getRegister(addr(1), 5));
assertNull(registerManager.getRegister(addr(1), 4));
Assert.assertEquals("B_0_4", registerManager.getRegister(addr(1), 3).getName());
assertEquals("B_0_4", registerManager.getRegister(addr(1), 3).getName());
assertNull(registerManager.getRegister(addr(1), 2));
assertNull(registerManager.getRegister(addr(1), 1));
assertNull(registerManager.getRegister(addr(1), 0));
assertNull(registerManager.getRegister(addr(2), 8));
assertNull(registerManager.getRegister(addr(2), 7));
Assert.assertEquals("B_0_8", registerManager.getRegister(addr(2), 6).getName());
assertEquals("B_0_8", registerManager.getRegister(addr(2), 6).getName());
assertNull(registerManager.getRegister(addr(2), 5));
assertNull(registerManager.getRegister(addr(2), 4));
assertNull(registerManager.getRegister(addr(2), 3));
Assert.assertEquals("B_2_2", registerManager.getRegister(addr(2), 2).getName());
assertEquals("B_2_2", registerManager.getRegister(addr(2), 2).getName());
assertNull(registerManager.getRegister(addr(2), 1));
Assert.assertEquals("B_2_2", registerManager.getRegister(addr(2), 0).getName());
assertEquals("B_2_2", registerManager.getRegister(addr(2), 0).getName());
assertNull(registerManager.getRegister(addr(3), 8));
assertNull(registerManager.getRegister(addr(3), 7));
assertNull(registerManager.getRegister(addr(3), 6));
Assert.assertEquals("B_0_8", registerManager.getRegister(addr(3), 5).getName());
assertEquals("B_0_8", registerManager.getRegister(addr(3), 5).getName());
assertNull(registerManager.getRegister(addr(3), 4));
assertNull(registerManager.getRegister(addr(3), 3));
assertNull(registerManager.getRegister(addr(3), 2));
Assert.assertEquals("B_2_2", registerManager.getRegister(addr(3), 1).getName());
assertEquals("B_2_2", registerManager.getRegister(addr(3), 1).getName());
assertNull(registerManager.getRegister(addr(3), 0));
assertNull(registerManager.getRegister(addr(4), 8));
assertNull(registerManager.getRegister(addr(4), 7));
assertNull(registerManager.getRegister(addr(4), 6));
assertNull(registerManager.getRegister(addr(4), 5));
Assert.assertEquals("B_4_4", registerManager.getRegister(addr(4), 4).getName());
assertEquals("B_4_4", registerManager.getRegister(addr(4), 4).getName());
assertNull(registerManager.getRegister(addr(4), 3));
assertNull(registerManager.getRegister(addr(4), 2));
assertNull(registerManager.getRegister(addr(4), 1));
Assert.assertEquals("B_4_4", registerManager.getRegister(addr(4), 0).getName());
assertEquals("B_4_4", registerManager.getRegister(addr(4), 0).getName());
assertNull(registerManager.getRegister(addr(5), 8));
assertNull(registerManager.getRegister(addr(5), 7));
assertNull(registerManager.getRegister(addr(5), 6));
assertNull(registerManager.getRegister(addr(5), 5));
assertNull(registerManager.getRegister(addr(5), 4));
Assert.assertEquals("B_4_4", registerManager.getRegister(addr(5), 3).getName());
assertEquals("B_4_4", registerManager.getRegister(addr(5), 3).getName());
assertNull(registerManager.getRegister(addr(5), 2));
assertNull(registerManager.getRegister(addr(5), 1));
assertNull(registerManager.getRegister(addr(5), 0));
@ -195,9 +177,9 @@ public class RegisterManagerTest extends AbstractGenericTest {
assertNull(registerManager.getRegister(addr(6), 5));
assertNull(registerManager.getRegister(addr(6), 4));
assertNull(registerManager.getRegister(addr(6), 3));
Assert.assertEquals("B_6_2", registerManager.getRegister(addr(6), 2).getName());
assertEquals("B_6_2", registerManager.getRegister(addr(6), 2).getName());
assertNull(registerManager.getRegister(addr(6), 1));
Assert.assertEquals("B_6_2", registerManager.getRegister(addr(6), 0).getName());
assertEquals("B_6_2", registerManager.getRegister(addr(6), 0).getName());
assertNull(registerManager.getRegister(addr(7), 8));
assertNull(registerManager.getRegister(addr(7), 7));
@ -206,9 +188,55 @@ public class RegisterManagerTest extends AbstractGenericTest {
assertNull(registerManager.getRegister(addr(7), 4));
assertNull(registerManager.getRegister(addr(7), 3));
assertNull(registerManager.getRegister(addr(7), 2));
Assert.assertEquals("B_6_2", registerManager.getRegister(addr(7), 1).getName());
assertEquals("B_6_2", registerManager.getRegister(addr(7), 1).getName());
assertNull(registerManager.getRegister(addr(7), 0));
}
@Test
public void testRenameAndAlias() {
RegisterBuilder builder = new RegisterBuilder();
builder.addRegister("A", "", addr(0), 8, false, 0);
builder.addRegister("B", "", addr(0), 4, false, 0);
builder.addRegister("C", "", addr(4), 4, false, 0);
builder.addRegister("D", "", addr(0), 2, false, 0);
builder.addRegister("E", "", addr(2), 2, false, 0);
builder.renameRegister("A", "L_0_8");
builder.renameRegister("B", "L_0_4");
builder.renameRegister("C", "L_4_4");
builder.renameRegister("D", "L_0_2");
builder.renameRegister("E", "L_2_2");
builder.addAlias("L_0_8", "L08");
builder.addAlias("L_0_4", "L04");
builder.addAlias("L_4_4", "L44");
builder.addAlias("L_0_2", "L02");
builder.addAlias("L_2_2", "L22");
RegisterManager registerManager = builder.getRegisterManager();
verifyRegisterNames(registerManager, "L_0_8", "L08");
verifyRegisterNames(registerManager, "L_0_4", "L04");
verifyRegisterNames(registerManager, "L_4_4", "L44");
verifyRegisterNames(registerManager, "L_0_2", "L02");
verifyRegisterNames(registerManager, "L_2_2", "L22");
}
private void verifyRegisterNames(RegisterManager registerManager, String name, String alias) {
Register r = registerManager.getRegister(name);
assertNotNull(r);
assertEquals(name, r.getName());
Iterator<String> iterator = r.getAliases().iterator();
assertEquals(alias, iterator.next());
assertFalse(iterator.hasNext());
// same register instance should be provided for all name forms
assertTrue(r == registerManager.getRegister(name.toLowerCase()));
assertTrue(r == registerManager.getRegister(alias));
assertTrue(r == registerManager.getRegister(alias.toLowerCase()));
}
}