Java Code Examples for ghidra.program.model.address.Address#NO_ADDRESS

The following examples show how to use ghidra.program.model.address.Address#NO_ADDRESS . 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: EditableParameterAddress.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
		public Address getAddress() throws InvalidInputException {
			int selectedIndex = jList.getSelectedIndex();
			if (selectedIndex == -1) {
				throw new InvalidInputException("No list item was selected.");
			}
			if (selectedIndex == 0) {
				return Address.NO_ADDRESS; // "No Address" was selected in the list.
			}
			final Parameter[] parameters = function.getParameters();
			Parameter parameter = null;
			if (selectedIndex >= 1 && selectedIndex <= parameters.length) {
				parameter = parameters[selectedIndex - 1];
			}
//			Parameter parameter = (Parameter) jList.getSelectedValue();
			if (parameter == null) {
				return null;
			}
			Address storageAddress = parameter.getMinAddress();
			if (storageAddress == null) {
				return null;
			}
			parameterAddress = storageAddress;
			return parameterAddress;
		}
 
Example 2
Source File: ApplyMatchTask.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void applyMarkupItem(VTMarkupItem item) throws VersionTrackingApplyException {

		// Markup with no destination address is currently the only incomplete item.
		Address destinationAddress = item.getDestinationAddress();
		if (destinationAddress == null || destinationAddress == Address.NO_ADDRESS) {
			if (ignoreIncompleteItem) {
				item.setConsidered(VTMarkupItemConsideredStatus.IGNORE_DONT_CARE);
			}
			return;
		}

		VTMarkupType markupType = item.getMarkupType();
		VTMarkupItemApplyActionType applyAction = markupType.getApplyAction(applyOptions);
		if (applyAction == null) {
			// The default action is an excluded, "Do Not Apply", item.
			if (item.canApply()) {
				if (ignoreExcludedItem) {
					item.setConsidered(VTMarkupItemConsideredStatus.IGNORE_DONT_CARE);
				}
			}
			return;
		}

		item.apply(applyAction, applyOptions);
	}
 
Example 3
Source File: AbstractMarkupItemAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isEnabledForContext(ActionContext context) {
	List<VTMarkupItem> markupItems = controller.getMarkupItems(context);

	if (markupItems.size() == 0) {
		return false;
	}

	for (VTMarkupItem markupItem : markupItems) {

		if (!markupItem.canApply()) {
			return false;
		}

		if (!markupItem.supportsApplyAction(getActionType())) {
			return false; // disabled if any of the items do not support our action type
		}

		Address address = markupItem.getDestinationAddress();
		if (address == null || address == Address.NO_ADDRESS) {
			return false; // disabled if we don't have an address to apply to
		}
	}

	return true;
}
 
Example 4
Source File: VTMarkupType.java    From ghidra with Apache License 2.0 6 votes vote down vote up
protected Stringable getOriginalDestinationValueForAppliedMarkupOfThisType(
		VTAssociation association, Address destinationAddress, TaskMonitor monitor)
		throws CancelledException {

	if ((destinationAddress == null) || destinationAddress == Address.NO_ADDRESS) {
		return null;
	}

	Collection<VTMarkupItem> markupItems = association.getMarkupItems(monitor);
	for (VTMarkupItem markupItem : markupItems) {
		if ((markupItem.getMarkupType() == this) && markupItem.canUnapply()) {
			Address itemDestination = markupItem.getDestinationAddress();
			if (destinationAddress.equals(itemDestination)) {
				// Return the original destination value for the first applied 
				// markup item we find of this type at this address.
				return markupItem.getOriginalDestinationValue();
			}
		}
	}

	return null;
}
 
Example 5
Source File: VTDualListingHighlightProvider.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void updateMap(VTMarkupItem markupItem) {
	VTMarkupType markupType = markupItem.getMarkupType();
	Address address;
	if (isSource) {
		address = markupItem.getSourceAddress();
	}
	else {
		address = markupItem.getDestinationAddress();
	}
	if (address == null || address == Address.NO_ADDRESS) {
		return;
	}

	HashMap<VTMarkupType, VTMarkupItem> typeMap = map.get(address);
	if (typeMap == null) {
		typeMap = new HashMap<VTMarkupType, VTMarkupItem>();
		map.put(address, typeMap);
	}

	typeMap.put(markupType, markupItem);
}
 
