mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(libc): implement binary search with guest comparator ABI
This commit is contained in:
@@ -307,6 +307,15 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(guest_memory PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
|
||||
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:$<TARGET_FILE_DIR:libc>")
|
||||
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)
|
||||
|
||||
@@ -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<size_t>::max() / size)
|
||||
throw std::overflow_error("bsearch: array size overflow");
|
||||
const auto* bytes = static_cast<const unsigned char*>(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<unsigned char*>(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");
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <array>
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <source_location>
|
||||
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<const int*>(key);
|
||||
const int right = static_cast<const Record*>(record)->key;
|
||||
return (left > right) - (left < right);
|
||||
}
|
||||
int main() {
|
||||
const std::array<Record, 6> records{{{1, "one"}, {3, "three"}, {5, "five"},
|
||||
{5, "duplicate"}, {7, "seven"}, {9, "nine"}}};
|
||||
std::array<unsigned char, sizeof(records)> before{};
|
||||
std::memcpy(before.data(), records.data(), sizeof(records));
|
||||
for (int key = 0; key <= 10; ++key) {
|
||||
calls = 0;
|
||||
const auto* found = static_cast<Record*>(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<std::size_t>::max(), sizeof(Record), Compare); }
|
||||
catch (const std::overflow_error&) { rejected = true; }
|
||||
Require(rejected);
|
||||
}
|
||||
@@ -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`.
|
||||
|
||||
Reference in New Issue
Block a user