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

The following examples show how to use ghidra.program.model.address.AddressSetView#isEmpty() . 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: FieldSearcher.java    From ghidra with Apache License 2.0 6 votes vote down vote up
protected FieldSearcher(Pattern pattern, boolean forward, ProgramLocation startLoc,
		AddressSetView set) {
	this.pattern = pattern;
	this.forward = forward;
	this.startLocation = startLoc;

	if (forward && set != null && !set.isEmpty() && startLoc != null &&
		!set.getMinAddress().equals(startLoc.getAddress())) {
		throw new IllegalArgumentException("Start location and addressSet are inconsistent!");
	}
	if (!forward && set != null && !set.isEmpty() && startLoc != null &&
		!set.getMaxAddress().equals(startLoc.getAddress())) {
		throw new IllegalArgumentException("Start location and addressSet are inconsistent!");
	}

}
 
Example 2
Source File: AbstractFunctionGraphVertex.java    From ghidra with Apache License 2.0 5 votes vote down vote up
AbstractFunctionGraphVertex(FGController controller, Program program, AddressSetView addresses,
		FlowType flowType, boolean isEntry) {

	if (addresses == null || addresses.isEmpty()) {
		throw new IllegalArgumentException("Vertex cannot have null or empty address body");
	}

	this.controller = controller;
	this.program = program;
	this.addressSet = addresses;
	this.flowType = flowType;
	this.isEntry = isEntry;
	this.location = new Point2D.Double();
}
 
Example 3
Source File: PropertyManagerTableModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * @param currentProgram
 * @param currentSelection
 * @param searchMarks
 */
public synchronized void update(Program program, AddressSetView addrSet) {
	boolean restrictedView = (addrSet != null && !addrSet.isEmpty()); 
	ArrayList<String> list = new ArrayList<String>();

	if (program != null) {
		PropertyMapManager propMgr = program.getUsrPropertyManager();
		Iterator<String> iter = propMgr.propertyManagers();
		while (iter.hasNext()) {
			String name = iter.next();
			if (restrictedView) {
				PropertyMap map = propMgr.getPropertyMap(name);
				if (map.intersects(addrSet)) {
					list.add(name);
				}
			}
			else {
				list.add(name);	
			}
		}
	}
	propertyNames = new String[list.size()];
	list.toArray(propertyNames);
	Arrays.sort(propertyNames);

	fireTableDataChanged();
}
 
Example 4
Source File: ProgramDatabaseFieldSearcher.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected ProgramDatabaseFieldSearcher(Pattern pattern, boolean forward, ProgramLocation startLoc, AddressSetView set) {
	this.pattern = pattern;
	this.forward = forward;
	this.startLocation = startLoc;
	
	if (forward && set != null && !set.isEmpty() && startLoc != null && 
			!set.getMinAddress().equals(startLoc.getAddress())) {
		throw new IllegalArgumentException("Start location and addressSet are inconsistent!");
	}
	if (!forward && set != null && !set.isEmpty() && startLoc != null && 
			!set.getMaxAddress().equals(startLoc.getAddress())) {
		throw new IllegalArgumentException("Start location and addressSet are inconsistent!");
	}

}
 
Example 5
Source File: CaptureFunctionDataTypesAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionContext context) {
	GTree gTree = (GTree) context.getContextObject();
	TreePath selectionPath = gTree.getSelectionPath();
	ArchiveNode node = (ArchiveNode) selectionPath.getLastPathComponent();
	if (!node.getArchive().isModifiable()) {
		informNotModifiable(node);
		return;
	}

	Program program = plugin.getProgram();
	AddressSetView currentSelection = plugin.getCurrentSelection();
	if (currentSelection == null || currentSelection.isEmpty()) {
		currentSelection = program.getMemory();
	}
	final DataTypeManager manager = node.getArchive().getDataTypeManager();
	final PluginTool tool = plugin.getTool();
	CaptureFunctionDataTypesCmd cmd =
		new CaptureFunctionDataTypesCmd(manager, currentSelection,
			new CaptureFunctionDataTypesListener() {

				@Override
				public void captureFunctionDataTypesCompleted(
						CaptureFunctionDataTypesCmd command) {
					tool.setStatusInfo("Captured function data types to \"" +
						manager.getName() + "\".");
				}
			});
	tool.executeBackgroundCommand(cmd, program);
}
 
