Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ 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 \
tests/host/process_subsystem_test.cpp \
tests/host/cpio_duration_test.cpp \
tests/host/virtio_blk_test.cpp \
drivers/dummy/DummyDriver.cpp \
tests/host/socket_syscall_test.cpp \
tests/host/gicv2_test.cpp \
Expand Down
2 changes: 1 addition & 1 deletion kernel/process/Signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ void deliver_pending_signals(HAL::CpuContext& ctx) noexcept {
exit_current_process(128 + static_cast<int64_t>(sig));
}

const SigAction& act = proc->signals().action(sig);
const SigAction act = proc->signals().action(sig);
if (act.handler == KERNEL_SIG_IGN) {
proc->signals().restore_saved_mask_if_any();
return;
Expand Down
7 changes: 6 additions & 1 deletion kernel/syscalls/FileSyscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
223 changes: 223 additions & 0 deletions tests/host/cpio_duration_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (c) 2026 Marco Buschini <marco.buschini@gmail.com>

#include "fs/cpio.hpp"
#include "time/Duration.hpp"

#include <algorithm>
#include <array>
#include <cstdint>
#include <cstring>
#include <snitch/snitch.hpp>
#include <string_view>
#include <vector>

namespace {

using namespace Kernel::FS;
using namespace Kernel::Time;

void write_hex8(char* dst, uint32_t val) noexcept {
static constexpr char kHex[] = "0123456789abcdef";
for (size_t i = 0U; i < 8U; ++i) {
dst[7U - i] = kHex[val & 0xFU];
val >>= 4U;
}
}

void append_cpio_entry(std::vector<uint8_t>& buf, const char* magic, std::string_view name,
uint32_t mode, const void* payload, uint32_t payload_size,
bool include_nul = true) noexcept {
CpioNewcHeader hdr{};
std::fill_n(reinterpret_cast<char*>(&hdr), sizeof(hdr), '0');
std::memcpy(hdr.c_magic, magic, 6U);
write_hex8(hdr.c_mode, mode);
write_hex8(hdr.c_filesize, payload_size);
const auto namesize = static_cast<uint32_t>(name.size() + 1U);
write_hex8(hdr.c_namesize, namesize);

const auto* hdr_bytes = reinterpret_cast<const uint8_t*>(&hdr);
buf.insert(buf.end(), hdr_bytes, hdr_bytes + sizeof(hdr));

for (char ch : name) {
buf.push_back(static_cast<uint8_t>(ch));
}
buf.push_back(include_nul ? 0U : static_cast<uint8_t>('X'));

while ((buf.size() & 3U) != 0U) {
buf.push_back(0U);
}

if (payload != nullptr && payload_size > 0U) {
const auto* data = static_cast<const uint8_t*>(payload);
buf.insert(buf.end(), data, data + payload_size);
while ((buf.size() & 3U) != 0U) {
buf.push_back(0U);
}
}
}

TEST_CASE("CpioArchive status strings, validation, lookup, and error paths", "[fs][cpio]") {
CHECK(std::strcmp(cpio_status_str(CpioStatus::Success), "Success") == 0);
CHECK(std::strcmp(cpio_status_str(CpioStatus::ArchiveTooSmall), "ArchiveTooSmall") == 0);
CHECK(std::strcmp(cpio_status_str(CpioStatus::InvalidMagic), "InvalidMagic") == 0);
CHECK(std::strcmp(cpio_status_str(CpioStatus::MalformedHeader), "MalformedHeader") == 0);
CHECK(std::strcmp(cpio_status_str(CpioStatus::CorruptedPadding), "CorruptedPadding") == 0);
CHECK(std::strcmp(cpio_status_str(CpioStatus::PathTooLong), "PathTooLong") == 0);
CHECK(std::strcmp(cpio_status_str(CpioStatus::FileNotFound), "FileNotFound") == 0);
CHECK(std::strcmp(cpio_status_str(CpioStatus::UnexpectedEndOfArchive),
"UnexpectedEndOfArchive") == 0);

CpioEntry entry{};
CHECK_FALSE(CpioArchive(nullptr, 128U).is_valid());
CHECK_FALSE(CpioArchive("070701", 6U).is_valid());
CHECK(cpio_lookup(nullptr, 0U, "a", entry) == CpioStatus::ArchiveTooSmall);

std::array<uint8_t, sizeof(CpioNewcHeader)> bad_hdr{};
std::memset(bad_hdr.data(), '0', bad_hdr.size());
std::memcpy(bad_hdr.data(), "070703", 6U);
CHECK_FALSE(CpioArchive(bad_hdr.data(), bad_hdr.size()).is_valid());
CHECK(cpio_lookup(bad_hdr.data(), bad_hdr.size(), "a", entry) == CpioStatus::InvalidMagic);

std::memcpy(bad_hdr.data(), "07070X", 6U);
CHECK_FALSE(CpioArchive(bad_hdr.data(), bad_hdr.size()).is_valid());
std::memcpy(bad_hdr.data(), "070702", 6U);
CHECK(CpioArchive(bad_hdr.data(), bad_hdr.size()).is_valid());
CHECK(cpio_lookup(bad_hdr.data(), bad_hdr.size(), nullptr, entry) == CpioStatus::FileNotFound);
CHECK(cpio_lookup(bad_hdr.data(), bad_hdr.size(), "", entry) == CpioStatus::FileNotFound);

// Valid archive with dir, regular file (070702), symlink, and TRAILER!!!
std::vector<uint8_t> archive;
append_cpio_entry(archive, CPIO_NEWC_MAGIC_NOCRC, "bin", CPIO_MODE_DIR | 0755U, nullptr, 0U);
const char file_data[] = "hello";
append_cpio_entry(archive, CPIO_NEWC_MAGIC_CRC, "./bin/hello", CPIO_MODE_REGULAR | 0644U,
file_data, 5U);
const char link_target[] = "/bin/hello";
append_cpio_entry(archive, CPIO_NEWC_MAGIC_NOCRC, "bin/hi", CPIO_MODE_SYMLINK | 0777U,
link_target, 10U);
append_cpio_entry(archive, CPIO_NEWC_MAGIC_NOCRC, CPIO_TRAILER_NAME, 0U, nullptr, 0U);

CHECK(cpio_lookup(archive.data(), archive.size(), "/bin", entry) == CpioStatus::Success);
CHECK(entry.is_directory);
CHECK_FALSE(entry.is_regular_file);

CHECK(cpio_lookup(archive.data(), archive.size(), "bin/hello", entry) == CpioStatus::Success);
CHECK(entry.is_regular_file);
CHECK(entry.size == 5U);
CHECK(std::memcmp(entry.data, "hello", 5U) == 0);

CHECK(cpio_lookup(archive.data(), archive.size(), "bin/hi", entry) == CpioStatus::Success);
CHECK_FALSE(entry.is_directory);
CHECK_FALSE(entry.is_regular_file);

CHECK(cpio_lookup(archive.data(), archive.size(), "missing", entry) ==
CpioStatus::FileNotFound);

// Void callback path in for_each
size_t count = 0U;
const CpioArchive valid_arch(archive.data(), archive.size());
CHECK(valid_arch.for_each([&](const CpioEntry&) { ++count; }) == CpioStatus::Success);
CHECK(count == 3U);

// Truncated archive without TRAILER!!! propagates UnexpectedEndOfArchive
std::vector<uint8_t> no_trailer;
append_cpio_entry(no_trailer, CPIO_NEWC_MAGIC_NOCRC, "a", CPIO_MODE_REGULAR, "x", 1U);
CHECK(cpio_lookup(no_trailer.data(), no_trailer.size(), "missing", entry) ==
CpioStatus::UnexpectedEndOfArchive);
CHECK(CpioArchive(no_trailer.data(), no_trailer.size() - 1U).for_each([](const CpioEntry&) {
}) == CpioStatus::UnexpectedEndOfArchive);

// Bad hex in header
std::vector<uint8_t> bad_hex = no_trailer;
bad_hex[offsetof(CpioNewcHeader, c_namesize)] = 'z';
CHECK(CpioArchive(bad_hex.data(), bad_hex.size()).for_each([](const CpioEntry&) {}) ==
CpioStatus::MalformedHeader);

// namesize == 0 and namesize >= 4096
std::vector<uint8_t> zero_name = no_trailer;
write_hex8(reinterpret_cast<char*>(zero_name.data() + offsetof(CpioNewcHeader, c_namesize)),
0U);
CHECK(CpioArchive(zero_name.data(), zero_name.size()).for_each([](const CpioEntry&) {}) ==
CpioStatus::MalformedHeader);
write_hex8(reinterpret_cast<char*>(zero_name.data() + offsetof(CpioNewcHeader, c_namesize)),
4096U);
CHECK(CpioArchive(zero_name.data(), zero_name.size()).for_each([](const CpioEntry&) {}) ==
CpioStatus::PathTooLong);

// Non-null-terminated name
std::vector<uint8_t> no_nul;
append_cpio_entry(no_nul, CPIO_NEWC_MAGIC_NOCRC, "ab", CPIO_MODE_REGULAR, nullptr, 0U, false);
CHECK(CpioArchive(no_nul.data(), no_nul.size()).for_each([](const CpioEntry&) {}) ==
CpioStatus::MalformedHeader);

// Corrupted name padding (name "ab" -> 110 + 3 = 113 bytes + 3 bytes padding to 116)
std::vector<uint8_t> bad_pad;
append_cpio_entry(bad_pad, CPIO_NEWC_MAGIC_NOCRC, "ab", CPIO_MODE_REGULAR, nullptr, 0U);
bad_pad[sizeof(CpioNewcHeader) + 3U] = 0xAAU;
CHECK(CpioArchive(bad_pad.data(), bad_pad.size()).for_each([](const CpioEntry&) {}) ==
CpioStatus::CorruptedPadding);

// Corrupted payload padding (payload "x" -> 1 byte + 3 bytes padding)
std::vector<uint8_t> bad_pay_pad = no_trailer;
bad_pay_pad[bad_pay_pad.size() - 1U] = 0x55U;
CHECK(CpioArchive(bad_pay_pad.data(), bad_pay_pad.size()).for_each([](const CpioEntry&) {}) ==
CpioStatus::CorruptedPadding);
}

TEST_CASE("Duration conversions, ticks, timespec, and overflow guards", "[time][duration]") {
const Duration z = Duration::zero();
CHECK(z.is_zero());
CHECK(z.nanoseconds() == 0ULL);
CHECK(z.to_timer_ticks(1000ULL) == 0ULL);

const Duration us = Duration::from_microseconds(1500ULL);
CHECK_FALSE(us.is_zero());
CHECK(us.nanoseconds() == 1500000ULL);

const Duration ns = Duration::from_nanoseconds(2500000500ULL);
const linux_timespec ts = ns.to_linux_timespec();
CHECK(ts.tv_sec == 2);
CHECK(ts.tv_nsec == 500000500);

const linux_timeval tv = ns.to_linux_timeval();
CHECK(tv.tv_sec == 2);
CHECK(tv.tv_usec == 500000);

// from_ticks (runtime inputs so constexpr functions execute at runtime)
volatile uint64_t zero_u = 0ULL;
volatile uint64_t ticks_val = 1500ULL;
volatile uint64_t freq_val = 1000ULL;
CHECK(Duration::from_ticks(ticks_val, zero_u).is_zero());
CHECK(Duration::from_ticks(zero_u, freq_val).is_zero());
const Duration t_dur = Duration::from_ticks(ticks_val, freq_val);
CHECK(t_dur.nanoseconds() == 1500000000ULL);

// to_timer_ticks (including rounding up sub-second remainder)
volatile uint64_t one_ns = 1ULL;
volatile uint64_t two_s_ns = 2000000000ULL;
CHECK(Duration::from_nanoseconds(one_ns).to_timer_ticks(zero_u) == 0ULL);
CHECK(Duration::from_nanoseconds(one_ns).to_timer_ticks(freq_val) == 1ULL);
CHECK(Duration::from_nanoseconds(two_s_ns).to_timer_ticks(freq_val) == 2000ULL);

// from_linux_timespec validation & overflow
Duration out{};
volatile int64_t neg_val = -1;
volatile int64_t sec_over = INT64_MAX;
volatile int64_t nsec_bad = static_cast<int64_t>(Duration::NS_PER_SEC);
CHECK_FALSE(
Duration::from_linux_timespec(linux_timespec{.tv_sec = neg_val, .tv_nsec = 0}, out));
CHECK_FALSE(
Duration::from_linux_timespec(linux_timespec{.tv_sec = 0, .tv_nsec = neg_val}, out));
CHECK_FALSE(
Duration::from_linux_timespec(linux_timespec{.tv_sec = 0, .tv_nsec = nsec_bad}, out));
CHECK_FALSE(
Duration::from_linux_timespec(linux_timespec{.tv_sec = sec_over, .tv_nsec = 0}, out));

volatile int64_t sec_ok = 3;
volatile int64_t nsec_ok = 250;
REQUIRE(
Duration::from_linux_timespec(linux_timespec{.tv_sec = sec_ok, .tv_nsec = nsec_ok}, out));
CHECK(out.nanoseconds() == 3000000250ULL);
}

} // namespace
74 changes: 74 additions & 0 deletions tests/host/elf_interp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,77 @@ TEST_CASE("load_elf handles shared boundary page and rolls back cleanly on mappi
CHECK(overflow_res.error() == Kernel::Loader::ElfStatus::SegmentOutOfBounds);
}
}

