Java Code Examples for ghidra.util.task.TaskMonitor#setMaximum()

The following examples show how to use ghidra.util.task.TaskMonitor#setMaximum() . 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: ProgramDB.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * External function pointers had previously been wrapped in a function.  This should know be
 * handled by creating an external function which corresponds to the pointers external location
 * reference.
 * @param monitor
 * @throws IOException
 */
private void checkFunctionWrappedPointers(TaskMonitor monitor)
		throws IOException, CancelledException {
	int storedVersion = getStoredVersion();
	if (storedVersion < EXTERNAL_FUNCTIONS_ADDED_VERSION) {
		FunctionManager functionManager = getFunctionManager();
		SymbolTable symbolTable = getSymbolTable();
		monitor.setProgress(0);
		monitor.setMaximum(functionManager.getFunctionCount());
		int cnt = 0;
		for (Symbol functionSymbol : symbolTable.getSymbols(memoryManager, SymbolType.FUNCTION,
			true)) {
			monitor.checkCanceled();
			ProgramUtilities.convertFunctionWrappedExternalPointer(functionSymbol);
			monitor.setProgress(++cnt);
		}
	}
	else {
		return;
	}
}
 
Example 2
Source File: DexHeaderFormatAnalyzer.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createNamespaces(Program program, DexHeader header, TaskMonitor monitor,
		MessageLog log) throws Exception {
	monitor.setMessage("DEX: creating namespaces");
	monitor.setMaximum(header.getClassDefsIdsSize());
	monitor.setProgress(0);

	// NOTE:
	// MUST CREATE ALL OF THE CLASSES AND NAMESPACES FIRST
	// OTHERWISE GHIDRA CANNOT HANDLE OBFUSCATED PACKAGES NAMES
	// FOR EXAMPLE, "a.a.a.a" and "a.a.a" WHERE THE LAST A IS A METHOD
	for (ClassDefItem item : header.getClassDefs()) {
		monitor.checkCanceled();
		monitor.incrementProgress(1);

		String className = DexUtil.convertTypeIndexToString(header, item.getClassIndex());
		Namespace classNameSpace =
			DexUtil.createNameSpaceFromMangledClassName(program, className);
		if (classNameSpace == null) {
			log.appendMsg("Failed to create namespace: " + className);
		}
	}
}
 
Example 3
Source File: DexHeaderFormatAnalyzer.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createMethods(Program program, DexHeader header, TaskMonitor monitor,
		MessageLog log) throws Exception {
	monitor.setMessage("DEX: creating methods");
	monitor.setMaximum(header.getClassDefsIdsSize());
	monitor.setProgress(0);
	for (ClassDefItem item : header.getClassDefs()) {
		monitor.checkCanceled();
		monitor.incrementProgress(1);

		ClassDataItem classDataItem = item.getClassDataItem();
		if (classDataItem == null) {
			continue;
		}

		createMethods(program, header, item, classDataItem.getDirectMethods(), monitor, log);
		createMethods(program, header, item, classDataItem.getVirtualMethods(), monitor, log);
	}
}
 
Example 4
Source File: SearchAllInstructionsTask.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Execute a memory search using the current settings in the dialog, returning the results.
 * 
 * @param taskMonitor the task monitor
 * @return list of instruction matches
 */
public List<InstructionMetadata> doSearch(TaskMonitor taskMonitor) {

	// First get all the search ranges we have to search.  
	List<AddressRange> searchRanges =
		searchDialog.getControlPanel().getRangeWidget().getSearchRange();

	// Now set up a list to hold all the search results.  
	List<InstructionMetadata> retList = new ArrayList<InstructionMetadata>();

	// Loop over all ranges, performing a separate search on each of them.  We keep track of 
	// the range number so we can display it to the user in progress window.
	int rangeNum = 1;
	for (AddressRange range : searchRanges) {

		// First reset the progress bar for this range.
		taskMonitor.setProgress(0);
		taskMonitor.setMaximum(range.getLength());

		if (searchRanges.size() > 1) {
			taskMonitor.setMessage(
				"Searching range " + rangeNum + " of " + searchRanges.size());
		}
		else {
			taskMonitor.setMessage("Searching...");
		}

		// Now perform the search and add the results to our list.
		List<InstructionMetadata> meta = searchDialog.getSearchData().search(
			searchPlugin.getCurrentProgram(), range, taskMonitor);
		retList.addAll(meta);

		// Increment the range counter.
		rangeNum++;
	}

	return retList;
}
 
