Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1272 Fixes bad setting internals of shared global static Dwarf parsing instances. If any dwarf parsing occurred, even on the same thread, that changed the nature of a shared decoder instance then bad indeterminate behavior would occur.

emteerecf7a4f5767bf
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/AbstractDwarfEHDecoder.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/AbstractDwarfEHDecoder.java
index 70af6d9c43..741ca0dba2 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/AbstractDwarfEHDecoder.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/AbstractDwarfEHDecoder.java
@@ -38,20 +38,15 @@ abstract class AbstractDwarfEHDecoder implements DwarfEHDecoder {
protected static SignedLeb128DataType SLEB_DATA_TYPE = SignedLeb128DataType.dataType;
protected static UnsignedLeb128DataType ULEB_DATA_TYPE = UnsignedLeb128DataType.dataType;
- protected DwarfEHDataApplicationMode appMode = DwarfEHDataApplicationMode.DW_EH_PE_absptr;
+ protected final DwarfEHDataApplicationMode appMode;
+ protected final boolean isIndirect;
- protected boolean isIndirect = false;
-
- @Override
- public void setApplicationMode(DwarfEHDataApplicationMode mode) {
+ public AbstractDwarfEHDecoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
if (mode == null) {
mode = DwarfEHDataApplicationMode.DW_EH_PE_absptr;
}
appMode = mode;
- }
- @Override
- public void setIndirect(boolean isIndirect) {
this.isIndirect = isIndirect;
}
@@ -91,8 +86,8 @@ abstract class AbstractDwarfEHDecoder implements DwarfEHDecoder {
return base;
default:
- throw new AddressTranslationException("Don't know how to make a " +
- addr.getPointerSize() + "-byte pointer");
+ throw new AddressTranslationException(
+ "Don't know how to make a " + addr.getPointerSize() + "-byte pointer");
}
}
@@ -113,8 +108,8 @@ abstract class AbstractDwarfEHDecoder implements DwarfEHDecoder {
case 8:
return readQWord(buf);
default:
- throw new AddressTranslationException("Don't know how to make a " + ptrSize +
- "-byte pointer");
+ throw new AddressTranslationException(
+ "Don't know how to make a " + ptrSize + "-byte pointer");
}
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfDecoderFactory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfDecoderFactory.java
index 2f3a0ba71d..e29bdcbcea 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfDecoderFactory.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfDecoderFactory.java
@@ -15,8 +15,7 @@
*/
package ghidra.app.plugin.exceptionhandlers.gcc;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
import ghidra.app.plugin.exceptionhandlers.gcc.datatype.SignedLeb128DataType;
import ghidra.app.plugin.exceptionhandlers.gcc.datatype.UnsignedLeb128DataType;
@@ -33,46 +32,8 @@ import ghidra.program.model.scalar.Scalar;
*/
public class DwarfDecoderFactory {
- private static final Map<DwarfEHDataDecodeFormat, DwarfEHDecoder> decoderMap = new HashMap<>();
- static {
- decoderMap.put(DwarfEHDataDecodeFormat.DW_EH_PE_absptr, new DW_EH_PE_absptr_Decoder());
- decoderMap.put(DwarfEHDataDecodeFormat.DW_EH_PE_uleb128, new DW_EH_PE_uleb128_Decoder());
-
- decoderMap.put(DwarfEHDataDecodeFormat.DW_EH_PE_udata2, new DW_EH_PE_udata2_Decoder());
- decoderMap.put(DwarfEHDataDecodeFormat.DW_EH_PE_udata4, new DW_EH_PE_udata4_Decoder());
- decoderMap.put(DwarfEHDataDecodeFormat.DW_EH_PE_udata8, new DW_EH_PE_udata8_Decoder());
-
- decoderMap.put(DwarfEHDataDecodeFormat.DW_EH_PE_signed, new DW_EH_PE_signed_Decoder());
- decoderMap.put(DwarfEHDataDecodeFormat.DW_EH_PE_sleb128, new DW_EH_PE_sleb128_Decoder());
-
- decoderMap.put(DwarfEHDataDecodeFormat.DW_EH_PE_sdata2, new DW_EH_PE_sdata2_Decoder());
- decoderMap.put(DwarfEHDataDecodeFormat.DW_EH_PE_sdata4, new DW_EH_PE_sdata4_Decoder());
- decoderMap.put(DwarfEHDataDecodeFormat.DW_EH_PE_sdata8, new DW_EH_PE_sdata8_Decoder());
-
- decoderMap.put(DwarfEHDataDecodeFormat.DW_EH_PE_omit, new DW_EH_PE_omit_Decoder());
- }
-
- /**
- * Get the appropriate decoder for the given decode format and application mode
- * @param decodeFormat the exception handling data decoding format
- * @param appFormat the desired application mode
- * @param isIndirect flag indicating whether or not the data is an indirect reference
- * @return the decoder or null
- */
- public static DwarfEHDecoder getDecoder(DwarfEHDataDecodeFormat decodeFormat,
- DwarfEHDataApplicationMode appFormat, boolean isIndirect) {
-
- DwarfEHDecoder decoder = decoderMap.get(decodeFormat);
- if (decoder == null) {
- return null;
- }
-
- decoder.setApplicationMode(appFormat);
-
- decoder.setIndirect(isIndirect);
-
- return decoder;
- }
+ private static final ConcurrentHashMap<Integer, DwarfEHDecoder> decoderMap =
+ new ConcurrentHashMap<>();
/**
* Get the appropriate decoder for the given 8-bit mode; mode is parsed into
@@ -83,24 +44,66 @@ public class DwarfDecoderFactory {
*/
public static DwarfEHDecoder getDecoder(int mode) {
+ DwarfEHDecoder dwarfEHDecoder = decoderMap.get(mode);
+ if (dwarfEHDecoder != null) {
+ return dwarfEHDecoder;
+ }
+
int format = mode & 0x0F;
int appl = mode & 0x70;
boolean isIndirect = (mode & 0x80) == 0x80;
+ DwarfEHDataDecodeFormat style = DwarfEHDataDecodeFormat.valueOf(format);
+ DwarfEHDataApplicationMode mod = DwarfEHDataApplicationMode.valueOf(appl);
+
if ((mode & 0xFF) == 0xFF) {
- DwarfEHDecoder decoder = decoderMap.get(DwarfEHDataDecodeFormat.DW_EH_PE_omit);
- decoder.setApplicationMode(DwarfEHDataApplicationMode.DW_EH_PE_omit);
- return decoder;
+ dwarfEHDecoder =
+ new DW_EH_PE_omit_Decoder(DwarfEHDataApplicationMode.DW_EH_PE_omit, false);
+ }
+ else {
+ dwarfEHDecoder = createDecoder(style, mod, isIndirect);
}
- DwarfEHDataDecodeFormat style = DwarfEHDataDecodeFormat.valueOf(format);
- DwarfEHDataApplicationMode mod = DwarfEHDataApplicationMode.valueOf(appl);
+ decoderMap.put(mode, dwarfEHDecoder);
+
+ return dwarfEHDecoder;
+ }
- return getDecoder(style, mod, isIndirect);
+ private static DwarfEHDecoder createDecoder(DwarfEHDataDecodeFormat style,
+ DwarfEHDataApplicationMode mod, boolean isIndirect) {
+ switch (style) {
+ case DW_EH_PE_absptr:
+ return new DW_EH_PE_absptr_Decoder(mod, isIndirect);
+ case DW_EH_PE_uleb128:
+ return new DW_EH_PE_uleb128_Decoder(mod, isIndirect);
+ case DW_EH_PE_udata2:
+ return new DW_EH_PE_udata2_Decoder(mod, isIndirect);
+ case DW_EH_PE_udata4:
+ return new DW_EH_PE_udata4_Decoder(mod, isIndirect);
+ case DW_EH_PE_udata8:
+ return new DW_EH_PE_udata8_Decoder(mod, isIndirect);
+ case DW_EH_PE_signed:
+ return new DW_EH_PE_signed_Decoder(mod, isIndirect);
+ case DW_EH_PE_sleb128:
+ return new DW_EH_PE_sleb128_Decoder(mod, isIndirect);
+ case DW_EH_PE_sdata2:
+ return new DW_EH_PE_sdata2_Decoder(mod, isIndirect);
+ case DW_EH_PE_sdata4:
+ return new DW_EH_PE_sdata4_Decoder(mod, isIndirect);
+ case DW_EH_PE_sdata8:
+ return new DW_EH_PE_sdata8_Decoder(mod, isIndirect);
+ case DW_EH_PE_omit:
+ default:
+ return new DW_EH_PE_omit_Decoder(mod, isIndirect);
+ }
}
private static abstract class AbstractSignedDwarEHfDecoder extends AbstractDwarfEHDecoder {
+ public AbstractSignedDwarEHfDecoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public boolean isSigned() {
return true;
@@ -109,6 +112,10 @@ public class DwarfDecoderFactory {
private static abstract class AbstractUnsignedDwarfEHDecoder extends AbstractDwarfEHDecoder {
+ public AbstractUnsignedDwarfEHDecoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public boolean isSigned() {
return false;
@@ -117,6 +124,10 @@ public class DwarfDecoderFactory {
static final class DW_EH_PE_absptr_Decoder extends AbstractUnsignedDwarfEHDecoder {
+ public DW_EH_PE_absptr_Decoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public DwarfEHDataDecodeFormat getDataFormat() {
return DwarfEHDataDecodeFormat.DW_EH_PE_absptr;
@@ -174,6 +185,10 @@ public class DwarfDecoderFactory {
static final class DW_EH_PE_omit_Decoder extends AbstractUnsignedDwarfEHDecoder {
+ public DW_EH_PE_omit_Decoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public DwarfEHDataDecodeFormat getDataFormat() {
return DwarfEHDataDecodeFormat.DW_EH_PE_omit;
@@ -200,6 +215,10 @@ public class DwarfDecoderFactory {
static final class DW_EH_PE_uleb128_Decoder extends AbstractUnsignedDwarfEHDecoder {
+ public DW_EH_PE_uleb128_Decoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public DwarfEHDataDecodeFormat getDataFormat() {
return DwarfEHDataDecodeFormat.DW_EH_PE_uleb128;
@@ -238,6 +257,10 @@ public class DwarfDecoderFactory {
static final class DW_EH_PE_udata2_Decoder extends AbstractUnsignedDwarfEHDecoder {
+ public DW_EH_PE_udata2_Decoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public DwarfEHDataDecodeFormat getDataFormat() {
return DwarfEHDataDecodeFormat.DW_EH_PE_udata2;
@@ -268,6 +291,10 @@ public class DwarfDecoderFactory {
static final class DW_EH_PE_udata4_Decoder extends AbstractUnsignedDwarfEHDecoder {
+ public DW_EH_PE_udata4_Decoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public DwarfEHDataDecodeFormat getDataFormat() {
return DwarfEHDataDecodeFormat.DW_EH_PE_udata4;
@@ -298,6 +325,10 @@ public class DwarfDecoderFactory {
static final class DW_EH_PE_udata8_Decoder extends AbstractUnsignedDwarfEHDecoder {
+ public DW_EH_PE_udata8_Decoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public DwarfEHDataDecodeFormat getDataFormat() {
return DwarfEHDataDecodeFormat.DW_EH_PE_udata8;
@@ -328,6 +359,10 @@ public class DwarfDecoderFactory {
static final class DW_EH_PE_signed_Decoder extends AbstractSignedDwarEHfDecoder {
+ public DW_EH_PE_signed_Decoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public DwarfEHDataDecodeFormat getDataFormat() {
return DwarfEHDataDecodeFormat.DW_EH_PE_signed;
@@ -352,6 +387,10 @@ public class DwarfDecoderFactory {
static final class DW_EH_PE_sleb128_Decoder extends AbstractSignedDwarEHfDecoder {
+ public DW_EH_PE_sleb128_Decoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public DwarfEHDataDecodeFormat getDataFormat() {
return DwarfEHDataDecodeFormat.DW_EH_PE_sleb128;
@@ -389,6 +428,10 @@ public class DwarfDecoderFactory {
static final class DW_EH_PE_sdata2_Decoder extends AbstractSignedDwarEHfDecoder {
+ public DW_EH_PE_sdata2_Decoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public DwarfEHDataDecodeFormat getDataFormat() {
return DwarfEHDataDecodeFormat.DW_EH_PE_sdata2;
@@ -419,6 +462,10 @@ public class DwarfDecoderFactory {
static final class DW_EH_PE_sdata4_Decoder extends AbstractSignedDwarEHfDecoder {
+ public DW_EH_PE_sdata4_Decoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public DwarfEHDataDecodeFormat getDataFormat() {
return DwarfEHDataDecodeFormat.DW_EH_PE_sdata4;
@@ -449,6 +496,10 @@ public class DwarfDecoderFactory {
static final class DW_EH_PE_sdata8_Decoder extends AbstractSignedDwarEHfDecoder {
+ public DW_EH_PE_sdata8_Decoder(DwarfEHDataApplicationMode mode, boolean isIndirect) {
+ super(mode, isIndirect);
+ }
+
@Override
public DwarfEHDataDecodeFormat getDataFormat() {
return DwarfEHDataDecodeFormat.DW_EH_PE_sdata8;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfEHDecoder.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfEHDecoder.java
index e6e440b394..7b00b040b0 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfEHDecoder.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/DwarfEHDecoder.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.
@@ -80,17 +79,4 @@ public interface DwarfEHDecoder {
* @return the data type.
*/
public DataType getDataType(Program program);
-
- /**
- * Sets the exception handling data application mode.
- * @param mode the application mode
- */
- public void setApplicationMode(DwarfEHDataApplicationMode mode);
-
- /**
- * Sets whether or not the data is an indirect reference.
- * @param isIndirect true for an indirect reference.
- */
- public void setIndirect(boolean isIndirect);
-
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/structures/ehFrame/FdeTable.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/structures/ehFrame/FdeTable.java
index f0a63bfbdc..b7a27f2bda 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/structures/ehFrame/FdeTable.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/exceptionhandlers/gcc/structures/ehFrame/FdeTable.java
@@ -67,19 +67,19 @@ public class FdeTable {
TaskMonitor monitor;
Program prog;
StructureDataType fdeTableEntry;
-
+
/**
* Constructor for an FDE table.
*
* @param monitor a status monitor for indicating progress or allowing a task to be cancelled.
* @param curProg the program containing the FDE table.
*/
- public FdeTable( TaskMonitor monitor, Program curProg ) {
+ public FdeTable(TaskMonitor monitor, Program curProg) {
this.monitor = monitor;
this.prog = curProg;
this.fdeTableEntry = new StructureDataType("fde_table_entry", 0);
}
-
+
private void initFdeTableDataType(DwarfEHDecoder decoder) throws ExceptionHandlerFrameException {
DataType encodedDt = decoder.getDataType(prog);
@@ -101,11 +101,11 @@ public class FdeTable {
fdeTableEntry.add(encodedDt, "initial_loc", "Initial Location");
fdeTableEntry.add(encodedDt, "data_loc", "Data location");
-
+
DataTypeManager dtManager = prog.getDataTypeManager();
- dtManager.addDataType(fdeTableEntry, DataTypeConflictHandler.REPLACE_HANDLER );
+ dtManager.addDataType(fdeTableEntry, DataTypeConflictHandler.REPLACE_HANDLER);
}
-
+
/**
* Creates an FDE Table at the specified Address.
*
@@ -119,13 +119,13 @@ public class FdeTable {
throws MemoryAccessException, ExceptionHandlerFrameException {
CreateStructureCmd dataCmd = null;
long curFdeTableCnt = 0;
-
- if (addr == null || decoder == null || monitor.isCancelled()) {
+
+ if (addr == null || decoder == null) {
return;
}
-
+
initFdeTableDataType(decoder);
-
+
monitor.setMessage("Creating Frame Description Table Entries");
monitor.setShowProgressValue(true);
monitor.setIndeterminate(false);
@@ -134,13 +134,17 @@ public class FdeTable {
/* Create a new FDE structures beginning at startAddress */
MemoryBlock curMemBlock = prog.getMemory().getBlock(".eh_frame_hdr");
while( curMemBlock != null &&
- (addr.compareTo( curMemBlock.getEnd()) < 0) &&
- (curFdeTableCnt < fdeTableCnt) )
+ (addr.compareTo( curMemBlock.getEnd()) < 0) &&
+ (curFdeTableCnt < fdeTableCnt) )
{
+ if (monitor.isCancelled()) {
+ return;
+ }
+
/* Create a new FDE structure */
- dataCmd = new CreateStructureCmd( fdeTableEntry, addr);
+ dataCmd = new CreateStructureCmd(fdeTableEntry, addr);
dataCmd.applyTo(prog);
-
+
/*
* -- Create references to the 'initial location' and 'data
* location' --
@@ -159,7 +163,7 @@ public class FdeTable {
// TODO: This should be a CODE flow, leaving as INDIRECTION until refactor
prog.getReferenceManager().addMemoryReference(locComponentAddr, locAddr,
RefType.INDIRECTION,
- SourceType.ANALYSIS, 0);
+ SourceType.ANALYSIS, 0);
DataTypeComponent dataComponent = fdeStruct.getComponent(1);
Address dataComponentAddr = addr.add(dataComponent.getOffset());
@@ -168,7 +172,7 @@ public class FdeTable {
Address dataAddr = decoder.decodeAddress(dataDecodeContext);
prog.getReferenceManager().addMemoryReference(dataComponentAddr, dataAddr, RefType.DATA,
- SourceType.ANALYSIS, 0);
+ SourceType.ANALYSIS, 0);
/* Increment curAddress by number of bytes in a FDE Table entry */
curFdeTableCnt++;