From 2e0b0b419484d0360ec930c3f83bbe6eb7b7691e Mon Sep 17 00:00:00 2001 From: marcobuschini Date: Fri, 4 Dec 2026 09:49:59 +0000 Subject: [PATCH] test(syscalls): add host unit tests for FileSyscalls and SyscallHelpers (#258) - Add host-only snitch unit tests in `tests/host/file_syscalls_test.cpp` and `tests/host/syscall_helpers_test.cpp` and register them in `Makefile` - Update `sys_chdir()` in `kernel/syscalls/FileSyscalls.cpp` to use `normalize_vfs_path()` when possible instead of `build_dentry_path()` --- Makefile | 2 + kernel/syscalls/FileSyscalls.cpp | 7 +- tests/host/file_syscalls_test.cpp | 406 ++++++++++++++++++++++++++++ tests/host/syscall_helpers_test.cpp | 236 ++++++++++++++++ 4 files changed, 650 insertions(+), 1 deletion(-) create mode 100644 tests/host/file_syscalls_test.cpp create mode 100644 tests/host/syscall_helpers_test.cpp diff --git a/Makefile b/Makefile index 068e83e..b7d6189 100644 --- a/Makefile +++ b/Makefile @@ -73,6 +73,8 @@ HOST_ONLY_TEST_SRCS := \ tests/host/distro_abi_test.cpp \ tests/host/ddk_dummy_test.cpp \ tests/host/ddk_kernel_services_test.cpp \ + tests/host/file_syscalls_test.cpp \ + tests/host/syscall_helpers_test.cpp \ drivers/dummy/DummyDriver.cpp \ tests/host/socket_syscall_test.cpp \ tests/host/gicv2_test.cpp \ diff --git a/kernel/syscalls/FileSyscalls.cpp b/kernel/syscalls/FileSyscalls.cpp index c6026b5..6684537 100644 --- a/kernel/syscalls/FileSyscalls.cpp +++ b/kernel/syscalls/FileSyscalls.cpp @@ -66,7 +66,12 @@ int64_t sys_chdir(uintptr_t user_path) noexcept { return ERR_ENOTDIR; } - build_dentry_path(target, proc->mutable_cwd(), Kernel::Process::MAX_PATH_LEN); + char normalized[Kernel::Process::MAX_PATH_LEN]{}; + if (normalize_vfs_path(proc->cwd(), raw_path, normalized, sizeof(normalized))) { + cstr_copy(proc->mutable_cwd(), normalized, Kernel::Process::MAX_PATH_LEN); + } else { + build_dentry_path(target, proc->mutable_cwd(), Kernel::Process::MAX_PATH_LEN); + } target->release(); return 0; } diff --git a/tests/host/file_syscalls_test.cpp b/tests/host/file_syscalls_test.cpp new file mode 100644 index 0000000..b5ceff5 --- /dev/null +++ b/tests/host/file_syscalls_test.cpp @@ -0,0 +1,406 @@ +// SPDX-License-Identifier: GPL-3.0-only +// Copyright (c) 2026 Marco Buschini + +#include "fs/Vfs.hpp" +#include "fs/devfs/devfs.hpp" +#include "fs/rootfs/rootfs.hpp" +#include "fs/tmpfs/tmpfs.hpp" +#include "mm/AddressSpace.hpp" +#include "mm/heap.hpp" +#include "mm/pfa.hpp" +#include "mocks/host_hal.hpp" +#include "process/Process.hpp" +#include "syscalls/SyscallInternal.hpp" +#include "uapi/errno.hpp" +#include "uapi/fs.hpp" +#include "uapi/sys.hpp" + +#include +#include +#include +#include + +namespace { + +using namespace Kernel::FS; +using namespace Kernel::Syscalls; +using namespace Kernel::UAPI; + +constexpr uintptr_t kUserPageBase = 0x00400000ULL; +constexpr uintptr_t kStrArea = kUserPageBase + 0x000U; +constexpr uintptr_t kStrArea2 = kUserPageBase + 0x200U; +constexpr uintptr_t kDataArea = kUserPageBase + 0x400U; +constexpr uintptr_t kStructArea = kUserPageBase + 0x800U; +constexpr uintptr_t kBadUserAddr = kUserPageBase + 0x4000U; + +void init_file_syscalls_env() noexcept { + HostTest::reset_host_hal(); + HAL::get_mmu().activate(); + + Kernel::Boot::MemoryMap mem_map{}; + mem_map.regions[0] = Kernel::Boot::MemoryRegion{ + .base_paddr = HostTest::SIM_RAM_BASE, + .size = HostTest::SIM_RAM_SIZE, + }; + mem_map.count = 1U; + mem_map.min_paddr = HostTest::SIM_RAM_BASE; + mem_map.max_paddr = HostTest::SIM_RAM_BASE + HostTest::SIM_RAM_SIZE; + mem_map.total_usable_bytes = HostTest::SIM_RAM_SIZE; + + Kernel::MM::pfa_init(mem_map, HostTest::SIM_RAM_BASE, + HostTest::SIM_RAM_BASE + HostTest::SIM_PAGE_SIZE, 0U, 0U, 0U, 0U, nullptr); + Kernel::MM::heap_init(); + + vfs_reset(); + rootfs_reset(); + devfs_reset(); + tmpfs_reset(); + REQUIRE(rootfs_mount_cpio(nullptr, 0U) == kOk); + REQUIRE(devfs_init_and_mount() == kOk); + REQUIRE(tmpfs_init_and_mount() == kOk); +} + +struct ScopedFileProcess final { + Kernel::Process::Process* proc{nullptr}; + Kernel::Process::Process* prev{nullptr}; + uintptr_t page_paddr{0U}; + + ScopedFileProcess() noexcept + : proc(Kernel::Process::Process::create(0U)), prev(Kernel::Process::Process::current()) { + REQUIRE(proc != nullptr); + auto as_res = Kernel::MM::AddressSpace::create(); + REQUIRE(as_res.is_ok()); + proc->address_space() = static_cast(as_res.value()); + page_paddr = Kernel::MM::alloc_frame(); + REQUIRE(page_paddr != 0U); + REQUIRE(proc->address_space().map_user_page(kUserPageBase, page_paddr, + HAL::PageFlags::User | HAL::PageFlags::Read | + HAL::PageFlags::Write)); + Kernel::Process::Process::set_current(proc); + } + + ~ScopedFileProcess() noexcept { + Kernel::Process::Process::set_current(prev); + if (proc != nullptr) { + proc->address_space().unmap_user_page(kUserPageBase); + proc->address_space().destroy(); + Kernel::Process::Process::destroy(*proc); + } + if (page_paddr != 0U) { + Kernel::MM::free_frame(page_paddr); + } + } + + ScopedFileProcess(const ScopedFileProcess&) = delete; + ScopedFileProcess& operator=(const ScopedFileProcess&) = delete; + ScopedFileProcess(ScopedFileProcess&&) = delete; + ScopedFileProcess& operator=(ScopedFileProcess&&) = delete; + + [[nodiscard]] uintptr_t root() const noexcept { return proc->address_space().root_paddr(); } + + void write_str(uintptr_t va, const char* s) const noexcept { + REQUIRE(Kernel::MM::copy_to_user(root(), va, s, std::strlen(s) + 1U) == 0); + } +}; + +} // namespace + +TEST_CASE("FileSyscalls directory, path, openat, rename, getdents64, and unlinkat paths", + "[syscalls][file]") { + init_file_syscalls_env(); + ScopedFileProcess ctx; + + // getcwd + CHECK(sys_getcwd(kBadUserAddr, 64U) == ERR_EFAULT); + CHECK(sys_getcwd(kDataArea, 1U) == ERR_ERANGE); + REQUIRE(sys_getcwd(kDataArea, 64U) > 0); + char cwd[64]{}; + REQUIRE(Kernel::MM::copy_from_user(ctx.root(), cwd, kDataArea, sizeof(cwd)) == 0); + CHECK(std::strcmp(cwd, "/") == 0); + + // mkdirat & chdir + ctx.write_str(kStrArea, "/tmp/dir1"); + CHECK(sys_mkdirat(AT_FDCWD, kBadUserAddr, 0755U) == ERR_EFAULT); + REQUIRE(sys_mkdirat(AT_FDCWD, kStrArea, 0755U) == 0); + CHECK(sys_mkdirat(AT_FDCWD, kStrArea, 0755U) == ERR_EEXIST); + + ctx.write_str(kStrArea2, "/tmp/missing"); + CHECK(sys_chdir(kStrArea2) == ERR_ENOENT); + CHECK(sys_chdir(kBadUserAddr) == ERR_EFAULT); + REQUIRE(sys_chdir(kStrArea) == 0); + + REQUIRE(sys_getcwd(kDataArea, 64U) > 0); + REQUIRE(Kernel::MM::copy_from_user(ctx.root(), cwd, kDataArea, sizeof(cwd)) == 0); + CHECK(std::strcmp(cwd, "/tmp/dir1") == 0); + + // openat: relative to cwd, O_CREAT|O_EXCL, O_DIRECTORY error, EROFS on rootfs + ctx.write_str(kStrArea, "alpha.txt"); + const int64_t fd = sys_openat(AT_FDCWD, kStrArea, O_CREAT | O_RDWR, 0644U); + REQUIRE(fd >= 0); + CHECK(sys_openat(AT_FDCWD, kStrArea, O_CREAT | O_EXCL | O_RDWR, 0644U) == ERR_EEXIST); + CHECK(sys_openat(AT_FDCWD, kStrArea, O_RDONLY | O_DIRECTORY, 0U) == ERR_ENOTDIR); + CHECK(sys_openat(99, kStrArea, O_RDONLY, 0U) == ERR_EBADF); + CHECK(sys_openat(AT_FDCWD, kBadUserAddr, O_RDONLY, 0U) == ERR_EFAULT); + ctx.write_str(kStrArea2, "/ro_file"); + CHECK(sys_openat(AT_FDCWD, kStrArea2, O_CREAT | O_WRONLY, 0644U) == ERR_EROFS); + REQUIRE(sys_close(static_cast(fd)) == 0); + + // chdir back to "/" and test chdir on regular file + ctx.write_str(kStrArea2, "/tmp/dir1/alpha.txt"); + CHECK(sys_chdir(kStrArea2) == ERR_ENOTDIR); + ctx.write_str(kStrArea2, "/"); + REQUIRE(sys_chdir(kStrArea2) == 0); + + // renameat / renameat2 + ctx.write_str(kStrArea, "/tmp/dir1/alpha.txt"); + ctx.write_str(kStrArea2, "/tmp/dir1/beta.txt"); + CHECK(sys_renameat2(AT_FDCWD, kStrArea, AT_FDCWD, kStrArea2, 0xFFFFU) == ERR_EINVAL); + REQUIRE(sys_renameat(AT_FDCWD, kStrArea, AT_FDCWD, kStrArea2) == 0); + + // getdents64 on /tmp/dir1 + ctx.write_str(kStrArea, "/tmp/dir1"); + const int64_t dfd = sys_openat(AT_FDCWD, kStrArea, O_RDONLY | O_DIRECTORY, 0U); + REQUIRE(dfd >= 0); + CHECK(sys_getdents64(99U, kDataArea, 256U) == ERR_EBADF); + CHECK(sys_getdents64(static_cast(dfd), kDataArea, 8U) == 0); + CHECK(sys_getdents64(static_cast(dfd), kBadUserAddr, 256U) == ERR_EFAULT); + const int64_t dent_bytes = sys_getdents64(static_cast(dfd), kDataArea, 256U); + CHECK(dent_bytes > 0); + REQUIRE(sys_close(static_cast(dfd)) == 0); + + // unlinkat file and directory + ctx.write_str(kStrArea, "/tmp/dir1"); + CHECK(sys_unlinkat(AT_FDCWD, kStrArea, 0U) == ERR_EISDIR); + ctx.write_str(kStrArea2, "/tmp/dir1/beta.txt"); + REQUIRE(sys_unlinkat(AT_FDCWD, kStrArea2, 0U) == 0); + REQUIRE(sys_unlinkat(AT_FDCWD, kStrArea, AT_REMOVEDIR) == 0); +} + +TEST_CASE("FileSyscalls read, write, writev, lseek, truncate, stat, chmod, chown, and access", + "[syscalls][file]") { + init_file_syscalls_env(); + ScopedFileProcess ctx; + + ctx.write_str(kStrArea, "/tmp/io_test.bin"); + const int64_t fd = sys_openat(AT_FDCWD, kStrArea, O_CREAT | O_RDWR, 0644U); + REQUIRE(fd >= 0); + const auto ufd = static_cast(fd); + + // write & writev + const char part1[] = "hello "; + const char part2[] = "milo"; + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kDataArea, part1, 6U) == 0); + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kDataArea + 16U, part2, 4U) == 0); + const linux_iovec iov[2] = { + {.iov_base = kDataArea, .iov_len = 6U}, + {.iov_base = kDataArea + 16U, .iov_len = 4U}, + }; + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kStructArea, iov, sizeof(iov)) == 0); + CHECK(sys_writev(ufd, kStructArea, 0U) == 0); + CHECK(sys_writev(99U, kStructArea, 2U) == ERR_EBADF); + CHECK(sys_writev(ufd, kBadUserAddr, 2U) == ERR_EFAULT); + REQUIRE(sys_writev(ufd, kStructArea, 2U) == 10); + + // lseek & read + CHECK(sys_lseek(99U, 0, SEEK_SET) == ERR_EBADF); + CHECK(sys_lseek(ufd, 0, 99U) == ERR_EINVAL); + CHECK(sys_lseek(ufd, 0, SEEK_END) == 10); + CHECK(sys_lseek(ufd, -4, SEEK_CUR) == 6); + CHECK(sys_lseek(ufd, 0, SEEK_SET) == 0); + + CHECK(sys_read(99U, kDataArea + 64U, 10U) == ERR_EBADF); + CHECK(sys_read(ufd, kBadUserAddr, 10U) == ERR_EFAULT); + REQUIRE(sys_read(ufd, kDataArea + 64U, 10U) == 10); + char readback[16]{}; + REQUIRE(Kernel::MM::copy_from_user(ctx.root(), readback, kDataArea + 64U, 10U) == 0); + CHECK(std::memcmp(readback, "hello milo", 10U) == 0); + + // ftruncate & truncate + CHECK(sys_ftruncate(ufd, -1) == ERR_EINVAL); + CHECK(sys_ftruncate(99U, 4) == ERR_EBADF); + REQUIRE(sys_ftruncate(ufd, 5) == 0); + CHECK(sys_truncate(kStrArea, -1) == ERR_EINVAL); + REQUIRE(sys_truncate(kStrArea, 3) == 0); + + // fstat & newfstatat + CHECK(sys_fstat(99U, kStructArea) == ERR_EBADF); + CHECK(sys_fstat(ufd, kBadUserAddr) == ERR_EFAULT); + REQUIRE(sys_fstat(ufd, kStructArea) == 0); + linux_stat st{}; + REQUIRE(Kernel::MM::copy_from_user(ctx.root(), &st, kStructArea, sizeof(st)) == 0); + CHECK(st.st_size == 3); + + REQUIRE(sys_newfstatat(AT_FDCWD, kStrArea, kStructArea, 0U) == 0); + ctx.write_str(kStrArea2, "/tmp/missing_stat"); + CHECK(sys_newfstatat(AT_FDCWD, kStrArea2, kStructArea, 0U) == ERR_ENOENT); + + // fsync, fchmod, fchmodat, fchown, fchownat, utimensat, faccessat, readlinkat + CHECK(sys_fsync(ufd, 0) == 0); + CHECK(sys_fsync(99U, 1) == ERR_EBADF); + CHECK(sys_fchmod(ufd, 0600U) == 0); + CHECK(sys_fchmod(99U, 0600U) == ERR_EBADF); + CHECK(sys_fchmodat(AT_FDCWD, kStrArea, 0644U, 0U) == 0); + CHECK(sys_fchown(ufd, 1000U, 1000U) == 0); + CHECK(sys_fchownat(AT_FDCWD, kStrArea, 0U, 0U, 0U) == 0); + CHECK(sys_utimensat(AT_FDCWD, kStrArea, 0U, 0U) == 0); + CHECK(sys_faccessat(AT_FDCWD, kStrArea, 0U, 0U) == 0); + CHECK(sys_faccessat2(AT_FDCWD, kStrArea, 0U, 0U) == 0); + CHECK(sys_readlinkat(AT_FDCWD, kStrArea, kDataArea, 32U) == ERR_EINVAL); + + REQUIRE(sys_close(ufd) == 0); + CHECK(sys_close(ufd) == ERR_EBADF); +} + +TEST_CASE("FileSyscalls pipe2, dup, dup3, fcntl, ioctl, ppoll, and pselect6", + "[syscalls][file][ipc]") { + init_file_syscalls_env(); + ScopedFileProcess ctx; + + // pipe2 + CHECK(sys_pipe2(kBadUserAddr, 0U) == ERR_EFAULT); + CHECK(sys_pipe2(kStructArea, 0x1234U) == ERR_EINVAL); + REQUIRE(sys_pipe2(kStructArea, O_CLOEXEC | O_NONBLOCK) == 0); + + int32_t fds[2]{-1, -1}; + REQUIRE(Kernel::MM::copy_from_user(ctx.root(), fds, kStructArea, sizeof(fds)) == 0); + REQUIRE(fds[0] >= 0); + REQUIRE(fds[1] >= 0); + + // dup & dup3 + CHECK(sys_dup(99U) == ERR_EBADF); + const int64_t dup_fd = sys_dup(static_cast(fds[0])); + REQUIRE(dup_fd >= 0); + CHECK(sys_dup3(static_cast(fds[0]), static_cast(fds[0]), 0U) == ERR_EINVAL); + CHECK(sys_dup3(static_cast(fds[0]), 12U, 0x1234U) == ERR_EINVAL); + REQUIRE(sys_dup3(static_cast(fds[0]), 12U, O_CLOEXEC) == 12); + REQUIRE(sys_close(12U) == 0); + REQUIRE(sys_close(static_cast(dup_fd)) == 0); + + // fcntl + CHECK(sys_fcntl(99U, F_GETFD, 0U) == ERR_EBADF); + CHECK(sys_fcntl(static_cast(fds[0]), F_GETFD, 0U) == 1); + REQUIRE(sys_fcntl(static_cast(fds[0]), F_SETFD, 0U) == 0); + CHECK(sys_fcntl(static_cast(fds[0]), F_GETFD, 0U) == 0); + CHECK(sys_fcntl(static_cast(fds[0]), 9999U, 0U) == ERR_EINVAL); + const int64_t fcntl_dup = sys_fcntl(static_cast(fds[0]), F_DUPFD_CLOEXEC, 15U); + REQUIRE(fcntl_dup == 15); + REQUIRE(sys_close(static_cast(fcntl_dup)) == 0); + + // write to pipe and poll/pselect6 + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kDataArea, "ping", 4U) == 0); + REQUIRE(sys_write(static_cast(fds[1]), kDataArea, 4U) == 4); + + linux_pollfd pfd{.fd = fds[0], .events = 0x0001 /*POLLIN*/, .revents = 0}; + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kStructArea, &pfd, sizeof(pfd)) == 0); + const uint64_t zero_ts[2] = {0ULL, 0ULL}; + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kStructArea + 64U, zero_ts, sizeof(zero_ts)) == 0); + CHECK(sys_ppoll(kBadUserAddr, 1U, 0U, 0U, 0U) == ERR_EFAULT); + REQUIRE(sys_ppoll(kStructArea, 1U, kStructArea + 64U, 0U, 0U) == 1); + REQUIRE(Kernel::MM::copy_from_user(ctx.root(), &pfd, kStructArea, sizeof(pfd)) == 0); + CHECK((static_cast(pfd.revents) & 0x0001U) != 0U); + + uint64_t rfds = (1ULL << static_cast(fds[0])); + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kStructArea + 128U, &rfds, sizeof(rfds)) == 0); + CHECK(sys_pselect6(-1, 0U, 0U, 0U, 0U, 0U) == ERR_EINVAL); + REQUIRE(sys_pselect6(fds[0] + 1, kStructArea + 128U, 0U, 0U, kStructArea + 64U, 0U) == 1); + + // ioctl on /dev/null -> ERR_ENOTTY + ctx.write_str(kStrArea, "/dev/null"); + const int64_t null_fd = sys_openat(AT_FDCWD, kStrArea, O_RDWR, 0U); + REQUIRE(null_fd >= 0); + CHECK(sys_ioctl(static_cast(null_fd), 0x5401ULL, 0U) == ERR_ENOTTY); + CHECK(sys_ioctl(99U, 0x5401ULL, 0U) == ERR_EBADF); + REQUIRE(sys_close(static_cast(null_fd)) == 0); + + REQUIRE(sys_close(static_cast(fds[0])) == 0); + REQUIRE(sys_close(static_cast(fds[1])) == 0); +} + +TEST_CASE("FileSyscalls extended fcntl, stat, utimensat, access, renameat2, ioctl, and poll " + "branches", + "[syscalls][file]") { + init_file_syscalls_env(); + ScopedFileProcess ctx; + + // Create two files in /tmp to exercise renameat2 RENAME_NOREPLACE & utimensat + ctx.write_str(kStrArea, "/tmp/src_a"); + ctx.write_str(kStrArea2, "/tmp/dst_b"); + const int64_t fa = sys_openat(AT_FDCWD, kStrArea, O_CREAT | O_RDWR, 0644U); + const int64_t fb = sys_openat(AT_FDCWD, kStrArea2, O_CREAT | O_RDWR, 0644U); + REQUIRE(fa >= 0); + REQUIRE(fb >= 0); + + // RENAME_NOREPLACE fails with EEXIST when dst exists; relative path w/ bad dirfd -> EBADF + CHECK(sys_renameat2(AT_FDCWD, kStrArea, AT_FDCWD, kStrArea2, 1U /*RENAME_NOREPLACE*/) == + ERR_EEXIST); + ctx.write_str(kDataArea + 128U, "rel_src"); + CHECK(sys_renameat2(99, kDataArea + 128U, AT_FDCWD, kStrArea2, 0U) == ERR_EBADF); + + // fcntl F_DUPFD, F_GETFL, F_SETFL + const int64_t dup_min = sys_fcntl(static_cast(fa), F_DUPFD, 10U); + REQUIRE(dup_min == 10); + CHECK(sys_fcntl(10U, F_GETFL, 0U) >= 0); + CHECK(sys_fcntl(10U, F_SETFL, O_NONBLOCK) == 0); + CHECK(sys_close(10U) == 0); + + // newfstatat with AT_EMPTY_PATH (0x1000) and empty string + ctx.write_str(kDataArea, ""); + CHECK(sys_newfstatat(static_cast(fa), kDataArea, kStructArea, 0x1000U) == 0); + CHECK(sys_newfstatat(99, kDataArea, kStructArea, 0x1000U) == ERR_EBADF); + + // utimensat with explicit timespec[2] and invalid nsec + const uint64_t valid_times[4] = {100ULL, 500ULL, 200ULL, 600ULL}; + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kStructArea, valid_times, sizeof(valid_times)) == + 0); + CHECK(sys_utimensat(AT_FDCWD, kStrArea, kStructArea, 0U) == 0); + CHECK(sys_utimensat(AT_FDCWD, kStrArea, kBadUserAddr, 0U) == ERR_EFAULT); + + const uint64_t bad_times[4] = {100ULL, 2000000000ULL, 200ULL, 0ULL}; + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kStructArea, bad_times, sizeof(bad_times)) == 0); + CHECK(sys_utimensat(AT_FDCWD, kStrArea, kStructArea, 0U) == ERR_EINVAL); + + // faccessat2 R/W/X checks and missing file + CHECK(sys_faccessat2(AT_FDCWD, kStrArea, 4U | 2U /*R_OK|W_OK*/, 0U) == 0); + CHECK(sys_faccessat2(AT_FDCWD, kStrArea, 1U /*X_OK*/, 0U) == ERR_EACCES); + CHECK(sys_faccessat2(AT_FDCWD, kBadUserAddr, 0U, 0U) == ERR_EFAULT); + + // write/read 0 bytes and read-only / write-only checks + CHECK(sys_write(static_cast(fa), kDataArea, 0U) == 0); + CHECK(sys_read(static_cast(fa), kDataArea, 0U) == 0); + + // /dev/console ioctl TCGETS & TIOCGWINSZ + ctx.write_str(kStrArea, "/dev/console"); + const int64_t con_fd = sys_openat(AT_FDCWD, kStrArea, O_RDWR, 0U); + REQUIRE(con_fd >= 0); + CHECK(sys_ioctl(static_cast(con_fd), 0x5401ULL /*TCGETS*/, kStructArea) == 0); + CHECK(sys_ioctl(static_cast(con_fd), 0x5413ULL /*TIOCGWINSZ*/, kStructArea) == 0); + CHECK(sys_ioctl(static_cast(con_fd), 0x5401ULL, kBadUserAddr) == ERR_EFAULT); + REQUIRE(sys_close(static_cast(con_fd)) == 0); + + // pipe nonblocking EAGAIN, ppoll POLLOUT + POLLNVAL, pselect6 wfds/efds + REQUIRE(sys_pipe2(kStructArea, O_NONBLOCK) == 0); + int32_t pfds[2]{-1, -1}; + REQUIRE(Kernel::MM::copy_from_user(ctx.root(), pfds, kStructArea, sizeof(pfds)) == 0); + CHECK(sys_read(static_cast(pfds[0]), kDataArea, 8U) == ERR_EAGAIN); + + const linux_pollfd poll_arr[2] = { + {.fd = pfds[1], .events = 0x0004 /*POLLOUT*/, .revents = 0}, + {.fd = 99, .events = 0x0001, .revents = 0}, + }; + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kStructArea, poll_arr, sizeof(poll_arr)) == 0); + const uint64_t zero_ts[2] = {0ULL, 0ULL}; + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kStructArea + 64U, zero_ts, sizeof(zero_ts)) == 0); + CHECK(sys_ppoll(kStructArea, 2U, kStructArea + 64U, 0U, 0U) == 2); + + uint64_t wfds = (1ULL << static_cast(pfds[1])); + uint64_t efds = 0ULL; + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kStructArea + 128U, &wfds, sizeof(wfds)) == 0); + REQUIRE(Kernel::MM::copy_to_user(ctx.root(), kStructArea + 136U, &efds, sizeof(efds)) == 0); + CHECK(sys_pselect6(pfds[1] + 1, 0U, kStructArea + 128U, kStructArea + 136U, kStructArea + 64U, + 0U) == 1); + + REQUIRE(sys_close(static_cast(pfds[0])) == 0); + REQUIRE(sys_close(static_cast(pfds[1])) == 0); + REQUIRE(sys_close(static_cast(fa)) == 0); + REQUIRE(sys_close(static_cast(fb)) == 0); +} diff --git a/tests/host/syscall_helpers_test.cpp b/tests/host/syscall_helpers_test.cpp new file mode 100644 index 0000000..86ebaf2 --- /dev/null +++ b/tests/host/syscall_helpers_test.cpp @@ -0,0 +1,236 @@ +// SPDX-License-Identifier: GPL-3.0-only +// Copyright (c) 2026 Marco Buschini + +#include "fs/Vfs.hpp" +#include "fs/devfs/devfs.hpp" +#include "fs/rootfs/rootfs.hpp" +#include "fs/tmpfs/tmpfs.hpp" +#include "mm/AddressSpace.hpp" +#include "mm/heap.hpp" +#include "mm/pfa.hpp" +#include "mocks/host_hal.hpp" +#include "process/Process.hpp" +#include "syscalls/SyscallInternal.hpp" +#include "uapi/errno.hpp" +#include "uapi/fs.hpp" + +#include +#include +#include +#include + +namespace { + +using namespace Kernel::FS; +using namespace Kernel::Syscalls; +using namespace Kernel::UAPI; + +constexpr uintptr_t kUserPageBase = 0x00400000ULL; + +void init_helpers_env() noexcept { + HostTest::reset_host_hal(); + HAL::get_mmu().activate(); + + Kernel::Boot::MemoryMap mem_map{}; + mem_map.regions[0] = Kernel::Boot::MemoryRegion{ + .base_paddr = HostTest::SIM_RAM_BASE, + .size = HostTest::SIM_RAM_SIZE, + }; + mem_map.count = 1U; + mem_map.min_paddr = HostTest::SIM_RAM_BASE; + mem_map.max_paddr = HostTest::SIM_RAM_BASE + HostTest::SIM_RAM_SIZE; + mem_map.total_usable_bytes = HostTest::SIM_RAM_SIZE; + + Kernel::MM::pfa_init(mem_map, HostTest::SIM_RAM_BASE, + HostTest::SIM_RAM_BASE + HostTest::SIM_PAGE_SIZE, 0U, 0U, 0U, 0U, nullptr); + Kernel::MM::heap_init(); + + vfs_reset(); + rootfs_reset(); + devfs_reset(); + tmpfs_reset(); + REQUIRE(rootfs_mount_cpio(nullptr, 0U) == kOk); + REQUIRE(devfs_init_and_mount() == kOk); + REQUIRE(tmpfs_init_and_mount() == kOk); +} + +struct ScopedHelperProcess final { + Kernel::Process::Process* proc{nullptr}; + Kernel::Process::Process* prev{nullptr}; + uintptr_t page_paddr{0U}; + + ScopedHelperProcess() noexcept + : proc(Kernel::Process::Process::create(0U)), prev(Kernel::Process::Process::current()) { + REQUIRE(proc != nullptr); + auto as_res = Kernel::MM::AddressSpace::create(); + REQUIRE(as_res.is_ok()); + proc->address_space() = static_cast(as_res.value()); + page_paddr = Kernel::MM::alloc_frame(); + REQUIRE(page_paddr != 0U); + REQUIRE(proc->address_space().map_user_page(kUserPageBase, page_paddr, + HAL::PageFlags::User | HAL::PageFlags::Read | + HAL::PageFlags::Write)); + Kernel::Process::Process::set_current(proc); + } + + ~ScopedHelperProcess() noexcept { + Kernel::Process::Process::set_current(prev); + if (proc != nullptr) { + proc->address_space().unmap_user_page(kUserPageBase); + proc->address_space().destroy(); + Kernel::Process::Process::destroy(*proc); + } + if (page_paddr != 0U) { + Kernel::MM::free_frame(page_paddr); + } + } + + ScopedHelperProcess(const ScopedHelperProcess&) = delete; + ScopedHelperProcess& operator=(const ScopedHelperProcess&) = delete; + ScopedHelperProcess(ScopedHelperProcess&&) = delete; + ScopedHelperProcess& operator=(ScopedHelperProcess&&) = delete; +}; + +} // namespace + +TEST_CASE("SyscallHelpers string, path normalization, and monotonic time utilities", + "[syscalls][helpers]") { + init_helpers_env(); + + CHECK(cstr_len("hello") == 5U); + CHECK(cstr_eq("abc", "abc")); + CHECK_FALSE(cstr_eq("abc", "abd")); + + char buf[8]{}; + cstr_copy(nullptr, "ignored", 4U); + cstr_copy(buf, nullptr, sizeof(buf)); + CHECK(buf[0] == '\0'); + cstr_copy(buf, "truncated_text", sizeof(buf)); + CHECK(cstr_eq(buf, "truncat")); + + char path[64]{}; + CHECK_FALSE(normalize_vfs_path("/", nullptr, path, sizeof(path))); + CHECK_FALSE(normalize_vfs_path("/", "/a", nullptr, 4U)); + CHECK_FALSE(normalize_vfs_path("/", "/a", path, 1U)); + CHECK(normalize_vfs_path("/tmp/sub", "file.txt", path, sizeof(path))); + CHECK(cstr_eq(path, "/tmp/sub/file.txt")); + CHECK(normalize_vfs_path("/tmp/sub", ".././other/../../etc", path, sizeof(path))); + CHECK(cstr_eq(path, "/etc")); + CHECK(normalize_vfs_path("/tmp", "/", path, sizeof(path))); + CHECK(cstr_eq(path, "/")); + + CHECK(monotonic_now_ns() == monotonic_now().nanoseconds()); +} + +TEST_CASE("SyscallHelpers user range validation, cstring copy, and string array staging", + "[syscalls][helpers]") { + init_helpers_env(); + ScopedHelperProcess ctx; + const uintptr_t root = ctx.proc->address_space().root_paddr(); + + // Range validation edge cases + CHECK(validate_user_read_range(root, kUserPageBase, 0U)); + CHECK_FALSE(validate_user_read_range(root, Kernel::MM::user_addr_space_limit(), 1U)); + CHECK_FALSE(validate_user_read_range(0U, kUserPageBase, 16U)); + CHECK_FALSE(validate_user_read_range(root, Kernel::MM::user_addr_space_limit() - 4U, 8U)); + CHECK(validate_user_read_range(root, kUserPageBase, 64U)); + CHECK(validate_user_write_range(root, kUserPageBase, 64U)); + CHECK_FALSE(validate_user_read_range(root, kUserPageBase + 4096U, 8U)); + + // Demand-zero and CoW page validation + const uintptr_t dz_va = kUserPageBase + 0x2000U; + REQUIRE(ctx.proc->address_space().map_demand_zero(dz_va, + HAL::PageFlags::User | HAL::PageFlags::Read)); + CHECK(validate_user_read_range(root, dz_va, 16U)); + CHECK_FALSE(validate_user_write_range(root, dz_va, 16U)); + ctx.proc->address_space().unmap_user_page(dz_va); + + const uintptr_t cow_va = kUserPageBase + 0x4000U; + const uintptr_t cow_pa = Kernel::MM::alloc_frame(); + REQUIRE(cow_pa != 0U); + REQUIRE(HAL::AddressSpaceBackend::map_cow_user_page( + root, cow_va, cow_pa, HAL::PageFlags::User | HAL::PageFlags::Read)); + CHECK(validate_user_write_range(root, cow_va, 16U)); + ctx.proc->address_space().unmap_user_page(cow_va); + Kernel::MM::free_frame(cow_pa); + + // copy_user_cstring + char dst[16]{}; + size_t len = 0U; + CHECK(copy_user_cstring(root, 0U, dst, sizeof(dst), len) == ERR_EFAULT); + CHECK(copy_user_cstring(root, kUserPageBase + 4096U, dst, sizeof(dst), len) == ERR_EFAULT); + + const char msg[] = "milo-helper"; + REQUIRE(Kernel::MM::copy_to_user(root, kUserPageBase, msg, sizeof(msg)) == 0); + CHECK(copy_user_cstring(root, kUserPageBase, dst, 4U, len) == ERR_E2BIG); + CHECK(copy_user_cstring(root, kUserPageBase, dst, sizeof(dst), len) == 0); + CHECK(len == sizeof(msg) - 1U); + CHECK(cstr_eq(dst, "milo-helper")); + + // stage_user_string_array + const char* ptrs[4]{}; + char pool[64]{}; + size_t pool_off = 0U; + int count = -1; + CHECK(stage_user_string_array(root, 0U, ptrs, 3U, pool, sizeof(pool), pool_off, count) == 0); + CHECK(count == 0); + + const uintptr_t str1_va = kUserPageBase + 0x100U; + const uintptr_t str2_va = kUserPageBase + 0x120U; + REQUIRE(Kernel::MM::copy_to_user(root, str1_va, "arg0", 5U) == 0); + REQUIRE(Kernel::MM::copy_to_user(root, str2_va, "arg1", 5U) == 0); + + const uint64_t arr[3] = {str1_va, str2_va, 0ULL}; + const uintptr_t arr_va = kUserPageBase + 0x200U; + REQUIRE(Kernel::MM::copy_to_user(root, arr_va, arr, sizeof(arr)) == 0); + + pool_off = 0U; + CHECK(stage_user_string_array(root, arr_va, ptrs, 3U, pool, sizeof(pool), pool_off, count) == + 0); + CHECK(count == 2); + CHECK(cstr_eq(ptrs[0], "arg0")); + CHECK(cstr_eq(ptrs[1], "arg1")); + + pool_off = 0U; + CHECK(stage_user_string_array(root, arr_va, ptrs, 1U, pool, sizeof(pool), pool_off, count) == + ERR_E2BIG); + CHECK(stage_user_string_array(root, kUserPageBase + 4096U, ptrs, 3U, pool, sizeof(pool), + pool_off, count) == ERR_EFAULT); +} + +TEST_CASE("SyscallHelpers dirfd resolution and dentry path construction", "[syscalls][helpers]") { + init_helpers_env(); + ScopedHelperProcess ctx; + + Dentry* base = nullptr; + REQUIRE(resolve_dirfd_dentry(*ctx.proc, AT_FDCWD, base) == 0); + REQUIRE(base != nullptr); + char out_path[64]{}; + build_dentry_path(base, out_path, sizeof(out_path)); + CHECK(cstr_eq(out_path, "/")); + base->release(); + + CHECK(resolve_dirfd_dentry(*ctx.proc, 99, base) == ERR_EBADF); + + // Regular file FD -> ERR_ENOTDIR + REQUIRE(Kernel::MM::copy_to_user(ctx.proc->address_space().root_paddr(), kUserPageBase, + "/tmp/notadir", 13U) == 0); + const int64_t file_fd = sys_openat(AT_FDCWD, kUserPageBase, O_CREAT | O_RDWR, 0644U); + REQUIRE(file_fd >= 0); + CHECK(resolve_dirfd_dentry(*ctx.proc, static_cast(file_fd), base) == ERR_ENOTDIR); + CHECK(sys_close(static_cast(file_fd)) == 0); + + // Directory FD -> succeeds and build_dentry_path returns "/tmp/sub" + REQUIRE(vfs_mkdir("/tmp/sub", 0755U) == kOk); + REQUIRE(Kernel::MM::copy_to_user(ctx.proc->address_space().root_paddr(), kUserPageBase, + "/tmp/sub", 9U) == 0); + const int64_t dir_fd = sys_openat(AT_FDCWD, kUserPageBase, O_RDONLY | O_DIRECTORY, 0U); + REQUIRE(dir_fd >= 0); + REQUIRE(resolve_dirfd_dentry(*ctx.proc, static_cast(dir_fd), base) == 0); + REQUIRE(base != nullptr); + // build_dentry_path walks the mount's dentry parent chain ("/sub" inside tmpfs) + build_dentry_path(base, out_path, sizeof(out_path)); + CHECK(cstr_eq(out_path, "/sub")); + base->release(); + CHECK(sys_close(static_cast(dir_fd)) == 0); +}