Example 6
Source File: MarkupItemStorageDB.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public MarkupItemStorage setDestinationAddress(Address destinationAddress, String addressSource) {
	if (destinationAddress == null) {
		destinationAddress = Address.NO_ADDRESS;
	}

	Program destinationProgram = session.getDestinationProgram();

	AddressMap addressMap = destinationProgram.getAddressMap();
	long addressID = addressMap.getKey(destinationAddress, false);
	record.setLongValue(DESTINATION_ADDRESS_COL.column(), addressID);

	record.setString(ADDRESS_SOURCE_COL.column(), addressSource);
	associationManager.updateMarkupRecord(record);
	return this;
}
 
Example 7
Source File: FunctionSignatureMarkupType.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private FunctionReturnTypeFieldLocation getFunctionReturnTypeLocation(
		VTAssociation association, Address address, boolean isSource) {

	if (address == null || address == Address.NO_ADDRESS) {
		return null; // Return null when there is no destination address.
	}

	Program program;
	if (isSource) {
		program = getSourceProgram(association);
	}
	else {
		program = getDestinationProgram(association);
	}

	Function function = program.getFunctionManager().getFunctionContaining(address);
	if (function == null) {
		return null;
	}
	Address entryAddress = function.getEntryPoint();
	Stringable value =
		isSource ? getSourceValue(association, address) : getCurrentDestinationValue(
			association, address);
	String displayString = (value != null) ? value.getDisplayString() : null;
	return new FunctionReturnTypeFieldLocation(program, entryAddress, displayString);
}
 
Example 8
Source File: EolCommentMarkupType.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
protected ProgramLocation getLocation(VTAssociation association, Address address,
		boolean isSource) {
	if (address == null || address == Address.NO_ADDRESS) {
		return null;
	}

	Program program =
		isSource ? getSourceProgram(association) : getDestinationProgram(association);
	return new EolCommentFieldLocation(program, address, null, null, 0, 0, 0);
}
 
Example 9
Source File: VarnodeAST.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public Address getPCAddress() {
	if (bInput) {
		return Address.NO_ADDRESS;
	}
	if (def != null) {
		return def.getSeqnum().getTarget();
	}
	if (descend.size() == 1) {
		return descend.get(0).getSeqnum().getTarget();
	}
	return Address.NO_ADDRESS;
}
 
Example 10
Source File: PlateCommentMarkupType.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
protected ProgramLocation getLocation(VTAssociation association, Address address,
		boolean isSource) {
	if (address == null || address == Address.NO_ADDRESS) {
		return null;
	}

	Program program =
		isSource ? getSourceProgram(association) : getDestinationProgram(association);
	return new PlateFieldLocation(program, address, null, 0, 0, null, -1);
}
 
Example 11
Source File: ReferenceAddressPair.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ReferenceAddressPair(Address source, Address destination) {
	if (source == null) {
		source = Address.NO_ADDRESS;
	}
	if (destination == null) {
		destination = Address.NO_ADDRESS;
	}
	this.source = source;
	this.destination = destination;
}
 
Example 12
Source File: FindPotentialDecompilerProblems.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the address of first function called by {@code func}.  That is, the returned {@link Address}
 * is the target of the call instruction with the least address within the body of {@code func}. 
 * @param func the {@link Function} to search for calls
 * @return the {@link Address} of the first called function, or {@code Address.NO_ADDRESS} if
 * no calls are found.
 */
private Address getFirstCalledFunction(Function func) {

	//could be issues if func's body has addresses that are before the entry point of
	//func - see the comment in getFirstFuncWithVar
	ReferenceIterator refIter =
		func.getProgram().getReferenceManager().getReferenceIterator(func.getEntryPoint());

	Address maxAddr = func.getBody().getMaxAddress();

	for (Reference ref : CollectionUtils.asIterable(refIter)) {
		// check whether we are at an address not in the function
		// only necessary in case func consists of non-contiguous blocks
		// TODO: handle tail-call elimination
		if (!func.getBody().contains(ref.getFromAddress())) {
			continue;
		}

		// return the first call for the function
		if (isValidCallReference(ref)) {
			return ref.getToAddress();
		}
		// The references are sorted by their "from" addresses, so if this condition is true, 
		// we've searched all the references from the body of func and haven't found anything.
		// So, stop looking.
		if (ref.getFromAddress().compareTo(maxAddr) > 0) {
			return Address.NO_ADDRESS;
		}
	}
	//in case there are no references with "from" addresses after the body of func
	return Address.NO_ADDRESS;
}
 
