GP-51: Review fixes

This commit is contained in:
Ryan Kurtz 2022-12-20 07:20:22 -05:00
parent 6a0037b69b
commit a2660f5d0d
3 changed files with 30 additions and 27 deletions

View file

@ -54,7 +54,7 @@ public class CoffBinaryAnalysisCommand extends FlatProgramAPI
ByteProvider provider =
MemoryByteProvider.createDefaultAddressSpaceByteProvider(program, false);
return new CoffFileHeader(provider).isValid(provider);
return CoffFileHeader.isValid(provider);
}
catch (Exception e) {
return false;

View file

@ -56,7 +56,7 @@ public class CoffFileHeader implements StructConverter {
}
}
private BinaryReader getBinaryReader(ByteProvider provider) {
private static BinaryReader getBinaryReader(ByteProvider provider) {
BinaryReader reader = new BinaryReader(provider, true/*COFF is always LE!!!*/);
return reader;
}
@ -272,21 +272,28 @@ public class CoffFileHeader implements StructConverter {
}
/**
* Tests if this {@link CoffFileHeader} is valid.
* Tests if the given {@link ByteProvider} is a valid {@link CoffFileHeader}.
* <p>
* To avoid false positives when the machine type is
* {@link CoffMachineType#IMAGE_FILE_MACHINE_UNKNOWN}, we do an additional check on some extra
* bytes at the beginning of the given {@link ByteProvider} to make sure the entire file isn't
* all 0's.
*
* @param provider The {@link ByteProvider} that this {@link CoffFileHeader} was created from
* @param provider The {@link ByteProvider} to check
* @return True if this is a is a valid {@link CoffFileHeader}; otherwise, false
* @throws IOException if there was an IO-related issue
*/
public boolean isValid(ByteProvider provider) throws IOException {
public static boolean isValid(ByteProvider provider) throws IOException {
final int MIN_BYTE_LENGTH = 22;
final int COFF_NULL_SANITY_CHECK_LEN = 64;
if (getMagic() == CoffMachineType.IMAGE_FILE_MACHINE_UNKNOWN /* ie. == 0 */ &&
if (provider.length() < MIN_BYTE_LENGTH) {
return false;
}
short magic = getBinaryReader(provider).readShort(0);
if (magic == CoffMachineType.IMAGE_FILE_MACHINE_UNKNOWN /* ie. == 0 */ &&
provider.length() > COFF_NULL_SANITY_CHECK_LEN) {
byte[] headerBytes = provider.readBytes(0, COFF_NULL_SANITY_CHECK_LEN);
boolean allZeros = true;
@ -301,7 +308,7 @@ public class CoffFileHeader implements StructConverter {
}
}
return CoffMachineType.isMachineTypeDefined(getMagic());
return CoffMachineType.isMachineTypeDefined(magic);
}
@Override

View file

@ -52,8 +52,6 @@ public class CoffLoader extends AbstractLibrarySupportLoader {
// properly with external symbols laid down
private static final int EMPTY_START_OFFSET = 0x2000;
private static final long MIN_BYTE_LENGTH = 22;
/**
* @return true if this loader assumes the Microsoft variant of the COFF format
*/
@ -96,13 +94,11 @@ public class CoffLoader extends AbstractLibrarySupportLoader {
public Collection<LoadSpec> findSupportedLoadSpecs(ByteProvider provider) throws IOException {
List<LoadSpec> loadSpecs = new ArrayList<>();
if (provider.length() < MIN_BYTE_LENGTH) {
if (!CoffFileHeader.isValid(provider)) {
return loadSpecs;
}
CoffFileHeader header = new CoffFileHeader(provider);
if (header.isValid(provider)) {
header.parseSectionHeaders(provider);
if (isVisualStudio(header) != isMicrosoftFormat()) {
@ -118,7 +114,7 @@ public class CoffLoader extends AbstractLibrarySupportLoader {
if (loadSpecs.isEmpty()) {
loadSpecs.add(new LoadSpec(this, header.getImageBase(false), true));
}
}
return loadSpecs;
}