<commit_before>#![allow(non_camel_case_types)]

pub use self::lucet_state::*;
pub use self::lucet_val::*;

use crate::alloc::Limits;
use crate::error::Error;
use crate::instance::signals::SignalBehavior;
use libc::{c_int, c_void};
use num_derive::FromPrimitive;

#[macro_export]
macro_rules! assert_nonnull {
    ( $name:ident ) => {
        if $name.is_null() {
            return lucet_error::InvalidArgument;
        }
    };
}

/// Wrap up the management of `Arc`s that go across the FFI boundary.
///
/// Trait objects must be wrapped in two `Arc`s in order to yield a thin pointer.
#[macro_export]
macro_rules! with_ffi_arcs {
    ( [ $name:ident : dyn $ty:ident ], $body:block ) => {{
        assert_nonnull!($name);
        let $name = Arc::from_raw($name as *const Arc<dyn $ty>);
        let res = $body;
        Arc::into_raw($name);
        res
    }};
    ( [ $name:ident : $ty:ty ], $body:block ) => {{
        assert_nonnull!($name);
        let $name = Arc::from_raw($name as *const $ty);
        let res = $body;
        Arc::into_raw($name);
        res
    }};
    ( [ $name:ident : dyn $ty:ident, $($tail:tt)* ], $body:block ) => {{
        assert_nonnull!($name);
        let $name = Arc::from_raw($name as *const Arc<dyn $ty>);
        let rec = with_ffi_arcs!([$($tail)*], $body);
        Arc::into_raw($name);
        rec
    }};
    ( [ $name:ident : $ty:ty, $($tail:tt)* ], $body:block ) => {{
        assert_nonnull!($name);
        let $name = Arc::from_raw($name as *const $ty);
        let rec = with_ffi_arcs!([$($tail)*], $body);
        Arc::into_raw($name);
        rec
    }};
}

/// Marker type for the `vmctx` pointer argument.
///
/// This type should only be used with [`Vmctx::from_raw()`](struct.Vmctx.html#method.from_raw) or
/// the C API.
#[repr(C)]
pub struct lucet_vmctx {
    _unused: [u8; 0],
}

#[repr(C)]
#[derive(Clone, Copy, Debug, FromPrimitive)]
pub enum lucet_error {
    Ok,
    InvalidArgument,
    RegionFull,
    Module,
    LimitsExceeded,
    NoLinearMemory,
    SymbolNotFound,
    FuncNotFound,
    RuntimeFault,
    RuntimeTerminated,
    Dl,
    Internal,
    Unsupported,
}

impl From<Error> for lucet_error {
    fn from(e: Error) -> lucet_error {
        match e {
            Error::InvalidArgument(_) => lucet_error::InvalidArgument,
            Error::RegionFull(_) => lucet_error::RegionFull,
            Error::ModuleError(_) => lucet_error::Module,
            Error::LimitsExceeded(_) => lucet_error::LimitsExceeded,
            Error::NoLinearMemory(_) => lucet_error::NoLinearMemory,
            Error::SymbolNotFound(_) => lucet_error::SymbolNotFound,
            Error::FuncNotFound(_, _) => lucet_error::FuncNotFound,
            Error::RuntimeFault(_) => lucet_error::RuntimeFault,
            Error::RuntimeTerminated(_) => lucet_error::RuntimeTerminated,
            Error::DlError(_) => lucet_error::Dl,
            Error::InternalError(_) => lucet_error::Internal,
            Error::Unsupported(_) => lucet_error::Unsupported,
        }
    }
}

pub struct lucet_instance {
    _unused: [u8; 0],
}

pub struct lucet_region {
    _unused: [u8; 0],
}

pub struct lucet_dl_module {
    _unused: [u8; 0],
}

/// Runtime limits for the various memories that back a Lucet instance.
///
/// Each value is specified in bytes, and must be evenly divisible by the host page size (4K).
#[derive(Clone, Debug)]
#[repr(C)]
pub struct lucet_alloc_limits {
    /// Max size of the heap, which can be backed by real memory. (default 1M)
    pub heap_memory_size: u64,
    /// Size of total virtual memory. (default 8G)
    pub heap_address_space_size: u64,
    /// Size of the guest stack. (default 128K)
    pub stack_size: u64,
    /// Size of the globals region in bytes; each global uses 8 bytes. (default 4K)
    pub globals_size: u64,
}

