Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

30.4 KB
Raw Blame
//! File copy operations for the BoxLite C SDK.

use std::io;
use std::os::raw::{c_char, c_void};
use std::path::PathBuf;
use std::sync::Arc;

use boxlite::litebox::copy::CopyOptions;
use boxlite::{BoxByteStream, BoxliteError};
use futures::StreamExt;
use tokio::runtime::Runtime as TokioRuntime;
use tokio::sync::mpsc;

use crate::box_handle::BoxHandle;
use crate::error::{BoxliteErrorCode, FFIError, error_to_code, null_pointer_error, write_error};
use crate::event_queue::{CBoxCopyCb, RuntimeEvent, push_event};
use crate::util::c_str_to_string;
use crate::{CBoxHandle, CBoxliteError};

#[unsafe(no_mangle)]
pub unsafe extern "C" fn boxlite_copy_into(
    handle: *mut CBoxHandle,
    host_src: *const c_char,
    guest_dst: *const c_char,
    cb: CBoxCopyCb,
    user_data: *mut c_void,
    out_error: *mut CBoxliteError,
) -> BoxliteErrorCode {
    box_copy_into(handle, host_src, guest_dst, cb, user_data, out_error)
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn boxlite_copy_out(
    handle: *mut CBoxHandle,
    guest_src: *const c_char,
    host_dst: *const c_char,
    cb: CBoxCopyCb,
    user_data: *mut c_void,
    out_error: *mut CBoxliteError,
) -> BoxliteErrorCode {
    box_copy_out(handle, guest_src, host_dst, cb, user_data, out_error)
}

unsafe fn box_copy_into(
    handle: *mut BoxHandle,
    host_src: *const c_char,
    guest_dst: *const c_char,
    cb: CBoxCopyCb,
    user_data: *mut c_void,
    out_error: *mut FFIError,
) -> BoxliteErrorCode {
    unsafe {
        if handle.is_null() {
            write_error(out_error, null_pointer_error("handle"));
            return BoxliteErrorCode::InvalidArgument;
        }

        let src = match c_str_to_string(host_src) {
            Ok(s) => PathBuf::from(s),
            Err(e) => {
                write_error(out_error, e);
                return BoxliteErrorCode::InvalidArgument;
            }
        };
        let dst = match c_str_to_string(guest_dst) {
            Ok(s) => s,
            Err(e) => {
                write_error(out_error, e);
                return BoxliteErrorCode::InvalidArgument;
            }
        };
        let cb = crate::unwrap_cb_or_return!(cb, out_error);

        let handle_ref = &*handle;
        let lite = handle_ref.handle.clone();
        let queue = handle_ref.queue.clone();
        let user_data_addr = user_data as usize;

        handle_ref.tokio_rt.spawn(async move {
            let result = lite.copy_into(src, dst, CopyOptions::default()).await;
            push_event(
                &queue,
                RuntimeEvent::Copy {
                    cb,
                    user_data: user_data_addr,
                    result,
                },
            )
            .await;
        });

        BoxliteErrorCode::Ok
    }
}

unsafe fn box_copy_out(
    handle: *mut BoxHandle,
    guest_src: *const c_char,
    host_dst: *const c_char,
    cb: CBoxCopyCb,
    user_data: *mut c_void,
    out_error: *mut FFIError,
) -> BoxliteErrorCode {
    unsafe {
        if handle.is_null() {
            write_error(out_error, null_pointer_error("handle"));
            return BoxliteErrorCode::InvalidArgument;
        }

        let src = match c_str_to_string(guest_src) {
            Ok(s) => s,
            Err(e) => {
                write_error(out_error, e);
                return BoxliteErrorCode::InvalidArgument;
            }
        };
        let dst = match c_str_to_string(host_dst) {
            Ok(s) => PathBuf::from(s),
            Err(e) => {
                write_error(out_error, e);
                return BoxliteErrorCode::InvalidArgument;
            }
        };
        let cb = crate::unwrap_cb_or_return!(cb, out_error);

        let handle_ref = &*handle;
        let lite = handle_ref.handle.clone();
        let queue = handle_ref.queue.clone();
        let user_data_addr = user_data as usize;

        handle_ref.tokio_rt.spawn(async move {
            let result = lite.copy_out(src, dst, CopyOptions::default()).await;
            push_event(
                &queue,
                RuntimeEvent::Copy {
                    cb,
                    user_data: user_data_addr,
                    result,
                },
            )
            .await;
        });

        BoxliteErrorCode::Ok
    }
}

// ─── Streaming copy ────────────────────────────────────────────────────────

/// Opaque handle for a streaming copy-in (push archive bytes into the guest).
///
/// The lifecycle is fixed, and the third step is not optional:
///
///     boxlite_copy_in_start
///       boxlite_copy_in_write   (repeat)
///       boxlite_copy_in_close   on success
///       boxlite_copy_in_abort   when the source read failed
///     boxlite_copy_in_free
///
/// Going straight from write to free is what makes a truncated upload
/// dangerous: freeing the handle drops the channel, which the guest reads
/// as a clean EOF and commits. Only boxlite_copy_in_abort turns a
/// mid-transfer source failure into a terminal error the guest can refuse.
pub struct CBoxCopyInStream {
    tx: Option<mpsc::Sender<io::Result<Vec<u8>>>>,
    tokio_rt: Arc<TokioRuntime>,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum CopyOutStreamState {
    Open,
    Eof,
    Failed,
}

/// Opaque handle for a streaming copy-out (pull archive bytes from the guest).
pub struct CBoxCopyOutStream {
    bytes: BoxByteStream,
    tokio_rt: Arc<TokioRuntime>,
    pending: Vec<u8>,
    pending_offset: usize,
    state: CopyOutStreamState,
}

impl CBoxCopyOutStream {
    fn new(bytes: BoxByteStream, tokio_rt: Arc<TokioRuntime>) -> Self {
        Self {
            bytes,
            tokio_rt,
            pending: Vec::new(),
            pending_offset: 0,
            state: CopyOutStreamState::Open,
        }
    }

    fn copy_pending(&mut self, dst: &mut [u8]) -> Option<usize> {
        if self.pending_offset == self.pending.len() {
            self.pending.clear();
            self.pending_offset = 0;
            return None;
        }

        let count = dst
            .len()
            .min(self.pending.len().saturating_sub(self.pending_offset));
        let end = self.pending_offset + count;
        dst[..count].copy_from_slice(&self.pending[self.pending_offset..end]);
        self.pending_offset = end;
        if self.pending_offset == self.pending.len() {
            self.pending.clear();
            self.pending_offset = 0;
        }
        Some(count)
    }

    fn read_into(&mut self, dst: &mut [u8]) -> Result<usize, BoxliteError> {
        debug_assert!(!dst.is_empty());
        match self.state {
            CopyOutStreamState::Eof => return Ok(0),
            CopyOutStreamState::Failed => {
                return Err(BoxliteError::InvalidState(
                    "copy-out stream has failed".to_string(),
                ));
            }
            CopyOutStreamState::Open => {}
        }

        loop {
            if let Some(count) = self.copy_pending(dst) {
                return Ok(count);
            }

            let tokio_rt = Arc::clone(&self.tokio_rt);
            match tokio_rt.block_on(self.bytes.next()) {
                Some(Ok(bytes)) if bytes.is_empty() => continue,
                Some(Ok(bytes)) => {
                    self.pending = bytes;
                    self.pending_offset = 0;
                }
                Some(Err(error)) => {
                    self.state = CopyOutStreamState::Failed;
                    return Err(BoxliteError::Internal(format!(
                        "copy_out stream error: {error}"
                    )));
                }
                None => {
                    self.state = CopyOutStreamState::Eof;
                    return Ok(0);
                }
            }
        }
    }
}

/// Begin downloading `guest_src` as a pull-based raw archive stream.
///
/// This call blocks until the stream is ready. On success the returned handle
/// must be released with [`boxlite_copy_out_free`]. The archive's first entry
/// tells the receiver how it lands.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn boxlite_copy_out_start(
    handle: *mut CBoxHandle,
    guest_src: *const c_char,
    out_error: *mut CBoxliteError,
) -> *mut CBoxCopyOutStream {
    unsafe {
        if handle.is_null() {
            write_error(out_error, null_pointer_error("handle"));
            return std::ptr::null_mut();
        }
        if guest_src.is_null() {
            write_error(out_error, null_pointer_error("guest_src"));
            return std::ptr::null_mut();
        }
        let src = match c_str_to_string(guest_src) {
            Ok(s) => s,
            Err(e) => {
                write_error(out_error, e);
                return std::ptr::null_mut();
            }
        };

        let handle_ref = &*handle;
        let lite = handle_ref.handle.clone();
        match handle_ref
            .tokio_rt
            .block_on(lite.copy_out_stream(src.as_str(), CopyOptions::default()))
        {
            Ok(bytes) => Box::into_raw(Box::new(CBoxCopyOutStream::new(
                bytes,
                handle_ref.tokio_rt.clone(),
            ))),
            Err(error) => {
                write_error(out_error, error);
                std::ptr::null_mut()
            }
        }
    }
}

/// Read the next raw archive bytes from a copy-out stream.
///
/// This call blocks while the upstream stream is pending. `Ok` with a
/// positive `out_read` returns data; `Ok` with zero length is sticky EOF. A
/// stream item error is terminal: its first read reports the stream error and
/// later reads return `InvalidState` without polling upstream again.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn boxlite_copy_out_read(
    stream: *mut CBoxCopyOutStream,
    buffer: *mut u8,
    capacity: usize,
    out_read: *mut usize,
    out_error: *mut CBoxliteError,
) -> BoxliteErrorCode {
    unsafe {
        if stream.is_null() {
            write_error(out_error, null_pointer_error("stream"));
            return BoxliteErrorCode::InvalidArgument;
        }
        if out_read.is_null() {
            write_error(out_error, null_pointer_error("out_read"));
            return BoxliteErrorCode::InvalidArgument;
        }
        *out_read = 0;
        if capacity == 0 {
            let error =
                BoxliteError::InvalidArgument("capacity must be greater than zero".to_string());
            let code = error_to_code(&error);
            write_error(out_error, error);
            return code;
        }
        if buffer.is_null() {
            write_error(out_error, null_pointer_error("buffer"));
            return BoxliteErrorCode::InvalidArgument;
        }

        let dst = std::slice::from_raw_parts_mut(buffer, capacity);
        match (&mut *stream).read_into(dst) {
            Ok(count) => {
                *out_read = count;
                BoxliteErrorCode::Ok
            }
            Err(error) => {
                let code = error_to_code(&error);
                write_error(out_error, error);
                code
            }
        }
    }
}

