// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® #include #include #include import std; import Crafter.Build; namespace fs = std::filesystem; using namespace Crafter; namespace { std::int32_t Failures = 0; void Check(bool cond, std::string_view msg) { if (!cond) { std::println(std::cerr, "FAIL: {}", msg); ++Failures; } } // The process's address space now, in bytes: the first field of // /proc/self/statm, in pages. std::uint64_t AddressSpace() { std::ifstream statm("/proc/self/statm"); std::uint64_t pages = 0; statm >> pages; return pages * static_cast(sysconf(_SC_PAGESIZE)); } } // A share entry can be a program's data, gigabytes of it, and the writer // compares each with its copy in files/share on every build, to leave an // unchanged one alone. It compared them by reading both files whole, so a // build with nothing to copy held twice the largest entry in memory, and // failed where that did not fit, every time after the first. A files/ with // a 32 MiB share entry is written, then written again with nothing changed // in a child process whose address space may grow by no more than 16 MiB: // that rewrite has to succeed. A change in the entry's last byte, in the // short block a comparison reads last, is still copied. The writer is // called directly, with a stand-in llvm-readelf: a whole Build() would start // compiles and threads, and the real llvm-readelf a write starts for the // libraries, which the same limit would hold as well. int main() { const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-share-large"; std::error_code ec; fs::remove_all(scratch, ec); fs::create_directories(scratch / "app" / "files" / "bin"); fs::create_directories(scratch / "build"); // The program, and a stand-in for llvm-readelf that says it needs // nothing: the writer reads the program's libraries on every write, and // the real llvm-readelf, started under the child's limit below, does not // fit in it. The libraries are not what is measured here. std::ofstream(scratch / "app" / "files" / "bin" / "large") << "a program\n"; const fs::path readelf = scratch / "readelf"; std::ofstream(readelf) << "#!/bin/sh\necho 'NeededLibraries ['\necho ']'\n"; fs::permissions(readelf, fs::perms::owner_all); // Not a whole number of blocks, so the last one is short. constexpr std::uintmax_t Size = (32 * 1024 * 1024) + 3; const fs::path data = scratch / "data.bin"; std::ofstream(data, std::ios::binary).close(); fs::resize_file(data, Size); const Toolchain& tc = GetToolchain(); FlatpakFiles d; d.root = scratch / "app"; d.record = scratch / "build" / "flatpak-record"; d.appId = "net.example.ShareLarge"; d.command = "large"; d.share = { {data, "large/data.bin"} }; d.clangxx = tc.clangxx; d.readelf = readelf; d.programEntries = {"large"}; const std::string first = WriteFlatpakFiles(d); const fs::path copy = d.root / "files" / "share" / "large" / "data.bin"; Check(first.empty() && fs::file_size(copy, ec) == Size, std::format("the share entry is copied: {}", first)); // The rewrite, in a child under the limit: its exit status is whether // the writer finished. No thread runs here to be lost in the fork, and // nothing buffered is written twice. std::fflush(nullptr); const pid_t child = fork(); if (child == 0) { rlimit limit{}; getrlimit(RLIMIT_AS, &limit); limit.rlim_cur = std::min(AddressSpace() + (16 * 1024 * 1024), limit.rlim_max); if (setrlimit(RLIMIT_AS, &limit) != 0) _exit(2); const std::string again = WriteFlatpakFiles(d); if (!again.empty()) std::println(std::cerr, "the rewrite failed: {}", again); _exit(again.empty() ? 0 : 1); } std::int32_t status = 0; if (child > 0) waitpid(child, &status, 0); Check(child > 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0, std::format("rewriting it with nothing changed fits in 16 MiB more than the process has (status {})", status)); // The comparison still sees a change in the last, short block. { std::fstream out(data, std::ios::binary | std::ios::in | std::ios::out); out.seekp(static_cast(Size - 1)); out.put('x'); } const std::string changed = WriteFlatpakFiles(d); std::ifstream in(copy, std::ios::binary); in.seekg(static_cast(Size - 1)); Check(changed.empty() && in.get() == 'x', std::format("a change in the entry's last byte is copied: {}", changed)); in.close(); fs::remove_all(scratch, ec); if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }