Java Code Examples for ghidra.program.model.address.AddressSetView#getMinAddress()

The following examples show how to use ghidra.program.model.address.AddressSetView#getMinAddress() . 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: GhidraScriptAnalyzerAdapter.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public boolean added(Program program, AddressSetView set, TaskMonitor monitor, MessageLog log)
		throws CancelledException {

	if (script == null) {
		return false;
	}
	ProgramLocation loc = new ProgramLocation(program, set.getMinAddress());
	ProgramSelection selection = new ProgramSelection(set);

	// TODO if this API is ever used, then we need a project passed for scripts that 
	//      may need it
	Project project = null;
	GhidraState scriptState = new GhidraState(null, project, program, loc, selection, null);

	return GhidraScriptUtil.runScript(scriptState, script, writer, this, monitor);
}
 
Example 2
Source File: OpenCloseManager.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void toggleAllDataInAddresses(boolean open, Program program, AddressSetView addresses,
		TaskMonitor monitor) {
	monitor.initialize(addresses.getNumAddresses());
	Address start = addresses.getMinAddress();

	Listing listing = program.getListing();
	DataIterator iterator = listing.getData(addresses, true);
	while (iterator.hasNext()) {
		if (monitor.isCancelled()) {
			return;
		}

		Data data = iterator.next();

		toggleDataRecursively(data, open, monitor);

		Address max = data.getMaxAddress();
		long progress = max.subtract(start);

		monitor.setProgress(progress);
	}

	notifyDataToggled();
}
 
Example 3
Source File: HeadlessAnalyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private GhidraState getInitialProgramState(Program program) {
	ProgramLocation location = null;
	AddressSetView initializedMem = program.getMemory().getLoadedAndInitializedAddressSet();
	if (!initializedMem.isEmpty()) {
		location = new ProgramLocation(program, initializedMem.getMinAddress());
	}
	return new GhidraState(null, project, program, location, null, null);
}
 
Example 4
Source File: CommentsDBAdapterV1.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
AddressKeyIterator getKeys(AddressSetView set, boolean forward) throws IOException {
	if (forward) {
		return new AddressKeyIterator(commentTable, addrMap, set, set.getMinAddress(), true);
	}
	return new AddressKeyIterator(commentTable, addrMap, set, set.getMaxAddress(), false);
}
 
Example 5
Source File: CommentsDBAdapterV0.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * @see ghidra.program.database.code.CommentsDBAdapter#getKeys(ghidra.program.model.address.AddressSetView, boolean)
 */
@Override
public AddressKeyIterator getKeys(AddressSetView set, boolean forward) throws IOException {
	if (forward) {
		return new AddressKeyIterator(commentTable, addrMap, set, set.getMinAddress(), true);
	}
	return new AddressKeyIterator(commentTable, addrMap, set, set.getMaxAddress(), false);
}
 
Example 6
Source File: GroupHistoryInfo.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private static Map<AddressHasher, FGVertex> hashVerticesByStartAndEndAddress(
		FunctionGraph functionGraph) {
	Map<AddressHasher, FGVertex> map = new HashMap<>();
	Graph<FGVertex, FGEdge> graph = functionGraph;
	Collection<FGVertex> vertices = graph.getVertices();

	for (FGVertex vertex : vertices) {
		AddressSetView addresses = vertex.getAddresses();
		Address minAddress = addresses.getMinAddress();
		Address maxAddress = addresses.getMaxAddress();
		map.put(new AddressHasher(minAddress, maxAddress), vertex);
	}
	return map;
}
 
Example 7
Source File: GroupVertexSerializer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private static Map<AddressHasher, FGVertex> hashVerticesByStartAndEndAddress(
		FunctionGraph functionGraph) {
	Map<AddressHasher, FGVertex> map = new HashMap<>();
	Set<FGVertex> vertices = functionGraph.getUngroupedVertices();
	for (FGVertex vertex : vertices) {
		AddressSetView addresses = vertex.getAddresses();
		Address minAddress = addresses.getMinAddress();
		Address maxAddress = addresses.getMaxAddress();
		map.put(new AddressHasher(minAddress, maxAddress), vertex);
	}
	return map;
}
 
