Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wiring SharedMemory with it's corresponding bindings #255

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions wasmtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from ._instance import Instance
from ._wasi import WasiConfig
from ._linker import Linker
from ._sharedmemory import SharedMemory

__all__ = [
'wat2wasm',
Expand All @@ -50,6 +51,7 @@
'Caller',
'Table',
'Memory',
'SharedMemory',
'Global',
'Trap',
'TrapCode',
Expand Down
78 changes: 39 additions & 39 deletions wasmtime/_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1955,9 +1955,9 @@ def wasi_config_inherit_stderr(config: Any) -> None:

_wasi_config_preopen_dir = dll.wasi_config_preopen_dir
_wasi_config_preopen_dir.restype = c_bool
_wasi_config_preopen_dir.argtypes = [POINTER(wasi_config_t), POINTER(c_char), POINTER(c_char)]
def wasi_config_preopen_dir(config: Any, path: Any, guest_path: Any) -> bool:
return _wasi_config_preopen_dir(config, path, guest_path) # type: ignore
_wasi_config_preopen_dir.argtypes = [POINTER(wasi_config_t), POINTER(c_char), POINTER(c_char), c_size_t, c_size_t]
def wasi_config_preopen_dir(config: Any, host_path: Any, guest_path: Any, dir_perms: Any, file_perms: Any) -> bool:
return _wasi_config_preopen_dir(config, host_path, guest_path, dir_perms, file_perms) # type: ignore

class wasmtime_error(Structure):
pass
Expand Down Expand Up @@ -2096,6 +2096,12 @@ def wasmtime_config_wasm_multi_memory_set(arg0: Any, arg1: Any) -> None:
def wasmtime_config_wasm_memory64_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_wasm_memory64_set(arg0, arg1) # type: ignore

_wasmtime_config_wasm_wide_arithmetic_set = dll.wasmtime_config_wasm_wide_arithmetic_set
_wasmtime_config_wasm_wide_arithmetic_set.restype = None
_wasmtime_config_wasm_wide_arithmetic_set.argtypes = [POINTER(wasm_config_t), c_bool]
def wasmtime_config_wasm_wide_arithmetic_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_wasm_wide_arithmetic_set(arg0, arg1) # type: ignore

_wasmtime_config_strategy_set = dll.wasmtime_config_strategy_set
_wasmtime_config_strategy_set.restype = None
_wasmtime_config_strategy_set.argtypes = [POINTER(wasm_config_t), wasmtime_strategy_t]
Expand Down Expand Up @@ -2132,35 +2138,29 @@ def wasmtime_config_cranelift_opt_level_set(arg0: Any, arg1: Any) -> None:
def wasmtime_config_profiler_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_profiler_set(arg0, arg1) # type: ignore

_wasmtime_config_static_memory_forced_set = dll.wasmtime_config_static_memory_forced_set
_wasmtime_config_static_memory_forced_set.restype = None
_wasmtime_config_static_memory_forced_set.argtypes = [POINTER(wasm_config_t), c_bool]
def wasmtime_config_static_memory_forced_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_static_memory_forced_set(arg0, arg1) # type: ignore

_wasmtime_config_static_memory_maximum_size_set = dll.wasmtime_config_static_memory_maximum_size_set
_wasmtime_config_static_memory_maximum_size_set.restype = None
_wasmtime_config_static_memory_maximum_size_set.argtypes = [POINTER(wasm_config_t), c_uint64]
def wasmtime_config_static_memory_maximum_size_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_static_memory_maximum_size_set(arg0, arg1) # type: ignore

_wasmtime_config_static_memory_guard_size_set = dll.wasmtime_config_static_memory_guard_size_set
_wasmtime_config_static_memory_guard_size_set.restype = None
_wasmtime_config_static_memory_guard_size_set.argtypes = [POINTER(wasm_config_t), c_uint64]
def wasmtime_config_static_memory_guard_size_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_static_memory_guard_size_set(arg0, arg1) # type: ignore

_wasmtime_config_dynamic_memory_guard_size_set = dll.wasmtime_config_dynamic_memory_guard_size_set
_wasmtime_config_dynamic_memory_guard_size_set.restype = None
_wasmtime_config_dynamic_memory_guard_size_set.argtypes = [POINTER(wasm_config_t), c_uint64]
def wasmtime_config_dynamic_memory_guard_size_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_dynamic_memory_guard_size_set(arg0, arg1) # type: ignore

_wasmtime_config_dynamic_memory_reserved_for_growth_set = dll.wasmtime_config_dynamic_memory_reserved_for_growth_set
_wasmtime_config_dynamic_memory_reserved_for_growth_set.restype = None
_wasmtime_config_dynamic_memory_reserved_for_growth_set.argtypes = [POINTER(wasm_config_t), c_uint64]
def wasmtime_config_dynamic_memory_reserved_for_growth_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_dynamic_memory_reserved_for_growth_set(arg0, arg1) # type: ignore
_wasmtime_config_memory_may_move_set = dll.wasmtime_config_memory_may_move_set
_wasmtime_config_memory_may_move_set.restype = None
_wasmtime_config_memory_may_move_set.argtypes = [POINTER(wasm_config_t), c_bool]
def wasmtime_config_memory_may_move_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_memory_may_move_set(arg0, arg1) # type: ignore

_wasmtime_config_memory_reservation_set = dll.wasmtime_config_memory_reservation_set
_wasmtime_config_memory_reservation_set.restype = None
_wasmtime_config_memory_reservation_set.argtypes = [POINTER(wasm_config_t), c_uint64]
def wasmtime_config_memory_reservation_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_memory_reservation_set(arg0, arg1) # type: ignore

_wasmtime_config_memory_guard_size_set = dll.wasmtime_config_memory_guard_size_set
_wasmtime_config_memory_guard_size_set.restype = None
_wasmtime_config_memory_guard_size_set.argtypes = [POINTER(wasm_config_t), c_uint64]
def wasmtime_config_memory_guard_size_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_memory_guard_size_set(arg0, arg1) # type: ignore

_wasmtime_config_memory_reservation_reserved_for_growth_set = dll.wasmtime_config_memory_reservation_reserved_for_growth_set
_wasmtime_config_memory_reservation_reserved_for_growth_set.restype = None
_wasmtime_config_memory_reservation_reserved_for_growth_set.argtypes = [POINTER(wasm_config_t), c_uint64]
def wasmtime_config_memory_reservation_reserved_for_growth_set(arg0: Any, arg1: Any) -> None:
return _wasmtime_config_memory_reservation_reserved_for_growth_set(arg0, arg1) # type: ignore

_wasmtime_config_native_unwind_info_set = dll.wasmtime_config_native_unwind_info_set
_wasmtime_config_native_unwind_info_set.restype = None
Expand Down Expand Up @@ -2937,9 +2937,9 @@ def wasmtime_linker_instantiate_pre(linker: Any, module: Any, instance_pre: Any)

_wasmtime_memorytype_new = dll.wasmtime_memorytype_new
_wasmtime_memorytype_new.restype = POINTER(wasm_memorytype_t)
_wasmtime_memorytype_new.argtypes = [c_uint64, c_bool, c_uint64, c_bool]
def wasmtime_memorytype_new(min: Any, max_present: Any, max: Any, is_64: Any) -> ctypes._Pointer:
return _wasmtime_memorytype_new(min, max_present, max, is_64) # type: ignore
_wasmtime_memorytype_new.argtypes = [c_uint64, c_bool, c_uint64, c_bool, c_bool]
def wasmtime_memorytype_new(min: Any, max_present: Any, max: Any, is_64: Any, shared: Any) -> ctypes._Pointer:
return _wasmtime_memorytype_new(min, max_present, max, is_64, shared) # type: ignore
atilag marked this conversation as resolved.
Show resolved Hide resolved

_wasmtime_memorytype_minimum = dll.wasmtime_memorytype_minimum
_wasmtime_memorytype_minimum.restype = c_uint64
Expand Down Expand Up @@ -3054,25 +3054,25 @@ def wasmtime_table_type(store: Any, table: Any) -> ctypes._Pointer:

_wasmtime_table_get = dll.wasmtime_table_get
_wasmtime_table_get.restype = c_bool
_wasmtime_table_get.argtypes = [POINTER(wasmtime_context_t), POINTER(wasmtime_table_t), c_uint32, POINTER(wasmtime_val_t)]
_wasmtime_table_get.argtypes = [POINTER(wasmtime_context_t), POINTER(wasmtime_table_t), c_uint64, POINTER(wasmtime_val_t)]
def wasmtime_table_get(store: Any, table: Any, index: Any, val: Any) -> bool:
return _wasmtime_table_get(store, table, index, val) # type: ignore

_wasmtime_table_set = dll.wasmtime_table_set
_wasmtime_table_set.restype = POINTER(wasmtime_error_t)
_wasmtime_table_set.argtypes = [POINTER(wasmtime_context_t), POINTER(wasmtime_table_t), c_uint32, POINTER(wasmtime_val_t)]
_wasmtime_table_set.argtypes = [POINTER(wasmtime_context_t), POINTER(wasmtime_table_t), c_uint64, POINTER(wasmtime_val_t)]
def wasmtime_table_set(store: Any, table: Any, index: Any, value: Any) -> ctypes._Pointer:
return _wasmtime_table_set(store, table, index, value) # type: ignore

_wasmtime_table_size = dll.wasmtime_table_size
_wasmtime_table_size.restype = c_uint32
_wasmtime_table_size.restype = c_uint64
_wasmtime_table_size.argtypes = [POINTER(wasmtime_context_t), POINTER(wasmtime_table_t)]
def wasmtime_table_size(store: Any, table: Any) -> int:
return _wasmtime_table_size(store, table) # type: ignore

_wasmtime_table_grow = dll.wasmtime_table_grow
_wasmtime_table_grow.restype = POINTER(wasmtime_error_t)
_wasmtime_table_grow.argtypes = [POINTER(wasmtime_context_t), POINTER(wasmtime_table_t), c_uint32, POINTER(wasmtime_val_t), POINTER(c_uint32)]
_wasmtime_table_grow.argtypes = [POINTER(wasmtime_context_t), POINTER(wasmtime_table_t), c_uint64, POINTER(wasmtime_val_t), POINTER(c_uint64)]
def wasmtime_table_grow(store: Any, table: Any, delta: Any, init: Any, prev_size: Any) -> ctypes._Pointer:
return _wasmtime_table_grow(store, table, delta, init, prev_size) # type: ignore

Expand Down
8 changes: 6 additions & 2 deletions wasmtime/_extern.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def wrap_extern(ptr: ffi.wasmtime_extern_t) -> AsExtern:
from wasmtime import Func, Table, Global, Memory, Module, Instance
from wasmtime import Func, Table, Global, Memory, SharedMemory, Module, Instance

if ptr.kind == ffi.WASMTIME_EXTERN_FUNC.value:
return Func._from_raw(ptr.of.func)
Expand All @@ -15,6 +15,8 @@ def wrap_extern(ptr: ffi.wasmtime_extern_t) -> AsExtern:
return Global._from_raw(ptr.of.global_)
if ptr.kind == ffi.WASMTIME_EXTERN_MEMORY.value:
return Memory._from_raw(ptr.of.memory)
if ptr.kind == ffi.WASMTIME_EXTERN_SHAREDMEMORY.value:
return SharedMemory._from_raw(ptr.of.sharedmemory)
if ptr.kind == ffi.WASMTIME_EXTERN_INSTANCE.value:
return Instance._from_raw(ptr.of.instance)
if ptr.kind == ffi.WASMTIME_EXTERN_MODULE.value:
Expand All @@ -23,14 +25,16 @@ def wrap_extern(ptr: ffi.wasmtime_extern_t) -> AsExtern:


def get_extern_ptr(item: AsExtern) -> ffi.wasmtime_extern_t:
from wasmtime import Func, Table, Global, Memory, Module, Instance
from wasmtime import Func, Table, Global, Memory, SharedMemory, Module, Instance

if isinstance(item, Func):
return item._as_extern()
elif isinstance(item, Global):
return item._as_extern()
elif isinstance(item, Memory):
return item._as_extern()
elif isinstance(item, SharedMemory):
return item._as_extern()
elif isinstance(item, Table):
return item._as_extern()
elif isinstance(item, Module):
Expand Down
1 change: 1 addition & 0 deletions wasmtime/_ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
WASMTIME_EXTERN_GLOBAL = c_uint8(1)
WASMTIME_EXTERN_TABLE = c_uint8(2)
WASMTIME_EXTERN_MEMORY = c_uint8(3)
WASMTIME_EXTERN_SHAREDMEMORY = c_uint8(4)
atilag marked this conversation as resolved.
Show resolved Hide resolved
WASMTIME_EXTERN_INSTANCE = c_uint8(4)
WASMTIME_EXTERN_MODULE = c_uint8(5)

Expand Down
77 changes: 77 additions & 0 deletions wasmtime/_sharedmemory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from . import _ffi as ffi
from ctypes import *
import ctypes
from wasmtime import MemoryType, WasmtimeError, Engine, Managed
from ._store import Storelike



class SharedMemory(Managed["ctypes._Pointer[ffi.wasmtime_sharedmemory_t]"]):
def __init__(self, engine: Engine, ty: MemoryType):
"""
Creates a new shared memory in `store` with the given `ty`
"""

sharedmemory = ffi.wasmtime_sharedmemory_t()
ptr = POINTER(ffi.wasmtime_sharedmemory_t)()
error = ffi.wasmtime_sharedmemory_new(engine.ptr(), ty.ptr(), byref(ptr))
if error:
raise WasmtimeError._from_ptr(error)
self._set_ptr(sharedmemory)

@classmethod
def _from_raw(cls, sharedmemory: ffi.wasmtime_sharedmemory_t) -> "SharedMemory":
if not isinstance(sharedmemory, ffi.wasmtime_sharedmemory_t):
raise TypeError("wrong shared memory pointer type provided to _from_raw")

ty: "SharedMemory" = cls.__new__(cls)
ty._set_ptr(sharedmemory)
atilag marked this conversation as resolved.
Show resolved Hide resolved
return ty

def type(self) -> MemoryType:
"""
Gets the type of this memory as a `MemoryType`
"""

ptr = ffi.wasmtime_sharedmemory_type(byref(self._sharedmemory))
return MemoryType._from_ptr(ptr, None)

def grow(self, delta: int) -> int:
"""
Grows this memory by the given number of pages
"""

if delta < 0:
raise WasmtimeError("cannot grow by negative amount")
prev = ffi.c_uint64(0)
error = ffi.wasmtime_sharedmemory_grow(byref(self.ptr()), delta, byref(prev))
if error:
raise WasmtimeError._from_ptr(error)
return prev.value

def size(self) -> int:
"""
Returns the size, in WebAssembly pages, of this shared memory.
"""

return ffi.wasmtime_sharedmemory_size(byref(self.ptr()))

def data_ptr(self) -> "ctypes._Pointer[c_ubyte]":
"""
Returns the raw pointer in memory where this wasm shared memory lives.

Remember that all accesses to wasm shared memory should be bounds-checked
against the `data_len` method.
"""
return ffi.wasmtime_sharedmemory_data(byref(self.ptr()))

def data_len(self) -> int:
"""
Returns the raw byte length of this memory.
"""

return ffi.wasmtime_sharedmemory_data_size(byref(self.ptr()))

def _as_extern(self) -> ffi.wasmtime_extern_t:
union = ffi.wasmtime_extern_union(sharedmemory=pointer(self.ptr()))
return ffi.wasmtime_extern_t(ffi.WASMTIME_EXTERN_SHAREDMEMORY, union)
4 changes: 2 additions & 2 deletions wasmtime/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def grow(self, store: Storelike, amt: int, init: Any) -> int:
Returns the previous size of the table otherwise.
"""
init_val = Val._convert_to_raw(store, self.type(store).element, init)
prev = c_uint32(0)
error = ffi.wasmtime_table_grow(store._context(), byref(self._table), c_uint32(amt), byref(init_val), byref(prev))
prev = c_uint64(0)
error = ffi.wasmtime_table_grow(store._context(), byref(self._table), c_uint64(amt), byref(init_val), byref(prev))
ffi.wasmtime_val_unroot(store._context(), byref(init_val))
if error:
raise WasmtimeError._from_ptr(error)
Expand Down
15 changes: 12 additions & 3 deletions wasmtime/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def _as_extern(self) -> "ctypes._Pointer[ffi.wasm_externtype_t]":


class MemoryType(Managed["ctypes._Pointer[ffi.wasm_memorytype_t]"]):
def __init__(self, limits: Limits, is_64: bool = False):
def __init__(self, limits: Limits, is_64: bool = False, shared: bool = False):
if not isinstance(limits, Limits):
raise TypeError("expected Limits")
if is_64:
Expand All @@ -296,7 +296,8 @@ def __init__(self, limits: Limits, is_64: bool = False):
ptr = ffi.wasmtime_memorytype_new(limits.min,
limits.max is not None,
limits.max if limits.max else 0,
is_64)
is_64,
shared)
if not ptr:
raise WasmtimeError("failed to allocate MemoryType")
self._set_ptr(ptr)
Expand Down Expand Up @@ -331,10 +332,18 @@ def is_64(self) -> bool:
Returns whether or not this is a 64-bit memory
"""
return ffi.wasmtime_memorytype_is64(self.ptr())

@property
def is_shared(self) -> bool:
"""
Returns whether or not this is a shared memory
"""
return ffi.wasmtime_memorytype_isshared(self.ptr())


def _as_extern(self) -> "ctypes._Pointer[ffi.wasm_externtype_t]":
return ffi.wasm_memorytype_as_externtype_const(self.ptr())


def wrap_externtype(ptr: "ctypes._Pointer[ffi.wasm_externtype_t]", owner: Optional[Any]) -> "AsExternType":
if not isinstance(ptr, POINTER(ffi.wasm_externtype_t)):
Expand Down
Loading