mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2026-09-25 17:00:36 -09:00
GP-2070 addressing code review comments
This commit is contained in:
@@ -17,6 +17,9 @@ package ghidra.app.plugin.core.string.variadic;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import ghidra.program.database.ProgramBuilder;
|
||||
@@ -25,29 +28,36 @@ import ghidra.program.model.address.AddressFactory;
|
||||
import ghidra.program.model.data.*;
|
||||
import ghidra.program.model.listing.Program;
|
||||
import ghidra.test.AbstractProgramBasedTest;
|
||||
import ghidra.util.Msg;
|
||||
|
||||
public class FormatStringFinderTest extends AbstractProgramBasedTest {
|
||||
|
||||
private static final int BASE_ADDRESS = 0x10000;
|
||||
private static final int LENGTH = 0x1000;
|
||||
|
||||
private static final byte[] SIMPLE_FORMAT =
|
||||
new byte[] { 'l', 'e', 'n', 'g', 't', 'h', ':', ' ', '%', 'd' };
|
||||
private static final String SIMPLE_FORMAT_STRING = "length: %d";
|
||||
private static final byte[] SIMPLE_FORMAT_BYTES =
|
||||
SIMPLE_FORMAT_STRING.getBytes(StandardCharsets.US_ASCII);
|
||||
|
||||
private static final int SIMPLE_NON_FORMAT_START = 0x10010;
|
||||
private static final byte[] SIMPLE_NON_FORMAT = new byte[] { 'h', 'e', 'l', 'l', 'o' };
|
||||
private static final String SIMPLE_NON_FORMAT_STRING = "hello";
|
||||
private static final byte[] SIMPLE_NON_FORMAT_BYTES =
|
||||
SIMPLE_NON_FORMAT_STRING.getBytes(StandardCharsets.US_ASCII);
|
||||
|
||||
private static final int WIDE_CHAR_STRING_START = 0x10020;
|
||||
private static final byte[] WIDE_CHAR_STRING = new byte[] { '%', 0, 0, 0, 's', 0, 0, 0 };
|
||||
private static final String WCHAR_EXAMPLE = "%s";
|
||||
private boolean wcharEncoded = false;
|
||||
|
||||
private static final int SHORT_FORMAT_STRING_START = 0x10030;
|
||||
private static final int SHORT_NEAR_END_START = BASE_ADDRESS + LENGTH - 4;
|
||||
private static final byte[] SHORT_FORMAT_STRING = new byte[] { '%', 'd' };
|
||||
private static final String SHORT_FORMAT_STRING = "%d";
|
||||
private static final byte[] SHORT_FORMAT_STRING_BYTES =
|
||||
SHORT_FORMAT_STRING.getBytes(StandardCharsets.US_ASCII);
|
||||
|
||||
private static final int ANSI_COLOR_CODE_STRING_START = 0x10040;
|
||||
private static final byte[] ANSI_COLOR_CODE_STRING =
|
||||
new byte[] { 'r', 'e', 'p', 'r', 'o', 'd', 'u', 'c', 'e', 's', ' ', 'e', 'r', 'r', 'o', 'r',
|
||||
' ', ':', ' ', '%', 's', '!', '\n', 0x1b, '[', '0', 'm' };
|
||||
private static final int ANSI_COLOR_CODE_START = 0x10040;
|
||||
private static final String ANSI_COLOR_CODE_STRING = "reproduces error : %s!\n\u001b[0m";
|
||||
private static final byte[] ANSI_COLOR_CODE_BYTES =
|
||||
ANSI_COLOR_CODE_STRING.getBytes(StandardCharsets.US_ASCII);
|
||||
|
||||
@Test
|
||||
public void testNullTerminatedStringFinder() throws Exception {
|
||||
@@ -61,27 +71,29 @@ public class FormatStringFinderTest extends AbstractProgramBasedTest {
|
||||
|
||||
Address base = addrFactory.getConstantAddress(BASE_ADDRESS);
|
||||
String simpleFormat = parser.findNullTerminatedString(base, charPointer);
|
||||
assertEquals(new String(SIMPLE_FORMAT), simpleFormat);
|
||||
assertEquals(SIMPLE_FORMAT_STRING, simpleFormat);
|
||||
|
||||
Address simpleNonFormatAddr = addrFactory.getConstantAddress(SIMPLE_NON_FORMAT_START);
|
||||
String simpleNonFormat = parser.findNullTerminatedString(simpleNonFormatAddr, charPointer);
|
||||
assertEquals(new String(SIMPLE_NON_FORMAT), simpleNonFormat);
|
||||
assertEquals(SIMPLE_NON_FORMAT_STRING, simpleNonFormat);
|
||||
|
||||
Address wideCharStringAddr = addrFactory.getConstantAddress(WIDE_CHAR_STRING_START);
|
||||
String wide = parser.findNullTerminatedString(wideCharStringAddr, widePointer);
|
||||
assertEquals("%s", wide);
|
||||
if (wcharEncoded) {
|
||||
Address wideCharStringAddr = addrFactory.getConstantAddress(WIDE_CHAR_STRING_START);
|
||||
String wide = parser.findNullTerminatedString(wideCharStringAddr, widePointer);
|
||||
assertEquals(WCHAR_EXAMPLE, wide);
|
||||
}
|
||||
|
||||
Address shortAddr = addrFactory.getConstantAddress(SHORT_FORMAT_STRING_START);
|
||||
String shortString = parser.findNullTerminatedString(shortAddr, charPointer);
|
||||
assertEquals(new String(SHORT_FORMAT_STRING), shortString);
|
||||
assertEquals(SHORT_FORMAT_STRING, shortString);
|
||||
|
||||
Address shortNearEndAddr = addrFactory.getConstantAddress(SHORT_NEAR_END_START);
|
||||
shortString = parser.findNullTerminatedString(shortNearEndAddr, charPointer);
|
||||
assertEquals(new String(SHORT_FORMAT_STRING), shortString);
|
||||
assertEquals(SHORT_FORMAT_STRING, shortString);
|
||||
|
||||
Address ansiColorAddr = addrFactory.getConstantAddress(ANSI_COLOR_CODE_STRING_START);
|
||||
Address ansiColorAddr = addrFactory.getConstantAddress(ANSI_COLOR_CODE_START);
|
||||
String ansiColorString = parser.findNullTerminatedString(ansiColorAddr, charPointer);
|
||||
assertEquals(new String(ANSI_COLOR_CODE_STRING), ansiColorString);
|
||||
assertEquals(ANSI_COLOR_CODE_STRING, ansiColorString);
|
||||
|
||||
}
|
||||
|
||||
@@ -92,13 +104,20 @@ public class FormatStringFinderTest extends AbstractProgramBasedTest {
|
||||
builder = new ProgramBuilder("test", ProgramBuilder._X64, "gcc", null);
|
||||
builder.createMemory("test", Integer.toHexString(BASE_ADDRESS), LENGTH, "test",
|
||||
(byte) 0x0);
|
||||
builder.setBytes(Integer.toHexString(BASE_ADDRESS), SIMPLE_FORMAT);
|
||||
builder.setBytes(Integer.toHexString(SIMPLE_NON_FORMAT_START), SIMPLE_NON_FORMAT);
|
||||
builder.setBytes(Integer.toHexString(WIDE_CHAR_STRING_START), WIDE_CHAR_STRING);
|
||||
builder.setBytes(Integer.toHexString(SHORT_FORMAT_STRING_START), SHORT_FORMAT_STRING);
|
||||
builder.setBytes(Integer.toHexString(SHORT_NEAR_END_START), SHORT_FORMAT_STRING);
|
||||
builder.setBytes(Integer.toHexString(ANSI_COLOR_CODE_STRING_START),
|
||||
ANSI_COLOR_CODE_STRING);
|
||||
builder.setBytes(Integer.toHexString(BASE_ADDRESS), SIMPLE_FORMAT_BYTES);
|
||||
builder.setBytes(Integer.toHexString(SIMPLE_NON_FORMAT_START), SIMPLE_NON_FORMAT_BYTES);
|
||||
try {
|
||||
builder.setBytes(Integer.toHexString(WIDE_CHAR_STRING_START),
|
||||
WCHAR_EXAMPLE.getBytes("UTF-32LE"));
|
||||
wcharEncoded = true;
|
||||
}
|
||||
catch (UnsupportedEncodingException e) {
|
||||
Msg.warn(this, e.getMessage());
|
||||
}
|
||||
builder.setBytes(Integer.toHexString(SHORT_FORMAT_STRING_START),
|
||||
SHORT_FORMAT_STRING_BYTES);
|
||||
builder.setBytes(Integer.toHexString(SHORT_NEAR_END_START), SHORT_FORMAT_STRING_BYTES);
|
||||
builder.setBytes(Integer.toHexString(ANSI_COLOR_CODE_START), ANSI_COLOR_CODE_BYTES);
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail("Exception creating testing program: " + e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user