/// Reclaim a copy-out stream handle. A null handle is a no-op.
///
/// The caller must not invoke [`boxlite_copy_out_read`] concurrently or race
/// a read with this function.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn boxlite_copy_out_free(stream: *mut CBoxCopyOutStream) {
    unsafe {
        if !stream.is_null() {
            drop(Box::from_raw(stream));
        }
    }
}

/// Begin a streaming copy-in, returning an opaque transfer handle. The
/// guest lands the archive by its first entry.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn boxlite_copy_in_start(
    handle: *mut CBoxHandle,
    guest_dst: *const c_char,
    copy_cb: CBoxCopyCb,
    user_data: *mut c_void,
    out_error: *mut CBoxliteError,
) -> *mut CBoxCopyInStream {
    unsafe {
        boxlite_copy_in_start_with_overwrite(handle, guest_dst, true, copy_cb, user_data, out_error)
    }
}

/// Begin streaming copy-in with explicit overwrite policy. False rejects
/// conflicting entries; earlier new entries can remain after a conflict.
/// The original start function retains overwrite=true for ABI compatibility.
///
/// # Safety
///
/// Non-null `handle` and `guest_dst` must reference a live box handle and a
/// NUL-terminated string for this call. A non-null `out_error` must reference
/// writable, exclusively accessible error storage for this call. The callback
/// and any data accessed through `user_data` must stay valid until the runtime
/// dispatches the copy completion event to `copy_cb`. Handle destruction must
/// not race this call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn boxlite_copy_in_start_with_overwrite(
    handle: *mut CBoxHandle,
    guest_dst: *const c_char,
    overwrite: bool,
    copy_cb: CBoxCopyCb,
    user_data: *mut c_void,
    out_error: *mut CBoxliteError,
) -> *mut CBoxCopyInStream {
    unsafe {
        if handle.is_null() {
            write_error(out_error, null_pointer_error("handle"));
            return std::ptr::null_mut();
        }
        let dst = match c_str_to_string(guest_dst) {
            Ok(s) => s,
            Err(e) => {
                write_error(out_error, e);
                return std::ptr::null_mut();
            }
        };
        let Some(copy_cb) = copy_cb else {
            write_error(out_error, null_pointer_error("copy_cb"));
            return std::ptr::null_mut();
        };

        let handle_ref = &*handle;
        let lite = handle_ref.handle.clone();
        let queue = handle_ref.queue.clone();
        let user_data_addr = user_data as usize;

        let (tx, rx) = mpsc::channel::<io::Result<Vec<u8>>>(4);
        let bytes = futures::stream::unfold(rx, |mut rx| async move {
            rx.recv().await.map(|item| (item, rx))
        });

        handle_ref.tokio_rt.spawn(async move {
            let result = lite
                .copy_in_stream(
                    bytes,
                    dst.as_str(),
                    CopyOptions {
                        overwrite,
                        ..CopyOptions::default()
                    },
                )
                .await;
            push_event(
                &queue,
                RuntimeEvent::Copy {
                    cb: copy_cb,
                    user_data: user_data_addr,
                    result,
                },
            )
            .await;
        });

        Box::into_raw(Box::new(CBoxCopyInStream {
            tx: Some(tx),
            tokio_rt: handle_ref.tokio_rt.clone(),
        }))
    }
}

