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
12 changes: 11 additions & 1 deletion core/libs/prx/libSceNKWebKit/src/WebCoreColorSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,22 @@
#include <cstdint>
#include <cstdio>
#include <stdexcept>
#include <type_traits>

namespace {

static_assert(std::is_trivially_copyable_v<WebCore::Color>);

constexpr std::uint64_t kOutOfLine = 1ULL << 51;
constexpr std::uint64_t kUseColorFunctionSerialization = 1ULL << 49;

WebCore::Color ReadGuestColor(const WebCore::Color* color, const char* function) {
WebCore::Color value{};
if (!GuestCopyIn_nid_no_patch(&value, color, sizeof(value)))
NotImplementedAt_nid_no_patch(function, __FILE__, __LINE__);
return value;
}

unsigned FractionDigits(std::uint8_t alpha, std::array<char, 4>& digits) {
const auto digit = [](unsigned value) { return static_cast<char>('0' + value); };
if (((alpha * 100 + 0x7F) / 0xFF * 0xFF + 50) / 100 != alpha) {
Expand All @@ -62,7 +72,7 @@ NKWebKit::WtfAbi::String MakeString(const char* text, std::size_t length) {
extern "C" NKWebKit::WtfAbi::String APS5_VABI _ZN7WebCore20serializationForHTMLERKNS_5ColorE(const WebCore::Color* color) {
if (!color) throw std::invalid_argument("Color::serializationForHTML: null color");

const std::uint64_t bits = color->m_colorAndFlags;
const std::uint64_t bits = ReadGuestColor(color, __func__).m_colorAndFlags;
if (bits & kOutOfLine)
NotImplemented_nid_no_patch("Color::serializationForHTML: out-of-line color");
if (bits & kUseColorFunctionSerialization)
Expand Down
89 changes: 89 additions & 0 deletions core/libs/prx/libSceNKWebKit/tests/WebCoreColorSerialization.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libSceNKWebKit/libSceNKWebKit.hpp"

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <string>

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

namespace {

void Require(bool value, int line) {
Expand Down Expand Up @@ -43,5 +53,84 @@ int main() {

const WebCore::Color invalid{0};
REQUIRE(Text(_ZN7WebCore20serializationForHTMLERKNS_5ColorE(&invalid)) == "rgba(0, 0, 0, 0)");

{
struct GuardedPages {
unsigned char* base = nullptr;
std::size_t pageSize = 0;

GuardedPages() {
#ifdef _WIN32
SYSTEM_INFO info{};
GetSystemInfo(&info);
pageSize = info.dwPageSize;
base = static_cast<unsigned char*>(VirtualAlloc(nullptr, pageSize * 3, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
REQUIRE(base != nullptr);
DWORD previous = 0;
REQUIRE(VirtualProtect(base + pageSize * 2, pageSize, PAGE_NOACCESS, &previous) != 0);
#else
const long size = sysconf(_SC_PAGESIZE);
REQUIRE(size > 0);
pageSize = static_cast<std::size_t>(size);
void* mapped = mmap(nullptr, pageSize * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
REQUIRE(mapped != MAP_FAILED);
base = static_cast<unsigned char*>(mapped);
REQUIRE(mprotect(base + pageSize * 2, pageSize, PROT_NONE) == 0);
#endif
}

~GuardedPages() {
if (!base) return;
#ifdef _WIN32
VirtualFree(base, 0, MEM_RELEASE);
#else
munmap(base, pageSize * 3);
#endif
}

void ProtectReadOnly() {
#ifdef _WIN32
DWORD previous = 0;
REQUIRE(VirtualProtect(base, pageSize, PAGE_READONLY, &previous) != 0);
#else
REQUIRE(mprotect(base, pageSize, PROT_READ) == 0);
#endif
}

unsigned char* Readable() const { return base; }
unsigned char* Guard() const { return base + pageSize * 2; }
};

auto requireNotImplemented = [](auto&& fn) {
const auto before = NotImplementedCalls_nid_no_patch();
bool threw = false;
try {
fn();
} catch (const std::runtime_error&) {
threw = true;
}
REQUIRE(threw && NotImplementedCalls_nid_no_patch() == before + 1);
};

bool threwInvalid = false;
try {
_ZN7WebCore20serializationForHTMLERKNS_5ColorE(nullptr);
} catch (const std::invalid_argument&) {
threwInvalid = true;
}
REQUIRE(threwInvalid);

GuardedPages pages;
std::memcpy(pages.Readable(), &opaque, sizeof(opaque));
pages.ProtectReadOnly();
REQUIRE(Text(_ZN7WebCore20serializationForHTMLERKNS_5ColorE(reinterpret_cast<const WebCore::Color*>(pages.Readable()))) == "#0c2238");

requireNotImplemented([&] {
_ZN7WebCore20serializationForHTMLERKNS_5ColorE(reinterpret_cast<const WebCore::Color*>(pages.Guard()));
});
requireNotImplemented([&] {
_ZN7WebCore20serializationForHTMLERKNS_5ColorE(reinterpret_cast<const WebCore::Color*>(pages.Guard() - 4));
});
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libSceNKWebKit

## add Functional

- [WebCoreColorSerialization guest pointer fault behavior](../../../core/libs/prx/libSceNKWebKit/src/WebCoreColorSerialization.cpp) (libSceNKWebKit) - Console fault behavior for invalid guest pointers in `WebCore::serializationForHTML` is unmeasured; the export reads guest `WebCore::Color` storage through `GuestCopyIn_nid_no_patch` and calls `NotImplementedAt_nid_no_patch` on unmapped or unreadable guest memory. Closes when: console behavior for invalid guest pointers in `WebCore::serializationForHTML` is verified on hardware.
Loading