Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Never cache a cover under another thread's root raise

Leonardo Lobatoba901171bf9f
diff --git a/src/homebrew/artwork_backend.cpp b/src/homebrew/artwork_backend.cpp
index 38d4ea0..2224621 100644
--- a/src/homebrew/artwork_backend.cpp
+++ b/src/homebrew/artwork_backend.cpp
@@ -1,6 +1,7 @@
#include "homebrew/artwork_backend.hpp"
#include "platform/managed_directory.h"
+#include "platform/ps5/title_allow_list.h"
#include "steam/backend/http_client.h"
#include <algorithm>
@@ -164,7 +165,8 @@ ArtworkImage FrameToPortrait(const ArtworkImage& src, std::uint32_t target_w = 6
out.height = target_h;
out.rgba.resize(target_w * target_h * 4, 0);
- const int banner_h = static_cast<int>(target_w * src.height / src.width);
+ // At least one row, so a very wide image still shows its banner.
+ const int banner_h = std::max(1, static_cast<int>(target_w * src.height / src.width));
const int offset_y = static_cast<int>((target_h - banner_h) / 2);
const int bottom_margin = static_cast<int>(target_h) - (offset_y + banner_h);
@@ -202,12 +204,13 @@ struct HttpArtworkBackend::Cache {
std::mutex mutex;
std::optional<platform::ManagedDirectory> dir;
- bool EnsureDir(const std::string& path) {
+ // Only a save creates the folder, while it holds the effective UID.
+ bool EnsureDir(const std::string& path, bool create) {
if (dir && dir->Validate()) return true;
if (path.empty()) return false;
const std::filesystem::path target = std::filesystem::path(path).lexically_normal();
dir = platform::ManagedDirectory::Open(target, platform::DirectoryPolicy::kPrivate);
- if (dir) return true;
+ if (dir || !create) return dir.has_value();
const auto parent_path = target.parent_path();
const auto child_name = target.filename().string();
@@ -224,12 +227,16 @@ struct HttpArtworkBackend::Cache {
// The cached file's bytes, and whether it is younger than the TTL.
std::optional<std::vector<std::uint8_t>> Read(const std::string& path, const std::string& name, bool* fresh) {
*fresh = false;
- if (!EnsureDir(path)) return std::nullopt;
- struct stat st{};
- if (!dir->InspectChild(name, &st) || !S_ISREG(st.st_mode) || st.st_size <= 0 ||
- static_cast<std::size_t>(st.st_size) > kMaxArtworkTransferBytes) return std::nullopt;
+ if (!EnsureDir(path, false)) return std::nullopt;
+ // Size and age come from the opened file: another backend may rename a new copy over it.
const int fd = dir->OpenRegularFile(name);
if (fd < 0) return std::nullopt;
+ struct stat st{};
+ if (::fstat(fd, &st) != 0 || !S_ISREG(st.st_mode) || st.st_size <= 0 ||
+ static_cast<std::size_t>(st.st_size) > kMaxArtworkTransferBytes) {
+ ::close(fd);
+ return std::nullopt;
+ }
std::vector<std::uint8_t> bytes(static_cast<std::size_t>(st.st_size));
std::size_t offset = 0;
while (offset < bytes.size()) {
@@ -246,9 +253,13 @@ struct HttpArtworkBackend::Cache {
}
// Writes a unique temporary file and renames it over the cached one. The title-icon builder
- // has its own backend, so two writers can save the same file at once.
+ // has its own backend, so two writers can save the same file at once. Another thread's root raise
+ // would leave a root-owned folder or file that the app user can never use again, so a save that
+ // can't hold the effective UID right away is skipped; the next load downloads the cover again.
void Save(const std::string& path, const std::string& name, const std::vector<std::uint8_t>& bytes) {
- if (bytes.empty() || bytes.size() > kMaxArtworkTransferBytes || !EnsureDir(path)) return;
+ if (bytes.empty() || bytes.size() > kMaxArtworkTransferBytes) return;
+ const auto uid = ps5::TryHoldEffectiveUid();
+ if (!uid || !EnsureDir(path, true)) return;
static std::atomic<std::uint64_t> sequence{0};
const std::string temp_name = "." + name + "." + std::to_string(::getpid()) + "." +
std::to_string(sequence.fetch_add(1)) + ".tmp";
diff --git a/tests/homebrew_artwork_backend_test.cpp b/tests/homebrew_artwork_backend_test.cpp
index 03d5c58..00b6e42 100644
--- a/tests/homebrew_artwork_backend_test.cpp
+++ b/tests/homebrew_artwork_backend_test.cpp
@@ -1,5 +1,6 @@
#include "homebrew/artwork_backend.hpp"
#include "platform/managed_directory.h"
+#include "platform/ps5/title_allow_list.h"
#include <atomic>
#include <cassert>
@@ -8,7 +9,9 @@
#include <ctime>
#include <filesystem>
#include <functional>
+#include <future>
#include <string>
+#include <thread>
#include <vector>
#include <sys/time.h>
#include <unistd.h>
@@ -120,6 +123,18 @@ void DecodesPngAndJpegToRgba() {
assert(jpeg->rgba[3] == 255 && jpeg->rgba[7] == 255);
}
+// A solid red 700x1 RGB PNG: wider than a card, so its framed banner is a single row.
+std::vector<std::uint8_t> RedBannerPng() {
+ return {
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48,
+ 0x44, 0x52, 0x00, 0x00, 0x02, 0xbc, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00,
+ 0x00, 0xb3, 0x13, 0xea, 0x96, 0x00, 0x00, 0x00, 0x1b, 0x49, 0x44, 0x41, 0x54, 0x78,
+ 0xda, 0x63, 0xf8, 0xcf, 0xc0, 0x30, 0x8a, 0x46, 0xd1, 0x28, 0x1a, 0x45, 0xa3, 0x68,
+ 0x14, 0x8d, 0xa2, 0x51, 0x44, 0x10, 0x01, 0x00, 0xa6, 0xd3, 0xb9, 0x63, 0x4a, 0x81,
+ 0x15, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
+ };
+}
+
void DecodeAllocatorRejectsBudgetExhaustion() {
assert(!artwork_internal::DecodeArtworkWithAllocationBudget(SmallPng(), 1));
assert(artwork_internal::DecodeArtworkWithAllocationBudget(
@@ -267,6 +282,18 @@ void MissingCoverFallsBackToFramedHeader() {
// The 2x1 banner is framed into a 600x900 portrait card.
assert(image && image->width == 600 && image->height == 900);
assert(image->rgba.size() == 600u * 900u * 4u);
+ // The banner fills the middle third undimmed; the margins fade to black at the edges.
+ const auto banner = DecodeArtwork(SmallPng());
+ assert(banner);
+ const auto pixel = [](const ArtworkImage& picture, std::uint32_t x, std::uint32_t y) {
+ const auto* p = &picture.rgba[(y * picture.width + x) * 4];
+ return std::vector<std::uint8_t>{p[0], p[1], p[2], p[3]};
+ };
+ auto opaque = pixel(*banner, 0, 0);
+ opaque[3] = 255;
+ assert(pixel(*image, 0, 450) == opaque);
+ assert((pixel(*image, 0, 0) == std::vector<std::uint8_t>{0, 0, 0, 255}));
+ assert((pixel(*image, 0, 899) == std::vector<std::uint8_t>{0, 0, 0, 255}));
assert(cdn.asked("/library_600x900.jpg") && cdn.asked("/header.jpg"));
assert(folder.Read("42_600x900.jpg") == SmallPng());
@@ -277,6 +304,47 @@ void MissingCoverFallsBackToFramedHeader() {
assert(cached && cached->width == 600 && cached->height == 900 && cdn.urls.empty());
}
+void VeryWideBannerKeepsARow() {
+ CacheFolder folder;
+ FakeCdn cdn;
+ cdn.answer = [](const std::string& url) {
+ return EndsWith(url, "/header.jpg") ? Status(200, RedBannerPng()) : Status(404);
+ };
+ HttpArtworkBackend backend(cdn.perform(), folder.art.string());
+ std::atomic<bool> cancel{false};
+ const auto image = backend.Load({1, SteamId(42), kCover}, cancel);
+ assert(image && image->width == 600 && image->height == 900);
+ // One row in the middle is the banner itself, not a dimmed margin.
+ bool banner_row = false;
+ for (std::uint32_t y = 0; y < 900; ++y) {
+ const auto* p = &image->rgba[(y * 600u) * 4];
+ banner_row = banner_row || (p[0] == 255 && p[1] == 0 && p[2] == 0);
+ }
+ assert(banner_row);
+}
+
+void SaveIsSkippedWhileTheEffectiveUidIsHeld() {
+ CacheFolder folder;
+ FakeCdn cdn;
+ cdn.answer = [](const std::string&) { return Status(200, SmallJpeg()); };
+ HttpArtworkBackend backend(cdn.perform(), folder.art.string());
+ std::atomic<bool> cancel{false};
+ std::promise<void> held, done;
+ std::thread holder([&] {
+ const auto uid = playden::ps5::HoldEffectiveUid();
+ held.set_value();
+ done.get_future().wait();
+ });
+ held.get_future().wait();
+ // The cover still shows; it just isn't cached, and no folder is created.
+ assert(backend.Load({1, SteamId(42), kCover}, cancel));
+ assert(!std::filesystem::exists(folder.art));
+ done.set_value();
+ holder.join();
+ assert(backend.Load({1, SteamId(42), kCover}, cancel));
+ assert(folder.Read("42_600x900.jpg") == SmallJpeg());
+}
+
void HeroHasNoHeaderFallback() {
CacheFolder folder;
FakeCdn cdn;
@@ -359,6 +427,8 @@ int main(int argc, char** argv) {
InvalidHttpIdentityIsRejectedWithoutTransport();
FreshCacheIsUsedWithoutTheNetwork();
MissingCoverFallsBackToFramedHeader();
+ VeryWideBannerKeepsARow();
+ SaveIsSkippedWhileTheEffectiveUidIsHeld();
HeroHasNoHeaderFallback();
OtherFailuresKeepTheCachedCover();
BadDownloadKeepsTheCachedCover();