diff --git a/Cargo.lock b/Cargo.lock index 7fc987a2..8fed5afe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,7 +151,7 @@ dependencies = [ "cfg-if", "libc", "miniz_oxide", - "object 0.36.0", + "object 0.36.1", "rustc-demangle", ] @@ -169,9 +169,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "block-buffer" @@ -289,9 +289,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.100" +version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c891175c3fb232128f48de6590095e59198bbeb8620c310be349bfc3afd12c7b" +checksum = "74b6a57f98764a267ff415d50a25e6e166f3831a5071af4995296ea97d210490" dependencies = [ "jobserver", "libc", @@ -306,9 +306,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.5.7" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5db83dced34638ad474f39f250d7fea9598bdd239eaced1bdf45d597da0f433f" +checksum = "84b3edb18336f4df585bc9aa31dd99c036dfa5dc5e9a2939a722a188f3a8970d" dependencies = [ "clap_builder", "clap_derive", @@ -316,9 +316,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.7" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7e204572485eb3fbf28f871612191521df159bc3e15a9f5064c66dba3a8c05f" +checksum = "c1c09dd5ada6c6c78075d6fd0da3f90d8080651e2d6cc8eb2f1aaa4034ced708" dependencies = [ "anstream", "anstyle", @@ -328,9 +328,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.5" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6" +checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085" dependencies = [ "heck 0.5.0", "proc-macro2", @@ -625,9 +625,9 @@ dependencies = [ [[package]] name = "either" -version = "1.12.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "embedded-io" @@ -846,7 +846,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27d12c0aed7f1e24276a241aadc4cb8ea9f83000f34bc062b7cc2d51e3b0fabd" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "debugid", "fxhash", "serde", @@ -1202,7 +1202,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "libc", ] @@ -1224,9 +1224,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.21" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "mach2" @@ -1327,9 +1327,9 @@ dependencies = [ [[package]] name = "object" -version = "0.36.0" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "576dfe1fc8f9df304abb159d767a29d0476f7750fbf8aa7ad07816004a207434" +checksum = "081b846d1d56ddfc18fdf1a922e4f6e07a11768ea1b92dec44e42b72712ccfce" dependencies = [ "crc32fast", "hashbrown 0.14.5", @@ -1561,7 +1561,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", ] [[package]] @@ -1665,7 +1665,7 @@ version = "0.38.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "errno", "itoa", "libc", @@ -1763,7 +1763,7 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "core-foundation", "core-foundation-sys", "libc", @@ -1823,9 +1823,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.117" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", "ryu", @@ -1998,7 +1998,7 @@ version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b858526d22750088a9b3cf2e3c2aacebd5377f13adeec02860c30d09113010a6" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "cap-fs-ext", "cap-std", "fd-lock", @@ -2067,9 +2067,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.6.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +checksum = "c55115c6fbe2d2bef26eb09ad74bde02d8255476fc0c7b515ef09fbb35742d82" dependencies = [ "tinyvec_macros", ] @@ -2337,9 +2337,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "uuid" -version = "1.9.0" +version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ea73390fe27785838dcbf75b91b1d84799e28f1ce71e6f372a5dc2200c80de5" +checksum = "5de17fd2f7da591098415cff336e12965a28061ddace43b59cb3c430179c9439" [[package]] name = "valuable" @@ -2385,7 +2385,7 @@ dependencies = [ name = "viceroy-component-adapter" version = "0.0.0" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "byte-array-literals", "object 0.33.0", "wasi", @@ -2433,6 +2433,7 @@ dependencies = [ "wasmtime", "wasmtime-wasi", "wasmtime-wasi-nn", + "wat", "wiggle", "wit-component", ] @@ -2537,9 +2538,9 @@ dependencies = [ [[package]] name = "wasm-encoder" -version = "0.211.1" +version = "0.212.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e7d931a1120ef357f32b74547646b6fa68ea25e377772b72874b131a9ed70d4" +checksum = "501940df4418b8929eb6d52f1aade1fdd15a5b86c92453cb696e3c906bd3fc33" dependencies = [ "leb128", ] @@ -2567,7 +2568,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd921789c9dcc495f589cb37d200155dee65b4a4beeb853323b5e24e0a5f9c58" dependencies = [ "ahash", - "bitflags 2.5.0", + "bitflags 2.6.0", "hashbrown 0.14.5", "indexmap", "semver 1.0.23", @@ -2581,7 +2582,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07035cc9a9b41e62d3bb3a3815a66ab87c993c06fe1cf6b2a3f2a18499d937db" dependencies = [ "ahash", - "bitflags 2.5.0", + "bitflags 2.6.0", "hashbrown 0.14.5", "indexmap", "semver 1.0.23", @@ -2622,7 +2623,7 @@ dependencies = [ "mach2", "memfd", "memoffset", - "object 0.36.0", + "object 0.36.1", "once_cell", "paste", "postcard", @@ -2720,7 +2721,7 @@ dependencies = [ "cranelift-wasm", "gimli 0.28.1", "log", - "object 0.36.0", + "object 0.36.1", "target-lexicon", "thiserror", "wasmparser 0.209.1", @@ -2740,7 +2741,7 @@ dependencies = [ "gimli 0.28.1", "indexmap", "log", - "object 0.36.0", + "object 0.36.1", "postcard", "rustc-demangle", "serde", @@ -2774,7 +2775,7 @@ version = "22.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9bc54198c6720f098210a85efb3ba8c078d1de4d373cdb6778850a66ae088d11" dependencies = [ - "object 0.36.0", + "object 0.36.1", "once_cell", "rustix", "wasmtime-versioned-export-macros", @@ -2830,7 +2831,7 @@ checksum = "8abb1301089ed8e0b4840f539cba316a73ac382090f1b25d22d8c8eed8df49c7" dependencies = [ "anyhow", "async-trait", - "bitflags 2.5.0", + "bitflags 2.6.0", "bytes", "cap-fs-ext", "cap-net-ext", @@ -2877,7 +2878,7 @@ dependencies = [ "anyhow", "cranelift-codegen", "gimli 0.28.1", - "object 0.36.0", + "object 0.36.1", "target-lexicon", "wasmparser 0.209.1", "wasmtime-cranelift", @@ -2908,24 +2909,24 @@ dependencies = [ [[package]] name = "wast" -version = "211.0.1" +version = "212.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b25506dd82d00da6b14a87436b3d52b1d264083fa79cdb72a0d1b04a8595ccaa" +checksum = "4606a05fb0aae5d11dd7d8280a640d88a63ee019360ba9be552da3d294b8d1f5" dependencies = [ "bumpalo", "leb128", "memchr", "unicode-width", - "wasm-encoder 0.211.1", + "wasm-encoder 0.212.0", ] [[package]] name = "wat" -version = "1.211.1" +version = "1.212.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb716ca6c86eecac2d82541ffc39860118fc0af9309c4f2670637bea2e1bdd7d" +checksum = "c74ca7f93f11a5d6eed8499f2a8daaad6e225cab0151bc25a091fff3b987532f" dependencies = [ - "wast 211.0.1", + "wast 212.0.0", ] [[package]] @@ -2936,7 +2937,7 @@ checksum = "29830e5d01c182d24b94092c697aa7ab0ee97d22e78a2bf40ca91eae6ebca5c2" dependencies = [ "anyhow", "async-trait", - "bitflags 2.5.0", + "bitflags 2.6.0", "thiserror", "tracing", "wasmtime", @@ -3182,7 +3183,7 @@ version = "0.36.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9643b83820c0cd246ecabe5fa454dd04ba4fa67996369466d0747472d337346" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "windows-sys 0.52.0", ] @@ -3232,7 +3233,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fef7dd0e47f5135dd8739ccc5b188ab8b7e27e1d64df668aa36680f0b8646db8" dependencies = [ "anyhow", - "bitflags 2.5.0", + "bitflags 2.6.0", "indexmap", "log", "serde", diff --git a/Cargo.toml b/Cargo.toml index a78f8ca0..10705ddf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ wasmtime = "22.0.0" wasmtime-wasi = "22.0.0" wasmtime-wasi-nn = "22.0.0" wiggle = "22.0.0" +wat = "1.212.0" wasmparser = "0.208.0" wasm-encoder = { version = "0.208.0", features = ["wasmparser"] } wit-component = "0.208.0" diff --git a/cli/src/main.rs b/cli/src/main.rs index 1c02d222..7df98cae 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -109,11 +109,40 @@ pub async fn main() -> ExitCode { } }; - let module = match viceroy_lib::adapt::adapt_bytes(&bytes) { - Ok(module) => module, - Err(e) => { - event!(Level::ERROR, "Failed to adapt module: {e}"); - return ExitCode::FAILURE; + if viceroy_lib::adapt::is_component(&bytes) { + event!( + Level::ERROR, + "File is already a component: {}", + input.display() + ); + return ExitCode::FAILURE; + } + + let is_wat = input.extension().map(|str| str == "wat").unwrap_or(false); + + let module = if is_wat { + let text = match String::from_utf8(bytes) { + Ok(module) => module, + Err(e) => { + event!(Level::ERROR, "Failed to parse wat: {e}"); + return ExitCode::FAILURE; + } + }; + + match viceroy_lib::adapt::adapt_wat(&text) { + Ok(module) => module, + Err(e) => { + event!(Level::ERROR, "Failed to adapt wat: {e}"); + return ExitCode::FAILURE; + } + } + } else { + match viceroy_lib::adapt::adapt_bytes(&bytes) { + Ok(module) => module, + Err(e) => { + event!(Level::ERROR, "Failed to adapt module: {e}"); + return ExitCode::FAILURE; + } } }; diff --git a/cli/tests/integration/memory.rs b/cli/tests/integration/memory.rs index 461adfb7..86a2e46b 100644 --- a/cli/tests/integration/memory.rs +++ b/cli/tests/integration/memory.rs @@ -1,19 +1,22 @@ -use crate::common::{Test, TestResult}; +use crate::{ + common::{Test, TestResult}, + viceroy_test, +}; use hyper::body::to_bytes; use hyper::{Request, StatusCode}; -#[tokio::test(flavor = "multi_thread")] -async fn direct_wasm_works() -> TestResult { +viceroy_test!(direct_wasm_works, |is_component| { let resp = Test::using_wat_fixture("return_ok.wat") + .adapt_component(is_component) .against_empty() .await?; assert_eq!(resp.status(), StatusCode::OK); Ok(()) -} +}); -#[tokio::test(flavor = "multi_thread")] -async fn heap_limit_test_ok() -> TestResult { +viceroy_test!(heap_limit_test_ok, |is_component| { let resp = Test::using_wat_fixture("combined_heap_limits.wat") + .adapt_component(is_component) .against( Request::get("/") .header("guest-kb", "235") @@ -34,11 +37,11 @@ async fn heap_limit_test_ok() -> TestResult { let body = resp.into_body(); assert_eq!(to_bytes(body).await.unwrap().len(), 16 * 1024); Ok(()) -} +}); -#[tokio::test(flavor = "multi_thread")] -async fn heap_limit_test_bad() -> TestResult { +viceroy_test!(heap_limit_test_bad, |is_component| { let resp = Test::using_wat_fixture("combined_heap_limits.wat") + .adapt_component(is_component) .against( Request::get("/") .header("guest-kb", "150000") @@ -48,4 +51,4 @@ async fn heap_limit_test_bad() -> TestResult { .await?; assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR); Ok(()) -} +}); diff --git a/cli/tests/trap-test/Cargo.lock b/cli/tests/trap-test/Cargo.lock index 25bfe678..f74ea7b5 100644 --- a/cli/tests/trap-test/Cargo.lock +++ b/cli/tests/trap-test/Cargo.lock @@ -184,9 +184,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "block-buffer" @@ -300,9 +300,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.100" +version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c891175c3fb232128f48de6590095e59198bbeb8620c310be349bfc3afd12c7b" +checksum = "74b6a57f98764a267ff415d50a25e6e166f3831a5071af4995296ea97d210490" dependencies = [ "jobserver", "libc", @@ -329,9 +329,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.7" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5db83dced34638ad474f39f250d7fea9598bdd239eaced1bdf45d597da0f433f" +checksum = "84b3edb18336f4df585bc9aa31dd99c036dfa5dc5e9a2939a722a188f3a8970d" dependencies = [ "clap_builder", "clap_derive", @@ -339,9 +339,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.7" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7e204572485eb3fbf28f871612191521df159bc3e15a9f5064c66dba3a8c05f" +checksum = "c1c09dd5ada6c6c78075d6fd0da3f90d8080651e2d6cc8eb2f1aaa4034ced708" dependencies = [ "anstream", "anstyle", @@ -351,9 +351,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.5" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6" +checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085" dependencies = [ "heck 0.5.0", "proc-macro2", @@ -635,9 +635,9 @@ dependencies = [ [[package]] name = "either" -version = "1.12.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "embedded-io" @@ -850,7 +850,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27d12c0aed7f1e24276a241aadc4cb8ea9f83000f34bc062b7cc2d51e3b0fabd" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "debugid", "fxhash", "serde", @@ -1206,7 +1206,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "libc", ] @@ -1228,9 +1228,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.21" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "mach2" @@ -1321,9 +1321,9 @@ dependencies = [ [[package]] name = "object" -version = "0.36.0" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "576dfe1fc8f9df304abb159d767a29d0476f7750fbf8aa7ad07816004a207434" +checksum = "081b846d1d56ddfc18fdf1a922e4f6e07a11768ea1b92dec44e42b72712ccfce" dependencies = [ "crc32fast", "hashbrown 0.14.5", @@ -1549,7 +1549,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", ] [[package]] @@ -1653,7 +1653,7 @@ version = "0.38.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "errno", "itoa", "libc", @@ -1751,7 +1751,7 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "core-foundation", "core-foundation-sys", "libc", @@ -1811,9 +1811,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.117" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", "ryu", @@ -1961,7 +1961,7 @@ version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b858526d22750088a9b3cf2e3c2aacebd5377f13adeec02860c30d09113010a6" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "cap-fs-ext", "cap-std", "fd-lock", @@ -2018,9 +2018,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.6.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +checksum = "c55115c6fbe2d2bef26eb09ad74bde02d8255476fc0c7b515ef09fbb35742d82" dependencies = [ "tinyvec_macros", ] @@ -2301,9 +2301,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "uuid" -version = "1.9.0" +version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ea73390fe27785838dcbf75b91b1d84799e28f1ce71e6f372a5dc2200c80de5" +checksum = "5de17fd2f7da591098415cff336e12965a28061ddace43b59cb3c430179c9439" [[package]] name = "valuable" @@ -2356,6 +2356,7 @@ dependencies = [ "wasmtime", "wasmtime-wasi", "wasmtime-wasi-nn", + "wat", "wiggle", "wit-component", ] @@ -2460,9 +2461,9 @@ dependencies = [ [[package]] name = "wasm-encoder" -version = "0.211.1" +version = "0.212.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e7d931a1120ef357f32b74547646b6fa68ea25e377772b72874b131a9ed70d4" +checksum = "501940df4418b8929eb6d52f1aade1fdd15a5b86c92453cb696e3c906bd3fc33" dependencies = [ "leb128", ] @@ -2490,7 +2491,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd921789c9dcc495f589cb37d200155dee65b4a4beeb853323b5e24e0a5f9c58" dependencies = [ "ahash", - "bitflags 2.5.0", + "bitflags 2.6.0", "hashbrown 0.14.5", "indexmap", "semver 1.0.23", @@ -2504,7 +2505,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07035cc9a9b41e62d3bb3a3815a66ab87c993c06fe1cf6b2a3f2a18499d937db" dependencies = [ "ahash", - "bitflags 2.5.0", + "bitflags 2.6.0", "hashbrown 0.14.5", "indexmap", "semver 1.0.23", @@ -2753,7 +2754,7 @@ checksum = "8abb1301089ed8e0b4840f539cba316a73ac382090f1b25d22d8c8eed8df49c7" dependencies = [ "anyhow", "async-trait", - "bitflags 2.5.0", + "bitflags 2.6.0", "bytes", "cap-fs-ext", "cap-net-ext", @@ -2831,24 +2832,24 @@ dependencies = [ [[package]] name = "wast" -version = "211.0.1" +version = "212.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b25506dd82d00da6b14a87436b3d52b1d264083fa79cdb72a0d1b04a8595ccaa" +checksum = "4606a05fb0aae5d11dd7d8280a640d88a63ee019360ba9be552da3d294b8d1f5" dependencies = [ "bumpalo", "leb128", "memchr", "unicode-width", - "wasm-encoder 0.211.1", + "wasm-encoder 0.212.0", ] [[package]] name = "wat" -version = "1.211.1" +version = "1.212.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb716ca6c86eecac2d82541ffc39860118fc0af9309c4f2670637bea2e1bdd7d" +checksum = "c74ca7f93f11a5d6eed8499f2a8daaad6e225cab0151bc25a091fff3b987532f" dependencies = [ - "wast 211.0.1", + "wast 212.0.0", ] [[package]] @@ -2859,7 +2860,7 @@ checksum = "29830e5d01c182d24b94092c697aa7ab0ee97d22e78a2bf40ca91eae6ebca5c2" dependencies = [ "anyhow", "async-trait", - "bitflags 2.5.0", + "bitflags 2.6.0", "thiserror", "tracing", "wasmtime", @@ -3105,7 +3106,7 @@ version = "0.36.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9643b83820c0cd246ecabe5fa454dd04ba4fa67996369466d0747472d337346" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "windows-sys 0.52.0", ] @@ -3116,7 +3117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fef7dd0e47f5135dd8739ccc5b188ab8b7e27e1d64df668aa36680f0b8646db8" dependencies = [ "anyhow", - "bitflags 2.5.0", + "bitflags 2.6.0", "indexmap", "log", "serde", diff --git a/crates/adapter/src/lib.rs b/crates/adapter/src/lib.rs index dc087180..aa6f2dc2 100644 --- a/crates/adapter/src/lib.rs +++ b/crates/adapter/src/lib.rs @@ -1256,8 +1256,10 @@ pub unsafe extern "C" fn poll_oneoff( /// termination of the program. The meanings of other values is dependent on /// the environment. #[no_mangle] -pub unsafe extern "C" fn proc_exit(_rval: Exitcode) -> ! { - unreachable!("no other implementation available in proxy world"); +pub unsafe extern "C" fn proc_exit(rval: Exitcode) -> ! { + let status = if rval == 0 { Ok(()) } else { Err(()) }; + crate::bindings::wasi::cli::exit::exit(status); // does not return + unreachable!("host exit implementation didn't exit!") // actually unreachable } /// Send a signal to the process of the calling thread. diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 5def6f37..7188c886 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -61,6 +61,7 @@ wit-component = { workspace = true } wasmtime = { workspace = true } wasmtime-wasi = { workspace = true } wasmtime-wasi-nn = { workspace = true } +wat = { workspace = true } wiggle = { workspace = true } [dev-dependencies] diff --git a/lib/data/viceroy-component-adapter.wasm b/lib/data/viceroy-component-adapter.wasm index ca23ca27..3202e302 100755 Binary files a/lib/data/viceroy-component-adapter.wasm and b/lib/data/viceroy-component-adapter.wasm differ diff --git a/lib/src/adapt.rs b/lib/src/adapt.rs index 3dd0d6f7..ac281e09 100644 --- a/lib/src/adapt.rs +++ b/lib/src/adapt.rs @@ -1,5 +1,26 @@ const ADAPTER_BYTES: &[u8] = include_bytes!("../data/viceroy-component-adapter.wasm"); +/// Check if the bytes represent a core wasm module, or a component. +pub fn is_component(bytes: &[u8]) -> bool { + matches!( + wasmparser::Parser::new(0).parse(&bytes, true), + Ok(wasmparser::Chunk::Parsed { + payload: wasmparser::Payload::Version { + encoding: wasmparser::Encoding::Component, + .. + }, + .. + }) + ) +} + +/// Given bytes that represent a core wasm module in the wat format, adapt it to a component using +/// the viceroy adapter. +pub fn adapt_wat(wat: &str) -> anyhow::Result> { + let bytes = wat::parse_str(wat)?; + adapt_bytes(&bytes) +} + /// Given bytes that represent a core wasm module, adapt it to a component using the viceroy /// adapter. pub fn adapt_bytes(bytes: &[u8]) -> anyhow::Result> { diff --git a/lib/src/execute.rs b/lib/src/execute.rs index 923a0435..60836dbe 100644 --- a/lib/src/execute.rs +++ b/lib/src/execute.rs @@ -117,31 +117,22 @@ impl ExecuteCtx { .map(|str| str == "wat") .unwrap_or(false); - let is_component = matches!( - wasmparser::Parser::new(0).parse(&input, true), - Ok(wasmparser::Chunk::Parsed { - payload: wasmparser::Payload::Version { - encoding: wasmparser::Encoding::Component, - .. - }, - .. - }) - ); - // When the input wasn't a component, but we're automatically adapting, // apply the component adapter. - let (is_component, input) = if !is_component && adapt_components { - // It's not possible to adapt a component from WAT, we can't continue at this point. - if is_wat { - return Err(Error::Other(anyhow::anyhow!( - "Wasm components may only be adapted from binary wasm components, not wat" - ))); - } + let is_component = adapt::is_component(&input); + let (is_wat, is_component, input) = if !is_component && adapt_components { + let input = if is_wat { + let text = String::from_utf8(input).map_err(|_| { + anyhow::anyhow!("Failed to parse {}", module_path.as_ref().display()) + })?; + adapt::adapt_wat(&text)? + } else { + adapt::adapt_bytes(&input)? + }; - let input = adapt::adapt_bytes(&input)?; - (true, input) + (false, true, input) } else { - (is_component, input) + (is_wat, is_component, input) }; let config = &configure_wasmtime(is_component, profiling_strategy); diff --git a/lib/wit/deps/fastly/compute.wit b/lib/wit/deps/fastly/compute.wit index 3a47e8f5..07675e5b 100644 --- a/lib/wit/deps/fastly/compute.wit +++ b/lib/wit/deps/fastly/compute.wit @@ -1089,6 +1089,7 @@ world compute { import wasi:io/streams@0.2.0; import wasi:random/random@0.2.0; import wasi:cli/environment@0.2.0; + import wasi:cli/exit@0.2.0; import wasi:cli/stdout@0.2.0; import wasi:cli/stderr@0.2.0; import wasi:cli/stdin@0.2.0; diff --git a/test-fixtures/Cargo.lock b/test-fixtures/Cargo.lock index b9b2d197..4848ed8c 100644 --- a/test-fixtures/Cargo.lock +++ b/test-fixtures/Cargo.lock @@ -250,9 +250,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.117" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", "ryu", @@ -368,9 +368,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.6.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +checksum = "c55115c6fbe2d2bef26eb09ad74bde02d8255476fc0c7b515ef09fbb35742d82" dependencies = [ "tinyvec_macros", ]