Path: blob/main/examples/min-platform/embedding/wasmtime-platform.h
2450 views
// Platform support for Wasmtime's `no_std` build.1//2// This header file is what Wasmtime will rely on when it does not otherwise3// have support for the native platform. This can happen with `no_std` binaries4// for example where the traditional Unix-or-Windows implementation is not5// suitable.6//7// Embedders are expected to implement the symbols defined in this header file.8// These symbols can be defined either in C/C++ or in Rust (using9// `#[unsafe(no_mangle)]`).10//11// Note that there are some `#define`s here which can be added before this12// header file is included to indicate how Wasmtime was built. This corresponds13// to the `wasmtime` crate's Cargo features where if the feature is disabled14// then the symbols will not be required.15//16// * `WASMTIME_SIGNALS_BASED_TRAPS` - corresponds to `signals-based-traps`17// * `WASMTIME_CUSTOM_SYNC` - corresponds to `custom-sync-primitives`18//19// Some more information about this header can additionally be found at20// <https://docs.wasmtime.dev/stability-platform-support.html>.212223#ifndef _WASMTIME_PLATFORM_H24#define _WASMTIME_PLATFORM_H2526/* Generated with cbindgen:0.29.0 */2728#include <stdarg.h>29#include <stdbool.h>30#include <stdint.h>31#include <stdlib.h>3233#if defined(WASMTIME_VIRTUAL_MEMORY)34/**35* Indicates that the memory region should be readable.36*/37#define WASMTIME_PROT_READ (1 << 0)38#endif3940#if defined(WASMTIME_VIRTUAL_MEMORY)41/**42* Indicates that the memory region should be writable.43*/44#define WASMTIME_PROT_WRITE (1 << 1)45#endif4647#if defined(WASMTIME_VIRTUAL_MEMORY)48/**49* Indicates that the memory region should be executable.50*/51#define WASMTIME_PROT_EXEC (1 << 2)52#endif5354#if defined(WASMTIME_VIRTUAL_MEMORY)55/**56* Abstract pointer type used in the `wasmtime_memory_image_*` APIs which57* is defined by the embedder.58*/59typedef struct wasmtime_memory_image wasmtime_memory_image;60#endif6162#if defined(WASMTIME_NATIVE_SIGNALS)63/**64* Handler function for traps in Wasmtime passed to `wasmtime_init_traps`.65*66* This function is invoked whenever a trap is caught by the system. For67* example this would be invoked during a signal handler on Linux. This68* function is passed a number of parameters indicating information about the69* trap:70*71* * `ip` - the instruction pointer at the time of the trap.72* * `fp` - the frame pointer register's value at the time of the trap.73* * `has_faulting_addr` - whether this trap is associated with an access74* violation (e.g. a segfault) meaning memory was accessed when it shouldn't75* be. If this is `true` then the next parameter is filled in.76* * `faulting_addr` - if `has_faulting_addr` is true then this is the address77* that was attempted to be accessed. Otherwise this value is not used.78*79* If this function returns then the trap was not handled by Wasmtime. This80* means that it's left up to the embedder how to deal with the trap/signal81* depending on its default behavior. This could mean forwarding to a82* non-Wasmtime handler, aborting the process, logging then crashing, etc. The83* meaning of a trap that's not handled by Wasmtime depends on the context in84* which the trap was generated.85*86* When this function does not return it's because a native exception handler87* was resumed to.88*/89typedef void (*wasmtime_trap_handler_t)(uintptr_t ip,90uintptr_t fp,91bool has_faulting_addr,92uintptr_t faulting_addr);93#endif9495#ifdef __cplusplus96extern "C" {97#endif // __cplusplus9899#if defined(WASMTIME_VIRTUAL_MEMORY)100/**101* Creates a new virtual memory mapping of the `size` specified with102* protection bits specified in `prot_flags`.103*104* Memory can be lazily committed.105*106* Stores the base pointer of the new mapping in `ret` on success.107*108* Returns 0 on success and an error code on failure.109*110* Similar to `mmap(0, size, prot_flags, MAP_PRIVATE, 0, -1)` on Linux.111*/112extern int32_t wasmtime_mmap_new(uintptr_t size, uint32_t prot_flags, uint8_t **ret);113#endif114115#if defined(WASMTIME_VIRTUAL_MEMORY)116/**117* Remaps the virtual memory starting at `addr` going for `size` bytes to118* the protections specified with a new blank mapping.119*120* This will unmap any prior mappings and decommit them. New mappings for121* anonymous memory are used to replace these mappings and the new area122* should have the protection specified by `prot_flags`.123*124* Returns 0 on success and an error code on failure.125*126* Similar to `mmap(addr, size, prot_flags, MAP_PRIVATE | MAP_FIXED, 0, -1)` on Linux.127*/128extern int32_t wasmtime_mmap_remap(uint8_t *addr, uintptr_t size, uint32_t prot_flags);129#endif130131#if defined(WASMTIME_VIRTUAL_MEMORY)132/**133* Unmaps memory at the specified `ptr` for `size` bytes.134*135* The memory should be discarded and decommitted and should generate a136* segfault if accessed after this function call.137*138* Returns 0 on success and an error code on failure.139*140* Similar to `munmap` on Linux.141*/142extern int32_t wasmtime_munmap(uint8_t *ptr, uintptr_t size);143#endif144145#if defined(WASMTIME_VIRTUAL_MEMORY)146/**147* Configures the protections associated with a region of virtual memory148* starting at `ptr` and going to `size`.149*150* Returns 0 on success and an error code on failure.151*152* Similar to `mprotect` on Linux.153*/154extern int32_t wasmtime_mprotect(uint8_t *ptr, uintptr_t size, uint32_t prot_flags);155#endif156157#if defined(WASMTIME_VIRTUAL_MEMORY)158/**159* Returns the page size, in bytes, of the current system.160*/161extern uintptr_t wasmtime_page_size(void);162#endif163164#if defined(WASMTIME_NATIVE_SIGNALS)165/**166* Initializes trap-handling logic for this platform.167*168* Wasmtime's implementation of WebAssembly relies on the ability to catch169* signals/traps/etc. For example divide-by-zero may raise a machine170* exception. Out-of-bounds memory accesses may also raise a machine171* exception. This function is used to initialize trap handling.172*173* The `handler` provided is a function pointer to invoke whenever a trap174* is encountered. The `handler` is invoked whenever a trap is caught by175* the system.176*177* Returns 0 on success and an error code on failure.178*/179extern int32_t wasmtime_init_traps(wasmtime_trap_handler_t handler);180#endif181182#if defined(WASMTIME_VIRTUAL_MEMORY)183/**184* Attempts to create a new in-memory image of the `ptr`/`len` combo which185* can be mapped to virtual addresses in the future.186*187* On success the returned `wasmtime_memory_image` pointer is stored into `ret`.188* This value stored can be `NULL` to indicate that an image cannot be189* created but no failure occurred. The structure otherwise will later be190* deallocated with `wasmtime_memory_image_free` and191* `wasmtime_memory_image_map_at` will be used to map the image into new192* regions of the address space.193*194* The `ptr` and `len` arguments are only valid for this function call, if195* the image needs to refer to them in the future then it must make a copy.196*197* Both `ptr` and `len` are guaranteed to be page-aligned.198*199* Returns 0 on success and an error code on failure. Note that storing200* `NULL` into `ret` is not considered a failure, and failure is used to201* indicate that something fatal has happened and Wasmtime will propagate202* the error upwards.203*/204extern int32_t wasmtime_memory_image_new(const uint8_t *ptr,205uintptr_t len,206struct wasmtime_memory_image **ret);207#endif208209#if defined(WASMTIME_VIRTUAL_MEMORY)210/**211* Maps the `image` provided to the virtual address at `addr` and `len`.212*213* This semantically should make it such that `addr` and `len` looks the214* same as the contents of what the memory image was first created with.215* The mappings of `addr` should be private and changes do not reflect back216* to `wasmtime_memory_image`.217*218* In effect this is to create a copy-on-write mapping at `addr`/`len`219* pointing back to the memory used by the image originally.220*221* Note that the memory region will be unmapped with `wasmtime_munmap` in222* the future.223*224* Aborts the process on failure.225*/226extern int32_t wasmtime_memory_image_map_at(struct wasmtime_memory_image *image,227uint8_t *addr,228uintptr_t len);229#endif230231#if defined(WASMTIME_VIRTUAL_MEMORY)232/**233* Deallocates the provided `wasmtime_memory_image`.234*235* Note that mappings created from this image are not guaranteed to be236* deallocated and/or unmapped before this is called.237*/238extern void wasmtime_memory_image_free(struct wasmtime_memory_image *image);239#endif240241/**242* Wasmtime requires a single pointer's space of TLS to be used at runtime,243* and this function returns the current value of the TLS variable.244*245* This value should default to `NULL`.246*/247extern uint8_t *wasmtime_tls_get(void);248249/**250* Sets the current TLS value for Wasmtime to the provided value.251*252* This value should be returned when later calling `wasmtime_tls_get`.253*/254extern void wasmtime_tls_set(uint8_t *ptr);255256#if defined(WASMTIME_CUSTOM_SYNC)257/**258* Frees a synchronization lock.259*260* May be called on a lock that was never used (still has a zero pattern).261* The implementor must handle this case gracefully.262*/263extern void wasmtime_sync_lock_free(uintptr_t *lock);264#endif265266#if defined(WASMTIME_CUSTOM_SYNC)267/**268* Acquires an exclusive lock.269*270* If the lock is uninitialized (zero pattern), it will be initialized lazily.271* This function blocks until the lock is acquired.272* Must be paired with [`wasmtime_sync_lock_release`].273*/274extern void wasmtime_sync_lock_acquire(uintptr_t *lock);275#endif276277#if defined(WASMTIME_CUSTOM_SYNC)278/**279* Releases an exclusive lock previously acquired with [`wasmtime_sync_lock_acquire`].280*/281extern void wasmtime_sync_lock_release(uintptr_t *lock);282#endif283284#if defined(WASMTIME_CUSTOM_SYNC)285/**286* Acquires a read lock on an RwLock.287*288* If the lock is uninitialized (zero pattern), it will be initialized lazily.289* Multiple readers can hold the lock simultaneously.290* Must be paired with [`wasmtime_sync_rwlock_read_release`].291*/292extern void wasmtime_sync_rwlock_read(uintptr_t *lock);293#endif294295#if defined(WASMTIME_CUSTOM_SYNC)296/**297* Releases a read lock previously acquired with [`wasmtime_sync_rwlock_read`].298*/299extern void wasmtime_sync_rwlock_read_release(uintptr_t *lock);300#endif301302#if defined(WASMTIME_CUSTOM_SYNC)303/**304* Acquires a write lock on an RwLock.305*306* If the lock is uninitialized (zero pattern), it will be initialized lazily.307* Only one writer can hold the lock, and no readers can be present.308* Must be paired with [`wasmtime_sync_rwlock_write_release`].309*/310extern void wasmtime_sync_rwlock_write(uintptr_t *lock);311#endif312313#if defined(WASMTIME_CUSTOM_SYNC)314/**315* Releases a write lock previously acquired with [`wasmtime_sync_rwlock_write`].316*/317extern void wasmtime_sync_rwlock_write_release(uintptr_t *lock);318#endif319320#if defined(WASMTIME_CUSTOM_SYNC)321/**322* Frees an RwLock.323*324* May be called on a lock that was never used (still has a zero pattern).325* The implementor must handle this case gracefully.326*/327extern void wasmtime_sync_rwlock_free(uintptr_t *lock);328#endif329330#ifdef __cplusplus331} // extern "C"332#endif // __cplusplus333334#endif /* _WASMTIME_PLATFORM_H */335336337