From f7f3c00e8712036e322fb40558e4f04ed8a39ae3 Mon Sep 17 00:00:00 2001 From: fffonion Date: Wed, 5 Aug 2026 21:32:11 +0800 Subject: [PATCH 1/4] feat(host): parse signature-inferred host results --- pd-host-function/src/lib.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/pd-host-function/src/lib.rs b/pd-host-function/src/lib.rs index 42dde1aa..29b03c8d 100644 --- a/pd-host-function/src/lib.rs +++ b/pd-host-function/src/lib.rs @@ -352,7 +352,7 @@ fn type_label(ty: &Type) -> Result { let inner_label = type_label(inner)?; Ok(format!("{inner_label} | null")) } - "VmResult" | "BuiltinResult" | "HostResult" => { + "VmResult" | "BuiltinResult" | "HostResult" | "HostCallResult" => { let syn::PathArguments::AngleBracketed(args) = &segment.arguments else { return Err(Error::new_spanned( &segment.arguments, @@ -472,3 +472,25 @@ fn uses_taken_extractor(ty: &Type) -> bool { _ => false, } } + +#[cfg(test)] +mod tests { + use super::expand_pd_host_function; + use syn::{ItemFn, Meta, Token, parse_quote, punctuated::Punctuated}; + + #[test] + fn accepts_host_call_result_from_the_function_signature() { + let attr: Punctuated = parse_quote!(name = "test::suspend"); + let item: ItemFn = parse_quote! { + /// Returns a value after a host operation completes. + #[pd_host_function(name = "test::suspend")] + fn suspend() -> VmResult> { + todo!() + } + }; + + let expanded = expand_pd_host_function(attr, item) + .expect("HostCallResult should be accepted from the return signature"); + assert!(expanded.to_string().contains("HostCallResult")); + } +} From 43d818a8e591d6b0375aecfaf7230c5eece24ca1 Mon Sep 17 00:00:00 2001 From: fffonion Date: Wed, 5 Aug 2026 21:43:20 +0800 Subject: [PATCH 2/4] feat(host): infer suspension from return signatures --- build.rs | 36 +++++++++++++++++++++++--- src/builtins/metadata.rs | 7 +++++ src/builtins/mod.rs | 4 ++- src/builtins/runtime/mod.rs | 1 + src/builtins/runtime/typed.rs | 33 ++++++++++++++++++++++- src/lib.rs | 9 ++++--- tests/host_binding_generation_tests.rs | 33 ++++++++++++++++++++++- 7 files changed, 114 insertions(+), 9 deletions(-) diff --git a/build.rs b/build.rs index 00b550e9..9af4097e 100644 --- a/build.rs +++ b/build.rs @@ -49,6 +49,12 @@ pub(crate) enum HostBindingKind { StaticNonYieldingArgs, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum HostExecutionKind { + Sync, + MaySuspend, +} + impl HostBindingKind { pub(crate) fn render_bind_static_call(&self, name: &str, function_name: &str) -> String { let method = match self { @@ -71,6 +77,7 @@ struct CallableDecl { static_return_type: String, wrapper: Option, host_binding_kind: HostBindingKind, + host_execution: HostExecutionKind, } #[derive(Clone, Debug)] @@ -230,6 +237,24 @@ pub(crate) fn classify_host_binding(function: &ItemFn) -> HostBindingKind { HostBindingKind::StaticArgs } +pub(crate) fn infer_host_execution(function: &ItemFn) -> HostExecutionKind { + let return_type = normalized_return_type(&function.sig.output); + if contains_host_call_result(&return_type) { + HostExecutionKind::MaySuspend + } else { + HostExecutionKind::Sync + } +} + +fn contains_host_call_result(ty: &Type) -> bool { + if sole_type_argument(ty, "HostCallResult").is_some() { + return true; + } + sole_type_argument(ty, "VmResult") + .or_else(|| sole_type_argument(ty, "HostResult")) + .is_some_and(|inner| contains_host_call_result(&inner)) +} + fn is_supported_ordinary_return_type(ty: &Type) -> bool { match ty { Type::Group(group) => is_supported_ordinary_return_type(&group.elem), @@ -362,6 +387,7 @@ fn parse_source_file(path: &Path, spec: &SourceSpec, _order_offset: usize) -> Ve static_return_type: static_return_type_label(&function.sig.output), wrapper, host_binding_kind: classify_host_binding(function), + host_execution: infer_host_execution(function), }); } out @@ -1086,9 +1112,13 @@ fn render_callable_consts(callables: &[&CallableDecl]) -> String { .unwrap(); writeln!( &mut out, - "#[allow(dead_code)]\nconst {base}_DEF: CallableDef = CallableDef {{ name: {:?}, docs: {:?}, signature: {base}_SIGNATURE }};", + "#[allow(dead_code)]\nconst {base}_DEF: CallableDef = CallableDef {{ name: {:?}, docs: {:?}, signature: {base}_SIGNATURE, host_execution: HostExecution::{} }};", callable.name, - callable.docs + callable.docs, + match callable.host_execution { + HostExecutionKind::Sync => "Sync", + HostExecutionKind::MaySuspend => "MaySuspend", + } ) .unwrap(); writeln!(&mut out).unwrap(); @@ -1965,7 +1995,7 @@ fn type_label(ty: &Type) -> String { }; format!("{} | null", type_label(inner)) } - "VmResult" | "BuiltinResult" | "HostResult" => { + "VmResult" | "BuiltinResult" | "HostResult" | "HostCallResult" => { let syn::PathArguments::AngleBracketed(args) = &segment.arguments else { panic!("{ident} requires one generic argument"); }; diff --git a/src/builtins/metadata.rs b/src/builtins/metadata.rs index c6f8e739..b7405f94 100644 --- a/src/builtins/metadata.rs +++ b/src/builtins/metadata.rs @@ -42,11 +42,18 @@ pub struct CallableSignature { pub return_type: &'static str, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HostExecution { + Sync, + MaySuspend, +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct CallableDef { pub name: &'static str, pub docs: &'static str, pub signature: CallableSignature, + pub host_execution: HostExecution, } #[allow(dead_code)] diff --git a/src/builtins/mod.rs b/src/builtins/mod.rs index e7397ddc..b47f7405 100644 --- a/src/builtins/mod.rs +++ b/src/builtins/mod.rs @@ -5,7 +5,9 @@ mod metadata; #[cfg(feature = "runtime")] pub(crate) mod runtime; -pub use self::metadata::{CallableDef, CallableParam, CallableParamType, CallableSignature}; +pub use self::metadata::{ + CallableDef, CallableParam, CallableParamType, CallableSignature, HostExecution, +}; use crate::ValueType; #[cfg(feature = "runtime")] pub(crate) use crate::vm::{HostFunctionRegistry, Value, Vm, VmResult}; diff --git a/src/builtins/runtime/mod.rs b/src/builtins/runtime/mod.rs index 0740220e..ac8b9dec 100644 --- a/src/builtins/runtime/mod.rs +++ b/src/builtins/runtime/mod.rs @@ -25,6 +25,7 @@ mod typed; use io_wasm as io; pub(crate) use io::IoState; +pub use typed::HostCallResult; use typed::{ AnyValue, BuiltinResult, IntoBuiltinCallOutcome, IntoHostCallOutcome, NumberValue, UnknownValue, VmArray, VmBytes, VmMap, arg, borrow_arg, return_none, return_one, take_arg, diff --git a/src/builtins/runtime/typed.rs b/src/builtins/runtime/typed.rs index 16146f84..432d350a 100644 --- a/src/builtins/runtime/typed.rs +++ b/src/builtins/runtime/typed.rs @@ -35,6 +35,12 @@ impl NumberValue { } } +#[derive(Debug, PartialEq)] +pub enum HostCallResult { + Return(T), + Pending(HostOpId), +} + pub(super) fn missing_arg(label: &str) -> VmError { VmError::HostError(format!("missing argument: {label}")) } @@ -475,6 +481,18 @@ impl IntoHostCallOutcome for CallOutcome { } } +impl IntoHostCallOutcome for HostCallResult +where + T: IntoVmValue, +{ + fn into_host_call_outcome(self) -> CallOutcome { + match self { + Self::Return(value) => value.into_host_call_outcome(), + Self::Pending(op_id) => CallOutcome::Pending(op_id), + } + } +} + impl IntoHostCallOutcome for T where T: IntoVmValue, @@ -486,7 +504,8 @@ where #[cfg(test)] mod tests { - use super::{Value, arg}; + use super::{HostCallResult, IntoHostCallOutcome, Value, arg}; + use crate::vm::{CallOutcome, CallReturn}; #[test] fn optional_arg_decodes_missing_as_none() { @@ -511,4 +530,16 @@ mod tests { arg::>(&args, 0, "label").expect("present optional arg should decode"); assert_eq!(value, Some("hello")); } + + #[test] + fn host_call_result_converts_return_and_pending_variants() { + assert_eq!( + HostCallResult::Return(true).into_host_call_outcome(), + CallOutcome::Return(CallReturn::one(Value::Bool(true))) + ); + assert_eq!( + HostCallResult::::Pending(17).into_host_call_outcome(), + CallOutcome::Pending(17) + ); + } } diff --git a/src/lib.rs b/src/lib.rs index fbdfcff6..06994d77 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,12 +22,15 @@ pub mod vmbc; pub use assembler::{AsmParseError, Assembler, AssemblerError, BytecodeBuilder, assemble}; #[cfg(feature = "runtime")] +pub use builtins::runtime::HostCallResult; +#[cfg(feature = "runtime")] pub use builtins::runtime::print::{PrintHostFunction, PrintlnHostFunction, format_value}; pub use builtins::{ BuiltinFunction, BuiltinNamespaceMemberSpec, BuiltinNamespaceSpec, CallableDef, CallableParam, - CallableParamType, CallableSignature, LanguageBuiltinSpec, builtin_namespace_specs, - callable_signatures_for_builtin_namespace_member, default_host_callables, is_builtin_namespace, - language_builtin_specs, resolve_builtin_namespace_call, + CallableParamType, CallableSignature, HostExecution, LanguageBuiltinSpec, + builtin_namespace_specs, callable_signatures_for_builtin_namespace_member, + default_host_callables, is_builtin_namespace, language_builtin_specs, + resolve_builtin_namespace_call, }; pub use bytecode::{ CallableEnvironment, CallableKind, CallablePrototype, CallableTarget, CallableValue, diff --git a/tests/host_binding_generation_tests.rs b/tests/host_binding_generation_tests.rs index eee76148..82d5dc97 100644 --- a/tests/host_binding_generation_tests.rs +++ b/tests/host_binding_generation_tests.rs @@ -2,7 +2,9 @@ #[path = "../build.rs"] mod build_script; -use build_script::{HostBindingKind, classify_host_binding}; +use build_script::{ + HostBindingKind, HostExecutionKind, classify_host_binding, infer_host_execution, +}; use syn::parse_quote; use vm::{HostFunctionRegistry, JitConfig, JitTraceTerminal, Value, Vm, VmStatus, compile_source}; @@ -120,6 +122,35 @@ fn classifies_best_effort_host_bindings_from_signatures() { } } +#[test] +fn infers_host_suspension_from_the_return_signature() { + for function in [ + parse_quote!( + fn host() -> HostCallResult {} + ), + parse_quote!( + fn host() -> VmResult> {} + ), + parse_quote!( + fn host() -> HostResult> {} + ), + ] { + assert_eq!( + infer_host_execution(&function), + HostExecutionKind::MaySuspend + ); + assert_eq!( + classify_host_binding(&function), + HostBindingKind::StaticArgs + ); + } + + let synchronous = parse_quote!( + fn host() -> VmResult {} + ); + assert_eq!(infer_host_execution(&synchronous), HostExecutionKind::Sync); +} + fn assert_runtime_sleep_loop_uses_native_host_call(bind_cached_registry: bool) { let compiled = compile_source( r#" From f577a42f7e3b12fdcb531b22027039256942d088 Mon Sep 17 00:00:00 2001 From: fffonion Date: Wed, 5 Aug 2026 21:45:45 +0800 Subject: [PATCH 3/4] fix(host): reject unsupported async macro arguments --- pd-host-function/src/lib.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pd-host-function/src/lib.rs b/pd-host-function/src/lib.rs index 29b03c8d..50ece7c1 100644 --- a/pd-host-function/src/lib.rs +++ b/pd-host-function/src/lib.rs @@ -53,6 +53,16 @@ fn parse_name_arg(args: &Punctuated) -> Result { "expected #[pd_host_function(name = \"...\")]", )); }; + if args.len() != 1 { + let extra = args + .iter() + .nth(1) + .expect("a non-empty attribute with more than one argument has an extra argument"); + return Err(Error::new_spanned( + extra, + "#[pd_host_function] only supports name = \"...\"", + )); + } if !name_value.path.is_ident("name") { return Err(Error::new_spanned( &name_value.path, @@ -493,4 +503,21 @@ mod tests { .expect("HostCallResult should be accepted from the return signature"); assert!(expanded.to_string().contains("HostCallResult")); } + + #[test] + fn rejects_async_attribute_instead_of_treating_it_as_a_host_contract() { + let attr: Punctuated = + parse_quote!(name = "test::suspend", r#async = true); + let item: ItemFn = parse_quote! { + /// Returns a value after a host operation completes. + #[pd_host_function(name = "test::suspend")] + fn suspend() -> VmResult> { + todo!() + } + }; + + let error = expand_pd_host_function(attr, item) + .expect_err("the pd-host-function macro must not accept an async attribute"); + assert!(error.to_string().contains("only supports name")); + } } From 0f28ee9930890daffae5aa3c0bb70daf9c123997 Mon Sep 17 00:00:00 2001 From: fffonion Date: Wed, 5 Aug 2026 22:33:11 +0800 Subject: [PATCH 4/4] feat(host): add policy-bound HTTP client --- Cargo.lock | 1031 ++++++++++++++++++++++++++++++++- Cargo.toml | 8 + build.rs | 17 +- src/builtins/runtime/http.rs | 553 ++++++++++++++++++ src/builtins/runtime/io.rs | 4 + src/builtins/runtime/mod.rs | 19 +- src/builtins/runtime/typed.rs | 14 +- src/compiler/pipeline.rs | 4 +- src/lib.rs | 4 +- src/vm/host.rs | 26 +- src/vm/mod.rs | 2 + tests/vm/http_host_tests.rs | 117 ++++ 12 files changed, 1772 insertions(+), 27 deletions(-) create mode 100644 src/builtins/runtime/http.rs create mode 100644 tests/vm/http_host_tests.rs diff --git a/Cargo.lock b/Cargo.lock index b99df352..29603691 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,12 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1" +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + [[package]] name = "base64" version = "0.22.1" @@ -56,6 +62,22 @@ dependencies = [ "allocator-api2", ] +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "cc" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5add81bb678e6cb321aff7fa0dc7689ad82b112dbc032cea19f91d6b8e3582b9" +dependencies = [ + "find-msvc-tools", + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.4" @@ -68,6 +90,23 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" +[[package]] +name = "cfg_aliases" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" + +[[package]] +name = "chacha20" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d524456ba66e72eb8b115ff89e01e497f8e6d11d78b70b1aa13c0fbd97540a81" +dependencies = [ + "cfg-if", + "cpufeatures", + "rand_core", +] + [[package]] name = "clipboard-win" version = "5.4.1" @@ -77,6 +116,15 @@ dependencies = [ "error-code", ] +[[package]] +name = "cpufeatures" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201" +dependencies = [ + "libc", +] + [[package]] name = "cranelift-assembler-x64" version = "0.129.1" @@ -243,6 +291,17 @@ version = "0.129.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d953932541249c91e3fa70a75ff1e52adc62979a2a8132145d4b9b3e6d1a9b6a" +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.3", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -282,6 +341,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + [[package]] name = "fnv" version = "1.0.7" @@ -294,6 +359,15 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + [[package]] name = "futures-channel" version = "0.3.32" @@ -301,13 +375,74 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" dependencies = [ "futures-core", + "futures-sink", ] [[package]] name = "futures-core" -version = "0.3.32" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cd50c473c80f6d7c3670a752354b8e569b1a7cbfdc0419ec88e5edad85e0dc7" + +[[package]] +name = "futures-io" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4577ecaa3c4f96589d473f679a71b596316f6641bc350038b962a5daf0085d7a" + +[[package]] +name = "futures-sink" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e34418ac499d6305c2fb5ad0ed2f6ac998c5f8ca209b4510f7f94242c647e307" + +[[package]] +name = "futures-task" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b231ed28831efb4a61a08580c4bc233ec56bc009f4cd8f52da2c3cb97df0c109" + +[[package]] +name = "futures-util" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a77a90a256fce34da66415271e30f94ee91c57b04b8a2c042d9cf3220179deaa" +dependencies = [ + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi", + "rand_core", + "wasm-bindgen", +] [[package]] name = "gimli" @@ -351,6 +486,207 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "http" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9f41fd6a08e4d4ec69df65976da761afd5ad5e58a9d4acb46bd1c953a9e3ff2" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "hyper" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d22053281f852e11534f5198498373cbb59295120a20771d90f7ed1897490a72" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "http", + "http-body", + "httparse", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ca68d021ef39cf6463ab54c1d0f5daf03377b70561305bb89a8f83aab66e0f" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "tokio", + "tokio-rustls", + "tower-service", + "webpki-roots", +] + +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "base64", + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "ipnet", + "libc", + "percent-encoding", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "icu_collections" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2984d1cd16c883d7935b9e07e44071dca8d917fd52ecc02c04d5fa0b5a3f191c" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92219b62b3e2b4d88ac5119f8904c10f8f61bf7e95b640d25ba3075e6cac2c29" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c56e5ee99d6e3d33bd91c5d85458b6005a22140021cc324cea84dd0e72cff3b4" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da3be0ae77ea334f4da67c12f149704f19f81d1adf7c51cf482943e84a2bad38" + +[[package]] +name = "icu_properties" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bee3b67d0ea5c2cca5003417989af8996f8604e34fb9ddf96208a033901e70de" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2bbb201e0c04f7b4b3e14382af113e17ba4f63e2c9d2ee626b720cbce54a14" + +[[package]] +name = "icu_provider" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "139c4cf31c8b5f33d7e199446eff9c1e02decfc2f0eec2c8d71f65befa45b421" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + [[package]] name = "indexmap" version = "2.13.0" @@ -361,12 +697,29 @@ dependencies = [ "hashbrown 0.16.1", ] +[[package]] +name = "ipnet" +version = "2.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a756c3fac73139e83f14c2d742155dd2b78d3ee56597b419a0579b7bdd6dd78" + [[package]] name = "itoa" version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" +[[package]] +name = "js-sys" +version = "0.3.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -391,12 +744,24 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" +[[package]] +name = "litemap" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0" + [[package]] name = "log" version = "0.4.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" +[[package]] +name = "lru-slab" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" + [[package]] name = "mach2" version = "0.4.3" @@ -412,6 +777,17 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" +[[package]] +name = "mio" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + [[package]] name = "nibble_vec" version = "0.1.0" @@ -429,10 +805,16 @@ checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" dependencies = [ "bitflags 2.11.0", "cfg-if", - "cfg_aliases", + "cfg_aliases 0.1.1", "libc", ] +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + [[package]] name = "paste" version = "1.0.15" @@ -446,7 +828,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9676d58588b220f7af69d7aa86108042d2acaf21dd24c641a6d9ef3c4e193ba" dependencies = [ "pd-host-function 0.22.2", - "syn", + "syn 2.0.117", ] [[package]] @@ -455,7 +837,7 @@ version = "0.1.0" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.117", ] [[package]] @@ -466,7 +848,7 @@ checksum = "d9c941589fbbb839a40f7b80595d7b8f3742a8811268d787218f0c45c274d1f9" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.117", ] [[package]] @@ -485,13 +867,15 @@ dependencies = [ "pd-edge-abi", "pd-host-function 0.1.0", "regex", + "reqwest", "rt-format", "rustyline", "self_cell", "serde", "serde_json", - "syn", + "syn 2.0.117", "tokio", + "url", "windows-sys 0.59.0", ] @@ -512,12 +896,27 @@ dependencies = [ "serde_json", ] +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + [[package]] name = "pin-project-lite" version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" +[[package]] +name = "potential_utf" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0103b1cef7ec0cf76490e969665504990193874ea05c85ff9bab8b911d0a0564" +dependencies = [ + "zerovec", +] + [[package]] name = "proc-macro2" version = "1.0.106" @@ -527,6 +926,62 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "quinn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c1a41e437b6bbd489372cd4971de128e85c855f56c57f283d20ff016cf7c0a8" +dependencies = [ + "bytes", + "cfg_aliases 0.2.2", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash", + "rustls", + "socket2", + "thiserror", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4bfc015262b9df63c8845072ce59068853ff5872180c2ce2f13038b970e560" +dependencies = [ + "bytes", + "getrandom 0.4.3", + "lru-slab", + "rand", + "rand_pcg", + "ring", + "rustc-hash", + "rustls", + "rustls-pki-types", + "slab", + "thiserror", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35a133f956daabe89a61a685c2649f13d82d5aa4bd5d12d1277e1072a21c0694" +dependencies = [ + "cfg_aliases 0.2.2", + "libc", + "once_cell", + "socket2", + "tracing", + "windows-sys 0.61.2", +] + [[package]] name = "quote" version = "1.0.44" @@ -536,6 +991,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + [[package]] name = "radix_trie" version = "0.2.1" @@ -546,6 +1007,32 @@ dependencies = [ "nibble_vec", ] +[[package]] +name = "rand" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80" +dependencies = [ + "chacha20", + "getrandom 0.4.3", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rand_pcg" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caa0f4137e1c0a72f4c651489402276c8e8e1cf081f3b0ba156d2cbeef09e86a" +dependencies = [ + "rand_core", +] + [[package]] name = "regalloc2" version = "0.13.5" @@ -602,17 +1089,71 @@ dependencies = [ ] [[package]] -name = "rt-format" -version = "0.3.1" +name = "reqwest" +version = "0.12.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45087cee619d316fa4bd1675494acff4a5eaa0892fa53bc364bd246f13e452e2" +checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147" dependencies = [ - "lazy_static", - "regex", + "base64", + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "js-sys", + "log", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-rustls", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "webpki-roots", ] [[package]] -name = "rustc-hash" +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rt-format" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45087cee619d316fa4bd1675494acff4a5eaa0892fa53bc364bd246f13e452e2" +dependencies = [ + "lazy_static", + "regex", +] + +[[package]] +name = "rustc-hash" version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" @@ -630,6 +1171,41 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "rustls" +version = "0.23.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0283386ce02abc0151e1761d08802dfe86c173b0b494af5cbc086574e453da06" +dependencies = [ + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + [[package]] name = "rustscript" version = "0.1.0" @@ -637,6 +1213,12 @@ dependencies = [ "pd-vm", ] +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + [[package]] name = "rustyline" version = "14.0.0" @@ -659,6 +1241,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + [[package]] name = "self_cell" version = "1.2.2" @@ -692,7 +1280,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.117", ] [[package]] @@ -708,18 +1296,58 @@ dependencies = [ "zmij", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + [[package]] name = "smallvec" version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "stable_deref_trait" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + [[package]] name = "syn" version = "2.0.117" @@ -731,20 +1359,101 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53e9bae58849f64dfa4f5d5ae372c8341f7305f82a3868709269343628b659a3" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "target-lexicon" version = "0.13.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca" +[[package]] +name = "thiserror" +version = "2.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09a43598840e33d5b0331f38c5e30d13bb11c11210a4b58f0d9b18a5a5eefcd9" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43cbfe0cf76104d42a574802844187e84a305e531ed54455f11fbde0f10541cd" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.3", +] + +[[package]] +name = "tinystr" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8323304221c2a851516f22236c5722a72eaa19749016521d6dff0824447d96d" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tinyvec" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb4ebadaa0af04fab11ae01eb5f9fdb5f9c5b875506e210e71c07873528baa7f" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.49.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86" dependencies = [ + "bytes", + "libc", + "mio", "pin-project-lite", + "socket2", "tokio-macros", + "windows-sys 0.61.2", ] [[package]] @@ -755,9 +1464,89 @@ checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.117", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-http" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" +dependencies = [ + "bitflags 2.11.0", + "bytes", + "futures-util", + "http", + "http-body", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", + "url", ] +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + [[package]] name = "unicode-ident" version = "1.0.24" @@ -776,12 +1565,106 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + [[package]] name = "utf8parse" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasm-bindgen" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62df1340f32221cb9c54d6a27b030e3dba64361d4a95bed55f9aacb44da291d" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 2.0.117", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24" +dependencies = [ + "unicode-ident", +] + [[package]] name = "wasmtime-internal-core" version = "42.0.1" @@ -803,6 +1686,35 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "web-sys" +version = "0.3.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8622dcb61c0bcc9fffa6938bed81210af2da9a7e4a1a834b2e37a59b6dfb6141" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki-roots" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "windows-link" version = "0.2.1" @@ -900,6 +1812,95 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "writeable" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ffae5123b2d3fc086436f8834ae3ab053a283cfac8fe0a0b8eaae044768a4c4" + +[[package]] +name = "yoke" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11532158c46691caf0f2593ea8358fed6bbf68a0315e80aae9bd41fbade684a1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e" + +[[package]] +name = "zerotrie" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f9152d31db0792fa83f70fb2f83148effb5c1f5b8c7686c3459e361d9bc20bf" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90f911cbc359ab6af17377d242225f4d75119aec87ea711a880987b18cd7b239" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "625dc425cab0dca6dc3c3319506e6593dcb08a9f387ea3b284dbd52a92c40555" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "zmij" version = "1.0.21" diff --git a/Cargo.toml b/Cargo.toml index 5cfc571d..0435e0a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ name = "vm" [features] default = ["runtime", "cli", "cranelift-jit"] runtime = [] +http-client = ["dep:reqwest", "dep:url"] edge-abi = [ "dep:edge_abi", "edge_abi/console", @@ -60,6 +61,8 @@ cranelift-jit = { version = "0.129.1", optional = true } cranelift-module = { version = "0.129.1", optional = true } cranelift-native = { version = "0.129.1", optional = true } pd-host-function = { path = "./pd-host-function", version = "0.1.0" } +reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls"], optional = true } +url = { version = "2", optional = true } edge_abi = { package = "pd-edge-abi", version = "0.1.1", default-features = false, optional = true } futures-channel = "0.3" paste = "1" @@ -85,5 +88,10 @@ name = "host_binding_generation_tests" path = "tests/host_binding_generation_tests.rs" required-features = ["cranelift-jit"] +[[test]] +name = "http_host_tests" +path = "tests/vm/http_host_tests.rs" +required-features = ["http-client"] + [build-dependencies] syn = { version = "2", features = ["full"] } diff --git a/build.rs b/build.rs index 9af4097e..3331a045 100644 --- a/build.rs +++ b/build.rs @@ -108,11 +108,18 @@ fn main() { println!("cargo:rerun-if-changed={}", namespace_manifest.display()); let namespaces = parse_namespace_manifest(&namespace_manifest); - let host_sources = [SourceSpec { - path: "src/builtins/runtime/host.rs".to_string(), - module: "host".to_string(), - category: SourceCategory::DefaultHost, - }]; + let host_sources = [ + SourceSpec { + path: "src/builtins/runtime/host.rs".to_string(), + module: "host".to_string(), + category: SourceCategory::DefaultHost, + }, + SourceSpec { + path: "src/builtins/runtime/http.rs".to_string(), + module: "http".to_string(), + category: SourceCategory::DefaultHost, + }, + ]; let builtin_sources = builtin_source_specs(&namespaces); let core_sources = [SourceSpec { path: "src/builtins/runtime/core.rs".to_string(), diff --git a/src/builtins/runtime/http.rs b/src/builtins/runtime/http.rs new file mode 100644 index 00000000..09f9f6fe --- /dev/null +++ b/src/builtins/runtime/http.rs @@ -0,0 +1,553 @@ +use std::task::{Context, Poll}; + +#[cfg(feature = "http-client")] +use std::io::Read; + +use pd_host_function::pd_host_function; + +use super::{HostCallResult, Vm, VmMap, VmResult}; +#[cfg(feature = "http-client")] +use crate::vm::Value; +use crate::vm::{CallReturn, HostOpId, VmError}; + +#[derive(Clone, Debug)] +pub struct HttpConfig { + pub allowed_schemes: Vec, + pub allowed_hosts: Vec, + pub allowed_ports: Vec, + pub max_redirects: usize, + pub max_request_body_bytes: usize, + pub max_response_body_bytes: usize, + pub connect_timeout: std::time::Duration, + pub request_timeout: std::time::Duration, + pub allow_private_ips: bool, +} + +impl Default for HttpConfig { + fn default() -> Self { + Self { + allowed_schemes: vec!["https".to_string()], + allowed_hosts: Vec::new(), + allowed_ports: Vec::new(), + max_redirects: 5, + max_request_body_bytes: 1024 * 1024, + max_response_body_bytes: 8 * 1024 * 1024, + connect_timeout: std::time::Duration::from_secs(10), + request_timeout: std::time::Duration::from_secs(30), + allow_private_ips: false, + } + } +} + +#[cfg(feature = "http-client")] +struct HttpCompletion { + result: VmResult, +} + +#[cfg(feature = "http-client")] +pub(crate) struct HttpState { + config: Option, + pending_ops: + std::collections::HashMap>, + cancel_flags: + std::collections::HashMap>, +} + +#[cfg(not(feature = "http-client"))] +pub(crate) struct HttpState; + +impl Default for HttpState { + fn default() -> Self { + #[cfg(feature = "http-client")] + { + return Self { + config: None, + pending_ops: std::collections::HashMap::new(), + cancel_flags: std::collections::HashMap::new(), + }; + } + + #[cfg(not(feature = "http-client"))] + Self + } +} + +impl HttpState { + pub(crate) fn configure(&mut self, config: HttpConfig) { + #[cfg(feature = "http-client")] + { + self.config = Some(config); + } + #[cfg(not(feature = "http-client"))] + let _ = config; + } + + pub(crate) fn clear_configuration(&mut self) { + #[cfg(feature = "http-client")] + { + self.cancel_all(); + self.config = None; + } + } + + pub(crate) fn is_configured(&self) -> bool { + #[cfg(feature = "http-client")] + { + return self.config.is_some(); + } + #[cfg(not(feature = "http-client"))] + false + } + + #[cfg(feature = "http-client")] + fn schedule( + &mut self, + op_id: HostOpId, + config: HttpConfig, + request: HttpRequest, + ) -> VmResult { + use std::sync::Arc; + use std::sync::atomic::AtomicBool; + + let (sender, receiver) = futures_channel::oneshot::channel(); + let cancelled = Arc::new(AtomicBool::new(false)); + let worker_cancelled = Arc::clone(&cancelled); + let thread_name = format!("rustscript-http-{op_id}"); + std::thread::Builder::new() + .name(thread_name) + .spawn(move || { + let result = if worker_cancelled.load(std::sync::atomic::Ordering::Acquire) { + Err(VmError::HostError("HTTP request was cancelled".to_string())) + } else { + execute_request(&config, &request, &worker_cancelled) + }; + let _ = sender.send(HttpCompletion { result }); + }) + .map_err(|error| { + VmError::HostError(format!("failed to start HTTP request: {error}")) + })?; + self.pending_ops.insert(op_id, receiver); + self.cancel_flags.insert(op_id, cancelled); + Ok(op_id) + } + + #[cfg(feature = "http-client")] + fn has_pending_op(&self, op_id: HostOpId) -> bool { + self.pending_ops.contains_key(&op_id) + } + + #[cfg(feature = "http-client")] + fn cancel_pending_op(&mut self, op_id: HostOpId) { + if let Some(flag) = self.cancel_flags.remove(&op_id) { + flag.store(true, std::sync::atomic::Ordering::Release); + } + self.pending_ops.remove(&op_id); + } + + #[cfg(feature = "http-client")] + fn cancel_all(&mut self) { + for flag in self.cancel_flags.values() { + flag.store(true, std::sync::atomic::Ordering::Release); + } + self.cancel_flags.clear(); + self.pending_ops.clear(); + } + + #[cfg(feature = "http-client")] + fn poll_pending_op( + &mut self, + op_id: HostOpId, + cx: &mut Context<'_>, + ) -> Poll> { + use std::pin::Pin; + + let poll_result = match self.pending_ops.get_mut(&op_id) { + Some(receiver) => Pin::new(receiver).poll(cx), + None => { + return Poll::Ready(Err(VmError::HostError(format!("unknown HTTP op {op_id}",)))); + } + }; + match poll_result { + Poll::Pending => Poll::Pending, + Poll::Ready(Ok(completion)) => { + self.pending_ops.remove(&op_id); + self.cancel_flags.remove(&op_id); + Poll::Ready(completion.result) + } + Poll::Ready(Err(_)) => { + self.pending_ops.remove(&op_id); + self.cancel_flags.remove(&op_id); + Poll::Ready(Err(VmError::HostError(format!( + "HTTP op {op_id} was cancelled", + )))) + } + } + } +} + +/// Starts an HTTP request under the VM's configured network policy. +/// +/// The request map accepts `method`, `url`, optional `headers`, and optional `body`. +/// The response map contains `status`, `headers`, `body`, and the final `url`. +#[pd_host_function(name = "http::client::request")] +pub(super) fn builtin_http_client_request( + vm: &mut Vm, + request: &VmMap, +) -> VmResult> { + #[cfg(not(feature = "http-client"))] + { + let _ = (vm, request); + return Err(VmError::HostError( + "HTTP client support is disabled; enable the http-client feature".to_string(), + )); + } + + #[cfg(feature = "http-client")] + { + let config = vm + .http_state + .config + .clone() + .ok_or_else(|| VmError::HostError("HTTP host is not configured".to_string()))?; + let request = parse_request(request, &config)?; + validate_url(&config, &request.url)?; + let op_id = vm.allocate_host_op_id(); + let op_id = vm.http_state.schedule(op_id, config, request)?; + Ok(HostCallResult::Pending(op_id)) + } +} + +#[cfg(feature = "http-client")] +pub(super) fn has_pending_op(vm: &Vm, op_id: HostOpId) -> bool { + vm.http_state.has_pending_op(op_id) +} + +#[cfg(not(feature = "http-client"))] +pub(super) fn has_pending_op(_vm: &Vm, _op_id: HostOpId) -> bool { + false +} + +pub(super) fn cancel_pending_op(vm: &mut Vm, op_id: HostOpId) { + #[cfg(feature = "http-client")] + vm.http_state.cancel_pending_op(op_id); + #[cfg(not(feature = "http-client"))] + let _ = (vm, op_id); +} + +pub(super) fn cancel_all_pending_ops(vm: &mut Vm) { + #[cfg(feature = "http-client")] + vm.http_state.cancel_all(); + #[cfg(not(feature = "http-client"))] + let _ = vm; +} + +pub(super) fn poll_pending_op( + vm: &mut Vm, + op_id: HostOpId, + cx: &mut Context<'_>, +) -> Poll> { + #[cfg(feature = "http-client")] + return vm.http_state.poll_pending_op(op_id, cx); + + #[cfg(not(feature = "http-client"))] + { + let _ = (vm, cx); + Poll::Ready(Err(VmError::HostError(format!( + "HTTP support is disabled for op {op_id}", + )))) + } +} + +#[cfg(feature = "http-client")] +struct HttpRequest { + method: reqwest::Method, + url: url::Url, + headers: Vec<(reqwest::header::HeaderName, reqwest::header::HeaderValue)>, + body: Option>, +} + +#[cfg(feature = "http-client")] +fn parse_request(map: &VmMap, config: &HttpConfig) -> VmResult { + let method = map_string(map, "method")?.to_ascii_uppercase(); + if !matches!( + method.as_str(), + "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS" + ) { + return Err(VmError::HostError(format!( + "HTTP method '{method}' is not allowed" + ))); + } + let method = reqwest::Method::from_bytes(method.as_bytes()) + .map_err(|_| VmError::HostError("invalid HTTP method".to_string()))?; + let url = map_string(map, "url")? + .parse::() + .map_err(|error| VmError::HostError(format!("invalid HTTP URL: {error}")))?; + if !url.username().is_empty() || url.password().is_some() { + return Err(VmError::HostError( + "HTTP URL userinfo is not allowed".to_string(), + )); + } + let body = match map.get(&Value::string("body")) { + None | Some(Value::Null) => None, + Some(Value::Bytes(bytes)) => { + if bytes.len() > config.max_request_body_bytes { + return Err(VmError::HostError( + "HTTP request body exceeds limit".to_string(), + )); + } + Some(bytes.as_ref().clone()) + } + Some(Value::String(text)) => { + if text.len() > config.max_request_body_bytes { + return Err(VmError::HostError( + "HTTP request body exceeds limit".to_string(), + )); + } + Some(text.as_bytes().to_vec()) + } + Some(_) => return Err(VmError::TypeMismatch("HTTP request body")), + }; + + let mut headers = Vec::new(); + if let Some(Value::Map(header_map)) = map.get(&Value::string("headers")) { + for (key, value) in header_map.iter() { + let Value::String(key) = key else { + return Err(VmError::TypeMismatch("HTTP header name")); + }; + let Value::String(value) = value else { + return Err(VmError::TypeMismatch("HTTP header value")); + }; + if matches!( + key.to_ascii_lowercase().as_str(), + "host" | "content-length" | "transfer-encoding" | "connection" + ) { + return Err(VmError::HostError(format!( + "HTTP header '{key}' is managed by the client", + ))); + } + let name = reqwest::header::HeaderName::from_bytes(key.as_bytes()) + .map_err(|_| VmError::HostError(format!("invalid HTTP header name '{key}'")))?; + let value = reqwest::header::HeaderValue::from_str(value).map_err(|_| { + VmError::HostError(format!("invalid HTTP header value for '{key}'")) + })?; + headers.push((name, value)); + } + } else if map.get(&Value::string("headers")).is_some() { + return Err(VmError::TypeMismatch("HTTP headers")); + } + + Ok(HttpRequest { + method, + url, + headers, + body, + }) +} + +#[cfg(feature = "http-client")] +fn map_string(map: &VmMap, key: &str) -> VmResult { + match map.get(&Value::string(key)) { + Some(Value::String(value)) => Ok(value.as_ref().clone()), + Some(_) => Err(VmError::TypeMismatch("HTTP request string field")), + None => Err(VmError::HostError(format!( + "missing HTTP request field '{key}'" + ))), + } +} + +#[cfg(feature = "http-client")] +fn validate_url(config: &HttpConfig, url: &url::Url) -> VmResult<()> { + let scheme = url.scheme().to_ascii_lowercase(); + if !config + .allowed_schemes + .iter() + .any(|allowed| allowed.eq_ignore_ascii_case(&scheme)) + { + return Err(VmError::HostError(format!( + "HTTP URL scheme '{scheme}' is not allowed", + ))); + } + let host = url + .host_str() + .ok_or_else(|| VmError::HostError("HTTP URL has no host".to_string()))?; + if !config + .allowed_hosts + .iter() + .any(|allowed| allowed.eq_ignore_ascii_case(host)) + { + return Err(VmError::HostError( + "HTTP target host is not allowed".to_string(), + )); + } + let port = url + .port_or_known_default() + .ok_or_else(|| VmError::HostError("HTTP URL has no known port".to_string()))?; + if !config.allowed_ports.is_empty() && !config.allowed_ports.contains(&port) { + return Err(VmError::HostError(format!( + "HTTP target port {port} is not allowed", + ))); + } + if !config.allow_private_ips { + let host_ip = host.parse::().ok(); + if host_ip.is_some_and(is_restricted_ip) { + return Err(VmError::HostError( + "HTTP target resolves to a restricted IP".to_string(), + )); + } + use std::net::ToSocketAddrs; + let addresses = (host, port) + .to_socket_addrs() + .map_err(|error| VmError::HostError(format!("HTTP host resolution failed: {error}")))?; + if addresses.map(|address| address.ip()).any(is_restricted_ip) { + return Err(VmError::HostError( + "HTTP target resolves to a restricted IP".to_string(), + )); + } + } + Ok(()) +} + +#[cfg(feature = "http-client")] +fn is_restricted_ip(ip: std::net::IpAddr) -> bool { + match ip { + std::net::IpAddr::V4(ip) => { + ip.is_loopback() + || ip.is_private() + || ip.is_link_local() + || ip.is_unspecified() + || ip.is_broadcast() + || ip.is_multicast() + } + std::net::IpAddr::V6(ip) => { + ip.is_loopback() + || ip.is_unique_local() + || ip.is_unicast_link_local() + || ip.is_unspecified() + || ip.is_multicast() + } + } +} + +#[cfg(feature = "http-client")] +fn execute_request( + config: &HttpConfig, + request: &HttpRequest, + cancelled: &std::sync::Arc, +) -> VmResult { + let client = reqwest::blocking::Client::builder() + .redirect(reqwest::redirect::Policy::none()) + .no_proxy() + .connect_timeout(config.connect_timeout) + .timeout(config.request_timeout) + .build() + .map_err(|error| VmError::HostError(format!("HTTP client setup failed: {error}")))?; + let mut method = request.method.clone(); + let mut url = request.url.clone(); + let mut body = request.body.clone(); + let mut headers = request.headers.clone(); + + for redirect_index in 0..=config.max_redirects { + if cancelled.load(std::sync::atomic::Ordering::Acquire) { + return Err(VmError::HostError("HTTP request was cancelled".to_string())); + } + validate_url(config, &url)?; + let origin = request.url.origin(); + let mut builder = client.request(method.clone(), url.clone()); + for (name, value) in &headers { + builder = builder.header(name, value); + } + if let Some(body) = &body { + builder = builder.body(body.clone()); + } + let mut response = builder + .send() + .map_err(|error| VmError::HostError(format!("HTTP request failed: {error}")))?; + if response.status().is_redirection() { + if redirect_index == config.max_redirects { + return Err(VmError::HostError( + "HTTP redirect limit exceeded".to_string(), + )); + } + let location = response + .headers() + .get(reqwest::header::LOCATION) + .ok_or_else(|| VmError::HostError("HTTP redirect has no location".to_string()))? + .to_str() + .map_err(|_| VmError::HostError("HTTP redirect location is invalid".to_string()))?; + let next_url = url + .join(location) + .map_err(|error| VmError::HostError(format!("invalid HTTP redirect: {error}")))?; + validate_url(config, &next_url)?; + if next_url.origin() != origin { + headers.retain(|(name, _)| { + name != reqwest::header::AUTHORIZATION && name != reqwest::header::COOKIE + }); + } + if response.status() == reqwest::StatusCode::SEE_OTHER + || ((response.status() == reqwest::StatusCode::MOVED_PERMANENTLY + || response.status() == reqwest::StatusCode::FOUND) + && method != reqwest::Method::GET + && method != reqwest::Method::HEAD) + { + method = reqwest::Method::GET; + body = None; + } + url = next_url; + continue; + } + + let mut bytes = Vec::new(); + let mut limited_response = + (&mut response).take(config.max_response_body_bytes.saturating_add(1) as u64); + limited_response + .read_to_end(&mut bytes) + .map_err(|error| VmError::HostError(format!("HTTP response read failed: {error}")))?; + if bytes.len() > config.max_response_body_bytes { + return Err(VmError::HostError( + "HTTP response body exceeds limit".to_string(), + )); + } + let response_headers = response + .headers() + .iter() + .filter_map(|(name, value)| { + value + .to_str() + .ok() + .map(|value| (Value::string(name.as_str()), Value::string(value))) + }) + .collect::>(); + let response_map = VmMap::from_entries(vec![ + ( + Value::string("status"), + Value::Int(i64::from(response.status().as_u16())), + ), + ( + Value::string("headers"), + Value::Map(std::sync::Arc::new(VmMap::from_entries(response_headers))), + ), + (Value::string("body"), Value::bytes(bytes)), + (Value::string("url"), Value::string(url.as_str())), + ]); + return Ok(CallReturn::one(Value::Map(std::sync::Arc::new( + response_map, + )))); + } + + Err(VmError::HostError( + "HTTP redirect processing failed".to_string(), + )) +} + +#[cfg(test)] +mod tests { + use super::HttpConfig; + + #[test] + fn default_http_policy_denies_all_hosts() { + let config = HttpConfig::default(); + assert_eq!(config.allowed_schemes, ["https"]); + assert!(config.allowed_hosts.is_empty()); + assert!(!config.allow_private_ips); + } +} diff --git a/src/builtins/runtime/io.rs b/src/builtins/runtime/io.rs index 1d9bfe47..e5b874ac 100644 --- a/src/builtins/runtime/io.rs +++ b/src/builtins/runtime/io.rs @@ -43,6 +43,10 @@ pub(super) fn cancel_pending_op(vm: &mut Vm, op_id: HostOpId) { vm.io_state.pending_ops.remove(&op_id); } +pub(super) fn has_pending_op(vm: &Vm, op_id: HostOpId) -> bool { + vm.io_state.pending_ops.contains_key(&op_id) +} + pub(super) fn poll_builtin_io_op( vm: &mut Vm, op_id: HostOpId, diff --git a/src/builtins/runtime/mod.rs b/src/builtins/runtime/mod.rs index ac8b9dec..4111ef38 100644 --- a/src/builtins/runtime/mod.rs +++ b/src/builtins/runtime/mod.rs @@ -9,6 +9,7 @@ mod aot; mod bytes; pub(crate) mod core; mod host; +mod http; #[cfg(not(target_arch = "wasm32"))] mod io; #[cfg(target_arch = "wasm32")] @@ -24,6 +25,8 @@ mod typed; #[cfg(target_arch = "wasm32")] use io_wasm as io; +pub use http::HttpConfig; +pub(crate) use http::HttpState; pub(crate) use io::IoState; pub use typed::HostCallResult; use typed::{ @@ -126,6 +129,7 @@ pub(crate) fn execute_builtin_call( pub(crate) fn cancel_builtin_io_op(vm: &mut Vm, op_id: HostOpId) { io::cancel_pending_op(vm, op_id); + http::cancel_pending_op(vm, op_id); } pub(crate) fn poll_builtin_io_op( @@ -133,11 +137,24 @@ pub(crate) fn poll_builtin_io_op( op_id: HostOpId, cx: &mut Context<'_>, ) -> Poll> { - io::poll_builtin_io_op(vm, op_id, cx) + if io::has_pending_op(vm, op_id) { + io::poll_builtin_io_op(vm, op_id, cx) + } else { + http::poll_pending_op(vm, op_id, cx) + } +} + +pub(crate) fn is_builtin_io_op(vm: &Vm, op_id: HostOpId) -> bool { + #[cfg(not(target_arch = "wasm32"))] + if io::has_pending_op(vm, op_id) { + return true; + } + http::has_pending_op(vm, op_id) } pub(crate) fn close_all_handles(vm: &mut Vm) { io::close_all_handles(vm); + http::cancel_all_pending_ops(vm); } #[cfg(test)] diff --git a/src/builtins/runtime/typed.rs b/src/builtins/runtime/typed.rs index 432d350a..d10ba7d6 100644 --- a/src/builtins/runtime/typed.rs +++ b/src/builtins/runtime/typed.rs @@ -465,7 +465,19 @@ where { fn into_builtin_call_outcome(self) -> BuiltinCallOutcome { match self { - Self::Return(value) => value.into_builtin_call_outcome(), + Self::Return(value) => BuiltinCallOutcome::Return(return_one(value)), + Self::Pending(op_id) => BuiltinCallOutcome::Pending(op_id), + } + } +} + +impl IntoBuiltinCallOutcome for HostCallResult +where + T: IntoVmValue, +{ + fn into_builtin_call_outcome(self) -> BuiltinCallOutcome { + match self { + Self::Return(value) => BuiltinCallOutcome::Return(return_one(value)), Self::Pending(op_id) => BuiltinCallOutcome::Pending(op_id), } } diff --git a/src/compiler/pipeline.rs b/src/compiler/pipeline.rs index 7f98363d..ff6e768a 100644 --- a/src/compiler/pipeline.rs +++ b/src/compiler/pipeline.rs @@ -579,7 +579,9 @@ fn schema_is_fully_known(schema: &TypeSchema) -> bool { | TypeSchema::GenericParam(_) => true, TypeSchema::Optional(inner) => schema_is_fully_known(inner), TypeSchema::Named(_, type_args) => type_args.iter().all(schema_is_fully_known), - TypeSchema::Array(item) | TypeSchema::Map(item) => schema_is_fully_known(item), + TypeSchema::Array(item) | TypeSchema::Map(item) => { + matches!(item.as_ref(), TypeSchema::Unknown) || schema_is_fully_known(item) + } TypeSchema::ArrayTuple(items) => items.iter().all(schema_is_fully_known), TypeSchema::ArrayTupleRest { prefix, rest } => { prefix.iter().all(schema_is_fully_known) && schema_is_fully_known(rest) diff --git a/src/lib.rs b/src/lib.rs index 06994d77..cf6758a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,8 @@ pub use assembler::{AsmParseError, Assembler, AssemblerError, BytecodeBuilder, a #[cfg(feature = "runtime")] pub use builtins::runtime::HostCallResult; #[cfg(feature = "runtime")] +pub use builtins::runtime::HttpConfig; +#[cfg(feature = "runtime")] pub use builtins::runtime::print::{PrintHostFunction, PrintlnHostFunction, format_value}; pub use builtins::{ BuiltinFunction, BuiltinNamespaceMemberSpec, BuiltinNamespaceSpec, CallableDef, CallableParam, @@ -35,7 +37,7 @@ pub use builtins::{ pub use bytecode::{ CallableEnvironment, CallableKind, CallablePrototype, CallableTarget, CallableValue, CaptureBindingMode, ExportedCallable, FunctionRegion, HostImport, OpCode, Program, - RootCallableBinding, ScriptFunction, TypeMap, Value, ValueType, + RootCallableBinding, ScriptFunction, TypeMap, Value, ValueType, VmMap, }; pub fn builtin_call_index(name: &str) -> Option { use builtins::BuiltinFunction; diff --git a/src/vm/host.rs b/src/vm/host.rs index ecca479a..3485f3f5 100644 --- a/src/vm/host.rs +++ b/src/vm/host.rs @@ -851,6 +851,18 @@ impl Vm { self.runtime_print_sink = None; } + pub fn configure_http(&mut self, config: crate::builtins::runtime::HttpConfig) { + self.http_state.configure(config); + } + + pub fn clear_http_configuration(&mut self) { + self.http_state.clear_configuration(); + } + + pub fn http_is_configured(&self) -> bool { + self.http_state.is_configured() + } + pub(crate) fn write_runtime_print(&mut self, rendered: String) -> VmResult<()> { let Some(sink) = self.runtime_print_sink.as_mut() else { return Err(VmError::HostError( @@ -1520,7 +1532,7 @@ impl Vm { saved_stack.append(&mut host_stack); self.stack = saved_stack; let resume_ip = self.call_resume_ip(call_ip)?; - self.set_waiting_host_op(op_id, WaitingHostOpSource::HostBridge)?; + self.set_waiting_host_op(op_id, self.pending_host_op_source(op_id))?; self.ip = resume_ip; Ok(HostCallExecOutcome::Pending(op_id)) } @@ -1630,7 +1642,7 @@ impl Vm { CallOutcome::Pending(op_id) => { self.stack.truncate(arg_start); let resume_ip = self.call_resume_ip(call_ip)?; - self.set_waiting_host_op(op_id, WaitingHostOpSource::HostBridge)?; + self.set_waiting_host_op(op_id, self.pending_host_op_source(op_id))?; self.ip = resume_ip; Ok(HostCallExecOutcome::Pending(op_id)) } @@ -1688,7 +1700,7 @@ impl Vm { CallOutcome::Pending(op_id) => { self.stack.truncate(arg_start); let resume_ip = self.call_resume_ip(call_ip)?; - self.set_waiting_host_op(op_id, WaitingHostOpSource::HostBridge)?; + self.set_waiting_host_op(op_id, self.pending_host_op_source(op_id))?; self.ip = resume_ip; Ok(HostCallExecOutcome::Pending(op_id)) } @@ -1715,6 +1727,14 @@ impl Vm { Ok(resume_ip) } + fn pending_host_op_source(&self, op_id: HostOpId) -> WaitingHostOpSource { + if crate::builtins::runtime::is_builtin_io_op(self, op_id) { + WaitingHostOpSource::BuiltinIo + } else { + WaitingHostOpSource::HostBridge + } + } + pub(super) fn set_waiting_host_op( &mut self, op_id: HostOpId, diff --git a/src/vm/mod.rs b/src/vm/mod.rs index ba148aec..0ddb396c 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -384,6 +384,7 @@ pub struct Vm { waiting_host_op: Option, next_host_op_id: HostOpId, pub(crate) io_state: crate::builtins::runtime::IoState, + pub(crate) http_state: crate::builtins::runtime::HttpState, regex_cache: crate::builtins::runtime::regex::RegexCache, map_iterators: Vec>>, epoch_handle: EpochHandle, @@ -727,6 +728,7 @@ impl Vm { waiting_host_op: None, next_host_op_id: 1, io_state: crate::builtins::runtime::IoState::default(), + http_state: crate::builtins::runtime::HttpState::default(), regex_cache: crate::builtins::runtime::regex::RegexCache::default(), map_iterators: Vec::new(), epoch_handle, diff --git a/tests/vm/http_host_tests.rs b/tests/vm/http_host_tests.rs new file mode 100644 index 00000000..43e070ac --- /dev/null +++ b/tests/vm/http_host_tests.rs @@ -0,0 +1,117 @@ +use std::io::{Read, Write}; +use std::net::TcpListener; +use std::thread; + +use vm::{HostFunctionRegistry, HttpConfig, Program, Value, Vm, VmStatus, compile_source}; + +fn build_request_program(url: String) -> Program { + compile_source(&format!( + r#" + use http; + http::client::request({{"method": "GET", "url": "{url}"}}); + "# + )) + .expect("HTTP request source should compile") + .program +} + +fn local_http_config(port: u16) -> HttpConfig { + HttpConfig { + allowed_schemes: vec!["http".to_string()], + allowed_hosts: vec!["127.0.0.1".to_string()], + allowed_ports: vec![port], + allow_private_ips: true, + ..HttpConfig::default() + } +} + +fn spawn_test_server() -> (u16, thread::JoinHandle<()>) { + let listener = TcpListener::bind(("127.0.0.1", 0)).expect("test listener should bind"); + let port = listener + .local_addr() + .expect("test listener should have an address") + .port(); + let handle = thread::spawn(move || { + let (mut stream, _) = listener.accept().expect("test request should arrive"); + let mut request = Vec::new(); + let mut buffer = [0_u8; 1024]; + loop { + let read = stream + .read(&mut buffer) + .expect("request should be readable"); + if read == 0 { + break; + } + request.extend_from_slice(&buffer[..read]); + if request.windows(4).any(|window| window == b"\r\n\r\n") { + break; + } + } + assert!(request.starts_with(b"GET / HTTP/1.1")); + stream + .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nX-Test: yes\r\n\r\nok") + .expect("response should be writable"); + }); + (port, handle) +} + +fn response_field<'a>(value: &'a Value, key: &str) -> &'a Value { + let Value::Map(map) = value else { + panic!("expected response map, got {value:?}"); + }; + map.get(&Value::string(key)) + .unwrap_or_else(|| panic!("response missing field {key}")) +} + +async fn drive_vm_to_halt(vm: &mut Vm) -> Result<(), vm::VmError> { + let mut status = vm.run()?; + loop { + match status { + VmStatus::Halted => return Ok(()), + VmStatus::Yielded => status = vm.resume()?, + VmStatus::Waiting(_) => { + vm.await_waiting_host_op().await?; + status = vm.resume()?; + } + } + } +} + +#[tokio::test(flavor = "current_thread")] +async fn http_host_executes_a_bounded_request_and_returns_a_response_map() { + let (port, server) = spawn_test_server(); + let mut vm = Vm::new(build_request_program(format!("http://127.0.0.1:{port}/"))); + vm.configure_http(local_http_config(port)); + HostFunctionRegistry::new() + .bind_vm_cached(&mut vm) + .expect("default host registry should bind HTTP"); + + drive_vm_to_halt(&mut vm) + .await + .expect("http request should complete"); + server.join().expect("test server should finish"); + + assert_eq!(response_field(&vm.stack()[0], "status"), &Value::Int(200)); + assert_eq!( + response_field(&vm.stack()[0], "body"), + &Value::bytes(b"ok".to_vec()) + ); +} + +#[test] +fn http_host_rejects_targets_until_an_explicit_policy_allows_them() { + let mut vm = Vm::new(build_request_program("http://127.0.0.1:1/".to_string())); + HostFunctionRegistry::new() + .bind_vm_cached(&mut vm) + .expect("default host registry should bind HTTP"); + let error = vm + .run() + .expect_err("unconfigured HTTP targets must be rejected"); + assert!( + error.to_string().contains("HTTP host is not configured") + || error + .to_string() + .contains("HTTP target host is not allowed"), + "unexpected error: {error}" + ); +}