Ghidra is a software reverse engineering (SRE) framework
Corrected test fallout from Memory changes
ghidra17385544bed22
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/AddBlockModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/AddBlockModel.javaindex fd5ca0f09e..5afe8c88f3 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/AddBlockModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/AddBlockModel.java@@ -360,13 +360,16 @@ class AddBlockModel {}private boolean hasMappedAddressIfNeeded() {- if (blockType == MemoryBlockType.BIT_MAPPED || blockType == MemoryBlockType.BYTE_MAPPED) {- if (baseAddr == null) {- String blockTypeStr =- (blockType == MemoryBlockType.BIT_MAPPED) ? "bit-mapped" : "byte-mapped";- message = "Please enter a source address for the " + blockTypeStr + " block";- return false;- }+ if (blockType != MemoryBlockType.BIT_MAPPED && blockType != MemoryBlockType.BYTE_MAPPED) {+ return true;+ }+ if (baseAddr == null) {+ String blockTypeStr =+ (blockType == MemoryBlockType.BIT_MAPPED) ? "bit-mapped" : "byte-mapped";+ message = "Please enter a source address for the " + blockTypeStr + " block";+ return false;+ }+ if (blockType == MemoryBlockType.BYTE_MAPPED) {if (schemeDestByteCount <= 0 || schemeDestByteCount > Byte.MAX_VALUE ||schemeSrcByteCount <= 0 || schemeSrcByteCount > Byte.MAX_VALUE) {message = "Mapping ratio values must be within range: 1 to 127";diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/MemoryMapModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/MemoryMapModel.javaindex 17acddcd01..f4c5ebb9e2 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/MemoryMapModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/MemoryMapModel.java@@ -267,7 +267,7 @@ class MemoryMapModel extends AbstractSortedTableModel<MemoryBlock> {if (name.equals(block.getName())) {break;}- if (Memory.isValidAddressSpaceName(name)) {+ if (!Memory.isValidAddressSpaceName(name)) {Msg.showError(this, provider.getComponent(), "Invalid Name","Invalid Memory Block Name: " + name);break;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/MemoryBlockStartFieldFactory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/MemoryBlockStartFieldFactory.javaindex a825d0fc0c..7b17a08eff 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/MemoryBlockStartFieldFactory.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/MemoryBlockStartFieldFactory.java@@ -112,10 +112,16 @@ public class MemoryBlockStartFieldFactory extends FieldFactory {}CodeUnit cu = (CodeUnit) proxyObject;+ String[] comments;List<AttributedString> attributedStrings = createBlockStartText(cu);- String[] comments = new String[attributedStrings.size()];- for (int i = 0; i < comments.length; i++) {- comments[i] = attributedStrings.get(i).getText();+ if (attributedStrings == null) {+ comments = new String[0];+ }+ else {+ comments = new String[attributedStrings.size()];+ for (int i = 0; i < comments.length; i++) {+ comments[i] = attributedStrings.get(i).getText();+ }}return new MemoryBlockStartFieldLocation(cu.getProgram(), cu.getMinAddress(), null, row,diff --git a/Ghidra/Features/Base/src/main/java/ghidra/program/database/ProgramBuilder.java b/Ghidra/Features/Base/src/main/java/ghidra/program/database/ProgramBuilder.javaindex acee54c937..bd1b8fc528 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/program/database/ProgramBuilder.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/program/database/ProgramBuilder.java@@ -332,6 +332,7 @@ public class ProgramBuilder {// can't happen}catch (Exception e) {+ e.printStackTrace();throw new RuntimeException("Exception building memory", e);}endTransaction();@@ -419,7 +420,8 @@ public class ProgramBuilder {startTransaction();MemoryBlock block = program.getMemory().getBlock(address);if (block == null) {- createMemory("Block_" + stringAddress, stringAddress, bytes.length);+ createMemory("Block_" + stringAddress.toString().replace(':', '_'), stringAddress,+ bytes.length);}Memory memory = program.getMemory();diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/memory/MemoryMapProvider1Test.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/memory/MemoryMapProvider1Test.javaindex 3f3c4c960d..9026d5f2e3 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/memory/MemoryMapProvider1Test.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/memory/MemoryMapProvider1Test.java@@ -268,36 +268,38 @@ public class MemoryMapProvider1Test extends AbstractGhidraHeadedIntegrationTestassertEquals(".test", model.getValueAt(0, MemoryMapModel.NAME));}- @Test- public void testDuplicateName() throws Exception {- table.addRowSelectionInterval(0, 0);- Rectangle rect = table.getCellRect(0, MemoryMapModel.NAME, true);- clickMouse(table, 1, rect.x, rect.y, 2, 0);- waitForPostedSwingRunnables();-- SwingUtilities.invokeLater(() -> {- int row = 0;- TableCellEditor editor = table.getCellEditor(row, MemoryMapModel.NAME);- Component c = editor.getTableCellEditorComponent(table,- model.getValueAt(row, MemoryMapModel.NAME), true, row, MemoryMapModel.NAME);- JTextField tf = (JTextField) c;-- tf.setText(".data");- editor.stopCellEditing();- });- waitForPostedSwingRunnables();- assertEquals(".text", model.getValueAt(0, MemoryMapModel.NAME));-- final OptionDialog d =- waitForDialogComponent(tool.getToolFrame(), OptionDialog.class, 2000);-- assertNotNull(d);- String msg = findMessage(d.getComponent());- assertNotNull(msg);- assertEquals("Block named .data already exists.", msg);- SwingUtilities.invokeAndWait(() -> d.close());-- }+// Test Eliminated - Memory API allows duplicate names which is a common occurance+// with import formats such as ELF+//+// public void testDuplicateName() throws Exception {+// table.addRowSelectionInterval(0, 0);+// Rectangle rect = table.getCellRect(0, MemoryMapModel.NAME, true);+// clickMouse(table, 1, rect.x, rect.y, 2, 0);+// waitForPostedSwingRunnables();+//+// SwingUtilities.invokeLater(() -> {+// int row = 0;+// TableCellEditor editor = table.getCellEditor(row, MemoryMapModel.NAME);+// Component c = editor.getTableCellEditorComponent(table,+// model.getValueAt(row, MemoryMapModel.NAME), true, row, MemoryMapModel.NAME);+// JTextField tf = (JTextField) c;+//+// tf.setText(".data");+// editor.stopCellEditing();+// });+// waitForPostedSwingRunnables();+// assertEquals(".text", model.getValueAt(0, MemoryMapModel.NAME));+//+// final OptionDialog d =+// waitForDialogComponent(tool.getToolFrame(), OptionDialog.class, 2000);+//+// assertNotNull(d);+// String msg = findMessage(d.getComponent());+// assertNotNull(msg);+// assertEquals("Block named .data already exists.", msg);+// SwingUtilities.invokeAndWait(() -> d.close());+//+// }@Testpublic void testEditComment() throws Exception {diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/GoToPluginTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/GoToPluginTest.javaindex e21b10d24b..0eab8446e6 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/GoToPluginTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/GoToPluginTest.java@@ -243,7 +243,7 @@ public class GoToPluginTest extends AbstractGhidraHeadedIntegrationTest {public void testWildcardInBlock() throws Exception {loadProgram("x86");- MemoryBlock block = createOverlay("Test Overlay", "1002000", 100);+ MemoryBlock block = createOverlay("TestOverlay", "1002000", 100);String name = block.getName();AddLabelCmd cmd = new AddLabelCmd(addr(name + "::1002000"), "Bob", SourceType.USER_DEFINED);@@ -252,7 +252,7 @@ public class GoToPluginTest extends AbstractGhidraHeadedIntegrationTest {// try a wildcard for an address in the new block (this gets us an extra code path tested)setText(name + "::*ob");performOkCallback();- assertEquals(addr("Test Overlay::1002000"), cbPlugin.getCurrentAddress());+ assertEquals(addr("TestOverlay::1002000"), cbPlugin.getCurrentAddress());}@Test@@ -272,7 +272,7 @@ public class GoToPluginTest extends AbstractGhidraHeadedIntegrationTest {// queries an address has multiple matches.//loadProgram("x86");- createOverlay("Test Overlay", "1002000", 100);+ createOverlay("TestOverlay", "1002000", 100);assumeCurrentAddressSpace(false);showDialog();setText("1002000");@@ -289,7 +289,7 @@ public class GoToPluginTest extends AbstractGhidraHeadedIntegrationTest {// addresses is off *and* the current address space has a matching address.//loadProgram("x86");- createOverlay("Test Overlay", "1002000", 100);+ createOverlay("TestOverlay", "1002000", 100);//// Turn off the option to show all addresses when there is a match in our current@@ -311,8 +311,8 @@ public class GoToPluginTest extends AbstractGhidraHeadedIntegrationTest {// option to always show all addresses is off.//loadProgram("x86");- createOverlay("Test Overlay 1", "1002000", 100);- MemoryBlock overlay2Block = createOverlay("Test Overlay 2", "1003000", 100);+ createOverlay("TestOverlay1", "1002000", 100);+ MemoryBlock overlay2Block = createOverlay("TestOverlay2", "1003000", 100);//// Put us in an address space that does not have a match for the query addressdiff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/BitMappedMemoryBlockTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/BitMappedMemoryBlockTest.javaindex 7268baf6cb..d2c7400e51 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/BitMappedMemoryBlockTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/BitMappedMemoryBlockTest.java@@ -95,6 +95,7 @@ public class BitMappedMemoryBlockTest extends AbstractGhidraHeadedIntegrationTesAssert.fail("Should not have gotten a byte");}catch (MemoryAccessException e) {+ // expected}}diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/ByteMappedMemoryBlockTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/ByteMappedMemoryBlockTest.javaindex b0dc85c139..ca06c7d962 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/ByteMappedMemoryBlockTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/ByteMappedMemoryBlockTest.java@@ -351,4 +351,21 @@ public class ByteMappedMemoryBlockTest extends AbstractGhidraHeadedIntegrationTeassertEquals(4, block2.getBytes(addr(0x100), data2));assertTrue(Arrays.equals(new byte[] { 1, 2, -3, -4 }, data2));}++ @Test+ public void testNoUnderlyingMemory() throws Exception {++ MemoryBlock byteMappedBlock = memory.createByteMappedBlock("BYTE_BLOCK", addr(0x1000),+ addr(0x1020), 0x10, new ByteMappingScheme(1, 1), false);++ Address addr = addr(0x1040);+ MemoryBlock newblock = memory.createBlock(byteMappedBlock, "Test", addr, 0x20);+ try {+ newblock.getByte(addr);+ Assert.fail("Should not have gotten a byte");+ }+ catch (MemoryAccessException e) {+ // expected+ }+ }}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.javaindex 9ce03f9dde..0b9baaabf3 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.java@@ -163,7 +163,7 @@ public class MemoryBlockDB implements MemoryBlock {if (oldName.equals(name)) {return;}- memMap.checkBlockName(name);+ memMap.checkBlockName(name, isOverlay());try {if (isOverlay()) {memMap.overlayBlockRenamed(oldName, name);diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryMapDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryMapDB.javaindex f57c32cc68..6523e229ce 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryMapDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryMapDB.java@@ -597,7 +597,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {public MemoryBlock createInitializedBlock(String name, Address start, InputStream is,long length, TaskMonitor monitor, boolean overlay) throws MemoryConflictException,AddressOverflowException, CancelledException, LockException, DuplicateNameException {- checkBlockName(name);+ checkBlockName(name, overlay);lock.acquire();try {checkBlockSize(length, true);@@ -643,7 +643,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {long offset, long length, boolean overlay) throws LockException, DuplicateNameException,MemoryConflictException, AddressOverflowException, IndexOutOfBoundsException {- checkBlockName(name);+ checkBlockName(name, overlay);lock.acquire();try {checkBlockSize(length, true);@@ -696,7 +696,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {boolean overlay) throws MemoryConflictException, AddressOverflowException,LockException, DuplicateNameException {- checkBlockName(name);+ checkBlockName(name, overlay);lock.acquire();try {checkBlockSize(size, false);@@ -732,7 +732,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {long length, boolean overlay) throws MemoryConflictException, AddressOverflowException,LockException, IllegalArgumentException, DuplicateNameException {- checkBlockName(name);+ checkBlockName(name, overlay);lock.acquire();try {checkBlockSize(length, false);@@ -768,7 +768,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {throws MemoryConflictException, AddressOverflowException, LockException,DuplicateNameException {- checkBlockName(name);+ checkBlockName(name, overlay);int mappingScheme = 0; // use for 1:1 mappingif (byteMappingScheme == null) {@@ -810,16 +810,17 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {/*** Check new block name for validity* @param name new block name+ * @param isOverlay true if block is overlay* @throws IllegalArgumentException if invalid block name specified* @throws DuplicateNameException if name conflicts with an address space name*/void checkBlockName(- String name)+ String name, boolean isOverlay)throws IllegalArgumentException, DuplicateNameException {if (!Memory.isValidAddressSpaceName(name)) {throw new IllegalArgumentException("Invalid block name: " + name);}- if (getAddressFactory().getAddressSpace(name) != null) {+ if (isOverlay && getAddressFactory().getAddressSpace(name) != null) {throw new DuplicateNameException("Block name conflicts with existing address space: " + name);}@@ -829,7 +830,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {public MemoryBlock createBlock(MemoryBlock block, String name, Address start, long length)throws MemoryConflictException, AddressOverflowException, LockException,DuplicateNameException {- checkBlockName(name);+ checkBlockName(name, false);lock.acquire();try {checkBlockSize(length, block.isInitialized());@@ -840,7 +841,9 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {int mappingScheme = 0;if (block.isMapped()) {MemoryBlockSourceInfo info = block.getSourceInfos().get(0);- mappingScheme = info.getByteMappingScheme().get().getEncodedMappingScheme();+ if (block.getType() == MemoryBlockType.BYTE_MAPPED) {+ mappingScheme = info.getByteMappingScheme().get().getEncodedMappingScheme();+ }mappedAddr = info.getMappedRange().get().getMinAddress();}MemoryBlockDB newBlock = adapter.createBlock(block.getType(), name, start, length,diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/Memory.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/Memory.javaindex 3e137e4f41..e4f0e34c38 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/Memory.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/Memory.java@@ -826,7 +826,7 @@ public interface Memory extends AddressSetView {}for (int i = 0; i < name.length(); i++) {char c = name.charAt(i);- if (c < 0x20 || c >= 0x7f || c == ':') {+ if (c <= 0x20 || c >= 0x7f || c == ':') {return false;}}