Updated Enums to handle old types that have a mix of values that contain

negative numbers and high unsigned values
This commit is contained in:
dragonmacher
2023-08-04 15:43:12 -04:00
parent daabec6aab
commit b3fbdd3c09
4 changed files with 52 additions and 15 deletions

View File

@@ -112,12 +112,16 @@ class EnumDB extends DataTypeDB implements Enum {
long minValue = valueMap.firstKey(); long minValue = valueMap.firstKey();
long maxValue = valueMap.lastKey(); long maxValue = valueMap.lastKey();
if (maxValue > getMaxPossibleValue(length, true)) {
if (minValue < 0) {
return INVALID;
}
return UNSIGNED;
}
if (minValue < 0) { if (minValue < 0) {
return SIGNED; return SIGNED;
} }
if (maxValue > getMaxPossibleValue(length, true)) {
return UNSIGNED;
}
return NONE; // we have no negatives and no large unsigned values return NONE; // we have no negatives and no large unsigned values
} }
@@ -874,6 +878,19 @@ class EnumDB extends DataTypeDB implements Enum {
} }
} }
@Override
public EnumSignedState getSignedState() {
lock.acquire();
try {
checkIsValid();
initializeIfNeeded();
return signedState;
}
finally {
lock.release();
}
}
@Override @Override
public int getMinimumPossibleLength() { public int getMinimumPossibleLength() {
lock.acquire(); lock.acquire();

View File

@@ -18,13 +18,14 @@ package ghidra.program.database.data;
/** /**
* Keeps track of the signed state of an enum datatype. Enum are fundamentally either signed or * Keeps track of the signed state of an enum datatype. Enum are fundamentally either signed or
* unsigned, but sometimes you can't tell based on the values they contain. Once a negative value * unsigned, but sometimes you can't tell based on the values they contain. Once a negative value
* is added, then the enum becomes locked as signed, preventing high unsigned values from being * is added, then the enum becomes locked as signed, preventing high unsigned values (those values
* added. Once a high value unsigned value is added, then it becomes locked as unsigned value. If * that are too big for signed value of the enum size) from being added. Once a high value unsigned
* neither a negative value or high unsigned value has been added, then the enum is not locked as * value is added, then it becomes locked as unsigned value. If neither a negative value or high
* either signed or unsigned. * unsigned value has been added, then the enum is not locked as either signed or unsigned.
*/ */
public enum EnumSignedState { public enum EnumSignedState {
SIGNED, // Enum contains at least 1 negative value, preventing high unsigned values SIGNED, // Enum contains at least 1 negative value, preventing high unsigned values
UNSIGNED, // Enum contains at least 1 high unsigned value, preventing negative values UNSIGNED, // Enum contains at least 1 high unsigned value, preventing negative values
NONE // Enum contains neither a negative or a high unsigned value, so can go either way NONE, // Enum contains neither a negative or a high unsigned value, so can go either way
INVALID // Enum contains both negative and high unsigned values; can happen with old types
} }

View File

@@ -19,6 +19,7 @@ import java.math.BigInteger;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import ghidra.docking.settings.Settings; import ghidra.docking.settings.Settings;
import ghidra.program.database.data.EnumSignedState;
public interface Enum extends DataType { public interface Enum extends DataType {
@@ -133,6 +134,12 @@ public interface Enum extends DataType {
*/ */
public boolean isSigned(); public boolean isSigned();
/**
* Returns the signed state.
* @return the signed state.
*/
public EnumSignedState getSignedState();
/** /**
* Returns the maximum value that this enum can represent based on its size and signedness. * Returns the maximum value that this enum can represent based on its size and signedness.
* @return the maximum value that this enum can represent based on its size and signedness. * @return the maximum value that this enum can represent based on its size and signedness.

View File

@@ -148,6 +148,11 @@ public class EnumDataType extends GenericDataType implements Enum {
@Override @Override
public void add(String valueName, long value, String comment) { public void add(String valueName, long value, String comment) {
doAdd(valueName, value, comment);
signedState = computeSignedness();
}
private void doAdd(String valueName, long value, String comment) {
bitGroups = null; bitGroups = null;
checkValue(value); checkValue(value);
if (nameMap.containsKey(valueName)) { if (nameMap.containsKey(valueName)) {
@@ -161,8 +166,6 @@ public class EnumDataType extends GenericDataType implements Enum {
if (!StringUtils.isBlank(comment)) { if (!StringUtils.isBlank(comment)) {
commentMap.put(valueName, comment); commentMap.put(valueName, comment);
} }
signedState = computeSignedness();
} }
private EnumSignedState computeSignedness() { private EnumSignedState computeSignedness() {
@@ -172,12 +175,16 @@ public class EnumDataType extends GenericDataType implements Enum {
long minValue = valueMap.firstKey(); long minValue = valueMap.firstKey();
long maxValue = valueMap.lastKey(); long maxValue = valueMap.lastKey();
if (maxValue > getMaxPossibleValue(length, true)) {
if (minValue < 0) {
return INVALID;
}
return UNSIGNED;
}
if (minValue < 0) { if (minValue < 0) {
return SIGNED; return SIGNED;
} }
if (maxValue > getMaxPossibleValue(length, true)) {
return UNSIGNED;
}
return NONE; // we have no negatives and no large unsigned values return NONE; // we have no negatives and no large unsigned values
} }
@@ -277,6 +284,11 @@ public class EnumDataType extends GenericDataType implements Enum {
return signedState == SIGNED; return signedState == SIGNED;
} }
@Override
public EnumSignedState getSignedState() {
return signedState;
}
@Override @Override
public long getMinPossibleValue() { public long getMinPossibleValue() {
return getMinPossibleValue(length, signedState != UNSIGNED); return getMinPossibleValue(length, signedState != UNSIGNED);
@@ -503,10 +515,10 @@ public class EnumDataType extends GenericDataType implements Enum {
commentMap = new HashMap<>(); commentMap = new HashMap<>();
setLength(enumm.getLength()); setLength(enumm.getLength());
String[] names = enumm.getNames(); String[] names = enumm.getNames();
signedState = enumm.getSignedState();
for (String valueName : names) { for (String valueName : names) {
add(valueName, enumm.getValue(valueName), enumm.getComment(valueName)); doAdd(valueName, enumm.getValue(valueName), enumm.getComment(valueName));
} }
computeSignedness();
} }
@Override @Override