impl From<Limits> for lucet_alloc_limits {
    fn from(limits: Limits) -> lucet_alloc_limits {
        (&limits).into()
    }<commit_msg>[lucet-runtime] add repr(C) to all C API opaque structs<commit_after>#![allow(non_camel_case_types)]

pub use self::lucet_state::*;
pub use self::lucet_val::*;

use crate::alloc::Limits;
use crate::error::Error;
use crate::instance::signals::SignalBehavior;
use libc::{c_int, c_void};
use num_derive::FromPrimitive;

#[macro_export]
macro_rules! assert_nonnull {
    ( $name:ident ) => {
        if $name.is_null() {
            return lucet_error::InvalidArgument;
        }
    };
}

/// Wrap up the management of `Arc`s that go across the FFI boundary.
///
/// Trait objects must be wrapped in two `Arc`s in order to yield a thin pointer.
#[macro_export]
macro_rules! with_ffi_arcs {
    ( [ $name:ident : dyn $ty:ident ], $body:block ) => {{
        assert_nonnull!($name);
        let $name = Arc::from_raw($name as *const Arc<dyn $ty>);
        let res = $body;
        Arc::into_raw($name);
        res
    }};
    ( [ $name:ident : $ty:ty ], $body:block ) => {{
        assert_nonnull!($name);
        let $name = Arc::from_raw($name as *const $ty);
        let res = $body;
        Arc::into_raw($name);
        res
    }};
    ( [ $name:ident : dyn $ty:ident, $($tail:tt)* ], $body:block ) => {{
        assert_nonnull!($name);
        let $name = Arc::from_raw($name as *const Arc<dyn $ty>);
        let rec = with_ffi_arcs!([$($tail)*], $body);
        Arc::into_raw($name);
        rec
    }};
    ( [ $name:ident : $ty:ty, $($tail:tt)* ], $body:block ) => {{
        assert_nonnull!($name);
        let $name = Arc::from_raw($name as *const $ty);
        let rec = with_ffi_arcs!([$($tail)*], $body);
        Arc::into_raw($name);
        rec
    }};
}

/// Marker type for the `vmctx` pointer argument.
///
/// This type should only be used with [`Vmctx::from_raw()`](struct.Vmctx.html#method.from_raw) or
/// the C API.
#[repr(C)]
pub struct lucet_vmctx {
    _unused: [u8; 0],
}

#[repr(C)]
#[derive(Clone, Copy, Debug, FromPrimitive)]
pub enum lucet_error {
    Ok,
    InvalidArgument,
    RegionFull,
    Module,
    LimitsExceeded,
    NoLinearMemory,
    SymbolNotFound,
    FuncNotFound,
    RuntimeFault,
    RuntimeTerminated,
    Dl,
    Internal,
    Unsupported,
}

impl From<Error> for lucet_error {
    fn from(e: Error) -> lucet_error {
        match e {
            Error::InvalidArgument(_) => lucet_error::InvalidArgument,
            Error::RegionFull(_) => lucet_error::RegionFull,
            Error::ModuleError(_) => lucet_error::Module,
            Error::LimitsExceeded(_) => lucet_error::LimitsExceeded,
            Error::NoLinearMemory(_) => lucet_error::NoLinearMemory,
            Error::SymbolNotFound(_) => lucet_error::SymbolNotFound,
            Error::FuncNotFound(_, _) => lucet_error::FuncNotFound,
            Error::RuntimeFault(_) => lucet_error::RuntimeFault,
            Error::RuntimeTerminated(_) => lucet_error::RuntimeTerminated,
            Error::DlError(_) => lucet_error::Dl,
            Error::InternalError(_) => lucet_error::Internal,
            Error::Unsupported(_) => lucet_error::Unsupported,
        }
    }
}

#[repr(C)]
pub struct lucet_instance {
    _unused: [u8; 0],
}

#[repr(C)]
pub struct lucet_region {
    _unused: [u8; 0],
}

#[repr(C)]
pub struct lucet_dl_module {
    _unused: [u8; 0],
}

/// Runtime limits for the various memories that back a Lucet instance.
///
/// Each value is specified in bytes, and must be evenly divisible by the host page size (4K).
#[derive(Clone, Debug)]
#[repr(C)]
pub struct lucet_alloc_limits {
    /// Max size of the heap, which can be backed by real memory. (default 1M)
    pub heap_memory_size: u64,
    /// Size of total virtual memory. (default 8G)
    pub heap_address_space_size: u64,
    /// Size of the guest stack. (default 128K)
    pub stack_size: u64,
    /// Size of the globals region in bytes; each global uses 8 bytes. (default 4K)
    pub globals_size: u64,
}

impl From<Limits> for lucet_alloc_limits {
    fn from(limits: Limits) -> lucet_alloc_limits {
        (&limits).into()
    }
{'fraction_non_alphanumeric': 0.15147335423197492, 'fraction_numerical': 0.006018808777429467, 'mean_word_length': 2.9504705299653295, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4147211', 'n_tokens_mistral': 2720, 'n_tokens_neox': 2705, 'n_words': 651}