/// Push a chunk of archive bytes into the guest. Blocks when the guest is
/// slow (bounded-channel backpressure).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn boxlite_copy_in_write(
    stream: *mut CBoxCopyInStream,
    data: *const u8,
    len: usize,
    out_error: *mut CBoxliteError,
) -> BoxliteErrorCode {
    unsafe {
        if stream.is_null() {
            write_error(out_error, null_pointer_error("stream"));
            return BoxliteErrorCode::InvalidArgument;
        }
        if data.is_null() && len > 0 {
            write_error(out_error, null_pointer_error("data"));
            return BoxliteErrorCode::InvalidArgument;
        }
        if len == 0 {
            return BoxliteErrorCode::Ok;
        }

        let stream_ref = &*stream;
        let Some(tx) = stream_ref.tx.as_ref() else {
            write_error(
                out_error,
                boxlite::BoxliteError::InvalidState("copy-in stream is closed".to_string()),
            );
            return BoxliteErrorCode::InvalidState;
        };

        let bytes = std::slice::from_raw_parts(data, len).to_vec();
        match stream_ref.tokio_rt.block_on(tx.send(Ok(bytes))) {
            Ok(()) => BoxliteErrorCode::Ok,
            Err(_) => {
                write_error(
                    out_error,
                    boxlite::BoxliteError::Internal("guest upload aborted".to_string()),
                );
                BoxliteErrorCode::Internal
            }
        }
    }
}

