ghidra.program.model.symbol.SourceType Java Examples

The following examples show how to use ghidra.program.model.symbol.SourceType. 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: FunctionSignatureMarkupParameterName1Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testImportPriorityNameReplace_AnalysisSrc_ImportDest() throws Exception {
	useMatch("0x00411860", "0x00411830");

	setParameterName(sourceFunction, 0, "SrcAnalysisList", SourceType.ANALYSIS);
	setParameterName(destinationFunction, 0, "DestImportList", SourceType.IMPORTED);

	String sourceSignature = "void addPerson(Person * * SrcAnalysisList, char * name)";
	String originalDestinationSignature =
		"void FUN_00411830(int * DestImportList, char * param_2)";
	String appliedDestinationSignature = "void FUN_00411830(int * DestImportList, char * name)";
	SourcePriorityChoices parameterNamesChoice = SourcePriorityChoices.PRIORITY_REPLACE;

	applyAndUnapplyParameterNameMarkup(sourceSignature, originalDestinationSignature,
		appliedDestinationSignature, parameterNamesChoice, IMPORT_PRIORITY_HIGHEST, false);
}
 
Example #2
Source File: CodeBrowserScreenMovementTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Program buildProgram() throws Exception {
	ProgramBuilder builder = new ProgramBuilder("Test", ProgramBuilder._TOY);
	builder.createMemory(".text", "0x1001000", 0x6600);
	builder.createMemory(".data", "0x1008000", 0x600);
	builder.createMemory(".rsrc", "0x100a000", 0x5400);
	builder.createMemory(".bound_import.table", "0xf0000428", 0xa8);
	builder.createMemory(".debug_data", "0xf0001300", 0x1c);
	builder.applyDataType("f000130d", new DoubleDataType(), 1);
	builder.applyDataType("1001000", new Pointer32DataType(), 7);
	builder.disassemble("0x10036a2", 1);

	// for structure in structure test
	builder.createMemoryReference("1001012", NESTED_STRUCT_ADDR, RefType.DATA,
		SourceType.DEFAULT, 0);
	builder.createMemoryReference("1001013", "10070001", RefType.DATA, SourceType.DEFAULT, 0);
	builder.createMemoryReference("1001014", "10070002", RefType.DATA, SourceType.DEFAULT, 0);

	return builder.getProgram();
}
 
Example #3
Source File: FunctionSignatureMarkupParameterName1Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testImportPriorityNameReplace_ImportSrc_ImportDest_DoNotReplaceSamePriority()
		throws Exception {
	useMatch("0x00411860", "0x00411830");

	setParameterName(sourceFunction, 0, "SrcImportList", SourceType.IMPORTED);
	setParameterName(destinationFunction, 0, "DestImportList", SourceType.IMPORTED);

	String sourceSignature = "void addPerson(Person * * SrcImportList, char * name)";
	String originalDestinationSignature =
		"void FUN_00411830(int * DestImportList, char * param_2)";
	String appliedDestinationSignature = "void FUN_00411830(int * DestImportList, char * name)";
	SourcePriorityChoices parameterNamesChoice = SourcePriorityChoices.PRIORITY_REPLACE;

	applyAndUnapplyParameterNameMarkup(sourceSignature, originalDestinationSignature,
		appliedDestinationSignature, parameterNamesChoice, IMPORT_PRIORITY_HIGHEST, false);
}
 
Example #4
Source File: AbstractFunctionSignatureMarkupTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
protected void setReturnType(Function function, DataType returnType, SourceType source)
		throws InvalidInputException {
	Program program = function.getProgram();
	int transaction = -1;
	boolean commit = false;
	try {
		transaction =
			program.startTransaction("Test - Set Return Type: " + returnType.getName());
		function.setReturnType(returnType, source);
		commit = true;
	}
	finally {
		program.endTransaction(transaction, commit);
	}
	waitOnPossibleBackgroundProcessing();
}
 
