示例#1
0
    def __init__(self, asset: ScriptAsset) -> None:
        self.store = wasmtime.Store()
        self.module = wasmtime.Module.from_file(self.store.engine, asset.path)
        self.wasm_instance = wasmtime.Instance(self.store, self.module, [])

        self.wasm_setup = self.wasm_instance.exports["setup"]
        self.wasm_update = self.wasm_instance.exports["update"]

        raw_memory = self.wasm_instance.exports["memory"]
        self.memory_len = raw_memory.data_len
        self.memory_ctype = ctypes.c_ubyte * self.memory_len

        mem_as_array = ctypes.cast(raw_memory.data_ptr,
                                   ctypes.POINTER(self.memory_ctype))[0]
        self.memory_view = memoryview(mem_as_array).cast("B")
示例#2
0
def init():
    # TODO: loader can not load module using WASI
    #   https://github.com/bytecodealliance/wasmtime-py/pull/44
    # import wasmtime.loader
    # import benchmark_wasm_wasi

    wasm_cfg = wasmtime.Config()
    wasm_cfg.cache = True

    wasi_cfg = wasmtime.WasiConfig()
    store = wasmtime.Store(wasmtime.Engine(wasm_cfg))
    linker = wasmtime.Linker(store)
    linker.define_wasi(wasmtime.WasiInstance(store,
                                             "wasi_snapshot_preview1", wasi_cfg))
    wasm = pkg_resources.read_binary(__package__, "benchmark_wasm.wasm")
    instance = linker.instantiate(wasmtime.Module(store.engine, wasm))
    return instance
示例#3
0
def _run_wasm_app(wasm_filename, argv):
    module_binary = importlib_resources.read_binary(__package__, wasm_filename)
    module_digest = hashlib.sha1(module_binary).digest()

    wasi_cfg = wasmtime.WasiConfig()
    wasi_cfg.argv = argv
    wasi_cfg.preopen_dir(str(importlib_resources.files(__package__) / "share"),
                         "/share")
    wasi_cfg.preopen_dir("/", "/")
    wasi_cfg.preopen_dir(".", ".")
    wasi_cfg.inherit_stdin()
    wasi_cfg.inherit_stdout()
    wasi_cfg.inherit_stderr()

    engine = wasmtime.Engine()
    cache_path = pathlib.Path(
        os.getenv("YOWASP_CACHE_DIR", appdirs.user_cache_dir("yowasp")))
    cache_path.mkdir(parents=True, exist_ok=True)
    cache_filename = (cache_path / "{}-cache".format(wasm_filename))
    digest_filename = (cache_path / "{}-digest".format(wasm_filename))
    try:
        with digest_filename.open("rb") as digest_file:
            if digest_file.read() != module_digest:
                raise Exception("cache miss")
        module = wasmtime.Module.deserialize_file(engine, str(cache_filename))
    except:
        print("Preparing to run {}. This might take a while...".format(
            argv[0]),
              file=sys.stderr)
        module = wasmtime.Module(engine, module_binary)
        with cache_filename.open("wb") as cache_file:
            cache_file.write(module.serialize())
        with digest_filename.open("wb") as digest_file:
            digest_file.write(module_digest)

    linker = wasmtime.Linker(engine)
    linker.define_wasi()
    store = wasmtime.Store(engine)
    store.set_wasi(wasi_cfg)
    app = linker.instantiate(store, module)
    linker.define_instance(store, "app", app)
    try:
        app.exports(store)["_start"](store)
        return 0
    except wasmtime.ExitTrap as trap:
        return trap.code
示例#4
0
文件: test.py 项目: lwerdna/workbench
# functions the guest/wasm is expecting to import
def proc_exit_(arg0):
    print("dummy proc_exit(0x%X)" % arg0)


def args_sizes_get_(arg0, arg1):
    print("dummy args_sizes_get(0x%X, 0x%X)" % (arg0, arg1))
    return 0


def args_get_(arg0, arg1):
    print("dummy args_get(0x%X, 0x%X)" % (arg0, arg1))
    return 0


store = wasmtime.Store()
module = wasmtime.Module.from_file(store.engine, 'foo.wasm')

i32 = wasmtime.ValType.i32()
proc_exit = wasmtime.Func(store, wasmtime.FuncType([i32], []), proc_exit_)
args_sizes_get = wasmtime.Func(store, wasmtime.FuncType([i32, i32], [i32]),
                               args_sizes_get_)
args_get = wasmtime.Func(store, wasmtime.FuncType([i32, i32], [i32]),
                         args_get_)

exports_to_guest = [proc_exit, args_sizes_get, args_get]
instance = wasmtime.Instance(store, module, exports_to_guest)

exports = instance.exports(store)

add = exports['add']
示例#5
0

relative_dir = 'lib/python'

wasm_cfg = wasmtime.Config()
wasm_cfg.cache = True

wasi_cfg = wasmtime.WasiConfig()
wasi_cfg.argv = ()
wasi_cfg.preopen_dir(".", "/")
wasi_cfg.inherit_stdin()
wasi_cfg.inherit_stdout()
wasi_cfg.inherit_stderr()

wasm_bytes = open(f'{relative_dir}/smartcore_wasi_lib.wasm', 'rb').read()
store = wasmtime.Store(wasmtime.Engine(wasm_cfg))
linker = wasmtime.Linker(store)
module = wasmtime.Module(store.engine, wasm_bytes)
wasi = linker.define_wasi(
    wasmtime.WasiInstance(store, "wasi_snapshot_preview1", wasi_cfg))
smartcore_wasi = linker.instantiate(module)

(file_ptr, file_len) = get_string_ptr(f'{relative_dir}/iris_knn.model',
                                      smartcore_wasi)
smartcore_wasi.exports["init"](file_ptr)
perfomances = []
if os.environ.get("noe") is not None:
    num_executions = int(os.environ.get("noe"))
print(f"Executing {num_executions} times")
start_time = datetime.datetime.now()
for i in range(num_executions):
示例#6
0
import wasmtime
try:
    from importlib import resources as importlib_resources
    importlib_resources.files  # py3.9+ stdlib
except (ImportError, AttributeError):
    import importlib_resources  # py3.8- shim

wasm_cfg = wasmtime.Config()
wasm_cfg.cache = True

wasi_cfg = wasmtime.WasiConfig()
wasi_cfg.argv = ("amaranth-yosys", *sys.argv[1:])
wasi_cfg.preopen_dir(".", ".")
wasi_cfg.inherit_stdin()
wasi_cfg.inherit_stdout()
wasi_cfg.inherit_stderr()

engine = wasmtime.Engine(wasm_cfg)
linker = wasmtime.Linker(engine)
linker.define_wasi()
store = wasmtime.Store(engine)
store.set_wasi(wasi_cfg)
yosys = linker.instantiate(
    store,
    wasmtime.Module(engine, (importlib_resources.files(__package__) /
                             "yosys.wasm").read_bytes()))
try:
    yosys.exports(store)["_start"](store)
except wasmtime.ExitTrap as trap:
    sys.exit(trap.code)