/// Close the copy-in stream, signalling EOF to the guest. Idempotent.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn boxlite_copy_in_close(
    stream: *mut CBoxCopyInStream,
    out_error: *mut CBoxliteError,
) -> BoxliteErrorCode {
    unsafe {
        if stream.is_null() {
            write_error(out_error, null_pointer_error("stream"));
            return BoxliteErrorCode::InvalidArgument;
        }
        let stream_ref = &mut *stream;
        // Dropping the sender closes the channel, which ends the byte stream and
        // signals EOF to the guest unpacker.
        stream_ref.tx.take();
        BoxliteErrorCode::Ok
    }
}

/// Abort the copy-in stream: deliver a terminal error to the guest and then
/// close the channel. Unlike [`boxlite_copy_in_close`], the guest sees a
/// failed stream — a truncated upload can never pass as a clean EOF. Call
/// this when the source read failed mid-transfer. This is a one-shot
/// transition: after the first call (abort or close), later abort calls return
/// `InvalidState`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn boxlite_copy_in_abort(
    stream: *mut CBoxCopyInStream,
    out_error: *mut CBoxliteError,
) -> BoxliteErrorCode {
    unsafe {
        if stream.is_null() {
            write_error(out_error, null_pointer_error("stream"));
            return BoxliteErrorCode::InvalidArgument;
        }
        let stream_ref = &mut *stream;
        let Some(tx) = stream_ref.tx.as_ref() else {
            write_error(
                out_error,
                boxlite::BoxliteError::InvalidState("copy-in stream is closed".to_string()),
            );
            return BoxliteErrorCode::InvalidState;
        };

        // A failed send means the consumer is already gone, so the stream is
        // already terminal and there is nothing left to signal.
        let _ = stream_ref
            .tokio_rt
            .block_on(tx.send(Err(std::io::Error::new(
                std::io::ErrorKind::BrokenPipe,
                "copy-in aborted",
            ))));
        // Drop the sender either way: the terminal error (if the consumer is
        // still alive) precedes EOF, and no further writes are accepted.
        stream_ref.tx.take();
        BoxliteErrorCode::Ok
    }
}

/// Reclaim a copy-in stream handle.
///
/// Freeing a stream that was never closed or aborted still drops the
/// channel, so it signals EOF exactly as boxlite_copy_in_close would — the
/// guest commits whatever it received. Call boxlite_copy_in_abort first
/// whenever the transfer did not complete; see CBoxCopyInStream for the
/// full sequence.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn boxlite_copy_in_free(stream: *mut CBoxCopyInStream) {
    unsafe {
        if !stream.is_null() {
            drop(Box::from_raw(stream));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::ffi::CStr;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

    fn copy_out_stream(items: Vec<io::Result<Vec<u8>>>) -> CBoxCopyOutStream {
        let bytes: BoxByteStream = Box::pin(futures::stream::iter(items));
        CBoxCopyOutStream::new(bytes, Arc::new(tokio::runtime::Runtime::new().unwrap()))
    }

    fn assert_error_contains(
        error: &CBoxliteError,
        expected_code: BoxliteErrorCode,
        expected_message: &str,
    ) {
        assert_eq!(error.code, expected_code);
        assert!(!error.message.is_null());
        let message = unsafe { CStr::from_ptr(error.message) }.to_string_lossy();
        assert!(
            message.contains(expected_message),
            "expected {message:?} to contain {expected_message:?}"
        );
    }

    #[test]
    fn copy_in_start_validates_arguments_before_dereferencing_handle() {
        let dangling = std::ptr::NonNull::<CBoxHandle>::dangling().as_ptr();
        let invalid_utf8 = [0xffu8, 0];
        for (handle, path, code, message) in [
            (
                std::ptr::null_mut(),
                c"/app".as_ptr(),
                BoxliteErrorCode::InvalidArgument,
                "handle is null",
            ),
            (
                dangling,
                std::ptr::null(),
                BoxliteErrorCode::Internal,
                "null",
            ),
            (
                dangling,
                invalid_utf8.as_ptr().cast(),
                BoxliteErrorCode::Internal,
                "UTF-8",
            ),
            (
                dangling,
                c"/app".as_ptr(),
                BoxliteErrorCode::InvalidArgument,
                "copy_cb is null",
            ),
        ] {
            let mut error = CBoxliteError::default();
            let stream = unsafe {
                boxlite_copy_in_start_with_overwrite(
                    handle,
                    path,
                    false,
                    None,
                    std::ptr::null_mut(),
                    &mut error,
                )
            };
            assert!(stream.is_null());
            assert_error_contains(&error, code, message);
            unsafe { crate::error::boxlite_error_free(&mut error) };
        }
        let mut error = CBoxliteError::default();
        let stream = unsafe {
            boxlite_copy_in_start(
                std::ptr::null_mut(),
                c"/app".as_ptr(),
                None,
                std::ptr::null_mut(),
                &mut error,
            )
        };
        assert!(stream.is_null());
        assert_error_contains(&error, BoxliteErrorCode::InvalidArgument, "handle is null");
        unsafe { crate::error::boxlite_error_free(&mut error) };
    }

    struct DropProbe(Arc<AtomicBool>);

    impl Drop for DropProbe {
        fn drop(&mut self) {
            self.0.store(true, Ordering::SeqCst);
        }
    }

    #[test]
    fn copy_out_start_null_handle_reports_error() {
        let mut error = CBoxliteError::default();

        let stream =
            unsafe { boxlite_copy_out_start(std::ptr::null_mut(), std::ptr::null(), &mut error) };
        assert!(stream.is_null());
        assert_error_contains(&error, BoxliteErrorCode::InvalidArgument, "handle is null");
        unsafe { crate::error::boxlite_error_free(&mut error) };

        let stream = unsafe {
            boxlite_copy_out_start(std::ptr::null_mut(), std::ptr::null(), std::ptr::null_mut())
        };
        assert!(stream.is_null());
    }

    #[test]
    fn copy_out_splits_chunks_skips_empty_items_and_sticks_eof() {
        let mut stream =
            copy_out_stream(vec![Ok(Vec::new()), Ok(b"abcde".to_vec()), Ok(Vec::new())]);
        let mut dst = [0_u8; 2];

        assert_eq!(stream.read_into(&mut dst).unwrap(), 2);
        assert_eq!(&dst, b"ab");
        assert_eq!(stream.read_into(&mut dst).unwrap(), 2);
        assert_eq!(&dst, b"cd");
        assert_eq!(stream.read_into(&mut dst).unwrap(), 1);
        assert_eq!(dst[0], b'e');
        assert_eq!(stream.read_into(&mut dst).unwrap(), 0);
        assert_eq!(stream.read_into(&mut dst).unwrap(), 0);
    }

    #[test]
    fn copy_out_stream_error_is_terminal() {
        let mut stream = copy_out_stream(vec![
            Ok(vec![7]),
            Err(io::Error::new(io::ErrorKind::BrokenPipe, "guest gone")),
            Ok(vec![8]),
        ]);
        let mut dst = [0_u8; 1];

        assert_eq!(stream.read_into(&mut dst).unwrap(), 1);
        assert_eq!(dst[0], 7);
        let error = stream.read_into(&mut dst).unwrap_err();
        assert!(matches!(error, BoxliteError::Internal(message) if message.contains("guest gone")));
        assert!(matches!(
            stream.read_into(&mut dst).unwrap_err(),
            BoxliteError::InvalidState(message) if message == "copy-out stream has failed"
        ));
    }

    #[test]
    fn copy_out_polls_upstream_only_when_read_needs_data() {
        let polls = Arc::new(AtomicUsize::new(0));
        let stream_polls = Arc::clone(&polls);
        let bytes: BoxByteStream = Box::pin(futures::stream::unfold(0_u8, move |step| {
            let stream_polls = Arc::clone(&stream_polls);
            async move {
                stream_polls.fetch_add(1, Ordering::SeqCst);
                match step {
                    0 => Some((Ok(vec![1, 2, 3]), 1)),
                    _ => None,
                }
            }
        }));
        let mut stream =
            CBoxCopyOutStream::new(bytes, Arc::new(tokio::runtime::Runtime::new().unwrap()));
        let mut dst = [0_u8; 1];

        assert_eq!(polls.load(Ordering::SeqCst), 0);
        assert_eq!(stream.read_into(&mut dst).unwrap(), 1);
        assert_eq!(polls.load(Ordering::SeqCst), 1);
        assert_eq!(stream.read_into(&mut dst).unwrap(), 1);
        assert_eq!(stream.read_into(&mut dst).unwrap(), 1);
        assert_eq!(polls.load(Ordering::SeqCst), 1);
        assert_eq!(stream.read_into(&mut dst).unwrap(), 0);
        assert_eq!(polls.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn copy_out_ffi_validates_read_arguments_and_frees_null() {
        let mut stream = copy_out_stream(vec![Ok(vec![1])]);
        let mut dst = [0_u8; 1];
        let mut out_len = usize::MAX;
        let mut error = CBoxliteError::default();

        let code = unsafe {
            boxlite_copy_out_read(&mut stream, dst.as_mut_ptr(), 0, &mut out_len, &mut error)
        };
        assert_eq!(code, BoxliteErrorCode::InvalidArgument);
        assert_eq!(out_len, 0);
        assert_error_contains(&error, BoxliteErrorCode::InvalidArgument, "capacity");
        unsafe { crate::error::boxlite_error_free(&mut error) };

        let code = unsafe {
            boxlite_copy_out_read(
                &mut stream,
                std::ptr::null_mut(),
                dst.len(),
                &mut out_len,
                &mut error,
            )
        };
        assert_eq!(code, BoxliteErrorCode::InvalidArgument);
        assert_eq!(out_len, 0);
        assert_error_contains(&error, BoxliteErrorCode::InvalidArgument, "buffer is null");
        unsafe { crate::error::boxlite_error_free(&mut error) };

        let code = unsafe {
            boxlite_copy_out_read(
                &mut stream,
                dst.as_mut_ptr(),
                dst.len(),
                std::ptr::null_mut(),
                &mut error,
            )
        };
        assert_eq!(code, BoxliteErrorCode::InvalidArgument);
        assert_error_contains(
            &error,
            BoxliteErrorCode::InvalidArgument,
            "out_read is null",
        );
        unsafe { crate::error::boxlite_error_free(&mut error) };

        let code = unsafe {
            boxlite_copy_out_read(
                std::ptr::null_mut(),
                dst.as_mut_ptr(),
                dst.len(),
                &mut out_len,
                &mut error,
            )
        };
        assert_eq!(code, BoxliteErrorCode::InvalidArgument);
        assert_error_contains(&error, BoxliteErrorCode::InvalidArgument, "stream is null");
        unsafe { crate::error::boxlite_error_free(&mut error) };

        let code = unsafe {
            boxlite_copy_out_read(
                &mut stream,
                dst.as_mut_ptr(),
                dst.len(),
                &mut out_len,
                &mut error,
            )
        };
        assert_eq!(code, BoxliteErrorCode::Ok);
        assert_eq!(out_len, 1);
        assert_eq!(dst[0], 1);

        let owned = Box::into_raw(Box::new(copy_out_stream(Vec::new())));
        unsafe {
            boxlite_copy_out_free(owned);
            boxlite_copy_out_free(std::ptr::null_mut());
        }
    }

    #[test]
    fn copy_out_free_drops_active_byte_stream() {
        let dropped = Arc::new(AtomicBool::new(false));
        let probe = DropProbe(Arc::clone(&dropped));
        let bytes: BoxByteStream = Box::pin(futures::stream::poll_fn(move |_cx| {
            let _keep_alive = &probe;
            std::task::Poll::<Option<io::Result<Vec<u8>>>>::Pending
        }));
        let stream = Box::into_raw(Box::new(CBoxCopyOutStream::new(
            bytes,
            Arc::new(tokio::runtime::Runtime::new().unwrap()),
        )));

        assert!(!dropped.load(Ordering::SeqCst));
        unsafe { boxlite_copy_out_free(stream) };
        assert!(dropped.load(Ordering::SeqCst));
    }

    /// The abort entrypoint must deliver a terminal error followed by EOF,
    /// and the spent stream must reject further writes and a second abort.
    #[test]
    fn copy_in_abort_delivers_terminal_error_then_eof() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        let (tx, mut rx) = mpsc::channel(4);
        let mut stream = CBoxCopyInStream {
            tx: Some(tx),
            tokio_rt: Arc::new(rt),
        };
        let mut err = CBoxliteError::default();

        let code = unsafe { boxlite_copy_in_abort(&mut stream, &mut err) };
        assert_eq!(code, BoxliteErrorCode::Ok);

        // First item is the terminal error, then EOF (channel closed).
        let first = stream.tokio_rt.block_on(rx.recv()).expect("item");
        assert_eq!(first.unwrap_err().kind(), io::ErrorKind::BrokenPipe);
        assert!(
            stream.tokio_rt.block_on(rx.recv()).is_none(),
            "must signal EOF after the error"
        );

        // A spent stream rejects both writes and a second abort.
        let mut err2 = CBoxliteError::default();
        let data = [1u8; 4];
        let code =
            unsafe { boxlite_copy_in_write(&mut stream, data.as_ptr(), data.len(), &mut err2) };
        assert_eq!(code, BoxliteErrorCode::InvalidState);
        let mut err3 = CBoxliteError::default();
        let code = unsafe { boxlite_copy_in_abort(&mut stream, &mut err3) };
        assert_eq!(code, BoxliteErrorCode::InvalidState);
    }

    /// Pins the hazard the copy-in lifecycle doc warns about: freeing a live
    /// handle instead of aborting it drops the sender, so the consumer sees a
    /// bare EOF and cannot tell the upload was cut short. If `free` ever grows
    /// an implicit abort, this test is the one that must be revisited.
    #[test]
    fn copy_in_free_without_abort_signals_a_bare_eof() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        let (tx, mut rx) = mpsc::channel(4);
        let stream = Box::into_raw(Box::new(CBoxCopyInStream {
            tx: Some(tx),
            tokio_rt: Arc::new(rt),
        }));

        let data = [7u8; 3];
        let mut err = CBoxliteError::default();
        let code = unsafe { boxlite_copy_in_write(stream, data.as_ptr(), data.len(), &mut err) };
        assert_eq!(code, BoxliteErrorCode::Ok);

        let drain_rt = tokio::runtime::Runtime::new().unwrap();
        unsafe { boxlite_copy_in_free(stream) };

        assert_eq!(
            drain_rt.block_on(rx.recv()).expect("chunk").unwrap(),
            data.to_vec()
        );
        assert!(
            drain_rt.block_on(rx.recv()).is_none(),
            "free must close the channel"
        );
    }
}