Example #5
Source File: ProgramDiff3Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testFunctionRegParamDiff2() throws Exception {

	// different named registers as param_1

	int transactionID1 = p1.startTransaction("Test Transaction");
	Function function1 = p1.getFunctionManager().getFunctionAt(addr(p1, 0x1002cf5));
	function1.removeParameter(0);
	Register dr0Reg = p1.getRegister("DR0");
	Variable var1 = new ParameterImpl("example", new DWordDataType(), dr0Reg, p1);
	function1.removeParameter(0);
	function1.insertParameter(0, var1, SourceType.USER_DEFINED);
	p1.endTransaction(transactionID1, true);

	int transactionID2 = p2.startTransaction("Test Transaction");
	Function function2 = p2.getFunctionManager().getFunctionAt(addr(p2, 0x1002cf5));
	function2.removeParameter(0);
	dr0Reg = p2.getRegister("DR0");
	Variable var2 = new ParameterImpl("variable", new DWordDataType(), dr0Reg, p2);
	function2.removeParameter(0);
	function2.insertParameter(0, var2, SourceType.USER_DEFINED);
	p2.endTransaction(transactionID2, true);

	AddressSet expectedDiffs = new AddressSet(addr(0x1002cf5), addr(0x1002cf5));
	checkDiff(expectedDiffs, ProgramDiffFilter.FUNCTION_DIFFS);
}
 
Example #6
Source File: InstructionStasher.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public void restore() throws CodeUnitInsertionException {
	if (prototype == null) {
		return;
	}
	MemBuffer buf = new DumbMemBufferImpl(program.getMemory(), minAddress);
	ProcessorContext context =
		new ProgramProcessorContext(program.getProgramContext(), minAddress);
	program.getListing().createInstruction(minAddress, prototype, buf, context);

	for (Reference reference : referencesFrom) {
		if (reference.getSource() != SourceType.DEFAULT) {
			program.getReferenceManager().addReference(reference);
		}
	}

}
 
Example #7
Source File: FunctionSignatureMarkupParameterName2Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testUserPriorityNameReplace_ImportSrc_ImportDest_ReplaceSamePriority()
		throws Exception {
	useMatch("0x00411860", "0x00411830");

	setParameterName(sourceFunction, 0, "SrcImportList", SourceType.IMPORTED);
	setParameterName(destinationFunction, 0, "DestImportList", SourceType.IMPORTED);

	String sourceSignature = "void addPerson(Person * * SrcImportList, char * name)";
	String originalDestinationSignature =
		"void FUN_00411830(int * DestImportList, char * param_2)";
	String appliedDestinationSignature = "void FUN_00411830(int * SrcImportList, char * name)";
	SourcePriorityChoices parameterNamesChoice = SourcePriorityChoices.PRIORITY_REPLACE;

	applyAndUnapplyParameterNameMarkup(sourceSignature, originalDestinationSignature,
		appliedDestinationSignature, parameterNamesChoice, USER_PRIORITY_HIGHEST, true);
}
 
Example #8
Source File: CreateFunctionCmdTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateFunctionFindEntryBodyError() {
	int transactionID = program.startTransaction("Perform the TEST");

	CreateFunctionCmd createCmd = new CreateFunctionCmd("bob", addr(0x1003),
		new AddressSet(addr(0x1003), addr(0x1007)), SourceType.USER_DEFINED, true, true);
	boolean result = createCmd.applyTo(program);

	assertFalse("Create Function", result);

	Function func1000 = func(addr(0x1000));
	Function func1003 = func(addr(0x1003));

	assertNull("No function at 1000", func1000);
	assertNull("No function at 10003", func1003);

	program.endTransaction(transactionID, true);
}
 
Example #9
Source File: FunctionSignatureMarkupParameterName2Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testUserPriorityNameReplace_UserSrc_ImportDest() throws Exception {
	useMatch("0x00411860", "0x00411830");

	setParameterName(sourceFunction, 0, "SrcUserList", SourceType.USER_DEFINED);
	setParameterName(destinationFunction, 0, "DestImportList", SourceType.IMPORTED);

	String sourceSignature = "void addPerson(Person * * SrcUserList, char * name)";
	String originalDestinationSignature =
		"void FUN_00411830(int * DestImportList, char * param_2)";
	String appliedDestinationSignature = "void FUN_00411830(int * SrcUserList, char * name)";
	SourcePriorityChoices parameterNamesChoice = SourcePriorityChoices.PRIORITY_REPLACE;

	applyAndUnapplyParameterNameMarkup(sourceSignature, originalDestinationSignature,
		appliedDestinationSignature, parameterNamesChoice, USER_PRIORITY_HIGHEST, false);
}
 
Example #10
Source File: FunctionSignatureMarkupParameterName1Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testImportPriorityNameReplace_DefaultSrc_AnalysisDest() throws Exception {
	useMatch("0x00411860", "0x00411830");

	setParameterName(sourceFunction, 0, null, SourceType.DEFAULT);
	setParameterName(destinationFunction, 0, "DestAnalysisList", SourceType.ANALYSIS);

	String sourceSignature = "void addPerson(Person * * param_1, char * name)";
	String originalDestinationSignature =
		"void FUN_00411830(int * DestAnalysisList, char * param_2)";
	String appliedDestinationSignature =
		"void FUN_00411830(int * DestAnalysisList, char * name)";
	SourcePriorityChoices parameterNamesChoice = SourcePriorityChoices.PRIORITY_REPLACE;

	applyAndUnapplyParameterNameMarkup(sourceSignature, originalDestinationSignature,
		appliedDestinationSignature, parameterNamesChoice, IMPORT_PRIORITY_HIGHEST, false);
}
 
Example #11
Source File: CliMetadataRoot.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
		NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException,
		IOException, MemoryAccessException {
	Address start = program.getImageBase().add(getRva());
	try {
		program.getSymbolTable().createLabel(start, NAME, SourceType.ANALYSIS);
	}
	catch (InvalidInputException e) {
		Msg.warn(this, "Invalid symbol name: \"" + NAME + "\"");
	}

	// Markup streams.  Must markup Metadata stream last.
	for (CliStreamHeader header : streamHeaderMap.values()) {
		if (header != metadataHeader) {
			header.markup(program, isBinary, monitor, log, ntHeader);
		}
	}
	if (metadataHeader != null) {
		metadataHeader.markup(program, isBinary, monitor, log, ntHeader);
	}
}
 
Example #12
Source File: ProgramDiff3Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Test that ProgramDiff can determine that function local types are
 * different.
 */
@Test
public void testFunctionLocalTypeDiff() throws Exception {

	// 0x10059a3: in p1 local_8 is a Undefined, in p2 it's Pointer.

	int transactionID1 = p1.startTransaction("Test Transaction");
	Function function1 = p1.getFunctionManager().getFunctionAt(addr(p1, 0x10059a3));
	Variable[] localVariables = function1.getLocalVariables();
	assertEquals(5, localVariables.length);
	assertEquals(-0x8, localVariables[0].getStackOffset());
	localVariables[0].setDataType(DataType.DEFAULT, SourceType.DEFAULT);
	p1.endTransaction(transactionID1, true);

	int transactionID2 = p2.startTransaction("Test Transaction");
	Function function2 = p2.getFunctionManager().getFunctionAt(addr(p2, 0x10059a3));
	Variable[] localVariables2 = function2.getLocalVariables();
	assertEquals(5, localVariables2.length);
	assertEquals(-0x8, localVariables2[0].getStackOffset());
	localVariables2[0].setDataType(new PointerDataType(), SourceType.DEFAULT);
	p2.endTransaction(transactionID2, true);

	AddressSet expectedDiffs = new AddressSet(addr(0x10059a3), addr(0x10059a3));
	checkDiff(expectedDiffs, ProgramDiffFilter.FUNCTION_DIFFS);
}
 
Example #13
Source File: HCS12ConventionAnalyzer.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void setPrototypeModel(Program program, Instruction instr, String convention) {
	if (convention == null) {
		return;
	}

	Function func = program.getFunctionManager().getFunctionContaining(instr.getMinAddress());
	if (func == null) {
		return;
	}

	if (func.getSignatureSource() != SourceType.DEFAULT) {
		return;
	}

	try {
		func.setCallingConvention(convention);
	} catch (InvalidInputException e) {
		Msg.error(this, "Unexpected Exception: " + e.getMessage(), e);
	}
}
 
