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

The following examples show how to use ghidra.program.model.address.AddressSetView#getNumAddresses() . 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: BasicBlockCounterFunctionAlgorithm.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public int score(Function function, TaskMonitor monitor) throws CancelledException {
	Program program = function.getProgram();
	CodeBlockModel blockModel = new BasicBlockModel(program);

	AddressSetView body = function.getBody();
	long maxIterations = body.getNumAddresses();
	monitor.initialize(maxIterations);

	CodeBlockIterator iterator = blockModel.getCodeBlocksContaining(body, monitor);

	int blockCount = 0;
	while (iterator.hasNext()) {
		monitor.checkCanceled();
		iterator.next();
		blockCount++;
		monitor.incrementProgress(1);

		artificialSleepForDemoPurposes();
	}
	return blockCount;
}
 
Example 2
Source File: FunctionIDHeadlessPostscript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
	protected void run() throws Exception {
		runScript("FixSwitchStatementsWithDecompiler.java");

		FunctionManager functionManager = currentProgram.getFunctionManager();
		int functionCount = functionManager.getFunctionCount();
		if (functionCount == 0) {
			printerr(currentProgram.getDomainFile().getPathname() + " has no functions");

			// keep the functionless domain object for now in case user wants to
			// inspect why analysis failed (or didn't)

//			getState().addEnvironmentVar(GhidraScript.SCRIPT_SET_CONTINUATION_STATUS,
//				HeadlessContinuationOption.ABORT_AND_DELETE);
			return;
		}
		FunctionIterator functions = functionManager.getFunctions(true);
		for (Function function : functions) {
			AddressSetView body = function.getBody();
			if (body.getNumAddresses() >= MINIMUM_FUNCTION_SIZE_IN_BYTES) {
				// at least one meets threshold; everything is OK
				return;
			}
		}
		printerr(currentProgram.getDomainFile().getPathname() +
			" has no normal-sized functions (>= " + MINIMUM_FUNCTION_SIZE_IN_BYTES + " bytes long)");

		// keep the functionless domain object for now in case user wants to
		// inspect why analysis failed (or didn't)

//		getState().addEnvironmentVar(GhidraScript.SCRIPT_SET_CONTINUATION_STATUS,
//			HeadlessContinuationOption.ABORT_AND_DELETE);
	}
 
Example 3
Source File: FunctionMatchSet.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public int getLength(Address addr, Program aProgram) {
	Function func = aProgram.getFunctionManager().getFunctionContaining(addr);
	AddressSetView asv = func.getBody();
	return (int) asv.getNumAddresses();

}
 
Example 4
Source File: ReportPercentDisassembled.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
	public void run() throws Exception {

		// find all the sections of memory marked as executable
		Program prog = currentProgram;
		AddressSetView execMemSet = prog.getMemory().getExecuteSet();
		/*int myExecSetLen = 0;
		MemoryBlock[] blocks = prog.getMemory().getBlocks();
		for (int i = 0; i < blocks.length; i++) {
			if (blocks[i].isExecute()) {
				myExecSetLen += blocks[i].getSize();
			}
		}*/

		// tally up all the bytes that have been marked as instructions
		//   (probably faster ways to do this, but it works)
		long numPossibleDis = execMemSet.getNumAddresses();
		InstructionIterator instIter = prog.getListing().getInstructions(execMemSet, true);
		int instCount = 0;
		while (instIter.hasNext()) {
			Instruction inst = instIter.next();
			instCount += inst.getLength();
		}
		DataIterator dataIter = prog.getListing().getData(execMemSet, true);
		int dataCount = 0;
		while (dataIter.hasNext()) {
			Data data = dataIter.next();
			if (data.isDefined()) {
				dataCount += data.getLength();
			}
		}

		// dump the info
		int total = instCount + dataCount;
		if (numPossibleDis != 0) {
			float coverage = (float) total / (float) numPossibleDis;
//	    	Msg.info(this,"execSetLen = " + numPossibleDis);
//	    	Msg.info(this,"MyExecSetLen = " + myExecSetLen);
//	    	Msg.info(this,"NumInsts = " + instCount);
//	    	Msg.info(this,"numData = " + dataCount);
//
//	    	Msg.info(this,"totalDis = " + total);
			float percentage = coverage * 100;
			Msg.info(this, "REPORT DISASSEMBLY EXTENT: " + prog.getName() + ": " + percentage +
				"% disassembled.");
		}
		return;

	}
 
