mirror of
https://github.com/siemens-mobile-hacks/ptr89.git
synced 2026-11-25 18:27:22 +00:00
This commit is contained in:
parent
3359738eaa
commit
3c76c02dd5
15 changed files with 463 additions and 185 deletions
14
.github/workflows/ci.yml
vendored
14
.github/workflows/ci.yml
vendored
|
|
@ -3,6 +3,11 @@ on:
|
|||
release:
|
||||
types: [published]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
release_tag:
|
||||
description: Release tag
|
||||
required: false
|
||||
type: string
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
|
@ -39,14 +44,11 @@ jobs:
|
|||
- run: cmake --build build-wasm
|
||||
- run: pnpm run build
|
||||
- run: pnpm test
|
||||
- if: github.event_name == 'release'
|
||||
run: pnpm run prepack
|
||||
- if: github.event_name == 'release'
|
||||
run: npm publish ./build-npm --access public
|
||||
|
||||
- uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
token: "${{ secrets.GITHUB_TOKEN }}"
|
||||
tag_name: ${{ inputs.release_tag || github.ref_name }}
|
||||
files: |
|
||||
../*.deb
|
||||
build/*.tar.gz
|
||||
|
|
@ -60,8 +62,10 @@ jobs:
|
|||
token: ${{ secrets.TAP_GITHUB_TOKEN }}
|
||||
|
||||
- name: Generate formula
|
||||
env:
|
||||
RELEASE_TAG: ${{ inputs.release_tag || github.ref_name }}
|
||||
run: |
|
||||
sed -i "s/RELEASE_TAG_NAME/$GITHUB_REF_NAME/g" ptr89.rb
|
||||
sed -i "s/RELEASE_TAG_NAME/$RELEASE_TAG/g" ptr89.rb
|
||||
sed -i "s/RELEASE_TAG_HASH/$GITHUB_SHA/g" ptr89.rb
|
||||
cat ptr89.rb
|
||||
|
||||
|
|
|
|||
126
.github/workflows/publish.yml
vendored
Normal file
126
.github/workflows/publish.yml
vendored
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
name: Publish
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
paths:
|
||||
- package.json
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: npm-publish
|
||||
|
||||
permissions:
|
||||
actions: write
|
||||
contents: write
|
||||
id-token: write
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
with:
|
||||
fetch-depth: 0
|
||||
submodules: true
|
||||
|
||||
- name: Check release state
|
||||
id: state
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
run: |
|
||||
NAME=$(node -p "require('./package.json').name")
|
||||
VERSION=$(node -p "require('./package.json').version")
|
||||
TAG="v$VERSION"
|
||||
|
||||
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
|
||||
|
||||
if npm view "${NAME}@${VERSION}" version > /dev/null 2>&1; then
|
||||
echo "needs_publish=false" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
echo "needs_publish=true" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
|
||||
if gh release view "$TAG" > /dev/null 2>&1; then
|
||||
echo "needs_release=false" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
echo "needs_release=true" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
|
||||
- uses: pnpm/action-setup@v6
|
||||
if: steps.state.outputs.needs_publish == 'true'
|
||||
|
||||
- uses: actions/setup-node@v7
|
||||
if: steps.state.outputs.needs_publish == 'true'
|
||||
with:
|
||||
node-version: 24
|
||||
registry-url: https://registry.npmjs.org
|
||||
package-manager-cache: false
|
||||
|
||||
- uses: mymindstorm/setup-emsdk@v14
|
||||
if: steps.state.outputs.needs_publish == 'true'
|
||||
|
||||
- name: Install dependencies
|
||||
if: steps.state.outputs.needs_publish == 'true'
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Build WebAssembly
|
||||
if: steps.state.outputs.needs_publish == 'true'
|
||||
run: |
|
||||
emcmake cmake -B build-wasm -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS:BOOL=TRUE
|
||||
cmake --build build-wasm -- -j"$(nproc)"
|
||||
|
||||
- name: Build
|
||||
if: steps.state.outputs.needs_publish == 'true'
|
||||
run: pnpm build
|
||||
|
||||
- name: Test
|
||||
if: steps.state.outputs.needs_publish == 'true'
|
||||
run: ctest --test-dir build-wasm --output-on-failure
|
||||
|
||||
- name: Prepare package
|
||||
if: steps.state.outputs.needs_publish == 'true'
|
||||
run: pnpm run prepack
|
||||
|
||||
- name: Publish to npm
|
||||
if: steps.state.outputs.needs_publish == 'true'
|
||||
run: pnpm publish ./build-npm --access public --provenance --no-git-checks
|
||||
|
||||
- name: Generate release notes
|
||||
if: steps.state.outputs.needs_release == 'true'
|
||||
env:
|
||||
RELEASE_TAG: ${{ steps.state.outputs.tag }}
|
||||
run: |
|
||||
END_REF=$GITHUB_SHA
|
||||
if git rev-parse "$RELEASE_TAG" > /dev/null 2>&1; then
|
||||
END_REF=$RELEASE_TAG
|
||||
fi
|
||||
|
||||
PREVIOUS_TAG=$(git describe --tags --abbrev=0 "${END_REF}^" 2> /dev/null || true)
|
||||
|
||||
{
|
||||
echo "## Changes"
|
||||
echo
|
||||
if [ -n "$PREVIOUS_TAG" ]; then
|
||||
git log --no-merges --pretty='- %s (%h)' "${PREVIOUS_TAG}..${END_REF}"
|
||||
echo
|
||||
echo "**Full Changelog**: https://github.com/${GITHUB_REPOSITORY}/compare/${PREVIOUS_TAG}...${RELEASE_TAG}"
|
||||
else
|
||||
git log --no-merges --pretty='- %s (%h)' "$END_REF"
|
||||
fi
|
||||
} > "$RUNNER_TEMP/release-notes.md"
|
||||
|
||||
- name: Create GitHub release
|
||||
if: steps.state.outputs.needs_release == 'true'
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
RELEASE_TAG: ${{ steps.state.outputs.tag }}
|
||||
run: gh release create "$RELEASE_TAG" --target "$GITHUB_SHA" --notes-file "$RUNNER_TEMP/release-notes.md" --title "$RELEASE_TAG"
|
||||
|
||||
- name: Trigger artifact build
|
||||
if: steps.state.outputs.needs_release == 'true'
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
RELEASE_TAG: ${{ steps.state.outputs.tag }}
|
||||
run: gh workflow run ci.yml --ref "$RELEASE_TAG" -f release_tag="$RELEASE_TAG"
|
||||
6
.gitmodules
vendored
6
.gitmodules
vendored
|
|
@ -7,3 +7,9 @@
|
|||
[submodule "third_party/spdlog"]
|
||||
path = third_party/spdlog
|
||||
url = https://github.com/gabime/spdlog.git
|
||||
[submodule "third_party/tomlplusplus"]
|
||||
path = third_party/tomlplusplus
|
||||
url = https://github.com/marzer/tomlplusplus.git
|
||||
[submodule "third_party/Catch2"]
|
||||
path = third_party/Catch2
|
||||
url = https://github.com/catchorg/Catch2.git
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.10)
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(ptr89)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
|
@ -65,9 +65,11 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
|
|||
set_tests_properties(wasm-test PROPERTIES WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
endif()
|
||||
else()
|
||||
add_executable(ptr89 src/main.cpp)
|
||||
target_link_libraries(ptr89 PRIVATE ptr89-lib)
|
||||
target_precompile_headers(ptr89 PRIVATE <argparse/argparse.hpp> <nlohmann/json.hpp> <string> <vector> <memory> <regex> <map> <tuple> <stdexcept>)
|
||||
add_subdirectory(third_party/tomlplusplus EXCLUDE_FROM_ALL)
|
||||
|
||||
add_executable(ptr89 src/main.cpp src/PatternsLibrary.cpp)
|
||||
target_link_libraries(ptr89 PRIVATE ptr89-lib tomlplusplus::tomlplusplus)
|
||||
target_precompile_headers(ptr89 PRIVATE <argparse/argparse.hpp> <nlohmann/json.hpp> <string> <vector> <memory> <map> <tuple> <stdexcept>)
|
||||
install(TARGETS ptr89)
|
||||
|
||||
if (MSVC)
|
||||
|
|
@ -87,8 +89,17 @@ else()
|
|||
|
||||
if (BUILD_TESTS)
|
||||
enable_testing()
|
||||
add_executable(ptr89-tests src/tests.cpp)
|
||||
target_link_libraries(ptr89-tests PRIVATE ptr89-lib)
|
||||
add_subdirectory(third_party/Catch2 EXCLUDE_FROM_ALL)
|
||||
add_executable(ptr89-tests src/tests.cpp src/PatternsLibrary.cpp)
|
||||
target_compile_definitions(ptr89-tests PRIVATE TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test")
|
||||
target_link_libraries(ptr89-tests PRIVATE ptr89-lib tomlplusplus::tomlplusplus Catch2::Catch2WithMain)
|
||||
add_test(NAME test COMMAND ptr89-tests)
|
||||
add_test(NAME toml-cli COMMAND ptr89
|
||||
-f ${CMAKE_CURRENT_SOURCE_DIR}/test/patterns.toml
|
||||
--swilib-patterns ${CMAKE_CURRENT_SOURCE_DIR}/test/patterns.toml
|
||||
--swilib-platform ELKA
|
||||
--json
|
||||
)
|
||||
set_tests_properties(toml-cli PROPERTIES PASS_REGULAR_EXPRESSION "\"pattern\": \"6C 61\"")
|
||||
endif()
|
||||
endif()
|
||||
|
|
|
|||
16
README.md
16
README.md
|
|
@ -62,8 +62,9 @@ Find xrefs:
|
|||
-x, --xref HEX address to search
|
||||
-n, --limit NUMBER limit results count [default 100]
|
||||
|
||||
Find patterns from functions.ini:
|
||||
--from-ini FILE path to functions.ini
|
||||
Find patterns from patterns.toml:
|
||||
--swilib-patterns FILE path to patterns.toml
|
||||
--swilib-platform NAME target platform
|
||||
|
||||
Prettify pattern:
|
||||
--prettify STRING pattern
|
||||
|
|
@ -167,12 +168,13 @@ x-ref results intentionally use different structures:
|
|||
}
|
||||
```
|
||||
|
||||
For a fixed address, `offset` and `bytes` are omitted. `--from-ini` returns a
|
||||
`functions` array with one `result` object or `null` for each entry.
|
||||
For a fixed address, `offset` and `bytes` are omitted. `--swilib-patterns`
|
||||
returns a `functions` array with one `result` object or `null` for each entry.
|
||||
The TOML format selects platform-specific patterns, including groups and
|
||||
fallback arrays defined by its `[platforms]` table:
|
||||
|
||||
### Convert patterns.ini to swilib.vkp
|
||||
```
|
||||
ptr89 -f EL71v45.bin --from-ini ELKA.ini > swilib.vkp
|
||||
ptr89 -f EL71v45.bin --swilib-patterns patterns.toml --swilib-platform ELKA > swilib.vkp
|
||||
```
|
||||
|
||||
# Pattern syntax
|
||||
|
|
@ -493,7 +495,7 @@ Steps:
|
|||
2. Result is: `0xA058BB98 | 1 = 0xA058BB99`
|
||||
|
||||
## Fixed address
|
||||
Usually used in `patterns.ini` for entries whose address is already known.
|
||||
Usually used in `patterns.toml` for entries whose address is already known.
|
||||
```bash
|
||||
# Address is 0xA8000000
|
||||
< A8000000 >
|
||||
|
|
|
|||
2
debian/changelog
vendored
2
debian/changelog
vendored
|
|
@ -1,4 +1,4 @@
|
|||
ptr89 (3.0.3) UNRELEASED; urgency=medium
|
||||
ptr89 (3.1.0) UNRELEASED; urgency=medium
|
||||
|
||||
* first release
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@sie-js/ptr89",
|
||||
"version": "3.0.3",
|
||||
"version": "3.1.0",
|
||||
"description": "ARM/THUMB/C166 binary pattern finder",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
|
|
|
|||
122
src/PatternsLibrary.cpp
Normal file
122
src/PatternsLibrary.cpp
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
#include "PatternsLibrary.h"
|
||||
#include <algorithm>
|
||||
#include <charconv>
|
||||
#include <format>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <toml++/toml.hpp>
|
||||
#include <utility>
|
||||
|
||||
struct PlatformGroup {
|
||||
std::string name;
|
||||
std::vector<std::string> platforms;
|
||||
};
|
||||
|
||||
static std::vector<PlatformGroup> parsePlatformGroups(const toml::table &root, const std::string &platform) {
|
||||
auto groupsTable = root["platforms"].as_table();
|
||||
if (!groupsTable)
|
||||
throw std::runtime_error("Missing [platforms] table.");
|
||||
|
||||
bool knownPlatform = false;
|
||||
std::vector<PlatformGroup> groups;
|
||||
for (const auto &[groupName, value]: *groupsTable) {
|
||||
auto values = value.as_array();
|
||||
if (!values)
|
||||
throw std::runtime_error(std::format("platforms.{} must be an array.", groupName.str()));
|
||||
|
||||
PlatformGroup group = { std::string(groupName.str()), { } };
|
||||
for (const auto &item: *values) {
|
||||
auto name = item.value<std::string>();
|
||||
if (!name)
|
||||
throw std::runtime_error(std::format("platforms.{} must contain only strings.", groupName.str()));
|
||||
group.platforms.push_back(*name);
|
||||
if (*name == platform)
|
||||
knownPlatform = true;
|
||||
}
|
||||
groups.push_back(std::move(group));
|
||||
}
|
||||
|
||||
if (!knownPlatform)
|
||||
throw std::runtime_error(std::format("Unknown platform '{}'.", platform));
|
||||
|
||||
std::stable_sort(groups.begin(), groups.end(), [](const PlatformGroup &a, const PlatformGroup &b) {
|
||||
return a.platforms.size() < b.platforms.size();
|
||||
});
|
||||
return groups;
|
||||
}
|
||||
|
||||
static const toml::node *findPatternNode(const toml::table &entry, const std::string &platform,
|
||||
const std::vector<PlatformGroup> &groups) {
|
||||
if (auto value = entry.get(platform))
|
||||
return value;
|
||||
|
||||
for (const auto &group: groups) {
|
||||
if (group.name == platform)
|
||||
continue;
|
||||
if (std::find(group.platforms.begin(), group.platforms.end(), platform) != group.platforms.end()) {
|
||||
if (auto value = entry.get(group.name))
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static std::vector<std::string> parsePatterns(const toml::node *node, const std::string &entryName) {
|
||||
if (!node)
|
||||
return { };
|
||||
if (auto pattern = node->value<std::string>())
|
||||
return { *pattern };
|
||||
if (auto enabled = node->value<bool>()) {
|
||||
if (!*enabled)
|
||||
return { };
|
||||
}
|
||||
if (auto values = node->as_array()) {
|
||||
std::vector<std::string> patterns;
|
||||
for (const auto &value: *values) {
|
||||
auto pattern = value.value<std::string>();
|
||||
if (!pattern)
|
||||
throw std::runtime_error(std::format("Patterns for [{}] must contain only strings.", entryName));
|
||||
patterns.push_back(*pattern);
|
||||
}
|
||||
return patterns;
|
||||
}
|
||||
throw std::runtime_error(std::format("Pattern for [{}] must be a string, an array of strings, or false.", entryName));
|
||||
}
|
||||
|
||||
static int parseId(std::string_view value) {
|
||||
int id = 0;
|
||||
auto [end, error] = std::from_chars(value.data(), value.data() + value.size(), id, 16);
|
||||
if (error != std::errc() || end != value.data() + value.size() || id < 0)
|
||||
throw std::runtime_error(std::format("Invalid function ID '{}'.", value));
|
||||
return id;
|
||||
}
|
||||
|
||||
std::vector<PatternsLibraryItem> parsePatternsToml(const std::string &tomlFile, const std::string &platform) {
|
||||
auto root = toml::parse_file(tomlFile);
|
||||
auto groups = parsePlatformGroups(root, platform);
|
||||
std::vector<PatternsLibraryItem> results;
|
||||
|
||||
for (const auto &[name, value]: root) {
|
||||
if (name == "platforms")
|
||||
continue;
|
||||
auto entry = value.as_table();
|
||||
if (!entry)
|
||||
throw std::runtime_error(std::format("{} must be a table.", name.str()));
|
||||
|
||||
auto declName = std::format("decl_{}", platform);
|
||||
auto declNode = entry->get(declName);
|
||||
if (!declNode)
|
||||
declNode = entry->get("decl");
|
||||
auto funcName = declNode ? declNode->value<std::string>() : std::optional<std::string>{ };
|
||||
if (!funcName)
|
||||
throw std::runtime_error(std::format("Declaration for [{}] must be a string.", name.str()));
|
||||
|
||||
auto patternNode = findPatternNode(*entry, platform, groups);
|
||||
results.push_back({ parseId(name.str()), *funcName, parsePatterns(patternNode, std::string(name.str())) });
|
||||
}
|
||||
|
||||
std::sort(results.begin(), results.end(), [](const PatternsLibraryItem &a, const PatternsLibraryItem &b) {
|
||||
return a.id < b.id;
|
||||
});
|
||||
return results;
|
||||
}
|
||||
12
src/PatternsLibrary.h
Normal file
12
src/PatternsLibrary.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct PatternsLibraryItem {
|
||||
int id;
|
||||
std::string funcName;
|
||||
std::vector<std::string> patterns;
|
||||
};
|
||||
|
||||
std::vector<PatternsLibraryItem> parsePatternsToml(const std::string &tomlFile, const std::string &platform);
|
||||
78
src/main.cpp
78
src/main.cpp
|
|
@ -1,4 +1,5 @@
|
|||
#include "main.h"
|
||||
#include "PatternsLibrary.h"
|
||||
#include "src/Pattern.h"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
|
@ -149,7 +150,7 @@ int main(int argc, char *argv[]) {
|
|||
spdlog::set_pattern("%v");
|
||||
spdlog::set_level(spdlog::level::warn);
|
||||
|
||||
argparse::ArgumentParser program("ptr89", "3.0.3");
|
||||
argparse::ArgumentParser program("ptr89", "3.1.0");
|
||||
|
||||
program.add_argument("-f", "--file")
|
||||
.required()
|
||||
|
|
@ -176,7 +177,10 @@ int main(int argc, char *argv[]) {
|
|||
.default_value(100)
|
||||
.nargs(1)
|
||||
.scan<'i', int>();
|
||||
program.add_argument("--from-ini")
|
||||
program.add_argument("--swilib-patterns")
|
||||
.default_value("")
|
||||
.nargs(1);
|
||||
program.add_argument("--swilib-platform")
|
||||
.default_value("")
|
||||
.nargs(1);
|
||||
program.add_argument("--prettify")
|
||||
|
|
@ -220,8 +224,9 @@ int main(int argc, char *argv[]) {
|
|||
std::cerr << " -x, --xref HEX address to search\n";
|
||||
std::cerr << " -n, --limit NUMBER limit results count [default 100]\n";
|
||||
std::cerr << "\n";
|
||||
std::cerr << "Find patterns from functions.ini:\n";
|
||||
std::cerr << " --from-ini FILE path to functions.ini\n";
|
||||
std::cerr << "Find patterns from patterns.toml:\n";
|
||||
std::cerr << " --swilib-patterns FILE path to patterns.toml\n";
|
||||
std::cerr << " --swilib-platform NAME target platform\n";
|
||||
std::cerr << "\n";
|
||||
std::cerr << "Prettify pattern:\n";
|
||||
std::cerr << " --prettify STRING pattern\n";
|
||||
|
|
@ -323,20 +328,31 @@ int main(int argc, char *argv[]) {
|
|||
if (!asJSON) {
|
||||
std::cout << std::format("Search done in {} ms\n", end - start);
|
||||
}
|
||||
} else if (program.is_used("--from-ini")) {
|
||||
auto patternsLib = parsePatternsIni(program.get<std::string>("--from-ini"));
|
||||
} else if (program.is_used("--swilib-patterns")) {
|
||||
auto platform = program.get<std::string>("--swilib-platform");
|
||||
if (platform.empty())
|
||||
throw std::runtime_error("--swilib-platform is required with --swilib-patterns.");
|
||||
auto patternsLib = parsePatternsToml(program.get<std::string>("--swilib-patterns"), platform);
|
||||
|
||||
j["functions"] = json::array();
|
||||
|
||||
for (auto &entry: patternsLib) {
|
||||
auto pattern = Pattern::parse(entry.pattern);
|
||||
auto results = Pattern::find(pattern, memoryRegion, 1);
|
||||
std::string patternStr;
|
||||
auto pattern = Pattern::parse(patternStr);
|
||||
std::vector<Pattern::SearchResult> results;
|
||||
for (const auto &candidate: entry.patterns) {
|
||||
patternStr = candidate;
|
||||
pattern = Pattern::parse(patternStr);
|
||||
results = Pattern::find(pattern, memoryRegion, 1);
|
||||
if (!results.empty())
|
||||
break;
|
||||
}
|
||||
|
||||
if (asJSON) {
|
||||
json functionJson = {
|
||||
{ "id", entry.id },
|
||||
{ "name", entry.funcName },
|
||||
{ "pattern", entry.pattern },
|
||||
{ "pattern", patternStr },
|
||||
{ "type", Pattern::getSearchTypeName(pattern->type) },
|
||||
};
|
||||
functionJson["result"] = results.empty() || results[0].address == 0xFFFFFFFF ?
|
||||
|
|
@ -381,40 +397,6 @@ int main(int argc, char *argv[]) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
std::vector<PatternsLibraryItem> parsePatternsIni(const std::string &iniFile) {
|
||||
auto iniText = readFile(iniFile);
|
||||
std::regex exp(R"(^[ \t]*([0-9a-f]+):[ \t]*([^=;\n]+)(?:[ \t]*=[ \t]*([^;:\n]*))?)", std::regex::icase | std::regex::multiline);
|
||||
std::smatch m;
|
||||
std::vector<PatternsLibraryItem> results;
|
||||
auto searchStart = iniText.cbegin();
|
||||
while (std::regex_search(searchStart, iniText.cend(), m, exp)) {
|
||||
auto id = stoi(m[1].str(), NULL, 16);
|
||||
auto funcName = trim(m[2].str());
|
||||
auto patternStr = trim(m[3].str());
|
||||
results.push_back({ id, funcName, patternStr });
|
||||
searchStart = m.suffix().first;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
std::string readFile(const std::string &path) {
|
||||
FILE *fp = fopen(path.c_str(), "r");
|
||||
if (!fp) {
|
||||
throw std::runtime_error("fopen(" + path + ") error: " + strerror(errno));
|
||||
}
|
||||
|
||||
char buff[4096];
|
||||
std::string result;
|
||||
while (!feof(fp)) {
|
||||
int readed = fread(buff, 1, sizeof(buff), fp);
|
||||
if (readed > 0)
|
||||
result.append(buff, readed);
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::pair<uint8_t *, size_t> readBinaryFile(const std::string &path) {
|
||||
FILE *fp = fopen(path.c_str(), "r");
|
||||
if (!fp) {
|
||||
|
|
@ -437,13 +419,3 @@ std::pair<uint8_t *, size_t> readBinaryFile(const std::string &path) {
|
|||
|
||||
return { bytes, readed };
|
||||
}
|
||||
|
||||
std::string trim(std::string s) {
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](uint8_t c) {
|
||||
return !isspace(c);
|
||||
}));
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(), [](uint8_t c) {
|
||||
return !isspace(c);
|
||||
}).base(), s.end());
|
||||
return s;
|
||||
}
|
||||
|
|
|
|||
11
src/main.h
11
src/main.h
|
|
@ -3,20 +3,9 @@
|
|||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
#include <regex>
|
||||
#include <filesystem>
|
||||
#include <ptr89.h>
|
||||
#include <argparse/argparse.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
struct PatternsLibraryItem {
|
||||
int id;
|
||||
std::string funcName;
|
||||
std::string pattern;
|
||||
};
|
||||
|
||||
std::string readFile(const std::string &path);
|
||||
std::pair<uint8_t *, size_t> readBinaryFile(const std::string &path);
|
||||
std::vector<PatternsLibraryItem> parsePatternsIni(const std::string &iniFile);
|
||||
std::string trim(std::string s);
|
||||
|
|
|
|||
210
src/tests.cpp
210
src/tests.cpp
|
|
@ -1,8 +1,9 @@
|
|||
#include <cstdint>
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/matchers/catch_matchers.hpp>
|
||||
#include <ptr89.h>
|
||||
#include <src/Arch.h>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include "PatternsLibrary.h"
|
||||
|
||||
using namespace Ptr89;
|
||||
|
||||
|
|
@ -12,52 +13,52 @@ static inline uint8_t *I(const std::vector<uint8_t> &value) {
|
|||
return &tmp[0];
|
||||
}
|
||||
|
||||
static void testArmDecoder() {
|
||||
TEST_CASE("ARM decoder", "[decoder][arm]") {
|
||||
const Arch &arch = getArch(ARCH_ARM);
|
||||
std::vector<uint8_t> data(0x2000);
|
||||
Pattern::Memory memory = { 0xA0000000, data.data(), data.size(), 1, ARCH_ARM };
|
||||
|
||||
// FE FB FF 0B: BLEQ 0xA0000000
|
||||
assert(arch.decodeBranch4(0xA0001000, I({ 0xFE, 0xFB, 0xFF, 0x0B }), memory) == std::make_pair(true, 0xA0000000));
|
||||
REQUIRE((arch.decodeBranch4(0xA0001000, I({ 0xFE, 0xFB, 0xFF, 0x0B }), memory) == std::make_pair(true, 0xA0000000)));
|
||||
|
||||
// FE FB FF FA: BLX 0xA0000000
|
||||
assert(arch.decodeBranch4(0xA0001000, I({ 0xFE, 0xFB, 0xFF, 0xFA }), memory) == std::make_pair(true, 0xA0000000));
|
||||
REQUIRE((arch.decodeBranch4(0xA0001000, I({ 0xFE, 0xFB, 0xFF, 0xFA }), memory) == std::make_pair(true, 0xA0000000)));
|
||||
|
||||
// FE 03 00 0B: BLEQ 0xA0001000
|
||||
assert(arch.decodeBranch4(0xA0000000, I({ 0xFE, 0x03, 0x00, 0x0B }), memory) == std::make_pair(true, 0xA0001000));
|
||||
REQUIRE((arch.decodeBranch4(0xA0000000, I({ 0xFE, 0x03, 0x00, 0x0B }), memory) == std::make_pair(true, 0xA0001000)));
|
||||
|
||||
// FE 03 00 FA: BLX 0xA0001000
|
||||
assert(arch.decodeBranch4(0xA0000000, I({ 0xFE, 0x03, 0x00, 0xFA }), memory) == std::make_pair(true, 0xA0001000));
|
||||
REQUIRE((arch.decodeBranch4(0xA0000000, I({ 0xFE, 0x03, 0x00, 0xFA }), memory) == std::make_pair(true, 0xA0001000)));
|
||||
|
||||
// FE FB FF FB: BLX 0xA0000002
|
||||
assert(arch.decodeBranch4(0xA0001000, I({ 0xFE, 0xFB, 0xFF, 0xFB }), memory) == std::make_pair(true, 0xA0000002));
|
||||
REQUIRE((arch.decodeBranch4(0xA0001000, I({ 0xFE, 0xFB, 0xFF, 0xFB }), memory) == std::make_pair(true, 0xA0000002)));
|
||||
|
||||
// FE 03 00 FB: BLX 0xA0001002
|
||||
assert(arch.decodeBranch4(0xA0000000, I({ 0xFE, 0x03, 0x00, 0xFB }), memory) == std::make_pair(true, 0xA0001002));
|
||||
REQUIRE((arch.decodeBranch4(0xA0000000, I({ 0xFE, 0x03, 0x00, 0xFB }), memory) == std::make_pair(true, 0xA0001002)));
|
||||
|
||||
// FE F7 FE FF: BL 0xA0000000
|
||||
assert(arch.decodeBranch4(0xA0001000, I({ 0xFE, 0xF7, 0xFE, 0xFF }), memory) == std::make_pair(true, 0xA0000000));
|
||||
REQUIRE((arch.decodeBranch4(0xA0001000, I({ 0xFE, 0xF7, 0xFE, 0xFF }), memory) == std::make_pair(true, 0xA0000000)));
|
||||
|
||||
// FE F7 FE EF: BLX 0xA0000000
|
||||
assert(arch.decodeBranch4(0xA0001000, I({ 0xFE, 0xF7, 0xFE, 0xEF }), memory) == std::make_pair(true, 0xA0000000));
|
||||
REQUIRE((arch.decodeBranch4(0xA0001000, I({ 0xFE, 0xF7, 0xFE, 0xEF }), memory) == std::make_pair(true, 0xA0000000)));
|
||||
|
||||
// 00 F0 FE FF: BL 0xA0001000
|
||||
assert(arch.decodeBranch4(0xA0000000, I({ 0x00, 0xF0, 0xFE, 0xFF }), memory) == std::make_pair(true, 0xA0001000));
|
||||
REQUIRE((arch.decodeBranch4(0xA0000000, I({ 0x00, 0xF0, 0xFE, 0xFF }), memory) == std::make_pair(true, 0xA0001000)));
|
||||
|
||||
// 00 F0 FE EF: BLX 0xA0001000
|
||||
assert(arch.decodeBranch4(0xA0000000, I({ 0x00, 0xF0, 0xFE, 0xEF }), memory) == std::make_pair(true, 0xA0001000));
|
||||
REQUIRE((arch.decodeBranch4(0xA0000000, I({ 0x00, 0xF0, 0xFE, 0xEF }), memory) == std::make_pair(true, 0xA0001000)));
|
||||
|
||||
// 7E E7: B 0xA0000000
|
||||
assert(arch.decodeBranch2(0xA0000100, I({ 0x7E, 0xE7 }), memory) == std::make_pair(true, 0xA0000000));
|
||||
REQUIRE((arch.decodeBranch2(0xA0000100, I({ 0x7E, 0xE7 }), memory) == std::make_pair(true, 0xA0000000)));
|
||||
|
||||
// 7E E0: B 0xA0000100
|
||||
assert(arch.decodeBranch2(0xA0000000, I({ 0x7E, 0xE0 }), memory) == std::make_pair(true, 0xA0000100));
|
||||
REQUIRE((arch.decodeBranch2(0xA0000000, I({ 0x7E, 0xE0 }), memory) == std::make_pair(true, 0xA0000100)));
|
||||
|
||||
// F6 D0: BEQ 0xA0000000
|
||||
assert(arch.decodeBranch2(0xA0000010, I({ 0xF6, 0xD0 }), memory) == std::make_pair(true, 0xA0000000));
|
||||
REQUIRE((arch.decodeBranch2(0xA0000010, I({ 0xF6, 0xD0 }), memory) == std::make_pair(true, 0xA0000000)));
|
||||
|
||||
// 06 D0: BEQ 0xA0000010
|
||||
assert(arch.decodeBranch2(0xA0000000, I({ 0x06, 0xD0 }), memory) == std::make_pair(true, 0xA0000010));
|
||||
REQUIRE((arch.decodeBranch2(0xA0000000, I({ 0x06, 0xD0 }), memory) == std::make_pair(true, 0xA0000010)));
|
||||
|
||||
const uint32_t pointer = 0xA0001800;
|
||||
auto writePointer = [&data, pointer](size_t offset) {
|
||||
|
|
@ -71,31 +72,31 @@ static void testArmDecoder() {
|
|||
writePointer(0x108);
|
||||
|
||||
// 16 48: LDR R0, [PC, #0x58] -> 0xA000005C
|
||||
assert(arch.decodeReferences2(0xA0000000, I({ 0x16, 0x48 }), memory) == std::vector<uint32_t>{ pointer });
|
||||
REQUIRE((arch.decodeReferences2(0xA0000000, I({ 0x16, 0x48 }), memory) == std::vector<uint32_t>{ pointer }));
|
||||
|
||||
// 16 48: LDR R0, [PC, #0x58] -> 0xA000005C
|
||||
assert(arch.decodeReferences2(0xA0000002, I({ 0x16, 0x48 }), memory) == std::vector<uint32_t>{ pointer });
|
||||
REQUIRE((arch.decodeReferences2(0xA0000002, I({ 0x16, 0x48 }), memory) == std::vector<uint32_t>{ pointer }));
|
||||
|
||||
// 00 01 9F E5: LDR R0, [PC, #0x100] -> 0xA0000108
|
||||
assert(arch.decodeReferences4(0xA0000000, I({ 0x00, 0x01, 0x9F, 0xE5 }), memory) == std::vector<uint32_t>{ pointer });
|
||||
REQUIRE((arch.decodeReferences4(0xA0000000, I({ 0x00, 0x01, 0x9F, 0xE5 }), memory) == std::vector<uint32_t>{ pointer }));
|
||||
|
||||
// 00 01 1F E5: LDR R0, [PC, #-0x100] -> 0xA0000008
|
||||
assert(arch.decodeReferences4(0xA0000100, I({ 0x00, 0x01, 0x1F, 0xE5 }), memory) == std::vector<uint32_t>{ pointer });
|
||||
REQUIRE((arch.decodeReferences4(0xA0000100, I({ 0x00, 0x01, 0x1F, 0xE5 }), memory) == std::vector<uint32_t>{ pointer }));
|
||||
// 00 00 8F E5: STR R0, [PC], not a reference load.
|
||||
assert(arch.decodeReferences4(0xA0000000, I({ 0x00, 0x00, 0x8F, 0xE5 }), memory).empty());
|
||||
REQUIRE((arch.decodeReferences4(0xA0000000, I({ 0x00, 0x00, 0x8F, 0xE5 }), memory).empty()));
|
||||
|
||||
// 00 F1 9F E5: LDR PC, [PC, #0x100] -> 0xA0000108
|
||||
assert(arch.decodeBranch4(0xA0000000, I({ 0x00, 0xF1, 0x9F, 0xE5 }), memory) == std::make_pair(true, pointer));
|
||||
REQUIRE((arch.decodeBranch4(0xA0000000, I({ 0x00, 0xF1, 0x9F, 0xE5 }), memory) == std::make_pair(true, pointer)));
|
||||
|
||||
// 00 F1 1F E5: LDR PC, [PC, #-0x100] -> 0xA0000008
|
||||
assert(arch.decodeBranch4(0xA0000100, I({ 0x00, 0xF1, 0x1F, 0xE5 }), memory) == std::make_pair(true, pointer));
|
||||
REQUIRE((arch.decodeBranch4(0xA0000100, I({ 0x00, 0xF1, 0x1F, 0xE5 }), memory) == std::make_pair(true, pointer)));
|
||||
|
||||
// 00 F0 FE FF: BL 0xA0001000; THUMB function pointer is 0xA0001001.
|
||||
data[0] = 0x00; data[1] = 0xF0; data[2] = 0xFE; data[3] = 0xFF;
|
||||
assert(arch.decodeBranchReference(0, memory) == std::make_tuple(true, 0xA0001001U, 4U));
|
||||
REQUIRE((arch.decodeBranchReference(0, memory) == std::make_tuple(true, 0xA0001001U, 4U)));
|
||||
// 00 F0 FE EF: BLX 0xA0001000; ARM function pointer is 0xA0001000.
|
||||
data[0] = 0x00; data[1] = 0xF0; data[2] = 0xFE; data[3] = 0xEF;
|
||||
assert(arch.decodeBranchReference(0, memory) == std::make_tuple(true, 0xA0001000U, 4U));
|
||||
REQUIRE((arch.decodeBranchReference(0, memory) == std::make_tuple(true, 0xA0001000U, 4U)));
|
||||
|
||||
std::vector<uint8_t> xrefBytes(0x50);
|
||||
uint32_t xrefTarget = 0xA0000040;
|
||||
|
|
@ -117,69 +118,69 @@ static void testArmDecoder() {
|
|||
}
|
||||
return false;
|
||||
};
|
||||
assert(hasXRef(RESULT_TYPE_BRANCH, 0xA0000000));
|
||||
assert(hasXRef(RESULT_TYPE_POINTER, 0xA0000004));
|
||||
assert(hasXRef(RESULT_TYPE_REFERENCE, 0xA0000008));
|
||||
assert(hasXRef(RESULT_TYPE_REFERENCE, 0xA000000C));
|
||||
assert(hasXRef(RESULT_TYPE_REFERENCE, 0xA0000012));
|
||||
assert(hasXRef(RESULT_TYPE_REFERENCE, 0xA0000014));
|
||||
assert(hasXRef(RESULT_TYPE_POINTER, 0xA0000020));
|
||||
assert(hasXRef(RESULT_TYPE_POINTER, 0xA0000024));
|
||||
REQUIRE((hasXRef(RESULT_TYPE_BRANCH, 0xA0000000)));
|
||||
REQUIRE((hasXRef(RESULT_TYPE_POINTER, 0xA0000004)));
|
||||
REQUIRE((hasXRef(RESULT_TYPE_REFERENCE, 0xA0000008)));
|
||||
REQUIRE((hasXRef(RESULT_TYPE_REFERENCE, 0xA000000C)));
|
||||
REQUIRE((hasXRef(RESULT_TYPE_REFERENCE, 0xA0000012)));
|
||||
REQUIRE((hasXRef(RESULT_TYPE_REFERENCE, 0xA0000014)));
|
||||
REQUIRE((hasXRef(RESULT_TYPE_POINTER, 0xA0000020)));
|
||||
REQUIRE((hasXRef(RESULT_TYPE_POINTER, 0xA0000024)));
|
||||
auto thumbXRefs = Pattern::finXRefs(xrefTarget | 1U, xrefMemory);
|
||||
assert(!hasXRef(RESULT_TYPE_BRANCH, 0xA0000010));
|
||||
REQUIRE((!hasXRef(RESULT_TYPE_BRANCH, 0xA0000010)));
|
||||
bool hasThumbBranch = false;
|
||||
for (const auto &xref: thumbXRefs) {
|
||||
if (xref.type == RESULT_TYPE_BRANCH && xref.address == 0xA0000010)
|
||||
hasThumbBranch = true;
|
||||
}
|
||||
assert(hasThumbBranch);
|
||||
REQUIRE((hasThumbBranch));
|
||||
}
|
||||
|
||||
static void testC166Decoder() {
|
||||
TEST_CASE("C166 decoder", "[decoder][c166]") {
|
||||
const Arch &arch = getArch(ARCH_C166);
|
||||
Pattern::Memory memory = { 0, nullptr, 0, 1, ARCH_C166 };
|
||||
|
||||
// BB FF: CALLR 0x250100
|
||||
assert(arch.decodeBranch2(0x250100, I({ 0xBB, 0xFF }), memory) == std::make_pair(true, 0x250100));
|
||||
REQUIRE((arch.decodeBranch2(0x250100, I({ 0xBB, 0xFF }), memory) == std::make_pair(true, 0x250100)));
|
||||
// 3D 03: JMPR cc_NE, 0x9B5942
|
||||
assert(arch.decodeBranch2(0x9B593A, I({ 0x3D, 0x03 }), memory) == std::make_pair(true, 0x9B5942));
|
||||
REQUIRE((arch.decodeBranch2(0x9B593A, I({ 0x3D, 0x03 }), memory) == std::make_pair(true, 0x9B5942)));
|
||||
// 0D 10: JMPR cc_UC, 0x9B599A
|
||||
assert(arch.decodeBranch2(0x9B5978, I({ 0x0D, 0x10 }), memory) == std::make_pair(true, 0x9B599A));
|
||||
REQUIRE((arch.decodeBranch2(0x9B5978, I({ 0x0D, 0x10 }), memory) == std::make_pair(true, 0x9B599A)));
|
||||
// 0D 00: JMPR cc_UC, 0x120000 (16-bit offset wrap)
|
||||
assert(arch.decodeBranch2(0x12FFFE, I({ 0x0D, 0x00 }), memory) == std::make_pair(true, 0x120000));
|
||||
REQUIRE((arch.decodeBranch2(0x12FFFE, I({ 0x0D, 0x00 }), memory) == std::make_pair(true, 0x120000)));
|
||||
// CC 00: NOP, not a branch
|
||||
assert(arch.decodeBranch2(0x250100, I({ 0xCC, 0x00 }), memory) == std::make_pair(false, 0U));
|
||||
REQUIRE((arch.decodeBranch2(0x250100, I({ 0xCC, 0x00 }), memory) == std::make_pair(false, 0U)));
|
||||
|
||||
// CA 00 56 34: CALLA cc_UC, 0x3456 -> 0x9A3456
|
||||
assert(arch.decodeBranch4(0x9A0100, I({ 0xCA, 0x00, 0x56, 0x34 }), memory) == std::make_pair(true, 0x9A3456));
|
||||
REQUIRE((arch.decodeBranch4(0x9A0100, I({ 0xCA, 0x00, 0x56, 0x34 }), memory) == std::make_pair(true, 0x9A3456)));
|
||||
// EA 30 56 34: JMPA cc_NE, 0x3456 -> 0x9A3456
|
||||
assert(arch.decodeBranch4(0x9A0100, I({ 0xEA, 0x30, 0x56, 0x34 }), memory) == std::make_pair(true, 0x9A3456));
|
||||
REQUIRE((arch.decodeBranch4(0x9A0100, I({ 0xEA, 0x30, 0x56, 0x34 }), memory) == std::make_pair(true, 0x9A3456)));
|
||||
// DA 9A 32 93: CALLS 0x9A, 0x9332 -> 0x9A9332
|
||||
assert(arch.decodeBranch4(0x259254, I({ 0xDA, 0x9A, 0x32, 0x93 }), memory) == std::make_pair(true, 0x9A9332));
|
||||
REQUIRE((arch.decodeBranch4(0x259254, I({ 0xDA, 0x9A, 0x32, 0x93 }), memory) == std::make_pair(true, 0x9A9332)));
|
||||
// DA A2 54 61: CALLS 0xA2, 0x6154 -> 0xA26154
|
||||
assert(arch.decodeBranch4(0x9B592E, I({ 0xDA, 0xA2, 0x54, 0x61 }), memory) == std::make_pair(true, 0xA26154));
|
||||
REQUIRE((arch.decodeBranch4(0x9B592E, I({ 0xDA, 0xA2, 0x54, 0x61 }), memory) == std::make_pair(true, 0xA26154)));
|
||||
// FA 9A A0 93: JMPS 0x9A, 0x93A0 -> 0x9A93A0
|
||||
assert(arch.decodeBranch4(0x37F506, I({ 0xFA, 0x9A, 0xA0, 0x93 }), memory) == std::make_pair(true, 0x9A93A0));
|
||||
REQUIRE((arch.decodeBranch4(0x37F506, I({ 0xFA, 0x9A, 0xA0, 0x93 }), memory) == std::make_pair(true, 0x9A93A0)));
|
||||
// E2 FE 56 34: PCALL R14, 0x3456 -> 0x9A3456
|
||||
assert(arch.decodeBranch4(0x9A0100, I({ 0xE2, 0xFE, 0x56, 0x34 }), memory) == std::make_pair(true, 0x9A3456));
|
||||
REQUIRE((arch.decodeBranch4(0x9A0100, I({ 0xE2, 0xFE, 0x56, 0x34 }), memory) == std::make_pair(true, 0x9A3456)));
|
||||
// 8A 00 FE F0: JB 0xFD00.15, 0x340100
|
||||
assert(arch.decodeBranch4(0x340100, I({ 0x8A, 0x00, 0xFE, 0xF0 }), memory) == std::make_pair(true, 0x340100));
|
||||
REQUIRE((arch.decodeBranch4(0x340100, I({ 0x8A, 0x00, 0xFE, 0xF0 }), memory) == std::make_pair(true, 0x340100)));
|
||||
// 9A 00 FE F0: JNB 0xFD00.15, 0x340100
|
||||
assert(arch.decodeBranch4(0x340100, I({ 0x9A, 0x00, 0xFE, 0xF0 }), memory) == std::make_pair(true, 0x340100));
|
||||
REQUIRE((arch.decodeBranch4(0x340100, I({ 0x9A, 0x00, 0xFE, 0xF0 }), memory) == std::make_pair(true, 0x340100)));
|
||||
// AA 00 FE F0: JBC 0xFD00.15, 0x340100
|
||||
assert(arch.decodeBranch4(0x340100, I({ 0xAA, 0x00, 0xFE, 0xF0 }), memory) == std::make_pair(true, 0x340100));
|
||||
REQUIRE((arch.decodeBranch4(0x340100, I({ 0xAA, 0x00, 0xFE, 0xF0 }), memory) == std::make_pair(true, 0x340100)));
|
||||
// BA 00 FE F0: JNBS 0xFD00.15, 0x340100
|
||||
assert(arch.decodeBranch4(0x340100, I({ 0xBA, 0x00, 0xFE, 0xF0 }), memory) == std::make_pair(true, 0x340100));
|
||||
REQUIRE((arch.decodeBranch4(0x340100, I({ 0xBA, 0x00, 0xFE, 0xF0 }), memory) == std::make_pair(true, 0x340100)));
|
||||
// 8A 00 00 FE: invalid JB encoding (low nibble of q0 must be zero)
|
||||
assert(arch.decodeBranch4(0x340100, I({ 0x8A, 0x00, 0x00, 0xFE }), memory) == std::make_pair(false, 0U));
|
||||
REQUIRE((arch.decodeBranch4(0x340100, I({ 0x8A, 0x00, 0x00, 0xFE }), memory) == std::make_pair(false, 0U)));
|
||||
|
||||
// 00 F5 37 AB: stored code pointer 0x37:F500; AB is padding.
|
||||
uint8_t pointerBytes[] = { 0x00, 0xF5, 0x37, 0xAB };
|
||||
Pattern::Memory pointerMemory = { 0x200000, pointerBytes, sizeof(pointerBytes), 1, ARCH_C166 };
|
||||
assert(Pattern::decodePointer(0x200000, pointerMemory) == std::make_pair(true, 0x37F500));
|
||||
REQUIRE((Pattern::decodePointer(0x200000, pointerMemory) == std::make_pair(true, 0x37F500)));
|
||||
auto pointerResults = Pattern::find(Pattern::parse("*(00F537AB)"), pointerMemory, 1);
|
||||
assert(pointerResults.size() == 1 && pointerResults[0].address == 0x37F500 &&
|
||||
pointerResults[0].offset == 0 && pointerResults[0].size == 4);
|
||||
REQUIRE((pointerResults.size() == 1 && pointerResults[0].address == 0x37F500 &&
|
||||
pointerResults[0].offset == 0 && pointerResults[0].size == 4));
|
||||
|
||||
uint8_t callBytes[] = {
|
||||
0xCA, 0x00, 0x08, 0x00, // CALLA cc_UC, 0x0008 -> 0x900008
|
||||
|
|
@ -189,12 +190,12 @@ static void testC166Decoder() {
|
|||
0xCC, 0x00, // NOP
|
||||
};
|
||||
Pattern::Memory callMemory = { 0x900000, callBytes, sizeof(callBytes), 1, ARCH_C166 };
|
||||
assert(Pattern::decodeBranchReference(0, callMemory) == std::make_tuple(true, 0x900008U, 4U));
|
||||
REQUIRE((Pattern::decodeBranchReference(0, callMemory) == std::make_tuple(true, 0x900008U, 4U)));
|
||||
auto branchResults = Pattern::find(Pattern::parse("&BL(CA000800)"), callMemory, 1);
|
||||
assert(branchResults.size() == 1 && branchResults[0].address == 0x900008 &&
|
||||
branchResults[0].offset == 0 && branchResults[0].size == 4);
|
||||
REQUIRE((branchResults.size() == 1 && branchResults[0].address == 0x900008 &&
|
||||
branchResults[0].offset == 0 && branchResults[0].size == 4));
|
||||
auto nestedBranch4 = Pattern::find(Pattern::parse("{ DB00 }"), callMemory, 1);
|
||||
assert(nestedBranch4.size() == 1 && nestedBranch4[0].address == 0x900000);
|
||||
REQUIRE((nestedBranch4.size() == 1 && nestedBranch4[0].address == 0x900000));
|
||||
|
||||
uint8_t referenceBytes[] = {
|
||||
0xE6, 0xFC, 0x8C, 0x75, // MOV R12, #0x758C; SOF(0x20758C)
|
||||
|
|
@ -205,7 +206,7 @@ static void testC166Decoder() {
|
|||
};
|
||||
Pattern::Memory referenceMemory = { 0x207580, referenceBytes, sizeof(referenceBytes), 1, ARCH_C166 };
|
||||
auto nestedReference = Pattern::find(Pattern::parse("LDR{ DB00 }"), referenceMemory, 1);
|
||||
assert(nestedReference.size() == 1 && nestedReference[0].address == 0x207580);
|
||||
REQUIRE((nestedReference.size() == 1 && nestedReference[0].address == 0x207580));
|
||||
|
||||
uint8_t farReferenceBytes[] = {
|
||||
0xCC, 0x00, // NOP; keep the MOV pair 2-byte aligned, not 4-byte aligned
|
||||
|
|
@ -220,7 +221,7 @@ static void testC166Decoder() {
|
|||
};
|
||||
Pattern::Memory farReferenceMemory = { 0x227FFC, farReferenceBytes, sizeof(farReferenceBytes), 1, ARCH_C166 };
|
||||
auto nestedFarReference = Pattern::find(Pattern::parse("LDR{ DB00 }"), farReferenceMemory, 1);
|
||||
assert(nestedFarReference.size() == 1 && nestedFarReference[0].address == 0x227FFE);
|
||||
REQUIRE((nestedFarReference.size() == 1 && nestedFarReference[0].address == 0x227FFE));
|
||||
|
||||
uint8_t shortBranchBytes[] = {
|
||||
0x0D, 0x01, // JMPR cc_UC, 0x900004
|
||||
|
|
@ -229,9 +230,9 @@ static void testC166Decoder() {
|
|||
};
|
||||
Pattern::Memory shortBranchMemory = { 0x900000, shortBranchBytes, sizeof(shortBranchBytes), 1, ARCH_C166 };
|
||||
auto nestedBranch2 = Pattern::find(Pattern::parse("[ DB00 ]"), shortBranchMemory, 1);
|
||||
assert(nestedBranch2.size() == 1 && nestedBranch2[0].address == 0x900000);
|
||||
REQUIRE((nestedBranch2.size() == 1 && nestedBranch2[0].address == 0x900000));
|
||||
auto branch2Results = Pattern::find(Pattern::parse("&BL(0D01)"), shortBranchMemory, 1);
|
||||
assert(branch2Results.size() == 1 && branch2Results[0].address == 0x900004 && branch2Results[0].size == 2);
|
||||
REQUIRE((branch2Results.size() == 1 && branch2Results[0].address == 0x900004 && branch2Results[0].size == 2));
|
||||
|
||||
// C166 offsets must not receive ARM's Thumb function marker.
|
||||
uint8_t patternBytes[] = {
|
||||
|
|
@ -241,7 +242,7 @@ static void testC166Decoder() {
|
|||
auto pattern = Pattern::parse("00 B4");
|
||||
Pattern::Memory c166Memory = { 0x200000, patternBytes, sizeof(patternBytes), 1, ARCH_C166 };
|
||||
auto c166Results = Pattern::find(pattern, c166Memory, 1);
|
||||
assert(c166Results.size() == 1 && c166Results[0].address == 0x200000 && c166Results[0].size == 2);
|
||||
REQUIRE((c166Results.size() == 1 && c166Results[0].address == 0x200000 && c166Results[0].size == 2));
|
||||
|
||||
std::vector<uint8_t> xrefBytes(0x40, 0xCC);
|
||||
uint32_t codeTarget = 0x250020;
|
||||
|
|
@ -263,31 +264,31 @@ static void testC166Decoder() {
|
|||
}
|
||||
return false;
|
||||
};
|
||||
assert(hasXRef(codeXRefs, RESULT_TYPE_BRANCH, 0x250000));
|
||||
assert(hasXRef(codeXRefs, RESULT_TYPE_POINTER, 0x250004));
|
||||
assert(hasXRef(codeXRefs, RESULT_TYPE_REFERENCE, 0x250008));
|
||||
assert(hasXRef(dataXRefs, RESULT_TYPE_POINTER, 0x250010));
|
||||
assert(hasXRef(dataXRefs, RESULT_TYPE_REFERENCE, 0x250014));
|
||||
assert(Pattern::finXRefs(codeTarget | 1U, xrefMemory).empty());
|
||||
REQUIRE((hasXRef(codeXRefs, RESULT_TYPE_BRANCH, 0x250000)));
|
||||
REQUIRE((hasXRef(codeXRefs, RESULT_TYPE_POINTER, 0x250004)));
|
||||
REQUIRE((hasXRef(codeXRefs, RESULT_TYPE_REFERENCE, 0x250008)));
|
||||
REQUIRE((hasXRef(dataXRefs, RESULT_TYPE_POINTER, 0x250010)));
|
||||
REQUIRE((hasXRef(dataXRefs, RESULT_TYPE_REFERENCE, 0x250014)));
|
||||
REQUIRE((Pattern::finXRefs(codeTarget | 1U, xrefMemory).empty()));
|
||||
std::array<uint32_t, 2> pointers;
|
||||
assert(arch.decodePointers(0x250010, xrefMemory, pointers) == 2);
|
||||
assert(pointers[0] == 0x7E3DA0 && pointers[1] == dataTarget);
|
||||
REQUIRE((arch.decodePointers(0x250010, xrefMemory, pointers) == 2));
|
||||
REQUIRE((pointers[0] == 0x7E3DA0 && pointers[1] == dataTarget));
|
||||
|
||||
std::vector<uint8_t> shortMovBytes(8);
|
||||
Pattern::Memory shortMovMemory = { 0x50000, shortMovBytes.data(), shortMovBytes.size(), 1, ARCH_C166 };
|
||||
// E0 A4: MOV R4, #0xA; E0 55: MOV R5, #0x5.
|
||||
assert(arch.decodeReferences2(0x50000, I({ 0xE0, 0xA4, 0xE0, 0x55 }), shortMovMemory) ==
|
||||
std::vector<uint32_t>({ 0x5000A, 0x1400A }));
|
||||
REQUIRE((arch.decodeReferences2(0x50000, I({ 0xE0, 0xA4, 0xE0, 0x55 }), shortMovMemory) ==
|
||||
std::vector<uint32_t>({ 0x5000A, 0x1400A })));
|
||||
// E0 A4: MOV R4, #0xA; E6 F5 05 00: MOV R5, #0x5.
|
||||
assert(arch.decodeReferences2(0x50000, I({ 0xE0, 0xA4, 0xE6, 0xF5, 0x05, 0x00 }), shortMovMemory) ==
|
||||
std::vector<uint32_t>({ 0x5000A, 0x1400A }));
|
||||
REQUIRE((arch.decodeReferences2(0x50000, I({ 0xE0, 0xA4, 0xE6, 0xF5, 0x05, 0x00 }), shortMovMemory) ==
|
||||
std::vector<uint32_t>({ 0x5000A, 0x1400A })));
|
||||
// E6 F4 0A 00: MOV R4, #0xA; E0 55: MOV R5, #0x5.
|
||||
assert(arch.decodeReferences4(0x50000, I({ 0xE6, 0xF4, 0x0A, 0x00, 0xE0, 0x55 }), shortMovMemory) ==
|
||||
std::vector<uint32_t>({ 0x5000A, 0x1400A }));
|
||||
REQUIRE((arch.decodeReferences4(0x50000, I({ 0xE6, 0xF4, 0x0A, 0x00, 0xE0, 0x55 }), shortMovMemory) ==
|
||||
std::vector<uint32_t>({ 0x5000A, 0x1400A })));
|
||||
|
||||
}
|
||||
|
||||
static void testArmPattern() {
|
||||
TEST_CASE("ARM patterns", "[pattern][arm]") {
|
||||
uint8_t bytes[] = {
|
||||
0x00, 0x00, 0x9F, 0xE5, // LDR R0, [PC, #0]
|
||||
0x00, 0xF0, 0x20, 0xE3, // NOP
|
||||
|
|
@ -297,22 +298,22 @@ static void testArmPattern() {
|
|||
};
|
||||
Pattern::Memory memory = { 0xA0000000, bytes, sizeof(bytes), 1, ARCH_ARM };
|
||||
auto results = Pattern::find(Pattern::parse("LDR{ 00F020E3 }"), memory, 1);
|
||||
assert(results.size() == 1 && results[0].address == 0xA0000000);
|
||||
REQUIRE((results.size() == 1 && results[0].address == 0xA0000000));
|
||||
auto referenceResults = Pattern::find(Pattern::parse("&(00009FE5)"), memory, 1);
|
||||
assert(referenceResults.size() == 1 && referenceResults[0].address == 0xA0000010 && referenceResults[0].offset == 0);
|
||||
REQUIRE((referenceResults.size() == 1 && referenceResults[0].address == 0xA0000010 && referenceResults[0].offset == 0));
|
||||
auto fixedAddressResults = Pattern::find(Pattern::parse("<A0123456>"), memory, 1);
|
||||
assert(fixedAddressResults.size() == 1 && fixedAddressResults[0].address == 0xA0123456 && fixedAddressResults[0].size == 0);
|
||||
assert(Pattern::stringify(Pattern::parse("&BL(00F020E3)")) == "&BL(00 F0 20 E3)");
|
||||
assert(Pattern::stringify(Pattern::parse("<A0123456>")) == "< A0123456 >");
|
||||
assert(Pattern::stringify(Pattern::parse("LDR[ 00BF ] CC")) == "LDR[ 00 BF ] CC");
|
||||
REQUIRE((fixedAddressResults.size() == 1 && fixedAddressResults[0].address == 0xA0123456 && fixedAddressResults[0].size == 0));
|
||||
REQUIRE((Pattern::stringify(Pattern::parse("&BL(00F020E3)")) == "&BL(00 F0 20 E3)"));
|
||||
REQUIRE((Pattern::stringify(Pattern::parse("<A0123456>")) == "< A0123456 >"));
|
||||
REQUIRE((Pattern::stringify(Pattern::parse("LDR[ 00BF ] CC")) == "LDR[ 00 BF ] CC"));
|
||||
|
||||
// 00 F0 20 E3: NOP; a longer pattern must not overrun the fixture.
|
||||
uint8_t shortBytes[] = { 0x00, 0xF0, 0x20, 0xE3 };
|
||||
Pattern::Memory shortMemory = { 0xA0000000, shortBytes, sizeof(shortBytes), 1, ARCH_ARM };
|
||||
assert(Pattern::find(Pattern::parse("00F020E300F020E3"), shortMemory, 1).empty());
|
||||
REQUIRE((Pattern::find(Pattern::parse("00F020E300F020E3"), shortMemory, 1).empty()));
|
||||
}
|
||||
|
||||
static void testThumbPattern() {
|
||||
TEST_CASE("THUMB patterns", "[pattern][thumb]") {
|
||||
uint8_t referenceBytes[] = {
|
||||
0x00, 0x48, // LDR R0, [PC, #0]
|
||||
0x00, 0xBF, // NOP
|
||||
|
|
@ -321,11 +322,11 @@ static void testThumbPattern() {
|
|||
};
|
||||
Pattern::Memory referenceMemory = { 0xA0000000, referenceBytes, sizeof(referenceBytes), 1, ARCH_ARM };
|
||||
auto referenceResults = Pattern::find(Pattern::parse("LDR[ 00BF ]"), referenceMemory, 1);
|
||||
assert(referenceResults.size() == 1 && referenceResults[0].address == 0xA0000000);
|
||||
REQUIRE((referenceResults.size() == 1 && referenceResults[0].address == 0xA0000000));
|
||||
|
||||
auto xrefs = Pattern::finXRefs(0xA0000008, referenceMemory, 1);
|
||||
assert(xrefs.size() == 1 && xrefs[0].type == RESULT_TYPE_REFERENCE &&
|
||||
xrefs[0].address == 0xA0000000 && xrefs[0].offset == 0);
|
||||
REQUIRE((xrefs.size() == 1 && xrefs[0].type == RESULT_TYPE_REFERENCE &&
|
||||
xrefs[0].address == 0xA0000000 && xrefs[0].offset == 0));
|
||||
|
||||
uint8_t functionBytes[] = {
|
||||
0x10, 0xB5, // PUSH {R4, LR}
|
||||
|
|
@ -333,18 +334,25 @@ static void testThumbPattern() {
|
|||
};
|
||||
Pattern::Memory functionMemory = { 0xA0000000, functionBytes, sizeof(functionBytes), 1, ARCH_ARM };
|
||||
auto functionResults = Pattern::find(Pattern::parse("10 B5"), functionMemory, 1);
|
||||
assert(functionResults.size() == 1 && functionResults[0].address == 0xA0000001 && functionResults[0].size == 2);
|
||||
REQUIRE((functionResults.size() == 1 && functionResults[0].address == 0xA0000001 && functionResults[0].size == 2));
|
||||
auto adjustedResults = Pattern::find(Pattern::parse("00 BF + 1"), functionMemory, 1);
|
||||
assert(adjustedResults.size() == 1 && adjustedResults[0].address == 0xA0000003 &&
|
||||
adjustedResults[0].offset == 2 && adjustedResults[0].size == 2);
|
||||
REQUIRE((adjustedResults.size() == 1 && adjustedResults[0].address == 0xA0000003 &&
|
||||
adjustedResults[0].offset == 2 && adjustedResults[0].size == 2));
|
||||
}
|
||||
|
||||
TEST_CASE("TOML patterns library", "[patterns-library][toml]") {
|
||||
auto elka = parsePatternsToml(TEST_DATA_DIR "/patterns.toml", "ELKA");
|
||||
REQUIRE((elka.size() == 4));
|
||||
REQUIRE((elka[0].id == 0 && elka[0].funcName == "Common" && elka[0].patterns == std::vector<std::string>{ "5B 70" }));
|
||||
REQUIRE((elka[1].id == 1 && elka[1].funcName == "ELKA override" &&
|
||||
elka[1].patterns == std::vector<std::string>({ "00 00", "6C 61" })));
|
||||
REQUIRE((elka[2].id == 2 && elka[2].patterns.empty()));
|
||||
REQUIRE((elka[3].id == 3 && elka[3].patterns.empty()));
|
||||
|
||||
int main() {
|
||||
testArmDecoder();
|
||||
testC166Decoder();
|
||||
testArmPattern();
|
||||
testThumbPattern();
|
||||
std::cout << "All tests passed.\n";
|
||||
return 0;
|
||||
auto x75 = parsePatternsToml(TEST_DATA_DIR "/patterns.toml", "X75");
|
||||
REQUIRE((x75[1].funcName == "Default declaration" && x75[1].patterns == std::vector<std::string>{ "20 B5" }));
|
||||
REQUIRE((x75[2].patterns == std::vector<std::string>{ "30 B5" }));
|
||||
REQUIRE_THROWS_WITH(parsePatternsToml(TEST_DATA_DIR "/patterns.toml", "UNKNOWN"), "Unknown platform 'UNKNOWN'.");
|
||||
REQUIRE_THROWS_WITH(parsePatternsToml(TEST_DATA_DIR "/patterns.toml", "BROKEN"),
|
||||
"Declaration for [000] must be a string.");
|
||||
}
|
||||
|
|
|
|||
24
test/patterns.toml
Normal file
24
test/patterns.toml
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
[platforms]
|
||||
ALL = ["NSG", "ELKA", "SG", "X75", "BROKEN"]
|
||||
NSG = ["NSG", "ELKA"]
|
||||
SG = ["SG", "X75"]
|
||||
|
||||
[003]
|
||||
decl = "Unsupported"
|
||||
SG = "30 B5"
|
||||
|
||||
[000]
|
||||
decl = "Common"
|
||||
decl_BROKEN = 42
|
||||
ALL = "5B 70"
|
||||
|
||||
[002]
|
||||
decl = "Disabled override"
|
||||
ALL = "30 B5"
|
||||
ELKA = false
|
||||
|
||||
[001]
|
||||
decl = "Default declaration"
|
||||
decl_ELKA = "ELKA override"
|
||||
NSG = ["00 00", "6C 61"]
|
||||
SG = "20 B5"
|
||||
1
third_party/Catch2
vendored
Submodule
1
third_party/Catch2
vendored
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 317ac1ed4c0bb6e6b91eafc817e05c488feffcb3
|
||||
1
third_party/tomlplusplus
vendored
Submodule
1
third_party/tomlplusplus
vendored
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 30172438cee64926dc41fdd9c11fb3ba5b2ba9de
|
||||
Loading…
Add table
Reference in a new issue