Example #14
Source File: FunctionSignatureMarkupParameterName1Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testImportPriorityNameReplace_DefaultSrc_ImportDest() throws Exception {
	useMatch("0x00411860", "0x00411830");

	setParameterName(sourceFunction, 0, null, SourceType.DEFAULT);
	setParameterName(destinationFunction, 0, "DestImportList", SourceType.IMPORTED);

	String sourceSignature = "void addPerson(Person * * param_1, char * name)";
	String originalDestinationSignature =
		"void FUN_00411830(int * DestImportList, char * param_2)";
	String appliedDestinationSignature = "void FUN_00411830(int * DestImportList, char * name)";
	SourcePriorityChoices parameterNamesChoice = SourcePriorityChoices.PRIORITY_REPLACE;

	applyAndUnapplyParameterNameMarkup(sourceSignature, originalDestinationSignature,
		appliedDestinationSignature, parameterNamesChoice, IMPORT_PRIORITY_HIGHEST, false);
}
 
Example #15
Source File: OperandFieldFactoryTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createOffcutFunctionReference(Function function, Address fromAddress) {

		Address entryPoint = function.getEntryPoint();
		Address oneByteOff = entryPoint.add(1);

		AddMemRefCmd addRefCmd = new AddMemRefCmd(fromAddress, oneByteOff,
			RefType.UNCONDITIONAL_CALL, SourceType.ANALYSIS, 0);

		RemoveAllReferencesCmd removeRefsCmd = new RemoveAllReferencesCmd(fromAddress);

		int ID = program.startTransaction("Test - Create Reference");
		try {
			removeRefsCmd.applyTo(program);
			addRefCmd.applyTo(program);
		}
		finally {
			program.endTransaction(ID, true);
		}

		program.flushEvents();
		waitForPostedSwingRunnables();
	}
 
Example #16
Source File: FunctionSignatureMarkupParameterName1Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testImportPriorityNameReplace_UserSrc_UserDest_DoNotReplaceSamePriority()
		throws Exception {
	useMatch("0x00411860", "0x00411830");

	setParameterName(sourceFunction, 0, "SrcUserList", SourceType.USER_DEFINED);
	setParameterName(destinationFunction, 0, "DestUserList", SourceType.USER_DEFINED);

	String sourceSignature = "void addPerson(Person * * SrcUserList, char * name)";
	String originalDestinationSignature =
		"void FUN_00411830(int * DestUserList, char * param_2)";
	String appliedDestinationSignature = "void FUN_00411830(int * DestUserList, char * name)";
	SourcePriorityChoices parameterNamesChoice = SourcePriorityChoices.PRIORITY_REPLACE;

	applyAndUnapplyParameterNameMarkup(sourceSignature, originalDestinationSignature,
		appliedDestinationSignature, parameterNamesChoice, IMPORT_PRIORITY_HIGHEST, false);
}
 
Example #17
Source File: MicrosoftDemanglerTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testArrayVariable() throws Exception { // NullPointerException
	String mangled = "?Te@NS1@BobsStuff@@0QAY0BAA@$$CBIA";

	MicrosoftDemangler demangler = new MicrosoftDemangler();
	DemangledObject demangledObject = demangler.demangle(mangled);

	int txID = program.startTransaction("Test");

	SymbolTable st = program.getSymbolTable();
	st.createLabel(addr("01001000"), mangled, SourceType.ANALYSIS);

	DemanglerOptions options = new DemanglerOptions();
	demangledObject.applyTo(program, addr("01001000"), options, TaskMonitor.DUMMY);
	program.endTransaction(txID, false);
}
 
Example #18
Source File: AddressCorrelationTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Program buildProgram1(String name) throws Exception {
	ProgramBuilder builder = new ProgramBuilder(name, ProgramBuilder._X86);
	Program p = builder.getProgram();
	builder.createMemory("text", "0x00401000", 0x100);
	builder.setBytes("0x00401000", "8b ff 55 8b ec c3");
	builder.disassemble("0x00401000", 6);
	Function function = builder.createFunction("0x00401000");
	int txID = p.startTransaction("Setting Function Name");
	boolean commit = false;
	try {
		function.setName("MyFunctionAB", SourceType.USER_DEFINED);
		Listing listing = p.getListing();
		CodeUnit cu = listing.getCodeUnitAt(function.getEntryPoint());
		cu.setComment(CodeUnit.EOL_COMMENT, "A sample end of line comment");
		commit = true;
	}
	finally {
		p.endTransaction(txID, commit);
	}
	return p;
}
 
Example #19
Source File: CreateFunctionCmdTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateOverlapping() {
	int transactionID = program.startTransaction("Perform the TEST");

	CreateFunctionCmd createCmd = new CreateFunctionCmd(addr(0x1000));
	createCmd.applyTo(program);

	createCmd =
		new CreateFunctionCmd("bob", addr(0x1003), null, SourceType.USER_DEFINED, false, true);
	boolean result = createCmd.applyTo(program);

	assertTrue("Create Function", result);

	Function func1003 = func(addr(0x1003));
	assertEquals("Created function body length", 4, func1003.getBody().getNumAddresses());

	Function func1000 = func(addr(0x1000));
	assertEquals("Function start", func1000.getEntryPoint(), addr(0x1000));
	assertEquals("Created function body length", 3, func1000.getBody().getNumAddresses());

	func1003 = func(addr(0x1003));
	assertEquals("Created function body length", 4, func1003.getBody().getNumAddresses());

	program.endTransaction(transactionID, true);
}
 
Example #20
Source File: XrefViewerTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private ProgramDB buildProgram() throws Exception {
	builder = new ProgramBuilder("notepad", ProgramBuilder._TOY, this);

	builder.createMemory(".text", "0x1001000", 0x6600);
	builder.createEntryPoint("1001000", "entrypoint");
	builder.createEmptyFunction(null, "1001005", 40, null);
	builder.setBytes("1001005", "ff 74 24 04", true);
	builder.setBytes("10010a0", "ff 15 d4 10 00 01", true);

	builder.createMemoryReference("1001005", "1001007", RefType.DATA, SourceType.DEFAULT, 0);
	builder.createMemoryReference("1001009", "1001007", RefType.DATA, SourceType.DEFAULT, 0);
	builder.createMemoryReadReference("1001009", "1001005");

	// structure at 100100b
	builder.createMemoryReference("1001005", "100100f", RefType.DATA, SourceType.DEFAULT, 0);
	builder.createMemoryReference("1001002", "100100f", RefType.DATA, SourceType.DEFAULT, 0);

	return builder.getProgram();
}
 
Example #21
Source File: OverrideFunctionPrototypesOnAcceptedMatchesScript.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private void transfer(Function sourceFunc, Function destFunc) throws InvalidInputException,
		DuplicateNameException {

	// FIXME: this is just a skeleton, I am dumb, failed all of RE, etc.
	// please make this do what you really want

	destFunc.setReturnType(sourceFunc.getReturnType(), sourceFunc.getSignatureSource());
	destFunc.setName(sourceFunc.getName(), SourceType.USER_DEFINED);
	destFunc.setCallingConvention(sourceFunc.getCallingConventionName());
	int parameterCount = destFunc.getParameterCount();
	for (int ii = parameterCount - 1; ii >= 0; --ii) {
		// TODO: Fix deprecated method call
		destFunc.removeParameter(ii);
	}
	parameterCount = sourceFunc.getParameterCount();
	for (int ii = 0; ii < parameterCount; ++ii) {
		Parameter parameter = sourceFunc.getParameter(ii);
		// TODO: Fix deprecated method call
		destFunc.addParameter(parameter, SourceType.USER_DEFINED);
	}
}
 