Example 5
Source File: CompareAnalysisScript.java    From ghidra with Apache License 2.0 4 votes vote down vote up
void reportPercentDisassembled(Program prog) throws MemoryAccessException {
		// find all the sections of memory marked as executable
		String programName = prog.getDomainFile().getName();

		AddressSetView execMemSet = prog.getMemory().getExecuteSet();
		/*
		int myExecSetLen = 0;
		MemoryBlock[] blocks = prog.getMemory().getBlocks();
		for (int i = 0; i < blocks.length; i++) {
			if (blocks[i].isExecute()) {
				myExecSetLen += blocks[i].getSize();
			}
		}*/

		// tally up all the bytes that have been marked as instructions
		//   (probably faster ways to do this, but it works)
		long numPossibleDis = execMemSet.getNumAddresses();
		InstructionIterator instIter = prog.getListing().getInstructions(execMemSet, true);
		int instCount = 0;
		while (instIter.hasNext()) {
			Instruction inst = instIter.next();
			instCount += inst.getBytes().length;
		}
		DataIterator dataIter = prog.getListing().getData(execMemSet, true);
		int dataCount = 0;
		while (dataIter.hasNext()) {
			Data data = dataIter.next();
			if (data.isDefined()) {
				dataCount += data.getBytes().length;
			}
		}

		// dump the info
		int total = instCount + dataCount;
		if (numPossibleDis != 0) {
			float coverage = (float) total / (float) numPossibleDis;
//    	Msg.info(this,"execSetLen = " + numPossibleDis);
//    	Msg.info(this,"MyExecSetLen = " + myExecSetLen);
//    	Msg.info(this,"NumInsts = " + instCount);
//    	Msg.info(this,"numData = " + dataCount);
//
//    	Msg.info(this,"totalDis = " + total);
			float percentage = coverage * 100;
			println(programName + ": " + percentage + "% disassembled.");
		}
		return;
	}
 
Example 6
Source File: ReportDisassemblyStats.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void run() throws Exception {

	// find all the sections of memory marked as executable

	if (this.isRunningHeadless()) {
		runScript("MakeFuncsAtLabelsScript.java");
	}

	AddressSetView execMemSet = currentProgram.getMemory().getExecuteSet();
	InstructionIterator instIter =
		currentProgram.getListing().getInstructions(execMemSet, true);
	FunctionManager fm = currentProgram.getFunctionManager();
	//calculate the number of instructions not in functions and the total number of instructions
	int instCount = 0;
	int instNotInFuncCount = 0;
	int instByteCount = 0;
	while (instIter.hasNext()) {
		Instruction inst = instIter.next();
		instByteCount += inst.getBytes().length;
		Function func = fm.getFunctionContaining(inst.getAddress());
		if (func == null) {
			instNotInFuncCount++;
		}
		instCount++;
	}
	//count the number of defined data bytes
	int dataByteCount = 0;
	DataIterator dataIter = currentProgram.getListing().getData(execMemSet, true);
	while (dataIter.hasNext()) {
		Data data = dataIter.next();
		if (data.isDefined()) {
			dataByteCount += data.getBytes().length;
		}
	}

	long numTotalBytes = execMemSet.getNumAddresses();
	double undefinedPercentage =
		100.0 - (100.0 * (instByteCount + dataByteCount) / numTotalBytes);
	double notInFuncPercentage = instNotInFuncCount * 100.0 / instCount;

	printf("Name: %s", getProgramFile().toString());
	printf("Language: %s", currentProgram.getLanguageID());
	printf("CompilerSpec: %s", currentProgram.getCompilerSpec().getCompilerSpecID());
	printf("Number of functions: %d", fm.getFunctionCount());
	printf("Number of addresses: %d", numTotalBytes);
	printf("Percentage of undefined addresses: %f", undefinedPercentage);
	printf("Percentage of instructions not in functions: %f", notInFuncPercentage);

	Iterator<Bookmark> bmi = currentProgram.getBookmarkManager().getBookmarksIterator("Error");
	int numConflicts = 0;
	int numRelocationErrors = 0;
	while (bmi.hasNext()) {
		Bookmark bm = bmi.next();
		if (bm.toString().contains("conflicting")) {
			numConflicts++;
			continue;
		}
		if (bm.toString().contains("relocation")) {
			numRelocationErrors++;
			continue;
		}
		printf("!!%s", bm.toString());
	}
	printf("Number of conflicts: %d", numConflicts);
	printf("Number of relocation errors: %d", numRelocationErrors);
	if (this.isRunningHeadless()) {
		if ((undefinedPercentage <= UNDEFINED_THRESHOLD) &&
			(notInFuncPercentage <= NOT_IN_FUNC_THRESHOLD)) {
			totalNumOfFunctions += fm.getFunctionCount();
			printf("Total number of functions: %d", totalNumOfFunctions);
			//search for .siginfo file
			File siginfoFile = new File(getProgramFile().getAbsolutePath() + ".siginfo");
			boolean siginfoExists = siginfoFile.exists();
			if (!siginfoExists) {
				printf("No siginfo file found");
			}
			else {
				//read the .siginfo file and save the information to the project
				BufferedReader br = new BufferedReader(new FileReader(siginfoFile));
				String verinfo = br.readLine();
				br.close();
				Options propList = currentProgram.getOptions("Signature Info");
				propList.setString("Version Name", verinfo);
				printf("Saving version %s to project", verinfo);
			}
		}
		else {
			printf("Program not imported");
			setHeadlessContinuationOption(HeadlessContinuationOption.ABORT_AND_DELETE);
		}
		printf("");

	}

}
 
Example 7
Source File: SizeFunctionAlgorithm.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public int score(Function function, TaskMonitor monitor) {
	AddressSetView body = function.getBody();
	return (int) body.getNumAddresses();
}