kh-runtime 0.1.5

Kakehashi native ARM64 execution context, memory layout, traps, and bottle
Documentation
//! BSD `read` / `write`.

use crate::mem::registry_check_range;

use super::common::{
    EBADF, EFAULT, EPERM, SyscallArgs, SyscallResult, guest_slice, guest_slice_mut,
};
use super::fd::{guest_to_host_fd, read_host_fd, write_host_fd};

fn io_note(msg: &str) {
    use std::sync::atomic::{AtomicU32, Ordering};
    static N: AtomicU32 = AtomicU32::new(0);
    if N.fetch_add(1, Ordering::Relaxed) >= 96 {
        return;
    }
    let line = format!("kh: io {msg}\n");
    drop(std::io::Write::write_all(
        &mut std::io::stderr(),
        line.as_bytes(),
    ));
}

/// `write`.
pub(crate) fn handle_write(args: SyscallArgs) -> SyscallResult {
    let name = "write";
    let gfd = super::common::reg_as_i32(args.x0);
    if gfd > 2 {
        io_note(&format!("write gfd={gfd} len={}", args.x2));
    }
    let Some(host_fd) = guest_to_host_fd(args.x0) else {
        log_io_fail("write", "EBADF", args.x0, args.x1, args.x2);
        return SyscallResult::err(name, EBADF);
    };
    let Ok(len) = usize::try_from(args.x2) else {
        return SyscallResult::err(name, EFAULT);
    };
    if len == 0 {
        return SyscallResult::ok(name, 0);
    }
    // Cap ridiculous lengths; full registry walk is TLS-cached on sequential I/O.
    if len > (1 << 30) || !registry_check_range(args.x1, len, false) {
        log_io_fail("write", "EFAULT", args.x0, args.x1, args.x2);
        return SyscallResult::err(name, EFAULT);
    }

    let slice = guest_slice(args.x1, len);
    match write_host_fd(host_fd, slice) {
        Ok(n) => SyscallResult::ok(name, u64::try_from(n).unwrap_or(0)),
        Err(e) => {
            log_io_fail("write", &format!("EPERM({e})"), args.x0, args.x1, args.x2);
            SyscallResult::err(name, EPERM)
        }
    }
}

fn log_io_fail(op: &str, why: &str, x0: u64, x1: u64, x2: u64) {
    use std::sync::atomic::{AtomicU32, Ordering};
    static N: AtomicU32 = AtomicU32::new(0);
    if N.fetch_add(1, Ordering::Relaxed) >= 24 {
        return;
    }
    let msg = format!("kh: {op} fail {why} x0={x0:#x} buf={x1:#x} len={x2:#x}\n");
    drop(std::io::Write::write_all(
        &mut std::io::stderr(),
        msg.as_bytes(),
    ));
}

/// `read`.
pub(crate) fn handle_read(args: SyscallArgs) -> SyscallResult {
    let name = "read";
    let gfd = super::common::reg_as_i32(args.x0);
    if gfd > 2 {
        io_note(&format!("read gfd={gfd} len={} (may block)", args.x2));
    }
    let Some(host_fd) = guest_to_host_fd(args.x0) else {
        return SyscallResult::err(name, EBADF);
    };
    let Ok(len) = usize::try_from(args.x2) else {
        return SyscallResult::err(name, EFAULT);
    };
    if len == 0 {
        return SyscallResult::ok(name, 0);
    }
    if len > (1 << 30) || !registry_check_range(args.x1, len, true) {
        return SyscallResult::err(name, EFAULT);
    }

    // Read straight into guest memory (identity map) — no intermediate heap
    // buffer. Double-copy + `Vec` was a major cost on multi‑MiB archive I/O.
    let buf = guest_slice_mut(args.x1, len);
    match read_host_fd(host_fd, buf) {
        Ok(nread) => {
            if gfd > 2 {
                io_note(&format!("read gfd={gfd} -> {nread}"));
            }
            SyscallResult::ok(name, u64::try_from(nread).unwrap_or(0))
        }
        Err(e) => {
            let os = e.raw_os_error().unwrap_or(0);
            // Map Linux EAGAIN/EWOULDBLOCK → Darwin EAGAIN (35).
            if os == libc::EAGAIN || os == libc::EWOULDBLOCK {
                if gfd > 2 {
                    io_note(&format!("read gfd={gfd} EAGAIN"));
                }
                return SyscallResult::err(name, 35);
            }
            if gfd > 2 {
                io_note(&format!("read gfd={gfd} err={os}"));
            }
            SyscallResult::err(name, EPERM)
        }
    }
}