Example #22
Source File: XrefViewerTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createStructureInStructure() {
	int id = program.startTransaction("Structure");

	Structure struct = new StructureDataType("ParentStructure", 0);
	Structure child = new StructureDataType("ChildStructure", 0);
	child.add(new ByteDataType());
	child.add(new ByteDataType());

	struct.add(child);
	struct.add(new ByteDataType()); // a child below the first child structure

	CreateStructureCmd cmd = new CreateStructureCmd(struct, addr(NESTED_STRUCT_ADDR));

	cmd.applyTo(program);
	program.endTransaction(id, true);

	// structure at 100101b - create refs to the parent structure and to the
	// child structure (this will be offcut at the parent level), and an element below the
	// first child structure.
	builder.createMemoryReference("1001012", NESTED_STRUCT_ADDR, RefType.DATA,
		SourceType.DEFAULT, 0);
	builder.createMemoryReference("1001013", "100101c", RefType.DATA, SourceType.DEFAULT, 0);
	builder.createMemoryReference("1001014", "100101d", RefType.DATA, SourceType.DEFAULT, 0);
}
 
Example #23
Source File: FunctionSignatureMarkupParameterName2Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testUserPriorityNameReplace_AnalysisSrc_ImportDest() throws Exception {
	useMatch("0x00411860", "0x00411830");

	setParameterName(sourceFunction, 0, "SrcAnalysisList", SourceType.ANALYSIS);
	setParameterName(destinationFunction, 0, "DestImportList", SourceType.IMPORTED);

	String sourceSignature = "void addPerson(Person * * SrcAnalysisList, char * name)";
	String originalDestinationSignature =
		"void FUN_00411830(int * DestImportList, char * param_2)";
	String appliedDestinationSignature = "void FUN_00411830(int * DestImportList, char * name)";
	SourcePriorityChoices parameterNamesChoice = SourcePriorityChoices.PRIORITY_REPLACE;

	applyAndUnapplyParameterNameMarkup(sourceSignature, originalDestinationSignature,
		appliedDestinationSignature, parameterNamesChoice, USER_PRIORITY_HIGHEST, false);
}
 
Example #24
Source File: DyldCacheProgramBuilder.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the DYLD Cache symbols.
 * 
 * @throws Exception if there was a problem creating the symbols
 */
private void createSymbols() throws Exception {
	DyldCacheLocalSymbolsInfo localSymbolsInfo = dyldCacheHeader.getLocalSymbolsInfo();
	if (localSymbolsInfo != null) {
		monitor.setMessage("Processing DYLD symbols...");
		monitor.initialize(localSymbolsInfo.getNList().size());
		for (NList nlist : localSymbolsInfo.getNList()) {
			if (!nlist.getString().trim().isEmpty()) {
				try {
					program.getSymbolTable().createLabel(space.getAddress(nlist.getValue()),
						SymbolUtilities.replaceInvalidChars(nlist.getString(), true),
						program.getGlobalNamespace(), SourceType.IMPORTED);
				}
				catch (Exception e) {
					log.appendMsg(e.getMessage() + " " + nlist.getString());
				}
			}
			monitor.checkCanceled();
			monitor.incrementProgress(1);
		}
	}
}
 
Example #25
Source File: ProgramDiff3Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Test that ProgramDiff can determine that function default stack params are
 * different.
 */
@Test
public void testFunctionDefaultStackParamDiff() throws Exception {

	// 0x1002cf5: created default stack param in p2.
	int transactionID = p2.startTransaction("Test Transaction");
	Function function = p2.getFunctionManager().getFunctionAt(addr(p2, 0x1002cf5));
	Variable var = new ParameterImpl("variable", DataType.DEFAULT, 0x1c, p2);
	function.addParameter(var, SourceType.USER_DEFINED);
	p2.endTransaction(transactionID, true);

	programDiff = new ProgramDiff(p1, p2);
	programDiff.setFilter(new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS));
	AddressSet diffAs = new AddressSet();
	diffAs.addRange(addr(0x1002cf5), addr(0x1002cf5));
	assertEquals(diffAs, programDiff.getDifferences(programDiff.getFilter(), null));
}
 
Example #26
Source File: FollowFlowProgramBuilder.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private int unconditionalJump(int from, int to) throws Exception {
	int thisInstructionsSize = 2;

	String fromString = "0x" + Integer.toHexString(from);
	String toString = "0x" + Integer.toHexString(to);
	String endString = "0x" + Integer.toHexString(from + thisInstructionsSize - 1);

	int distance = to - from - thisInstructionsSize;

	byte[] bytes = new byte[thisInstructionsSize];
	bytes[0] = (byte) 0xeb; // Unconditional Jump.
	bytes[1] = (byte) distance;
	clearCodeUnits(fromString, endString, false);
	setBytes(fromString, bytes, true);
	createMemoryReference(fromString, toString, RefType.UNCONDITIONAL_JUMP,
		SourceType.ANALYSIS, 0);

	return thisInstructionsSize; // instruction size in bytes.
}
 
