kh-runtime 0.1.2

Kakehashi native ARM64 execution context, memory layout, traps, and bottle
Documentation
//! Path and stat-related BSD syscalls.

use crate::bottle::{self, HOST_PATH_STACK, PathError, translate_path_cstr};
use crate::host;
use crate::mem::registry_check_range;

use super::common::{
    EBADF, EEXIST, EFAULT, EINVAL, ENOENT, EPERM, SyscallArgs, SyscallResult, guest_write, reg_as_i32,
    reg_as_i64,
};
use super::fd::guest_to_host_fd;

/// Darwin `struct stat64` size (XNU).
pub(crate) const DARWIN_STAT64_SIZE: usize = 144;

/// Darwin `F_OK` for access.
const F_OK: libc::c_int = 0;

/// Map a path syscall through stack CStr translation.
fn with_host_path(
    path_ptr: u64,
    name: &'static str,
    f: impl FnOnce(&std::ffi::CStr) -> SyscallResult,
) -> SyscallResult {
    if !registry_check_range(path_ptr, 1, false) {
        return SyscallResult::err(name, EFAULT);
    }
    let result = bottle::with_guest_path_bytes(path_ptr, 4096, |bytes| {
        let Ok(path) = std::str::from_utf8(bytes) else {
            return SyscallResult::err(name, EFAULT);
        };
        let mut host_buf = [0_u8; HOST_PATH_STACK];
        match translate_path_cstr(path, &mut host_buf) {
            Ok(c) => f(c),
            Err(PathError::TooLong) => {
                let Ok(host_path) = bottle::translate_path(path) else {
                    return SyscallResult::err(name, ENOENT);
                };
                let Ok(c) = std::ffi::CString::new(host_path.as_os_str().as_encoded_bytes()) else {
                    return SyscallResult::err(name, EFAULT);
                };
                f(&c)
            }
            Err(_) => SyscallResult::err(name, ENOENT),
        }
    });
    result.unwrap_or_else(|| SyscallResult::err(name, EFAULT))
}

/// `access`.
pub(crate) fn handle_access(args: SyscallArgs) -> SyscallResult {
    let name = "access";
    with_host_path(args.x0, name, |c_path| {
        if host::access_path(c_path, F_OK) {
            SyscallResult::ok(name, 0)
        } else {
            SyscallResult::err(name, ENOENT)
        }
    })
}

/// `stat` / `stat64` — path `x0`, buffer `x1`.
pub(crate) fn handle_stat(args: SyscallArgs) -> SyscallResult {
    path_stat(args, false, "stat")
}

/// `lstat` / `lstat64` — path `x0`, buffer `x1` (do not follow symlinks).
pub(crate) fn handle_lstat(args: SyscallArgs) -> SyscallResult {
    path_stat(args, true, "lstat")
}

fn path_stat(args: SyscallArgs, no_follow: bool, name: &'static str) -> SyscallResult {
    if !registry_check_range(args.x1, DARWIN_STAT64_SIZE, true) {
        return SyscallResult::err(name, EFAULT);
    }
    with_host_path(args.x0, name, |c_path| {
        let st = if no_follow {
            host::lstat_path(c_path)
        } else {
            host::stat_path(c_path)
        };
        let Some(st) = st else {
            return SyscallResult::err(name, ENOENT);
        };
        write_darwin_stat64_from_libc(args.x1, &st, name)
    })
}

/// `fstat` / `fstat64` — fd `x0`, buffer `x1`.
pub(crate) fn handle_fstat(args: SyscallArgs) -> SyscallResult {
    let name = "fstat";
    let Some(host_fd) = guest_to_host_fd(args.x0) else {
        return SyscallResult::err(name, EBADF);
    };
    if !registry_check_range(args.x1, DARWIN_STAT64_SIZE, true) {
        return SyscallResult::err(name, EFAULT);
    }
    let Some(st) = host::fstat_fd(host_fd) else {
        return SyscallResult::err(name, EBADF);
    };
    write_darwin_stat64_from_libc(args.x1, &st, name)
}

/// Darwin `AT_FDCWD` for `fstatat`.
const AT_FDCWD: i32 = -2;
/// Darwin `AT_SYMLINK_NOFOLLOW`.
const AT_SYMLINK_NOFOLLOW: i32 = 0x0020;

/// `fstatat` — dirfd `x0`, path `x1`, buf `x2`, flag `x3`.
pub(crate) fn handle_fstatat(args: SyscallArgs) -> SyscallResult {
    let name = "fstatat";
    let dirfd = reg_as_i32(args.x0);
    let flag = reg_as_i32(args.x3);
    if !registry_check_range(args.x1, 1, false) {
        return SyscallResult::err(name, EFAULT);
    }
    if !registry_check_range(args.x2, DARWIN_STAT64_SIZE, true) {
        return SyscallResult::err(name, EFAULT);
    }
    let Some(path) = bottle::read_c_string(args.x1, 4096) else {
        return SyscallResult::err(name, EFAULT);
    };

    let no_follow = flag & AT_SYMLINK_NOFOLLOW != 0;
    let host_flags = if no_follow {
        libc::AT_SYMLINK_NOFOLLOW
    } else {
        0
    };

    // Absolute / AT_FDCWD: translate then stat/lstat.
    if dirfd == AT_FDCWD || path.starts_with('/') {
        let mut host_buf = [0_u8; HOST_PATH_STACK];
        let c_path = match translate_path_cstr(&path, &mut host_buf) {
            Ok(c) => c,
            Err(PathError::TooLong) => {
                let Ok(host_path) = bottle::translate_path(&path) else {
                    return SyscallResult::err(name, ENOENT);
                };
                let Ok(c) = std::ffi::CString::new(host_path.as_os_str().as_encoded_bytes()) else {
                    return SyscallResult::err(name, EFAULT);
                };
                let st = if no_follow {
                    host::lstat_path(&c)
                } else {
                    host::stat_path(&c)
                };
                return match st {
                    Some(st) => write_darwin_stat64_from_libc(args.x2, &st, name),
                    None => SyscallResult::err(name, ENOENT),
                };
            }
            Err(_) => return SyscallResult::err(name, ENOENT),
        };
        let st = if no_follow {
            host::lstat_path(c_path)
        } else {
            host::stat_path(c_path)
        };
        return match st {
            Some(st) => write_darwin_stat64_from_libc(args.x2, &st, name),
            None => SyscallResult::err(name, ENOENT),
        };
    }

    // Relative to dirfd: one libc fstatat (no /proc/self/fd readlink).
    let Some(host_dir) = guest_to_host_fd(args.x0) else {
        return SyscallResult::err(name, EBADF);
    };
    let Ok(c_path) = std::ffi::CString::new(path) else {
        return SyscallResult::err(name, EFAULT);
    };
    let Some(st) = host::fstatat_path(host_dir, &c_path, host_flags) else {
        return SyscallResult::err(name, ENOENT);
    };
    write_darwin_stat64_from_libc(args.x2, &st, name)
}

/// `unlink` — path `x0`.
pub(crate) fn handle_unlink(args: SyscallArgs) -> SyscallResult {
    let name = "unlink";
    with_host_path(args.x0, name, |c_path| match host::unlink_path(c_path) {
        Ok(()) => SyscallResult::ok(name, 0),
        Err(2) => SyscallResult::err(name, ENOENT),
        Err(_) => SyscallResult::err(name, EPERM),
    })
}

/// `mkdir` — path `x0`, mode `x1` (mode currently ignored; host umask applies).
pub(crate) fn handle_mkdir(args: SyscallArgs) -> SyscallResult {
    let name = "mkdir";
    let mode = u32::try_from(args.x1 & 0xFFFF).unwrap_or(0o777);
    let host_mode = libc::mode_t::try_from(mode).unwrap_or(0o777);
    with_host_path(args.x0, name, |c_path| {
        match host::mkdir_path(c_path, host_mode) {
            Ok(()) => SyscallResult::ok(name, 0),
            Err(17) => SyscallResult::err(name, EEXIST),
            Err(2) => SyscallResult::err(name, ENOENT),
            Err(_) => SyscallResult::err(name, EPERM),
        }
    })
}

/// `rmdir` — path `x0`.
pub(crate) fn handle_rmdir(args: SyscallArgs) -> SyscallResult {
    let name = "rmdir";
    with_host_path(args.x0, name, |c_path| match host::rmdir_path(c_path) {
        Ok(()) => SyscallResult::ok(name, 0),
        Err(2) => SyscallResult::err(name, ENOENT),
        Err(_) => SyscallResult::err(name, EPERM),
    })
}

/// `rename` — from `x0`, to `x1`.
pub(crate) fn handle_rename(args: SyscallArgs) -> SyscallResult {
    let name = "rename";
    if !registry_check_range(args.x0, 1, false) || !registry_check_range(args.x1, 1, false) {
        return SyscallResult::err(name, EFAULT);
    }
    let Some(from) = bottle::read_c_string(args.x0, 4096) else {
        return SyscallResult::err(name, EFAULT);
    };
    let Some(to) = bottle::read_c_string(args.x1, 4096) else {
        return SyscallResult::err(name, EFAULT);
    };
    let mut from_buf = [0_u8; HOST_PATH_STACK];
    let mut to_buf = [0_u8; HOST_PATH_STACK];
    let Ok(c_from) = translate_path_cstr(&from, &mut from_buf) else {
        return SyscallResult::err(name, ENOENT);
    };
    // Separate stack buffers so both CStrs stay live for rename.
    let Ok(c_to) = translate_path_cstr(&to, &mut to_buf) else {
        return SyscallResult::err(name, ENOENT);
    };
    match host::rename_path(c_from, c_to) {
        Ok(()) => SyscallResult::ok(name, 0),
        Err(2) => SyscallResult::err(name, ENOENT),
        Err(_) => SyscallResult::err(name, EPERM),
    }
}

/// `ftruncate` — fd `x0`, length `x1`.
pub(crate) fn handle_ftruncate(args: SyscallArgs) -> SyscallResult {
    let name = "ftruncate";
    let Some(host_fd) = guest_to_host_fd(args.x0) else {
        return SyscallResult::err(name, EBADF);
    };
    let len = reg_as_i64(args.x1);
    if len < 0 {
        return SyscallResult::err(name, EINVAL);
    }
    match host::ftruncate_fd(host_fd, len) {
        Some(()) => SyscallResult::ok(name, 0),
        None => SyscallResult::err(name, EBADF),
    }
}

/// `fsync` — fd `x0`.
pub(crate) fn handle_fsync(args: SyscallArgs) -> SyscallResult {
    let name = "fsync";
    let Some(host_fd) = guest_to_host_fd(args.x0) else {
        return SyscallResult::err(name, EBADF);
    };
    match host::fsync_fd(host_fd) {
        Some(()) => SyscallResult::ok(name, 0),
        None => SyscallResult::err(name, EBADF),
    }
}

fn write_darwin_stat64_from_libc(
    buf_addr: u64,
    st: &libc::stat,
    name: &'static str,
) -> SyscallResult {
    let mut raw = [0_u8; DARWIN_STAT64_SIZE];
    fill_stat64_bytes(
        &mut raw,
        &Stat64Fields {
            dev: narrow_i32(st.st_dev),
            mode: narrow_u16(st.st_mode),
            nlink: narrow_u16(st.st_nlink),
            ino: st.st_ino,
            uid: st.st_uid,
            gid: st.st_gid,
            rdev: narrow_i32(st.st_rdev),
            atime: st.st_atime,
            mtime: st.st_mtime,
            ctime: st.st_ctime,
            size: st.st_size,
            blocks: st.st_blocks,
            blksize: narrow_i32(st.st_blksize),
        },
    );
    guest_write(buf_addr, &raw);
    SyscallResult::ok(name, 0)
}

/// Narrow a host `libc` integer field to `i32` without host-specific casts.
fn narrow_i32<T: Copy + TryInto<i64>>(v: T) -> i32 {
    let wide = v.try_into().unwrap_or(0_i64);
    i32::try_from(wide).unwrap_or(if wide.is_negative() {
        i32::MIN
    } else {
        i32::MAX
    })
}

/// Narrow a host `libc` integer field to `u16`.
fn narrow_u16<T: Copy + TryInto<u64>>(v: T) -> u16 {
    let wide = v.try_into().unwrap_or(0_u64);
    u16::try_from(wide & 0xFFFF).unwrap_or(0)
}

/// Fields packed into Darwin `struct stat64` (144 bytes, little-endian).
#[derive(Clone, Copy)]
struct Stat64Fields {
    dev: i32,
    mode: u16,
    nlink: u16,
    ino: u64,
    uid: u32,
    gid: u32,
    rdev: i32,
    atime: i64,
    mtime: i64,
    ctime: i64,
    size: i64,
    blocks: i64,
    blksize: i32,
}

/// Packs a Darwin `struct stat64` (144 bytes) little-endian.
fn fill_stat64_bytes(raw: &mut [u8; DARWIN_STAT64_SIZE], f: &Stat64Fields) {
    put_i32(raw, 0, f.dev);
    put_u16(raw, 4, f.mode);
    put_u16(raw, 6, f.nlink);
    put_u64(raw, 8, f.ino);
    put_u32(raw, 16, f.uid);
    put_u32(raw, 20, f.gid);
    put_i32(raw, 24, f.rdev);
    put_i64(raw, 32, f.atime);
    put_i64(raw, 40, 0);
    put_i64(raw, 48, f.mtime);
    put_i64(raw, 56, 0);
    put_i64(raw, 64, f.ctime);
    put_i64(raw, 72, 0);
    put_i64(raw, 80, 0);
    put_i64(raw, 88, 0);
    put_i64(raw, 96, f.size);
    put_i64(raw, 104, f.blocks);
    put_i32(raw, 112, f.blksize);
    put_u32(raw, 116, 0);
    put_u32(raw, 120, 0);
}

fn put_u16(buf: &mut [u8], off: usize, v: u16) {
    if let Some(slot) = buf.get_mut(off..off.saturating_add(2)) {
        slot.copy_from_slice(&v.to_le_bytes());
    }
}

fn put_u32(buf: &mut [u8], off: usize, v: u32) {
    if let Some(slot) = buf.get_mut(off..off.saturating_add(4)) {
        slot.copy_from_slice(&v.to_le_bytes());
    }
}

fn put_i32(buf: &mut [u8], off: usize, v: i32) {
    if let Some(slot) = buf.get_mut(off..off.saturating_add(4)) {
        slot.copy_from_slice(&v.to_le_bytes());
    }
}

fn put_u64(buf: &mut [u8], off: usize, v: u64) {
    if let Some(slot) = buf.get_mut(off..off.saturating_add(8)) {
        slot.copy_from_slice(&v.to_le_bytes());
    }
}

fn put_i64(buf: &mut [u8], off: usize, v: i64) {
    if let Some(slot) = buf.get_mut(off..off.saturating_add(8)) {
        slot.copy_from_slice(&v.to_le_bytes());
    }
}