Skip to content

Commit

Permalink
stabs: Bail out by default if the analyzer has already been run
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoticgd committed Feb 20, 2023
1 parent 5a9ecac commit 4b57d82
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class StabsAnalyzer extends AbstractAnalyzer {
" instructions there).\n\n" +
"For more information see:\n" +
"https://github.com/chaoticgd/ccc";

public static final String OPTION_IMPORT_FUNCTIONS = "Import Functions";
public static final String OPTION_IMPORT_FUNCTIONS_DESC =
"Import functions from the symbol table into Ghidra.";
Expand All @@ -43,6 +43,10 @@ public class StabsAnalyzer extends AbstractAnalyzer {
public static final String OPTION_LINE_NUMBERS_DESC =
"Output source line numbers as end-of-line comments that will appear in the diassembly.";

public static final String OPTION_ONLY_RUN_ONCE = "Only Run Once";
public static final String OPTION_ONLY_RUN_ONCE_DESC =
"Bail out if over 50% of the recovered types already exist to prevent the user from accidentally corrupting their file.";

public static final String OPTION_OVERRIDE_ELF_PATH = "Override ELF Path (Optional)";
public static final String OPTION_OVERRIDE_ELF_PATH_DESC =
"Use and ELF file of your choice as input to stdump instead of the currently loaded program.";
Expand Down Expand Up @@ -86,6 +90,7 @@ public void registerOptions(Options options, Program program) {
options.registerOption(OPTION_IMPORT_GLOBALS, DEFAULT_OPTIONS.importGlobals, null, OPTION_IMPORT_GLOBALS_DESC);
options.registerOption(OPTION_INLINED_CODE, DEFAULT_OPTIONS.markInlinedCode, null, OPTION_INLINED_CODE_DESC);
options.registerOption(OPTION_LINE_NUMBERS, DEFAULT_OPTIONS.outputLineNumbers, null, OPTION_LINE_NUMBERS_DESC);
options.registerOption(OPTION_ONLY_RUN_ONCE, DEFAULT_OPTIONS.onlyRunOnce, null, OPTION_ONLY_RUN_ONCE_DESC);
options.registerOption(OPTION_OVERRIDE_ELF_PATH, DEFAULT_OPTIONS.overrideElfPath, null, OPTION_OVERRIDE_ELF_PATH_DESC);
options.registerOption(OPTION_OVERRIDE_JSON_PATH, DEFAULT_OPTIONS.overrideJsonPath, null, OPTION_OVERRIDE_JSON_PATH_DESC);
}
Expand All @@ -96,6 +101,7 @@ public void optionsChanged(Options options, Program program) {
importOptions.importGlobals = options.getBoolean(OPTION_IMPORT_GLOBALS, DEFAULT_OPTIONS.importGlobals);
importOptions.markInlinedCode = options.getBoolean(OPTION_INLINED_CODE, DEFAULT_OPTIONS.markInlinedCode);
importOptions.outputLineNumbers = options.getBoolean(OPTION_LINE_NUMBERS, DEFAULT_OPTIONS.outputLineNumbers);
importOptions.onlyRunOnce = options.getBoolean(OPTION_ONLY_RUN_ONCE, DEFAULT_OPTIONS.onlyRunOnce);
importOptions.overrideElfPath = options.getString(OPTION_OVERRIDE_ELF_PATH, DEFAULT_OPTIONS.overrideElfPath);
importOptions.overrideJsonPath = options.getString(OPTION_OVERRIDE_JSON_PATH, DEFAULT_OPTIONS.overrideJsonPath);
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/ghidra/emotionengine/symboltable/StabsImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ghidra.program.model.address.AddressSet;
import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.DataTypeManager;
import ghidra.program.model.data.DataUtilities;
import ghidra.program.model.data.DataUtilities.ClearDataMode;
import ghidra.program.model.data.PointerDataType;
Expand All @@ -47,6 +48,7 @@ public static class ImportOptions {
boolean importGlobals = true;
boolean markInlinedCode = true;
boolean outputLineNumbers = true;
boolean onlyRunOnce = true;
String overrideElfPath = "";
String overrideJsonPath = "";
}
Expand Down Expand Up @@ -139,6 +141,7 @@ public boolean doImport() {
cleanupTemporaryFiles();

if(monitor.isCancelled()) {
log.appendMsg("STABS", "Import operation cancelled by user.");
return false;
}

Expand All @@ -152,7 +155,14 @@ public boolean doImport() {
return false;
}

if(options.onlyRunOnce && shouldBailOut(program, ast)) {
log.appendMsg("STABS", "Import operation cancelled since it has already been run.");
return false;
}


if(monitor.isCancelled()) {
log.appendMsg("STABS", "Import operation cancelled by user.");
return false;
}

Expand Down Expand Up @@ -184,6 +194,24 @@ public void cleanupTemporaryFiles() {
temporaryFiles.clear();
}

public boolean shouldBailOut(Program program, StdumpAST.ParsedJsonFile ast) {
if(ast.deduplicatedTypes.size() < 10) {
return false;
}
DataTypeManager dataTypeManager = program.getDataTypeManager();
int existingTypes = 0;
int newTypes = 0;
for(StdumpAST.Node node : ast.deduplicatedTypes) {
if(dataTypeManager.getDataType("/" + node.name) != null) {
existingTypes++;
} else {
newTypes++;
}
}
return existingTypes > newTypes;
}


public void importDataTypes(StdumpAST.ImporterState importer) {
int type_count = importer.ast.deduplicatedTypes.size();

Expand Down

0 comments on commit 4b57d82

Please sign in to comment.