Example 13
Source File: PreCommentMarkupType.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
protected ProgramLocation getLocation(VTAssociation association, Address address,
		boolean isSource) {
	if (address == null || address == Address.NO_ADDRESS) {
		return null;
	}

	Program program =
		isSource ? getSourceProgram(association) : getDestinationProgram(association);
	return new CommentFieldLocation(program, address, null, null, CodeUnit.PRE_COMMENT, 0, 0);
}
 
Example 14
Source File: DisplayableListingAddress.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public String getDisplayString() {
	if (address == null || address == Address.NO_ADDRESS) {
		return NO_ADDRESS;
	}
	AddressBasedLocation location = new AddressBasedLocation(program, address);
	return location.toString();
}
 
Example 15
Source File: DisplayableListingOffset.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public String getDisplayString() {
	if (address == null || address == Address.NO_ADDRESS) {
		return NO_OFFSET;
	}
	return offsetAsBigInteger.toString();
}
 
Example 16
Source File: SymbolType.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isValidAddress(Program program, Address symbolAddress) {
	return symbolAddress == Address.NO_ADDRESS;
}
 
Example 17
Source File: DisplayableVariableOffset.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public String getDisplayString() {
	if (parameterAddress == null || parameterAddress == Address.NO_ADDRESS) {
		return NO_OFFSET;
	}
	return parameterAddress.toString();
}
 
Example 18
Source File: VTDualListingHighlightProvider.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private Color getMarkupBackgroundColor(int cursorTextOffset, VTMarkupItem vtMarkupItem,
		int startIndex, int endIndex) {
	Color highlightColor = null;
	Address sourceAddress = vtMarkupItem.getSourceAddress();
	Address destinationAddress = vtMarkupItem.getDestinationAddress();
	VTMarkupItemStatus status = vtMarkupItem.getStatus();
	if (status == VTMarkupItemStatus.DONT_CARE) {
		highlightColor = IGNORED_MARKUP_COLOR;
	}
	else if (status == VTMarkupItemStatus.DONT_KNOW) {
		highlightColor = IGNORED_MARKUP_COLOR;
	}
	else if (status == VTMarkupItemStatus.REJECTED) {
		highlightColor = REJECTED_MARKUP_COLOR;
	}
	else if (status == VTMarkupItemStatus.CONFLICT) {
		highlightColor = CONFLICT_MARKUP_COLOR;
	}
	else if (destinationAddress == null || destinationAddress == Address.NO_ADDRESS) {
		highlightColor = NO_ADDRESS_MARKUP_COLOR;
	}
	else if (status == VTMarkupItemStatus.UNAPPLIED) {
		highlightColor = UNAPPLIED_MARKUP_COLOR;
	}
	else if (status.isUnappliable()) {
		highlightColor = APPLIED_MARKUP_COLOR;
	}
	else if (status == VTMarkupItemStatus.FAILED_APPLY) {
		highlightColor = FAILED_MARKUP_COLOR;
	}
	else if (status == VTMarkupItemStatus.SAME) {
		highlightColor = SAME_MARKUP_COLOR;
	}
	else {
		return null;
	}
	if (currentMarkupItem != null) {
		VTMarkupType currentMarkupType = currentMarkupItem.getMarkupType();
		VTMarkupType markupType = vtMarkupItem.getMarkupType();
		Address currentSourceAddress = currentMarkupItem.getSourceAddress();
		boolean inCurrentMarkup =
			(currentMarkupType == markupType) && currentSourceAddress.equals(sourceAddress);
		if (inCurrentMarkup) {
			// Set the highlight color to be a bit darker than normal.  Color.darker() returns
			// a color that is too dark, so use this custom function instead.
			highlightColor = shade(highlightColor, 0.85);
		}
	}
	return highlightColor;
}
 
Example 19
Source File: FindPotentialDecompilerProblems.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the target of the first (in address order) call in the body of {@code func}
 * which takes {@code vn} as a parameter.
 * @param func {@link Function} whose body to search for calls
 * @param vn {@link Varnode} representing required parameter
 * @return entry point of first function called by {@code func} which uses {@code vn}
 * as a parameter, or {@code Address.NO_ADDRESS} if no such function found.
 */
