Merge pull request #9161 from yabanana/feat/kernel-sys-path-operations

feat(libkernel_sys): implement statfs symlinkat and futimesat
This commit is contained in:
Dean authored and GitHub committed 2026-11-09 23:56:29 +13:00
commit bbaa4bf253
10 files changed
+401 -38

No files matched your search

+19
View File
@@ -323,6 +323,25 @@ include(prx/libSceLibreSsl/Cmac.cmake)
include(prx/libSceLibreSsl/ProtocolSelection.cmake)
if(BUILD_TESTING)
add_test_executable(guest_kernel_sys_paths_tests tests/GuestKernelSysPaths.cpp
nid/src/NidCompute.cpp nid/src/Sha1.cpp)
target_include_directories(guest_kernel_sys_paths_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/nid/include)
target_link_libraries(guest_kernel_sys_paths_tests PRIVATE ${CMAKE_DL_LIBS})
add_custom_target(guest_kernel_sys_paths_libraries DEPENDS
${PATCHED_DIR}/libkernel_sys.prx ${PATCHED_DIR}/libkernel.prx ${LIBC_PATCHED})
add_dependencies(guest_kernel_sys_paths_tests guest_kernel_sys_paths_libraries)
configure_windows_unwind(guest_kernel_sys_paths_tests)
if(MINGW)
target_compile_options(guest_kernel_sys_paths_tests PRIVATE -fasynchronous-unwind-tables)
target_link_options(guest_kernel_sys_paths_tests PRIVATE -static -static-libgcc -static-libstdc++)
endif()
foreach(case IN ITEMS statfs symlinkat symlinkat_errors futimesat)
add_test(NAME guest_kernel_sys_paths_${case} COMMAND guest_kernel_sys_paths_tests ${UNPATCHED_DIR} unpatched ${case})
add_test(NAME guest_kernel_sys_paths_${case}_patched COMMAND guest_kernel_sys_paths_tests ${PATCHED_DIR} patched ${case})
set_tests_properties(guest_kernel_sys_paths_${case} guest_kernel_sys_paths_${case}_patched PROPERTIES TIMEOUT 20)
endforeach()
set_tests_properties(guest_kernel_sys_paths_symlinkat guest_kernel_sys_paths_symlinkat_patched PROPERTIES SKIP_RETURN_CODE 77)
add_test_executable(guest_posix_scalar_io_tests tests/GuestPosixScalarIo.cpp)
target_include_directories(guest_posix_scalar_io_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(guest_posix_scalar_io_tests PRIVATE libkernel libc)
@@ -14,6 +14,8 @@
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
struct KernelStatfs;
extern "C" {
int APS5_VABI sceKernelOpen(const char* path, int flags, std::uint16_t mode);
@@ -21,6 +23,8 @@ int APS5_VABI sceKernelClose(int d);
std::int64_t APS5_VABI sceKernelRead(int d, void* buf, std::size_t nbytes);
std::int64_t APS5_VABI sceKernelWrite(int d, const void* buf, std::size_t nbytes);
std::int64_t APS5_VABI sceKernelLseek(int d, std::int64_t offset, int whence);
int PosixStatfs_nid_no_patch(const char* path, KernelStatfs* buf);
int PosixUtimesAt_nid_no_patch(int directory, const char* path, const KernelTimeval* times);
int APS5_VABI sceKernelStat(const char* path, FileStat* sb);
int APS5_VABI sceKernelUnlink(const char* path);
int APS5_VABI sceKernelFcntl(int d, int command, ...);
+72 -20
View File
@@ -627,6 +627,29 @@ static int DeviceFilesystem(KernelStatfs* fs) {
}
#ifdef _WIN32
static int NativeStatfs(const std::filesystem::path& path, KernelStatfs* fs) {
std::wstring root(path.native().size() + 2, L'\0');
if (!::GetVolumePathNameW(path.c_str(), root.data(), static_cast<DWORD>(root.size())))
throw std::runtime_error("_fstatfs: GetVolumePathNameW failed for " + path.string() + ", error=" + std::to_string(::GetLastError()));
DWORD sectorsPerCluster = 0, bytesPerSector = 0, freeClusters = 0, totalClusters = 0;
ULARGE_INTEGER available{}, total{}, totalFree{};
DWORD nameMax = 0, volumeFlags = 0;
if (!::GetDiskFreeSpaceW(root.c_str(), &sectorsPerCluster, &bytesPerSector, &freeClusters, &totalClusters) ||
!::GetDiskFreeSpaceExW(root.c_str(), &available, &total, &totalFree) ||
!::GetVolumeInformationW(root.c_str(), nullptr, 0, nullptr, &nameMax, &volumeFlags, nullptr, 0))
throw std::runtime_error("_fstatfs: volume query failed for " + path.string() + ", error=" + std::to_string(::GetLastError()));
const std::uint64_t cluster = static_cast<std::uint64_t>(sectorsPerCluster) * bytesPerSector;
if (cluster == 0) throw std::runtime_error("_fstatfs: volume of " + path.string() + " reports no cluster size");
fs->f_flags = (volumeFlags & FILE_READ_ONLY_VOLUME) ? KERNEL_MNT_RDONLY : 0;
fs->f_bsize = cluster;
fs->f_iosize = cluster;
fs->f_blocks = total.QuadPart / cluster;
fs->f_bfree = totalFree.QuadPart / cluster;
fs->f_bavail = static_cast<std::int64_t>(available.QuadPart / cluster);
fs->f_namemax = nameMax;
return 0;
}
static int NativeFstatfs(int descriptor, KernelStatfs* fs) {
if (!File::IsDirectoryDescriptor(descriptor)) {
const auto handle = reinterpret_cast<HANDLE>(::_get_osfhandle(descriptor));
@@ -639,26 +662,7 @@ static int NativeFstatfs(int descriptor, KernelStatfs* fs) {
}
const auto path = File::WindowsDescriptorPath_nid_no_patch(descriptor);
if (!path) throw std::runtime_error("_fstatfs: no path for descriptor " + std::to_string(descriptor) + ", errno=" + std::to_string(errno));
std::wstring root(path->native().size() + 2, L'\0');
if (!::GetVolumePathNameW(path->c_str(), root.data(), static_cast<DWORD>(root.size())))
throw std::runtime_error("_fstatfs: GetVolumePathNameW failed for " + path->string() + ", error=" + std::to_string(::GetLastError()));
DWORD sectorsPerCluster = 0, bytesPerSector = 0, freeClusters = 0, totalClusters = 0;
ULARGE_INTEGER available{}, total{}, totalFree{};
DWORD nameMax = 0, volumeFlags = 0;
if (!::GetDiskFreeSpaceW(root.c_str(), &sectorsPerCluster, &bytesPerSector, &freeClusters, &totalClusters) ||
!::GetDiskFreeSpaceExW(root.c_str(), &available, &total, &totalFree) ||
!::GetVolumeInformationW(root.c_str(), nullptr, 0, nullptr, &nameMax, &volumeFlags, nullptr, 0))
throw std::runtime_error("_fstatfs: volume query failed for " + path->string() + ", error=" + std::to_string(::GetLastError()));
const std::uint64_t cluster = static_cast<std::uint64_t>(sectorsPerCluster) * bytesPerSector;
if (cluster == 0) throw std::runtime_error("_fstatfs: volume of " + path->string() + " reports no cluster size");
fs->f_flags = (volumeFlags & FILE_READ_ONLY_VOLUME) ? KERNEL_MNT_RDONLY : 0;
fs->f_bsize = cluster;
fs->f_iosize = cluster;
fs->f_blocks = total.QuadPart / cluster;
fs->f_bfree = totalFree.QuadPart / cluster;
fs->f_bavail = static_cast<std::int64_t>(available.QuadPart / cluster);
fs->f_namemax = nameMax;
return 0;
return NativeStatfs(*path, fs);
}
static bool NativeIsPipe(int descriptor) {
@@ -686,6 +690,8 @@ static bool NativeIsPipe(int descriptor) {
#endif
}
static int FillStatfs(const struct statvfs& host, KernelStatfs* fs);
static int NativeFstatfs(int descriptor, KernelStatfs* fs) {
struct stat file{};
if (::fstat(descriptor, &file) != 0) return SceErrorFromErrno(errno) & 0xffff;
@@ -699,6 +705,10 @@ static int NativeFstatfs(int descriptor, KernelStatfs* fs) {
if (S_ISCHR(file.st_mode)) return DeviceFilesystem(fs);
struct statvfs host{};
if (::fstatvfs(descriptor, &host) != 0) return SceErrorFromErrno(errno) & 0xffff;
return FillStatfs(host, fs);
}
static int FillStatfs(const struct statvfs& host, KernelStatfs* fs) {
if (host.f_flag & ST_RDONLY) fs->f_flags |= KERNEL_MNT_RDONLY;
if (host.f_flag & ST_NOSUID) fs->f_flags |= KERNEL_MNT_NOSUID;
#ifdef __APPLE__
@@ -717,6 +727,14 @@ static int NativeFstatfs(int descriptor, KernelStatfs* fs) {
fs->f_namemax = static_cast<std::uint32_t>(host.f_namemax);
return 0;
}
static int NativeStatfs(const std::filesystem::path& path, KernelStatfs* fs) {
struct statvfs host{};
if (::statvfs(path.c_str(), &host) != 0) return SceErrorFromErrno(errno == ENOENT ? NotFoundError_nid_no_patch(path) : errno) & 0xffff;
return FillStatfs(host, fs);
}
#endif
extern "C" int APS5_VABI pipe_nid_postfix(int* descriptors) {
@@ -1098,6 +1116,26 @@ int APS5_VABI _fstatfs_nid_postfix(int d, KernelStatfs* buf) {
return 0;
}
int PosixStatfs_nid_no_patch(const char* path, KernelStatfs* buf) {
std::string copy;
if (const int error = PathError(path, copy)) return PosixFailure(error);
path = copy.c_str();
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return PosixFailure(error);
AppArchiveRefuseOnly_nid_no_patch(native, path, "statfs");
#ifdef _WIN32
std::error_code error;
const auto status = std::filesystem::status(native, error);
if (const int failure = PathLookupFailure(error)) return PosixResult(failure);
if (!std::filesystem::exists(status)) return PosixFailure(NotFoundError_nid_no_patch(native));
#endif
KernelStatfs result{};
result.f_version = KERNEL_STATFS_VERSION;
if (const int error = NativeStatfs(native, &result)) return PosixFailure(error);
return GuestCopyOut_nid_no_patch(buf, &result, sizeof(result)) ? 0 : PosixFailure(GUEST_EFAULT);
}
int64_t APS5_VABI fstat_nid_disambig1_nid_postfix(int d, FileStat* sb) {
if (sb == nullptr) {
FileStat ignored{};
@@ -2047,6 +2085,20 @@ int APS5_VABI utimes_nid_postfix(const char* path, const KernelTimeval* times) {
return PosixResult(sceKernelUtimes_nid_postfix(path, times));
}
int PosixUtimesAt_nid_no_patch(int directory, const char* path, const KernelTimeval* times) {
if (InvalidTimes(times)) return PosixFailure(GUEST_EINVAL);
std::string copy;
if (const int error = PathError(path, copy)) return PosixFailure(error);
path = copy.c_str();
if (directory == -100 || *path == '/') return utimes_nid_postfix(path, times);
int error = 0;
const char* base = GetDirectoryDescriptorPath_nid_no_patch(directory, &error);
if (!base) return PosixFailure(error);
const auto native = std::filesystem::path(base) / std::filesystem::path(reinterpret_cast<const char8_t*>(path));
if (NativeUtimes(native, times) != 0) return PosixFailure(SceErrorFromErrno(errno == ENOENT ? NotFoundError_nid_no_patch(native) : errno) & 0xffff);
return 0;
}
int APS5_VABI lutimes_nid_postfix(const char* path, const KernelTimeval* times) {
KernelTimeval local[2];
if (!CopyInTimes(times, local)) return PosixFailure(GUEST_EFAULT);
@@ -9,6 +9,7 @@
#include "prx/libc/include/Api.hpp"
#include "prx/libc/include/GuestAllocations.hpp"
#include "SceTypes.hpp"
#include "prx/libkernel/File/include/File.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libkernel/KernelErrors.hpp"
#include "prx/libkernel/Apr/include/AprCommandBuffer.hpp"
@@ -103,9 +104,8 @@ int APS5_VABI dynlib_get_obj_member_nid_postfix(void) {
return 0;
}
int APS5_VABI futimesat_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI futimesat_nid_postfix(int directory, const char* path, const KernelTimeval* times) {
return PosixUtimesAt_nid_no_patch(directory, path, times);
}
int APS5_VABI get_module_info_list_nid_postfix(void) {
@@ -2092,9 +2092,8 @@ int APS5_VABI sigreturn_nid_postfix(const UserUcontext* context) {
return SigreturnContext_nid_no_patch(context);
}
int APS5_VABI statfs_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI statfs_nid_postfix(const char* path, KernelStatfs* buf) {
return PosixStatfs_nid_no_patch(path, buf);
}
int APS5_VABI swapon_nid_postfix(void) {
@@ -2102,11 +2101,6 @@ int APS5_VABI swapon_nid_postfix(void) {
return 0;
}
int APS5_VABI symlinkat_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sysc_e00_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
+18 -2
View File
@@ -894,7 +894,7 @@ int APS5_VABI kldsym_nid_postfix(int fileid, int cmd, void* data) {
throw std::runtime_error("kldsym: looking up the symbol " + name + " in every kernel module is not supported");
}
int APS5_VABI symlink_nid_postfix(const char* target, const char* created) {
static int CreateSymlinkAt(const char* target, int dirfd, const char* created) {
std::string targetCopy, createdCopy;
if (const int error = CopyPath(target, targetCopy)) return static_cast<int>(Fail(error));
if (const int error = CopyPath(created, createdCopy)) return static_cast<int>(Fail(error));
@@ -904,7 +904,15 @@ int APS5_VABI symlink_nid_postfix(const char* target, const char* created) {
return -1;
}
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(created, native)) return static_cast<int>(Fail(error));
if (std::strlen(created) >= GuestPathMax || HasLongComponent(created)) return static_cast<int>(Fail(GuestEnametoolong));
if (dirfd == GuestAtFdcwd || *created == '/') {
if (const int error = ResolvePathError_nid_no_patch(created, native)) return static_cast<int>(Fail(error));
} else {
int error = 0;
const char* base = GetDirectoryDescriptorPath_nid_no_patch(dirfd, &error);
if (!base) return static_cast<int>(Fail(error));
native = std::filesystem::path(base) / std::filesystem::path(reinterpret_cast<const char8_t*>(created));
}
std::error_code error;
#ifdef _WIN32
if (GetFileAttributesW(native.wstring().c_str()) != INVALID_FILE_ATTRIBUTES) return static_cast<int>(Fail(GuestEexist));
@@ -939,6 +947,14 @@ int APS5_VABI symlink_nid_postfix(const char* target, const char* created) {
return 0;
}
int APS5_VABI symlink_nid_postfix(const char* target, const char* created) {
return CreateSymlinkAt(target, GuestAtFdcwd, created);
}
int APS5_VABI symlinkat_nid_postfix(const char* target, int directory, const char* created) {
return CreateSymlinkAt(target, directory, created);
}
int APS5_VABI openat_nid_postfix(int fd, const char* path, int flags, ...) {
int mode = 0;
if (flags & SCE_KERNEL_O_CREAT) {
+3 -3
View File
@@ -101,7 +101,7 @@ int APS5_VABI blockpool_map_nid_postfix(void);
int APS5_VABI blockpool_open_nid_postfix(void);
int APS5_VABI blockpool_unmap_nid_postfix(void);
int APS5_VABI dynlib_get_obj_member_nid_postfix(void);
int APS5_VABI futimesat_nid_postfix(void);
int APS5_VABI futimesat_nid_postfix(int directory, const char* path, const KernelTimeval* times);
int APS5_VABI get_authinfo_nid_postfix(int pid, KernelAuthInfo* info);
int APS5_VABI get_module_info_list_nid_postfix(void);
int APS5_VABI get_page_table_stats_nid_postfix(void);
@@ -528,9 +528,9 @@ int APS5_VABI set_phys_fmem_limit_nid_postfix(void);
int APS5_VABI setlogin_nid_postfix(const char* name);
int APS5_VABI sigqueue_nid_postfix(void);
int APS5_VABI sigreturn_nid_postfix(const UserUcontext* context);
int APS5_VABI statfs_nid_postfix(void);
int APS5_VABI statfs_nid_postfix(const char* path, KernelStatfs* buf);
int APS5_VABI swapon_nid_postfix(void);
int APS5_VABI symlinkat_nid_postfix(void);
int APS5_VABI symlinkat_nid_postfix(const char* target, int directory, const char* created);
int APS5_VABI sysc_e00_nid_postfix(void);
int APS5_VABI sysc_m00_nid_postfix(void);
int APS5_VABI sysc_s00_nid_postfix(void);
+235
View File
@@ -0,0 +1,235 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libkernel/File/include/FileFlags.hpp"
#include <nid/NidCompute.hpp>
#include "SceTypes.hpp"
#include "prx/libkernel/File/include/KernelStatfs.hpp"
#include <span>
#include <array>
#include <cstddef>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <limits>
#include <source_location>
#include <string>
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <winioctl.h>
#else
#include <dlfcn.h>
#include <sys/mman.h>
#include <unistd.h>
#endif
static void Require(bool value, std::source_location location = std::source_location::current()) {
if (!value) {
std::fprintf(stderr, "Kernel sys API check failed at line %u\n", location.line());
std::abort();
}
}
static void* LoadLibraryFile(const std::filesystem::path& path) {
#ifdef _WIN32
auto* module = reinterpret_cast<void*>(LoadLibraryExW(path.c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH));
#else
auto* module = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
if (!module) std::fprintf(stderr, "%s\n", dlerror());
#endif
Require(module != nullptr);
#ifdef _WIN32
std::wstring loaded(32768, L'\0');
const auto length = GetModuleFileNameW(static_cast<HMODULE>(module), loaded.data(), static_cast<DWORD>(loaded.size()));
Require(length != 0 && length < loaded.size());
loaded.resize(length);
Require(std::filesystem::equivalent(path, std::filesystem::path(loaded)));
#endif
return module;
}
template <typename Function>
static Function LoadFunction(void* module, const char* name, bool patched, const char* unpatched = nullptr) {
const auto symbol = patched ? Nid::ComputeNid(name, "") : unpatched ? std::string(unpatched) : std::string(name) + "_nid_postfix";
#ifdef _WIN32
const auto function = reinterpret_cast<Function>(reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(module), symbol.c_str())));
#else
const auto function = reinterpret_cast<Function>(dlsym(module, symbol.c_str()));
#endif
if (!function) std::fprintf(stderr, "Missing symbol %s\n", symbol.c_str());
Require(function != nullptr);
return function;
}
int main(int argc, char** argv) {
Require(argc == 4);
const auto directory = std::filesystem::absolute(argv[1]);
const bool patched = std::strcmp(argv[2], "patched") == 0;
const std::string test = argv[3];
auto* sys = LoadLibraryFile(directory / "libkernel_sys.prx");
auto* kernel = LoadLibraryFile(directory / "libkernel.prx");
auto* libc = LoadLibraryFile(directory / "libc.prx");
const auto error = LoadFunction<int* (APS5_VABI*)()>(libc, "__error", patched);
const auto open = LoadFunction<int (APS5_VABI*)(const char*, int, int)>(kernel, "open", patched);
const auto close = LoadFunction<int (APS5_VABI*)(int)>(kernel, "close", patched);
const auto stat = LoadFunction<std::int64_t (APS5_VABI*)(const char*, FileStat*)>(kernel, "stat", patched);
const auto root = std::filesystem::path("anyps5-kernel-sys-paths-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
Require(std::filesystem::create_directory(root));
const auto path = (root / "data").string();
{ std::ofstream stream(path); stream << "data"; Require(stream.good()); }
const int base = open(root.string().c_str(), SCE_KERNEL_O_RDONLY | SCE_KERNEL_O_DIRECTORY, 0);
const int file = open(path.c_str(), SCE_KERNEL_O_RDWR, 0);
Require(base >= 0 && file >= 0);
bool skipped = false;
if (test == "statfs") {
const auto query = LoadFunction<int (APS5_VABI*)(const char*, KernelStatfs*)>(sys, "statfs", patched);
const auto fquery = LoadFunction<int (APS5_VABI*)(int, KernelStatfs*)>(kernel, "_fstatfs", patched);
struct Output { std::uint64_t before; KernelStatfs value; std::uint64_t after; } out{};
out.before = 0xdeadbeef;
out.after = 0xbeefdead;
KernelStatfs byDescriptor{};
*error() = 71;
Require(query(path.c_str(), &out.value) == 0 && *error() == 71);
Require(out.before == 0xdeadbeef && out.after == 0xbeefdead);
Require(fquery(file, &byDescriptor) == 0);
Require(out.value.f_version == 0x20030518 && out.value.f_bsize > 0 && out.value.f_namemax > 0);
Require(out.value.f_bsize == byDescriptor.f_bsize && out.value.f_iosize == byDescriptor.f_iosize);
Require(out.value.f_flags == byDescriptor.f_flags && out.value.f_blocks == byDescriptor.f_blocks);
Require(query(root.string().c_str(), &out.value) == 0);
std::memset(&out.value, 0xa5, sizeof(out.value));
Require(query((root / "missing").string().c_str(), &out.value) == -1 && *error() == 2);
for (const auto byte : std::as_bytes(std::span(&out.value, 1))) Require(byte == std::byte{0xa5});
Require(query(nullptr, &out.value) == -1 && *error() == 14);
Require(query(reinterpret_cast<const char*>(1), &out.value) == -1 && *error() == 14);
Require(query("", &out.value) == -1 && *error() == 2);
Require(query((root / "data" / "child").string().c_str(), &out.value) == -1 && *error() == 20);
Require(query(path.c_str(), nullptr) == -1 && *error() == 14);
Require(query(path.c_str(), reinterpret_cast<KernelStatfs*>(1)) == -1 && *error() == 14);
#ifdef _WIN32
SYSTEM_INFO system{};
GetSystemInfo(&system);
const auto page = static_cast<std::size_t>(system.dwPageSize);
auto* memory = static_cast<unsigned char*>(VirtualAlloc(nullptr, page * 2, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
Require(memory != nullptr);
DWORD previous = 0;
Require(VirtualProtect(memory, page, PAGE_READONLY, &previous) != FALSE);
#else
const auto page = static_cast<std::size_t>(sysconf(_SC_PAGESIZE));
auto* memory = static_cast<unsigned char*>(mmap(nullptr, page * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
Require(memory != MAP_FAILED);
Require(mprotect(memory, page, PROT_READ) == 0);
#endif
Require(query(path.c_str(), reinterpret_cast<KernelStatfs*>(memory)) == -1 && *error() == 14);
#ifdef _WIN32
Require(VirtualProtect(memory, page, PAGE_READWRITE, &previous) != FALSE);
Require(VirtualProtect(memory + page, page, PAGE_NOACCESS, &previous) != FALSE);
#else
Require(mprotect(memory, page, PROT_READ | PROT_WRITE) == 0);
Require(mprotect(memory + page, page, PROT_NONE) == 0);
#endif
Require(query(path.c_str(), reinterpret_cast<KernelStatfs*>(memory + page - sizeof(KernelStatfs) / 2)) == -1 && *error() == 14);
#ifdef _WIN32
Require(VirtualFree(memory, 0, MEM_RELEASE) != FALSE);
#else
Require(munmap(memory, page * 2) == 0);
#endif
} else if (test == "futimesat") {
const auto update = LoadFunction<int (APS5_VABI*)(int, const char*, const KernelTimeval*)>(sys, "futimesat", patched);
const KernelTimeval times[2]{{1000000000, 123456}, {1000000001, 654321}};
*error() = 71;
Require(update(base, "data", times) == 0 && *error() == 71);
FileStat info{};
Require(stat(path.c_str(), &info) == 0);
Require(info.st_atim.tv_sec == times[0].tv_sec && info.st_mtim.tv_sec == times[1].tv_sec);
Require(info.st_atim.tv_nsec == times[0].tv_usec * 1000 && info.st_mtim.tv_nsec == times[1].tv_usec * 1000);
Require(update(-100, path.c_str(), times) == 0);
Require(update(-1, ("/" + path).c_str(), times) == 0);
Require(update(-1, "data", times) == -1 && *error() == 9);
Require(update(file, "data", times) == -1 && *error() == 20);
Require(update(base, nullptr, times) == -1 && *error() == 14);
Require(update(base, reinterpret_cast<const char*>(1), times) == -1 && *error() == 14);
Require(update(base, "", times) == -1 && *error() == 2);
Require(update(base, "missing", times) == -1 && *error() == 2);
KernelTimeval invalid[2]{{0, -1}, {0, 0}};
Require(update(-1, nullptr, invalid) == -1 && *error() == 22);
invalid[0].tv_usec = 1000000;
Require(update(base, "data", invalid) == -1 && *error() == 22);
Require(update(base, "data", nullptr) == 0);
Require(stat(path.c_str(), &info) == 0 && info.st_mtim.tv_sec > 1000000001);
} else if (test == "symlinkat") {
const auto link = LoadFunction<int (APS5_VABI*)(const char*, int, const char*)>(sys, "symlinkat", patched);
*error() = 71;
const int result = link("data", base, "link");
#ifdef _WIN32
if (result == -1 && *error() == 1) {
std::fputs("SKIP: Windows symbolic-link privileges unavailable\n", stderr);
skipped = true;
} else
#endif
{
Require(result == 0);
#ifdef _WIN32
const auto handle = CreateFileW((root / "link").c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, nullptr);
Require(handle != INVALID_HANDLE_VALUE);
std::array<std::byte, MAXIMUM_REPARSE_DATA_BUFFER_SIZE> data{};
DWORD size = 0;
const BOOL read = DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, nullptr, 0, data.data(), static_cast<DWORD>(data.size()), &size, nullptr);
Require(CloseHandle(handle) != FALSE);
Require(read != FALSE);
struct Header {
ULONG tag;
USHORT dataLength, reserved, substituteOffset, substituteLength, printOffset, printLength;
ULONG flags;
} header{};
static_assert(sizeof(Header) == 20);
Require(size >= sizeof(header));
std::memcpy(&header, data.data(), sizeof(header));
Require(header.tag == IO_REPARSE_TAG_SYMLINK && (header.flags & 1) != 0);
Require(header.dataLength == size - 8);
const auto begin = sizeof(header) + header.substituteOffset;
Require(begin <= size && header.substituteLength <= size - begin);
Require(header.substituteLength == 4 * sizeof(wchar_t));
Require(std::memcmp(data.data() + begin, L"data", header.substituteLength) == 0);
#else
Require(std::filesystem::read_symlink(root / "link") == "data");
#endif
std::ifstream stream(root / "link");
std::string contents;
stream >> contents;
Require(contents == "data");
Require(link("data", base, "link") == -1 && *error() == 17);
std::filesystem::create_directory(root / "child");
Require(link("../data", -100, (root / "child" / "parent").string().c_str()) == 0);
std::ifstream parentStream(root / "child" / "parent");
std::string parentContents;
parentStream >> parentContents;
Require(parentContents == "data");
Require(link("data", -100, (root / "cwd").string().c_str()) == 0);
Require(link("data", -1, ("/" + (root / "absolute").string()).c_str()) == 0);
}
} else if (test == "symlinkat_errors") {
const auto link = LoadFunction<int (APS5_VABI*)(const char*, int, const char*)>(sys, "symlinkat", patched);
Require(link("data", -1, "missing") == -1 && *error() == 9);
Require(link("data", file, "missing") == -1 && *error() == 20);
Require(link(nullptr, base, "missing") == -1 && *error() == 14);
Require(link(reinterpret_cast<const char*>(1), base, "missing") == -1 && *error() == 14);
Require(link("data", base, reinterpret_cast<const char*>(1)) == -1 && *error() == 14);
Require(link("data", base, nullptr) == -1 && *error() == 14);
Require(link("", base, "missing") == -1 && *error() == 2);
Require(link(std::string(1024, 'x').c_str(), -1, "missing") == -1 && *error() == 63);
Require(link("data", base, std::string(256, 'x').c_str()) == -1 && *error() == 63);
} else {
Require(false);
}
Require(close(file) == 0 && close(base) == 0);
std::filesystem::remove_all(root);
return skipped ? 77 : 0;
}
+3
View File
@@ -363,6 +363,9 @@ The following eight candidate spellings are absent from that aerolib revision. T
| `sceKernelSetAppInfoByAppId` | `fIyST5BItRg` |
### Functional
- [statfs and futimesat](../../../core/libs/prx/libkernel_sys/Export.Discovery.cpp) are not console-verified. `statfs` shares libkernel's host-volume statistics and incomplete guest mount identity fields with `_fstatfs`; archive-only paths throw because they have no host volume lookup. On Linux, FIFO and socket paths report their containing filesystem; this special-file behaviour is not independently measured on console. `futimesat` shares the existing timestamp implementation; descriptor-relative paths use the same host directory paths and guest-root limitations as `openat`, including Windows rename-after-open limits. A null path is `EFAULT`, as FreeBSD 11.4 `kern_utimesat`, not Linux's descriptor-only extension.
- [symlinkat](../../../core/libs/prx/libkernel_sys/Export.cpp) shares `symlink`'s target restrictions: absolute targets throw; a parent-directory target throws when the host would not follow it to the guest path it names. Descriptor-relative destination paths have the same guest-root limitations as `mkdirat`; Windows requires symbolic-link privileges. Console behaviour is unverified.
- [sceKernelGetPhysPageSize](../../../core/libs/prx/libkernel_sys/Export.Discovery.cpp) (libkernel_sys) returns `PS5_PAGE_SIZE` (16 KiB), the guest page granularity used by libkernel's direct-memory and mapping implementation. The export has no arguments. Console behaviour is unverified
- [revoke](../../../core/libs/prx/libkernel_sys/Export.Discovery.cpp) (libkernel_sys) - a character device throws, because device revocation is not modelled. Other paths follow FreeBSD 11 `sys_revoke`: path errors as `stat` reports them, and `EINVAL` for anything that is not a character device. Guest paths have no `/dev`, so `/dev/null` and the like fail with `ENOENT`, not as devices
+9 -2
View File
@@ -26,6 +26,13 @@ def device_limitations(output, patterns):
return bool(reasons) and all(any(pattern.search(reason) for pattern in patterns) for reason in reasons)
def symlink_privilege_limitation(name, output):
if name not in ("guest_kernel_sys_paths_symlinkat", "guest_kernel_sys_paths_symlinkat_patched"):
return False
reasons = [match.group(0).strip() for line in output.splitlines() if (match := re.search(r"(?:skipped,|SKIP:).*", line, re.IGNORECASE))]
return bool(reasons) and all(reason == "SKIP: Windows symbolic-link privileges unavailable" for reason in reasons)
def high_refresh_limitation(output):
reasons = []
for line in output.splitlines():
@@ -88,8 +95,8 @@ def main(argv=None):
print(f"{name}: {reason}")
return 0
if args.job == "windows":
unexpected = [name for name, output in found if not name.startswith(WINDOWS_PREFIX) and not device_limitations(output, (NO_DISPLAY, NO_VULKAN_DEVICE, NO_RAW_SOCKET)) and not high_refresh_limitation(output)]
expected = f"any {WINDOWS_PREFIX}* test, which the runner has no Vulkan driver for, or a test that reports no display or Vulkan device, a display without a 120 Hz mode or no permission to create a raw socket"
unexpected = [name for name, output in found if not name.startswith(WINDOWS_PREFIX) and not symlink_privilege_limitation(name, output) and not device_limitations(output, (NO_DISPLAY, NO_VULKAN_DEVICE, NO_RAW_SOCKET)) and not high_refresh_limitation(output)]
expected = f"any {WINDOWS_PREFIX}* test, which the runner has no Vulkan driver for, or a test that reports no display or Vulkan device, a display without a 120 Hz mode or no permission to create a raw socket, or the two symlink creation tests reporting unavailable Windows symbolic-link privileges"
else:
unexpected = [name for name, output in found if not device_limitations(output, (DEVICE_LIMITATION,)) and not high_refresh_limitation(output)]
expected = "any test that reports the device limitation it needs, such as narrow subgroups, a missing extension, no display for a Vulkan window, a display without a 120 Hz mode or no permission to create a raw socket"
+33
View File
@@ -124,6 +124,39 @@ class SkipJsonTests(unittest.TestCase):
class CheckSkipsTests(unittest.TestCase):
def test_windows_symlink_creation_privilege_skip_is_narrow(self):
reason = "SKIP: Windows symbolic-link privileges unavailable"
names = ("guest_kernel_sys_paths_symlinkat", "guest_kernel_sys_paths_symlinkat_patched")
with tempfile.TemporaryDirectory() as directory:
junit = Path(directory) / "junit.xml"
for name in names:
for channel in ("system-out", "system-err"):
with self.subTest(name=name, channel=channel):
junit.write_text(f'<testsuite><testcase name="{name}"><skipped message="SKIP_RETURN_CODE=77"/>'
f'<{channel}>startup log\n{reason}\n</{channel}></testcase></testsuite>', encoding="utf-8")
result = subprocess.run([sys.executable, "-I", str(TOOL), str(junit), "--job", "windows"], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
invalid = (
("guest_kernel_sys_paths_symlinkat_errors", reason, "windows", ""),
("guest_kernel_sys_paths_symlinkat_refused_unpatched", reason, "windows", ""),
("guest_probe", reason, "windows", ""),
(names[0], reason, "linux", ""),
(names[0], "", "windows", ""),
(names[0], reason + " extra", "windows", ""),
(names[0], "SKIP: unrelated failure", "windows", ""),
(names[0], reason + "\nSKIP: flaky", "windows", ""),
(names[0], reason + "\nskipped, flaky", "windows", ""),
(names[0], reason + "\nfixture: skipped, flaky", "windows", ""),
(names[0], reason + "\nfixture: SKIP: flaky", "windows", ""),
(names[0], reason, "windows", "flaky"),
)
for name, output, job, skipped_text in invalid:
with self.subTest(name=name, output=output, job=job, skipped_text=skipped_text):
junit.write_text(f'<testsuite><testcase name="{name}"><skipped message="SKIP_RETURN_CODE=77">{skipped_text}</skipped>'
f'<system-out>{output}</system-out></testcase></testsuite>', encoding="utf-8")
result = subprocess.run([sys.executable, "-I", str(TOOL), str(junit), "--job", job], capture_output=True, text=True)
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
def test_ctest_status_and_prefixed_device_reasons_are_expected(self):
reports = (
('<skipped message="SKIP_RETURN_CODE=77"/><system-out>skipped, the device lacks geometryShader</system-out>', "linux"),