From 6cdf4a5be1634d19ff977e242efc992190acf7c7 Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Fri, 25 Sep 2026 07:24:17 +1000 Subject: [PATCH] feat(libc): implement binary search with guest comparator ABI --- core/libs/CMakeLists.txt | 9 ++++++ core/libs/prx/libc/src/FileAndHeap.cpp | 21 ++++++++++++ core/libs/tests/GuestSearch.cpp | 45 ++++++++++++++++++++++++++ docs/YAMAGI_QUAKE2.md | 10 ++++++ 4 files changed, 85 insertions(+) create mode 100644 core/libs/tests/GuestSearch.cpp diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index 20103697f..35cdadb35 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -307,6 +307,15 @@ if(BUILD_TESTING) set_tests_properties(guest_memory PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_executable(guest_search_tests tests/GuestSearch.cpp) + target_include_directories(guest_search_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(guest_search_tests PRIVATE libc) + configure_windows_unwind(guest_search_tests) + add_test(NAME guest_search COMMAND guest_search_tests) + if(WIN32) + set_tests_properties(guest_search PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_executable(guest_string_tests tests/GuestStrings.cpp) target_include_directories(guest_string_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(guest_string_tests PRIVATE libc libScePosixForWebKit) diff --git a/core/libs/prx/libc/src/FileAndHeap.cpp b/core/libs/prx/libc/src/FileAndHeap.cpp index 79b7d70fb..b3e3ed183 100644 --- a/core/libs/prx/libc/src/FileAndHeap.cpp +++ b/core/libs/prx/libc/src/FileAndHeap.cpp @@ -157,6 +157,27 @@ int APS5_VABI posix_memalign_nid_postfix(void** pointer, size_t alignment, size_ return ApplicationHeapPosixAlign_nid_no_patch(pointer, alignment, size); } +void* APS5_VABI bsearch_nid_postfix(const void* key, const void* base, size_t count, + size_t size, int (APS5_VABI *compare)(const void*, const void*)) { + if (count == 0) return nullptr; + if (!key || !base || !compare || size == 0) + throw std::invalid_argument("bsearch: invalid arguments"); + if (count > std::numeric_limits::max() / size) + throw std::overflow_error("bsearch: array size overflow"); + const auto* bytes = static_cast(base); + size_t first = 0; + while (count != 0) { + const size_t half = count / 2; + const size_t middle = first + half; + const auto* element = bytes + middle * size; + const int result = compare(key, element); + if (result == 0) return const_cast(element); + if (result < 0) count = half; + else { first = middle + 1; count -= half + 1; } + } + return nullptr; +} + void APS5_VABI qsort_nid_postfix(void* base, size_t count, size_t size, int (APS5_VABI *compare)(const void*, const void*)) { if (!compare) throw std::invalid_argument("qsort: null comparator"); if (size == 0) throw std::invalid_argument("qsort: zero element size"); diff --git a/core/libs/tests/GuestSearch.cpp b/core/libs/tests/GuestSearch.cpp new file mode 100644 index 000000000..f1572c364 --- /dev/null +++ b/core/libs/tests/GuestSearch.cpp @@ -0,0 +1,45 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include +#include +using Comparator = int (APS5_VABI *)(const void*, const void*); +extern "C" void* APS5_VABI bsearch_nid_postfix(const void*, const void*, std::size_t, std::size_t, Comparator); +static void Require(bool value, std::source_location location = std::source_location::current()) { + if (!value) { std::fprintf(stderr, "Guest search failed at line %u\n", location.line()); std::abort(); } +} +struct Record { int key; char payload[13]; }; +static int calls = 0; +static int APS5_VABI Compare(const void* key, const void* record) { + ++calls; + const int left = *static_cast(key); + const int right = static_cast(record)->key; + return (left > right) - (left < right); +} +int main() { + const std::array records{{{1, "one"}, {3, "three"}, {5, "five"}, + {5, "duplicate"}, {7, "seven"}, {9, "nine"}}}; + std::array before{}; + std::memcpy(before.data(), records.data(), sizeof(records)); + for (int key = 0; key <= 10; ++key) { + calls = 0; + const auto* found = static_cast(bsearch_nid_postfix(&key, records.data(), records.size(), sizeof(Record), Compare)); + Require(calls <= 3); + if (key % 2) Require(found && found->key == key && found >= records.data() && found < records.data() + records.size()); + else Require(!found); + } + Require(std::memcmp(before.data(), records.data(), sizeof(records)) == 0); + calls = 0; + int key = 1; + Require(bsearch_nid_postfix(&key, nullptr, 0, sizeof(Record), Compare) == nullptr && calls == 0); + Require(bsearch_nid_postfix(&key, records.data(), 1, sizeof(Record), Compare) == records.data()); + key = 2; + Require(!bsearch_nid_postfix(&key, records.data(), 1, sizeof(Record), Compare)); + bool rejected = false; + try { bsearch_nid_postfix(&key, records.data(), std::numeric_limits::max(), sizeof(Record), Compare); } + catch (const std::overflow_error&) { rejected = true; } + Require(rejected); +} diff --git a/docs/YAMAGI_QUAKE2.md b/docs/YAMAGI_QUAKE2.md index 9e9173fd6..6cbfe79b1 100644 --- a/docs/YAMAGI_QUAKE2.md +++ b/docs/YAMAGI_QUAKE2.md @@ -531,3 +531,13 @@ internal rendering-wait encoding still require actual game execution validation. The shader-link wrapper now propagates unsupported-state runtime exceptions from its helpers, preserving the README's strict failure policy; its regression test checks the exception and that output registers are not partially published. + +## Follow-up: binary search + +Implemented bsearch with direct SysV comparator calls, key-first argument order, +and overflow-checked element addressing. Regression coverage uses records and a +distinct key type to check callback argument order, duplicates, absent keys, +empty and singleton arrays, and unchanged input bytes. All twenty-nine tests +have passed (the new test was rerun after correcting its padding-byte snapshot). +The unchanged game resolves bsearch and stops at `jbz9I9vkqkk` (`vsprintf`), +still before guest entry with exit `0xc0000139`.