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
18 changes: 11 additions & 7 deletions core/libs/prx/libc/src/General.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,23 +628,27 @@ extern "C" int CopyGuestString_nid_no_patch(const char* source, std::size_t maxi
if (source == nullptr) return 14;
const auto address = reinterpret_cast<std::uintptr_t>(source);
std::vector<char> bytes;
try {
bytes.resize(maximum);
} catch (const std::bad_alloc&) {
return 12;
}
std::size_t offset = 0;
while (offset < bytes.size()) {
while (offset < maximum) {
if (address > std::numeric_limits<std::uintptr_t>::max() - offset) return 14;
const auto current = address + offset;
const auto pageRemaining = HostPageSize() - current % HostPageSize();
const auto count = std::min(pageRemaining, bytes.size() - offset);
const auto count = std::min(pageRemaining, maximum - offset);
try {
bytes.resize(offset + count);
} catch (const std::bad_alloc&) {
return 12;
} catch (const std::length_error&) {
return 12;
}
if (!CopyGuestBytes(bytes.data() + offset, reinterpret_cast<const void*>(current), count)) return 14;
if (const auto* end = static_cast<const char*>(std::memchr(bytes.data() + offset, 0, count))) {
try {
destination.assign(bytes.data(), static_cast<std::size_t>(end - bytes.data()));
} catch (const std::bad_alloc&) {
return 12;
} catch (const std::length_error&) {
return 12;
}
return 0;
}
Expand Down
12 changes: 12 additions & 0 deletions core/libs/prx/libc/tests/cmake/guest_copy_guest_string.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if(BUILD_TESTING)
add_test_executable(guest_copy_guest_string_tests ${LIBS_INCLUDE_DIR}/tests/GuestCopyGuestString.cpp)
target_include_directories(guest_copy_guest_string_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_copy_guest_string_tests PRIVATE libc)
configure_windows_unwind(guest_copy_guest_string_tests)
add_test(NAME guest_copy_guest_string COMMAND guest_copy_guest_string_tests)
set_tests_properties(guest_copy_guest_string PROPERTIES TIMEOUT 10)
if(WIN32)
set_tests_properties(guest_copy_guest_string PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
endif()
73 changes: 73 additions & 0 deletions core/libs/tests/GuestCopyGuestString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "prx/libc/include/General.hpp"
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <string>

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

static void Check(bool value, int line) {
if (!value) {
std::fprintf(stderr, "CopyGuestString check failed at line %d\n", line);
std::abort();
}
}
#define Require(value) Check((value), __LINE__)

static char* ReadablePageBeforeGuard() {
#ifdef _WIN32
SYSTEM_INFO system{};
GetSystemInfo(&system);
const std::size_t pageSize = system.dwPageSize;
auto* pages = static_cast<char*>(VirtualAlloc(nullptr, 2 * pageSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
Require(pages != nullptr);
DWORD oldProtection = 0;
Require(VirtualProtect(pages + pageSize, pageSize, PAGE_NOACCESS, &oldProtection) != 0);
#else
const long hostPageSize = sysconf(_SC_PAGESIZE);
Require(hostPageSize > 0);
const std::size_t pageSize = static_cast<std::size_t>(hostPageSize);
auto* pages = static_cast<char*>(mmap(nullptr, 2 * pageSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
Require(pages != MAP_FAILED);
Require(mprotect(pages + pageSize, pageSize, PROT_NONE) == 0);
#endif
std::memset(pages, 'x', pageSize);
return pages + pageSize;
}

int main() {
std::string copy = "unchanged";
Require(CopyGuestString_nid_no_patch(nullptr, 16, copy) == 14 && copy == "unchanged");
Require(CopyGuestString_nid_no_patch(reinterpret_cast<const char*>(0x10), 16, copy) == 14 && copy == "unchanged");
Require(CopyGuestString_nid_no_patch("guest", 0, copy) == 63 && copy == "unchanged");
Require(CopyGuestString_nid_no_patch("guest", 5, copy) == 63 && copy == "unchanged");
Require(CopyGuestString_nid_no_patch("guest", 6, copy) == 0 && copy == "guest");
Require(CopyGuestString_nid_no_patch("", 1, copy) == 0 && copy.empty());
Require(CopyGuestString_nid_no_patch("guest", 16, copy) == 0 && copy == "guest");

const std::string longText(100000, 'x');
Require(CopyGuestString_nid_no_patch(longText.c_str(), longText.size(), copy) == 63);
Require(CopyGuestString_nid_no_patch(longText.c_str(), longText.size() + 1, copy) == 0 && copy == longText);

const std::size_t unbounded = std::numeric_limits<std::size_t>::max() / 2;
Require(CopyGuestString_nid_no_patch("guest", unbounded, copy) == 0 && copy == "guest");
Require(CopyGuestString_nid_no_patch(longText.c_str(), unbounded, copy) == 0 && copy == longText);

char* const guard = ReadablePageBeforeGuard();
std::memcpy(guard - 4, "abc", 4);
Require(CopyGuestString_nid_no_patch(guard - 4, unbounded, copy) == 0 && copy == "abc");
Require(CopyGuestString_nid_no_patch(guard - 4, 4, copy) == 0 && copy == "abc");
std::memcpy(guard - 4, "abcd", 4);
copy = "unchanged";
Require(CopyGuestString_nid_no_patch(guard - 4, 4, copy) == 63 && copy == "unchanged");
Require(CopyGuestString_nid_no_patch(guard - 4, 5, copy) == 14 && copy == "unchanged");
Require(CopyGuestString_nid_no_patch(guard - 4, unbounded, copy) == 14 && copy == "unchanged");
}
Loading