Java Code Examples for ghidra.program.model.lang.Register#getParentRegister()

The following examples show how to use ghidra.program.model.lang.Register#getParentRegister() . 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: CreateThunkFunctionCmd.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the setRegisters contains the varnode or any of its parents.
 * If a parent register has been set, then this varnode is set
 */
private static boolean containsRegister(Program program, HashSet<Varnode> setRegisters,
		Varnode regVarnode) {
	if (setRegisters.contains(regVarnode)) {
		return true;
	}
	// check the parent varnode
	Register register = program.getRegister(regVarnode);
	Register parentRegister = register.getParentRegister();
	if (parentRegister == null) {
		return false;
	}
	Varnode parentVarnode =
		new Varnode(parentRegister.getAddress(), parentRegister.getBitLength() / 8);
	return setRegisters.contains(parentVarnode);
}
 
Example 2
Source File: ResultsState.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private boolean addRegister(Register reg, List<Register> regList) {
	// Ignore duplicate / reconcile related register
	for (int i = 0; i < regList.size(); i++) {
		Register existingReg = regList.get(i);
		Register parentReg = reg.getParentRegister();
		if (existingReg == reg || existingReg == parentReg ||
			existingReg == reg.getBaseRegister()) {
			return false;
		}
		if (parentReg != null && existingReg.getParentRegister() == parentReg) {
			regList.set(i, parentReg);
			return true;
		}
	}
	regList.add(reg);
	return true;
}
 
Example 3
Source File: VariableImpl.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Varnode expandVarnode(Varnode varnode, int sizeIncrease, VariableStorage curStorage,
		int newSize, DataType type) throws InvalidInputException {

	Address addr = varnode.getAddress();
	if (addr.isStackAddress()) {
		return resizeStackVarnode(varnode, varnode.getSize() + sizeIncrease, curStorage,
			newSize, type);
	}
	int size = varnode.getSize() + sizeIncrease;
	boolean bigEndian = program.getMemory().isBigEndian();
	Register reg = program.getRegister(varnode);
	Address vnAddr = varnode.getAddress();
	if (reg != null) {
		// Register expansion
		Register newReg = reg;
		while ((newReg.getMinimumByteSize() < size)) {
			newReg = newReg.getParentRegister();
			if (newReg == null) {
				throw new InvalidInputException("Current storage can't be expanded to " +
					newSize + " bytes: " + curStorage.toString());
			}
		}
		if (bigEndian) {
			vnAddr = vnAddr.add(newReg.getMinimumByteSize() - size);
			return new Varnode(vnAddr, size);
		}
	}
	boolean complexDt = (type instanceof Composite) || (type instanceof Array);
	if (bigEndian && !complexDt) {
		return new Varnode(vnAddr.subtract(sizeIncrease), size);
	}
	return new Varnode(vnAddr, size);
}
 
Example 4
Source File: ListingHighlightProvider.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private List<String> gatherRegisterNames(List<String> names, Register register) {

		// get the parents
		Register parent = register.getParentRegister();
		while (parent != null) {
			names.add(parent.getName());
			parent = parent.getParentRegister();
		}

		// now the register and its children
		accumulateSubRegisters(names, register);

		return names;
	}
 
Example 5
Source File: ListingHighlightProvider.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Set<Register> getRegisterSet(Register reg) {
	Set<Register> regSet = new HashSet<Register>();
	regSet.add(reg);
	Register r = reg.getParentRegister();
	while (r != null) {
		regSet.add(r);
		r = r.getParentRegister();
	}
	addChildren(reg, regSet);
	return regSet;
}
 
Example 6
Source File: ProgramDiff.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/** Gets the addresses where the Program Context (register bits) differ
 * between two programs.
 *
 * @param addressSet the addresses to check for differences.
 * The addresses in this address set should be derived from program1.
 * @param monitor the task monitor for indicating the progress of
 * determining differences. This monitor reports the progress to the user.
 *
 * @return the addresses of code units where the program context (register
 * bits values) differs.
 * The addresses in this address set are derived from program1.
 *
 * @throws ProgramConflictException if the two programs are not comparable since registers differ.
 * @throws CancelledException if the user cancelled the Diff.
 */
private AddressSet getProgramContextDifferences(AddressSetView addressSet, TaskMonitor monitor)
		throws ProgramConflictException, CancelledException {
	AddressSet differences = new AddressSet();
	ProgramContext pc1 = program1.getProgramContext();
	ProgramContext pc2 = program2.getProgramContext();
	String[] names1 = pc1.getRegisterNames();
	String[] names2 = pc2.getRegisterNames();
	Arrays.sort(names1);
	Arrays.sort(names2);
	if (!Arrays.equals(names1, names2)) {
		throw new ProgramConflictException(
			"Program Context Registers don't match between the programs.");
	}

	// Check each address range from the address set for differences.
	AddressSet inCommon = pgmMemComp.getAddressesInCommon();
	addressSet = (addressSet != null) ? inCommon.intersect(addressSet) : inCommon;

	for (String element : names1) {
		monitor.checkCanceled();
		Register rb1 = pc1.getRegister(element);
		Register rb2 = pc2.getRegister(element);
		if (rb1.isProcessorContext() || rb2.isProcessorContext()) {
			continue; // context handled with CodeUnit differencing
		}
		Register p1 = rb1.getParentRegister();
		Register p2 = rb2.getParentRegister();
		if (p1 != null && p2 != null && p1.getName().equals(p2.getName())) {
			continue;
		}

		getProgramContextDifferences(pc1, rb1, pc2, rb2, addressSet, differences, monitor);
	}
	return differences;
}
 
Example 7
Source File: ResultsState.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private boolean containsRegister(Register reg, List<Register> regList) {
	for (Register existingReg : regList) {
		Register parentReg = reg.getParentRegister();
		if (existingReg == reg || existingReg == parentReg ||
			existingReg == reg.getBaseRegister()) {
			return true;
		}
		if (parentReg != null && existingReg.getParentRegister() == parentReg) {
			return false;
		}
	}
	return false;
}
 
Example 8
Source File: RegisterFieldFactoryTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterField() throws ContextChangeException {

	FunctionIterator iter =
		program.getFunctionManager().getFunctions(program.getMinAddress(), true);
	Function function = iter.next();
	Address entry = function.getEntryPoint();
	Address end = function.getBody().getMaxAddress();

	ProgramContext pc = program.getProgramContext();
	List<Register> nonContextRegs = getNonContextRegisters(pc);

	int transactionID = program.startTransaction("test");
	int count = 0;
	try {
		for (Register register : nonContextRegs) {
			pc.setValue(register, entry, end, BigInteger.valueOf(5));
			if (register.getParentRegister() == null) {
				++count;
			}
		}
	}
	finally {
		program.endTransaction(transactionID, true);
	}
	program.flushEvents();
	waitForPostedSwingRunnables();
	cb.updateNow();

	assertTrue(cb.goToField(entry, RegisterFieldFactory.FIELD_NAME, 0, 0, 0));

	ListingTextField tf = (ListingTextField) cb.getCurrentField();
	assertEquals(count, tf.getNumRows());
}
 
Example 9
Source File: RegisterFieldFactoryTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testSubsetRegisterField() throws ContextChangeException {
	FunctionIterator iter =
		program.getFunctionManager().getFunctions(program.getMinAddress(), true);
	Function function = iter.next();
	Address entry = function.getEntryPoint();

	ProgramContext pc = program.getProgramContext();
	List<Register> regs = getNonContextRegisters(pc);

	int count = 0;
	int transactionID = program.startTransaction("test");
	try {
		for (int i = 0; i < regs.size(); i++) {
			if (i % 2 == 0) {
				pc.setValue(regs.get(i), entry, entry, BigInteger.valueOf(i));
			}
		}
		for (int i = 0; i < regs.size(); i++) {
			Register reg = regs.get(i);
			RegisterValue value = pc.getNonDefaultValue(reg, entry);
			Register parent = reg.getParentRegister();
			if (value != null && value.getSignedValue() != null &&
				(parent == null || !pc.getNonDefaultValue(parent, entry).hasValue())) {
				++count;
			}
		}
	}
	finally {
		program.endTransaction(transactionID, true);
	}
	program.flushEvents();
	waitForPostedSwingRunnables();
	cb.updateNow();

	assertTrue(cb.goToField(entry, RegisterFieldFactory.FIELD_NAME, 0, 0, 0));

	ListingTextField tf = (ListingTextField) cb.getCurrentField();
	assertEquals(count, tf.getNumRows());

}