Example 5
Source File: Ext4FileSystem.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Ext4Inode[] getInodes(BinaryReader reader, Ext4SuperBlock superBlock,
		Ext4GroupDescriptor[] groupDescriptors, boolean is64Bit, TaskMonitor monitor)
		throws IOException, CancelledException {

	int inodeCount = superBlock.getS_inodes_count();
	Ext4Inode[] inodes = new Ext4Inode[inodeCount + 1];
	int inodeIndex = 1;

	for (int i = 0; i < groupDescriptors.length; i++) {
		monitor.checkCanceled();
		long inodeTableBlockOffset = groupDescriptors[i].getBg_inode_table_lo() & 0xffffffffL;
		if (is64Bit) {
			inodeTableBlockOffset =
				(groupDescriptors[i].getBg_inode_table_hi() << 32) | inodeTableBlockOffset;
		}
		long offset = inodeTableBlockOffset * blockSize;
		reader.setPointerIndex(offset);
		int inodesPerGroup = superBlock.getS_inodes_per_group();
		monitor.setMessage(
			"Reading inode table " + i + " of " + (groupDescriptors.length - 1) + "...");
		monitor.setMaximum(inodesPerGroup);
		monitor.setProgress(0);
		for (int j = 0; j < inodesPerGroup; j++) {
			monitor.checkCanceled();
			monitor.incrementProgress(1);

			Ext4Inode inode = new Ext4Inode(reader);
			offset = offset + superBlock.getS_inode_size();
			reader.setPointerIndex(offset);

			inodes[inodeIndex++] = inode; //inodes[ inodesPerGroup * i + j ] = inode;
		}
	}
	return inodes;
}
 
Example 6
Source File: Ext4Analyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void createSuperBlockCopies(Program program, BinaryReader reader,
		int groupSize, int numGroups, boolean is64Bit, boolean isSparseSuper, TaskMonitor monitor) throws DuplicateNameException, IOException, Exception {
	monitor.setMessage("Creating super block and group descriptor copies...");
	monitor.setMaximum(numGroups);
	for( int i = 1; i < numGroups; i++ ) {
		monitor.checkCanceled();
		if( isSparseSuper && (!isXpowerOfY(i, 3) && !isXpowerOfY(i, 5) && !isXpowerOfY(i, 7)) ) {
			continue;
		}
		int offset = groupSize * i;
		Address address = toAddr(program, offset);
		reader.setPointerIndex(offset);
		Ext4SuperBlock superBlock = new Ext4SuperBlock(reader);
		createData(program, address, superBlock.toDataType());
		
		
		long groupDescOffset = offset + blockSize;
		Address groupDescAddress = toAddr(program, groupDescOffset);
		reader.setPointerIndex(groupDescOffset);
		for( int j = 0; j < numGroups; j++ ) {
			Ext4GroupDescriptor groupDesc = new Ext4GroupDescriptor(reader, is64Bit);
			DataType groupDescDataType = groupDesc.toDataType();
			createData(program, groupDescAddress, groupDescDataType);
			groupDescAddress = groupDescAddress.add(groupDescDataType.getLength());			
		}
		monitor.incrementProgress(1);
	}
	
}
 
Example 7
Source File: NewExt4Analyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void createInodeTables( Program program, 
								BinaryReader reader, 
								Ext4SuperBlock superBlock, 
								Ext4GroupDescriptor [] groupDescriptors, 
								boolean is64Bit, 
								TaskMonitor monitor ) throws DuplicateNameException, Exception {
	
	int inodeCount = superBlock.getS_inodes_count( );
	Ext4Inode [] inodes = new Ext4Inode [ inodeCount ];
	int inodeIndex = 0;

	for ( int i = 0; i < groupDescriptors.length; i++ ) {
		monitor.checkCanceled( );
		long inodeTableBlockOffset = groupDescriptors[ i ].getBg_inode_table_lo( ) & 0xffffffffL;
		if ( is64Bit ) {
			inodeTableBlockOffset = ( groupDescriptors[ i ].getBg_inode_table_hi( ) << 32 ) | inodeTableBlockOffset;
		}
		long offset = inodeTableBlockOffset * blockSize;
		reader.setPointerIndex( offset );
		Address address = null;
		try {
			address = toAddr( program, offset );
		}
		catch ( Exception e ) {
			throw new IOException( "offset " + offset + " not in program." );
		}

		int inodesPerGroup = superBlock.getS_inodes_per_group( );
		monitor.setMessage( "Creating inode table " + i + " of " + ( groupDescriptors.length - 1 ) + "..." );
		monitor.setMaximum( inodesPerGroup );
		monitor.setProgress( 0 );
		for ( int j = 0; j < inodesPerGroup; j++ ) {
			monitor.checkCanceled( );

			Ext4Inode inode = new Ext4Inode( reader );
			DataType dataType = inode.toDataType( );
			createData( program, address, dataType );

			String comment = "Inode: 0x" + Integer.toHexString( inodeIndex + 1 ) + "\n";
			comment += "Group Descriptor ID: 0x" + Integer.toHexString( i ) + "\n";
			comment += "Inode Offset Into Group: 0x" + Integer.toHexString( j ) + "\n";

			Ext4IBlock iBlock = inode.getI_block( );
			if ( iBlock != null ) {
				for ( Ext4Extent extent : iBlock.getExtentEntries( ) ) {
					monitor.checkCanceled( );
					long lo = extent.getEe_start_lo( ) & 0xffffffffL;
					long hi = extent.getEe_start_hi( ) & 0xffffffffL;
					long value = ( hi << 32 ) | lo;
					long destination = value * blockSize;
					comment += "Extent: 0x" + Long.toHexString( destination ) + "\n";
				}
			}

			setPlateComment( program, address, comment );
			createLabel( program, address, "INODE_" + "0x" + Integer.toHexString( inodeIndex + 1 ) );
			address = address.add( superBlock.getS_inode_size( ) );
			reader.setPointerIndex( address.getOffset( ) );
			monitor.incrementProgress( 1 );
			inodes[ inodeIndex++ ] = inode; //inodes[ inodesPerGroup * i + j ] = inode;
		}
	}

	processInodes( program, reader, superBlock, inodes, monitor );
}
 
Example 8
Source File: DWARFProgram.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Iterates over all the DWARF DIE records in the program and checks for some
 * pre-known issues, throwing an exception if there is a problem that would
 * prevent a successful run.
 *
 * @param monitor {@link TaskMonitor} to check for cancel and upate with status.
 * @throws DWARFException if DWARF structure error.
 * @throws CancelledException if user cancels.
 * @throws IOException if error reading data.
 */
public void checkPreconditions(TaskMonitor monitor)
		throws DWARFPreconditionException, DWARFException, CancelledException, IOException {
	monitor.setIndeterminate(false);
	monitor.setShowProgressValue(true);

	monitor.setMaximum(getCompilationUnits().size());

	if (getCompilationUnits().size() > 0 &&
		getCompilationUnits().get(0).getCompileUnit().hasDWO()) {
		// probably won't get anything from the file because its all in an external DWO
		Msg.warn(this,
			"Unsupported DWARF DWO (external debug file) detected -- unlikely any debug information will be found");
	}

	// This loop:
	// 1) preloads the DIEs if that option is set
	// 2) checks for cross-cu refs
	// 3) sums up the total number of DIE records found and updates prog with total.
	boolean preLoad = importOptions.isPreloadAllDIEs();
	totalDIECount = 0;
	totalAggregateCount = 0;
	clearDIEIndexes();
	for (DWARFCompilationUnit cu : getCompilationUnits()) {
		monitor.setMessage("DWARF Checking Preconditions - Compilation Unit #" +
			cu.getCompUnitNumber() + "/" + getCompilationUnits().size());
		monitor.setProgress(cu.getCompUnitNumber());

		cu.readDIEs(currentDIEs, monitor);

		if (totalDIECount > importOptions.getImportLimitDIECount() && !preLoad) {
			throw new DWARFPreconditionException(
				String.format(program.getName() + " has more DIE records (%d) than limit of %d",
					totalDIECount, importOptions.getImportLimitDIECount()));
		}

		if (!preLoad) {
			foundCrossCURefs |= checkForCrossCURefs(currentDIEs);
			totalDIECount += currentDIEs.size();
			totalAggregateCount += countAggregates();
			currentDIEs.clear();
			if (foundCrossCURefs) {
				throw new DWARFPreconditionException(
					"Found cross-compilation unit references between DIE records, but 'preload' is not turned on");
			}
		}

	}
	if (preLoad) {
		// build DIE indexes once
		rebuildDIEIndexes();
		this.totalAggregateCount = aggregates.size();
		this.totalDIECount = currentDIEs.size();
	}
}
 
Example 9
Source File: GCAnalyzer.java    From Ghidra-GameCube-Loader with Apache License 2.0 4 votes vote down vote up
@Override
public boolean added(Program program, AddressSetView set, TaskMonitor monitor, MessageLog log)
        throws CancelledException {
    monitor.setMaximum(100);
    Msg.info(this, "Starting GC program analysis...");
    var setSDA = false;
    var setSDA2 = false;
    
    if (this.searchSDARegs == true) {
        monitor.setMessage("Searching for SDA register (r13)...");
        var r13 = trySetDefaultRegisterValue("r13", program, monitor);
        setSDA = r13 != null;
        if (setSDA == false) {
            Msg.warn(this, "Failed to set the SDA register (r13) value!");
        }
        monitor.setProgress(10);
    
        monitor.setMessage("Searching for SDA2 (ToC) register (r2)...");
        var r2 = trySetDefaultRegisterValue("r2", program, monitor);
        setSDA2 = r2 != null;
        if (setSDA2 == false) {
            Msg.warn(this, "Failed to set the SDA2 (ToC) register (r2) value!");
        }
        
        int numUpdated = 0;
        var addressSpace = program.getAddressFactory().getDefaultAddressSpace();
        var codeManager = ((ProgramDB)program).getCodeManager();
        for (Instruction instruction : codeManager.getInstructions(addressSpace.getMinAddress(), true)) {
            if (updateInstruction(program, r2, r13, instruction)) {
                numUpdated++;
            }
        }
        Msg.debug(this, "Updated " + numUpdated + " SDA references");
    }
    monitor.setProgress(20);
    
    
    var setGQRs = false;
    if (this.searchGQRRegs == true) {
        for (var i = 0; i < 8; i++) {
            monitor.setMessage(String.format("Searching for GQR%d register...", i));
            var setGQR = trySetGQRegister(String.format("GQR%d", i), program, monitor);
            monitor.setProgress(30 + i * 10);
            if (setGQR == false) {
                Msg.warn(this, String.format("Failed to set the GQR%d register value!", i));
            }
            setGQRs |= setGQR;
        }
    }
    monitor.setProgress(100);
    
    return setSDA | setSDA2 | setGQRs;
}
 
Example 10
Source File: DexExceptionHandlersAnalyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private AddressSetView processMethods(Program program, Address baseAddress, DexHeader header,
			ClassDefItem item, List<EncodedMethod> methods, TaskMonitor monitor) throws Exception {
		AddressSet set = new AddressSet();

		monitor.setMaximum(methods.size());
		monitor.setProgress(0);

		for (int i = 0; i < methods.size(); ++i) {
			monitor.checkCanceled();
			monitor.incrementProgress(1);

			EncodedMethod method = methods.get(i);

			Address codeAddress = baseAddress.add(method.getCodeOffset());

			CodeItem codeItem = method.getCodeItem();
			if (codeItem == null) {
				continue;
			}

//			for ( TryItem tryItem : codeItem.getTries( ) ) {
//				monitor.checkCanceled( );
//
//				Address tryAddress = codeAddress.add( tryItem.getStartAddress( ) );
//				set.add( tryAddress );
//			}

			EncodedCatchHandlerList handlerList = codeItem.getHandlerList();
			if (handlerList == null) {
				continue;
			}

			for (EncodedCatchHandler handler : handlerList.getHandlers()) {
				monitor.checkCanceled();

				List<EncodedTypeAddressPair> pairs = handler.getPairs();
				for (EncodedTypeAddressPair pair : pairs) {
					monitor.checkCanceled();

					int catchTypeIndex = pair.getTypeIndex();
					TypeIDItem catchTypeIDItem = header.getTypes().get(catchTypeIndex);
					StringIDItem catchStringItem =
						header.getStrings().get(catchTypeIDItem.getDescriptorIndex());
					String catchString = catchStringItem.getStringDataItem().getString();
					Address catchAddress = codeAddress.add(pair.getAddress() * 2);

					createCatchSymbol(program, catchString, catchAddress);
					set.add(catchAddress);
				}

				if (handler.getSize() <= 0) {
					Address catchAllAddress = codeAddress.add(handler.getCatchAllAddress() * 2);
					createCatchSymbol(program, "CatchAll", catchAllAddress);
					set.add(catchAllAddress);
				}
			}
		}

		return set;
	}
 
Example 11
Source File: ConstantPropagationAnalyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
protected AddressSetView runAddressAnalysis(final Program program, final Set<Address> locations,
		final TaskMonitor monitor) throws CancelledException, InterruptedException, Exception {

	monitor.checkCanceled();

	final AddressSet analyzedSet = new AddressSet();
	if (locations.isEmpty()) {
		return analyzedSet;
	}

	GThreadPool pool = AutoAnalysisManager.getSharedAnalsysThreadPool();
	monitor.setMessage("Analyzing functions...");
	monitor.setMaximum(locations.size());

	QCallback<Address, AddressSetView> callback = new QCallback<Address, AddressSetView>() {
		@Override
		public AddressSetView process(Address loc, TaskMonitor taskMonitor) {
			synchronized (analyzedSet) {
				if (analyzedSet.contains(loc)) {
					taskMonitor.incrementProgress(1);
					return EMPTY_ADDRESS_SET;
				}
			}

			try {
				AddressSetView result = analyzeLocation(program, loc, null, taskMonitor);
				synchronized (analyzedSet) {
					analyzedSet.add(result);
				}

				taskMonitor.incrementProgress(1);
				return result;
			}
			catch (CancelledException e) {
				return null; // monitor was cancelled
			}
		}
	};

	// bound check thread limit
	if (maxThreadCount > pool.getMaxThreadCount() || maxThreadCount < 1) {
		maxThreadCount = 1;
	}

	// @formatter:off
	ConcurrentQ<Address, AddressSetView> queue = new ConcurrentQBuilder<Address, AddressSetView>()
		.setThreadPool(pool)
		.setMaxInProgress(maxThreadCount)
		.setMonitor(monitor)
		.build(callback);
	// @formatter:on

	queue.add(locations);

	queue.waitUntilDone();

	return analyzedSet;
}
 
Example 12
Source File: FunctionPurgeAnalysisCmd.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @see ghidra.framework.cmd.BackgroundCommand#applyTo(ghidra.framework.model.DomainObject, ghidra.util.task.TaskMonitor)
 */
@Override
public boolean applyTo(DomainObject obj, TaskMonitor monitor) {
	program = (Program) obj;

	Processor processor = program.getLanguage().getProcessor();
	AddressSpace defaultSpace = program.getLanguage().getDefaultSpace();
	if (defaultSpace.getSize() > 32 ||
		!processor.equals(Processor.findOrPossiblyCreateProcessor("x86"))) {
		Msg.error(this,
			"Unsupported operation for language " + program.getLanguage().getLanguageID());
		return false;
	}
	if (defaultSpace instanceof SegmentedAddressSpace) {	// For 16-bit x86, prepare to establish near/fear calling convention models
		setupNearFarModels();
	}

	AddressSetView set = entryPoints;

	long maxCount = set.getNumAddresses();

	monitor.setMaximum(maxCount);
	monitor.setProgress(0);

	for (Function function : program.getFunctionManager().getFunctions(entryPoints, true)) {
		if (monitor.isCancelled()) {
			break;
		}

		set = set.subtract(
			new AddressSet(program, entryPoints.getMinAddress(), function.getEntryPoint()));
		monitor.setProgress(maxCount - set.getNumAddresses());

		monitor.setMessage("Purge " + function.getName());

		try {
			analyzeFunction(function, monitor);
		}
		catch (CancelledException e) {
			// do nothing
		}
	}
	if (monitor.isCancelled()) {
		setStatusMsg("Function Purge analysis cancelled");
		return false;
	}
	return true;
}
 
Example 13
Source File: DexHeaderFormatAnalyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void processMethods(Program program, DexHeader header, TaskMonitor monitor,
		MessageLog log) throws Exception {
	monitor.setMessage("DEX: processing methods");
	monitor.setMaximum(header.getMethodIdsSize());
	monitor.setProgress(0);
	Address address = toAddr(program, header.getMethodIdsOffset());
	int methodIndex = 0;
	for (MethodIDItem item : header.getMethods()) {
		monitor.checkCanceled();
		monitor.incrementProgress(1);

		DataType dataType = item.toDataType();
		createData(program, address, dataType);
		createFragment(program, "methods", address, address.add(dataType.getLength()));

		StringBuilder builder = new StringBuilder();
		builder.append("Method Index: 0x" + Integer.toHexString(methodIndex) + "\n");
		builder.append(
			"Class: " + DexUtil.convertTypeIndexToString(header, item.getClassIndex()) + "\n");
		builder.append("Prototype: " +
			DexUtil.convertPrototypeIndexToString(header, item.getProtoIndex()) + "\n");
		builder.append("Name: " + DexUtil.convertToString(header, item.getNameIndex()) + "\n");

		setPlateComment(program, address, builder.toString());

		Address methodIndexAddress = DexUtil.toLookupAddress(program, methodIndex);

		if (program.getMemory().getInt(methodIndexAddress) == -1) {
			// Add placeholder symbol for external functions
			String methodName = DexUtil.convertToString(header, item.getNameIndex());
			String className = DexUtil.convertTypeIndexToString(header, item.getClassIndex());
			Namespace classNameSpace =
				DexUtil.createNameSpaceFromMangledClassName(program, className);
			if (classNameSpace != null) {
				Address externalAddress = DexUtil.toLookupAddress(program, methodIndex);
				Symbol methodSymbol = createMethodSymbol(program, externalAddress, methodName,
					classNameSpace, log);
				if (methodSymbol != null) {
					String externalName = methodSymbol.getName(true);
					program.getReferenceManager().addExternalReference(methodIndexAddress,
						"EXTERNAL.dex", externalName, null, SourceType.ANALYSIS, 0,
						RefType.DATA);
				}
			}
		}
		createData(program, methodIndexAddress, new PointerDataType());

		++methodIndex;

		address = address.add(dataType.getLength());
	}
}
 
Example 14
Source File: DexHeaderFormatAnalyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void createProgramDataTypes(Program program, DexHeader header, TaskMonitor monitor,
		MessageLog log) throws CancelledException {
	monitor.setMessage("DEX: creating program datatypes");
	monitor.setMaximum(header.getTypeIdsSize());
	monitor.setProgress(0);
	DataTypeManager dtm = program.getDataTypeManager();
	int curGroup = -1;
	CategoryPath handlePath = null;
	List<TypeIDItem> types = header.getTypes();
	for (int typeID = 0; typeID < header.getTypeIdsSize(); ++typeID) {
		TypeIDItem item = types.get(typeID);
		monitor.checkCanceled();
		monitor.incrementProgress(1);
		String name = DexUtil.convertToString(header, item.getDescriptorIndex());
		String[] path = DexUtil.convertClassStringToPathArray(DexUtil.CATEGORY_PATH, name);
		if (path == null) {
			continue;
		}

		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < path.length - 1; ++i) {
			builder.append(CategoryPath.DELIMITER_CHAR);
			builder.append(path[i]);
		}
		CategoryPath catPath = new CategoryPath(builder.toString());
		DataType dataType =
			new TypedefDataType(catPath, path[path.length - 1], DWordDataType.dataType);
		dataType = dtm.resolve(dataType, DataTypeConflictHandler.DEFAULT_HANDLER);

		// Create unchanging typedef to each class based on typeID, so we can find class type even if name changes
		if (typeID / 100 != curGroup) {
			curGroup = typeID / 100;
			builder = new StringBuilder();
			builder.append(DexUtil.HANDLE_PATH);
			builder.append("group").append(curGroup);
			handlePath = new CategoryPath(builder.toString());
		}
		DataType handleType = new TypedefDataType(handlePath, "type" + typeID, dataType);
		dtm.resolve(handleType, DataTypeConflictHandler.DEFAULT_HANDLER);
	}
}
 
Example 15
Source File: MipsPreAnalyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean added(Program program, AddressSetView set, TaskMonitor monitor, MessageLog log)
		throws CancelledException {

	pairBitRegister = program.getProgramContext().getRegister("PAIR_INSTRUCTION_FLAG");
	isamode = program.getProgramContext().getRegister("ISA_MODE");
	ismbit = program.getProgramContext().getRegister("ISAModeSwitch");
	rel6bit = program.getProgramContext().getRegister("REL6");
	micro16bit = program.getProgramContext().getRegister("RELP");

	set = removeUninitializedBlock(program, set);

	final long locationCount = set.getNumAddresses();
	if (locationCount > NOTIFICATION_INTERVAL) {
		monitor.initialize(locationCount);
	}

	AddressIterator addresses = set.getAddresses(true);

	int count = 0;

	AddressSet pairSet = new AddressSet();
	while (addresses.hasNext()) {
		monitor.checkCanceled();

		Address addr = addresses.next();

		if (locationCount > NOTIFICATION_INTERVAL) {

			if ((count % NOTIFICATION_INTERVAL) == 0) {
				monitor.setMaximum(locationCount);
				monitor.setProgress(count);
			}
			count++;
		}

		if ((addr.getOffset() & 0x3) != 0) {
			continue;
		}

		if (pairSet.contains(addr)) {
			continue;
		}

		if (!checkPossiblePairInstruction(program, addr)) {
			continue;
		}

		Instruction instr = program.getListing().getInstructionAt(addr);
		if (instr != null) {
			if (skipif16orR6(program, instr)) {
				continue;
			}
			findPair(program, pairSet, instr, monitor);
		}
	}

	redoAllPairs(program, pairSet, monitor);

	return true;
}
 
Example 16
Source File: SparseImageDecompressor.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Performs the decompression of the file, writing all bytes available to the
 * output stream.
 *
 * @param monitor {@link TaskMonitor} to watch for cancel and to update with progress info.
 * @throws CancelledException if user cancels
 * @throws IOException if error when reading or writing.
 */
public void decompress(TaskMonitor monitor) throws CancelledException, IOException {

	SparseHeader sparseHeader = new SparseHeader(reader);
	if (sparseHeader.getMajor_version() != SparseConstants.MAJOR_VERSION_NUMBER) {
		throw new IOException("Unsupported major version number.");
	}

	this.blockSize = sparseHeader.getBlk_sz();

	int totalBlocks = 0;
	monitor.setMaximum(sparseHeader.getTotal_chunks());
	monitor.setProgress(0);

	for (int i = 0; i < sparseHeader.getTotal_chunks(); i++) {
		monitor.checkCanceled();
		monitor.setMessage(
			"Processing chunk " + i + " of " + sparseHeader.getTotal_chunks() + "...");

		ChunkHeader chunkHeader = new ChunkHeader(reader);
		short chunkType = chunkHeader.getChunk_type();
		int chunkSize = chunkHeader.getChunk_sz();
		if (chunkType == SparseConstants.CHUNK_TYPE_RAW) {
			processRawChunk(chunkSize, monitor);
			totalBlocks += chunkSize;
		}
		else if (chunkType == SparseConstants.CHUNK_TYPE_FILL) {
			processFillChunk(chunkSize, monitor);
			totalBlocks += chunkSize;
		}
		else if (chunkType == SparseConstants.CHUNK_TYPE_DONT_CARE) {
			processSkipChunk(chunkSize, monitor);
			totalBlocks += chunkSize;
		}
		else if (chunkType == SparseConstants.CHUNK_TYPE_CRC32) {
			processCrcChunk();
			totalBlocks += chunkSize;
		}
		else {
			throw new IOException("Unkown chunk type: " + chunkType);
		}
		monitor.incrementProgress(1);
	}

	long totalSize = (long) totalBlocks * sparseHeader.getBlk_sz();
	monitor.setMessage("Total bytes: " + totalSize);
}
 