Example #27
Source File: OldFunctionDataDB.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public synchronized Parameter[] getParameters() {

		loadRegisterParameterList();

		Parameter[] parms = new Parameter[regParams.size() + frame.getParameterCount()];
		int ordinal = 0;

		Iterator<Parameter> iter = regParams.iterator();
		while (iter.hasNext()) {
			Parameter rp = iter.next();
			parms[ordinal++] = rp;
		}

		try {
			Variable[] stackParams = frame.getParameters();
			for (int i = 0; i < stackParams.length; i++) {
				parms[ordinal++] =
					new OldFunctionParameter(stackParams[i].getName(), ordinal,
						stackParams[i].getDataType(), stackParams[i].getVariableStorage(), program,
						SourceType.USER_DEFINED);
			}
		}
		catch (InvalidInputException e) {
			throw new RuntimeException(e); // unexpected
		}
		return parms;
	}
 
Example #28
Source File: CreateMultipleFunctionsAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ListingActionContext context) {
	ProgramSelection selection = context.getSelection();
	if (!isEnabledForContext(context)) {
		return;
	}
	Program currentProgram = context.getProgram();
	if (currentProgram == null) {
		return;
	}

	CreateMultipleFunctionsCmd cmd =
		new CreateMultipleFunctionsCmd(selection, SourceType.USER_DEFINED);
	funcPlugin.execute(currentProgram, cmd);
}
 
Example #29
Source File: CodeBrowserNavigationx86Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Program buildProgram() throws Exception {
	ProgramBuilder builder = new ProgramBuilder("Test", ProgramBuilder._X86);
	builder.createMemory(".text", "0x1001000", 0x6600);
	builder.createMemory(".data", "0x1008000", 0x600);
	builder.createMemory(".rsrc", "0x100a000", 0x5400);
	builder.setBytes("1002cf5", "55 8b ec 83 7d 14 00 c2 14 00");
	builder.disassemble("1002cf5", 10);
	DataType dt = new DWordDataType();
	ParameterImpl param = new ParameterImpl(null, dt, builder.getProgram());
	builder.createEmptyFunction("ghidra", "1002cf5", 20, DataType.DEFAULT, param, param, param,
		param, param, param);
	builder.createStackReference("1002cf8", RefType.DATA, 0x14, SourceType.ANALYSIS, 0);
	return builder.getProgram();
}
 
Example #30
Source File: AbstractLocationReferencesTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void configureProgram() throws Exception {

		//
		// Xrefs
		//
		builder.createMemoryCallReference("0x0100446f", "0x01001004");

		//
		// Labels
		//
		builder.createMemoryReference("0x010036ee", "0x010039fe", RefType.CONDITIONAL_JUMP,
			SourceType.USER_DEFINED);

		//
		// Arrays/Structures
		//		
		DataType type = new IntegerDataType();
		DataType pointer = new PointerDataType(type);
		ArrayDataType array = new ArrayDataType(pointer, 4, pointer.getLength());
		builder.applyDataType("0x01005500", array);

		StructureDataType struct = new StructureDataType("struct_in_array", 0);
		struct.add(new IntegerDataType(), "my_int", "comment 1");
		struct.add(new ByteDataType(), "my_byte", "comment 2");
		array = new ArrayDataType(struct, 4, struct.getLength());
		builder.applyDataType("0x01005520", array);

		struct = new StructureDataType("struct_containing_array", 0);
		array = new ArrayDataType(pointer, 4, pointer.getLength());
		struct.add(new ByteDataType(), "my_byte", "comment 1");
		struct.add(array, "my_array", "comment 2");
		builder.applyDataType("0x01005540", struct);

		// a value that does not point to valid memory
		builder.setBytes("0x01004480", "cc cc cc cc");
		builder.applyDataType("0x01004480", new PointerDataType());
	}