Example 6
Source File: EmbeddedMediaAnalyzer.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 {

	Memory memory = program.getMemory();
	AddressSetView validMemorySet = memory.getLoadedAndInitializedAddressSet();
	AddressSetView searchSet = set.intersect(validMemorySet);
	if (searchSet.isEmpty()) {
		return false;  // no valid addresses to search
	}

	MemoryBytePatternSearcher searcher = new MemoryBytePatternSearcher("Embedded Media");

	List<Address> foundMedia = new ArrayList<>();

	addByteSearchPattern(searcher, program, foundMedia, new GifDataType(), "GIF 87",
		GifDataType.MAGIC_87, GifDataType.GIFMASK);

	addByteSearchPattern(searcher, program, foundMedia, new GifDataType(), "GIF 89",
		GifDataType.MAGIC_89, GifDataType.GIFMASK);

	addByteSearchPattern(searcher, program, foundMedia, new PngDataType(), "PNG",
		PngDataType.MAGIC, PngDataType.MASK);

	addByteSearchPattern(searcher, program, foundMedia, new JPEGDataType(), "JPEG",
		JPEGDataType.MAGIC, JPEGDataType.MAGIC_MASK);

	addByteSearchPattern(searcher, program, foundMedia, new WAVEDataType(), "WAVE",
		WAVEDataType.MAGIC, WAVEDataType.MAGIC_MASK);

	addByteSearchPattern(searcher, program, foundMedia, new AUDataType(), "AU",
		AUDataType.MAGIC, AUDataType.MAGIC_MASK);

	addByteSearchPattern(searcher, program, foundMedia, new AIFFDataType(), "AIFF",
		AIFFDataType.MAGIC, AIFFDataType.MAGIC_MASK);

	searcher.search(program, searchSet, monitor);

	return foundMedia.size() > 0;
}
 
Example 7
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 8
Source File: MarkUnimplementedPcode.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
	if (currentProgram == null) {
		return;
	}
	AddressSetView set = currentSelection;
	if (set == null || set.isEmpty()) {
		set = currentProgram.getMemory().getExecuteSet();
	}

	Disassembler.clearUnimplementedPcodeWarnings(currentProgram, set, monitor);

	int completed = 0;
	monitor.initialize(set.getNumAddresses());

	InstructionIterator instructions = currentProgram.getListing().getInstructions(set, true);
	while (instructions.hasNext()) {
		monitor.checkCanceled();
		Instruction instr = instructions.next();

		PcodeOp[] pcode = instr.getPcode();
		if (pcode != null && pcode.length == 1 &&
			pcode[0].getOpcode() == PcodeOp.UNIMPLEMENTED) {
			markUnimplementedPcode(instr);
		}

		completed += instr.getLength();
		if ((completed % 1000) == 0) {
			monitor.setProgress(completed);
		}
	}

}
 
Example 9
Source File: MarkCallOtherPcode.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
	if (currentProgram == null) {
		return;
	}
	AddressSetView set = currentSelection;
	if (set == null || set.isEmpty()) {
		set = currentProgram.getMemory().getExecuteSet();
	}

	Disassembler.clearUnimplementedPcodeWarnings(currentProgram, set, monitor);

	int completed = 0;
	monitor.initialize(set.getNumAddresses());

	InstructionIterator instructions = currentProgram.getListing().getInstructions(set, true);
	while (instructions.hasNext()) {
		monitor.checkCanceled();
		Instruction instr = instructions.next();

		PcodeOp[] pcode = instr.getPcode();

		for (int i = 0; i < pcode.length; i++) {
			if (pcode[i].getOpcode() == PcodeOp.CALLOTHER) {
				markCallOtherPcode(instr, pcode[i]);
			}
		}

		completed += instr.getLength();
		if ((completed % 1000) == 0) {
			monitor.setProgress(completed);
		}
	}

}
 