private Address getFirstFuncWithVar(Function func, Varnode vn) {
	Address variableAddr = vn.getAddress();
	if (variableAddr == null) {
		return Address.NO_ADDRESS;
	}

	// Note: this handles some cases where functions consist of non-contiguous blocks,
	// but since we start at the entry point we might miss things if part of the body of
	// the function is before the entry point (in address order)
	ReferenceIterator refIter =
		func.getProgram().getReferenceManager().getReferenceIterator(func.getEntryPoint());

	Address maxAddr = func.getBody().getMaxAddress();

	// return the first call to a function which takes vn as an argument
	for (Reference ref : CollectionUtils.asIterable(refIter)) {
		// check whether we are at an address not in the function
		// only necessary in case func consists of non-contiguous blocks
		// TODO: handle tail-call elimination
		if (!func.getBody().contains(ref.getFromAddress())) {
			continue;
		}
		if (isValidCallReference(ref)) {
			Function calledFunc =
				currentProgram.getFunctionManager().getFunctionAt(ref.getToAddress());
			Parameter[] params = calledFunc.getParameters();
			for (Parameter param : params) {
				Address addr = param.getMinAddress();
				if (addr != null && addr.equals(variableAddr)) {
					return ref.getToAddress();
				}
			}
		}
		// The references are sorted by their "from" addresses, so if this condition is true, 
		// we've searched all the references from the body of func and haven't found anything.
		// So, stop looking.
		if (ref.getFromAddress().compareTo(maxAddr) > 0) {
			return Address.NO_ADDRESS;
		}
	}
	//in case there are no references with "from" addresses after the body of func
	return Address.NO_ADDRESS;
}
 
Example 20
Source File: AddressCorrelationTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Check that the expected comment markup was created and it has the expected destination address.
 * @param desiredCommentMarkupType the comment markup type we are checking
 * @param sourceAddressString the source address of the markup
 * @param comment the expected comment
 * @param destinationAddressString the expected destination address for the markup
 */
private void checkCommentMarkup(VTMarkupType desiredCommentMarkupType,
		String sourceAddressString, String comment, String destinationAddressString) {
	Address srcAddress = addr(sourceAddressString, sourceProgram);
	Address destAddress =
		destinationAddressString.equals("NO_ADDRESS") ? Address.NO_ADDRESS : addr(
			destinationAddressString, destinationProgram);

	Collection<VTMarkupItem> appliableMarkupItems =
		controller.getMatchInfo(testMatch).getAppliableMarkupItems(TaskMonitor.DUMMY); // Initialize the cache.

	for (VTMarkupItem vtMarkupItem : appliableMarkupItems) {
		VTMarkupType markupType = vtMarkupItem.getMarkupType();
		if (markupType != desiredCommentMarkupType) {
			continue; // Not the right markup type.
		}
		Address markupSrcAddress = vtMarkupItem.getSourceAddress();
		if (!markupSrcAddress.equals(srcAddress)) {
			continue; // Not the right source address.
		}

		// Check the comment.
		Stringable sourceValue = vtMarkupItem.getSourceValue();
		String displayString = sourceValue.getDisplayString();
		assertEquals(comment, displayString);

		// Check destination address
		Address markupDestAddress = vtMarkupItem.getDestinationAddress();
		if (markupDestAddress == null) {
			markupDestAddress = Address.NO_ADDRESS;
		}
		boolean isNoAddress =
			markupDestAddress == null || markupDestAddress == Address.NO_ADDRESS;
		if (destAddress == Address.NO_ADDRESS) {
			assertTrue("Unexpected destination address of NO_ADDRESS for " +
				vtMarkupItem.getMarkupType().getDisplayName() + " markup @ " +
				vtMarkupItem.getSourceAddress().toString() + ".", isNoAddress);
			return;
		}
		assertTrue("Unexpected destination address of " + markupDestAddress.toString() +
			" when expecting " + destAddress.toString() + " for " +
			vtMarkupItem.getMarkupType().getDisplayName() + " markup @ " +
			vtMarkupItem.getSourceAddress().toString() + ".",
			markupDestAddress.equals(destAddress));
		return;
	}
}