Java Code Examples for ghidra.program.model.mem.MemoryBlock#getEnd()
The following examples show how to use
ghidra.program.model.mem.MemoryBlock#getEnd() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListingCodeComparisonPanel.java From ghidra with Apache License 2.0 | 6 votes |
/** * Gets the ending address to be displayed. It tries to get an ending address that is * maxOffset number of bytes beyond the minAddress without leaving the memory block * that contains the minAddress. If the maxOffset is beyond the end of the block then * the end of the block is returned. For an externalAddress the minAddress is returned. * @param program the program containing the data * @param maxOffset the max offset * @param minAddress the minimum address of the data * @return the end address to display */ private Address getEndAddress(Program program, long maxOffset, Address minAddress) { if (minAddress.isExternalAddress()) { return minAddress; // Begin and end address are same for external data. } MemoryBlock block = program.getMemory().getBlock(minAddress); Address blockEnd = block.getEnd(); Address endAddress; try { endAddress = minAddress.add(maxOffset); if (endAddress.compareTo(blockEnd) > 0) { endAddress = blockEnd; } } catch (AddressOutOfBoundsException e) { endAddress = blockEnd; } return endAddress; }
Example 2
Source File: MemorySectionResolver.java From ghidra with Apache License 2.0 | 6 votes |
/** * Determine loaded memory conflict set. Use physical address of loaded overlay * blocks to force reconciliation and avoid duplication. * @param rangeMin * @param rangeMax * @return conflict memory set */ private AddressSet getMemoryConflictSet(Address rangeMin, Address rangeMax) { // dedicated non-loaded overlay - don't bother with conflict check if (rangeMin.isNonLoadedMemoryAddress()) { return new AddressSet(); } // Get base memory conflict set Memory memory = getMemory(); AddressSet rangeSet = new AddressSet(rangeMin, rangeMax); AddressSet conflictSet = memory.intersect(rangeSet); // Add in loaded overlay conflicts (use their physical address) for (MemoryBlock block : memory.getBlocks()) { Address minAddr = block.getStart(); Address maxAddr = block.getEnd(); if (minAddr.isLoadedMemoryAddress() && minAddr.getAddressSpace().isOverlaySpace()) { AddressSet intersection = rangeSet.intersectRange(minAddr.getPhysicalAddress(), maxAddr.getPhysicalAddress()); conflictSet.add(intersection); } } return conflictSet; }
Example 3
Source File: TreeManagerTest.java From ghidra with Apache License 2.0 | 6 votes |
@Test public void testRemoveBlock() throws Exception { ProgramModule root = treeManager.createRootModule("Test-One"); ProgramModule r2 = treeManager.createRootModule("Test-Two"); addBlock("TestBlock", 0x5000, 100); MemoryBlock b2 = addBlock("TestTwoBlock", 0x6000, 200); addBlock("TestThreeBlock", 0x6500, 100); int r1FragCount = root.getChildren().length; int r2FragCount = r2.getChildren().length; assertEquals(r1FragCount, r2FragCount); assertEquals(5, r1FragCount); Address startAddr = b2.getStart(); Address endAddr = b2.getEnd(); treeManager.deleteAddressRange(startAddr, endAddr, TaskMonitorAdapter.DUMMY_MONITOR); r1FragCount = root.getChildren().length; r2FragCount = r2.getChildren().length; assertEquals(r1FragCount, r2FragCount); assertEquals(4, r1FragCount); }
Example 4
Source File: TreeManager.java From ghidra with Apache License 2.0 | 5 votes |
/** * Method addMemoryBlocks; called when a new module manager is * being created. */ private void addMemoryBlocks(ModuleManager mgr) { Memory memory = program.getMemory(); MemoryBlock[] blocks = memory.getBlocks(); for (MemoryBlock block : blocks) { AddressRange range = new AddressRangeImpl(block.getStart(), block.getEnd()); try { mgr.addMemoryBlock(block.getName(), range); } catch (IOException e) { errHandler.dbError(e); break; } } }
Example 5
Source File: SymbolicPropogator.java From ghidra with Apache License 2.0 | 5 votes |
/** * Identify EXTERNAL block range which should not be disassembled. * @param program * @return EXTERNAL block range or null if not found */ private void setExternalRange(Program program) { MemoryBlock block = program.getMemory().getBlock(MemoryBlock.EXTERNAL_BLOCK_NAME); if (block != null) { externalBlockRange = new AddressRangeImpl(block.getStart(), block.getEnd()); } }
Example 6
Source File: CreateArrayAction.java From ghidra with Apache License 2.0 | 5 votes |
/** * Get the maximum number of elements that will fit. This means the undefined * bytes available up to the next instruction or defined data. * * NOTE: right now this does not include holes in memory, or looking for the * end of memory. The protoBuf would need to be changed to find the end of the * current memory block and restrict the length to that. * * @return the maximum number of elements for an array of the given data type */ private int getMaxElementsThatFit(Program program, Address addr, int elementSize) { // can't go past the end of a block to start with MemoryBlock block = program.getMemory().getBlock(addr); if (block == null) { return 0; } Address maxAddr = block.getEnd(); // get the next non undefined element in memory Instruction instr = program.getListing().getInstructionAfter(addr); if (instr != null) { Address instrAddr = instr.getMinAddress(); if (instrAddr.compareTo(maxAddr) < 0) { maxAddr = instrAddr.subtract(1); } } // get the next non undefined data element in memory Data data = DataUtilities.getNextNonUndefinedDataAfter(program, addr, maxAddr); if (data != null) { Address dataAddr = data.getMinAddress(); if (dataAddr.compareTo(maxAddr) < 0) { maxAddr = dataAddr.subtract(1); } } int length = (int) maxAddr.subtract(addr) + 1; if (length < 0) { return 0; } return (length / elementSize); }
Example 7
Source File: MoveBlockModel.java From ghidra with Apache License 2.0 | 5 votes |
/** * Set up this model. * * @param blockToInitialize block to move */ void initialize(MemoryBlock blockToInitialize) { this.block = blockToInitialize; newStartAddr = blockToInitialize.getStart(); blockStart = newStartAddr; newEndAddr = blockToInitialize.getEnd(); listener.stateChanged(); }
Example 8
Source File: ExpandBlockModel.java From ghidra with Apache License 2.0 | 5 votes |
/** * Initialize this model using the given block. * @param newBlock block that will be expanded */ void initialize(MemoryBlock newBlock) { this.block = newBlock; length = newBlock.getSize(); startAddr = newBlock.getStart(); endAddr = newBlock.getEnd(); blockStart = startAddr; message = ""; listener.stateChanged(null); }
Example 9
Source File: MemoryMapProvider.java From ghidra with Apache License 2.0 | 5 votes |
private void selectAddress() { int row = memTable.getSelectedRow(); int viewColumn = memTable.getSelectedColumn(); int col = memTable.convertColumnIndexToModel(viewColumn); MemoryBlock block = mapModel.getBlockAt(row); if (block != null && (col == 1 || col == 2)) { Address addr = (col == 1 ? block.getStart() : block.getEnd()); plugin.blockSelected(block, addr); memTable.setRowSelectionInterval(row, row); } }
Example 10
Source File: MemoryMapProvider.java From ghidra with Apache License 2.0 | 5 votes |
/** * Enable/disable the expand up/down actions according to the selected * block. * * @param numSelected number of blocks selected */ private void enableExpandActions(int numSelected) { if (numSelected != 1) { expandUpAction.setEnabled(false); expandDownAction.setEnabled(false); } else { MemoryBlock block = getSelectedBlock(); if (block.getType() != MemoryBlockType.DEFAULT) { expandDownAction.setEnabled(false); expandUpAction.setEnabled(false); return; } if (block.getStart().getOffset() == 0) { expandUpAction.setEnabled(false); } else { expandUpAction.setEnabled(true); } Address endAddr = block.getEnd(); if (endAddr.equals(endAddr.getAddressSpace().getMaxAddress())) { expandDownAction.setEnabled(false); } else { expandDownAction.setEnabled(true); } } }
Example 11
Source File: DecompileDebug.java From ghidra with Apache License 2.0 | 5 votes |
private boolean isReadOnly(Address addr) { if ((readonlycache != null) && (readonlycache.contains(addr))) { return readonlycacheval; } MemoryBlock block = program.getMemory().getBlock(addr); readonlycache = null; readonlycacheval = false; if (block != null) { readonlycacheval = !block.isWrite(); readonlycache = new AddressRangeImpl(block.getStart(), block.getEnd()); } return readonlycacheval; }
Example 12
Source File: DexCondenseFillerBytesAnalyzer.java From ghidra with Apache License 2.0 | 5 votes |
@Override public boolean analyze( Program program, AddressSetView set, TaskMonitor monitor, MessageLog log ) throws Exception { AlignmentDataType alignmentDataType = new AlignmentDataType( ); Address address = toAddr( program, DexUtil.METHOD_ADDRESS ); MemoryBlock block = program.getMemory().getBlock( address ); if ( block == null ) { log.appendMsg( "Can't locate block with method byte code!" ); return false; } AddressSet blockSet = new AddressSet( block.getStart( ), block.getEnd( ) ); AddressSetView undefinedSet = program.getListing().getUndefinedRanges( blockSet, true, monitor ); monitor.setMaximum( undefinedSet.getNumAddressRanges() ); monitor.setProgress( 0 ); monitor.setMessage( "DEX: condensing filler bytes" ); AddressRangeIterator addressRanges = undefinedSet.getAddressRanges(); while ( addressRanges.hasNext( ) ) { monitor.checkCanceled( ); monitor.incrementProgress( 1 ); AddressRange addressRange = addressRanges.next(); if ( isRangeAllSameBytes( program, addressRange, (byte) 0xff, monitor ) ) { program.getListing().createData( addressRange.getMinAddress(), alignmentDataType, (int)addressRange.getLength() ); } } //collapseFillerBytes( program, monitor ); return true; }
Example 13
Source File: PowerPC64_ElfExtension.java From ghidra with Apache License 2.0 | 5 votes |
private void processOPDSection(ElfLoadHelper elfLoadHelper, TaskMonitor monitor) throws CancelledException { MemoryBlock opdBlock = elfLoadHelper.getProgram().getMemory().getBlock(".opd"); if (opdBlock == null) { return; } monitor.setMessage("Processing Function Descriptor Symbols..."); Address addr = opdBlock.getStart(); Address endAddr = opdBlock.getEnd(); monitor.setShowProgressValue(true); monitor.setProgress(0); monitor.setMaximum((endAddr.subtract(addr) + 1) / 24); int count = 0; try { while (addr.compareTo(endAddr) < 0) { monitor.checkCanceled(); monitor.setProgress(++count); processOPDEntry(elfLoadHelper, addr); addr = addr.addNoWrap(24); } } catch (AddressOverflowException e) { // ignore end of space } // allow .opd section contents to be treated as constant values opdBlock.setWrite(false); }
Example 14
Source File: SampleProgramTreePlugin.java From ghidra with Apache License 2.0 | 4 votes |
@Override public boolean applyTo(DomainObject obj, TaskMonitor monitor) { Program program = (Program) obj; listing = program.getListing(); createDefaultTreeView(program, programTreeName); Memory mem = program.getMemory(); ProgramModule root_module = listing.getRootModule(programTreeName); AddressSet set = new AddressSet(mem); try { root_module.createModule("Fragments"); } catch (DuplicateNameException e) { // don't care??? } ProgramModule frags = listing.getModule(programTreeName, "Fragments"); long startCount = set.getNumAddresses(); monitor.initialize(startCount); while (!monitor.isCancelled() && !set.isEmpty()) { MemoryBlock block = mem.getBlock(set.getMinAddress()); Address start = block.getStart(); Address end = block.getEnd(); set.deleteRange(block.getStart(), block.getEnd()); long numLeft = set.getNumAddresses(); monitor.setProgress(startCount - numLeft); String mod_name = block.getName(); monitor.setMessage("Module " + start + " : " + mod_name); ProgramModule mod = make_module(mod_name, frags); makeFragment(start, end, "frag_" + fragment_count, mod); fragment_count++; } return true; }
Example 15
Source File: DiffOverlayApplyTest.java From ghidra with Apache License 2.0 | 4 votes |
@Test public void testShowHideDiffApplySettings() throws Exception { ClassicSampleX86ProgramBuilder builder = new ClassicSampleX86ProgramBuilder(); builder.createMemory(".data", "0x2001000", 1000); Program p1 = builder.getProgram(); assertTrue(p1.getAddressFactory() instanceof ProgramAddressFactory); assertEquals(2, p1.getAddressFactory().getNumAddressSpaces()); // ram, OTHER int id1 = p1.startTransaction(""); Memory memory1 = p1.getMemory(); MemoryBlock dataBlock1 = memory1.getBlock(".data"); MemoryBlock overlayBlock1 = memory1.createInitializedBlock("OVL1", dataBlock1.getStart(), 0x20L, (byte) 0, TaskMonitorAdapter.DUMMY_MONITOR, true); assertEquals(3, p1.getAddressFactory().getNumAddressSpaces()); // ram, OTHER, OVL1 AddressSet addressSet1 = new AddressSet(overlayBlock1.getStart(), overlayBlock1.getEnd()); byte[] bytes1 = { 'a', 'p', 'p', 'l', 'e', (byte) 0, 'o', 'r', 'a', 'n', 'g', 'e', (byte) 0 }; memory1.setBytes(overlayBlock1.getStart(), bytes1); Listing listing1 = p1.getListing(); Address overlayAddress1 = overlayBlock1.getStart(); listing1.createData(overlayAddress1, new TerminatedStringDataType()); overlayAddress1 = overlayAddress1.add(6); listing1.createData(overlayAddress1, new TerminatedStringDataType()); p1.endTransaction(id1, true); ClassicSampleX86ProgramBuilder builder2 = new ClassicSampleX86ProgramBuilder(); builder2.createMemory(".data", "0x2001000", 1000); Program p2 = builder2.getProgram(); assertTrue(p2.getAddressFactory() instanceof ProgramAddressFactory); assertEquals(2, p2.getAddressFactory().getNumAddressSpaces()); int id2 = p2.startTransaction(""); Memory memory2 = p2.getMemory(); MemoryBlock dataBlock2 = memory2.getBlock(".data"); MemoryBlock overlayBlock2 = memory2.createInitializedBlock("OVL1", dataBlock2.getStart(), 0x20L, (byte) 0, TaskMonitorAdapter.DUMMY_MONITOR, true); assertEquals(3, p2.getAddressFactory().getNumAddressSpaces()); AddressSet addressSet2 = DiffUtility.getCompatibleAddressSet(addressSet1, p2); byte[] bytes2 = { 'd', 'o', 'b', 'e', 'r', 'm', 'a', 'n', (byte) 0, 'p', 'o', 'o', 'd', 'l', 'e', (byte) 0 }; memory2.setBytes(overlayBlock2.getStart(), bytes2); Listing listing2 = p2.getListing(); Address overlayAddress2 = overlayBlock2.getStart(); listing2.createData(overlayAddress2, new TerminatedStringDataType()); overlayAddress2 = overlayAddress2.add(9); listing2.createData(overlayAddress2, new TerminatedStringDataType()); p2.endTransaction(id2, true); openProgram(p1); openDiff(p2); setDiffSelection(addressSet2); apply(); Listing listing = p1.getListing(); MemoryBlock overlayBlock = p1.getMemory().getBlock("OVL1"); Address overlayAddress = overlayBlock.getStart(); Data dataAt = listing.getDataAt(overlayAddress); assertNotNull(dataAt); assertEquals("doberman", dataAt.getValue()); overlayAddress = overlayBlock.getStart().add(9); dataAt = listing.getDataAt(overlayAddress); assertNotNull(dataAt); assertEquals("poodle", dataAt.getValue()); }
Example 16
Source File: iOS_KextStubFixupAnalyzer.java From ghidra with Apache License 2.0 | 4 votes |
private AddressSet toAddressSet(MemoryBlock block) { return new AddressSet(block.getStart(), block.getEnd()); }
Example 17
Source File: DataPlugin.java From ghidra with Apache License 2.0 | 4 votes |
private boolean checkEnoughSpace(Program program, Address start, DataType dataType, boolean convertPointers) { Listing listing = program.getListing(); Data data = listing.getDataAt(start); if (data == null) { tool.setStatusInfo("Invalid data location"); return false; } if (canConvertPointer(dataType, data, convertPointers)) { return true; } int newSize = getDataTypeSize(program, dataType, start); if (newSize == 1) { return true; } Address end = null; try { end = start.addNoWrap(newSize - 1); } catch (AddressOverflowException e) { return false; } if (intstrutionExists(listing, dataType, start, end)) { return false; } // See if the Data will fit within the block of memory where it starts. MemoryBlock memBlock = program.getMemory().getBlock(start); Address blockMaxAddress = memBlock.getEnd(); if (blockMaxAddress.compareTo(end) < 0) { tool.setStatusInfo("Create " + dataType.getName() + " failed: Not enough room in memory block containing address " + start + " which ends at " + blockMaxAddress + "."); return false; } // Ignore any sized undefined data types until we get to a defined data type. // If only sized Undefined types are found then overwrite them. Data definedData = DataUtilities.getNextNonUndefinedDataAfter(program, start, blockMaxAddress); if (dataExists(program, dataType, definedData, start, end)) { return false; } return true; }
Example 18
Source File: DisassembledViewPluginTest.java From ghidra with Apache License 2.0 | 4 votes |
/** * Tests the plugins response to * {@link ghidra.app.events.ProgramLocationPluginEvent}s. This plugin is * driven off of these events. * * @throws Exception If there is a problem opening the program. */ @Test public void testProcessingOnLocationChanged() throws Exception { openProgram("notepad"); // get the list hiding inside of the component provider JList list = (JList) getInstanceField("contentList", componentProvider); // sanity check assertEquals("The component provider has data when it is not visible.", 0, list.getModel().getSize()); // show the plugin and make sure it is visible before we continue tool.showComponentProvider(componentProvider, true); waitForPostedSwingRunnables(); ListModel modelOne = list.getModel(); // now the list should have data, as it will populate itself off of the // current program location of the plugin assertTrue("The component provider does not have data when it " + "should.", (modelOne.getSize() != 0)); // make sure we process the event in order to show the user the // preview CodeBrowserPlugin cbPlugin = getPlugin(tool, CodeBrowserPlugin.class); // scroll the display and force a new selection pageDown(cbPlugin.getFieldPanel()); simulateButtonPress(cbPlugin); waitForPostedSwingRunnables(); // get the data ListModel modelTwo = list.getModel(); boolean sameData = compareListData(modelOne, modelTwo); assertTrue("The contents of the two lists are the same when they " + "should not be.", !sameData); // make sure no work is done when we are not visible tool.showComponentProvider(componentProvider, false); waitForPostedSwingRunnables(); assertEquals("The component provider has data when it is not visible.", 0, list.getModel().getSize()); // show the plugin so that it will get the program location change // data tool.showComponentProvider(componentProvider, true); waitForPostedSwingRunnables(); // test that sending a bad address will not return any results or // throw any exceptions Memory memory = program.getMemory(); MemoryBlock textBlock = memory.getBlock(".text"); Address endAddress = textBlock.getEnd(); // creating a program location based upon the end address should result // in only one item in the disassembled list ProgramLocation location = new ProgramLocation(program, endAddress); // call the locationChanged() method invokeInstanceMethod("locationChanged", plugin, new Class[] { ProgramLocation.class }, new Object[] { location }); assertTrue( "The plugin's display list has more than 1 element when " + "at the end address of a memory block. List size: " + list.getModel().getSize(), (list.getModel().getSize() == 1)); Listing listing = program.getListing(); CodeUnit codeUnit = listing.getCodeUnitAt(endAddress); Address invalidAddress = endAddress.addNoWrap(codeUnit.getLength()); ProgramLocation newLocation = new ProgramLocation(program, invalidAddress); invokeInstanceMethod("locationChanged", plugin, new Class[] { ProgramLocation.class }, new Object[] { newLocation }); assertEquals("The plugin's display list has data when there is an " + "invalid address at the current program location.", list.getModel().getSize(), 0); }
Example 19
Source File: CondenseRepeatingBytesAtEndOfMemory.java From ghidra with Apache License 2.0 | 4 votes |
@Override public void run() throws Exception { if (currentAddress == null) { println("No Location."); return; } MemoryBlock memoryBlock = currentProgram.getMemory().getBlock(currentAddress); if(!memoryBlock.isInitialized()){ println("Script cannot run in uninitialized memory."); return; } Listing listing = currentProgram.getListing(); Address currentAddr = currentAddress; boolean isInitializedBlock = memoryBlock.isInitialized(); if(isInitializedBlock){ currentAddr = memoryBlock.getEnd(); println("end of byte addr is " + currentAddr); byte repeatingByte = currentProgram.getMemory().getByte(currentAddr); MemoryBlock currentMemoryBlock = null; // search for next repeatedByte from the end of memory // until it hits defined area or different byte byte prevByte = repeatingByte; int repeatLen = 0; boolean noCollisions = listing.isUndefined(currentAddr,currentAddr); boolean hasLabels = currentProgram.getSymbolTable().hasSymbol(currentAddr); println("no collisions at end = " + noCollisions); currentMemoryBlock = currentProgram.getMemory().getBlock(currentAddr); while((prevByte == repeatingByte) && (noCollisions) && (currentMemoryBlock.equals(memoryBlock)) && (!hasLabels)){ repeatLen++; currentAddr = currentAddr.addNoWrap(-1); prevByte = currentProgram.getMemory().getByte(currentAddr); noCollisions = listing.isUndefined(currentAddr,currentAddr); hasLabels = currentProgram.getSymbolTable().hasSymbol(currentAddr); currentMemoryBlock = currentProgram.getMemory().getBlock(currentAddr); } if(repeatLen > 0){ // this takes care of the last one tested that failed currentAddr = currentAddr.addNoWrap(1); listing.createData(currentAddr, new AlignmentDataType(), repeatLen); println("Applied Alignment datatype at " + currentAddr.toString()); } else{ println("No repeating bytes OR data already defined at end of " + memoryBlock); } } else{ println("Cannot condense uninitialized memory."); } }
Example 20
Source File: DataDB.java From ghidra with Apache License 2.0 | 4 votes |
private void computeLength() { length = dataType.getLength(); if (length < 1) { length = codeMgr.getLength(address); } if (length < 1) { if (baseDataType instanceof Pointer) { length = address.getPointerSize(); } else { length = 1; } } // FIXME Trying to get Data to display for External. if (address.isExternalAddress()) { // FIXME return; // FIXME } // FIXME Memory mem = program.getMemory(); Address endAddress = null; boolean noEndAddr = false; try { endAddress = address.addNoWrap(length - 1); } catch (AddressOverflowException ex) { noEndAddr = true; } if (noEndAddr || (!mem.contains(address, endAddress))) { MemoryBlock block = mem.getBlock(address); if (block != null) { endAddress = block.getEnd(); length = (int) endAddress.subtract(address) + 1; } else { length = 1; // ?? what should this be? } } Address nextAddr = codeMgr.getDefinedAddressAfter(address); if ((nextAddr != null) && nextAddr.compareTo(endAddress) <= 0) { length = (int) nextAddr.subtract(address); } bytes = null; }