TEST_CASE("elf_loader PT_PHDR, OOM, and invalid ELF paths", "[elf][loader]") {
init_interp_test_env();

// InvalidElf
const uint8_t bad_bytes[16]{};
auto bad_res = Kernel::Loader::load_elf(bad_bytes, sizeof(bad_bytes));
REQUIRE(bad_res.is_err());
CHECK(bad_res.error() == Kernel::Loader::ElfLoaderError::InvalidElf);

// NoLoadSegments (valid ET_EXEC header with 1 PT_NULL phdr, 0 PT_LOAD)
std::array<uint8_t, 0x200> zero_load{};
auto* ehdr = reinterpret_cast<Kernel::Loader::Elf64_Ehdr*>(zero_load.data());
ehdr->e_ident[0] = 0x7FU;
ehdr->e_ident[1] = 'E';
ehdr->e_ident[2] = 'L';
ehdr->e_ident[3] = 'F';
ehdr->e_ident[4] = Kernel::Loader::ELFCLASS64;
ehdr->e_ident[5] = Kernel::Loader::ELFDATA2LSB;
ehdr->e_ident[6] = Kernel::Loader::EV_CURRENT;
ehdr->e_type = Kernel::Loader::ET_EXEC;
ehdr->e_machine = HAL::get_platform_info().elf_machine;
ehdr->e_version = 1U;
ehdr->e_entry = 0x400000ULL;
ehdr->e_phoff = sizeof(Kernel::Loader::Elf64_Ehdr);
ehdr->e_ehsize = sizeof(Kernel::Loader::Elf64_Ehdr);
ehdr->e_phentsize = sizeof(Kernel::Loader::Elf64_Phdr);
ehdr->e_phnum = 1U;
auto no_seg_res = Kernel::Loader::load_elf(zero_load.data(), zero_load.size());
REQUIRE(no_seg_res.is_err());
CHECK(no_seg_res.error() == Kernel::Loader::ElfLoaderError::NoLoadSegments);

// PT_PHDR + PT_LOAD + OutOfMemory (fail_create_root)
ehdr->e_phnum = 2U;
auto* phdrs = reinterpret_cast<Kernel::Loader::Elf64_Phdr*>(zero_load.data() + ehdr->e_phoff);
phdrs[0].p_type = Kernel::Loader::PT_PHDR;
phdrs[0].p_offset = ehdr->e_phoff;
phdrs[0].p_vaddr = 0x400040ULL;
phdrs[0].p_filesz = 2U * sizeof(Kernel::Loader::Elf64_Phdr);
phdrs[0].p_memsz = 2U * sizeof(Kernel::Loader::Elf64_Phdr);
phdrs[0].p_align = 8U;

phdrs[1].p_type = Kernel::Loader::PT_LOAD;
phdrs[1].p_flags = Kernel::Loader::PF_R | Kernel::Loader::PF_X;
phdrs[1].p_offset = 0U;
phdrs[1].p_vaddr = 0x400000ULL;
phdrs[1].p_filesz = 0x100U;
phdrs[1].p_memsz = 0x100U;
phdrs[1].p_align = 0x1000U;

HostTest::set_fail_create_root(true);
auto oom_res = Kernel::Loader::load_elf(zero_load.data(), zero_load.size());
HostTest::set_fail_create_root(false);
REQUIRE(oom_res.is_err());
CHECK(oom_res.error() == Kernel::Loader::ElfLoaderError::OutOfMemory);

auto ok_res = Kernel::Loader::load_elf(zero_load.data(), zero_load.size());
REQUIRE(ok_res.is_ok());
CHECK(ok_res.value().phdr_vaddr == 0x400040ULL);

// load_elf_interpreter with non-PIE ET_EXEC interpreter
std::array<uint8_t, 0x200> interp_exec{};
std::memcpy(interp_exec.data(), zero_load.data(), zero_load.size());
auto* interp_phdrs = reinterpret_cast<Kernel::Loader::Elf64_Phdr*>(
interp_exec.data() + sizeof(Kernel::Loader::Elf64_Ehdr));
interp_phdrs[0].p_vaddr = 0x600040ULL;
interp_phdrs[1].p_vaddr = 0x600000ULL;
auto interp_res = Kernel::Loader::load_elf_interpreter(
interp_exec.data(), interp_exec.size(), ok_res.value().address_space, 0x500000ULL);
REQUIRE(interp_res.is_ok());
CHECK(interp_res.value().base_addr == 0x600000ULL);

ok_res.value().address_space.destroy();
}
Loading
Loading