Example 10
Source File: AddressSetOptionsPanel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void enterPanel(WizardState<VTWizardStateKey> state) {
	@SuppressWarnings("unchecked")
	List<VTProgramCorrelatorFactory> list = (List<VTProgramCorrelatorFactory>) state.get(
		VTWizardStateKey.PROGRAM_CORRELATOR_FACTORY_LIST);

	Boolean value = (Boolean) state.get(VTWizardStateKey.EXCLUDE_ACCEPTED_MATCHES);
	if (value != null) {
		excludeCheckbox.setSelected(value.booleanValue());
	}
	value = (Boolean) state.get(VTWizardStateKey.SHOW_ADDRESS_SET_PANELS);
	if (value != null) {
		showAddressSetPanelsCheckbox.setSelected(value.booleanValue());
	}
	else {
		AddressSetView sourceSelection =
			(AddressSetView) state.get(VTWizardStateKey.SOURCE_SELECTION);
		AddressSetView destinationSelection =
			(AddressSetView) state.get(VTWizardStateKey.DESTINATION_SELECTION);
		boolean somethingSelected = (sourceSelection != null && !sourceSelection.isEmpty()) ||
			(destinationSelection != null && !destinationSelection.isEmpty());
		showAddressSetPanelsCheckbox.setSelected(somethingSelected);
	}

	if (allowRestrictions(list)) {
		excludeCheckbox.setEnabled(true);
	}
	else {
		excludeCheckbox.setSelected(false);
		excludeCheckbox.setEnabled(false);
	}

}
 
Example 11
Source File: MipsR5900AddressAnalyzer.java    From ghidra-emotionengine with Apache License 2.0 4 votes vote down vote up
/**
 * Check for a global GP register symbol or discovered symbol
 * @param set
 */
private void checkForGlobalGP(Program program, AddressSetView set, TaskMonitor monitor) {
	// don't want to check for it
	if (!discoverGlobalGPSetting) {
		return;
	}

	// TODO: Use gp_value provided by MIPS .reginfo or dynamic attributes - check for Elf loader symbol
	// see MIPS_ElfExtension.MIPS_GP_VALUE_SYMBOL
	Symbol symbol = SymbolUtilities.getLabelOrFunctionSymbol(program, "_mips_gp_value",
		err -> Msg.error(this, err));
	if (symbol != null) {
		gp_assumption_value = symbol.getAddress();
		return;
	}

	if (set != null && !set.isEmpty()) {
		// if GP is already Set, don't go looking for a value.
		AddressRangeIterator registerValueAddressRanges =
			program.getProgramContext().getRegisterValueAddressRanges(gp);
		while (registerValueAddressRanges.hasNext()) {
			// but set it so we know if the value we are assuming actually changes
			AddressRange next = registerValueAddressRanges.next();
			if (set.contains(next.getMinAddress(), next.getMaxAddress())) {
				RegisterValue registerValue =
					program.getProgramContext().getRegisterValue(gp, next.getMinAddress());
				gp_assumption_value = next.getMinAddress().getNewAddress(
					registerValue.getUnsignedValue().longValue());
				return;
			}
		}
	}

	// look for the global _gp variable set by ELF binaries

	symbol =
		SymbolUtilities.getLabelOrFunctionSymbol(program, "_gp", err -> Msg.error(this, err));
	if (symbol == null) {
		symbol = SymbolUtilities.getLabelOrFunctionSymbol(program, "_GP",
			err -> Msg.error(this, err));
	}

	if (symbol != null) {
		gp_assumption_value = symbol.getAddress();
	}

	// look for any setting of _gp_# variables
	Symbol s1 =
		SymbolUtilities.getLabelOrFunctionSymbol(program, "_gp_1", err -> Msg.error(this, err));
	if (s1 == null) {
		return;
	}
	// if we found a _gp symbol we set, and there is a global symbol, something is amiss
	if (gp_assumption_value != null && s1.getAddress().equals(gp_assumption_value)) {
		gp_assumption_value = null;
		return;
	}
	Symbol s2 =
		SymbolUtilities.getLabelOrFunctionSymbol(program, "_gp_2", err -> Msg.error(this, err));
	if (s2 == null) {
		// if there is only 1, assume can use the value for now
		gp_assumption_value = s1.getAddress();
	}
	return;
}
 
Example 12
Source File: PseudoDisassembler.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private boolean checkPseudoBody(Address entry, AddressSet body, AddressSet starts,
		boolean allowExistingInstructions, boolean didCallValidSubroutine) {

	if (program == null) {
		return true;
	}

	// check that body does not wander into non-executable memory
	AddressSetView execSet = memory.getExecuteSet();
	if (respectExecuteFlag && !execSet.isEmpty() && !execSet.contains(body)) {
		return false;
	}

	// check that the body traversed to a terminal does not
	//   have any anomolies in it.
	//   Existing Instructions/Data
	if (program.getListing().getDefinedData(body, true).hasNext()) {
		return false;
	}

	boolean canHaveOffcutEntry = hasLowBitCodeModeInAddrValues(program);
	AddressSet strictlyBody = body.subtract(starts);
	if (canHaveOffcutEntry) {
		strictlyBody.deleteRange(entry, entry.add(1));
	}
	AddressIterator addrIter =
		program.getReferenceManager().getReferenceDestinationIterator(strictlyBody, true);
	if (addrIter.hasNext()) {
		return false;  // don't allow offcut references
	}

	// if existing instructions are allowed,
	//    don't worry about multiple entry points either.
	if (allowExistingInstructions) {
		return true;
	}

	if (program.getListing().getInstructions(body, true).hasNext()) {
		return false;
	}

	// don't allow one instruction
	if (!didCallValidSubroutine && starts.getMinAddress().equals(starts.getMaxAddress())) {
		return false;
	}

	// if there are any references internally, that isn't the entry point
	//  it is a bady subroutine.
	AddressIterator iter;
	iter = program.getReferenceManager().getReferenceDestinationIterator(body, true);
	while (iter.hasNext()) {
		Address toAddr = iter.next();
		if (!toAddr.equals(entry)) {
			if (entry.add(1).equals(toAddr) && hasLowBitCodeModeInAddrValues(program)) {
				continue;
			}
			return false;
		}
	}
	return true;
}
 
Example 13
Source File: GhidraScript.java    From ghidra with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the selection state to the given address set.
 * <p>
 * The actual behavior of the method depends on your environment, which can be GUI or
 * headless:
 * <ol>
 * 		<li>In the GUI environment this method will set the {@link #currentSelection}
 * 			variable to the given value, update the {@link GhidraState}'s selection
 * 			variable, <b>and</b> will set the Tool's selection to the given value.</li>
 * 		<li>In the headless environment this method will set the {@link #currentSelection}
 * 			variable to the given value and update the GhidraState's selection variable.</li>
 * </ol>
 * <p>
 *
 * @param addressSet the set of addresses to include in the selection.  If this value is null,
 * the current selection will be cleared and the variables set to null.
 */
public void setCurrentSelection(AddressSetView addressSet) {
	if (addressSet == null || addressSet.isEmpty()) {
		this.currentSelection = null;
	}
	else {
		this.currentSelection = new ProgramSelection(addressSet);
	}
	state.setCurrentSelection(currentSelection);
}
 
Example 14
Source File: GhidraScript.java    From ghidra with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the highlight state to the given address set.
 * <p>
 * The actual behavior of the method depends on your environment, which can be GUI or
 * headless:
 * <ol>
 * 		<li>In the GUI environment this method will set the {@link #currentHighlight}
 * 			variable to the given value, update the {@link GhidraState}'s highlight variable,
 * 			<b>and</b> will set the Tool's highlight to the given value.</li>
 * 		<li>In the headless environment this method will set the {@link #currentHighlight}
 * 			variable to	the given value and update the GhidraState's highlight variable.</li>
 * </ol>
 * <p>
 *
 * @param addressSet the set of addresses to include in the highlight.  If this value is null,
 * the current highlight will be cleared and the variables set to null.
 */
public void setCurrentHighlight(AddressSetView addressSet) {
	if (addressSet == null || addressSet.isEmpty()) {
		this.currentHighlight = null;
	}
	else {
		this.currentHighlight = new ProgramSelection(addressSet);
	}
	state.setCurrentHighlight(currentHighlight);
}