Merge remote-tracking branch 'origin/GP-6008_ghidra1_SourceTypePriorityUse--SQUASHED' into Ghidra_12.0

This commit is contained in:
ghidra1 2025-09-24 09:18:36 -04:00
commit ed4ad257f2
20 changed files with 131 additions and 144 deletions

View file

@ -43,8 +43,7 @@ import ghidra.program.model.symbol.*;
public class AutoRenameSimpleLabels extends GhidraScript {
boolean isDefaultName(Symbol symbol) {
return symbol.getSource() == SourceType.DEFAULT ||
symbol.getSource() == SourceType.ANALYSIS;
return symbol.getSource().isLowerOrEqualPriorityThan(SourceType.ANALYSIS);
}
@Override
@ -61,6 +60,9 @@ public class AutoRenameSimpleLabels extends GhidraScript {
//get this instruction's info
Symbol s = iter.next();
Address startAddr = s.getAddress();
if (!startAddr.isLoadedMemoryAddress()) {
continue;
}
// read the instruction type and operand
Instruction inst = getInstructionAt(startAddr);

View file

@ -43,12 +43,11 @@ public class LabelDataScript extends GhidraScript {
(!data.getBaseDataType().getName().toLowerCase().contains("string")) &&
(!data.getBaseDataType().getName().toLowerCase().contains("unicode"))) {
Symbol sym = symbolTable.getPrimarySymbol(data.getMinAddress());
if ((sym != null) && ((sym.getSource() == SourceType.DEFAULT) ||
(sym.getSource() == SourceType.ANALYSIS))) {
String newLabel =
data.getDefaultLabelPrefix(null) + "_" +
SymbolUtilities.replaceInvalidChars(
data.getDefaultValueRepresentation(), false) +
if (sym != null &&
sym.getSource().isLowerOrEqualPriorityThan(SourceType.ANALYSIS)) {
String newLabel = data.getDefaultLabelPrefix(null) +
"_" + SymbolUtilities
.replaceInvalidChars(data.getDefaultValueRepresentation(), false) +
"_" + data.getMinAddress().toString();
Symbol newSym = symbolTable.createLabel(data.getMinAddress(), newLabel,
SourceType.ANALYSIS);

View file

@ -145,8 +145,10 @@ public class NewFunctionStackAnalysisCmd extends BackgroundCommand<Program> {
}
private boolean checkForX86(Program p) {
return program.getLanguage().getProcessor().equals(
Processor.findOrPossiblyCreateProcessor(X86_NAME)) && program.getDefaultPointerSize() <= 32;
return program.getLanguage()
.getProcessor()
.equals(Processor.findOrPossiblyCreateProcessor(X86_NAME)) &&
program.getDefaultPointerSize() <= 32;
}
/**
@ -221,7 +223,7 @@ public class NewFunctionStackAnalysisCmd extends BackgroundCommand<Program> {
private boolean isProtectedVariable(Variable var) {
return !var.isStackVariable() || !Undefined.isUndefined(var.getDataType()) ||
var.getSource() == SourceType.IMPORTED || var.getSource() == SourceType.USER_DEFINED ||
var.getSource().isHigherOrEqualPriorityThan(SourceType.IMPORTED) ||
var.isCompoundVariable();
}
@ -339,9 +341,11 @@ public class NewFunctionStackAnalysisCmd extends BackgroundCommand<Program> {
return;
}
}
long extendedOffset = extendOffset(address.getOffset(), stackReg.getBitLength());
long extendedOffset =
extendOffset(address.getOffset(), stackReg.getBitLength());
defineFuncVariable(symEval, func, instr, opIndex, (int) extendedOffset, sortedVariables);
defineFuncVariable(symEval, func, instr, opIndex, (int) extendedOffset,
sortedVariables);
}
}
@ -634,8 +638,8 @@ public class NewFunctionStackAnalysisCmd extends BackgroundCommand<Program> {
// return true;
// }
private void defineFuncVariable(SymbolicPropogator symEval, Function func, Instruction instr, int opIndex, int stackOffset,
List<Variable> sortedVariables) {
private void defineFuncVariable(SymbolicPropogator symEval, Function func, Instruction instr,
int opIndex, int stackOffset, List<Variable> sortedVariables) {
ReferenceManager refMgr = program.getReferenceManager();
int refSize = getRefSize(symEval, instr, opIndex);

View file

@ -224,7 +224,7 @@ public abstract class AbstractDemanglerAnalyzer extends AbstractAnalyzer {
if (symbol.getSymbolType() == SymbolType.FUNCTION) {
Function function = (Function) symbol.getObject();
if (!function.isThunk() &&
function.getSignatureSource().isHigherPriorityThan(SourceType.ANALYSIS)) {
function.getSignatureSource().isHigherOrEqualPriorityThan(SourceType.IMPORTED)) {
return true;
}
}
@ -297,8 +297,7 @@ public abstract class AbstractDemanglerAnalyzer extends AbstractAnalyzer {
return;
}
String errorString = demangled.getErrorMessage();
logApplyErrorMessage(log, demangled, mangledContext.getAddress(), null,
errorString);
logApplyErrorMessage(log, demangled, mangledContext.getAddress(), null, errorString);
}
catch (Exception e) {
logApplyErrorMessage(log, demangled, mangledContext.getAddress(), e, null);

View file

@ -427,7 +427,7 @@ public class ConstantPropagationContextEvaluator extends ContextEvaluatorAdapter
}
SourceType mostTrusted = getMostTrustedParameterSource(func);
if (SourceType.ANALYSIS.isLowerPriorityThan(mostTrusted)) {
if (mostTrusted.isHigherOrEqualPriorityThan(SourceType.IMPORTED)) {
return;
}
}

View file

@ -225,6 +225,8 @@ public class GolangSymbolAnalyzer extends AbstractAnalyzer {
continue;
}
// NOTE: DWARF may have applied function signature with IMPORTED source type but
// this analyzer must apply more details
Function func = markupSession.createFunctionIfMissing(funcname, funcns, funcAddr);
if (func == null ||
func.getSignatureSource().isHigherPriorityThan(SourceType.IMPORTED)) {
@ -503,8 +505,8 @@ public class GolangSymbolAnalyzer extends AbstractAnalyzer {
Address mbStart = max.subtract(offset_from_eom + len - 1);
MemoryBlock newMB =
MemoryBlockUtils.createUninitializedBlock(program, false, "ARTIFICAL_GOLANG_CONTEXT",
mbStart, len, "Artifical memory block created to hold Go context data types",
null, true, true, false, null);
mbStart, len, "Artifical memory block created to hold Go context data types", null,
true, true, false, null);
newMB.setArtificial(true);
return newMB.getStart();
}
@ -1034,7 +1036,6 @@ public class GolangSymbolAnalyzer extends AbstractAnalyzer {
Register register, java.util.function.Function<GoType, DataType> returnTypeMapper) {
}
private GoRttiMapper goBinary;
private Program program;
private MarkupSession markupSession;

View file

@ -109,8 +109,9 @@ public class PefAnalyzer extends AbstractAnalyzer {
}
Function function = functions.next();
try {
program.getProgramContext().setRegisterValue(function.getEntryPoint(),
function.getEntryPoint(), regVal);
program.getProgramContext()
.setRegisterValue(function.getEntryPoint(), function.getEntryPoint(),
regVal);
}
catch (ContextChangeException e) {
// should never happen when changing r2 register
@ -149,11 +150,8 @@ public class PefAnalyzer extends AbstractAnalyzer {
return;
}
Function function = listing.getFunctionContaining(instruction.getMinAddress());
if (function == null) {
return;
}
if (function.getSymbol().getSource() == SourceType.IMPORTED ||
function.getSymbol().getSource() == SourceType.USER_DEFINED) {
if (function == null ||
function.getSymbol().getSource().isHigherOrEqualPriorityThan(SourceType.IMPORTED)) {
return;
}
Symbol symbol = symbolTable.getPrimarySymbol(symbolAddress);

View file

@ -717,11 +717,9 @@ public class ClearFlowAndRepairCmd extends BackgroundCommand<Program> {
continue; // do not include data
}
Symbol s = symbolTable.getPrimarySymbol(blockAddr);
if (s != null && s.getSymbolType() == SymbolType.FUNCTION) {
SourceType source = s.getSource();
if (source == SourceType.USER_DEFINED || source == SourceType.IMPORTED) {
continue; // keep imported or user-defined function
}
if (s != null && s.getSymbolType() == SymbolType.FUNCTION &&
s.getSource().isHigherOrEqualPriorityThan(SourceType.IMPORTED)) {
continue;
// TODO: GP-5872 Clearing thunks explicitly created by loader or pattern
// generally have default SourceType and may not have references
// to them. We need to prevent these thunks from getting cleared.

View file

@ -378,8 +378,7 @@ public class DemangledFunction extends DemangledObject {
if (f != null && f.getSymbol().getSource() == SourceType.USER_DEFINED) {
return true;
}
if (f == null || f.getSignatureSource() == SourceType.DEFAULT ||
f.getSignatureSource() == SourceType.ANALYSIS) {
if (f == null || f.getSignatureSource().isLowerOrEqualPriorityThan(SourceType.ANALYSIS)) {
return false;
}
return super.isAlreadyDemangled(program, address);
@ -809,7 +808,7 @@ public class DemangledFunction extends DemangledObject {
continue;
}
// if the parameters source is higher than
if (parameter.getSource().isHigherPriorityThan(SourceType.ANALYSIS)) {
if (parameter.getSource().isHigherOrEqualPriorityThan(SourceType.IMPORTED)) {
return true;
}
}

View file

@ -150,12 +150,12 @@ public class DecompilerParameterIdCmd extends BackgroundCommand<Program> {
// since decompile could fail and leave the source types changed.
Parameter retParam = func.getReturn();
if (retParam != null) {
if (!retParam.getSource().isHigherPriorityThan(sourceTypeClearLevel)) {
if (retParam.getSource().isLowerOrEqualPriorityThan(sourceTypeClearLevel)) {
func.setReturn(retParam.getDataType(), retParam.getVariableStorage(),
SourceType.DEFAULT);
}
}
if (!func.getSignatureSource().isHigherPriorityThan(sourceTypeClearLevel)) {
if (func.getSignatureSource().isLowerOrEqualPriorityThan(sourceTypeClearLevel)) {
func.setSignatureSource(SourceType.DEFAULT);
}
}

View file

@ -174,12 +174,10 @@ public class OverridePrototypeAction extends AbstractDecompilerAction {
// for the initial signature. HighFunction does not make it easy to grab
// existing override prototype
if (calledfunc != null) {
SourceType signatureSource = calledfunc.getSignatureSource();
if (signatureSource == SourceType.DEFAULT || signatureSource == SourceType.ANALYSIS) {
if (calledfunc != null &&
calledfunc.getSignatureSource().isLowerOrEqualPriorityThan(SourceType.ANALYSIS)) {
calledfunc = null; // ignore
}
}
StringBuffer buf = new StringBuffer();

View file

@ -63,6 +63,8 @@ public class DecompilerParameterIDValidator extends PostAnalysisValidator {
if (inst != null) {
final SourceType signatureSource = func.getSignatureSource();
// TODO: There is a misconception that ANALYSIS source type can be used to
// determine if Param ID Analyzer has been used. This should be re-examined.
if (signatureSource == SourceType.ANALYSIS) {
++numFuncsWithParameterID;
}

View file

@ -273,9 +273,8 @@ public class ApplyFidEntriesCommand extends BackgroundCommand<Program> {
SymbolTable symbolTable = program.getSymbolTable();
SymbolIterator symbols = symbolTable.getSymbolsAsIterator(function.getEntryPoint());
for (Symbol symbol : symbols) {
SourceType sourceType = symbol.getSource();
if (sourceType == SourceType.USER_DEFINED || sourceType == SourceType.IMPORTED) {
return true;
if (symbol.getSource().isHigherOrEqualPriorityThan(SourceType.IMPORTED)) {
return true; // symbol has trusted source
}
}
return false;

View file

@ -2341,25 +2341,6 @@ public class DefaultPdbApplicator implements PdbApplicator {
}
}
//==============================================================================================
boolean shouldForcePrimarySymbol(Address address, boolean forceIfMangled) {
Symbol primarySymbol = program.getSymbolTable().getPrimarySymbol(address);
if (primarySymbol != null) {
if (primarySymbol.getName().startsWith("?") && forceIfMangled &&
applicatorOptions.allowDemotePrimaryMangledSymbols()) {
return true;
}
SourceType primarySymbolSource = primarySymbol.getSource();
if (!SourceType.ANALYSIS.isHigherPriorityThan(primarySymbolSource)) {
return true;
}
}
return false;
}
//==============================================================================================
boolean addToPlateUnique(Address address, String comment) {
if (StringUtils.isBlank(comment)) {
@ -2411,8 +2392,8 @@ public class DefaultPdbApplicator implements PdbApplicator {
Function existingFunction = program.getListing().getFunctionAt(address);
if (existingFunction != null) { // Maybe I should care if there is a data type there too.
if (existingFunction.getSignatureSource().isHigherPriorityThan(SourceType.ANALYSIS)) {
// Existing is USER or IMPORTED
if (existingFunction.getSignatureSource()
.isHigherOrEqualPriorityThan(SourceType.IMPORTED)) {
return doCreateSymbol(address, symbolPath, false, plateAddition);
}
}

View file

@ -79,8 +79,7 @@ public class FunctionSymbolApplier extends AbstractBlockContextApplier
return applicator.getAddress(symbol);
}
private void processSymbol(MsSymbolIterator iter)
throws CancelledException, PdbException {
private void processSymbol(MsSymbolIterator iter) throws CancelledException, PdbException {
Address address = applicator.getAddress(symbol);
String name = symbol.getName();
@ -186,8 +185,7 @@ public class FunctionSymbolApplier extends AbstractBlockContextApplier
}
// Remaining are non-thunks
if (function.getSignatureSource().isHigherPriorityThan(SourceType.ANALYSIS)) {
// return if IMPORTED or USER_DEFINED
if (function.getSignatureSource().isHigherOrEqualPriorityThan(SourceType.IMPORTED)) {
return false;
}

View file

@ -86,8 +86,7 @@ public class ManagedProcedureSymbolApplier extends AbstractBlockContextApplier
// TODO. Investigate more. This is not working for at least one CLI dll in that we are
// not getting correct addresses. There is no omap and the one section is unnamed.
private void processSymbol(MsSymbolIterator iter)
throws CancelledException, PdbException {
private void processSymbol(MsSymbolIterator iter) throws CancelledException, PdbException {
Address address = applicator.getAddress(symbol);
String name = symbol.getName();
@ -124,8 +123,7 @@ public class ManagedProcedureSymbolApplier extends AbstractBlockContextApplier
}
@Override
public void deferredApply(MsSymbolIterator iter)
throws PdbException, CancelledException {
public void deferredApply(MsSymbolIterator iter) throws PdbException, CancelledException {
// Pealing the symbol off again, as the iterator is coming in fresh, and we need the symbol
getValidatedSymbol(iter, true);
@ -260,9 +258,8 @@ public class ManagedProcedureSymbolApplier extends AbstractBlockContextApplier
return; // silently return.
}
// Currently just placing a comment.
String comment =
context.getIndent(symbolBlockNestingLevel + 1) + "static local (stored at " +
address + ") " + dataType.getName() + " " + name;
String comment = context.getIndent(symbolBlockNestingLevel + 1) +
"static local (stored at " + address + ") " + dataType.getName() + " " + name;
context.getComments().addPreComment(currentBlockAddress, comment);
}
@ -300,8 +297,7 @@ public class ManagedProcedureSymbolApplier extends AbstractBlockContextApplier
private boolean setFunctionDefinition(Function function, Address address,
AbstractManagedProcedureMsSymbol symbol) throws CancelledException, PdbException {
if (function.getSignatureSource().isHigherPriorityThan(SourceType.ANALYSIS)) {
// return if IMPORTED or USER_DEFINED
if (function.getSignatureSource().isHigherOrEqualPriorityThan(SourceType.IMPORTED)) {
return false;
}

View file

@ -106,8 +106,7 @@ public class SimilarSymbolNameProgramCorrelator extends VTAbstractProgramCorrela
if (!addressSet.contains(symbol.getAddress())) {
continue;
}
if (symbol.getSource() == SourceType.DEFAULT ||
symbol.getSource() == SourceType.ANALYSIS) {
if (symbol.getSource().isLowerOrEqualPriorityThan(SourceType.ANALYSIS)) {
continue;
}

View file

@ -979,38 +979,29 @@ public class FunctionSignatureStringable extends Stringable {
private boolean isFirstHigherPriorityWhenUserPriority(SourceType first, SourceType second,
boolean replaceSamePriorityNames) {
if (first == second && first != SourceType.DEFAULT) {
if (first == SourceType.DEFAULT) {
return false;
}
if (first == second) {
return replaceSamePriorityNames;
}
if (first == SourceType.USER_DEFINED) {
return (second == SourceType.IMPORTED || second == SourceType.ANALYSIS ||
second == SourceType.DEFAULT);
}
if (first == SourceType.IMPORTED) {
return (second == SourceType.ANALYSIS || second == SourceType.DEFAULT);
}
if (first == SourceType.ANALYSIS) {
return (second == SourceType.DEFAULT);
}
return false;
return first.isHigherPriorityThan(second);
}
private boolean isFirstHigherPriorityWhenImportedPriority(SourceType first, SourceType second,
boolean replaceSamePriorityNames) {
if (first == second && first != SourceType.DEFAULT) {
if (first == SourceType.DEFAULT) {
return false;
}
if (first == second) {
return replaceSamePriorityNames;
}
// NOTE: If a new SourceType is added with a priority in between IMPORTED and USER_DEFINED
// VT and this code will need to change.
if (first == SourceType.IMPORTED) {
return (second == SourceType.USER_DEFINED || second == SourceType.ANALYSIS ||
second == SourceType.DEFAULT);
return true; // IMPORTED is highest priority
}
if (first == SourceType.USER_DEFINED) {
return (second == SourceType.ANALYSIS || second == SourceType.DEFAULT);
}
if (first == SourceType.ANALYSIS) {
return (second == SourceType.DEFAULT);
}
return false;
return first.isHigherPriorityThan(second);
}
private void replaceParameterComments(Function toFunction, CommentChoices commentChoice) {

View file

@ -585,12 +585,9 @@ public class FunctionDB extends DatabaseObject implements Function {
if (Undefined.isUndefined(variableDataType)) {
return;
}
SourceType type = SourceType.ANALYSIS;
if (variableSourceType != type && variableSourceType.isHigherPriorityThan(type)) {
type = variableSourceType;
}
if (type.isHigherPriorityThan(getStoredSignatureSource())) {
setSignatureSource(type);
// TODO: It seems that the lowest parameter priority should win out (see GP-6013)
if (variableSourceType.isHigherPriorityThan(getStoredSignatureSource())) {
setSignatureSource(variableSourceType);
}
}
@ -604,14 +601,14 @@ public class FunctionDB extends DatabaseObject implements Function {
boolean isReturnUndefined = Undefined.isUndefined(returnType);
SourceType type = isReturnUndefined ? SourceType.DEFAULT : SourceType.ANALYSIS;
// TODO: It seems that the lowest parameter priority should win out (see GP-6013)
Parameter[] parameters = getParameters();
for (Parameter parameter : parameters) {
if (Undefined.isUndefined(parameter.getDataType())) {
continue;
}
SourceType paramSourceType = parameter.getSource();
if (paramSourceType != SourceType.ANALYSIS &&
paramSourceType.isHigherPriorityThan(SourceType.ANALYSIS)) {
if (paramSourceType.isHigherOrEqualPriorityThan(SourceType.IMPORTED)) {
type = paramSourceType;
}
else {
@ -1458,7 +1455,7 @@ public class FunctionDB extends DatabaseObject implements Function {
symbolMap.put(s, paramDb);
}
if (source.isHigherPriorityThan(getStoredSignatureSource())) {
if (source != getStoredSignatureSource()) {
setSignatureSource(source);
}

View file

@ -42,7 +42,7 @@ public enum SourceType {
}
/**
* Determines if this source type is a higher priority than the one being
* Determine if this source type has a higher priority than the one being
* passed to this method as a parameter.
* USER_DEFINED objects are higher priority than IMPORTED objects which are higher
* priority than ANALYSIS objects which are higher priority than DEFAULT objects.
@ -55,7 +55,20 @@ public enum SourceType {
}
/**
* Determines if this source type is a lower priority than the one being
* Determine if this source type has the same or higher priority than the one being
* passed to this method as a parameter.
* USER_DEFINED objects are higher priority than IMPORTED objects which are higher
* priority than ANALYSIS objects which are higher priority than DEFAULT objects.
* @param source the source type whose priority is to be compared with this one's.
* @return true if this source type is a higher priority.
* false if this source type is the same priority or lower priority.
*/
public boolean isHigherOrEqualPriorityThan(SourceType source) {
return this.priority >= source.priority;
}
/**
* Determine if this source type has a lower priority than the one being
* passed to this method as a parameter.
* DEFAULT objects are lower priority than ANALYSIS objects which are lower
* priority than IMPORTED objects which are lower priority than USER_DEFINED objects.
@ -66,4 +79,17 @@ public enum SourceType {
public boolean isLowerPriorityThan(SourceType source) {
return this.priority < source.priority;
}
/**
* Determine if this source type has the same or lower priority than the one being
* passed to this method as a parameter.
* DEFAULT objects are lower priority than ANALYSIS objects which are lower
* priority than IMPORTED objects which are lower priority than USER_DEFINED objects.
* @param source the source type whose priority is to be compared with this one's.
* @return true if this source type is a lower priority.
* false if this source type is the same priority or higher priority.
*/
public boolean isLowerOrEqualPriorityThan(SourceType source) {
return this.priority <= source.priority;
}
}