Example 17
Source File: NewExt4Analyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean analyze( Program program, AddressSetView set, TaskMonitor monitor, MessageLog log ) throws Exception {

	program2 = findOtherProgram( program, "0x70000000" );
	int transactionId2 = -1;
	if ( program2 != null ) {
		transactionId2 = program2.startTransaction( getName( ) );
	}

	program3 = findOtherProgram( program, "0xE0000000" );
	int transactionId3 = -1;
	if ( program3 != null ) {
		transactionId3 = program3.startTransaction( getName( ) );
	}

	try { 
		ByteProvider provider = new MultiProgramMemoryByteProvider( program, program2, program3 );
		BinaryReader reader = new BinaryReader( provider, true );
		int start = getSuperBlockStart( reader );
		int groupStart = 0;
		reader.setPointerIndex( start );
		Ext4SuperBlock superBlock = new Ext4SuperBlock( reader );
		Address superBlockAddress = toAddr( program, start );
		createData( program, superBlockAddress, superBlock.toDataType( ) );
		

		boolean is64Bit = ( superBlock.getS_desc_size( ) > 32 ) && ( ( superBlock.getS_feature_incompat( ) & 0x80 ) > 0 );

		long numBytes = program.getMaxAddress( ).getOffset( ) - program.getMinAddress( ).getOffset( ) + 1;
		if ( program2 != null ) {
			numBytes = program2.getMaxAddress( ).getOffset( ) - program.getMinAddress( ).getOffset( ) + 1;
		}
		if ( program3 != null ) {
			numBytes = program3.getMaxAddress( ).getOffset( ) - program.getMinAddress( ).getOffset( ) + 1;
		}

		int groupSize = calculateGroupSize( superBlock );
		int numGroups = ( int ) ( numBytes / groupSize );
		if ( numBytes % groupSize != 0 ) {
			numGroups++;
		}

		setPlateComment( program, superBlockAddress, "SuperBlock (main) \n" + 
						"Group Size In Bytes: 0x" + Integer.toHexString( groupSize ) + "\n" +
						"Number of Groups: 0x" + Integer.toHexString( numGroups ) );

		long groupDescOffset = groupStart + blockSize;
		Address groupDescAddress = toAddr( program, groupDescOffset );
		reader.setPointerIndex( groupDescOffset );
		Ext4GroupDescriptor groupDescriptors[] = new Ext4GroupDescriptor [ numGroups ];
		monitor.setMessage( "Creating group descriptors..." );
		monitor.setMaximum( numGroups );
		for ( int i = 0; i < numGroups; i++ ) {
			monitor.checkCanceled( );
			groupDescriptors[ i ] = new Ext4GroupDescriptor( reader, is64Bit );
			DataType groupDescDataType = groupDescriptors[ i ].toDataType( );
			createData( program, groupDescAddress, groupDescDataType );
			setPlateComment( program, groupDescAddress, "group descriptor: " + i );

			groupDescAddress = groupDescAddress.add( groupDescDataType.getLength( ) );
			monitor.incrementProgress( 1 );
		}

		boolean isSparseSuper = ( superBlock.getS_feature_ro_compat( ) & 1 ) != 0;
		createSuperBlockCopies( program, reader, groupSize, numGroups, is64Bit, isSparseSuper, monitor );

		createInodeTables( program, reader, superBlock, groupDescriptors, is64Bit, monitor );
		
//		test(program, reader);
	}
	catch ( Exception e ) {
		throw e;
	}
	finally {
		if ( program2 != null ) {
			program2.endTransaction( transactionId2, true );
			program2 = null;
		}
		if ( program3 != null ) {
			program3.endTransaction( transactionId3, true );
			program3 = null;
		}
	}

	return true;
}
 
Example 18
Source File: Ext4Analyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void createInodeTables(Program program, BinaryReader reader, Ext4SuperBlock superBlock,
		Ext4GroupDescriptor[] groupDescriptors, boolean is64Bit, TaskMonitor monitor) throws DuplicateNameException, Exception {
	
	int inodeCount = superBlock.getS_inodes_count();
	Ext4Inode inodes[] = new Ext4Inode[inodeCount];

	for( int i = 0; i < groupDescriptors.length; i++ ) {
		monitor.checkCanceled();
		long inodeTableBlockOffset = groupDescriptors[i].getBg_inode_table_lo() & 0xffffffffL;
		if( is64Bit ) {
			inodeTableBlockOffset = (groupDescriptors[i].getBg_inode_table_hi() << 32) | inodeTableBlockOffset;
		}
		long offset = inodeTableBlockOffset * blockSize;
		reader.setPointerIndex(offset);
		Address address = null;
		try {
			address = toAddr(program, offset);
		} catch (Exception e ) {
			throw new IOException("offset " + offset + " not in program.");
		}
		
		int inodesPerGroup = superBlock.getS_inodes_per_group();
		monitor.setMessage("Creating inode table " + i + " of " + (groupDescriptors.length - 1) + "...");
		monitor.setMaximum(inodesPerGroup);
		monitor.setProgress(0);
		for( int j = 0; j < inodesPerGroup; j++ ) {
			if( i == 0 && j == 0) {
				//inode 0 does not exist
				continue;
			}
			monitor.checkCanceled();
			Ext4Inode inode = new Ext4Inode(reader);
			DataType dataType = inode.toDataType();
			createData(program, address, dataType);
			program.getListing().setComment(address, CodeUnit.EOL_COMMENT, "0x" + (Integer.toHexString(inodesPerGroup * i + j )));
			address = address.add(superBlock.getS_inode_size());
			reader.setPointerIndex(address.getOffset());
			monitor.incrementProgress(1);
			inodes[inodesPerGroup * i + j] = inode;
		}
	}
	processInodes( program, reader, superBlock, inodes, monitor);
}
 
