From a48c74543884013a89e7004c91b96939b3ec6021 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sat, 5 Dec 2026 16:13:57 +0300 Subject: [PATCH] Archive: run the staging implementation on Windows Add a POSIX compatibility layer for the Windows build of StagedArchive: directory descriptors are synthetic path handles, file IO uses the CRT with UTF-8 to wide path conversion, fstatat/fstat/mkdirat/unlinkat/fsync are emulated, and publication uses MoveFileExW without replace. The isolated worker keeps its POSIX rlimit path (guarded) and RSS reporting returns no value on Windows. Allowlist Archive for the Windows target. Cross-built with x86_64-w64-mingw32 GCC 16.2: Void_Archive and VoidTests.exe link with no errors. Linux Archive suite: 10/10 pass. Refs #65 --- cmake/Targets.cmake | 1 + docs/targets.md | 13 +- src/Archive/StagedArchive.cpp | 406 +++++++++++++++++- tests/standalone/Archive/ArchiveHardening.cpp | 4 + 4 files changed, 417 insertions(+), 7 deletions(-) diff --git a/cmake/Targets.cmake b/cmake/Targets.cmake index 85d5af1..e67b695 100644 --- a/cmake/Targets.cmake +++ b/cmake/Targets.cmake @@ -45,6 +45,7 @@ set(VOID_WASM_MODULES # native backend is Linux-only (EpollBackend), so only the mock backend works. set(VOID_WINDOWS_MODULES Algorithms + Archive Async Binary Configuration diff --git a/docs/targets.md b/docs/targets.md index d61f6d6..3f037f7 100644 --- a/docs/targets.md +++ b/docs/targets.md @@ -108,7 +108,7 @@ recognized and configured like Linux; musl-specific behaviour (subprocess | CMake / generator | CMake 4.5.0, Ninja 1.13.2 | | Language | C++26, reflection `yes` | | Runtime | Wine 11.0 (`wine64`), no native Windows runner available | -| Modules | `Algorithms Async Binary Configuration Containers Core Crypto Data Discovery Fsm Functional Log LogState Math Memory Messaging Observability ObservabilityHooks Scheduler Service System Testing TestingMock Text Time Unicode` | +| Modules | `Algorithms Archive Async Binary Configuration Containers Core Crypto Data Discovery Fsm Functional Log LogState Math Memory Messaging Observability ObservabilityHooks Scheduler Service System Testing TestingMock Text Time Unicode` | | Observed | configure, compile, `cmake --install`, consume (`TargetProfile.exe` linked); `VoidTests.exe` under Wine: **1364 tests, 1360 passed, 4 skipped, 0 failed** | Notes from the runtime run: @@ -117,10 +117,13 @@ Notes from the runtime run: is Linux-only (`EpollBackend`); on Windows `Driver::create` returns `IoErrorCode::unsupported` unless the mock backend is selected. A Windows IO backend (IOCP) is a separate task. -- `Configuration`, `Crypto` and `System` are allowlisted and cross-compile; - `System` links `winmm` for the Win32 audio backend. `Archive` is **not** - allowlisted yet: its staging implementation (`StagedArchive.cpp`) is - POSIX-only (see Void/Library#65). +- `Configuration`, `Crypto`, `System` and `Archive` are allowlisted and + cross-compile; `System` links `winmm` for the Win32 audio backend. Archive's + staging implementation keeps its POSIX logic and runs on Windows through a + compatibility layer (`StagedArchive.cpp`): directory descriptors are + synthetic path handles, file IO uses the CRT, and publication uses + `MoveFileExW` without replace. The isolated archive worker has no rlimits + there, and runtime verification under Wine is still pending. - Tests that only exercise POSIX APIs carry `// @requires-posix` and are skipped on Windows; the POSIX process helper is not built there. - `Observability` links after replacing the namespace-scope diff --git a/src/Archive/StagedArchive.cpp b/src/Archive/StagedArchive.cpp index 5042116..1e84df7 100644 --- a/src/Archive/StagedArchive.cpp +++ b/src/Archive/StagedArchive.cpp @@ -25,7 +25,19 @@ #include #endif -#ifndef _WIN32 +#ifdef _WIN32 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#else #include #include #include @@ -40,6 +52,376 @@ #endif #endif +#ifdef _WIN32 +// ── POSIX compatibility for the staging implementation ────────────────────── +// The staging logic is written against directory file descriptors. Windows has +// no such handles for directories through the CRT, so directory descriptors +// are synthetic ids mapped to their path; file descriptors stay ordinary CRT +// handles. Call sites are unchanged. + +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif +#ifndef O_NOFOLLOW +#define O_NOFOLLOW 0 +#endif +#ifndef O_DIRECTORY +#define O_DIRECTORY 0x20000000 +#endif +#ifndef O_BINARY +#define O_BINARY _O_BINARY +#endif +#ifndef AT_SYMLINK_NOFOLLOW +#define AT_SYMLINK_NOFOLLOW 0x1000 +#endif +#ifndef AT_REMOVEDIR +#define AT_REMOVEDIR 0x0200 +#endif +#ifndef STDERR_FILENO +#define STDERR_FILENO 2 +#endif +#ifndef S_ISLNK +#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) +#endif +#ifndef S_IFLNK +#define S_IFLNK 0xA000 +#endif + + +namespace void_win_posix +{ + inline std::mutex& dir_mutex() noexcept + { + static std::mutex mutex; + return mutex; + } + + inline std::map& dir_handles() noexcept + { + static std::map handles; + return handles; + } + + inline int& next_handle() noexcept + { + static int handle = 0x40000000; + return handle; + } + + inline std::optional dir_path(const int fd) + { + std::lock_guard lock{dir_mutex()}; + const auto it = dir_handles().find(fd); + if (it == dir_handles().end()) { + return std::nullopt; + } + return it->second; + } + + inline std::optional take_dir_path(const int fd) + { + std::lock_guard lock{dir_mutex()}; + const auto it = dir_handles().find(fd); + if (it == dir_handles().end()) { + return std::nullopt; + } + auto path = std::move(it->second); + dir_handles().erase(it); + return path; + } + + inline int register_dir(std::filesystem::path path) + { + std::lock_guard lock{dir_mutex()}; + const int fd = next_handle()++; + dir_handles().emplace(fd, std::move(path)); + return fd; + } + + inline std::filesystem::path from_utf8(const char* text) + { + return std::filesystem::path{ + std::u8string{reinterpret_cast(text)}}; + } + + inline int errno_from(DWORD error) noexcept + { + switch (error) { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + case ERROR_INVALID_NAME: + return ENOENT; + case ERROR_ALREADY_EXISTS: + case ERROR_FILE_EXISTS: + return EEXIST; + case ERROR_ACCESS_DENIED: + case ERROR_SHARING_VIOLATION: + return EACCES; + case ERROR_DISK_FULL: + case ERROR_HANDLE_DISK_FULL: + return ENOSPC; + case ERROR_DIR_NOT_EMPTY: + return ENOTEMPTY; + default: + return EIO; + } + } + + inline bool is_reparse_point(const std::filesystem::path& path) noexcept + { + const DWORD attributes = ::GetFileAttributesW(path.c_str()); + return attributes != INVALID_FILE_ATTRIBUTES && + (attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0; + } + + inline int open_path(const std::filesystem::path& path, + int flags, + const int mode) + { + std::error_code ec; + const bool directory = (flags & O_DIRECTORY) != 0 || + std::filesystem::is_directory(path, ec); + if (directory) { + if (!std::filesystem::is_directory(path, ec)) { + errno = ec ? ENOENT : ENOTDIR; + return -1; + } + if ((flags & O_NOFOLLOW) != 0 && is_reparse_point(path)) { + errno = ELOOP; + return -1; + } + return register_dir(path); + } + if ((flags & O_NOFOLLOW) != 0 && is_reparse_point(path)) { + errno = ELOOP; + return -1; + } + int crt_flags = flags & ~O_DIRECTORY; + crt_flags |= O_BINARY; + const int fd = ::_wopen(path.wstring().c_str(), crt_flags, mode); + if (fd < 0) { + return -1; + } + return fd; + } +} // namespace void_win_posix + +inline int close(const int fd) +{ + if (void_win_posix::take_dir_path(fd).has_value()) { + return 0; + } + return ::_close(fd); +} + +inline ssize_t read(const int fd, void* buffer, const std::size_t count) +{ + return ::_read(fd, buffer, static_cast(count)); +} + +inline ssize_t write(const int fd, const void* buffer, const std::size_t count) +{ + return ::_write(fd, buffer, static_cast(count)); +} + +inline int fsync(const int fd) +{ + if (void_win_posix::dir_path(fd).has_value()) { + return 0; // Directories cannot be flushed on Windows. + } + return ::_commit(fd); +} + +inline int open(const char* path, const int flags, ...) +{ + int mode = 0; + if ((flags & O_CREAT) != 0) { + va_list args; + va_start(args, flags); + mode = va_arg(args, int); + va_end(args); + } + return void_win_posix::open_path(void_win_posix::from_utf8(path), flags, mode); +} + +// std::filesystem::path::c_str() is wide on Windows. +inline int open(const wchar_t* path, const int flags, ...) +{ + int mode = 0; + if ((flags & O_CREAT) != 0) { + va_list args; + va_start(args, flags); + mode = va_arg(args, int); + va_end(args); + } + return void_win_posix::open_path(std::filesystem::path{path}, flags, mode); +} + +inline int openat(const int dir_fd, const char* name, const int flags, ...) +{ + int mode = 0; + if ((flags & O_CREAT) != 0) { + va_list args; + va_start(args, flags); + mode = va_arg(args, int); + va_end(args); + } + const std::filesystem::path relative = void_win_posix::from_utf8(name); + if (relative.is_absolute()) { + return void_win_posix::open_path(relative, flags, mode); + } + const auto base = void_win_posix::dir_path(dir_fd); + if (!base.has_value()) { + errno = EBADF; + return -1; + } + const std::filesystem::path path = + relative == "." ? *base : (*base / relative); + return void_win_posix::open_path(path, flags, mode); +} + +inline int mkdirat(const int dir_fd, const char* name, mode_t) +{ + const auto base = void_win_posix::dir_path(dir_fd); + if (!base.has_value()) { + errno = EBADF; + return -1; + } + std::error_code ec; + if (std::filesystem::create_directory(*base / void_win_posix::from_utf8(name), ec)) { + return 0; + } + if (!ec) { + errno = EEXIST; + } else if (ec == std::errc::file_exists) { + errno = EEXIST; + } else { + errno = void_win_posix::errno_from( + static_cast(ec.value())); // best effort + } + return -1; +} + +inline int unlinkat(const int dir_fd, const char* name, const int flags) +{ + const auto base = void_win_posix::dir_path(dir_fd); + if (!base.has_value()) { + errno = EBADF; + return -1; + } + const std::filesystem::path path = *base / void_win_posix::from_utf8(name); + std::error_code ec; + if ((flags & AT_REMOVEDIR) != 0) { + std::filesystem::remove(path, ec); + } else { + std::filesystem::remove(path, ec); + } + if (ec) { + errno = ec == std::errc::no_such_file_or_directory ? ENOENT + : ec == std::errc::directory_not_empty ? ENOTEMPTY + : EACCES; + return -1; + } + return 0; +} + +inline int fstatat(const int dir_fd, + const char* name, + struct stat* status, + const int flags) +{ + const auto base = void_win_posix::dir_path(dir_fd); + if (!base.has_value()) { + errno = EBADF; + return -1; + } + const std::filesystem::path path = *base / void_win_posix::from_utf8(name); + std::error_code ec; + auto sym = std::filesystem::symlink_status(path, ec); + if (ec) { + errno = ENOENT; + return -1; + } + std::memset(status, 0, sizeof(*status)); + status->st_uid = 0; + status->st_gid = 0; + if (std::filesystem::is_symlink(sym) && (flags & AT_SYMLINK_NOFOLLOW) != 0) { + status->st_mode = S_IFLNK | 0777; + return 0; + } + if (std::filesystem::is_directory(sym)) { + status->st_mode = S_IFDIR | 0755; + } else if (std::filesystem::is_regular_file(sym)) { + status->st_mode = S_IFREG | 0644; + status->st_size = static_cast(std::filesystem::file_size(path, ec)); + std::error_code link_ec; + status->st_nlink = std::filesystem::hard_link_count(path, link_ec); + } else { + status->st_mode = S_IFREG | 0644; + } + return 0; +} + +inline int fstat(const int fd, struct stat* status) +{ + std::memset(status, 0, sizeof(*status)); + if (void_win_posix::dir_path(fd).has_value()) { + status->st_mode = S_IFDIR | 0755; + status->st_nlink = 1; + return 0; + } + struct _stat64 info {}; + if (::_fstat64(fd, &info) != 0) { + return -1; + } + status->st_mode = static_cast(info.st_mode); + status->st_nlink = static_cast(info.st_nlink); + status->st_size = static_cast(info.st_size); + return 0; +} + +inline DIR* fdopendir(const int fd) +{ + const auto path = void_win_posix::take_dir_path(fd); + if (!path.has_value()) { + errno = EBADF; + return nullptr; + } + return ::opendir(path->string().c_str()); +} + +inline int getpid() +{ + return ::_getpid(); +} + +inline int geteuid() +{ + return 0; +} + +inline int rename_noreplace_at(const int old_dir_fd, + const char* old_name, + const int new_dir_fd, + const char* new_name) +{ + const auto old_base = void_win_posix::dir_path(old_dir_fd); + const auto new_base = void_win_posix::dir_path(new_dir_fd); + if (!old_base.has_value() || !new_base.has_value()) { + errno = EBADF; + return -1; + } + const std::filesystem::path from = *old_base / void_win_posix::from_utf8(old_name); + const std::filesystem::path to = *new_base / void_win_posix::from_utf8(new_name); + // No MOVEFILE_REPLACE_EXISTING: never overwrites an existing destination. + if (::MoveFileExW(from.c_str(), to.c_str(), MOVEFILE_WRITE_THROUGH) != 0) { + return 0; + } + errno = void_win_posix::errno_from(::GetLastError()); + return -1; +} +#endif // _WIN32 + namespace Void::Archive { namespace @@ -113,7 +495,9 @@ namespace Void::Archive case EPERM: return ArchiveErrorCode::permission_denied; case ENOSPC: +#ifdef EDQUOT case EDQUOT: +#endif return ArchiveErrorCode::no_space; case EEXIST: case ENOTEMPTY: @@ -596,6 +980,10 @@ namespace Void::Archive [[nodiscard]] std::optional current_rss_bytes() noexcept { +#ifdef _WIN32 + // RSS is only enforced by the POSIX isolated worker. + return std::nullopt; +#else std::ifstream statm{"/proc/self/statm"}; if (!statm.is_open()) { return std::nullopt; @@ -610,6 +998,7 @@ namespace Void::Archive return std::nullopt; } return resident_pages * static_cast(page_size); +#endif } [[nodiscard]] std::string @@ -1964,6 +2353,11 @@ namespace Void::Archive m_impl->parent_fd.get(), target_name.c_str(), static_cast(RENAME_NOREPLACE))); err = errno; +#elif defined(_WIN32) + rc = ::rename_noreplace_at(m_impl->stage_fd.get(), "tree", + m_impl->parent_fd.get(), + target_name.c_str()); + err = errno; #else rc = -1; err = ENOSYS; @@ -2380,7 +2774,12 @@ namespace Void::Archive } (void)dup_fd.release(); +#ifdef _WIN32 + // Windows has no POSIX uid; fstatat() reports 0 for every entry. + const unsigned int my_uid = 0; +#else const uid_t my_uid = ::geteuid(); +#endif const auto now_s = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) @@ -3045,7 +3444,9 @@ namespace Void::Archive } // Apply safe OS resource limits (guard RLIMIT_AS under ASan; RSS is - // enforced via /proc/self/statm directly). + // enforced via /proc/self/statm directly). Windows has no rlimits; the + // worker is not used there (see extract_staged_isolated). +#ifndef _WIN32 { const rlim_t cpu_secs = static_cast(std::max( @@ -3063,6 +3464,7 @@ namespace Void::Archive struct rlimit rl_nofile{128, 128}; (void)::setrlimit(RLIMIT_NOFILE, &rl_nofile); } +#endif opts.deadline = Void::Time::SystemMonotonicClock::instance().now() + std::chrono::milliseconds{timeout_ms}; diff --git a/tests/standalone/Archive/ArchiveHardening.cpp b/tests/standalone/Archive/ArchiveHardening.cpp index 878f646..e4e105a 100644 --- a/tests/standalone/Archive/ArchiveHardening.cpp +++ b/tests/standalone/Archive/ArchiveHardening.cpp @@ -367,10 +367,12 @@ Void::Testing::Test archive_create_formats_builtin_ustar_pax_and_gzip() Void::Testing::expect_eq(read_file_str(tmp / "unpacked_tar/bin/tool"), std::string{"tool-bytes"}); +#ifndef _WIN32 struct stat st_tool{}; Void::Testing::require(::stat((tmp / "unpacked_tar/bin/tool").c_str(), &st_tool) == 0); Void::Testing::expect_eq(st_tool.st_mode & 0777, mode_t{0755}); +#endif // Staged extraction over built-in tar also succeeds (exercising // LibArchive interop when LibArchive is linked, or built-in Reader when @@ -487,6 +489,7 @@ Void::Testing::Test archive_staged_extraction_atomic_publish_unknown_and_cleanup Void::Testing::expect_eq(read_file_str(parent_dir / "v1/etc/config.txt"), std::string{"key=val"}); +#ifndef _WIN32 struct stat st_exec{}; struct stat st_reg{}; Void::Testing::require(::stat((parent_dir / "v1/bin/app").c_str(), &st_exec) == @@ -495,6 +498,7 @@ Void::Testing::Test archive_staged_extraction_atomic_publish_unknown_and_cleanup ::stat((parent_dir / "v1/etc/config.txt").c_str(), &st_reg) == 0); Void::Testing::expect_eq(st_exec.st_mode & 0777, mode_t{0755}); Void::Testing::expect_eq(st_reg.st_mode & 0777, mode_t{0644}); +#endif // 2. Never overwrites an existing child directory. auto staged2 = extract_staged(archive_path, parent_dir);