|
| 1 | +/* ### |
| 2 | + * IP: GHIDRA |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package ghidra.app.plugin.core.string.variadic; |
| 17 | + |
| 18 | +import static org.junit.Assert.*; |
| 19 | + |
| 20 | +import java.io.UnsupportedEncodingException; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | + |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import ghidra.program.database.ProgramBuilder; |
| 26 | +import ghidra.program.model.address.Address; |
| 27 | +import ghidra.program.model.address.AddressFactory; |
| 28 | +import ghidra.program.model.data.*; |
| 29 | +import ghidra.program.model.listing.Program; |
| 30 | +import ghidra.test.AbstractProgramBasedTest; |
| 31 | +import ghidra.util.Msg; |
| 32 | + |
| 33 | +public class FormatStringFinderTest extends AbstractProgramBasedTest { |
| 34 | + |
| 35 | + private static final int BASE_ADDRESS = 0x10000; |
| 36 | + private static final int LENGTH = 0x1000; |
| 37 | + |
| 38 | + private static final String SIMPLE_FORMAT_STRING = "length: %d"; |
| 39 | + private static final byte[] SIMPLE_FORMAT_BYTES = |
| 40 | + SIMPLE_FORMAT_STRING.getBytes(StandardCharsets.US_ASCII); |
| 41 | + |
| 42 | + private static final int SIMPLE_NON_FORMAT_START = 0x10010; |
| 43 | + private static final String SIMPLE_NON_FORMAT_STRING = "hello"; |
| 44 | + private static final byte[] SIMPLE_NON_FORMAT_BYTES = |
| 45 | + SIMPLE_NON_FORMAT_STRING.getBytes(StandardCharsets.US_ASCII); |
| 46 | + |
| 47 | + private static final int WIDE_CHAR_STRING_START = 0x10020; |
| 48 | + private static final String WCHAR_EXAMPLE = "%s"; |
| 49 | + private boolean wcharEncoded = false; |
| 50 | + |
| 51 | + private static final int SHORT_FORMAT_STRING_START = 0x10030; |
| 52 | + private static final int SHORT_NEAR_END_START = BASE_ADDRESS + LENGTH - 4; |
| 53 | + private static final String SHORT_FORMAT_STRING = "%d"; |
| 54 | + private static final byte[] SHORT_FORMAT_STRING_BYTES = |
| 55 | + SHORT_FORMAT_STRING.getBytes(StandardCharsets.US_ASCII); |
| 56 | + |
| 57 | + private static final int ANSI_COLOR_CODE_START = 0x10040; |
| 58 | + private static final String ANSI_COLOR_CODE_STRING = "reproduces error : %s!\n\u001b[0m"; |
| 59 | + private static final byte[] ANSI_COLOR_CODE_BYTES = |
| 60 | + ANSI_COLOR_CODE_STRING.getBytes(StandardCharsets.US_ASCII); |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testNullTerminatedStringFinder() throws Exception { |
| 64 | + initialize(); |
| 65 | + PcodeFunctionParser parser = new PcodeFunctionParser(program); |
| 66 | + AddressFactory addrFactory = program.getAddressFactory(); |
| 67 | + |
| 68 | + Pointer charPointer = program.getDataTypeManager().getPointer(new CharDataType()); |
| 69 | + Pointer widePointer = program.getDataTypeManager() |
| 70 | + .getPointer(new WideCharDataType(program.getDataTypeManager())); |
| 71 | + |
| 72 | + Address base = addrFactory.getConstantAddress(BASE_ADDRESS); |
| 73 | + String simpleFormat = parser.findNullTerminatedString(base, charPointer); |
| 74 | + assertEquals(SIMPLE_FORMAT_STRING, simpleFormat); |
| 75 | + |
| 76 | + Address simpleNonFormatAddr = addrFactory.getConstantAddress(SIMPLE_NON_FORMAT_START); |
| 77 | + String simpleNonFormat = parser.findNullTerminatedString(simpleNonFormatAddr, charPointer); |
| 78 | + assertEquals(SIMPLE_NON_FORMAT_STRING, simpleNonFormat); |
| 79 | + |
| 80 | + if (wcharEncoded) { |
| 81 | + Address wideCharStringAddr = addrFactory.getConstantAddress(WIDE_CHAR_STRING_START); |
| 82 | + String wide = parser.findNullTerminatedString(wideCharStringAddr, widePointer); |
| 83 | + assertEquals(WCHAR_EXAMPLE, wide); |
| 84 | + } |
| 85 | + |
| 86 | + Address shortAddr = addrFactory.getConstantAddress(SHORT_FORMAT_STRING_START); |
| 87 | + String shortString = parser.findNullTerminatedString(shortAddr, charPointer); |
| 88 | + assertEquals(SHORT_FORMAT_STRING, shortString); |
| 89 | + |
| 90 | + Address shortNearEndAddr = addrFactory.getConstantAddress(SHORT_NEAR_END_START); |
| 91 | + shortString = parser.findNullTerminatedString(shortNearEndAddr, charPointer); |
| 92 | + assertEquals(SHORT_FORMAT_STRING, shortString); |
| 93 | + |
| 94 | + Address ansiColorAddr = addrFactory.getConstantAddress(ANSI_COLOR_CODE_START); |
| 95 | + String ansiColorString = parser.findNullTerminatedString(ansiColorAddr, charPointer); |
| 96 | + assertEquals(ANSI_COLOR_CODE_STRING, ansiColorString); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected Program getProgram() { |
| 102 | + ProgramBuilder builder = null; |
| 103 | + try { |
| 104 | + builder = new ProgramBuilder("test", ProgramBuilder._X64, "gcc", null); |
| 105 | + builder.createMemory("test", Integer.toHexString(BASE_ADDRESS), LENGTH, "test", |
| 106 | + (byte) 0x0); |
| 107 | + builder.setBytes(Integer.toHexString(BASE_ADDRESS), SIMPLE_FORMAT_BYTES); |
| 108 | + builder.setBytes(Integer.toHexString(SIMPLE_NON_FORMAT_START), SIMPLE_NON_FORMAT_BYTES); |
| 109 | + try { |
| 110 | + builder.setBytes(Integer.toHexString(WIDE_CHAR_STRING_START), |
| 111 | + WCHAR_EXAMPLE.getBytes("UTF-32LE")); |
| 112 | + wcharEncoded = true; |
| 113 | + } |
| 114 | + catch (UnsupportedEncodingException e) { |
| 115 | + Msg.warn(this, e.getMessage()); |
| 116 | + } |
| 117 | + builder.setBytes(Integer.toHexString(SHORT_FORMAT_STRING_START), |
| 118 | + SHORT_FORMAT_STRING_BYTES); |
| 119 | + builder.setBytes(Integer.toHexString(SHORT_NEAR_END_START), SHORT_FORMAT_STRING_BYTES); |
| 120 | + builder.setBytes(Integer.toHexString(ANSI_COLOR_CODE_START), ANSI_COLOR_CODE_BYTES); |
| 121 | + } |
| 122 | + catch (Exception e) { |
| 123 | + fail("Exception creating testing program: " + e.getMessage()); |
| 124 | + } |
| 125 | + |
| 126 | + return builder.getProgram(); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments