diff --git a/core/libs/prx/libSceNKWebKit/src/WebCoreColorSerialization.cpp b/core/libs/prx/libSceNKWebKit/src/WebCoreColorSerialization.cpp index 19ae2ceea548..2c5ce006feb7 100644 --- a/core/libs/prx/libSceNKWebKit/src/WebCoreColorSerialization.cpp +++ b/core/libs/prx/libSceNKWebKit/src/WebCoreColorSerialization.cpp @@ -32,12 +32,22 @@ #include #include #include +#include namespace { +static_assert(std::is_trivially_copyable_v); + 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& digits) { const auto digit = [](unsigned value) { return static_cast('0' + value); }; if (((alpha * 100 + 0x7F) / 0xFF * 0xFF + 50) / 100 != alpha) { @@ -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) diff --git a/core/libs/prx/libSceNKWebKit/tests/WebCoreColorSerialization.cpp b/core/libs/prx/libSceNKWebKit/tests/WebCoreColorSerialization.cpp index ef0fff2621bc..1c5d51109511 100644 --- a/core/libs/prx/libSceNKWebKit/tests/WebCoreColorSerialization.cpp +++ b/core/libs/prx/libSceNKWebKit/tests/WebCoreColorSerialization.cpp @@ -1,10 +1,20 @@ +#include "prx/libc/include/General.hpp" #include "prx/libc/include/general/VabiMacros.hpp" #include "prx/libSceNKWebKit/libSceNKWebKit.hpp" #include #include +#include +#include #include +#ifdef _WIN32 +#include +#else +#include +#include +#endif + namespace { void Require(bool value, int line) { @@ -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(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(size); + void* mapped = mmap(nullptr, pageSize * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + REQUIRE(mapped != MAP_FAILED); + base = static_cast(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(pages.Readable()))) == "#0c2238"); + + requireNotImplemented([&] { + _ZN7WebCore20serializationForHTMLERKNS_5ColorE(reinterpret_cast(pages.Guard())); + }); + requireNotImplemented([&] { + _ZN7WebCore20serializationForHTMLERKNS_5ColorE(reinterpret_cast(pages.Guard() - 4)); + }); + } return 0; } diff --git a/docs/dev/debt/changes/fix-libSceNKWebKit-webcore-color-serialization-guest-pointers.md b/docs/dev/debt/changes/fix-libSceNKWebKit-webcore-color-serialization-guest-pointers.md new file mode 100644 index 000000000000..108948114bc9 --- /dev/null +++ b/docs/dev/debt/changes/fix-libSceNKWebKit-webcore-color-serialization-guest-pointers.md @@ -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.