From ba48cead8fc7194c2c1492c0a2d2e976e3907a2e Mon Sep 17 00:00:00 2001 From: Philipp Tessenow Date: Wed, 6 Mar 2024 22:47:35 +0100 Subject: [PATCH] update wasmtime from from 14.0.4 to 18.0.2 and follow wasmtimes fuel API changes --- CHANGELOG.md | 9 +- lib/wasmex/engine_config.ex | 2 +- lib/wasmex/native.ex | 5 +- lib/wasmex/store_or_caller.ex | 89 +---- native/wasmex/Cargo.lock | 519 ++++++--------------------- native/wasmex/Cargo.toml | 6 +- native/wasmex/src/lib.rs | 5 +- native/wasmex/src/store.rs | 41 +-- test/wasmex/store_or_caller_test.exs | 151 ++------ test/wasmex_test.exs | 4 +- 10 files changed, 170 insertions(+), 661 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96eab4c3..e7178f1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,19 +14,24 @@ Types of changes - `Fixed` for any bug fixes. - `Security` in case of vulnerabilities. -## unreleased +## [0.9.0 - unreleased] -put your changes here +Wasmtime rewrote their fuel-related API and simplified it. To remain consistent with Wasmtime, we follow this change in this release. A Wasmex `Store` now only implements `set_fuel/2` and `get_fuel/1`. All other methods are removed with this release. + +The underlying implementation of the fuel system got rewritten as well. If you are using fuel in your app, +please check your fuel consumption values. * Thanks to @RoyalIcing for helping us keeping our dependencies up to date for this release 💜 ### Added * official support for Elixir 1.15 and 1.16 +* fuel-related API got rewritten, because the underlying Wasm library (wasmtime) changed their API and we want to be consistent. Added `Store.get_fuel/1` and `Store.set_fuel/2` which is a much simpler API than before. ### Removed * removed support for Elixir 1.12 +* with the fuel-related API changed, the existing methods on `Store` (`consume_fuel`, `fuel_remaining`, `add_fuel`) were removed. Please call `set_fuel/2` and `get_fuel/1` instead. ### Changed diff --git a/lib/wasmex/engine_config.ex b/lib/wasmex/engine_config.ex index 426a1448..0aed535a 100644 --- a/lib/wasmex/engine_config.ex +++ b/lib/wasmex/engine_config.ex @@ -36,7 +36,7 @@ defmodule Wasmex.EngineConfig do Note that a `Wasmex.Store` starts with no fuel, so if you enable this option you'll have to be sure to pour some fuel into `Wasmex.Store` before - executing some code. See `Wasmex.StoreOrCaller.add_fuel/2`. + executing some code. See `Wasmex.StoreOrCaller.set_fuel/2`. ## Example diff --git a/lib/wasmex/native.ex b/lib/wasmex/native.ex index fe1c394f..eb7eed22 100644 --- a/lib/wasmex/native.ex +++ b/lib/wasmex/native.ex @@ -69,9 +69,8 @@ defmodule Wasmex.Native do def store_new(_store_limits, _engine_resource), do: error() def store_new_wasi(_wasi_options, _store_limits, _engine_resource), do: error() - def store_or_caller_add_fuel(_store_or_caller_resource, _fuel), do: error() - def store_or_caller_consume_fuel(_store_or_caller_resource, _fuel), do: error() - def store_or_caller_fuel_consumed(_store_or_caller_resource), do: error() + def store_or_caller_get_fuel(_store_or_caller_resource), do: error() + def store_or_caller_set_fuel(_store_or_caller_resource, _fuel), do: error() # When the NIF is loaded, it will override functions in this module. # Calling error is handles the case when the nif could not be loaded. diff --git a/lib/wasmex/store_or_caller.ex b/lib/wasmex/store_or_caller.ex index 2fad3157..8708d6d7 100644 --- a/lib/wasmex/store_or_caller.ex +++ b/lib/wasmex/store_or_caller.ex @@ -32,7 +32,7 @@ defmodule Wasmex.StoreOrCaller do end @doc ~S""" - Adds fuel to this Store for Wasm to consume while executing. + Sets fuel to for Wasm to consume while executing. For this method to work, fuel consumption must be enabled via `Wasmex.EngineConfig.consume_fuel/2. By default a `Wasmex.Store` @@ -58,55 +58,17 @@ defmodule Wasmex.StoreOrCaller do iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) iex> {:ok, store} = Wasmex.Store.new(nil, engine) - iex> Wasmex.StoreOrCaller.add_fuel(store, 10) + iex> Wasmex.StoreOrCaller.set_fuel(store, 10) :ok """ - @spec add_fuel(__MODULE__.t(), pos_integer()) :: :ok | {:error, binary()} - def add_fuel(%__MODULE__{resource: resource}, fuel) do - case Wasmex.Native.store_or_caller_add_fuel(resource, fuel) do + @spec set_fuel(__MODULE__.t(), pos_integer()) :: :ok | {:error, binary()} + def set_fuel(%__MODULE__{resource: resource}, fuel) do + case Wasmex.Native.store_or_caller_set_fuel(resource, fuel) do {} -> :ok {:error, reason} -> {:error, reason} end end - @doc ~S""" - Synthetically consumes fuel from this Store. - - For this method to work fuel, consumption must be enabled via - `Wasmex.EngineConfig.consume_fuel/2`. - - WebAssembly execution will automatically consume fuel but if so desired - the embedder can also consume fuel manually to account for relative - costs of host functions, for example. - - This function will attempt to consume `fuel` units of fuel from within - this store. If the remaining amount of fuel allows this then `{:ok, N}` - is returned where `N` is the amount of remaining fuel. Otherwise an - error is returned and no fuel is consumed. - - ## Errors - - This function will return an error either if fuel consumption is not - enabled via `Wasmex.EngineConfig.consume_fuel/2` or if `fuel` exceeds - the amount of remaining fuel within this store. - - ## Examples - - iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) - iex> {:ok, store} = Wasmex.Store.new(nil, engine) - iex> Wasmex.StoreOrCaller.add_fuel(store, 10) - iex> Wasmex.StoreOrCaller.fuel_remaining(store) - {:ok, 10} - """ - @spec consume_fuel(__MODULE__.t(), pos_integer() | 0) :: - {:ok, pos_integer()} | {:error, binary()} - def consume_fuel(%__MODULE__{resource: resource}, fuel) do - case Wasmex.Native.store_or_caller_consume_fuel(resource, fuel) do - {:error, reason} -> {:error, reason} - fuel_remaining -> {:ok, fuel_remaining} - end - end - @doc ~S""" Returns the amount of fuel available for future execution of this store. @@ -114,44 +76,15 @@ defmodule Wasmex.StoreOrCaller do iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) iex> {:ok, store} = Wasmex.Store.new(nil, engine) - iex> Wasmex.StoreOrCaller.add_fuel(store, 10) - iex> Wasmex.StoreOrCaller.fuel_remaining(store) + iex> Wasmex.StoreOrCaller.set_fuel(store, 10) + iex> Wasmex.StoreOrCaller.get_fuel(store) {:ok, 10} """ - @spec fuel_remaining(__MODULE__.t()) :: {:ok, pos_integer()} | {:error, binary()} - def fuel_remaining(%__MODULE__{} = store_or_caller) do - consume_fuel(store_or_caller, 0) - end - - @doc ~S""" - Returns the amount of fuel consumed by this store's execution so far. - - Note that fuel, if enabled, must be initially added via - `Wasmex.StoreOrCaller.add_fuel/2`. - - ## Errors - - If fuel consumption is not enabled via - `Wasmex.EngineConfig.consume_fuel/2` then this function will return - an error tuple. - - ## Examples - - iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) - iex> {:ok, store} = Wasmex.Store.new(nil, engine) - iex> Wasmex.StoreOrCaller.fuel_consumed(store) - {:ok, 0} - iex> Wasmex.StoreOrCaller.add_fuel(store, 10) - iex> {:ok, _fuel} = Wasmex.StoreOrCaller.consume_fuel(store, 8) - iex> Wasmex.StoreOrCaller.fuel_consumed(store) - {:ok, 8} - """ - @spec fuel_consumed(__MODULE__.t()) :: {:ok, pos_integer()} | {:error, binary()} - def fuel_consumed(%__MODULE__{resource: resource}) do - case Wasmex.Native.store_or_caller_fuel_consumed(resource) do + @spec get_fuel(__MODULE__.t()) :: {:ok, pos_integer()} | {:error, binary()} + def get_fuel(%__MODULE__{resource: resource}) do + case Wasmex.Native.store_or_caller_get_fuel(resource) do {:error, reason} -> {:error, reason} - nil -> {:error, "Could not consume fuel: fuel is not configured in this store"} - fuel_consumed -> {:ok, fuel_consumed} + get_fuel -> {:ok, get_fuel} end end end diff --git a/native/wasmex/Cargo.lock b/native/wasmex/Cargo.lock index cc8753da..0ccb62ef 100644 --- a/native/wasmex/Cargo.lock +++ b/native/wasmex/Cargo.lock @@ -270,25 +270,25 @@ dependencies = [ [[package]] name = "cranelift-bforest" -version = "0.101.4" +version = "0.105.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b5bb9245ec7dcc04d03110e538d31f0969d301c9d673145f4b4d5c3478539a3" +checksum = "9515fcc42b6cb5137f76b84c1a6f819782d0cf12473d145d3bc5cd67eedc8bc2" dependencies = [ - "cranelift-entity 0.101.4", + "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.101.4" +version = "0.105.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebb18d10e5ddac43ba4ca8fd4e310938569c3e484cc01b6372b27dc5bb4dfd28" +checksum = "1ad827c6071bfe6d22de1bc331296a29f9ddc506ff926d8415b435ec6a6efce0" dependencies = [ "bumpalo", "cranelift-bforest", "cranelift-codegen-meta", "cranelift-codegen-shared", "cranelift-control", - "cranelift-entity 0.101.4", + "cranelift-entity", "cranelift-isle", "gimli", "hashbrown 0.14.3", @@ -300,38 +300,28 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.101.4" +version = "0.105.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a3ce6d22982c1b9b6b012654258bab1a13947bb12703518bef06b1a4867c3d6" +checksum = "10e6b36237a9ca2ce2fb4cc7741d418a080afa1327402138412ef85d5367bef1" dependencies = [ "cranelift-codegen-shared", ] [[package]] name = "cranelift-codegen-shared" -version = "0.101.4" +version = "0.105.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47220fd4f9a0ce23541652b6f16f83868d282602c600d14934b2a4c166b4bd80" +checksum = "c36bf4bfb86898a94ccfa773a1f86e8a5346b1983ff72059bdd2db4600325251" [[package]] name = "cranelift-control" -version = "0.101.4" +version = "0.105.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed5a4c42672aea9b6e820046b52e47a1c05d3394a6cdf4cb3c3c4b702f954bd2" +checksum = "7cbf36560e7a6bd1409ca91e7b43b2cc7ed8429f343d7605eadf9046e8fac0d0" dependencies = [ "arbitrary", ] -[[package]] -name = "cranelift-entity" -version = "0.101.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b4e9a3296fc827f9d35135dc2c0c8dd8d8359eb1ef904bae2d55d5bcb0c9f94" -dependencies = [ - "serde", - "serde_derive", -] - [[package]] name = "cranelift-entity" version = "0.105.2" @@ -344,9 +334,9 @@ dependencies = [ [[package]] name = "cranelift-frontend" -version = "0.101.4" +version = "0.105.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ec537d0f0b8e084517f3e7bfa1d89af343d7c7df455573fca9f272d4e01267" +checksum = "af5d4da63143ee3485c7bcedde0a818727d737d1083484a0ceedb8950c89e495" dependencies = [ "cranelift-codegen", "log", @@ -356,15 +346,15 @@ dependencies = [ [[package]] name = "cranelift-isle" -version = "0.101.4" +version = "0.105.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45bab6d69919d210a50331d35cc6ce111567bc040aebac63a8ae130d0400a075" +checksum = "457a9832b089e26f5eea70dcf49bed8ec6edafed630ce7c83161f24d46ab8085" [[package]] name = "cranelift-native" -version = "0.101.4" +version = "0.105.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f32e81605f352cf37af5463f11cd7deec7b6572741931a8d372f7fdd4a744f5d" +checksum = "9b490d579df1ce365e1ea359e24ed86d82289fa785153327c2f6a69a59a731e4" dependencies = [ "cranelift-codegen", "libc", @@ -373,18 +363,18 @@ dependencies = [ [[package]] name = "cranelift-wasm" -version = "0.101.4" +version = "0.105.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0edaa4cbec1bc787395c074233df2652dd62f3e29d3ee60329514a0a51e6b045" +checksum = "8cd747ed7f9a461dda9c388415392f6bb95d1a6ef3b7694d17e0817eb74b7798" dependencies = [ "cranelift-codegen", - "cranelift-entity 0.101.4", + "cranelift-entity", "cranelift-frontend", "itertools", "log", "smallvec", - "wasmparser 0.115.0", - "wasmtime-types 14.0.4", + "wasmparser", + "wasmtime-types", ] [[package]] @@ -793,9 +783,9 @@ checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" [[package]] name = "ittapi" -version = "0.3.5" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25a5c0b993601cad796222ea076565c5d9f337d35592f8622c753724f06d7271" +checksum = "6b996fe614c41395cdaedf3cf408a9534851090959d90d54a535f675550b64b1" dependencies = [ "anyhow", "ittapi-sys", @@ -804,9 +794,9 @@ dependencies = [ [[package]] name = "ittapi-sys" -version = "0.3.5" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb7b5e473765060536a660eed127f758cf1a810c73e49063264959c60d1727d9" +checksum = "52f5385394064fa2c886205dba02598013ce83d3e92d33dbdc0c52fe0e7bf4fc" dependencies = [ "cc", ] @@ -1498,13 +1488,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] -name = "wasi-cap-std-sync" -version = "14.0.4" +name = "wasi-common" +version = "18.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fd94e147b273348ec68ae412b8bc17a4d372b9e070535b98e3e2c5a3ffd8e83" +checksum = "880c1461417b2bf90262591bf8a5f04358fb86dac8a585a49b87024971296763" dependencies = [ "anyhow", - "async-trait", + "bitflags 2.4.2", "cap-fs-ext", "cap-rand", "cap-std", @@ -1512,32 +1502,15 @@ dependencies = [ "fs-set-times", "io-extras", "io-lifetimes", + "log", "once_cell", "rustix", "system-interface", - "tracing", - "wasi-common", - "windows-sys 0.48.0", -] - -[[package]] -name = "wasi-common" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d5166f7432ee36d06aa9f9bd7990a00330401fdbc75be7887ea952a299b9a19" -dependencies = [ - "anyhow", - "bitflags 2.4.2", - "cap-rand", - "cap-std", - "io-extras", - "log", - "rustix", "thiserror", "tracing", - "wasmtime 14.0.4", - "wiggle 14.0.4", - "windows-sys 0.48.0", + "wasmtime", + "wiggle", + "windows-sys 0.52.0", ] [[package]] @@ -1594,15 +1567,6 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" -[[package]] -name = "wasm-encoder" -version = "0.35.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ca90ba1b5b0a70d3d49473c5579951f3bddc78d47b59256d2f9d4922b150aca" -dependencies = [ - "leb128", -] - [[package]] name = "wasm-encoder" version = "0.41.2" @@ -1629,20 +1593,10 @@ dependencies = [ "rand", "rustler", "wasi-common", - "wasmtime 14.0.4", + "wasmtime", "wasmtime-wasi", "wat", - "wiggle 18.0.2", -] - -[[package]] -name = "wasmparser" -version = "0.115.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e06c0641a4add879ba71ccb3a1e4278fd546f76f1eafb21d8f7b07733b547cd5" -dependencies = [ - "indexmap", - "semver", + "wiggle", ] [[package]] @@ -1663,15 +1617,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60e73986a6b7fdfedb7c5bf9e7eb71135486507c8fbc4c0c42cffcb6532988b7" dependencies = [ "anyhow", - "wasmparser 0.121.2", + "wasmparser", ] [[package]] name = "wasmtime" -version = "14.0.4" +version = "18.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca54f6090ce46973f33a79f265924b204f248f91aec09229bce53d19d567c1a6" +checksum = "4c843b8bc4dd4f3a76173ba93405c71111d570af0d90ea5f6299c705d0c2add2" dependencies = [ + "addr2line", "anyhow", "async-trait", "bincode", @@ -1679,74 +1634,36 @@ dependencies = [ "cfg-if", "encoding_rs", "fxprof-processed-profile", + "gimli", "indexmap", + "ittapi", "libc", "log", "object", "once_cell", "paste", - "psm", "rayon", + "rustix", "serde", "serde_derive", "serde_json", "target-lexicon", - "wasm-encoder 0.35.0", - "wasmparser 0.115.0", + "wasm-encoder 0.41.2", + "wasmparser", "wasmtime-cache", - "wasmtime-component-macro 14.0.4", - "wasmtime-component-util 14.0.4", + "wasmtime-component-macro", + "wasmtime-component-util", "wasmtime-cranelift", - "wasmtime-environ 14.0.4", - "wasmtime-fiber 14.0.4", - "wasmtime-jit", - "wasmtime-runtime 14.0.4", + "wasmtime-environ", + "wasmtime-fiber", + "wasmtime-jit-debug", + "wasmtime-jit-icache-coherence", + "wasmtime-runtime", "wasmtime-winch", "wat", - "windows-sys 0.48.0", -] - -[[package]] -name = "wasmtime" -version = "18.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c843b8bc4dd4f3a76173ba93405c71111d570af0d90ea5f6299c705d0c2add2" -dependencies = [ - "anyhow", - "async-trait", - "bincode", - "bumpalo", - "cfg-if", - "gimli", - "indexmap", - "libc", - "log", - "object", - "once_cell", - "paste", - "rustix", - "serde", - "serde_derive", - "serde_json", - "target-lexicon", - "wasmparser 0.121.2", - "wasmtime-component-macro 18.0.2", - "wasmtime-environ 18.0.2", - "wasmtime-fiber 18.0.2", - "wasmtime-jit-icache-coherence 18.0.2", - "wasmtime-runtime 18.0.2", "windows-sys 0.52.0", ] -[[package]] -name = "wasmtime-asm-macros" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54984bc0b5689da87a43d7c181d23092b4d5cfcbb7ae3eb6b917dd55865d95e6" -dependencies = [ - "cfg-if", -] - [[package]] name = "wasmtime-asm-macros" version = "18.0.2" @@ -1758,9 +1675,9 @@ dependencies = [ [[package]] name = "wasmtime-cache" -version = "14.0.4" +version = "18.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a4df7655bb73b592189033ab046aa47c1da486d70bc9c1ebf45e55ac030bdf4" +checksum = "6fb4fc2bbf9c790a57875eba65588fa97acf57a7d784dc86d057e648d9a1ed91" dependencies = [ "anyhow", "base64", @@ -1772,25 +1689,10 @@ dependencies = [ "serde_derive", "sha2", "toml", - "windows-sys 0.48.0", + "windows-sys 0.52.0", "zstd", ] -[[package]] -name = "wasmtime-component-macro" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64de99fb7c4c383832b85efcaae95f7094a5c505d80146227ce97ab436cbac68" -dependencies = [ - "anyhow", - "proc-macro2", - "quote", - "syn", - "wasmtime-component-util 14.0.4", - "wasmtime-wit-bindgen 14.0.4", - "wit-parser 0.12.2", -] - [[package]] name = "wasmtime-component-macro" version = "18.0.2" @@ -1801,17 +1703,11 @@ dependencies = [ "proc-macro2", "quote", "syn", - "wasmtime-component-util 18.0.2", - "wasmtime-wit-bindgen 18.0.2", - "wit-parser 0.13.2", + "wasmtime-component-util", + "wasmtime-wit-bindgen", + "wit-parser", ] -[[package]] -name = "wasmtime-component-util" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f9141a8df069e106eee0c3a8173c0809cf1a4b5630628cfb1f25ab114720093" - [[package]] name = "wasmtime-component-util" version = "18.0.2" @@ -1820,15 +1716,15 @@ checksum = "1d6d69c430cddc70ec42159506962c66983ce0192ebde4eb125b7aabc49cff88" [[package]] name = "wasmtime-cranelift" -version = "14.0.4" +version = "18.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cf3cee8be02f5006d21b773ffd6802f96a0b7d661ff2ad8a01fb93df458b1aa" +checksum = "31ca62f519225492bd555d0ec85a2dacb0c10315db3418c8b9aeb3824bf54a24" dependencies = [ "anyhow", "cfg-if", "cranelift-codegen", "cranelift-control", - "cranelift-entity 0.101.4", + "cranelift-entity", "cranelift-frontend", "cranelift-native", "cranelift-wasm", @@ -1837,17 +1733,17 @@ dependencies = [ "object", "target-lexicon", "thiserror", - "wasmparser 0.115.0", + "wasmparser", "wasmtime-cranelift-shared", - "wasmtime-environ 14.0.4", - "wasmtime-versioned-export-macros 14.0.4", + "wasmtime-environ", + "wasmtime-versioned-export-macros", ] [[package]] name = "wasmtime-cranelift-shared" -version = "14.0.4" +version = "18.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "420fd2a69bc162957f4c94f21c7fa08ecf60d916f4e87b56332507c555da381d" +checksum = "fd5f2071f42e61490bf7cb95b9acdbe6a29dd577a398019304a960585f28b844" dependencies = [ "anyhow", "cranelift-codegen", @@ -1856,30 +1752,7 @@ dependencies = [ "gimli", "object", "target-lexicon", - "wasmtime-environ 14.0.4", -] - -[[package]] -name = "wasmtime-environ" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb6a445ce2b2810127caee6c1b79b8da4ae57712b05556a674592c18b7500a14" -dependencies = [ - "anyhow", - "cranelift-entity 0.101.4", - "gimli", - "indexmap", - "log", - "object", - "serde", - "serde_derive", - "target-lexicon", - "thiserror", - "wasm-encoder 0.35.0", - "wasmparser 0.115.0", - "wasmprinter", - "wasmtime-component-util 14.0.4", - "wasmtime-types 14.0.4", + "wasmtime-environ", ] [[package]] @@ -1890,31 +1763,22 @@ checksum = "82bf1a47f384610da19f58b0fd392ca6a3b720974315c08afb0392c0f3951fed" dependencies = [ "anyhow", "bincode", - "cranelift-entity 0.105.2", + "cpp_demangle", + "cranelift-entity", "gimli", "indexmap", "log", "object", + "rustc-demangle", "serde", "serde_derive", "target-lexicon", "thiserror", - "wasmparser 0.121.2", - "wasmtime-types 18.0.2", -] - -[[package]] -name = "wasmtime-fiber" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "345a8b061c9eab459e10b9112df9fc357d5a9e8b5b1004bc5fc674fba9be6d2a" -dependencies = [ - "cc", - "cfg-if", - "rustix", - "wasmtime-asm-macros 14.0.4", - "wasmtime-versioned-export-macros 14.0.4", - "windows-sys 0.48.0", + "wasm-encoder 0.41.2", + "wasmparser", + "wasmprinter", + "wasmtime-component-util", + "wasmtime-types", ] [[package]] @@ -1927,59 +1791,21 @@ dependencies = [ "cc", "cfg-if", "rustix", - "wasmtime-asm-macros 18.0.2", - "wasmtime-versioned-export-macros 18.0.2", + "wasmtime-asm-macros", + "wasmtime-versioned-export-macros", "windows-sys 0.52.0", ] -[[package]] -name = "wasmtime-jit" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f0f6586c61125fbfc13c3108c3dd565d21f314dd5bac823b9a5b7ab576d21f1" -dependencies = [ - "addr2line", - "anyhow", - "bincode", - "cfg-if", - "cpp_demangle", - "gimli", - "ittapi", - "log", - "object", - "rustc-demangle", - "rustix", - "serde", - "serde_derive", - "target-lexicon", - "wasmtime-environ 14.0.4", - "wasmtime-jit-debug", - "wasmtime-jit-icache-coherence 14.0.4", - "wasmtime-runtime 14.0.4", - "windows-sys 0.48.0", -] - [[package]] name = "wasmtime-jit-debug" -version = "14.0.4" +version = "18.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "109a9e46afe33580b952b14a4207354355f19bcdf0b47485b397b68409eaf553" +checksum = "833dae95bc7a4f9177bf93f9497419763535b74e37eb8c37be53937d3281e287" dependencies = [ "object", "once_cell", "rustix", - "wasmtime-versioned-export-macros 14.0.4", -] - -[[package]] -name = "wasmtime-jit-icache-coherence" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f67e6be36375c39cff57ed3b137ab691afbf2d9ba8ee1c01f77888413f218749" -dependencies = [ - "cfg-if", - "libc", - "windows-sys 0.48.0", + "wasmtime-versioned-export-macros", ] [[package]] @@ -1993,36 +1819,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "wasmtime-runtime" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d07986b2327b5e7f535ed638fbde25990fc8f85400194fda0d26db71c7b685e" -dependencies = [ - "anyhow", - "cc", - "cfg-if", - "encoding_rs", - "indexmap", - "libc", - "log", - "mach", - "memfd", - "memoffset", - "paste", - "rand", - "rustix", - "sptr", - "wasm-encoder 0.35.0", - "wasmtime-asm-macros 14.0.4", - "wasmtime-environ 14.0.4", - "wasmtime-fiber 14.0.4", - "wasmtime-jit-debug", - "wasmtime-versioned-export-macros 14.0.4", - "wasmtime-wmemcheck 14.0.4", - "windows-sys 0.48.0", -] - [[package]] name = "wasmtime-runtime" version = "18.0.2" @@ -2032,6 +1828,7 @@ dependencies = [ "anyhow", "cc", "cfg-if", + "encoding_rs", "indexmap", "libc", "log", @@ -2043,49 +1840,26 @@ dependencies = [ "rustix", "sptr", "wasm-encoder 0.41.2", - "wasmtime-asm-macros 18.0.2", - "wasmtime-environ 18.0.2", - "wasmtime-fiber 18.0.2", - "wasmtime-versioned-export-macros 18.0.2", - "wasmtime-wmemcheck 18.0.2", + "wasmtime-asm-macros", + "wasmtime-environ", + "wasmtime-fiber", + "wasmtime-jit-debug", + "wasmtime-versioned-export-macros", + "wasmtime-wmemcheck", "windows-sys 0.52.0", ] -[[package]] -name = "wasmtime-types" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e810a0d2e869abd1cb42bd232990f6bd211672b3d202d2ae7e70ffb97ed70ea3" -dependencies = [ - "cranelift-entity 0.101.4", - "serde", - "serde_derive", - "thiserror", - "wasmparser 0.115.0", -] - [[package]] name = "wasmtime-types" version = "18.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54a327d7a0ef57bd52a507d28b4561a74126c7a8535a2fc6f2025716bc6a52e8" dependencies = [ - "cranelift-entity 0.105.2", + "cranelift-entity", "serde", "serde_derive", "thiserror", - "wasmparser 0.121.2", -] - -[[package]] -name = "wasmtime-versioned-export-macros" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09b5575a75e711ca6c36bb9ad647c93541cdc8e34218031acba5da3f35919dd3" -dependencies = [ - "proc-macro2", - "quote", - "syn", + "wasmparser", ] [[package]] @@ -2101,9 +1875,9 @@ dependencies = [ [[package]] name = "wasmtime-wasi" -version = "14.0.4" +version = "18.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e6730a2853226292cee755a36549dd1a443b324cf99319cb390af1afed6cb8a" +checksum = "d04d2fb2257245aa05ff799ded40520ae4d8cd31b0d14972afac89061f12fe12" dependencies = [ "anyhow", "async-trait", @@ -2118,7 +1892,6 @@ dependencies = [ "futures", "io-extras", "io-lifetimes", - "libc", "log", "once_cell", "rustix", @@ -2127,42 +1900,29 @@ dependencies = [ "tokio", "tracing", "url", - "wasi-cap-std-sync", "wasi-common", - "wasmtime 14.0.4", - "wiggle 14.0.4", - "windows-sys 0.48.0", + "wasmtime", + "wiggle", + "windows-sys 0.52.0", ] [[package]] name = "wasmtime-winch" -version = "14.0.4" +version = "18.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1c1b6abbba5a01739bef9f00a87b419414a7dd99b795823d93fb12fc2bf994a" +checksum = "db3378c0e808a744b5d4df2a9a9d2746a53b151811926731f04fc401707f7d54" dependencies = [ "anyhow", "cranelift-codegen", "gimli", "object", "target-lexicon", - "wasmparser 0.115.0", + "wasmparser", "wasmtime-cranelift-shared", - "wasmtime-environ 14.0.4", + "wasmtime-environ", "winch-codegen", ] -[[package]] -name = "wasmtime-wit-bindgen" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d214ca7513d76af2872ad5bba4b0dcc0225821931745fdcb4fc30dd34bc3bf7" -dependencies = [ - "anyhow", - "heck", - "indexmap", - "wit-parser 0.12.2", -] - [[package]] name = "wasmtime-wit-bindgen" version = "18.0.2" @@ -2172,15 +1932,9 @@ dependencies = [ "anyhow", "heck", "indexmap", - "wit-parser 0.13.2", + "wit-parser", ] -[[package]] -name = "wasmtime-wmemcheck" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dafab2db172a53e23940e0fa3078c202f567ee5f13f4b42f66b694fab43c658" - [[package]] name = "wasmtime-wmemcheck" version = "18.0.2" @@ -2218,21 +1972,6 @@ dependencies = [ "wast 201.0.0", ] -[[package]] -name = "wiggle" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f6ce56a4019ce3d8592c298029a75abe6887d1c95a078a4c53ec77a0628262d" -dependencies = [ - "anyhow", - "async-trait", - "bitflags 2.4.2", - "thiserror", - "tracing", - "wasmtime 14.0.4", - "wiggle-macro 14.0.4", -] - [[package]] name = "wiggle" version = "18.0.2" @@ -2244,23 +1983,8 @@ dependencies = [ "bitflags 2.4.2", "thiserror", "tracing", - "wasmtime 18.0.2", - "wiggle-macro 18.0.2", - "witx", -] - -[[package]] -name = "wiggle-generate" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e585a4b1e84195031c77d8484af99cd93f129f45d519e83cb8cc75e9a420cfd3" -dependencies = [ - "anyhow", - "heck", - "proc-macro2", - "quote", - "shellexpand", - "syn", + "wasmtime", + "wiggle-macro", "witx", ] @@ -2279,18 +2003,6 @@ dependencies = [ "witx", ] -[[package]] -name = "wiggle-macro" -version = "14.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6f321dbce722989d65c3082dba479fa392c7b7a1a4c3adc2a39545dd5aa452f" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "wiggle-generate 14.0.4", -] - [[package]] name = "wiggle-macro" version = "18.0.2" @@ -2300,7 +2012,7 @@ dependencies = [ "proc-macro2", "quote", "syn", - "wiggle-generate 18.0.2", + "wiggle-generate", ] [[package]] @@ -2327,9 +2039,9 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "winch-codegen" -version = "0.12.4" +version = "0.16.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f112bebb367a544d20c254083798087f22ceeb426168a970b955e8436f749dca" +checksum = "8197ed4a2ebf612f0624ddda10de71f8cd2d3a4ecf8ffac0586a264599708d63" dependencies = [ "anyhow", "cranelift-codegen", @@ -2337,8 +2049,8 @@ dependencies = [ "regalloc2", "smallvec", "target-lexicon", - "wasmparser 0.115.0", - "wasmtime-environ 14.0.4", + "wasmparser", + "wasmtime-environ", ] [[package]] @@ -2492,23 +2204,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "wit-parser" -version = "0.12.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43771ee863a16ec4ecf9da0fc65c3bbd4a1235c8e3da5f094b562894843dfa76" -dependencies = [ - "anyhow", - "id-arena", - "indexmap", - "log", - "semver", - "serde", - "serde_derive", - "serde_json", - "unicode-xid", -] - [[package]] name = "wit-parser" version = "0.13.2" diff --git a/native/wasmex/Cargo.toml b/native/wasmex/Cargo.toml index b37b232d..c9aef1e7 100644 --- a/native/wasmex/Cargo.toml +++ b/native/wasmex/Cargo.toml @@ -21,8 +21,8 @@ rustler = { version = "0.31", default-features = false, features = [ ] } once_cell = "1.19.0" rand = "0.8.5" -wasmtime = "14.0.4" -wasmtime-wasi = "14.0.4" -wasi-common = "14.0.4" +wasmtime = "18.0.2" +wasmtime-wasi = "18.0.2" +wasi-common = "18.0.2" wiggle = "18.0.2" wat = "1.201.0" diff --git a/native/wasmex/src/lib.rs b/native/wasmex/src/lib.rs index 1387e662..a8cc719b 100644 --- a/native/wasmex/src/lib.rs +++ b/native/wasmex/src/lib.rs @@ -42,9 +42,8 @@ rustler::init! { pipe::seek, pipe::size, pipe::write_binary, - store::add_fuel, - store::consume_fuel, - store::fuel_consumed, + store::get_fuel, + store::set_fuel, store::new_wasi, store::new, ], diff --git a/native/wasmex/src/store.rs b/native/wasmex/src/store.rs index 3cc4b82b..d916c0b0 100644 --- a/native/wasmex/src/store.rs +++ b/native/wasmex/src/store.rs @@ -211,28 +211,8 @@ pub fn new_wasi( Ok(resource) } -#[rustler::nif(name = "store_or_caller_fuel_consumed")] -pub fn fuel_consumed( - store_or_caller_resource: ResourceArc, -) -> Result, rustler::Error> { - let store_or_caller: &StoreOrCaller = - &*(store_or_caller_resource.inner.try_lock().map_err(|e| { - rustler::Error::Term(Box::new(format!("Could not unlock store resource: {e}"))) - })?); - match store_or_caller { - StoreOrCaller::Store(store) => Ok(store.fuel_consumed()), - StoreOrCaller::Caller(token) => get_caller(token) - .ok_or_else(|| { - rustler::Error::Term(Box::new( - "Caller is not valid. Only use a caller within its own function scope.", - )) - }) - .map(|c| c.fuel_consumed()), - } -} - -#[rustler::nif(name = "store_or_caller_add_fuel")] -pub fn add_fuel( +#[rustler::nif(name = "store_or_caller_set_fuel")] +pub fn set_fuel( store_or_caller_resource: ResourceArc, fuel: u64, ) -> Result<(), rustler::Error> { @@ -241,38 +221,37 @@ pub fn add_fuel( rustler::Error::Term(Box::new(format!("Could not unlock store resource: {e}"))) })?); match store_or_caller { - StoreOrCaller::Store(store) => store.add_fuel(fuel), + StoreOrCaller::Store(store) => store.set_fuel(fuel), StoreOrCaller::Caller(token) => get_caller_mut(token) .ok_or_else(|| { rustler::Error::Term(Box::new( "Caller is not valid. Only use a caller within its own function scope.", )) }) - .map(|c| c.add_fuel(fuel))?, + .map(|c| c.set_fuel(fuel))?, } - .map_err(|e| rustler::Error::Term(Box::new(format!("Could not add fuel to store: {e}")))) + .map_err(|e| rustler::Error::Term(Box::new(format!("Could not set fuel: {e}")))) } -#[rustler::nif(name = "store_or_caller_consume_fuel")] -pub fn consume_fuel( +#[rustler::nif(name = "store_or_caller_get_fuel")] +pub fn get_fuel( store_or_caller_resource: ResourceArc, - fuel: u64, ) -> Result { let store_or_caller: &mut StoreOrCaller = &mut *(store_or_caller_resource.inner.try_lock().map_err(|e| { rustler::Error::Term(Box::new(format!("Could not unlock store resource: {e}"))) })?); match store_or_caller { - StoreOrCaller::Store(store) => store.consume_fuel(fuel), + StoreOrCaller::Store(store) => store.get_fuel(), StoreOrCaller::Caller(token) => get_caller_mut(token) .ok_or_else(|| { rustler::Error::Term(Box::new( "Caller is not valid. Only use a caller within its own function scope.", )) }) - .map(|c| c.consume_fuel(fuel))?, + .map(|c| c.get_fuel())?, } - .map_err(|e| rustler::Error::Term(Box::new(format!("Could not consume fuel: {e}")))) + .map_err(|e| rustler::Error::Term(Box::new(format!("Could not get fuel: {e}")))) } fn add_pipe( diff --git a/test/wasmex/store_or_caller_test.exs b/test/wasmex/store_or_caller_test.exs index a77de903..0e2245b9 100644 --- a/test/wasmex/store_or_caller_test.exs +++ b/test/wasmex/store_or_caller_test.exs @@ -5,12 +5,12 @@ defmodule Wasmex.StoreOrCallerTest do doctest Wasmex.StoreOrCaller - describe t(&StoreOrCaller.add_fuel/2) do + describe t(&StoreOrCaller.set_fuel/2) do test "adds fuel to a store" do {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) {:ok, store} = Wasmex.Store.new(nil, engine) - assert StoreOrCaller.add_fuel(store, 10) == :ok - assert StoreOrCaller.fuel_remaining(store) == {:ok, 10} + assert StoreOrCaller.set_fuel(store, 10) == :ok + assert StoreOrCaller.get_fuel(store) == {:ok, 10} end test "adds fuel from within an imported function" do @@ -25,45 +25,46 @@ defmodule Wasmex.StoreOrCallerTest do imported_sum3: {:fn, [:i32, :i32, :i32], [:i32], fn context, _a, _b, _c -> - # calling using_imported_sum3 spends 34 fuel, we add that and 42 fuel + # calling using_imported_sum3 spends 10 fuel, we add that and 42 fuel # more to assert that number later - :ok = StoreOrCaller.add_fuel(context.caller, 34 + 42) + :ok = StoreOrCaller.set_fuel(context.caller, 10 + 42) 0 end} }) } - :ok = StoreOrCaller.add_fuel(store, 10_000) + :ok = StoreOrCaller.set_fuel(store, 10_000) pid = start_supervised!({Wasmex, %{store: store, module: module, imports: imports}}) assert {:ok, [0]} = Wasmex.call_function(pid, "using_imported_sum3", [1, 2, 3]) - # 34 fuel is spent in the function_call, 42 fuel is added synthetically on top + # 10 fuel is spent in the function_call, 42 fuel is added synthetically on top # within the `imported_sum3` function. - # We started with 10_000 fuel, so we should have 10_000 - 34 + (34 + 42) = 10_042 fuel left - assert StoreOrCaller.fuel_remaining(store) == {:ok, 10_042} + # We started with 10_000 fuel, but set a different value (10 + 42) in the function - which + # should leave us with 42 fuel remaining. + assert StoreOrCaller.get_fuel(store) == {:ok, 42} end test "errors with a store that has fuel_consumption disabled" do {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: false}) {:ok, store} = Wasmex.Store.new(nil, engine) - assert StoreOrCaller.add_fuel(store, 1) == - {:error, "Could not add fuel to store: fuel is not configured in this store"} + assert StoreOrCaller.set_fuel(store, 1) == + {:error, "Could not set fuel: fuel is not configured in this store"} end end - describe t(&StoreOrCaller.fuel_remaining/1) do - test "reports fuel remaining with a fresh store" do + describe t(&StoreOrCaller.get_fuel/1) do + test "reports fuel with a fresh store" do {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) {:ok, store} = Wasmex.Store.new(nil, engine) - assert StoreOrCaller.fuel_remaining(store) == {:ok, 0} + assert StoreOrCaller.get_fuel(store) == {:ok, 0} end - test "reports fuel remaining with a store that has fuel" do + test "reports fuel with a store that has fuel" do {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) {:ok, store} = Wasmex.Store.new(nil, engine) - assert StoreOrCaller.add_fuel(store, 42) == :ok - assert StoreOrCaller.fuel_remaining(store) == {:ok, 42} + assert StoreOrCaller.set_fuel(store, 42) == :ok + assert StoreOrCaller.get_fuel(store) == {:ok, 42} end test "reports fuel from within an imported function" do @@ -78,127 +79,25 @@ defmodule Wasmex.StoreOrCallerTest do imported_sum3: {:fn, [:i32, :i32, :i32], [:i32], fn context, _a, _b, _c -> - {:ok, fuel_remaining} = StoreOrCaller.fuel_remaining(context.caller) - fuel_remaining + {:ok, fuel} = StoreOrCaller.get_fuel(context.caller) + fuel end} }) } - :ok = StoreOrCaller.add_fuel(store, 10_000) + :ok = StoreOrCaller.set_fuel(store, 10_000) pid = start_supervised!({Wasmex, %{store: store, module: module, imports: imports}}) - assert {:ok, [fuel_remaining]} = Wasmex.call_function(pid, "using_imported_sum3", [1, 2, 3]) + assert {:ok, [fuel]} = Wasmex.call_function(pid, "using_imported_sum3", [1, 2, 3]) - assert fuel_remaining == 9_976 + assert fuel == 9_976 end test "with a store that has fuel_consumption disabled" do {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: false}) {:ok, store} = Wasmex.Store.new(nil, engine) - assert StoreOrCaller.fuel_remaining(store) == {:ok, 0} - end - end - - describe t(&StoreOrCaller.consume_fuel/2) do - test "consumes fuel on a store that has more fuel than consumed" do - {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) - {:ok, store} = Wasmex.Store.new(nil, engine) - assert StoreOrCaller.add_fuel(store, 10) == :ok - assert StoreOrCaller.consume_fuel(store, 8) == {:ok, 2} - assert StoreOrCaller.consume_fuel(store, 2) == {:ok, 0} - end - - test "consumes fuel on a store that has less fuel than consumed" do - {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) - {:ok, store} = Wasmex.Store.new(nil, engine) - assert StoreOrCaller.add_fuel(store, 10) == :ok - - assert StoreOrCaller.consume_fuel(store, 18) == - {:error, "Could not consume fuel: not enough fuel remaining in store"} - end - - test "consumes fuel from within an imported function" do - {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) - {:ok, store} = Wasmex.Store.new(nil, engine) - bytes = File.read!(TestHelper.wasm_import_test_file_path()) - {:ok, module} = Wasmex.Module.compile(store, bytes) - - imports = %{ - env: - Map.merge(TestHelper.default_imported_functions_env(), %{ - imported_sum3: - {:fn, [:i32, :i32, :i32], [:i32], - fn context, _a, _b, _c -> - {:ok, remaining_fuel} = StoreOrCaller.consume_fuel(context.caller, 976) - remaining_fuel - end} - }) - } - - :ok = StoreOrCaller.add_fuel(store, 10_000) - pid = start_supervised!({Wasmex, %{store: store, module: module, imports: imports}}) - - assert {:ok, [remaining_fuel]} = Wasmex.call_function(pid, "using_imported_sum3", [1, 2, 3]) - assert remaining_fuel == 9_000 - end - - test "errors with a store that has fuel_consumption disabled" do - {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: false}) - {:ok, store} = Wasmex.Store.new(nil, engine) - - assert StoreOrCaller.consume_fuel(store, 1) == - {:error, "Could not consume fuel: not enough fuel remaining in store"} - end - end - - describe t(&StoreOrCaller.fuel_consumed/2) do - test "reports fuel consumption with a fresh store" do - {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) - {:ok, store} = Wasmex.Store.new(nil, engine) - assert StoreOrCaller.add_fuel(store, 10) == :ok - assert StoreOrCaller.fuel_consumed(store) == {:ok, 0} - assert StoreOrCaller.consume_fuel(store, 2) == {:ok, 8} - assert StoreOrCaller.fuel_consumed(store) == {:ok, 2} - end - - test "reports fuel consumption from within an imported function" do - {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true}) - {:ok, store} = Wasmex.Store.new(nil, engine) - bytes = File.read!(TestHelper.wasm_import_test_file_path()) - {:ok, module} = Wasmex.Module.compile(store, bytes) - - imports = %{ - env: - Map.merge(TestHelper.default_imported_functions_env(), %{ - imported_sum3: - {:fn, [:i32, :i32, :i32], [:i32], - fn context, _a, _b, _c -> - {:ok, fuel_consumed_in_imported_fun} = - StoreOrCaller.fuel_consumed(context.caller) - - fuel_consumed_in_imported_fun - end} - }) - } - - :ok = StoreOrCaller.add_fuel(store, 10_000) - pid = start_supervised!({Wasmex, %{store: store, module: module, imports: imports}}) - - assert {:ok, [fuel_consumed_in_imported_fun]} = - Wasmex.call_function(pid, "using_imported_sum3", [1, 2, 3]) - - assert fuel_consumed_in_imported_fun == 24 - - # after full function execution - assert StoreOrCaller.fuel_consumed(store) == {:ok, 34} - end - - test "errors with a store that has fuel_consumption disabled" do - {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: false}) - {:ok, store} = Wasmex.Store.new(nil, engine) - - assert StoreOrCaller.fuel_consumed(store) == - {:error, "Could not consume fuel: fuel is not configured in this store"} + assert StoreOrCaller.get_fuel(store) == + {:error, "Could not get fuel: fuel is not configured in this store"} end end end diff --git a/test/wasmex_test.exs b/test/wasmex_test.exs index c2fff63c..6cc8c715 100644 --- a/test/wasmex_test.exs +++ b/test/wasmex_test.exs @@ -313,10 +313,10 @@ defmodule WasmexTest do {:ok, store} = Wasmex.Store.new(nil, engine) bytes = File.read!(TestHelper.wasm_test_file_path()) pid = start_supervised!({Wasmex, %{store: store, bytes: bytes}}) - Wasmex.StoreOrCaller.add_fuel(store, 2) + Wasmex.StoreOrCaller.set_fuel(store, 2) assert Wasmex.call_function(pid, :void, []) == {:ok, []} - assert Wasmex.StoreOrCaller.fuel_remaining(store) == {:ok, 1} + assert Wasmex.StoreOrCaller.get_fuel(store) == {:ok, 1} assert {:error, err_msg} = Wasmex.call_function(pid, :void, [])