diff options
Diffstat (limited to 'third_party/rust/lucet-runtime-wasmsbx/include')
4 files changed, 433 insertions, 0 deletions
diff --git a/third_party/rust/lucet-runtime-wasmsbx/include/lucet.h b/third_party/rust/lucet-runtime-wasmsbx/include/lucet.h new file mode 100644 index 0000000000..b360784ce7 --- /dev/null +++ b/third_party/rust/lucet-runtime-wasmsbx/include/lucet.h @@ -0,0 +1,86 @@ +#ifndef LUCET_H +#define LUCET_H + +#include <signal.h> +#include <stdarg.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> + +#include "lucet_types.h" +#include "lucet_val.h" +#include "lucet_vmctx.h" + +#define LUCET_WASM_PAGE_SIZE (64 * 1024) + +enum lucet_error lucet_dl_module_load(const char *path, struct lucet_dl_module **mod_out); + +void lucet_dl_module_release(const struct lucet_dl_module *module); + +const char *lucet_error_name(enum lucet_error e); + +bool lucet_instance_check_heap(const struct lucet_instance *inst, const void *ptr, uintptr_t len); + +void *lucet_instance_embed_ctx(struct lucet_instance *inst); + +enum lucet_error lucet_instance_grow_heap(struct lucet_instance *inst, + uint32_t additional_pages, + uint32_t * previous_pages_out); + +uint8_t *lucet_instance_heap(struct lucet_instance *inst); + +uint32_t lucet_instance_heap_len(const struct lucet_instance *inst); + +void lucet_instance_release(struct lucet_instance *inst); + +enum lucet_error lucet_instance_reset(struct lucet_instance *inst); + +enum lucet_error lucet_instance_run(struct lucet_instance * inst, + const char * entrypoint, + uintptr_t argc, + const struct lucet_val *argv, + struct lucet_result * result_out); + +enum lucet_error lucet_instance_run_func_idx(struct lucet_instance * inst, + uint32_t table_idx, + uint32_t func_idx, + uintptr_t argc, + const struct lucet_val *argv, + struct lucet_result * result_out); + +enum lucet_error +lucet_instance_resume(struct lucet_instance *inst, void *val, struct lucet_result *result_out); + +enum lucet_error lucet_instance_set_fatal_handler(struct lucet_instance *inst, + lucet_fatal_handler fatal_handler); + +/** + * Release or run* must not be called in the body of this function! + */ +enum lucet_error lucet_instance_set_signal_handler(struct lucet_instance *inst, + lucet_signal_handler signal_handler); + +enum lucet_error lucet_mmap_region_create(uint64_t instance_capacity, + const struct lucet_alloc_limits *limits, + struct lucet_region ** region_out); + +enum lucet_error lucet_region_new_instance(const struct lucet_region * region, + const struct lucet_dl_module *module, + struct lucet_instance ** inst_out); + +enum lucet_error lucet_region_new_instance_with_ctx(const struct lucet_region * region, + const struct lucet_dl_module *module, + void * embed_ctx, + struct lucet_instance ** inst_out); + +void lucet_region_release(const struct lucet_region *region); + +float lucet_retval_f32(const struct lucet_untyped_retval *retval); + +double lucet_retval_f64(const struct lucet_untyped_retval *retval); + +union lucet_retval_gp lucet_retval_gp(const struct lucet_untyped_retval *retval); + +const char *lucet_result_tag_name(enum lucet_result_tag tag); + +#endif /* LUCET_H */ diff --git a/third_party/rust/lucet-runtime-wasmsbx/include/lucet_types.h b/third_party/rust/lucet-runtime-wasmsbx/include/lucet_types.h new file mode 100644 index 0000000000..d419395c7c --- /dev/null +++ b/third_party/rust/lucet-runtime-wasmsbx/include/lucet_types.h @@ -0,0 +1,199 @@ +#ifndef LUCET_TYPES_H +#define LUCET_TYPES_H + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 500 +#endif + +#include <stdarg.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> + +#ifdef __APPLE__ +#include <sys/ucontext.h> +#else +#include <ucontext.h> +#endif + +enum lucet_error { + lucet_error_ok, + lucet_error_invalid_argument, + lucet_error_region_full, + lucet_error_module, + lucet_error_limits_exceeded, + lucet_error_no_linear_memory, + lucet_error_symbol_not_found, + lucet_error_func_not_found, + lucet_error_runtime_fault, + lucet_error_runtime_terminated, + lucet_error_dl, + lucet_error_instance_not_returned, + lucet_error_instance_not_yielded, + lucet_error_start_yielded, + lucet_error_internal, + lucet_error_unsupported, +}; + +enum lucet_signal_behavior { + lucet_signal_behavior_default, + lucet_signal_behavior_continue, + lucet_signal_behavior_terminate, +}; + +enum lucet_terminated_reason { + lucet_terminated_reason_signal, + lucet_terminated_reason_ctx_not_found, + lucet_terminated_reason_yield_type_mismatch, + lucet_terminated_reason_borrow_error, + lucet_terminated_reason_provided, +}; + +enum lucet_trapcode { + lucet_trapcode_stack_overflow, + lucet_trapcode_heap_out_of_bounds, + lucet_trapcode_out_of_bounds, + lucet_trapcode_indirect_call_to_null, + lucet_trapcode_bad_signature, + lucet_trapcode_integer_overflow, + lucet_trapcode_integer_div_by_zero, + lucet_trapcode_bad_conversion_to_integer, + lucet_trapcode_interrupt, + lucet_trapcode_table_out_of_bounds, + lucet_trapcode_user, + lucet_trapcode_unknown, +}; + +enum lucet_val_type { + lucet_val_type_c_ptr, + lucet_val_type_guest_ptr, + lucet_val_type_u8, + lucet_val_type_u16, + lucet_val_type_u32, + lucet_val_type_u64, + lucet_val_type_i8, + lucet_val_type_i16, + lucet_val_type_i32, + lucet_val_type_i64, + lucet_val_type_usize, + lucet_val_type_isize, + lucet_val_type_bool, + lucet_val_type_f32, + lucet_val_type_f64, +}; + +union lucet_val_inner_val { + void * as_c_ptr; + uint64_t as_u64; + int64_t as_i64; + float as_f32; + double as_f64; +}; + +struct lucet_val { + enum lucet_val_type ty; + union lucet_val_inner_val inner_val; +}; + +struct lucet_dl_module; + +struct lucet_instance; + +struct lucet_region; + +/** + * 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). + */ +struct lucet_alloc_limits { + /** + * Max size of the heap, which can be backed by real memory. (default 1M) + */ + uint64_t heap_memory_size; + /** + * Size of total virtual memory. (default 8G) + */ + uint64_t heap_address_space_size; + /** + * Size of the guest stack. (default 128K) + */ + uint64_t stack_size; + /** + * Size of the globals region in bytes; each global uses 8 bytes. (default 4K) + */ + uint64_t globals_size; + /** + * Size of the signal stack region in bytes. + * + * SIGSTKSZ from <signals.h> is a good default when linking against a Rust release build of + * lucet-runtime, but 12K or more is recommended when using a Rust debug build. + */ + uint64_t signal_stack_size; +}; + +typedef enum lucet_signal_behavior (*lucet_signal_handler)(struct lucet_instance * inst, + const enum lucet_trapcode trap, + int signum, const siginfo_t *siginfo, + const void *context); + +typedef void (*lucet_fatal_handler)(struct lucet_instance *inst); + +struct lucet_untyped_retval { + char fp[16]; + char gp[8]; +}; + +#define LUCET_MODULE_ADDR_DETAILS_NAME_LEN 256 + +struct lucet_module_addr_details { + bool module_code_resolvable; + bool in_module_code; + char file_name[LUCET_MODULE_ADDR_DETAILS_NAME_LEN]; + char sym_name[LUCET_MODULE_ADDR_DETAILS_NAME_LEN]; +}; + +struct lucet_runtime_faulted { + bool fatal; + enum lucet_trapcode trapcode; + uintptr_t rip_addr; + struct lucet_module_addr_details rip_addr_details; +}; + +struct lucet_terminated { + enum lucet_terminated_reason reason; + void * provided; +}; + +struct lucet_yielded { + void *val; +}; + +union lucet_result_val { + struct lucet_untyped_retval returned; + struct lucet_yielded yielded; + struct lucet_runtime_faulted faulted; + struct lucet_terminated terminated; + enum lucet_error errored; +}; + +enum lucet_result_tag { + lucet_result_tag_returned, + lucet_result_tag_yielded, + lucet_result_tag_faulted, + lucet_result_tag_terminated, + lucet_result_tag_errored, +}; + +struct lucet_result { + enum lucet_result_tag tag; + union lucet_result_val val; +}; + +union lucet_retval_gp { + char as_untyped[8]; + void * as_c_ptr; + uint64_t as_u64; + int64_t as_i64; +}; + +#endif /* LUCET_TYPES_H */ diff --git a/third_party/rust/lucet-runtime-wasmsbx/include/lucet_val.h b/third_party/rust/lucet-runtime-wasmsbx/include/lucet_val.h new file mode 100644 index 0000000000..78ea1c17df --- /dev/null +++ b/third_party/rust/lucet-runtime-wasmsbx/include/lucet_val.h @@ -0,0 +1,103 @@ +#ifndef LUCET_VAL_H +#define LUCET_VAL_H 1 + +/** + * Typed values + * + * `struct lucet_val` represents a typed value, used in arguments lists. + * Such arguments can be built with the `LUCET_VAL_*` convenience macros. + * + * A guest function call with these arguments eventually returns a + * `struct lucet_untyped_retval` value, that can be converted to a + * native type with the `LUCET_UNTYPED_RETVAL_TO_*` macros. + * + * Usage: + * + * lucet_instance_run(inst, "add_2", 2, (struct lucet_val[]){ LUCET_VAL_U64(123), LUCET_VAL_U64(456) + * }); lucet_instance_state(inst, &state); uint64_t res = + * LUCET_UNTYPED_RETVAL_TO_U64(state.val.returned); + */ + +#include <sys/types.h> + +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +// Creates a lucet_val value from the given type + +#define LUCET_VAL_T(T, C, X) \ + ((struct lucet_val[1]){ { .ty = lucet_val_type_##T, .inner_val.C = (X) } }[0]) + +#define LUCET_VAL_C_PTR(X) LUCET_VAL_T(c_ptr, as_c_ptr, X) +#define LUCET_VAL_GUEST_PTR(X) LUCET_VAL_T(guest_ptr, as_u64, X) + +#define LUCET_VAL_U8(X) LUCET_VAL_T(u8, as_u64, X) +#define LUCET_VAL_U16(X) LUCET_VAL_T(u16, as_u64, X) +#define LUCET_VAL_U32(X) LUCET_VAL_T(u32, as_u64, X) +#define LUCET_VAL_U64(X) LUCET_VAL_T(u64, as_u64, X) + +#define LUCET_VAL_I8(X) LUCET_VAL_T(i8, as_i64, X) +#define LUCET_VAL_I16(X) LUCET_VAL_T(i16, as_i64, X) +#define LUCET_VAL_I32(X) LUCET_VAL_T(i32, as_i64, X) +#define LUCET_VAL_I64(X) LUCET_VAL_T(i64, as_i64, X) + +#define LUCET_VAL_USIZE(X) LUCET_VAL_T(usize, as_u64, X) +#define LUCET_VAL_ISIZE(X) LUCET_VAL_T(isize, as_i64, X) + +#define LUCET_VAL_BOOL(X) LUCET_VAL_T(bool, as_u64, X) + +#define LUCET_VAL_F32(X) LUCET_VAL_T(f32, as_f32, X) +#define LUCET_VAL_F64(X) LUCET_VAL_T(f64, as_f64, X) + +// Converts a lucet_val value to the given type + +#define LUCET_VAL_TO_T(T, C, V) ((T)((V).inner_val.C)) + +#define LUCET_VAL_TO_C_PTR(X) LUCET_VAL_TO_T(void *, as_c_ptr, X) +#define LUCET_VAL_TO_GUEST_PTR(X) LUCET_VAL_TO_T(guest_ptr_t, as_u64, X) + +#define LUCET_VAL_TO_U8(X) LUCET_VAL_TO_T(uint8_t, as_u64, X) +#define LUCET_VAL_TO_U16(X) LUCET_VAL_TO_T(uint16_t, as_u64, X) +#define LUCET_VAL_TO_U32(X) LUCET_VAL_TO_T(uint32_t, as_u64, X) +#define LUCET_VAL_TO_U64(X) LUCET_VAL_TO_T(uint64_t, as_u64, X) + +#define LUCET_VAL_TO_I8(X) LUCET_VAL_TO_T(int8_t, as_i64, X) +#define LUCET_VAL_TO_I16(X) LUCET_VAL_TO_T(int16_t, as_i64, X) +#define LUCET_VAL_TO_I32(X) LUCET_VAL_TO_T(int32_t, as_i64, X) +#define LUCET_VAL_TO_I64(X) LUCET_VAL_TO_T(int64_t, as_i64, X) + +#define LUCET_VAL_TO_USIZE(X) LUCET_VAL_TO_T(size_t, as_u64, X) +#define LUCET_VAL_TO_ISIZE(X) LUCET_VAL_TO_T(ssize_t, as_i64, X) + +#define LUCET_VAL_TO_BOOL(X) LUCET_VAL_TO_T(bool, as_u64, X) + +#define LUCET_VAL_TO_F32(X) LUCET_VAL_TO_T(float, as_f32, X) +#define LUCET_VAL_TO_F64(X) LUCET_VAL_TO_T(double, as_f64, X) + +// Converts an untyped return value to the given type + +#define LUCET_UNTYPED_RETVAL_TO_GP_T(T, C, X) ((T) lucet_retval_gp(&(X)).C) +#define LUCET_UNTYPED_RETVAL_TO_C_PTR(X) LUCET_UNTYPED_RETVAL_TO_GP_T(void *, as_c_ptr, &(X)) + +#define LUCET_UNTYPED_RETVAL_TO_GUEST_PTR(X) LUCET_UNTYPED_RETVAL_TO_GP_T(guest_ptr_t, as_u64, X) + +#define LUCET_UNTYPED_RETVAL_TO_U8(X) LUCET_UNTYPED_RETVAL_TO_GP_T(uint8_t, as_u64, X) +#define LUCET_UNTYPED_RETVAL_TO_U16(X) LUCET_UNTYPED_RETVAL_TO_GP_T(uint16_t, as_u64, X) +#define LUCET_UNTYPED_RETVAL_TO_U32(X) LUCET_UNTYPED_RETVAL_TO_GP_T(uint32_t, as_u64, X) +#define LUCET_UNTYPED_RETVAL_TO_U64(X) LUCET_UNTYPED_RETVAL_TO_GP_T(uint64_t, as_u64, X) + +#define LUCET_UNTYPED_RETVAL_TO_I8(X) LUCET_UNTYPED_RETVAL_TO_GP_T(int8_t, as_i64, X) +#define LUCET_UNTYPED_RETVAL_TO_I16(X) LUCET_UNTYPED_RETVAL_TO_GP_T(int16_t, as_i64, X) +#define LUCET_UNTYPED_RETVAL_TO_I32(X) LUCET_UNTYPED_RETVAL_TO_GP_T(int32_t, as_i64, X) +#define LUCET_UNTYPED_RETVAL_TO_I64(X) LUCET_UNTYPED_RETVAL_TO_GP_T(int64_t, as_i64, X) + +#define LUCET_UNTYPED_RETVAL_TO_USIZE(X) LUCET_UNTYPED_RETVAL_TO_GP_T(size_t, as_u64, X) +#define LUCET_UNTYPED_RETVAL_TO_ISIZE(X) LUCET_UNTYPED_RETVAL_TO_GP_T(ssize_t, as_i64, X) + +#define LUCET_UNTYPED_RETVAL_TO_BOOL(X) LUCET_UNTYPED_RETVAL_TO_GP_T(bool, as_u64, X) + +#define LUCET_UNTYPED_RETVAL_TO_F32(X) lucet_retval_f32(&(X)) +#define LUCET_UNTYPED_RETVAL_TO_F64(X) lucet_retval_f64(&(X)) + +#endif diff --git a/third_party/rust/lucet-runtime-wasmsbx/include/lucet_vmctx.h b/third_party/rust/lucet-runtime-wasmsbx/include/lucet_vmctx.h new file mode 100644 index 0000000000..76af4afe58 --- /dev/null +++ b/third_party/rust/lucet-runtime-wasmsbx/include/lucet_vmctx.h @@ -0,0 +1,45 @@ +#ifndef LUCET_VMCTX_H +#define LUCET_VMCTX_H + +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +// Typedefs for use in hostcall signatures (host functions called from the +// guest). Instances always have a 32-bit pointer size into their heap, and +// 32-bit size_t type. +typedef uint32_t guest_int; +typedef uint32_t guest_ptr_t; +typedef uint32_t guest_size_t; + +struct lucet_vmctx; + +// Get a pointer to the instance heap. +char *lucet_vmctx_get_heap(struct lucet_vmctx const *); + +// Check if a memory region is inside the instance heap. +bool lucet_vmctx_check_heap(struct lucet_vmctx const *, void *ptr, size_t len); + +// Get the embedding context for a given instance +// TODO: rename +void *lucet_vmctx_get_delegate(struct lucet_vmctx const *); + +void lucet_vmctx_terminate(struct lucet_vmctx const *, void *info); + +void *lucet_vmctx_yield(struct lucet_vmctx const *, void *val); + +// returns the current number of wasm pages +uint32_t lucet_vmctx_current_memory(struct lucet_vmctx const *); + +// takes the number of wasm pages to grow by. returns the number of pages before +// the call on success, or -1 on failure. +int32_t lucet_vmctx_grow_memory(struct lucet_vmctx const *, uint32_t additional_pages); + +// returns the address of a function given its ID +void *lucet_vmctx_get_func_from_idx(struct lucet_vmctx const *ctx, uint32_t table_id, + uint32_t func_id); + +// Mostly for tests - this conversion is builtin to lucetc +int64_t *lucet_vmctx_get_globals(struct lucet_vmctx const *ctx); + +#endif // LUCET_VMCTX_H |