mirror of
https://github.com/siemens-mobile-hacks/ptr89.git
synced 2026-11-25 18:27:22 +00:00
fix: improve result offsets and WASM performance
This commit is contained in:
parent
2e43c256b1
commit
07dd50c1df
11 changed files with 184 additions and 78 deletions
|
|
@ -10,6 +10,11 @@ include_directories("./lib" "./third_party/argparse/include" "./third_party/json
|
|||
set(SPDLOG_USE_STD_FORMAT ON CACHE BOOL "" FORCE)
|
||||
set(SPDLOG_BUILD_EXAMPLE OFF CACHE BOOL "" FORCE)
|
||||
set(SPDLOG_BUILD_TESTS OFF CACHE BOOL "" FORCE)
|
||||
if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
|
||||
set(SPDLOG_NO_ATOMIC_LEVELS ON CACHE BOOL "" FORCE)
|
||||
set(SPDLOG_NO_THREAD_ID ON CACHE BOOL "" FORCE)
|
||||
set(SPDLOG_NO_TLS ON CACHE BOOL "" FORCE)
|
||||
endif()
|
||||
add_subdirectory(third_party/spdlog EXCLUDE_FROM_ALL)
|
||||
|
||||
set(LIB_SRC lib/src/Arch.cpp lib/src/Pattern.cpp lib/src/Tokenizer.cpp lib/src/Parser.cpp lib/src/utils.cpp)
|
||||
|
|
@ -20,6 +25,9 @@ target_link_libraries(ptr89-lib PUBLIC spdlog::spdlog)
|
|||
|
||||
if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
|
||||
add_executable(ptr89_wasm wasm/main.cpp wasm/Ptr89Wasm.cpp)
|
||||
set_target_properties(ptr89-lib spdlog PROPERTIES
|
||||
INTERPROCEDURAL_OPTIMIZATION TRUE
|
||||
)
|
||||
set_target_properties(ptr89_wasm PROPERTIES
|
||||
INTERPROCEDURAL_OPTIMIZATION TRUE
|
||||
SUFFIX ".js"
|
||||
|
|
|
|||
7
JS.md
7
JS.md
|
|
@ -87,9 +87,10 @@ interface Ptr89SearchResult {
|
|||
}
|
||||
```
|
||||
|
||||
`address` is the resolved address, pointer value or decoded target. `offset` is
|
||||
`address - base` and is omitted when `address` is outside the fullflash.
|
||||
`bytes` is returned only for a regular pattern result.
|
||||
`address` is the resolved address, pointer value or decoded target. For a
|
||||
regular pattern, `offset` points to the matched bytes. For other results it is
|
||||
`address - base` and is omitted when `address` is outside the fullflash. `bytes`
|
||||
is returned only for a regular pattern result.
|
||||
|
||||
### `findXRefs(address, limit?)`
|
||||
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@ For example, a 14 MiB M55 fullflash gets base `0x200000`. An explicit `--base` a
|
|||
$ ptr89 -f M55_v91.bin -A c166 -p '&BL(F0C8F0D9DA257293F0C8F0D9DA9A3293 + C)'
|
||||
Pattern: '&BL(F0C8F0D9DA257293F0C8F0D9DA9A3293 + C)'
|
||||
Found 1 branch:
|
||||
BRANCH OFFSET
|
||||
FUNCTION OFFSET
|
||||
009A9332 007A9332
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -27,14 +27,20 @@ static std::tuple<bool, uint32_t, bool> decodeThumbBL(uint32_t address, const ui
|
|||
int32_t high = static_cast<int32_t>(signExtend(first & 0x7FF, 11, 32) << 12);
|
||||
uint32_t low = (second & 0x7FF) << 1;
|
||||
uint32_t target = (address + 4 + high + low) & 0xFFFFFFFC;
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} BLX #0x{:08X}", address, bytes[0], bytes[1], bytes[2], bytes[3], target);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} BLX #0x{:08X}",
|
||||
address, bytes[0], bytes[1], bytes[2], bytes[3], target);
|
||||
}
|
||||
return { true, target, true };
|
||||
}
|
||||
if ((first & 0xF800) == 0xF000 && (second & 0xF800) == 0xF800) {
|
||||
int32_t high = static_cast<int32_t>(signExtend(first & 0x7FF, 11, 32) << 12);
|
||||
uint32_t low = (second & 0x7FF) << 1;
|
||||
uint32_t target = address + 4 + high + low;
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} BL #0x{:08X}", address, bytes[0], bytes[1], bytes[2], bytes[3], target);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} BL #0x{:08X}",
|
||||
address, bytes[0], bytes[1], bytes[2], bytes[3], target);
|
||||
}
|
||||
return { true, target, false };
|
||||
}
|
||||
return { false, 0, false };
|
||||
|
|
@ -52,7 +58,10 @@ static std::tuple<bool, uint32_t, bool> decodeArmBL(uint32_t address, const uint
|
|||
int32_t displacement = static_cast<int32_t>(signExtend(instruction & 0xFFFFFF, 24, 30) << 2U);
|
||||
uint32_t h = (instruction & 0x01000000) != 0 ? 1 : 0;
|
||||
uint32_t target = address + 8 + displacement + (h << 1);
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} BLX #0x{:08X}", address, bytes[0], bytes[1], bytes[2], bytes[3], target);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} BLX #0x{:08X}",
|
||||
address, bytes[0], bytes[1], bytes[2], bytes[3], target);
|
||||
}
|
||||
return { true, target, true };
|
||||
}
|
||||
if ((instruction & 0x0F000000) == 0x0B000000 || (instruction & 0x0F000000) == 0x0A000000) {
|
||||
|
|
@ -60,8 +69,10 @@ static std::tuple<bool, uint32_t, bool> decodeArmBL(uint32_t address, const uint
|
|||
uint32_t target = address + 8 + displacement;
|
||||
uint32_t condition = (instruction & 0xF0000000) >> 28;
|
||||
bool link = (instruction & 0x0F000000) == 0x0B000000;
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} B{}{} #0x{:08X}", address,
|
||||
bytes[0], bytes[1], bytes[2], bytes[3], link ? "L" : "", MNEMONICS[condition], target);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} B{}{} #0x{:08X}", address,
|
||||
bytes[0], bytes[1], bytes[2], bytes[3], link ? "L" : "", MNEMONICS[condition], target);
|
||||
}
|
||||
return { true, target, false };
|
||||
}
|
||||
return { false, 0, false };
|
||||
|
|
@ -75,14 +86,19 @@ static std::pair<bool, uint32_t> decodeThumbB(uint32_t address, const uint8_t *b
|
|||
if ((instruction & 0xF800) == 0xE000) {
|
||||
int32_t displacement = static_cast<int32_t>(signExtend(instruction & 0x7FF, 11, 32) << 1);
|
||||
uint32_t target = address + 4 + displacement;
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} B #0x{:08X}", address, bytes[0], bytes[1], target);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} B #0x{:08X}", address, bytes[0], bytes[1], target);
|
||||
}
|
||||
return { true, target };
|
||||
}
|
||||
if ((instruction & 0xF000) == 0xD000) {
|
||||
int32_t displacement = static_cast<int32_t>(signExtend(instruction & 0xFF, 8, 32) << 1);
|
||||
uint32_t target = address + 4 + displacement;
|
||||
uint32_t condition = (instruction & 0x0F00) >> 8;
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} B{} #0x{:08X}", address, bytes[0], bytes[1], MNEMONICS[condition], target);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} B{} #0x{:08X}",
|
||||
address, bytes[0], bytes[1], MNEMONICS[condition], target);
|
||||
}
|
||||
return { true, target };
|
||||
}
|
||||
return { false, 0 };
|
||||
|
|
@ -96,8 +112,10 @@ static std::pair<bool, uint32_t> decodeThumbLDR(uint32_t address, const uint8_t
|
|||
uint32_t displacement = (instruction & 0xFF) << 2;
|
||||
uint32_t reg = (instruction & 0x700) >> 8;
|
||||
uint32_t target = address + (address % 4 == 0 ? 4 : 2) + displacement;
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} LDR {}, [PC, #0x{:X}] ; 0x{:08X}", address,
|
||||
bytes[0], bytes[1], REGNAMES[reg], displacement, target);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} LDR {}, [PC, #0x{:X}] ; 0x{:08X}", address,
|
||||
bytes[0], bytes[1], REGNAMES[reg], displacement, target);
|
||||
}
|
||||
return { true, target };
|
||||
}
|
||||
|
||||
|
|
@ -114,9 +132,11 @@ static std::tuple<bool, uint32_t, bool> decodeArmLDR(uint32_t address, const uin
|
|||
uint32_t target = address + 8 + displacement;
|
||||
uint32_t reg = (instruction & 0xF000) >> 12;
|
||||
uint32_t condition = (instruction & 0xF0000000) >> 28;
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} LDR{} {}, [PC, #{}0x{:X}] ; 0x{:08X}", address,
|
||||
bytes[0], bytes[1], bytes[2], bytes[3], MNEMONICS[condition], REGNAMES[reg],
|
||||
add ? '+' : '-', displacement < 0 ? -displacement : displacement, target);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} LDR{} {}, [PC, #{}0x{:X}] ; 0x{:08X}", address,
|
||||
bytes[0], bytes[1], bytes[2], bytes[3], MNEMONICS[condition], REGNAMES[reg],
|
||||
add ? '+' : '-', displacement < 0 ? -displacement : displacement, target);
|
||||
}
|
||||
return { true, target, reg == 0xF };
|
||||
}
|
||||
|
||||
|
|
@ -223,7 +243,9 @@ class ArmArch final: public Arch {
|
|||
if (!success || pointer == address || !Pattern::inMemory(memory, pointer))
|
||||
break;
|
||||
|
||||
spdlog::debug("Found thunk at {:08X}: PC->{:08X}", address, pointer);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("Found thunk at {:08X}: PC->{:08X}", address, pointer);
|
||||
}
|
||||
address = pointer;
|
||||
}
|
||||
return address;
|
||||
|
|
@ -258,8 +280,10 @@ class C166Arch final: public Arch {
|
|||
const uint32_t segment = address & 0xFF0000;
|
||||
const uint16_t targetOffset = static_cast<uint16_t>(address + 2 + displacement);
|
||||
const uint32_t target = segment | targetOffset;
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {} #0x{:08X}", address, bytes[0], bytes[1],
|
||||
opcode == 0xBB ? "CALLR" : "JMPR", target);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {} #0x{:08X}", address, bytes[0], bytes[1],
|
||||
opcode == 0xBB ? "CALLR" : "JMPR", target);
|
||||
}
|
||||
return { true, target };
|
||||
}
|
||||
|
||||
|
|
@ -289,8 +313,10 @@ class C166Arch final: public Arch {
|
|||
return { false, 0 };
|
||||
}
|
||||
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} {} #0x{:08X}", address,
|
||||
bytes[0], bytes[1], bytes[2], bytes[3], mnemonic, target);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} {} #0x{:08X}", address,
|
||||
bytes[0], bytes[1], bytes[2], bytes[3], mnemonic, target);
|
||||
}
|
||||
return { true, target };
|
||||
}
|
||||
|
||||
|
|
@ -372,7 +398,9 @@ class C166Arch final: public Arch {
|
|||
if (!success || target == address || !Pattern::inMemory(memory, target, 2))
|
||||
break;
|
||||
|
||||
spdlog::debug("Found C166 thunk at {:08X}: PC->{:08X}", address, target);
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
spdlog::debug("Found C166 thunk at {:08X}: PC->{:08X}", address, target);
|
||||
}
|
||||
address = target;
|
||||
}
|
||||
return address;
|
||||
|
|
|
|||
|
|
@ -74,33 +74,40 @@ bool Pattern::fuzzyMatch(const uint8_t *bytes, const uint8_t *masks, int pattern
|
|||
}
|
||||
|
||||
bool Pattern::checkPattern(const std::shared_ptr<PtrExp> &pattern, size_t offset, const Memory &memory) {
|
||||
if (spdlog::should_log(spdlog::level::debug))
|
||||
bool debug = spdlog::should_log(spdlog::level::debug);
|
||||
if (debug)
|
||||
spdlog::debug("Checking pattern: '{}' at {:08X}", stringify(pattern), memory.base + offset);
|
||||
|
||||
if (pattern->type == RESULT_TYPE_ADDRESS) {
|
||||
spdlog::debug("Fixed address: {:08X}", pattern->fixedAddress);
|
||||
if (debug)
|
||||
spdlog::debug("Fixed address: {:08X}", pattern->fixedAddress);
|
||||
return true;
|
||||
}
|
||||
|
||||
int patternSize = pattern->bytes.size();
|
||||
if (!patternSize) {
|
||||
spdlog::debug("FAIL: empty pattern!");
|
||||
if (debug)
|
||||
spdlog::debug("FAIL: empty pattern!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (offset > memory.size || static_cast<size_t>(patternSize) > memory.size - offset) {
|
||||
spdlog::debug("FAIL: Address {:08X} is out of range.", memory.base + offset);
|
||||
if (debug)
|
||||
spdlog::debug("FAIL: Address {:08X} is out of range.", memory.base + offset);
|
||||
return false;
|
||||
}
|
||||
if (!fuzzyMatch(&pattern->bytes[0], &pattern->masks[0], patternSize, memory.data + offset)) {
|
||||
spdlog::debug("FAIL: bytes not matched.");
|
||||
if (debug)
|
||||
spdlog::debug("FAIL: bytes not matched.");
|
||||
return false;
|
||||
}
|
||||
if (!checkSubpatterns(pattern, offset, memory)) {
|
||||
spdlog::debug("FAIL: sub patterns not matched.");
|
||||
if (debug)
|
||||
spdlog::debug("FAIL: sub patterns not matched.");
|
||||
return false;
|
||||
}
|
||||
spdlog::debug("Pattern matched!");
|
||||
if (debug)
|
||||
spdlog::debug("Pattern matched!");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -108,7 +115,9 @@ bool Pattern::checkSubpatterns(const std::shared_ptr<PtrExp> &pattern, size_t of
|
|||
if (!pattern->subPatterns.size())
|
||||
return true;
|
||||
|
||||
spdlog::debug("Checking sub patterns...");
|
||||
bool debug = spdlog::should_log(spdlog::level::debug);
|
||||
if (debug)
|
||||
spdlog::debug("Checking sub patterns...");
|
||||
|
||||
const Arch &arch = getArch(memory.arch);
|
||||
for (auto it: pattern->subPatterns) {
|
||||
|
|
@ -147,7 +156,7 @@ bool Pattern::checkSubpatterns(const std::shared_ptr<PtrExp> &pattern, size_t of
|
|||
if (checkPattern(p.pattern, fileOffset, memory))
|
||||
return true;
|
||||
}
|
||||
if (decoded.empty())
|
||||
if (decoded.empty() && debug)
|
||||
spdlog::debug("FAIL: architecture instruction could not be decoded.");
|
||||
}
|
||||
|
||||
|
|
@ -163,13 +172,17 @@ std::tuple<bool, uint32_t, size_t> Pattern::decodeBranchReference(uint32_t offse
|
|||
}
|
||||
|
||||
std::pair<bool, uint32_t> Pattern::decodePointer(uint32_t addr, const Memory &memory) {
|
||||
spdlog::debug("Try decoding pointer at {:08X}", addr);
|
||||
bool debug = spdlog::should_log(spdlog::level::debug);
|
||||
if (debug)
|
||||
spdlog::debug("Try decoding pointer at {:08X}", addr);
|
||||
auto [success, value] = getArch(memory.arch).decodePointer(addr, memory);
|
||||
if (success) {
|
||||
spdlog::debug("Pointer address: {:08X}", value);
|
||||
if (debug)
|
||||
spdlog::debug("Pointer address: {:08X}", value);
|
||||
return { true, value };
|
||||
}
|
||||
spdlog::debug("FAIL: address is out of memory range!");
|
||||
if (debug)
|
||||
spdlog::debug("FAIL: address is out of memory range!");
|
||||
return { false, 0 };
|
||||
}
|
||||
|
||||
|
|
@ -264,24 +277,28 @@ std::vector<Pattern::SearchResult> Pattern::find(const std::shared_ptr<PtrExp> &
|
|||
int patternSize = pattern->bytes.size();
|
||||
|
||||
std::vector<SearchResult> searchResults;
|
||||
bool debug = spdlog::should_log(spdlog::level::debug);
|
||||
|
||||
if (spdlog::should_log(spdlog::level::debug)) {
|
||||
if (debug) {
|
||||
spdlog::debug("Searching pattern: {}", stringify(pattern));
|
||||
spdlog::debug("Memory: {:08X} {:08X}", memory.base, memory.size);
|
||||
}
|
||||
|
||||
if (pattern->type == RESULT_TYPE_ADDRESS) {
|
||||
spdlog::debug("Fixed address: {:08X}", pattern->fixedAddress);
|
||||
if (debug)
|
||||
spdlog::debug("Fixed address: {:08X}", pattern->fixedAddress);
|
||||
searchResults.push_back({ pattern->fixedAddress, 0, 0 });
|
||||
return searchResults;
|
||||
}
|
||||
|
||||
if (!patternSize) {
|
||||
spdlog::debug("FAIL: empty pattern!");
|
||||
if (debug)
|
||||
spdlog::debug("FAIL: empty pattern!");
|
||||
return searchResults;
|
||||
}
|
||||
if (static_cast<size_t>(patternSize) > memory.size) {
|
||||
spdlog::debug("FAIL: pattern is larger than memory!");
|
||||
if (debug)
|
||||
spdlog::debug("FAIL: pattern is larger than memory!");
|
||||
return searchResults;
|
||||
}
|
||||
|
||||
|
|
@ -299,7 +316,8 @@ std::vector<Pattern::SearchResult> Pattern::find(const std::shared_ptr<PtrExp> &
|
|||
if (align != 1)
|
||||
firstNonWildcardByte = 0;
|
||||
|
||||
spdlog::debug("Search align: {}", align);
|
||||
if (debug)
|
||||
spdlog::debug("Search align: {}", align);
|
||||
|
||||
/*
|
||||
* Optimized variant of checkPattern().
|
||||
|
|
@ -309,12 +327,14 @@ std::vector<Pattern::SearchResult> Pattern::find(const std::shared_ptr<PtrExp> &
|
|||
int size = patternSize - firstNonWildcardByte;
|
||||
|
||||
if (size >= 4 && !isTrulyWildcard) { // faster
|
||||
spdlog::debug("Using fast pattern matching algorithm.");
|
||||
if (debug)
|
||||
spdlog::debug("Using fast pattern matching algorithm.");
|
||||
|
||||
uint32_t mask = *reinterpret_cast<uint32_t *>(masks);
|
||||
uint32_t searchValue = *reinterpret_cast<uint32_t *>(bytes) & mask;
|
||||
|
||||
spdlog::debug("Search prefix: mask={:08X}, searchValue={:08X}", mask, searchValue);
|
||||
if (debug)
|
||||
spdlog::debug("Search prefix: mask={:08X}, searchValue={:08X}", mask, searchValue);
|
||||
|
||||
for (size_t i = firstNonWildcardByte; i < memory.size - patternSize + 1; i += align) {
|
||||
uint32_t memoryValue = *reinterpret_cast<const uint32_t *>(memory.data + i);
|
||||
|
|
@ -322,17 +342,20 @@ std::vector<Pattern::SearchResult> Pattern::find(const std::shared_ptr<PtrExp> &
|
|||
if (size == 4 || fuzzyMatch(bytes + 4, masks + 4, size - 4, memory.data + i + 4)) {
|
||||
size_t foundOffset = i - firstNonWildcardByte;
|
||||
|
||||
spdlog::debug("Possible result at {:08X}", memory.base + foundOffset);
|
||||
if (debug)
|
||||
spdlog::debug("Possible result at {:08X}", memory.base + foundOffset);
|
||||
|
||||
if (checkSubpatterns(pattern, foundOffset, memory)) {
|
||||
auto [isDecoded, result] = decodeResult(pattern, foundOffset, memory);
|
||||
if (isDecoded) {
|
||||
searchResults.push_back(result);
|
||||
|
||||
spdlog::debug("FOUND: address={:08X}, offset={:08X}", result.address, result.offset);
|
||||
if (debug)
|
||||
spdlog::debug("FOUND: address={:08X}, offset={:08X}", result.address, result.offset);
|
||||
|
||||
if (maxResults && searchResults.size() >= maxResults) {
|
||||
spdlog::debug("Maximum search results are reached.");
|
||||
if (debug)
|
||||
spdlog::debug("Maximum search results are reached.");
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -345,30 +368,36 @@ std::vector<Pattern::SearchResult> Pattern::find(const std::shared_ptr<PtrExp> &
|
|||
i -= align;
|
||||
}
|
||||
} else {
|
||||
spdlog::debug("FAIL: can't decode result!");
|
||||
if (debug)
|
||||
spdlog::debug("FAIL: can't decode result!");
|
||||
}
|
||||
} else {
|
||||
spdlog::debug("FAIL: sub patterns not matched.");
|
||||
if (debug)
|
||||
spdlog::debug("FAIL: sub patterns not matched.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
spdlog::debug("Using slow pattern matching algorithm.");
|
||||
if (debug)
|
||||
spdlog::debug("Using slow pattern matching algorithm.");
|
||||
|
||||
for (size_t i = firstNonWildcardByte; i < memory.size - patternSize + 1; i += align) {
|
||||
if (fuzzyMatch(bytes, masks, size, memory.data + i)) {
|
||||
size_t foundOffset = i - firstNonWildcardByte;
|
||||
spdlog::debug("Possible result at {:08X}", memory.base + foundOffset);
|
||||
if (debug)
|
||||
spdlog::debug("Possible result at {:08X}", memory.base + foundOffset);
|
||||
if (checkSubpatterns(pattern, foundOffset, memory)) {
|
||||
auto [isDecoded, result] = decodeResult(pattern, foundOffset, memory);
|
||||
if (isDecoded) {
|
||||
searchResults.push_back(result);
|
||||
|
||||
spdlog::debug("FOUND: address={:08X}, offset={:08X}", result.address, result.offset);
|
||||
if (debug)
|
||||
spdlog::debug("FOUND: address={:08X}, offset={:08X}", result.address, result.offset);
|
||||
|
||||
if (maxResults && searchResults.size() >= maxResults) {
|
||||
spdlog::debug("Maximum search results are reached.");
|
||||
if (debug)
|
||||
spdlog::debug("Maximum search results are reached.");
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -381,10 +410,12 @@ std::vector<Pattern::SearchResult> Pattern::find(const std::shared_ptr<PtrExp> &
|
|||
i -= align;
|
||||
}
|
||||
} else {
|
||||
spdlog::debug("FAIL: can't decode result!");
|
||||
if (debug)
|
||||
spdlog::debug("FAIL: can't decode result!");
|
||||
}
|
||||
} else {
|
||||
spdlog::debug("FAIL: sub patterns not matched.");
|
||||
if (debug)
|
||||
spdlog::debug("FAIL: sub patterns not matched.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -394,7 +425,9 @@ std::vector<Pattern::SearchResult> Pattern::find(const std::shared_ptr<PtrExp> &
|
|||
}
|
||||
|
||||
std::vector<Pattern::XRef> Pattern::finXRefs(uint32_t addr, const Memory &memory, size_t maxResults) {
|
||||
spdlog::debug("Searching XRef's for {:08X}", addr);
|
||||
bool debug = spdlog::should_log(spdlog::level::debug);
|
||||
if (debug)
|
||||
spdlog::debug("Searching XRef's for {:08X}", addr);
|
||||
std::vector<XRef> searchResults;
|
||||
for (size_t i = 0; i < memory.size; i += 2) {
|
||||
bool isReference;
|
||||
|
|
@ -405,18 +438,22 @@ std::vector<Pattern::XRef> Pattern::finXRefs(uint32_t addr, const Memory &memory
|
|||
std::tie(isBranchReference, branchAddr, std::ignore) = decodeBranchReference(i, memory);
|
||||
auto [isPointer, ptrAddr] = decodePointer(i + memory.base, memory);
|
||||
if (isBranchReference && (branchAddr & ~1) == (addr & ~1)) {
|
||||
spdlog::debug("FOUND: branch at {:08X}", i + memory.base);
|
||||
if (debug)
|
||||
spdlog::debug("FOUND: branch at {:08X}", i + memory.base);
|
||||
searchResults.push_back({ RESULT_TYPE_BRANCH, static_cast<uint32_t>(memory.base + i), static_cast<uint32_t>(i) });
|
||||
} else if (isReference && (refAddr & ~1) == (addr & ~1)) {
|
||||
spdlog::debug("FOUND: reference at {:08X}", i + memory.base);
|
||||
if (debug)
|
||||
spdlog::debug("FOUND: reference at {:08X}", i + memory.base);
|
||||
searchResults.push_back({ RESULT_TYPE_REFERENCE, static_cast<uint32_t>(memory.base + i), static_cast<uint32_t>(i) });
|
||||
} else if (isPointer && (ptrAddr & ~1) == (addr & ~1)) {
|
||||
spdlog::debug("FOUND: pointer at {:08X}", i + memory.base);
|
||||
if (debug)
|
||||
spdlog::debug("FOUND: pointer at {:08X}", i + memory.base);
|
||||
searchResults.push_back({ RESULT_TYPE_POINTER, static_cast<uint32_t>(memory.base + i), static_cast<uint32_t>(i) });
|
||||
}
|
||||
|
||||
if (maxResults && searchResults.size() >= maxResults) {
|
||||
spdlog::debug("Maximum search results are reached.");
|
||||
if (debug)
|
||||
spdlog::debug("Maximum search results are reached.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@sie-js/ptr89",
|
||||
"version": "3.0.1",
|
||||
"version": "3.0.2",
|
||||
"description": "ARM/THUMB/C166 binary pattern finder",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
|
|
|
|||
10
src/index.ts
10
src/index.ts
|
|
@ -68,6 +68,7 @@ function validateAlign(align: number): void {
|
|||
export class Ptr89 {
|
||||
private module?: WasmModule;
|
||||
private handle?: WasmPtr89;
|
||||
private ptr?: number;
|
||||
|
||||
async open(data: Uint8Array, options: Ptr89OpenOptions = {}): Promise<void> {
|
||||
await this.openData(data.byteLength, options, (module, ptr) => module.HEAPU8.set(data, ptr));
|
||||
|
|
@ -100,8 +101,11 @@ export class Ptr89 {
|
|||
return;
|
||||
this.handle.close();
|
||||
this.handle.delete();
|
||||
if (this.module && this.ptr !== undefined)
|
||||
this.module._free(this.ptr);
|
||||
this.handle = undefined;
|
||||
this.module = undefined;
|
||||
this.ptr = undefined;
|
||||
}
|
||||
|
||||
setDebug(enabled: boolean): void {
|
||||
|
|
@ -191,14 +195,18 @@ export class Ptr89 {
|
|||
try {
|
||||
await write(module, ptr);
|
||||
handle.open(ptr, size, base, arch);
|
||||
if (this.module && this.ptr !== undefined)
|
||||
this.module._free(this.ptr);
|
||||
this.module = module;
|
||||
this.handle = handle;
|
||||
this.ptr = ptr;
|
||||
} catch (error) {
|
||||
if (!this.handle)
|
||||
handle.delete();
|
||||
throw getError(module, error);
|
||||
} finally {
|
||||
module._free(ptr);
|
||||
if (this.ptr !== ptr)
|
||||
module._free(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
18
src/main.cpp
18
src/main.cpp
|
|
@ -41,7 +41,9 @@ static std::string getResultBytesJSON(uint32_t offset, size_t resultSize, const
|
|||
|
||||
static json searchResultToJSON(const Pattern::SearchResult &result, ResultType type, const Pattern::Memory &memory) {
|
||||
json item = { { "address", result.address } };
|
||||
if (Pattern::inMemory(memory, result.address))
|
||||
if (type == RESULT_TYPE_OFFSET)
|
||||
item["offset"] = result.offset;
|
||||
else if (Pattern::inMemory(memory, result.address))
|
||||
item["offset"] = result.address - memory.base;
|
||||
if (type == RESULT_TYPE_OFFSET)
|
||||
item["bytes"] = getResultBytesJSON(result.offset, result.size, memory);
|
||||
|
|
@ -61,17 +63,19 @@ static const char *getTargetColumnName(ResultType type) {
|
|||
case RESULT_TYPE_POINTER:
|
||||
return "POINTER";
|
||||
case RESULT_TYPE_REFERENCE:
|
||||
return "REFERENCE";
|
||||
return "ADDRESS";
|
||||
case RESULT_TYPE_BRANCH:
|
||||
return "BRANCH";
|
||||
return "FUNCTION";
|
||||
default:
|
||||
return "ADDRESS";
|
||||
}
|
||||
}
|
||||
|
||||
static std::string getResultOffset(uint32_t address, const Pattern::Memory &memory) {
|
||||
return Pattern::inMemory(memory, address) ?
|
||||
std::format("{:08X}", address - memory.base) :
|
||||
static std::string getResultOffset(const Pattern::SearchResult &result, ResultType type, const Pattern::Memory &memory) {
|
||||
if (type == RESULT_TYPE_OFFSET)
|
||||
return std::format("{:08X}", result.offset);
|
||||
return Pattern::inMemory(memory, result.address) ?
|
||||
std::format("{:08X}", result.address - memory.base) :
|
||||
"-";
|
||||
}
|
||||
|
||||
|
|
@ -87,7 +91,7 @@ static void printResults(const std::vector<Pattern::SearchResult> &results, Resu
|
|||
std::cout << '\n';
|
||||
|
||||
for (const auto &result: results) {
|
||||
std::string offset = getResultOffset(result.address, memory);
|
||||
std::string offset = getResultOffset(result, type, memory);
|
||||
if (hasBytes) {
|
||||
std::cout << std::format(" {:08X} {:<8} {}\n",
|
||||
result.address, offset, getResultBytes(result.offset, result.size, memory, showBytes));
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ describe("Ptr89 WASM", () => {
|
|||
type: "address",
|
||||
results: [{
|
||||
address: 0xA000000B,
|
||||
offset: 11,
|
||||
offset: 10,
|
||||
bytes: "10B5",
|
||||
}],
|
||||
});
|
||||
|
|
@ -59,7 +59,7 @@ describe("Ptr89 WASM", () => {
|
|||
type: "address",
|
||||
results: [{
|
||||
address: 0xA0000003,
|
||||
offset: 3,
|
||||
offset: 2,
|
||||
bytes: "00BF",
|
||||
}],
|
||||
});
|
||||
|
|
@ -109,6 +109,25 @@ describe("Ptr89 WASM", () => {
|
|||
ptr89.close();
|
||||
});
|
||||
|
||||
it("reopens memory", async () => {
|
||||
const ptr89 = new Ptr89();
|
||||
await ptr89.open(Uint8Array.from([
|
||||
0x10, 0xB5, // PUSH {R4, LR}
|
||||
]));
|
||||
await ptr89.open(Uint8Array.from([
|
||||
0x00, 0xBF, // NOP
|
||||
]));
|
||||
|
||||
expect(ptr89.find("10 B5", 1).results).toEqual([]);
|
||||
expect(ptr89.find("00 BF", 1).results).toEqual([{
|
||||
address: 0xA0000000,
|
||||
offset: 0,
|
||||
bytes: "00BF",
|
||||
}]);
|
||||
|
||||
ptr89.close();
|
||||
});
|
||||
|
||||
it("opens a Blob directly", async () => {
|
||||
const memory = Uint8Array.from([
|
||||
0x00, 0xBF, // NOP
|
||||
|
|
@ -127,7 +146,7 @@ describe("Ptr89 WASM", () => {
|
|||
type: "address",
|
||||
results: [{
|
||||
address: 0xA0000003,
|
||||
offset: 3,
|
||||
offset: 2,
|
||||
bytes: "10B5",
|
||||
}],
|
||||
});
|
||||
|
|
|
|||
|
|
@ -35,18 +35,16 @@ void Ptr89Wasm::open(uintptr_t ptr, size_t size, uint32_t base, const std::strin
|
|||
throw std::invalid_argument("Invalid architecture '" + archName + "'. Expected arm or c166.");
|
||||
}
|
||||
|
||||
const auto *data = reinterpret_cast<const uint8_t *>(ptr);
|
||||
m_data.clear();
|
||||
if (size != 0)
|
||||
m_data.assign(data, data + size);
|
||||
m_data = reinterpret_cast<const uint8_t *>(ptr);
|
||||
m_size = size;
|
||||
m_base = base;
|
||||
m_arch = arch;
|
||||
m_open = true;
|
||||
}
|
||||
|
||||
void Ptr89Wasm::close() {
|
||||
m_data.clear();
|
||||
m_data.shrink_to_fit();
|
||||
m_data = nullptr;
|
||||
m_size = 0;
|
||||
m_open = false;
|
||||
}
|
||||
|
||||
|
|
@ -63,7 +61,9 @@ Ptr89Search Ptr89Wasm::find(const std::string &patternText, size_t limit, int al
|
|||
search.results.reserve(matches.size());
|
||||
for (const auto &match: matches) {
|
||||
std::optional<uint32_t> offset;
|
||||
if (Pattern::inMemory(memory, match.address))
|
||||
if (pattern->type == RESULT_TYPE_OFFSET)
|
||||
offset = match.offset;
|
||||
else if (Pattern::inMemory(memory, match.address))
|
||||
offset = match.address - memory.base;
|
||||
std::string bytes = pattern->type == RESULT_TYPE_OFFSET ?
|
||||
getBytes(match.offset, match.size, memory) :
|
||||
|
|
@ -89,7 +89,7 @@ Pattern::Memory Ptr89Wasm::getMemory(int align) const {
|
|||
throw std::runtime_error("Ptr89 is not opened.");
|
||||
if (align <= 0)
|
||||
throw std::invalid_argument("Search alignment must be greater than zero.");
|
||||
return { m_base, m_data.data(), m_data.size(), align, m_arch };
|
||||
return { m_base, m_data, m_size, align, m_arch };
|
||||
}
|
||||
|
||||
std::string prettify(const std::string &pattern) {
|
||||
|
|
|
|||
|
|
@ -30,7 +30,8 @@ struct Ptr89XRef {
|
|||
|
||||
class Ptr89Wasm {
|
||||
private:
|
||||
std::vector<uint8_t> m_data;
|
||||
const uint8_t *m_data = nullptr;
|
||||
size_t m_size = 0;
|
||||
uint32_t m_base = 0;
|
||||
Architecture m_arch = ARCH_ARM;
|
||||
bool m_open = false;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue