mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #8 from Pacuka/feat/scerandom
feat(random): implement sceRandomGetRandomNumber - Fill the buffer from `std::random_device`. - Return `SCE_RANDOM_ERROR_INVALID` (`0x817C0016`) for a null buffer or a size above `SCE_RANDOM_MAX_SIZE` (64), matching the system library, and leave the buffer untouched in that case. - Add a `guest_random` test that covers the invalid arguments, a zero size, partial-word sizes (no write past `size`), and two 64-byte draws that differ.
This commit is contained in:
@@ -286,6 +286,14 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(guest_errors PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_executable(guest_random_tests tests/GuestRandom.cpp)
|
||||
target_include_directories(guest_random_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_random_tests PRIVATE libSceRandom)
|
||||
add_test(NAME guest_random COMMAND guest_random_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_random PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceRandom>")
|
||||
endif()
|
||||
add_executable(guest_resolver_tests tests/GuestResolver.cpp)
|
||||
target_include_directories(guest_resolver_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_resolver_tests PRIVATE libScePosixForWebKit)
|
||||
|
||||
@@ -1,15 +1,29 @@
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <random>
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int SCE_RANDOM_ERROR_INVALID = static_cast<int>(0x817C0016);
|
||||
constexpr std::size_t SCE_RANDOM_MAX_SIZE = 64;
|
||||
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
int APS5_VABI sceRandomGetRandomNumber(void* buf, size_t size) {
|
||||
(void)buf;
|
||||
(void)size;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
if (!buf || size > SCE_RANDOM_MAX_SIZE) return SCE_RANDOM_ERROR_INVALID;
|
||||
static thread_local std::random_device device;
|
||||
auto* bytes = static_cast<unsigned char*>(buf);
|
||||
for (std::size_t offset = 0; offset < size; offset += sizeof(unsigned int)) {
|
||||
const unsigned int value = device();
|
||||
std::memcpy(bytes + offset, &value, std::min(sizeof(value), size - offset));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
extern "C" {
|
||||
int APS5_VABI sceRandomGetRandomNumber(void*, std::size_t);
|
||||
}
|
||||
static void Require(bool value) { if (!value) std::abort(); }
|
||||
int main() {
|
||||
constexpr int invalid = static_cast<int>(0x817C0016);
|
||||
unsigned char buffer[80];
|
||||
Require(sceRandomGetRandomNumber(nullptr, 16) == invalid);
|
||||
std::memset(buffer, 0xAA, sizeof(buffer));
|
||||
Require(sceRandomGetRandomNumber(buffer, 65) == invalid);
|
||||
Require(buffer[0] == 0xAA);
|
||||
Require(sceRandomGetRandomNumber(buffer, 0) == 0);
|
||||
Require(buffer[0] == 0xAA);
|
||||
Require(sceRandomGetRandomNumber(buffer, 7) == 0);
|
||||
Require(buffer[7] == 0xAA);
|
||||
unsigned char first[64] = {};
|
||||
unsigned char second[64] = {};
|
||||
Require(sceRandomGetRandomNumber(first, sizeof(first)) == 0);
|
||||
Require(sceRandomGetRandomNumber(second, sizeof(second)) == 0);
|
||||
Require(std::memcmp(first, second, sizeof(first)) != 0);
|
||||
}
|
||||
Reference in New Issue
Block a user