Example 19
Source File: CallFixupAnalyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
protected void clearAndRepairFlows(Program program, AddressSet repairedCallLocations,
		AddressSet protectedLocs, TaskMonitor monitor) throws CancelledException {
	//ReferenceIterator refIter = program.getReferenceManager().getReferencesTo(entry);
	long numRefs = repairedCallLocations.getNumAddresses();
	int refCnt = 0;

	lastPrimaryStatusMessage = "Repair";
	SubMonitor subMonitor = new SubMonitor(monitor);

	AddressSet clearInstSet = new AddressSet();
	AddressSet clearDataSet = new AddressSet();

	AddressIterator addrIter = repairedCallLocations.getAddresses(true);

	while (addrIter.hasNext()) {
		monitor.checkCanceled();
		monitor.setMaximum(numRefs);
		monitor.setProgress(refCnt++);
		Address fromAddr = addrIter.next();

		Instruction instr = program.getListing().getInstructionAt(fromAddr);
		if (instr == null) {
			continue;
		}
		Address fallthruAddr = instr.getFallThrough();
		if (fallthruAddr == null) {
			try {
				fallthruAddr =
					instr.getMinAddress().addNoWrap(instr.getDefaultFallThroughOffset());
			}
			catch (AddressOverflowException e) {
				// handled below
			}
		}
		if (fallthruAddr == null) {
			continue;
		}
		// if location right below is an entry point, don't clear it
		if (program.getSymbolTable().isExternalEntryPoint(fallthruAddr)) {
			continue;
		}

		// If there is a non-default function below, don't clear
		Function functionBelow = program.getFunctionManager().getFunctionAt(fallthruAddr);
		if (functionBelow != null &&
			functionBelow.getSymbol().getSource() != SourceType.DEFAULT) {
			continue;
		}

		if (!hasFlowRefInto(program, fallthruAddr)) {
			Instruction inst = program.getListing().getInstructionAt(fallthruAddr);
			if (inst != null) {
				clearInstSet.add(fallthruAddr);
			}
			else {
				clearDataSet.add(fallthruAddr);
			}
		}
	}

	program.getBookmarkManager().removeBookmarks(repairedCallLocations, BookmarkType.ERROR,
		monitor);

	if (!clearInstSet.isEmpty()) {
		// entries including data flow referenced from instructions will be repaired
		AddressSet protect = new AddressSet(repairedCallLocations).union(protectedLocs);
		ClearFlowAndRepairCmd cmd =
			new ClearFlowAndRepairCmd(clearInstSet, protect, true, false, true);
		cmd.applyTo(program, subMonitor);
	}
	if (!clearDataSet.isEmpty()) {
		// entries that are data should not be cleared, only possible bookmarks
		ClearFlowAndRepairCmd.clearBadBookmarks(program, clearDataSet, subMonitor);
	}
}
 
Example 20
Source File: KernelFileSystem.java    From ghidra with Apache License 2.0 3 votes vote down vote up
@Override
public void open(TaskMonitor monitor) throws IOException, CryptoException, CancelledException {

	// Scan through the file looking for the message below.
	// The next byte after the message should be the compressed kernel

	final String message = "uncompression error";

	int index = INDEX_WHERE_TO_START;

	monitor.setMaximum(provider.length());

	while (index < provider.length() - message.length() + 1) {
		monitor.checkCanceled();
		monitor.setProgress(index);

		String actualMessage = new String(provider.readBytes(index, message.length()));

		if (message.equals(actualMessage)) {// immediately following this string is the compressed pay-load....

			compressedKernelIndex =
				(index & 0xffffffffL) + (message.length() & 0xffffffffL) + 1;

			compressedKernelLength = provider.length() - compressedKernelIndex;

			compressedKernelFile = GFileImpl.fromFilename(this, root, "compressed-kernel",
				false, compressedKernelLength, null);

			break;
		}

		++index;
	}
}