Ghidra is a software reverse engineering (SRE) framework
GT-3374_emteere several bugs and inneficiencies causing analysis to fail for programs with debug info
emteere67d774fa42d6
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.javaindex a5604ec4a1..163bcd9e6b 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java@@ -50,8 +50,8 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {private Function referencedFunction;private List<Address> referringThunkAddresses = new ArrayList<>();private boolean checkForSideEffects = true;-- private static final int MAX_NUMBER_OF_THUNKING_INSTRUCTIONS = 5;++ private static final int MAX_NUMBER_OF_THUNKING_INSTRUCTIONS = 8;static String DEFAULT_FUNCTION_COMMENT = " THUNK-FUNCTION";@@ -147,7 +147,7 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {Program program = (Program) obj;FunctionManager functionMgr = program.getFunctionManager();-+if (referencedFunctionAddr == Address.NO_ADDRESS) {referencedFunctionAddr = null;}@@ -269,10 +269,10 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {}referencedFunctionAddr = referencedSymbol.getAddress();}- else if ((referencedFunctionAddr == null || referencedFunctionAddr == Address.NO_ADDRESS) && autoThunkOK) {+ else if ((referencedFunctionAddr == null || referencedFunctionAddr == Address.NO_ADDRESS) &&+ autoThunkOK) {// first try to get the address that is already there- referencedFunctionAddr =- getThunkedExternalFunctionAddress(program, entry);+ referencedFunctionAddr = getThunkedExternalFunctionAddress(program, entry);if (referencedFunctionAddr == null) {referencedFunctionAddr = getThunkedAddr(program, entry, checkForSideEffects);}@@ -280,7 +280,8 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {if (referencedFunctionAddr == null || referencedFunctionAddr == Address.NO_ADDRESS) {try {if (resolveComputableFlow(program, entry, monitor)) {- referencedFunctionAddr = getThunkedAddr(program, entry, checkForSideEffects);+ referencedFunctionAddr =+ getThunkedAddr(program, entry, checkForSideEffects);}}catch (CancelledException e) {@@ -479,7 +480,6 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {return referencedFunction;}-/*** if the code starting at entry is a thunk, return the thunked addess if known.*@@ -500,7 +500,8 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {** @return address that the thunk thunks,Address.NO_ADDRESS if thunk but unknown addr, null otherwise*/- public static Address getThunkedAddr(Program program, Address entry, boolean checkForSideEffects) {+ public static Address getThunkedAddr(Program program, Address entry,+ boolean checkForSideEffects) {// General algorithm://// get function, if has no other calls, and no other flow@@ -566,8 +567,8 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {}// record any used registers, checking for use of an unexpected unset register- if (!addRegisterUsage(program, setAtStartRegisters, setRegisters, usedRegisters, pcodeOp,- allow8bitNonUse)) {+ if (checkForSideEffects && !addRegisterUsage(program, setAtStartRegisters,+ setRegisters, usedRegisters, pcodeOp, allow8bitNonUse)) {return null;}}@@ -586,8 +587,8 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {}// reached a flow, end of the line, gotta see what we have- return getFlowingAddrFromFinalState(program, instr, flowType,- checkForSideEffects, setRegisters, usedRegisters);+ return getFlowingAddrFromFinalState(program, instr, flowType, checkForSideEffects,+ setRegisters, usedRegisters);}return null;@@ -664,8 +665,8 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {}private static Address getFlowingAddrFromFinalState(Program program, Instruction instr,- FlowType flowType, boolean checkForSideEffects,- HashSet<Varnode> setRegisters, HashSet<Varnode> usedRegisters) {+ FlowType flowType, boolean checkForSideEffects, HashSet<Varnode> setRegisters,+ HashSet<Varnode> usedRegisters) {// conditional jumps can't be thunks.// any other flow, not good@@ -736,8 +737,8 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {* false if input register found that was not initialized*/private static boolean addRegisterUsage(Program program, HashSet<Varnode> setAtStartRegisters,- HashSet<Varnode> setRegisters, HashSet<Varnode> usedRegisters,- PcodeOp pcode, boolean allow8bitNonUse) {+ HashSet<Varnode> setRegisters, HashSet<Varnode> usedRegisters, PcodeOp pcode,+ boolean allow8bitNonUse) {int opcode = pcode.getOpcode();Varnode output = pcode.getOutput();diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfDecodeContext.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfDecodeContext.javaindex 99ce9e7980..e4177210af 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfDecodeContext.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfDecodeContext.java@@ -1,6 +1,5 @@/* ###* IP: GHIDRA- * REVIEWED: YES** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.@@ -21,7 +20,6 @@ import ghidra.program.model.listing.Function;import ghidra.program.model.listing.Program;import ghidra.program.model.mem.MemBuffer;import ghidra.program.model.mem.MemoryBlock;-import ghidra.program.util.ProgramLocation;/*** Organizational class to record vital data used by a DwarfEHDecoder.@@ -33,12 +31,10 @@ public class DwarfDecodeContext {private final MemoryBlock ehBlock;private final Address functionEntryPoint;- private final ProgramLocation loc;private Object decodedValue;private int encodedLength;private MemBuffer buffer;-/*** Constructs a Dwarf decode context.* @param program the program containing the encoded data@@ -85,7 +81,8 @@ public class DwarfDecodeContext {* @param ehBlock the exception handling memory block* @param entryPoint the associated function's entry point*/- public DwarfDecodeContext(Program program, Address readAddr, MemoryBlock ehBlock, Address entryPoint) {+ public DwarfDecodeContext(Program program, Address readAddr, MemoryBlock ehBlock,+ Address entryPoint) {if (program == null) {throw new NullPointerException("DwarfDecodeContext requires a program");@@ -99,7 +96,6 @@ public class DwarfDecodeContext {this.ehBlock = ehBlock;this.functionEntryPoint = entryPoint;- this.loc = new ProgramLocation(program, addr);}/**@@ -124,16 +120,6 @@ public class DwarfDecodeContext {this.addr = buffer.getAddress();this.ehBlock = ehBlock;this.functionEntryPoint = entryPoint;-- this.loc = new ProgramLocation(program, addr);- }-- /**- * Gets the location of the encoded data.- * @return the program location- */- public ProgramLocation getProgramLocation() {- return loc;}/**diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/GccAnalysisClass.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/GccAnalysisClass.javaindex 119ac4a1a6..7c396224fd 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/GccAnalysisClass.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/GccAnalysisClass.java@@ -21,6 +21,7 @@ import ghidra.program.model.address.Address;import ghidra.program.model.data.*;import ghidra.program.model.listing.CodeUnit;import ghidra.program.model.listing.Program;+import ghidra.program.model.util.CodeUnitInsertionException;import ghidra.util.task.TaskMonitor;/**@@ -80,9 +81,14 @@ public abstract class GccAnalysisClass {* @param dt the type for the data*/protected static void createData(Program program, Address addr, DataType dt) {-- CreateDataCmd dataCmd = new CreateDataCmd(addr, dt);- dataCmd.applyTo(program);+ try {+ // try creating without clearing, the code units should be clear+ program.getListing().createData(addr, dt);+ }+ catch (CodeUnitInsertionException | DataTypeConflictException e) {+ CreateDataCmd dataCmd = new CreateDataCmd(addr, dt);+ dataCmd.applyTo(program);+ }}/**diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/sections/DebugFrameSection.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/sections/DebugFrameSection.javaindex 236288d4f5..82335f41a3 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/sections/DebugFrameSection.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/sections/DebugFrameSection.java@@ -1,6 +1,5 @@/* ###* IP: GHIDRA- * REVIEWED: YES** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.@@ -84,18 +83,21 @@ public class DebugFrameSection extends AbstractFrameSection {}private List<RegionDescriptor> analyzeSection(MemoryBlock curMemBlock)- throws MemoryAccessException, AddressOutOfBoundsException, ExceptionHandlerFrameException {-+ throws MemoryAccessException, AddressOutOfBoundsException,+ ExceptionHandlerFrameException {monitor.setMessage("Analyzing " + curMemBlock.getName() + " section");monitor.setShowProgressValue(true);- monitor.setIndeterminate(true);+ monitor.setIndeterminate(false);ProgramLocation loc = new ProgramLocation(program, curMemBlock.getStart());Address curAddress = loc.getAddress();List<RegionDescriptor> regions = new ArrayList<>();+ if (curAddress != null) {+ monitor.setMaximum(curMemBlock.getEnd().subtract(curAddress));+ }while (curAddress != null && curAddress.compareTo(curMemBlock.getEnd()) < 0) {@@ -112,8 +114,12 @@ public class DebugFrameSection extends AbstractFrameSection {/** Add each Frame Description Entry (FDE) for the current CIE.*/+ List<RegionDescriptor> newRegions = new ArrayList<>();+while (curAddress != null && (curAddress.compareTo(curMemBlock.getEnd()) < 0)) {+ monitor.setProgress(curAddress.subtract(loc.getAddress()));+Address currFdeAddr = curAddress;try {@@ -126,23 +132,24 @@ public class DebugFrameSection extends AbstractFrameSection {}if (region != null) {- regions.add(region);+ newRegions.add(region);createFdeComment(curAddress);- monitor.incrementProgress(1);}curAddress = fde.getNextAddress(); // This can be null.- } catch (ExceptionHandlerFrameException efe) {+ }+ catch (ExceptionHandlerFrameException efe) {// May have run into another CIE.curAddress = currFdeAddr;break;}}- createAugmentationData(regions, cie);+ createAugmentationData(newRegions, cie);++ regions.addAll(newRegions);}return regions;-}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/structures/ehFrame/FrameDescriptionEntry.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/structures/ehFrame/FrameDescriptionEntry.javaindex 18b9d868f9..619bce2f2a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/structures/ehFrame/FrameDescriptionEntry.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/structures/ehFrame/FrameDescriptionEntry.java@@ -17,6 +17,7 @@ package ghidra.app.plugin.exceptionhandlers.gcc.structures.ehFrame;import ghidra.app.cmd.comments.SetCommentCmd;import ghidra.app.cmd.data.CreateArrayCmd;+import ghidra.app.cmd.data.CreateDataCmd;import ghidra.app.cmd.function.CreateFunctionCmd;import ghidra.app.plugin.exceptionhandlers.gcc.*;import ghidra.app.plugin.exceptionhandlers.gcc.datatype.UnsignedLeb128DataType;@@ -29,6 +30,7 @@ import ghidra.program.model.listing.*;import ghidra.program.model.mem.*;import ghidra.program.model.scalar.Scalar;import ghidra.program.model.symbol.*;+import ghidra.program.model.util.CodeUnitInsertionException;import ghidra.util.Msg;import ghidra.util.exception.InvalidInputException;import ghidra.util.task.TaskMonitor;@@ -265,8 +267,11 @@ public class FrameDescriptionEntry extends GccAnalysisClass {DataType encodedDt = cie.getFDEDecoder().getDataType(program);createAndCommentData(program, addr, encodedDt, comment, CodeUnit.EOL_COMMENT);- program.getReferenceManager().addMemoryReference(addr, pcBeginAddr, RefType.DATA,- SourceType.ANALYSIS, 0);+ if (pcBeginAddr.getOffset() != 0x0) {+ pcBeginAddr = pcBeginAddr.add(program.getImageBase().getOffset());+ program.getReferenceManager().addMemoryReference(addr, pcBeginAddr, RefType.DATA,+ SourceType.ANALYSIS, 0);+ }curSize += encodedLen;return addr.add(encodedLen);@@ -385,9 +390,15 @@ public class FrameDescriptionEntry extends GccAnalysisClass {CreateArrayCmd arrayCmd = null;// Create initial instructions array with remaining bytes.- int instructionLength = intLength - curSize;- arrayCmd = new CreateArrayCmd(addr, instructionLength, new ByteDataType(), BYTE_LEN);- arrayCmd.applyTo(program);+ int instructionLength = intLength - curSize;+ ArrayDataType adt = new ArrayDataType(ByteDataType.dataType, instructionLength, BYTE_LEN);+ try {+ program.getListing().createData(addr, adt, adt.getLength());+ }+ catch (CodeUnitInsertionException e) {+ CreateDataCmd dataCmd = new CreateDataCmd(addr, adt);+ dataCmd.applyTo(program);+ }SetCommentCmd.createComment(program, addr, "(FDE) Call Frame Instructions",CodeUnit.EOL_COMMENT);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/demangler/DemangledThunk.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/demangler/DemangledThunk.javaindex 291beb1e86..f1daaf45c4 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/demangler/DemangledThunk.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/demangler/DemangledThunk.java@@ -17,6 +17,7 @@ package ghidra.app.util.demangler;import ghidra.app.cmd.disassemble.DisassembleCommand;import ghidra.app.cmd.function.CreateFunctionCmd;+import ghidra.app.cmd.function.CreateThunkFunctionCmd;import ghidra.program.model.address.*;import ghidra.program.model.lang.InstructionContext;import ghidra.program.model.listing.*;@@ -110,8 +111,7 @@ public class DemangledThunk extends DemangledObject {function.setThunkedFunction(thunkedFunction);}- Symbol s =- applyDemangledName(thunkAddress, function.isThunk(), false, program);+ Symbol s = applyDemangledName(thunkAddress, function.isThunk(), false, program);return s != null;}@@ -181,8 +181,15 @@ public class DemangledThunk extends DemangledObject {}Symbol s = SymbolUtilities.getExpectedLabelOrFunctionSymbol(program,- thunkedFunctionObject.originalMangled, err -> Msg.error(this, err));+ thunkedFunctionObject.originalMangled, err -> Msg.warn(this, err));+ if (s == null) {+ Address thunkedAddr =+ CreateThunkFunctionCmd.getThunkedAddr(program, thunkAddress, false);+ if (thunkedAddr != null) {+ s = program.getSymbolTable().getPrimarySymbol(thunkedAddr);+ }+ }if (s == null || !block.contains(s.getAddress())) {return null;}diff --git a/Ghidra/Features/GnuDemangler/src/main/java/ghidra/app/util/demangler/gnu/GnuDemanglerParser.java b/Ghidra/Features/GnuDemangler/src/main/java/ghidra/app/util/demangler/gnu/GnuDemanglerParser.javaindex bdb3f9153b..bd6eddc2a7 100644--- a/Ghidra/Features/GnuDemangler/src/main/java/ghidra/app/util/demangler/gnu/GnuDemanglerParser.java+++ b/Ghidra/Features/GnuDemangler/src/main/java/ghidra/app/util/demangler/gnu/GnuDemanglerParser.java@@ -502,6 +502,9 @@ public class GnuDemanglerParser implements DemanglerParser {if (pos == -1) {throw new RuntimeException();}+ if (str.endsWith(")")) {+ throw new RuntimeException();+ }DemangledObject dobj = parse(null, str.substring(0, pos));if (dobj == null) {diff --git a/Ghidra/Features/GnuDemangler/src/test/java/ghidra/app/util/demangler/GnuDemanglerParserTest.java b/Ghidra/Features/GnuDemangler/src/test/java/ghidra/app/util/demangler/GnuDemanglerParserTest.javaindex 7be2635d0d..325910193a 100644--- a/Ghidra/Features/GnuDemangler/src/test/java/ghidra/app/util/demangler/GnuDemanglerParserTest.java+++ b/Ghidra/Features/GnuDemangler/src/test/java/ghidra/app/util/demangler/GnuDemanglerParserTest.java@@ -62,6 +62,8 @@ public class GnuDemanglerParserTest extends AbstractGenericTest {"_ZTv0_n24_NSt19basic_ostringstreamIcSt11char_traitsIcE14pool_allocatorIcEED0Ev");// virtual thunkdemangle(process, parser, "_ZTch0_h16_NK8KHotKeys13WindowTrigger4copyEPNS_10ActionDataE");// covariant return thunk+ demangle(process, parser, "_ZNK2cc14ScrollSnapTypeneERKS0_");+List<String> list = loadTextResource(GnuDemanglerParserTest.class, "libMagick.symbols.txt");for (String mangled : list) {if (mangled == null) {@@ -73,8 +75,8 @@ public class GnuDemanglerParserTest extends AbstractGenericTest {System.out.println("Elapsed Time: " + (System.currentTimeMillis() - start));}- private void demangle(GnuDemanglerNativeProcess process, GnuDemanglerParser parser, String mangled)- throws IOException {+ private void demangle(GnuDemanglerNativeProcess process, GnuDemanglerParser parser,+ String mangled) throws IOException {String demangled = process.demangle(mangled);assertNotNull(demangled);assertNotEquals(mangled, demangled);@@ -165,6 +167,16 @@ public class GnuDemanglerParserTest extends AbstractGenericTest {"covariant return thunk [nv:0] [nv:16] to KHotKeys::WindowTrigger::copy(KHotKeys::ActionData*) const");assertTrue(parse instanceof DemangledThunk);assertName(parse, "copy", "KHotKeys", "WindowTrigger");++ try {+ parse = parser.parse(+ "_ZZN12GrGLFunctionIFPKhjEEC1IZN13skia_bindings28CreateGLES2InterfaceBindingsEPN3gpu5gles214GLES2InterfaceEPNS6_14ContextSupportEE3$_0EET_ENUlPKvjE_8__invokeESF_j",+ "GrGLFunction<unsigned char const* (unsigned int)>::GrGLFunction<skia_bindings::CreateGLES2InterfaceBindings(gpu::gles2::GLES2Interface*, gpu::ContextSupport*)::$_0>(skia_bindings::CreateGLES2InterfaceBindings(gpu::gles2::GLES2Interface*, gpu::ContextSupport*)::$_0)::{lambda(void const*, unsigned int)#1}::__invoke(void const*, unsigned int)");+ assertNull("Shouldn't have parsed", parser);+ }+ catch (Exception exc) {+ // should get an exception+ }}private void assertName(DemangledObject demangledObj, String name, String... namespaces) {@@ -193,9 +205,8 @@ public class GnuDemanglerParserTest extends AbstractGenericTest {DemangledObject object = parser.parse(mangled, demangled);assertTrue(object instanceof DemangledFunction);- assertEquals(- "undefined glob_fn9(" +- "char,int,long,long long,unsigned int,unsigned long,float,double,long double,bool,void *,void * *)",+ assertEquals("undefined glob_fn9(" ++ "char,int,long,long long,unsigned int,unsigned long,float,double,long double,bool,void *,void * *)",object.getSignature(false));}diff --git a/Ghidra/Processors/ARM/data/languages/ARMTHUMBinstructions.sinc b/Ghidra/Processors/ARM/data/languages/ARMTHUMBinstructions.sincindex 51abf6007c..31a363f889 100644--- a/Ghidra/Processors/ARM/data/languages/ARMTHUMBinstructions.sinc+++ b/Ghidra/Processors/ARM/data/languages/ARMTHUMBinstructions.sinc@@ -1358,7 +1358,8 @@ with : ARMcondCk=1 {:hlt immed6 is TMode=1 & op6=0b1011101010 & immed6{- software_hlt(immed6:4);+ pc = software_hlt(immed6:4);+ goto [pc];}:bkpt immed8 is TMode=1 & ItCond & op8=0xbe & immed8@@ -1384,14 +1385,16 @@ with : ARMcondCk=1 {:udf^ItCond "#"thc0007 is TMode=1 & ItCond & op8 = 0xde & thc0007{build ItCond;- software_udf(thc0007:4);+ pc = software_udf(thc0007:4);+ goto [pc];}@endif:udf^ItCond "#"tmp is TMode=1 & ItCond & op4=0xf7f & thc0003; op12=0xa & thc0011 [tmp = thc0003 << 12 | thc0011;]{build ItCond;- software_udf(tmp:4);+ pc = software_udf(tmp:4);+ goto [pc];}@endif # VERSION_5