Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/libs/prx/libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,16 @@ if(BUILD_TESTING)
set_tests_properties(guest_underflow_error PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test_executable(guest_stpcpy_extent_tests ${LIBS_INCLUDE_DIR}/tests/GuestStpcpyExtent.cpp)
target_include_directories(guest_stpcpy_extent_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_stpcpy_extent_tests PRIVATE libc)
configure_windows_unwind(guest_stpcpy_extent_tests)
add_test(NAME guest_stpcpy_extent COMMAND guest_stpcpy_extent_tests)
set_tests_properties(guest_stpcpy_extent PROPERTIES TIMEOUT 20)
if(WIN32)
set_tests_properties(guest_stpcpy_extent PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test_executable(guest_throw_bad_array_new_length_tests ${LIBS_INCLUDE_DIR}/tests/GuestThrowBadArrayNewLength.cpp)
target_include_directories(guest_throw_bad_array_new_length_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_throw_bad_array_new_length_tests PRIVATE libc)
Expand Down
86 changes: 86 additions & 0 deletions core/libs/tests/GuestStpcpyExtent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "prx/libc/include/Api.hpp"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>

static void Require(bool condition) {
if (!condition) {
std::fputs("GuestStpcpyExtent check failed\n", stderr);
std::abort();
}
}

#ifdef _WIN32
#include <windows.h>
#else
#include <sys/mman.h>
#include <unistd.h>
#endif

class Buffer {
unsigned char* allocation;
std::size_t page;
public:
Buffer() {
#ifdef _WIN32
SYSTEM_INFO info{};
GetSystemInfo(&info);
page = info.dwPageSize;
allocation = static_cast<unsigned char*>(VirtualAlloc(nullptr, page * 3, MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS));
Require(allocation != nullptr);
DWORD previous;
Require(VirtualProtect(allocation + page, page, PAGE_READWRITE, &previous) != 0);
#else
const long size = sysconf(_SC_PAGESIZE);
Require(size > 0);
page = static_cast<std::size_t>(size);
allocation = static_cast<unsigned char*>(mmap(nullptr, page * 3, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
Require(allocation != MAP_FAILED);
Require(mprotect(allocation + page, page, PROT_READ | PROT_WRITE) == 0);
#endif
std::memset(allocation + page, 0xa5, page);
}
~Buffer() {
#ifdef _WIN32
Require(VirtualFree(allocation, 0, MEM_RELEASE) != 0);
#else
Require(munmap(allocation, page * 3) == 0);
#endif
}
Buffer(const Buffer&) = delete;
Buffer& operator=(const Buffer&) = delete;
template<class T = unsigned char>
T* At(std::size_t count) {
Require(count <= (page - 32) / sizeof(T));
return reinterpret_cast<T*>(allocation + page * 2 - (std::max)(count, std::size_t{1}) * sizeof(T));
}
};

template<class T>
static void Prefix(const T* data) {
for (int index = 1; index <= 8; ++index) Require(data[-index] == static_cast<T>(sizeof(T) == 1 ? 0xa5 : 0xa5a5));
}

int main() {
Buffer input, output;
for (std::size_t count = 0; count <= 192; ++count) {
auto* source = input.At<char>(count + 1);
auto* destination = output.At<char>(count + 1);
for (std::size_t index = 0; index < count; ++index) source[index] = static_cast<char>(1 + (index * 97 + count) % 255);
source[count] = 0;
const std::string saved(source, count + 1);
std::memset(destination - 16, 0xa5, count + 17);
char* end = stpcpy_nid_postfix(destination, source);
Require(end == destination + count && *end == 0);
Require(std::equal(saved.begin(), saved.end(), destination));
Require(std::equal(saved.begin(), saved.end(), source));
Require(stpcpy_nid_postfix(end, "") == end);
Prefix(reinterpret_cast<unsigned char*>(destination));
}
}
Loading