Example 8
Source File: AbstractDemanglerAnalyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean added(Program program, AddressSetView set, TaskMonitor monitor, MessageLog log)
		throws CancelledException {

	DemanglerOptions options = getOptions();
	if (!validateOptions(options, log)) {
		log.error(getName(), "Invalid demangler options--cannot demangle");
		return false;
	}

	monitor.initialize(100);

	SymbolTable symbolTable = program.getSymbolTable();
	SymbolIterator it = symbolTable.getPrimarySymbolIterator(set, true);
	while (it.hasNext()) {
		monitor.checkCanceled();

		Symbol symbol = it.next();
		if (skipSymbol(symbol)) {
			continue;
		}

		Address address = symbol.getAddress();
		String mangled = cleanSymbol(address, symbol.getName());
		DemangledObject demangled = demangle(mangled, options, log);
		if (demangled != null) {
			apply(program, address, demangled, options, log, monitor);
		}

		Address min = set.getMinAddress();
		Address max = set.getMaxAddress();
		int distance = (int) (address.getOffset() - min.getOffset());
		int percent = (int) ((distance / max.getOffset()) * 100);
		monitor.setProgress(percent);
	}

	return true;
}
 
Example 9
Source File: FunctionGraphGroupVertices1Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetUserText_WithPersistence() {
	FGData graphData = graphFunction("01002cf5");
	FunctionGraph functionGraph = graphData.getFunctionGraph();
	Graph<FGVertex, FGEdge> graph = functionGraph;

	Set<FGVertex> ungroupedVertices =
		selectVertices(functionGraph, "01002d2b" /* Another Local*/, "01002d1f" /* MyLocal */);
	Set<FGEdge> ungroupedEdges = getEdges(graph, ungroupedVertices);
	assertEquals("Did not grab all known edges for vertices", 4, ungroupedEdges.size());

	String groupVertexText = "Test Text";
	group(ungroupedVertices, groupVertexText);

	assertVerticesRemoved(graph, ungroupedVertices);
	assertEdgesRemoved(graph, ungroupedEdges);

	// -1 because one one of the edges was between two of the vertices being grouped
	int expectedGroupedEdgeCount = ungroupedEdges.size() - 1;
	GroupedFunctionGraphVertex groupedVertex = validateNewGroupedVertexFromVertices(
		functionGraph, ungroupedVertices, expectedGroupedEdgeCount);
	AddressSetView addresses = groupedVertex.getAddresses();
	Address minAddress = addresses.getMinAddress();
	Address maxAddress = addresses.getMaxAddress();

	graphData = triggerPersistenceAndReload("01002cf5");

	waitForAnimation();// the re-grouping may be using animation, which runs after the graph is loaded
	functionGraph = graphData.getFunctionGraph();
	graph = functionGraph;
	FGVertex vertex = functionGraph.getVertexForAddress(minAddress);
	assertTrue(vertex instanceof GroupedFunctionGraphVertex);
	assertEquals(maxAddress, vertex.getAddresses().getMaxAddress());

	groupedVertex = (GroupedFunctionGraphVertex) vertex;
	assertEquals("User-defined grouped vertex text was not restored after graph reload",
		groupVertexText, groupedVertex.getUserText());
}
 
Example 10
Source File: FunctionGraphGroupVertices1Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
	public void testGroupingPersistence_WhenOneOfTheGroupIsAGroup() throws Exception {
		// 
		// This test seeks to ensure that groups within groups are persisted and restored.
		//

		FGData graphData = graphFunction("01002cf5");
		FunctionGraph functionGraph = graphData.getFunctionGraph();
		Graph<FGVertex, FGEdge> graph = functionGraph;

		Set<FGVertex> ungroupedVertices =
			selectVertices(functionGraph, "01002d2b" /* Another Local*/, "01002d1f" /* MyLocal */);
		Set<FGEdge> ungroupedEdges = getEdges(graph, ungroupedVertices);

		//printEdges(ungroupedEdges);
		group(ungroupedVertices);

		// -1 because one one of the edges was between two of the vertices being grouped
		int expectedGroupedEdgeCount = ungroupedEdges.size() - 1;
		GroupedFunctionGraphVertex innerGroupedVertex = validateNewGroupedVertexFromVertices(
			functionGraph, ungroupedVertices, expectedGroupedEdgeCount);
		assertVerticesRemoved(graph, ungroupedVertices);
		assertEdgesRemoved(graph, ungroupedEdges);

		AddressSetView addresses = innerGroupedVertex.getAddresses();
		Address innerMinAddress = addresses.getMinAddress();
		Address innerMaxAddress = addresses.getMaxAddress();

		//
		// Now group the group vertex with another vertex
		//
		Set<FGVertex> outerUngroupedVertices = selectVertices(functionGraph,
			"01002d0f" /* LAB_01002d0f */, "01002d1f" /* Grouped Vertex */);
		Set<FGEdge> outerUngroupedEdges = getEdges(graph, outerUngroupedVertices);

		//printEdges(outerUngroupedEdges);
		group(outerUngroupedVertices);

		// 5 edges expected: 
		// -ungrouped vertex: 1 in, 1 out
		// -grouped vertex  : 1 in, 2 out
		expectedGroupedEdgeCount = 5;
		GroupedFunctionGraphVertex outerGroupedVertex = validateNewGroupedVertexFromVertices(
			functionGraph, outerUngroupedVertices, expectedGroupedEdgeCount);
		assertVerticesRemoved(graph, outerUngroupedVertices);
		assertEdgesRemoved(graph, outerUngroupedEdges);

		AddressSetView outerAddresses = outerGroupedVertex.getAddresses();
		Address secondMinAddress = outerAddresses.getMinAddress();
		Address secondMaxAddress = outerAddresses.getMaxAddress();

		graphData = triggerPersistenceAndReload("01002cf5");

		waitForAnimation();// the re-grouping may be using animation, which runs after the graph is loaded
		functionGraph = graphData.getFunctionGraph();
		graph = functionGraph;
		FGVertex vertex = functionGraph.getVertexForAddress(secondMinAddress);
		assertTrue(vertex instanceof GroupedFunctionGraphVertex);
		assertEquals(secondMaxAddress, vertex.getAddresses().getMaxAddress());
		outerGroupedVertex = (GroupedFunctionGraphVertex) vertex;

//		outerUngroupedVertices =
//			selectVertices(functionGraph, "01002d0f" /* LAB_01002d0f */, "01002d1f" /* Grouped Vertex */);
//		outerUngroupedEdges = getEdges(graph, outerUngroupedVertices);

		//printEdges(outerUngroupedEdges);
		ungroup(outerGroupedVertex);

		vertex = functionGraph.getVertexForAddress(innerMinAddress);
		assertTrue(vertex instanceof GroupedFunctionGraphVertex);
		assertEquals(innerMaxAddress, vertex.getAddresses().getMaxAddress());
		innerGroupedVertex = (GroupedFunctionGraphVertex) vertex;

		//printEdges(outerUngroupedEdges);
		assertEdgesAdded(functionGraph, outerUngroupedEdges);

		ungroup(innerGroupedVertex);

		assertEdgesAdded(functionGraph, ungroupedEdges);
	}
 
Example 11
Source File: FunctionGraphPlugin2Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
protected void doTestRestoringWhenCodeBlocksHaveChanged_DoesntRegroup() {
	// 
	// Tests the behavior of how group vertices are restored when one or more of the vertices 
	// inside of the grouped vertex is no longer available when the graph attempts to restore
	// the group vertex user settings (i.e., when restarting Ghidra, the previously grouped
	// vertices should reappear).  
	//
	// In this test, we will be mutating a group of 2 nodes such
	// that one of the nodes has been split into two.  This leaves only one vertex to 
	// be found by the regrouping algorithm.  Furthermore, the regrouping will not take place
	// if at least two vertices cannot be found.
	//

	// 
	// Pick a function and group some nodes.
	//
	FGData graphData = graphFunction("01002cf5");
	FunctionGraph functionGraph = graphData.getFunctionGraph();

	Set<FGVertex> ungroupedVertices =
		selectVertices(functionGraph, "01002d11" /* LAB_01002d11 */, "01002cf5" /* ghidra */);

	group(ungroupedVertices);

	// 5 edges expected: 
	// -01002cf5: 2 out 
	// -01002cf5: 2 in, 1 out
	int expectedGroupedEdgeCount = 5;
	GroupedFunctionGraphVertex groupedVertex = validateNewGroupedVertexFromVertices(
		functionGraph, ungroupedVertices, expectedGroupedEdgeCount);

	AddressSetView addresses = groupedVertex.getAddresses();
	Address minAddress = addresses.getMinAddress();

	//
	// Ideally, we would like to save, close and re-open the program so that we can get 
	// a round-trip saving and reloading.  However, in the test environment, we cannot save 
	// our programs.  So, we will instead just navigate away from the current function, clear
	// the cache (to make sure that we read the settings again), and then verify that the 
	// data saved in the program has been used to re-group.
	//
	graphFunction("0100415a");
	clearCache();

	//
	// Add a label to trigger a code block change
	//
	createLabel("01002d18");// in the middle of the LAB_01002d11 code block

	//
	// Relaunch the graph, which will use the above persisted group settings...
	//
	graphData = graphFunction("01002cf5");
	waitForAnimation();// the re-grouping may be using animation, which runs after the graph is loaded
	functionGraph = graphData.getFunctionGraph();
	FGVertex expectedGroupVertex = functionGraph.getVertexForAddress(minAddress);
	assertFalse(expectedGroupVertex instanceof GroupedFunctionGraphVertex);
}
 
Example 12
Source File: FunctionGraphPlugin2Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public void dont_testSelectionFromGraphToCodeBrowser() {
	FGData graphData = getFunctionGraphData();
	assertNotNull(graphData);
	assertTrue("Unexpectedly received an empty FunctionGraphData", graphData.hasResults());
	ProgramLocation location = getLocationForAddressString(startAddressString);
	assertTrue(graphData.containsLocation(location));
	FunctionGraph functionGraph = graphData.getFunctionGraph();

	// locate vertex with cursor
	FGVertex focusedVertex = getFocusVertex(functionGraph);
	assertNotNull("We did not start with a focused vertex", focusedVertex);

	AddressSetView addresses = focusedVertex.getAddresses();
	Address address = addresses.getMinAddress();
	focusedVertex.setProgramSelection(new ProgramSelection(address, address));

	// make sure the code browser now contains a matching selection
	ProgramSelection firstSelection = focusedVertex.getProgramSelection();
	ListingPanel listingPanel = codeBrowser.getListingPanel();
	ProgramSelection codeBrowserSelection = listingPanel.getProgramSelection();
	assertTrue(!firstSelection.isEmpty());
	assertEquals("Selecting text in the vertex did not select text in the code browser",
		firstSelection, codeBrowserSelection);

	Collection<FGVertex> vertices = functionGraph.getVertices();
	FGVertex otherVertex = null;
	for (FGVertex vertex : vertices) {
		if (vertex != focusedVertex) {
			otherVertex = vertex;
			break;
		}
	}
	assertNotNull(otherVertex);

	Address otherAddress = otherVertex.getAddresses().getMinAddress();
	otherVertex.setProgramSelection(new ProgramSelection(otherAddress, otherAddress));
	ProgramSelection secondSelection = otherVertex.getProgramSelection();
	assertTrue(!secondSelection.isEmpty());

	codeBrowserSelection = listingPanel.getProgramSelection();

	// the new code browser selection should have both of our vertex selections
	assertTrue(codeBrowserSelection.getMinAddress().equals(firstSelection.getMinAddress()));
	assertTrue(codeBrowserSelection.getMaxAddress().equals(secondSelection.getMaxAddress()));
}
 
Example 13
Source File: FunctionGraphGroupVertices1Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
protected void doTestRestoringWhenCodeBlocksHaveChanged_WillRegroup() {
	// 
	// Tests the behavior of how group vertices are restored when one or more of the vertices 
	// inside of the grouped vertex is no longer available when the graph attempts to restore
	// the group vertex user settings (i.e., when restarting Ghidra, the previously grouped
	// vertices should reappear).
	//
	// In this test, we will be mutating a group of 3 nodes such
	// that one of the nodes has been split into two.  This leaves 2 vertices to 
	// be found by the regrouping algorithm.  Furthermore, the regrouping *will* still
	// take place, as at least two vertices cannot be found.
	//

	// 
	// Pick a function and group some nodes.
	//
	FGData graphData = graphFunction("01002cf5");
	FunctionGraph functionGraph = graphData.getFunctionGraph();

	Set<FGVertex> ungroupedVertices = selectVertices(functionGraph,
		"01002d11" /* LAB_01002d11 */, "01002cf5" /* ghidra */, "01002d1f" /* MyLocal */);

	group(ungroupedVertices);

	// 5 edges expected: 
	// -01002cf5: 2 out 
	// -01002d11: 2 in, (1 out that was removed)
	// -01002d1f: 2 out (1 in that was removed)
	int expectedGroupedEdgeCount = 6;
	GroupedFunctionGraphVertex groupedVertex = validateNewGroupedVertexFromVertices(
		functionGraph, ungroupedVertices, expectedGroupedEdgeCount);

	AddressSetView addresses = groupedVertex.getAddresses();
	Address minAddress = addresses.getMinAddress();
	Address maxAddress = addresses.getMaxAddress();

	//
	// Ideally, we would like to save, close and re-open the program so that we can get 
	// a round-trip saving and reloading.  However, in the test environment, we cannot save 
	// our programs.  So, we will instead just navigate away from the current function, clear
	// the cache (to make sure that we read the settings again), and then verify that the 
	// data saved in the program has been used to re-group.
	//
	graphFunction("0100415a");
	clearCache();

	//
	// Add a label to trigger a code block change
	//
	Address labelAddress = createLabel("01002d18");// in the middle of the LAB_01002d11 code block

	//
	// Relaunch the graph, which will use the above persisted group settings...
	//
	graphData = graphFunction("01002cf5");
	waitForAnimation();// the re-grouping may be using animation, which runs after the graph is loaded
	functionGraph = graphData.getFunctionGraph();
	FGVertex expectedGroupVertex = functionGraph.getVertexForAddress(minAddress);
	assertTrue(expectedGroupVertex instanceof GroupedFunctionGraphVertex);
	assertEquals(maxAddress, expectedGroupVertex.getAddresses().getMaxAddress());

	// ...we expect that the two original grouped vertices have again been grouped...
	FGVertex splitVertex =
		functionGraph.getVertexForAddress(getAddress("01002d11") /* LAB_01002d11 */);
	assertTrue("The split vertex should not have been regrouped",
		!(splitVertex instanceof GroupedFunctionGraphVertex));

	FGVertex unchangedVertex =
		functionGraph.getVertexForAddress(getAddress("01002cf5") /* ghidra */);
	assertTrue("An unchanged vertex should have been regrouped: " + unchangedVertex,
		(unchangedVertex instanceof GroupedFunctionGraphVertex));

	unchangedVertex = functionGraph.getVertexForAddress(getAddress("01002d1f") /* MyLocal */);
	assertTrue("An unchanged vertex should have been regrouped: " + unchangedVertex,
		(unchangedVertex instanceof GroupedFunctionGraphVertex));

	// ...but the newly created code block has not
	FGVertex newlyCreatedVertex = functionGraph.getVertexForAddress(labelAddress);
	assertNotNull(newlyCreatedVertex);
}
 
Example 14
Source File: RelocationDBAdapterV4.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
RecordIterator iterator(AddressSetView set) throws IOException {
	return new AddressKeyRecordIterator(relocTable, addrMap, set, set.getMinAddress(), true);
}
 
Example 15
Source File: FunctionGraphPlugin1Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testCopyKeyBinding() throws Exception {
	//
	// Make a program selection and test that executing the copy keybinding will copy the 
	// selection (across vertices).
	//

	// 
	// Initialize the clipboard with known data
	//
	Clipboard systemClipboard = tool.getToolFrame().getToolkit().getSystemClipboard();
	systemClipboard.setContents(DUMMY_TRANSFERABLE, null);
	waitForSwing();

	//
	// Verify our initial state
	FGData graphData = getFunctionGraphData();
	assertNotNull(graphData);
	assertTrue("Unexpectedly received an empty FunctionGraphData", graphData.hasResults());
	ProgramLocation location = getLocationForAddressString(startAddressString);
	assertTrue(graphData.containsLocation(location));
	FunctionGraph functionGraph = graphData.getFunctionGraph();

	// locate vertex with cursor
	FGVertex focusedVertex = getFocusVertex(functionGraph);
	assertNotNull("We did not start with a focused vertex", focusedVertex);

	//
	// Create a selection that we will copy from the executing the action 
	//
	AddressSetView addresses = focusedVertex.getAddresses();
	Address address = addresses.getMinAddress();
	ProgramSelection selection =
		new ProgramSelection(program.getAddressFactory(), address, address.add(8));
	tool.firePluginEvent(new ProgramSelectionPluginEvent("Test", selection, program));

	//
	// Validate and execute the action
	//
	DockingAction copyAction = getCopyAction();
	FGController controller = getFunctionGraphController();
	ComponentProvider provider = controller.getProvider();
	assertTrue(copyAction.isEnabledForContext(provider.getActionContext(null)));

	performAction(copyAction, provider, false);

	waitForTasks();

	Transferable contents = systemClipboard.getContents(systemClipboard);
	assertNotNull(contents);
	assertTrue("Contents not copied into system clipboard", (contents != DUMMY_TRANSFERABLE));
}
 
Example 16
Source File: RelocationDBAdapterV2.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
RecordIterator iterator(AddressSetView set) throws IOException {
	RecordIterator recIter =
		new AddressKeyRecordIterator(relocTable, addrMap, set, set.getMinAddress(), true);
	return new RecordIteratorAdapter(recIter);
}
 
Example 17
Source File: RelocationDBAdapterV1.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
RecordIterator iterator(AddressSetView set) throws IOException {
	RecordIterator recIter =
		new AddressKeyRecordIterator(relocTable, addrMap, set, set.getMinAddress(), true);
	return new RecordIteratorAdapter(recIter);
}
 
Example 18
Source File: RelocationDBAdapterV3.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
RecordIterator iterator(AddressSetView set) throws IOException {
	return new AddressKeyRecordIterator(relocTable, addrMap, set, set.getMinAddress(), true);
}