From c4caab7c7cba7e5349a49fd91fec221a227db4b3 Mon Sep 17 00:00:00 2001 From: fffonion Date: Thu, 17 Sep 2026 11:13:10 +0800 Subject: [PATCH 1/6] chore(deps): pin rustscript core to b1d6cffede77f49410bf63525f30b9a46b02dc01 Lock pd-host-function and pd-vm to the frozen full SHA and reject stale or abbreviated pins. --- Cargo.lock | 6 +-- Cargo.toml | 6 +-- tests/core_pin.rs | 74 +++++++++++++++++++++++++++++++++ tests/test_release_packaging.py | 44 ++++++++++++++++++++ 4 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 tests/core_pin.rs diff --git a/Cargo.lock b/Cargo.lock index b36479a..a01ec1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -703,7 +703,7 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "pd-host-function" version = "0.1.0" -source = "git+https://github.com/rustscript-lang/rustscript?rev=f5f71ebc2f38e276c35f2aebaf63f4e2780efe99#f5f71ebc2f38e276c35f2aebaf63f4e2780efe99" +source = "git+https://github.com/rustscript-lang/rustscript?rev=b1d6cffede77f49410bf63525f30b9a46b02dc01#b1d6cffede77f49410bf63525f30b9a46b02dc01" dependencies = [ "pd-host-schema", "proc-macro2", @@ -714,7 +714,7 @@ dependencies = [ [[package]] name = "pd-host-schema" version = "0.1.0" -source = "git+https://github.com/rustscript-lang/rustscript?rev=f5f71ebc2f38e276c35f2aebaf63f4e2780efe99#f5f71ebc2f38e276c35f2aebaf63f4e2780efe99" +source = "git+https://github.com/rustscript-lang/rustscript?rev=b1d6cffede77f49410bf63525f30b9a46b02dc01#b1d6cffede77f49410bf63525f30b9a46b02dc01" dependencies = [ "proc-macro2", "syn 2.0.119", @@ -723,7 +723,7 @@ dependencies = [ [[package]] name = "pd-vm" version = "0.1.0" -source = "git+https://github.com/rustscript-lang/rustscript?rev=f5f71ebc2f38e276c35f2aebaf63f4e2780efe99#f5f71ebc2f38e276c35f2aebaf63f4e2780efe99" +source = "git+https://github.com/rustscript-lang/rustscript?rev=b1d6cffede77f49410bf63525f30b9a46b02dc01#b1d6cffede77f49410bf63525f30b9a46b02dc01" dependencies = [ "base64", "futures-channel", diff --git a/Cargo.toml b/Cargo.toml index 99d4dd5..b32d140 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ ngx-rust-rev = "cda9d8372ccd2e139ef2b03e5806e019e7796187" nginx-version = "1.30.4" nginx-tarball-sha256 = "4261dc90e9e47c1c4041276e9aaa3d48ebe2e664f728e14fa95ae6c67d57a08b" rustscript-git = "https://github.com/rustscript-lang/rustscript" -rustscript-rev = "f5f71ebc2f38e276c35f2aebaf63f4e2780efe99" +rustscript-rev = "b1d6cffede77f49410bf63525f30b9a46b02dc01" [lib] crate-type = ["cdylib", "rlib"] @@ -27,7 +27,7 @@ sha1 = { version = "0.10", default-features = false } sha2 = { version = "0.10", default-features = false } nginx-sys = { git = "https://github.com/nginx/ngx-rust", rev = "cda9d8372ccd2e139ef2b03e5806e019e7796187" } ngx = { git = "https://github.com/nginx/ngx-rust", rev = "cda9d8372ccd2e139ef2b03e5806e019e7796187", default-features = false, features = ["std"] } -pd-host-function = { git = "https://github.com/rustscript-lang/rustscript", rev = "f5f71ebc2f38e276c35f2aebaf63f4e2780efe99" } +pd-host-function = { git = "https://github.com/rustscript-lang/rustscript", rev = "b1d6cffede77f49410bf63525f30b9a46b02dc01" } tokio = { version = "1", default-features = false, features = ["rt", "rt-multi-thread", "time", "net", "sync", "io-util"] } rustls = { version = "0.23", default-features = false, features = ["std", "tls12", "ring"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } @@ -36,7 +36,7 @@ rustls-pemfile = { version = "2", default-features = false, features = ["std"] } rustls-native-certs = "0.8" webpki-roots = "0.26" zeroize = { version = "1", default-features = false, features = ["alloc"] } -vm = { package = "pd-vm", git = "https://github.com/rustscript-lang/rustscript", rev = "f5f71ebc2f38e276c35f2aebaf63f4e2780efe99", default-features = false, features = ["runtime", "async", "http-client", "sqlite"] } +vm = { package = "pd-vm", git = "https://github.com/rustscript-lang/rustscript", rev = "b1d6cffede77f49410bf63525f30b9a46b02dc01", default-features = false, features = ["runtime", "async", "http-client", "sqlite"] } [features] default = ["export-modules"] diff --git a/tests/core_pin.rs b/tests/core_pin.rs new file mode 100644 index 0000000..086820d --- /dev/null +++ b/tests/core_pin.rs @@ -0,0 +1,74 @@ +//! Frozen core pin: full SHA only, no stale or abbreviated revisions. + +use std::fs; +use std::path::PathBuf; + +const FROZEN_REV: &str = "b1d6cffede77f49410bf63525f30b9a46b02dc01"; +const STALE_REV: &str = "f5f71ebc2f38e276c35f2aebaf63f4e2780efe99"; + +fn repo_root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) +} + +fn require_full_git_rev(rev: &str, label: &str) -> Result<(), String> { + if rev.len() != 40 || !rev.chars().all(|ch| ch.is_ascii_hexdigit()) { + return Err(format!( + "{label} must be a full 40-character git SHA, got {rev:?}" + )); + } + if rev == STALE_REV { + return Err(format!("{label} still pins the stale core revision {rev}")); + } + Ok(()) +} + +fn quoted_rev_after<'a>(source: &'a str, key: &str) -> Option<&'a str> { + let pattern = format!("{key} = \""); + let start = source.find(&pattern)? + pattern.len(); + let end = source[start..].find('"')? + start; + Some(&source[start..end]) +} + +#[test] +fn rustscript_core_pin_is_frozen_full_sha() { + let root = repo_root(); + let cargo_toml = fs::read_to_string(root.join("Cargo.toml")).expect("Cargo.toml"); + let cargo_lock = fs::read_to_string(root.join("Cargo.lock")).expect("Cargo.lock"); + + let metadata_rev = quoted_rev_after(&cargo_toml, "rustscript-rev").expect("rustscript-rev"); + let host_rev = cargo_toml + .split("pd-host-function") + .nth(1) + .and_then(|rest| quoted_rev_after(rest, "rev")) + .expect("pd-host-function rev"); + let vm_rev = cargo_toml + .split("package = \"pd-vm\"") + .nth(1) + .and_then(|rest| quoted_rev_after(rest, "rev")) + .expect("pd-vm rev"); + + require_full_git_rev(metadata_rev, "rustscript-rev").unwrap(); + require_full_git_rev(host_rev, "pd-host-function.rev").unwrap(); + require_full_git_rev(vm_rev, "pd-vm.rev").unwrap(); + assert_eq!(metadata_rev, FROZEN_REV); + assert_eq!(host_rev, FROZEN_REV); + assert_eq!(vm_rev, FROZEN_REV); + + let expected_source = + format!("git+https://github.com/rustscript-lang/rustscript?rev={FROZEN_REV}#{FROZEN_REV}"); + assert!( + cargo_lock.contains(&expected_source), + "Cargo.lock must resolve pd-vm/pd-host-function to {expected_source}" + ); + assert!( + !cargo_toml.contains(STALE_REV), + "Cargo.toml must not keep the stale pin" + ); + assert!( + !cargo_lock.contains(STALE_REV), + "Cargo.lock must not keep the stale pin" + ); + + assert!(require_full_git_rev("b1d6cff", "abbreviated").is_err()); + assert!(require_full_git_rev(STALE_REV, "stale").is_err()); +} diff --git a/tests/test_release_packaging.py b/tests/test_release_packaging.py index 97e8643..195a695 100644 --- a/tests/test_release_packaging.py +++ b/tests/test_release_packaging.py @@ -89,7 +89,51 @@ def directory_payload(path: Path) -> dict[str, bytes]: } +FROZEN_RUSTSCRIPT_REV = "b1d6cffede77f49410bf63525f30b9a46b02dc01" +STALE_RUSTSCRIPT_REV = "f5f71ebc2f38e276c35f2aebaf63f4e2780efe99" +FULL_GIT_SHA = re.compile(r"^[0-9a-f]{40}$") + + +def require_full_git_rev(rev: str, label: str) -> str: + if not FULL_GIT_SHA.fullmatch(rev): + raise AssertionError(f"{label} must be a full 40-character git SHA, got {rev!r}") + if rev == STALE_RUSTSCRIPT_REV: + raise AssertionError(f"{label} still pins the stale core revision {rev}") + if len(rev) != 40: + raise AssertionError(f"{label} rejected a non-full pin: {rev!r}") + return rev + + class ReleasePackagingTests(unittest.TestCase): + def test_rustscript_core_pin_is_frozen_full_sha(self) -> None: + cargo_toml = (ROOT / "Cargo.toml").read_text() + metadata = tomllib.loads(cargo_toml)["package"]["metadata"]["ngx-rustscript"] + deps = tomllib.loads(cargo_toml)["dependencies"] + lock = (ROOT / "Cargo.lock").read_text() + + metadata_rev = require_full_git_rev(metadata["rustscript-rev"], "package.metadata.ngx-rustscript.rustscript-rev") + host_rev = require_full_git_rev(deps["pd-host-function"]["rev"], "pd-host-function.rev") + vm_rev = require_full_git_rev(deps["vm"]["rev"], "pd-vm.rev") + self.assertEqual(metadata_rev, FROZEN_RUSTSCRIPT_REV) + self.assertEqual(host_rev, FROZEN_RUSTSCRIPT_REV) + self.assertEqual(vm_rev, FROZEN_RUSTSCRIPT_REV) + self.assertEqual(deps["vm"]["package"], "pd-vm") + + expected_source = ( + f"git+https://github.com/rustscript-lang/rustscript?rev={FROZEN_RUSTSCRIPT_REV}" + f"#{FROZEN_RUSTSCRIPT_REV}" + ) + self.assertIn(f'name = "pd-vm"', lock) + self.assertIn(f'name = "pd-host-function"', lock) + self.assertGreaterEqual(lock.count(expected_source), 2) + self.assertNotIn(STALE_RUSTSCRIPT_REV, cargo_toml) + self.assertNotIn(STALE_RUSTSCRIPT_REV, lock) + + with self.assertRaises(AssertionError): + require_full_git_rev("b1d6cff", "abbreviated") + with self.assertRaises(AssertionError): + require_full_git_rev(STALE_RUSTSCRIPT_REV, "stale") + def test_workflows_parse_and_cover_ci_cd_contract(self) -> None: self.assertTrue(CI_WORKFLOW.is_file()) self.assertTrue(RELEASE_WORKFLOW.is_file()) From e45b27ef4ed9035b087e26ab15473e5c7ff58896 Mon Sep 17 00:00:00 2001 From: fffonion Date: Thu, 17 Sep 2026 11:13:11 +0800 Subject: [PATCH 2/6] feat(host): compose ngx hosts from frozen descriptors Replace parallel catalog/schema/registration loops with explicit HostModuleDescriptor composition, typed HostResourceType metadata, owned timer adapters, and named envelope structs for decode_args/get_headers/get_*_args. --- src/host/body.rs | 40 +- src/host/catalog.rs | 907 ++++++++++++++++++++++++++++++++++++-- src/host/codec.rs | 68 ++- src/host/log.rs | 16 +- src/host/meta.rs | 636 +++++++++++++++++--------- src/host/mod.rs | 97 ++-- src/host/output.rs | 28 +- src/host/request.rs | 88 +++- src/host/request_ctx.rs | 24 +- src/host/response_meta.rs | 37 +- src/host/semaphore.rs | 29 +- src/host/shared_dict.rs | 89 +++- src/host/socket.rs | 98 +++- src/host/time.rs | 40 +- src/host/timer.rs | 155 ++++--- src/host/variables.rs | 19 +- src/semaphore.rs | 8 +- src/socket/tcp.rs | 5 + src/socket/tls.rs | 10 + src/socket/udp.rs | 5 + 20 files changed, 1908 insertions(+), 491 deletions(-) diff --git a/src/host/body.rs b/src/host/body.rs index 228482c..d37beed 100644 --- a/src/host/body.rs +++ b/src/host/body.rs @@ -18,6 +18,7 @@ use nginx_sys::{ }; use ngx::http::Request; use pd_host_function::pd_host_function; +use vm::HostModuleDescriptor; use vm::{Value, VmError}; use super::super::VmResult; @@ -254,7 +255,7 @@ fn map_wait_outcome(outcome: NginxOpOutcome) -> VmResult<()> { } /// Read the client request body. Yields on NGX_AGAIN; idempotent after completion. -#[pd_host_function(name = "ngx::req::read_body")] +#[pd_host_function(name = "ngx::req::read_body", contract = crate::host::catalog::ngx_req_read_body_contract)] pub async fn read_body() -> VmResult<()> { match start_read_body()? { ReadBodyStart::Done => Ok(()), @@ -401,7 +402,7 @@ pub unsafe extern "C" fn ngx_rustscript_on_body_wait_closed(r: *mut ngx_http_req } /// Discard the client request body. Sync and idempotent per NGINX core. -#[pd_host_function(name = "ngx::req::discard_body")] +#[pd_host_function(name = "ngx::req::discard_body", contract = crate::host::catalog::ngx_req_discard_body_contract)] pub fn discard_body() -> VmResult { with_request_write(|request| { let raw = request.as_mut() as *mut ngx_http_request_t; @@ -421,7 +422,7 @@ pub fn discard_body() -> VmResult { } /// In-memory request body as an owned string, or `null` when file-backed/unread. -#[pd_host_function(name = "ngx::req::get_body_data")] +#[pd_host_function(name = "ngx::req::get_body_data", contract = crate::host::catalog::ngx_req_get_body_data_contract)] pub fn get_body_data() -> VmResult { with_request(|request| { let raw = request.as_ref(); @@ -447,7 +448,7 @@ pub fn get_body_data() -> VmResult { } /// NGINX temp-file path as an owned string, or `null` when memory-backed. -#[pd_host_function(name = "ngx::req::get_body_file")] +#[pd_host_function(name = "ngx::req::get_body_file", contract = crate::host::catalog::ngx_req_get_body_file_contract)] pub fn get_body_file() -> VmResult { with_request(|request| { let raw = request.as_ref(); @@ -653,7 +654,7 @@ fn copy_to_private_temp(src: &std::fs::File) -> VmResult<(std::fs::File, PathBuf } /// Replace the in-memory request body transactionally. -#[pd_host_function(name = "ngx::req::set_body_data")] +#[pd_host_function(name = "ngx::req::set_body_data", contract = crate::host::catalog::ngx_req_set_body_data_contract)] pub fn set_body_data(data: String) -> VmResult { with_request_write(|request| { let raw = request.as_mut() as *mut ngx_http_request_t; @@ -664,7 +665,7 @@ pub fn set_body_data(data: String) -> VmResult { /// Install a file-backed request body. `auto_clean=true` copies into a /// module-owned private temp file and never unlinks the caller source. -#[pd_host_function(name = "ngx::req::set_body_file")] +#[pd_host_function(name = "ngx::req::set_body_file", contract = crate::host::catalog::ngx_req_set_body_file_contract)] pub fn set_body_file(path: String, auto_clean: &Value) -> VmResult { let auto_clean = parse_optional_bool(auto_clean, false)?; if path.is_empty() || path.len() > MAX_PATH_BYTES || path.contains('\0') { @@ -736,7 +737,7 @@ pub fn set_body_file(path: String, auto_clean: &Value) -> VmResult } /// Create or reset the request-owned memory body builder. -#[pd_host_function(name = "ngx::req::init_body")] +#[pd_host_function(name = "ngx::req::init_body", contract = crate::host::catalog::ngx_req_init_body_contract)] pub fn init_body(buffer_size: &Value) -> VmResult { let cap = parse_optional_capacity(buffer_size)?; with_request_write(|request| { @@ -753,7 +754,7 @@ pub fn init_body(buffer_size: &Value) -> VmResult { } /// Append bytes to the request-owned builder. -#[pd_host_function(name = "ngx::req::append_body")] +#[pd_host_function(name = "ngx::req::append_body", contract = crate::host::catalog::ngx_req_append_body_contract)] pub fn append_body(data: String) -> VmResult { with_request_write(|request| { let raw = request.as_mut() as *mut ngx_http_request_t; @@ -775,7 +776,7 @@ pub fn append_body(data: String) -> VmResult { } /// Atomically install the builder contents as one memory body. -#[pd_host_function(name = "ngx::req::finish_body")] +#[pd_host_function(name = "ngx::req::finish_body", contract = crate::host::catalog::ngx_req_finish_body_contract)] pub fn finish_body() -> VmResult { with_request_write(|request| { let raw = request.as_mut() as *mut ngx_http_request_t; @@ -801,3 +802,24 @@ pub fn finish_body() -> VmResult { } }) } + + +const BODY_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + read_body_descriptor, + discard_body_descriptor, + get_body_data_descriptor, + get_body_file_descriptor, + set_body_data_descriptor, + set_body_file_descriptor, + init_body_descriptor, + append_body_descriptor, + finish_body_descriptor, +]; + +pub fn body_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.body", + functions: BODY_HOST_MODULE_FUNCTIONS, + resources: &[], + } +} diff --git a/src/host/catalog.rs b/src/host/catalog.rs index ed73083..7533dff 100644 --- a/src/host/catalog.rs +++ b/src/host/catalog.rs @@ -4,13 +4,13 @@ //! capability data. This module is the import allowlist and config-time //! validator. Runtime dispatch stays in [`crate::host::bind_ngx_hosts`]. -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use std::fmt; use std::sync::OnceLock; use vm::{ - HostApiBuilder, HostApiCatalog, HostFunctionSchema, HostParamPassing, HostParamSchema, - HostTypeSchema, ResourceTypeKey, ResourceTypeSchema, + HostApiCatalog, HostFunctionDescriptor, HostFunctionSchema, HostParamPassing, HostParamSchema, + HostStructField, HostTypeSchema, ResourceTypeKey, }; /// Import name compiled into access-phase programs for OpenResty-style sleep. @@ -1770,6 +1770,40 @@ fn nullary(name: &str, return_type: HostTypeSchema) -> HostFunctionSchema { HostFunctionSchema::with_return(name, Vec::new(), return_type) } +fn map_unknown() -> HostTypeSchema { + HostTypeSchema::Map(Box::new(HostTypeSchema::Unknown)) +} + +fn decoded_args_result_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "DecodedArgsResult", + vec![ + HostStructField::new("args", map_unknown()), + HostStructField::new("truncated", HostTypeSchema::Bool), + ], + ) +} + +fn request_headers_result_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "RequestHeadersResult", + vec![ + HostStructField::new("headers", map_unknown()), + HostStructField::new("truncated", HostTypeSchema::Bool), + ], + ) +} + +fn request_args_result_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "RequestArgsResult", + vec![ + HostStructField::new("args", map_unknown()), + HostStructField::new("truncated", HostTypeSchema::Bool), + ], + ) +} + fn dict_handle_param() -> HostParamSchema { HostParamSchema::with_passing( "dict", @@ -1803,7 +1837,7 @@ fn dict_store_schema(name: &'static str) -> HostFunctionSchema { ) } -fn schema_for(name: &str) -> HostFunctionSchema { +fn host_function_contract(name: &str) -> HostFunctionSchema { match name { NGX_SLEEP_NAME => sleep_schema(), NGX_SLEEP_MS_NAME => sleep_ms_schema(), @@ -1857,7 +1891,7 @@ fn schema_for(name: &str) -> HostFunctionSchema { HostParamSchema::value("query", HostTypeSchema::String), HostParamSchema::value("max_args", HostTypeSchema::Unknown), ], - HostTypeSchema::Unknown, + decoded_args_result_schema(), ), NGX_ENCODE_BASE64_NAME => HostFunctionSchema::with_return( NGX_ENCODE_BASE64_NAME, @@ -1931,17 +1965,17 @@ fn schema_for(name: &str) -> HostFunctionSchema { HostParamSchema::value("max_headers", HostTypeSchema::Unknown), HostParamSchema::value("raw", HostTypeSchema::Unknown), ], - HostTypeSchema::Unknown, + request_headers_result_schema(), ), NGX_REQ_GET_URI_ARGS_NAME => HostFunctionSchema::with_return( NGX_REQ_GET_URI_ARGS_NAME, vec![HostParamSchema::value("max_args", HostTypeSchema::Unknown)], - HostTypeSchema::Unknown, + request_args_result_schema(), ), NGX_REQ_GET_POST_ARGS_NAME => HostFunctionSchema::with_return( NGX_REQ_GET_POST_ARGS_NAME, vec![HostParamSchema::value("max_args", HostTypeSchema::Unknown)], - HostTypeSchema::Unknown, + request_args_result_schema(), ), NGX_REQ_READ_BODY_NAME => nullary(NGX_REQ_READ_BODY_NAME, HostTypeSchema::Null), NGX_REQ_DISCARD_BODY_NAME => nullary(NGX_REQ_DISCARD_BODY_NAME, HostTypeSchema::Null), @@ -2485,44 +2519,835 @@ fn is_int_constant(name: &str) -> bool { ) } +pub(crate) fn ngx_req_read_body_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::read_body") +} + +pub(crate) fn ngx_req_discard_body_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::discard_body") +} + +pub(crate) fn ngx_req_get_body_data_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::get_body_data") +} + +pub(crate) fn ngx_req_get_body_file_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::get_body_file") +} + +pub(crate) fn ngx_req_set_body_data_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::set_body_data") +} + +pub(crate) fn ngx_req_set_body_file_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::set_body_file") +} + +pub(crate) fn ngx_req_init_body_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::init_body") +} + +pub(crate) fn ngx_req_append_body_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::append_body") +} + +pub(crate) fn ngx_req_finish_body_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::finish_body") +} + +pub(crate) fn ngx_escape_uri_contract() -> HostFunctionSchema { + host_function_contract("ngx::escape_uri") +} + +pub(crate) fn ngx_unescape_uri_contract() -> HostFunctionSchema { + host_function_contract("ngx::unescape_uri") +} + +pub(crate) fn ngx_encode_args_contract() -> HostFunctionSchema { + host_function_contract("ngx::encode_args") +} + +pub(crate) fn ngx_decode_args_contract() -> HostFunctionSchema { + host_function_contract("ngx::decode_args") +} + +pub(crate) fn ngx_encode_base64_contract() -> HostFunctionSchema { + host_function_contract("ngx::encode_base64") +} + +pub(crate) fn ngx_decode_base64_contract() -> HostFunctionSchema { + host_function_contract("ngx::decode_base64") +} + +pub(crate) fn ngx_decode_base64mime_contract() -> HostFunctionSchema { + host_function_contract("ngx::decode_base64mime") +} + +pub(crate) fn ngx_crc32_short_contract() -> HostFunctionSchema { + host_function_contract("ngx::crc32_short") +} + +pub(crate) fn ngx_crc32_long_contract() -> HostFunctionSchema { + host_function_contract("ngx::crc32_long") +} + +pub(crate) fn ngx_hmac_sha1_contract() -> HostFunctionSchema { + host_function_contract("ngx::hmac_sha1") +} + +pub(crate) fn ngx_md5_contract() -> HostFunctionSchema { + host_function_contract("ngx::md5") +} + +pub(crate) fn ngx_md5_bin_contract() -> HostFunctionSchema { + host_function_contract("ngx::md5_bin") +} + +pub(crate) fn ngx_sha1_bin_contract() -> HostFunctionSchema { + host_function_contract("ngx::sha1_bin") +} + +pub(crate) fn ngx_quote_sql_str_contract() -> HostFunctionSchema { + host_function_contract("ngx::quote_sql_str") +} + +pub(crate) fn ngx_log_contract() -> HostFunctionSchema { + host_function_contract("ngx::log") +} + +pub(crate) fn ngx_core_ok_contract() -> HostFunctionSchema { + host_function_contract("ngx::core::ok") +} + +pub(crate) fn ngx_core_error_contract() -> HostFunctionSchema { + host_function_contract("ngx::core::error") +} + +pub(crate) fn ngx_core_again_contract() -> HostFunctionSchema { + host_function_contract("ngx::core::again") +} + +pub(crate) fn ngx_core_done_contract() -> HostFunctionSchema { + host_function_contract("ngx::core::done") +} + +pub(crate) fn ngx_core_declined_contract() -> HostFunctionSchema { + host_function_contract("ngx::core::declined") +} + +pub(crate) fn ngx_http_get_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::get") +} + +pub(crate) fn ngx_http_head_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::head") +} + +pub(crate) fn ngx_http_put_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::put") +} + +pub(crate) fn ngx_http_post_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::post") +} + +pub(crate) fn ngx_http_delete_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::delete") +} + +pub(crate) fn ngx_http_options_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::options") +} + +pub(crate) fn ngx_http_mkcol_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::mkcol") +} + +pub(crate) fn ngx_http_copy_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::copy") +} + +pub(crate) fn ngx_http_move_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::move") +} + +pub(crate) fn ngx_http_propfind_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::propfind") +} + +pub(crate) fn ngx_http_proppatch_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::proppatch") +} + +pub(crate) fn ngx_http_lock_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::lock") +} + +pub(crate) fn ngx_http_unlock_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::unlock") +} + +pub(crate) fn ngx_http_patch_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::patch") +} + +pub(crate) fn ngx_http_trace_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::trace") +} + +pub(crate) fn ngx_http_status_continue_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_continue") +} + +pub(crate) fn ngx_http_status_switching_protocols_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_switching_protocols") +} + +pub(crate) fn ngx_http_status_ok_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_ok") +} + +pub(crate) fn ngx_http_status_created_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_created") +} + +pub(crate) fn ngx_http_status_accepted_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_accepted") +} + +pub(crate) fn ngx_http_status_no_content_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_no_content") +} + +pub(crate) fn ngx_http_status_partial_content_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_partial_content") +} + +pub(crate) fn ngx_http_status_special_response_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_special_response") +} + +pub(crate) fn ngx_http_status_moved_permanently_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_moved_permanently") +} + +pub(crate) fn ngx_http_status_moved_temporarily_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_moved_temporarily") +} + +pub(crate) fn ngx_http_status_see_other_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_see_other") +} + +pub(crate) fn ngx_http_status_not_modified_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_not_modified") +} + +pub(crate) fn ngx_http_status_temporary_redirect_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_temporary_redirect") +} + +pub(crate) fn ngx_http_status_permanent_redirect_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_permanent_redirect") +} + +pub(crate) fn ngx_http_status_bad_request_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_bad_request") +} + +pub(crate) fn ngx_http_status_unauthorized_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_unauthorized") +} + +pub(crate) fn ngx_http_status_payment_required_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_payment_required") +} + +pub(crate) fn ngx_http_status_forbidden_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_forbidden") +} + +pub(crate) fn ngx_http_status_not_found_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_not_found") +} + +pub(crate) fn ngx_http_status_not_allowed_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_not_allowed") +} + +pub(crate) fn ngx_http_status_not_acceptable_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_not_acceptable") +} + +pub(crate) fn ngx_http_status_request_timeout_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_request_timeout") +} + +pub(crate) fn ngx_http_status_conflict_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_conflict") +} + +pub(crate) fn ngx_http_status_gone_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_gone") +} + +pub(crate) fn ngx_http_status_upgrade_required_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_upgrade_required") +} + +pub(crate) fn ngx_http_status_too_many_requests_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_too_many_requests") +} + +pub(crate) fn ngx_http_status_illegal_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_illegal") +} + +pub(crate) fn ngx_http_status_close_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_close") +} + +pub(crate) fn ngx_http_status_internal_server_error_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_internal_server_error") +} + +pub(crate) fn ngx_http_status_not_implemented_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_not_implemented") +} + +pub(crate) fn ngx_http_status_bad_gateway_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_bad_gateway") +} + +pub(crate) fn ngx_http_status_service_unavailable_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_service_unavailable") +} + +pub(crate) fn ngx_http_status_gateway_timeout_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_gateway_timeout") +} + +pub(crate) fn ngx_http_status_version_not_supported_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_version_not_supported") +} + +pub(crate) fn ngx_http_status_insufficient_storage_contract() -> HostFunctionSchema { + host_function_contract("ngx::http::status_insufficient_storage") +} + +pub(crate) fn ngx_log_level_stderr_contract() -> HostFunctionSchema { + host_function_contract("ngx::log_level::stderr") +} + +pub(crate) fn ngx_log_level_emerg_contract() -> HostFunctionSchema { + host_function_contract("ngx::log_level::emerg") +} + +pub(crate) fn ngx_log_level_alert_contract() -> HostFunctionSchema { + host_function_contract("ngx::log_level::alert") +} + +pub(crate) fn ngx_log_level_crit_contract() -> HostFunctionSchema { + host_function_contract("ngx::log_level::crit") +} + +pub(crate) fn ngx_log_level_error_contract() -> HostFunctionSchema { + host_function_contract("ngx::log_level::error") +} + +pub(crate) fn ngx_log_level_warn_contract() -> HostFunctionSchema { + host_function_contract("ngx::log_level::warn") +} + +pub(crate) fn ngx_log_level_notice_contract() -> HostFunctionSchema { + host_function_contract("ngx::log_level::notice") +} + +pub(crate) fn ngx_log_level_info_contract() -> HostFunctionSchema { + host_function_contract("ngx::log_level::info") +} + +pub(crate) fn ngx_log_level_debug_contract() -> HostFunctionSchema { + host_function_contract("ngx::log_level::debug") +} + +pub(crate) fn ngx_core_null_value_contract() -> HostFunctionSchema { + host_function_contract("ngx::core::null_value") +} + +pub(crate) fn ngx_get_phase_contract() -> HostFunctionSchema { + host_function_contract("ngx::get_phase") +} + +pub(crate) fn ngx_config_subsystem_contract() -> HostFunctionSchema { + host_function_contract("ngx::config::subsystem") +} + +pub(crate) fn ngx_config_debug_contract() -> HostFunctionSchema { + host_function_contract("ngx::config::debug") +} + +pub(crate) fn ngx_config_prefix_contract() -> HostFunctionSchema { + host_function_contract("ngx::config::prefix") +} + +pub(crate) fn ngx_config_nginx_version_contract() -> HostFunctionSchema { + host_function_contract("ngx::config::nginx_version") +} + +pub(crate) fn ngx_config_nginx_configure_contract() -> HostFunctionSchema { + host_function_contract("ngx::config::nginx_configure") +} + +pub(crate) fn ngx_config_ngx_rustscript_version_contract() -> HostFunctionSchema { + host_function_contract("ngx::config::ngx_rustscript_version") +} + +pub(crate) fn ngx_worker_exiting_contract() -> HostFunctionSchema { + host_function_contract("ngx::worker::exiting") +} + +pub(crate) fn ngx_worker_pid_contract() -> HostFunctionSchema { + host_function_contract("ngx::worker::pid") +} + +pub(crate) fn ngx_worker_pids_contract() -> HostFunctionSchema { + host_function_contract("ngx::worker::pids") +} + +pub(crate) fn ngx_worker_count_contract() -> HostFunctionSchema { + host_function_contract("ngx::worker::count") +} + +pub(crate) fn ngx_worker_id_contract() -> HostFunctionSchema { + host_function_contract("ngx::worker::id") +} + +pub(crate) fn ngx_sleep_contract() -> HostFunctionSchema { + host_function_contract("ngx::sleep") +} + +pub(crate) fn ngx_sleep_ms_contract() -> HostFunctionSchema { + host_function_contract("ngx::sleep_ms") +} + +pub(crate) fn ngx_send_headers_contract() -> HostFunctionSchema { + host_function_contract("ngx::send_headers") +} + +pub(crate) fn ngx_print_contract() -> HostFunctionSchema { + host_function_contract("ngx::print") +} + +pub(crate) fn ngx_say_contract() -> HostFunctionSchema { + host_function_contract("ngx::say") +} + +pub(crate) fn ngx_flush_contract() -> HostFunctionSchema { + host_function_contract("ngx::flush") +} + +pub(crate) fn ngx_eof_contract() -> HostFunctionSchema { + host_function_contract("ngx::eof") +} + +pub(crate) fn ngx_req_get_method_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::get_method") +} + +pub(crate) fn ngx_req_http_version_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::http_version") +} + +pub(crate) fn ngx_req_raw_header_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::raw_header") +} + +pub(crate) fn ngx_req_get_headers_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::get_headers") +} + +pub(crate) fn ngx_req_get_uri_args_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::get_uri_args") +} + +pub(crate) fn ngx_req_get_post_args_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::get_post_args") +} + +pub(crate) fn ngx_req_is_internal_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::is_internal") +} + +pub(crate) fn ngx_req_start_time_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::start_time") +} + +pub(crate) fn ngx_is_subrequest_contract() -> HostFunctionSchema { + host_function_contract("ngx::is_subrequest") +} + +pub(crate) fn ngx_headers_sent_contract() -> HostFunctionSchema { + host_function_contract("ngx::headers_sent") +} + +pub(crate) fn ngx_req_set_method_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::set_method") +} + +pub(crate) fn ngx_req_set_uri_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::set_uri") +} + +pub(crate) fn ngx_req_set_uri_args_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::set_uri_args") +} + +pub(crate) fn ngx_req_set_header_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::set_header") +} + +pub(crate) fn ngx_req_clear_header_contract() -> HostFunctionSchema { + host_function_contract("ngx::req::clear_header") +} + +pub(crate) fn ngx_ctx_get_contract() -> HostFunctionSchema { + host_function_contract("ngx::ctx::get") +} + +pub(crate) fn ngx_ctx_set_contract() -> HostFunctionSchema { + host_function_contract("ngx::ctx::set") +} + +pub(crate) fn ngx_ctx_delete_contract() -> HostFunctionSchema { + host_function_contract("ngx::ctx::delete") +} + +pub(crate) fn ngx_ctx_clear_contract() -> HostFunctionSchema { + host_function_contract("ngx::ctx::clear") +} + +pub(crate) fn ngx_status_get_contract() -> HostFunctionSchema { + host_function_contract("ngx::status::get") +} + +pub(crate) fn ngx_status_set_contract() -> HostFunctionSchema { + host_function_contract("ngx::status::set") +} + +pub(crate) fn ngx_header_get_contract() -> HostFunctionSchema { + host_function_contract("ngx::header::get") +} + +pub(crate) fn ngx_header_set_contract() -> HostFunctionSchema { + host_function_contract("ngx::header::set") +} + +pub(crate) fn ngx_header_set_all_contract() -> HostFunctionSchema { + host_function_contract("ngx::header::set_all") +} + +pub(crate) fn ngx_header_clear_contract() -> HostFunctionSchema { + host_function_contract("ngx::header::clear") +} + +pub(crate) fn ngx_exit_contract() -> HostFunctionSchema { + host_function_contract("ngx::exit") +} + +pub(crate) fn ngx_redirect_contract() -> HostFunctionSchema { + host_function_contract("ngx::redirect") +} + +pub(crate) fn ngx_semaphore_new_contract() -> HostFunctionSchema { + host_function_contract("ngx::semaphore::new") +} + +pub(crate) fn ngx_semaphore_wait_contract() -> HostFunctionSchema { + host_function_contract("ngx::semaphore::wait") +} + +pub(crate) fn ngx_semaphore_post_contract() -> HostFunctionSchema { + host_function_contract("ngx::semaphore::post") +} + +pub(crate) fn ngx_semaphore_count_contract() -> HostFunctionSchema { + host_function_contract("ngx::semaphore::count") +} + +pub(crate) fn ngx_shared_dict_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::dict") +} + +pub(crate) fn ngx_shared_get_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::get") +} + +pub(crate) fn ngx_shared_get_stale_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::get_stale") +} + +pub(crate) fn ngx_shared_set_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::set") +} + +pub(crate) fn ngx_shared_safe_set_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::safe_set") +} + +pub(crate) fn ngx_shared_add_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::add") +} + +pub(crate) fn ngx_shared_safe_add_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::safe_add") +} + +pub(crate) fn ngx_shared_replace_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::replace") +} + +pub(crate) fn ngx_shared_delete_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::delete") +} + +pub(crate) fn ngx_shared_incr_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::incr") +} + +pub(crate) fn ngx_shared_ttl_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::ttl") +} + +pub(crate) fn ngx_shared_expire_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::expire") +} + +pub(crate) fn ngx_shared_capacity_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::capacity") +} + +pub(crate) fn ngx_shared_free_space_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::free_space") +} + +pub(crate) fn ngx_shared_lpush_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::lpush") +} + +pub(crate) fn ngx_shared_rpush_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::rpush") +} + +pub(crate) fn ngx_shared_lpop_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::lpop") +} + +pub(crate) fn ngx_shared_rpop_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::rpop") +} + +pub(crate) fn ngx_shared_llen_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::llen") +} + +pub(crate) fn ngx_shared_flush_all_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::flush_all") +} + +pub(crate) fn ngx_shared_flush_expired_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::flush_expired") +} + +pub(crate) fn ngx_shared_get_keys_contract() -> HostFunctionSchema { + host_function_contract("ngx::shared::get_keys") +} + +pub(crate) fn ngx_socket_udp_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::udp") +} + +pub(crate) fn ngx_socket_udp_bind_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::udp::bind") +} + +pub(crate) fn ngx_socket_udp_setpeername_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::udp::setpeername") +} + +pub(crate) fn ngx_socket_udp_send_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::udp::send") +} + +pub(crate) fn ngx_socket_udp_receive_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::udp::receive") +} + +pub(crate) fn ngx_socket_udp_close_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::udp::close") +} + +pub(crate) fn ngx_socket_udp_settimeout_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::udp::settimeout") +} + +pub(crate) fn ngx_socket_tcp_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp") +} + +pub(crate) fn ngx_socket_tcp_connect_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::connect") +} + +pub(crate) fn ngx_socket_connect_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::connect") +} + +pub(crate) fn ngx_socket_tcp_send_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::send") +} + +pub(crate) fn ngx_socket_tcp_receive_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::receive") +} + +pub(crate) fn ngx_socket_tcp_receiveany_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::receiveany") +} + +pub(crate) fn ngx_socket_tcp_receiveuntil_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::receiveuntil") +} + +pub(crate) fn ngx_socket_tcp_close_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::close") +} + +pub(crate) fn ngx_socket_tcp_settimeout_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::settimeout") +} + +pub(crate) fn ngx_socket_tcp_setkeepalive_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::setkeepalive") +} + +pub(crate) fn ngx_socket_tcp_getreusedtimes_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::getreusedtimes") +} + +pub(crate) fn ngx_socket_tcp_sslhandshake_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::sslhandshake") +} + +pub(crate) fn ngx_socket_tcp_settrustedstore_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::settrustedstore") +} + +pub(crate) fn ngx_socket_tcp_setclientcert_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::setclientcert") +} + +pub(crate) fn ngx_socket_tcp_trustedstore_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::trustedstore") +} + +pub(crate) fn ngx_socket_tcp_clientcert_contract() -> HostFunctionSchema { + host_function_contract("ngx::socket::tcp::clientcert") +} + +pub(crate) fn ngx_today_contract() -> HostFunctionSchema { + host_function_contract("ngx::today") +} + +pub(crate) fn ngx_time_contract() -> HostFunctionSchema { + host_function_contract("ngx::time") +} + +pub(crate) fn ngx_now_contract() -> HostFunctionSchema { + host_function_contract("ngx::now") +} + +pub(crate) fn ngx_update_time_contract() -> HostFunctionSchema { + host_function_contract("ngx::update_time") +} + +pub(crate) fn ngx_localtime_contract() -> HostFunctionSchema { + host_function_contract("ngx::localtime") +} + +pub(crate) fn ngx_utctime_contract() -> HostFunctionSchema { + host_function_contract("ngx::utctime") +} + +pub(crate) fn ngx_cookie_time_contract() -> HostFunctionSchema { + host_function_contract("ngx::cookie_time") +} + +pub(crate) fn ngx_http_time_contract() -> HostFunctionSchema { + host_function_contract("ngx::http_time") +} + +pub(crate) fn ngx_parse_http_time_contract() -> HostFunctionSchema { + host_function_contract("ngx::parse_http_time") +} + +pub(crate) fn ngx_var_get_contract() -> HostFunctionSchema { + host_function_contract("ngx::var::get") +} + +pub(crate) fn ngx_var_set_contract() -> HostFunctionSchema { + host_function_contract("ngx::var::set") +} + +pub(crate) fn ngx_timer_at_contract() -> HostFunctionSchema { + host_function_contract("ngx::timer::at") +} + +pub(crate) fn ngx_timer_every_contract() -> HostFunctionSchema { + host_function_contract("ngx::timer::every") +} + +pub(crate) fn ngx_timer_pending_count_contract() -> HostFunctionSchema { + host_function_contract("ngx::timer::pending_count") +} + +pub(crate) fn ngx_timer_running_count_contract() -> HostFunctionSchema { + host_function_contract("ngx::timer::running_count") +} + /// Compiler catalog for production ngx imports. /// -/// This is compiler import-allowlist metadata, not a runtime binding. The -/// pinned `pd-host-function` macro generates the wrapper, argument conversion, -/// and `submit_host_future` call; it does not emit catalog APIs. Bindings stay -/// exclusively macro-generated via [`crate::host::bind_ngx_hosts`]. +/// Schemas, resource types, and named structs come from the explicit +/// [`crate::host::ngx_host_modules`] descriptors, ordered to match +/// [`MANIFEST`] so catalog fingerprints stay stable. Bindings stay +/// name-compatible via [`crate::host::bind_ngx_hosts`] for tests that do not +/// use owned timer dispatch. pub fn ngx_host_catalog() -> HostApiCatalog { - let mut builder = HostApiBuilder::new(); - builder.resource(ResourceTypeSchema::new( - crate::shared_dict::shared_dict_resource_key(), - "Shared dictionary zone handle", - )); - builder.resource(ResourceTypeSchema::new( - udp_socket_resource_key(), - "OpenResty ngx.socket.udp handle", - )); - builder.resource(ResourceTypeSchema::new( - tcp_socket_resource_key(), - "OpenResty ngx.socket.tcp handle", - )); - builder.resource(ResourceTypeSchema::new( - tls_trustedstore_resource_key(), - "VM-owned TLS trusted-store (parsed rustls RootCertStore)", - )); - builder.resource(ResourceTypeSchema::new( - tls_clientcert_resource_key(), - "VM-owned TLS client certificate and private key", - )); - builder.resource(ResourceTypeSchema::new( - semaphore_resource_key(), - "VM-local handle to a worker-owned counting semaphore", - )); + let descriptors: Vec = crate::host::ngx_host_modules() + .iter() + .flat_map(|module| module.descriptors()) + .collect(); + let mut by_name = HashMap::with_capacity(descriptors.len()); + for descriptor in descriptors { + let name = descriptor.schema.name.clone(); + if by_name.insert(name.clone(), descriptor).is_some() { + panic!("duplicate ngx host descriptor: {name}"); + } + } + let mut ordered = Vec::with_capacity(MANIFEST.len()); for entry in MANIFEST { - builder.function(schema_for(entry.name)); + let Some(descriptor) = by_name.remove(entry.name) else { + panic!("ngx host catalog missing descriptor for {}", entry.name); + }; + ordered.push(descriptor); + } + if !by_name.is_empty() { + let mut extras: Vec<_> = by_name.keys().cloned().collect(); + extras.sort(); + panic!("ngx host catalog has descriptors not in the manifest: {extras:?}"); } - builder - .build() - .expect("ngx host catalog must be a valid HostApiCatalog") + HostFunctionDescriptor::collect_catalog(&ordered) + .unwrap_or_else(|error| panic!("ngx host catalog must be a valid HostApiCatalog: {error}")) } /// Compiler catalog for production ngx scripts. diff --git a/src/host/codec.rs b/src/host/codec.rs index 2645749..ca2965b 100644 --- a/src/host/codec.rs +++ b/src/host/codec.rs @@ -5,6 +5,7 @@ // `optional` as compatible with `T` or `null` at a host call site. use pd_host_function::pd_host_function; +use vm::{HostModuleDescriptor, HostNamedStruct, HostStructField, HostTypeSchema}; use vm::{Value, VmError, VmMap}; use super::{CallOutcome, IntoHostCallOutcome}; @@ -26,6 +27,20 @@ struct DecodedArgsResult { truncated: bool, } +impl HostNamedStruct for DecodedArgsResult { + const NAME: &'static str = "DecodedArgsResult"; + + fn host_struct_fields() -> Vec { + vec![ + HostStructField::new( + "args", + HostTypeSchema::Map(Box::new(HostTypeSchema::Unknown)), + ), + HostStructField::new("truncated", HostTypeSchema::Bool), + ] + } +} + impl IntoHostCallOutcome for DecodedArgsResult { fn into_host_call_outcome(self) -> CallOutcome { let mut carrier = VmMap::new(); @@ -214,7 +229,7 @@ fn hex_nibble(byte: u8) -> u8 { /// Escapes `uri` using OpenResty `ngx.escape_uri`. /// /// `escape_type` is `null` (default component / type 2) or an integer 0..=8. -#[pd_host_function(name = "ngx::escape_uri")] +#[pd_host_function(name = "ngx::escape_uri", contract = crate::host::catalog::ngx_escape_uri_contract)] pub fn escape_uri(uri: &str, escape_type: &Value) -> VmResult { limit_input("ngx::escape_uri", uri.len())?; let escape_type = optional_int(escape_type, "ngx::escape_uri type")?.unwrap_or(2); @@ -223,7 +238,7 @@ pub fn escape_uri(uri: &str, escape_type: &Value) -> VmResult { } /// Unescapes URI text. `+` becomes space; invalid `%` sequences are left as-is. -#[pd_host_function(name = "ngx::unescape_uri")] +#[pd_host_function(name = "ngx::unescape_uri", contract = crate::host::catalog::ngx_unescape_uri_contract)] pub fn unescape_uri(uri: &str) -> VmResult { limit_input("ngx::unescape_uri", uri.len())?; ret(bytes_as_string(unescape_bytes(uri.as_bytes()))) @@ -291,7 +306,7 @@ fn push_encoded_pair( /// `map`. Array values emit repeated keys, matching OpenResty table /// values. VmMap duplicate-key inserts overwrite; callers must use arrays for /// multi-value keys. -#[pd_host_function(name = "ngx::encode_args")] +#[pd_host_function(name = "ngx::encode_args", contract = crate::host::catalog::ngx_encode_args_contract)] pub fn encode_args(args: &VmMap) -> VmResult { ret(encode_args_map(args)?) } @@ -395,7 +410,7 @@ pub(crate) fn decode_query_args(query: &[u8], max_args: usize) -> (VmMap, bool) /// Returns `{ args, truncated }`. `max_args` is `null` (default 100), a /// positive cap, or `0` which is clamped to the hard limit 1024 rather than /// becoming unbounded. -#[pd_host_function(name = "ngx::decode_args")] +#[pd_host_function(name = "ngx::decode_args", contract = crate::host::catalog::ngx_decode_args_contract)] pub fn decode_args(query: &str, max_args: &Value) -> VmResult { limit_input("ngx::decode_args", query.len())?; let max_args = match optional_int(max_args, "ngx::decode_args max_args")? { @@ -461,7 +476,7 @@ fn decode_base64_mime(text: &str) -> Option> { /// Encodes string or bytes as standard Base64. `no_padding` is `null`/`false` /// or `true` to drop `=` padding. -#[pd_host_function(name = "ngx::encode_base64")] +#[pd_host_function(name = "ngx::encode_base64", contract = crate::host::catalog::ngx_encode_base64_contract)] pub fn encode_base64(input: &Value, no_padding: &Value) -> VmResult { let bytes = input_bytes(input, "ngx::encode_base64")?; let no_padding = optional_bool(no_padding, "ngx::encode_base64 no_padding")?.unwrap_or(false); @@ -471,7 +486,7 @@ pub fn encode_base64(input: &Value, no_padding: &Value) -> VmResult } /// Decodes standard Base64. Padding may be omitted. Invalid input is `null`. -#[pd_host_function(name = "ngx::decode_base64")] +#[pd_host_function(name = "ngx::decode_base64", contract = crate::host::catalog::ngx_decode_base64_contract)] pub fn decode_base64(text: &str) -> VmResult { limit_input("ngx::decode_base64", text.len())?; ret(decode_base64_bytes(text)) @@ -479,7 +494,7 @@ pub fn decode_base64(text: &str) -> VmResult { /// MIME Base64 decode: non-alphabet bytes are skipped and decoding stops at /// the first `=`, matching OpenResty `ngx.decode_base64mime`. -#[pd_host_function(name = "ngx::decode_base64mime")] +#[pd_host_function(name = "ngx::decode_base64mime", contract = crate::host::catalog::ngx_decode_base64mime_contract)] pub fn decode_base64mime(text: &str) -> VmResult { limit_input("ngx::decode_base64mime", text.len())?; ret(decode_base64_mime(text)) @@ -501,21 +516,21 @@ fn crc32_ieee(data: &[u8]) -> u32 { } /// CRC-32 of `data`. OpenResty `crc32_short` and `crc32_long` currently match. -#[pd_host_function(name = "ngx::crc32_short")] +#[pd_host_function(name = "ngx::crc32_short", contract = crate::host::catalog::ngx_crc32_short_contract)] pub fn crc32_short(data: &str) -> VmResult { limit_input("ngx::crc32_short", data.len())?; ret(i64::from(crc32_ieee(data.as_bytes()))) } /// CRC-32 of `data`. Same result as [`crc32_short`]. -#[pd_host_function(name = "ngx::crc32_long")] +#[pd_host_function(name = "ngx::crc32_long", contract = crate::host::catalog::ngx_crc32_long_contract)] pub fn crc32_long(data: &str) -> VmResult { limit_input("ngx::crc32_long", data.len())?; ret(i64::from(crc32_ieee(data.as_bytes()))) } /// HMAC-SHA1 of `data` with `secret`. Returns raw bytes. -#[pd_host_function(name = "ngx::hmac_sha1")] +#[pd_host_function(name = "ngx::hmac_sha1", contract = crate::host::catalog::ngx_hmac_sha1_contract)] pub fn hmac_sha1(secret: &str, data: &str) -> VmResult { limit_input("ngx::hmac_sha1", secret.len())?; limit_input("ngx::hmac_sha1", data.len())?; @@ -548,28 +563,28 @@ fn to_hex_lower(bytes: &[u8]) -> String { } /// MD5 hex digest (lowercase), matching OpenResty `ngx.md5`. -#[pd_host_function(name = "ngx::md5")] +#[pd_host_function(name = "ngx::md5", contract = crate::host::catalog::ngx_md5_contract)] pub fn md5(data: &str) -> VmResult { limit_input("ngx::md5", data.len())?; ret(to_hex_lower(&md5_bytes(data.as_bytes()))) } /// MD5 raw digest bytes. -#[pd_host_function(name = "ngx::md5_bin")] +#[pd_host_function(name = "ngx::md5_bin", contract = crate::host::catalog::ngx_md5_bin_contract)] pub fn md5_bin(data: &str) -> VmResult { limit_input("ngx::md5_bin", data.len())?; ret(md5_bytes(data.as_bytes())) } /// SHA-1 raw digest bytes. -#[pd_host_function(name = "ngx::sha1_bin")] +#[pd_host_function(name = "ngx::sha1_bin", contract = crate::host::catalog::ngx_sha1_bin_contract)] pub fn sha1_bin(data: &str) -> VmResult { limit_input("ngx::sha1_bin", data.len())?; ret(sha1_bytes(data.as_bytes())) } /// Quotes `value` as a MySQL string literal, matching `ngx.quote_sql_str`. -#[pd_host_function(name = "ngx::quote_sql_str")] +#[pd_host_function(name = "ngx::quote_sql_str", contract = crate::host::catalog::ngx_quote_sql_str_contract)] pub fn quote_sql_str(value: &str) -> VmResult { limit_input("ngx::quote_sql_str", value.len())?; let mut out = String::with_capacity(value.len() + 2); @@ -593,6 +608,31 @@ pub fn quote_sql_str(value: &str) -> VmResult { ret(out) } +const CODEC_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + escape_uri_descriptor, + unescape_uri_descriptor, + encode_args_descriptor, + decode_args_descriptor, + encode_base64_descriptor, + decode_base64_descriptor, + decode_base64mime_descriptor, + crc32_short_descriptor, + crc32_long_descriptor, + hmac_sha1_descriptor, + md5_descriptor, + md5_bin_descriptor, + sha1_bin_descriptor, + quote_sql_str_descriptor, +]; + +pub fn codec_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.codec", + functions: CODEC_HOST_MODULE_FUNCTIONS, + resources: &[], + } +} + #[cfg(test)] mod encode_pair_cap { use super::*; diff --git a/src/host/log.rs b/src/host/log.rs index 11f0be0..2479ad7 100644 --- a/src/host/log.rs +++ b/src/host/log.rs @@ -3,6 +3,7 @@ use nginx_sys::{ngx_cycle, NGX_LOG_DEBUG, NGX_LOG_STDERR}; use ngx::http::Request; use pd_host_function::pd_host_function; +use vm::HostModuleDescriptor; use vm::VmError; use super::super::VmResult; @@ -54,7 +55,7 @@ fn cycle_log() -> Option<*mut nginx_sys::ngx_log_t> { } /// OpenResty `ngx.log(level, message)`. -#[pd_host_function(name = "ngx::log")] +#[pd_host_function(name = "ngx::log", contract = crate::host::catalog::ngx_log_contract)] pub fn log(level: i64, message: &str) -> VmResult { if level < NGX_LOG_STDERR as i64 || level > NGX_LOG_DEBUG as i64 { return Err(VmError::HostError(format!( @@ -78,3 +79,16 @@ pub fn log(level: i64, message: &str) -> VmResult { ); ret(()) } + + +const LOG_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + log_descriptor, +]; + +pub fn log_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.log", + functions: LOG_HOST_MODULE_FUNCTIONS, + resources: &[], + } +} diff --git a/src/host/meta.rs b/src/host/meta.rs index 791d9a3..6817be3 100644 --- a/src/host/meta.rs +++ b/src/host/meta.rs @@ -7,6 +7,7 @@ use nginx_sys::{ngx_cycle, ngx_exiting, ngx_pid, ngx_processes, ngx_str_t, ngx_worker}; use ngx::core::{CoreModuleMainConf, NgxCoreModule}; use pd_host_function::pd_host_function; +use vm::HostModuleDescriptor; use vm::{Value, VmError}; use super::super::VmResult; @@ -63,220 +64,338 @@ fn phase_openresty_name(phase: crate::host::NgxPhase) -> &'static str { } } -macro_rules! ngx_int_const { - ($fn_name:ident, $host:literal, $value:expr) => { - /// Zero-argument OpenResty-compatible NGINX constant. - #[pd_host_function(name = $host)] - pub fn $fn_name() -> VmResult { - ret($value as i64) - } - }; +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::core::ok", contract = crate::host::catalog::ngx_core_ok_contract)] +pub fn core_ok() -> VmResult { + ret(nginx_sys::NGX_OK as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::core::error", contract = crate::host::catalog::ngx_core_error_contract)] +pub fn core_error() -> VmResult { + ret(nginx_sys::NGX_ERROR as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::core::again", contract = crate::host::catalog::ngx_core_again_contract)] +pub fn core_again() -> VmResult { + ret(nginx_sys::NGX_AGAIN as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::core::done", contract = crate::host::catalog::ngx_core_done_contract)] +pub fn core_done() -> VmResult { + ret(nginx_sys::NGX_DONE as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::core::declined", contract = crate::host::catalog::ngx_core_declined_contract)] +pub fn core_declined() -> VmResult { + ret(nginx_sys::NGX_DECLINED as i64) } - -ngx_int_const!(core_ok, "ngx::core::ok", nginx_sys::NGX_OK); -ngx_int_const!(core_error, "ngx::core::error", nginx_sys::NGX_ERROR); -ngx_int_const!(core_again, "ngx::core::again", nginx_sys::NGX_AGAIN); -ngx_int_const!(core_done, "ngx::core::done", nginx_sys::NGX_DONE); -ngx_int_const!(core_declined, "ngx::core::declined", nginx_sys::NGX_DECLINED); /// OpenResty `ngx.null`. Named `null_value` because `null` is a reserved token. -#[pd_host_function(name = "ngx::core::null_value")] +#[pd_host_function(name = "ngx::core::null_value", contract = crate::host::catalog::ngx_core_null_value_contract)] pub fn core_null_value() -> VmResult { ret(()) } -ngx_int_const!(http_get, "ngx::http::get", nginx_sys::NGX_HTTP_GET); -ngx_int_const!(http_head, "ngx::http::head", nginx_sys::NGX_HTTP_HEAD); -ngx_int_const!(http_put, "ngx::http::put", nginx_sys::NGX_HTTP_PUT); -ngx_int_const!(http_post, "ngx::http::post", nginx_sys::NGX_HTTP_POST); -ngx_int_const!(http_delete, "ngx::http::delete", nginx_sys::NGX_HTTP_DELETE); -ngx_int_const!(http_options, "ngx::http::options", nginx_sys::NGX_HTTP_OPTIONS); -ngx_int_const!(http_mkcol, "ngx::http::mkcol", nginx_sys::NGX_HTTP_MKCOL); -ngx_int_const!(http_copy, "ngx::http::copy", nginx_sys::NGX_HTTP_COPY); -ngx_int_const!(http_move, "ngx::http::move", nginx_sys::NGX_HTTP_MOVE); -ngx_int_const!(http_propfind, "ngx::http::propfind", nginx_sys::NGX_HTTP_PROPFIND); -ngx_int_const!(http_proppatch, "ngx::http::proppatch", nginx_sys::NGX_HTTP_PROPPATCH); -ngx_int_const!(http_lock, "ngx::http::lock", nginx_sys::NGX_HTTP_LOCK); -ngx_int_const!(http_unlock, "ngx::http::unlock", nginx_sys::NGX_HTTP_UNLOCK); -ngx_int_const!(http_patch, "ngx::http::patch", nginx_sys::NGX_HTTP_PATCH); -ngx_int_const!(http_trace, "ngx::http::trace", nginx_sys::NGX_HTTP_TRACE); +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::get", contract = crate::host::catalog::ngx_http_get_contract)] +pub fn http_get() -> VmResult { + ret(nginx_sys::NGX_HTTP_GET as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::head", contract = crate::host::catalog::ngx_http_head_contract)] +pub fn http_head() -> VmResult { + ret(nginx_sys::NGX_HTTP_HEAD as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::put", contract = crate::host::catalog::ngx_http_put_contract)] +pub fn http_put() -> VmResult { + ret(nginx_sys::NGX_HTTP_PUT as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::post", contract = crate::host::catalog::ngx_http_post_contract)] +pub fn http_post() -> VmResult { + ret(nginx_sys::NGX_HTTP_POST as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::delete", contract = crate::host::catalog::ngx_http_delete_contract)] +pub fn http_delete() -> VmResult { + ret(nginx_sys::NGX_HTTP_DELETE as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::options", contract = crate::host::catalog::ngx_http_options_contract)] +pub fn http_options() -> VmResult { + ret(nginx_sys::NGX_HTTP_OPTIONS as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::mkcol", contract = crate::host::catalog::ngx_http_mkcol_contract)] +pub fn http_mkcol() -> VmResult { + ret(nginx_sys::NGX_HTTP_MKCOL as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::copy", contract = crate::host::catalog::ngx_http_copy_contract)] +pub fn http_copy() -> VmResult { + ret(nginx_sys::NGX_HTTP_COPY as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::move", contract = crate::host::catalog::ngx_http_move_contract)] +pub fn http_move() -> VmResult { + ret(nginx_sys::NGX_HTTP_MOVE as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::propfind", contract = crate::host::catalog::ngx_http_propfind_contract)] +pub fn http_propfind() -> VmResult { + ret(nginx_sys::NGX_HTTP_PROPFIND as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::proppatch", contract = crate::host::catalog::ngx_http_proppatch_contract)] +pub fn http_proppatch() -> VmResult { + ret(nginx_sys::NGX_HTTP_PROPPATCH as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::lock", contract = crate::host::catalog::ngx_http_lock_contract)] +pub fn http_lock() -> VmResult { + ret(nginx_sys::NGX_HTTP_LOCK as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::unlock", contract = crate::host::catalog::ngx_http_unlock_contract)] +pub fn http_unlock() -> VmResult { + ret(nginx_sys::NGX_HTTP_UNLOCK as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::patch", contract = crate::host::catalog::ngx_http_patch_contract)] +pub fn http_patch() -> VmResult { + ret(nginx_sys::NGX_HTTP_PATCH as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::trace", contract = crate::host::catalog::ngx_http_trace_contract)] +pub fn http_trace() -> VmResult { + ret(nginx_sys::NGX_HTTP_TRACE as i64) +} -ngx_int_const!( - http_status_continue, - "ngx::http::status_continue", - nginx_sys::NGX_HTTP_CONTINUE -); -ngx_int_const!( - http_status_switching_protocols, - "ngx::http::status_switching_protocols", - nginx_sys::NGX_HTTP_SWITCHING_PROTOCOLS -); -ngx_int_const!(http_status_ok, "ngx::http::status_ok", nginx_sys::NGX_HTTP_OK); -ngx_int_const!( - http_status_created, - "ngx::http::status_created", - nginx_sys::NGX_HTTP_CREATED -); -ngx_int_const!( - http_status_accepted, - "ngx::http::status_accepted", - nginx_sys::NGX_HTTP_ACCEPTED -); -ngx_int_const!( - http_status_no_content, - "ngx::http::status_no_content", - nginx_sys::NGX_HTTP_NO_CONTENT -); -ngx_int_const!( - http_status_partial_content, - "ngx::http::status_partial_content", - nginx_sys::NGX_HTTP_PARTIAL_CONTENT -); -ngx_int_const!( - http_status_special_response, - "ngx::http::status_special_response", - nginx_sys::NGX_HTTP_SPECIAL_RESPONSE -); -ngx_int_const!( - http_status_moved_permanently, - "ngx::http::status_moved_permanently", - nginx_sys::NGX_HTTP_MOVED_PERMANENTLY -); -ngx_int_const!( - http_status_moved_temporarily, - "ngx::http::status_moved_temporarily", - nginx_sys::NGX_HTTP_MOVED_TEMPORARILY -); -ngx_int_const!( - http_status_see_other, - "ngx::http::status_see_other", - nginx_sys::NGX_HTTP_SEE_OTHER -); -ngx_int_const!( - http_status_not_modified, - "ngx::http::status_not_modified", - nginx_sys::NGX_HTTP_NOT_MODIFIED -); -ngx_int_const!( - http_status_temporary_redirect, - "ngx::http::status_temporary_redirect", - nginx_sys::NGX_HTTP_TEMPORARY_REDIRECT -); -ngx_int_const!( - http_status_permanent_redirect, - "ngx::http::status_permanent_redirect", - nginx_sys::NGX_HTTP_PERMANENT_REDIRECT -); -ngx_int_const!( - http_status_bad_request, - "ngx::http::status_bad_request", - nginx_sys::NGX_HTTP_BAD_REQUEST -); -ngx_int_const!( - http_status_unauthorized, - "ngx::http::status_unauthorized", - nginx_sys::NGX_HTTP_UNAUTHORIZED -); -ngx_int_const!( - http_status_payment_required, - "ngx::http::status_payment_required", - 402 -); -ngx_int_const!( - http_status_forbidden, - "ngx::http::status_forbidden", - nginx_sys::NGX_HTTP_FORBIDDEN -); -ngx_int_const!( - http_status_not_found, - "ngx::http::status_not_found", - nginx_sys::NGX_HTTP_NOT_FOUND -); -ngx_int_const!( - http_status_not_allowed, - "ngx::http::status_not_allowed", - nginx_sys::NGX_HTTP_NOT_ALLOWED -); -ngx_int_const!( - http_status_not_acceptable, - "ngx::http::status_not_acceptable", - 406 -); -ngx_int_const!( - http_status_request_timeout, - "ngx::http::status_request_timeout", - nginx_sys::NGX_HTTP_REQUEST_TIME_OUT -); -ngx_int_const!( - http_status_conflict, - "ngx::http::status_conflict", - nginx_sys::NGX_HTTP_CONFLICT -); -ngx_int_const!(http_status_gone, "ngx::http::status_gone", 410); -ngx_int_const!( - http_status_upgrade_required, - "ngx::http::status_upgrade_required", - 426 -); -ngx_int_const!( - http_status_too_many_requests, - "ngx::http::status_too_many_requests", - nginx_sys::NGX_HTTP_TOO_MANY_REQUESTS -); -ngx_int_const!(http_status_illegal, "ngx::http::status_illegal", 451); -ngx_int_const!( - http_status_close, - "ngx::http::status_close", - nginx_sys::NGX_HTTP_CLOSE -); -ngx_int_const!( - http_status_internal_server_error, - "ngx::http::status_internal_server_error", - nginx_sys::NGX_HTTP_INTERNAL_SERVER_ERROR -); -ngx_int_const!( - http_status_not_implemented, - "ngx::http::status_not_implemented", - nginx_sys::NGX_HTTP_NOT_IMPLEMENTED -); -ngx_int_const!( - http_status_bad_gateway, - "ngx::http::status_bad_gateway", - nginx_sys::NGX_HTTP_BAD_GATEWAY -); -ngx_int_const!( - http_status_service_unavailable, - "ngx::http::status_service_unavailable", - nginx_sys::NGX_HTTP_SERVICE_UNAVAILABLE -); -ngx_int_const!( - http_status_gateway_timeout, - "ngx::http::status_gateway_timeout", - nginx_sys::NGX_HTTP_GATEWAY_TIME_OUT -); -ngx_int_const!( - http_status_version_not_supported, - "ngx::http::status_version_not_supported", - nginx_sys::NGX_HTTP_VERSION_NOT_SUPPORTED -); -ngx_int_const!( - http_status_insufficient_storage, - "ngx::http::status_insufficient_storage", - nginx_sys::NGX_HTTP_INSUFFICIENT_STORAGE -); +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_continue", contract = crate::host::catalog::ngx_http_status_continue_contract)] +pub fn http_status_continue() -> VmResult { + ret(nginx_sys::NGX_HTTP_CONTINUE as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_switching_protocols", contract = crate::host::catalog::ngx_http_status_switching_protocols_contract)] +pub fn http_status_switching_protocols() -> VmResult { + ret(nginx_sys::NGX_HTTP_SWITCHING_PROTOCOLS as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_ok", contract = crate::host::catalog::ngx_http_status_ok_contract)] +pub fn http_status_ok() -> VmResult { + ret(nginx_sys::NGX_HTTP_OK as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_created", contract = crate::host::catalog::ngx_http_status_created_contract)] +pub fn http_status_created() -> VmResult { + ret(nginx_sys::NGX_HTTP_CREATED as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_accepted", contract = crate::host::catalog::ngx_http_status_accepted_contract)] +pub fn http_status_accepted() -> VmResult { + ret(nginx_sys::NGX_HTTP_ACCEPTED as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_no_content", contract = crate::host::catalog::ngx_http_status_no_content_contract)] +pub fn http_status_no_content() -> VmResult { + ret(nginx_sys::NGX_HTTP_NO_CONTENT as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_partial_content", contract = crate::host::catalog::ngx_http_status_partial_content_contract)] +pub fn http_status_partial_content() -> VmResult { + ret(nginx_sys::NGX_HTTP_PARTIAL_CONTENT as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_special_response", contract = crate::host::catalog::ngx_http_status_special_response_contract)] +pub fn http_status_special_response() -> VmResult { + ret(nginx_sys::NGX_HTTP_SPECIAL_RESPONSE as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_moved_permanently", contract = crate::host::catalog::ngx_http_status_moved_permanently_contract)] +pub fn http_status_moved_permanently() -> VmResult { + ret(nginx_sys::NGX_HTTP_MOVED_PERMANENTLY as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_moved_temporarily", contract = crate::host::catalog::ngx_http_status_moved_temporarily_contract)] +pub fn http_status_moved_temporarily() -> VmResult { + ret(nginx_sys::NGX_HTTP_MOVED_TEMPORARILY as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_see_other", contract = crate::host::catalog::ngx_http_status_see_other_contract)] +pub fn http_status_see_other() -> VmResult { + ret(nginx_sys::NGX_HTTP_SEE_OTHER as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_not_modified", contract = crate::host::catalog::ngx_http_status_not_modified_contract)] +pub fn http_status_not_modified() -> VmResult { + ret(nginx_sys::NGX_HTTP_NOT_MODIFIED as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_temporary_redirect", contract = crate::host::catalog::ngx_http_status_temporary_redirect_contract)] +pub fn http_status_temporary_redirect() -> VmResult { + ret(nginx_sys::NGX_HTTP_TEMPORARY_REDIRECT as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_permanent_redirect", contract = crate::host::catalog::ngx_http_status_permanent_redirect_contract)] +pub fn http_status_permanent_redirect() -> VmResult { + ret(nginx_sys::NGX_HTTP_PERMANENT_REDIRECT as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_bad_request", contract = crate::host::catalog::ngx_http_status_bad_request_contract)] +pub fn http_status_bad_request() -> VmResult { + ret(nginx_sys::NGX_HTTP_BAD_REQUEST as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_unauthorized", contract = crate::host::catalog::ngx_http_status_unauthorized_contract)] +pub fn http_status_unauthorized() -> VmResult { + ret(nginx_sys::NGX_HTTP_UNAUTHORIZED as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_payment_required", contract = crate::host::catalog::ngx_http_status_payment_required_contract)] +pub fn http_status_payment_required() -> VmResult { + ret(402_i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_forbidden", contract = crate::host::catalog::ngx_http_status_forbidden_contract)] +pub fn http_status_forbidden() -> VmResult { + ret(nginx_sys::NGX_HTTP_FORBIDDEN as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_not_found", contract = crate::host::catalog::ngx_http_status_not_found_contract)] +pub fn http_status_not_found() -> VmResult { + ret(nginx_sys::NGX_HTTP_NOT_FOUND as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_not_allowed", contract = crate::host::catalog::ngx_http_status_not_allowed_contract)] +pub fn http_status_not_allowed() -> VmResult { + ret(nginx_sys::NGX_HTTP_NOT_ALLOWED as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_not_acceptable", contract = crate::host::catalog::ngx_http_status_not_acceptable_contract)] +pub fn http_status_not_acceptable() -> VmResult { + ret(406_i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_request_timeout", contract = crate::host::catalog::ngx_http_status_request_timeout_contract)] +pub fn http_status_request_timeout() -> VmResult { + ret(nginx_sys::NGX_HTTP_REQUEST_TIME_OUT as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_conflict", contract = crate::host::catalog::ngx_http_status_conflict_contract)] +pub fn http_status_conflict() -> VmResult { + ret(nginx_sys::NGX_HTTP_CONFLICT as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_gone", contract = crate::host::catalog::ngx_http_status_gone_contract)] +pub fn http_status_gone() -> VmResult { + ret(410_i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_upgrade_required", contract = crate::host::catalog::ngx_http_status_upgrade_required_contract)] +pub fn http_status_upgrade_required() -> VmResult { + ret(426_i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_too_many_requests", contract = crate::host::catalog::ngx_http_status_too_many_requests_contract)] +pub fn http_status_too_many_requests() -> VmResult { + ret(nginx_sys::NGX_HTTP_TOO_MANY_REQUESTS as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_illegal", contract = crate::host::catalog::ngx_http_status_illegal_contract)] +pub fn http_status_illegal() -> VmResult { + ret(451_i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_close", contract = crate::host::catalog::ngx_http_status_close_contract)] +pub fn http_status_close() -> VmResult { + ret(nginx_sys::NGX_HTTP_CLOSE as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_internal_server_error", contract = crate::host::catalog::ngx_http_status_internal_server_error_contract)] +pub fn http_status_internal_server_error() -> VmResult { + ret(nginx_sys::NGX_HTTP_INTERNAL_SERVER_ERROR as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_not_implemented", contract = crate::host::catalog::ngx_http_status_not_implemented_contract)] +pub fn http_status_not_implemented() -> VmResult { + ret(nginx_sys::NGX_HTTP_NOT_IMPLEMENTED as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_bad_gateway", contract = crate::host::catalog::ngx_http_status_bad_gateway_contract)] +pub fn http_status_bad_gateway() -> VmResult { + ret(nginx_sys::NGX_HTTP_BAD_GATEWAY as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_service_unavailable", contract = crate::host::catalog::ngx_http_status_service_unavailable_contract)] +pub fn http_status_service_unavailable() -> VmResult { + ret(nginx_sys::NGX_HTTP_SERVICE_UNAVAILABLE as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_gateway_timeout", contract = crate::host::catalog::ngx_http_status_gateway_timeout_contract)] +pub fn http_status_gateway_timeout() -> VmResult { + ret(nginx_sys::NGX_HTTP_GATEWAY_TIME_OUT as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_version_not_supported", contract = crate::host::catalog::ngx_http_status_version_not_supported_contract)] +pub fn http_status_version_not_supported() -> VmResult { + ret(nginx_sys::NGX_HTTP_VERSION_NOT_SUPPORTED as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::http::status_insufficient_storage", contract = crate::host::catalog::ngx_http_status_insufficient_storage_contract)] +pub fn http_status_insufficient_storage() -> VmResult { + ret(nginx_sys::NGX_HTTP_INSUFFICIENT_STORAGE as i64) +} -ngx_int_const!(log_level_stderr, "ngx::log_level::stderr", nginx_sys::NGX_LOG_STDERR); -ngx_int_const!(log_level_emerg, "ngx::log_level::emerg", nginx_sys::NGX_LOG_EMERG); -ngx_int_const!(log_level_alert, "ngx::log_level::alert", nginx_sys::NGX_LOG_ALERT); -ngx_int_const!(log_level_crit, "ngx::log_level::crit", nginx_sys::NGX_LOG_CRIT); -ngx_int_const!(log_level_error, "ngx::log_level::error", nginx_sys::NGX_LOG_ERR); -ngx_int_const!(log_level_warn, "ngx::log_level::warn", nginx_sys::NGX_LOG_WARN); -ngx_int_const!(log_level_notice, "ngx::log_level::notice", nginx_sys::NGX_LOG_NOTICE); -ngx_int_const!(log_level_info, "ngx::log_level::info", nginx_sys::NGX_LOG_INFO); -ngx_int_const!(log_level_debug, "ngx::log_level::debug", nginx_sys::NGX_LOG_DEBUG); +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::log_level::stderr", contract = crate::host::catalog::ngx_log_level_stderr_contract)] +pub fn log_level_stderr() -> VmResult { + ret(nginx_sys::NGX_LOG_STDERR as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::log_level::emerg", contract = crate::host::catalog::ngx_log_level_emerg_contract)] +pub fn log_level_emerg() -> VmResult { + ret(nginx_sys::NGX_LOG_EMERG as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::log_level::alert", contract = crate::host::catalog::ngx_log_level_alert_contract)] +pub fn log_level_alert() -> VmResult { + ret(nginx_sys::NGX_LOG_ALERT as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::log_level::crit", contract = crate::host::catalog::ngx_log_level_crit_contract)] +pub fn log_level_crit() -> VmResult { + ret(nginx_sys::NGX_LOG_CRIT as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::log_level::error", contract = crate::host::catalog::ngx_log_level_error_contract)] +pub fn log_level_error() -> VmResult { + ret(nginx_sys::NGX_LOG_ERR as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::log_level::warn", contract = crate::host::catalog::ngx_log_level_warn_contract)] +pub fn log_level_warn() -> VmResult { + ret(nginx_sys::NGX_LOG_WARN as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::log_level::notice", contract = crate::host::catalog::ngx_log_level_notice_contract)] +pub fn log_level_notice() -> VmResult { + ret(nginx_sys::NGX_LOG_NOTICE as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::log_level::info", contract = crate::host::catalog::ngx_log_level_info_contract)] +pub fn log_level_info() -> VmResult { + ret(nginx_sys::NGX_LOG_INFO as i64) +} +/// Zero-argument OpenResty-compatible NGINX constant. +#[pd_host_function(name = "ngx::log_level::debug", contract = crate::host::catalog::ngx_log_level_debug_contract)] +pub fn log_level_debug() -> VmResult { + ret(nginx_sys::NGX_LOG_DEBUG as i64) +} /// Current NGINX execution phase as an OpenResty `ngx.get_phase` name. -#[pd_host_function(name = "ngx::get_phase")] +#[pd_host_function(name = "ngx::get_phase", contract = crate::host::catalog::ngx_get_phase_contract)] pub fn get_phase() -> VmResult { match crate::host::current_phase() { Ok(phase) => ret(phase_openresty_name(phase)), @@ -287,19 +406,19 @@ pub fn get_phase() -> VmResult { } /// OpenResty `ngx.config.subsystem` (`http`). -#[pd_host_function(name = "ngx::config::subsystem")] +#[pd_host_function(name = "ngx::config::subsystem", contract = crate::host::catalog::ngx_config_subsystem_contract)] pub fn config_subsystem() -> VmResult { ret("http") } /// OpenResty `ngx.config.debug`. -#[pd_host_function(name = "ngx::config::debug")] +#[pd_host_function(name = "ngx::config::debug", contract = crate::host::catalog::ngx_config_debug_contract)] pub fn config_debug() -> VmResult { ret(ngx::log::DEBUG) } /// OpenResty `ngx.config.prefix`. -#[pd_host_function(name = "ngx::config::prefix")] +#[pd_host_function(name = "ngx::config::prefix", contract = crate::host::catalog::ngx_config_prefix_contract)] pub fn config_prefix() -> VmResult { let cycle = live_cycle().map_err(|_| { VmError::HostError("ngx::config::prefix requires a live NGINX cycle".into()) @@ -308,25 +427,25 @@ pub fn config_prefix() -> VmResult { } /// OpenResty `ngx.config.nginx_version`. -#[pd_host_function(name = "ngx::config::nginx_version")] +#[pd_host_function(name = "ngx::config::nginx_version", contract = crate::host::catalog::ngx_config_nginx_version_contract)] pub fn config_nginx_version() -> VmResult { ret(nginx_sys::nginx_version as i64) } /// OpenResty `ngx.config.nginx_configure`. -#[pd_host_function(name = "ngx::config::nginx_configure")] +#[pd_host_function(name = "ngx::config::nginx_configure", contract = crate::host::catalog::ngx_config_nginx_configure_contract)] pub fn config_nginx_configure() -> VmResult { ret(nginx_sys::NGX_CONFIGURE.to_string_lossy().into_owned()) } /// Module version analog of `ngx.config.ngx_lua_version`. -#[pd_host_function(name = "ngx::config::ngx_rustscript_version")] +#[pd_host_function(name = "ngx::config::ngx_rustscript_version", contract = crate::host::catalog::ngx_config_ngx_rustscript_version_contract)] pub fn config_ngx_rustscript_version() -> VmResult { ret(NGX_RUSTSCRIPT_VERSION) } /// OpenResty `ngx.worker.exiting`. -#[pd_host_function(name = "ngx::worker::exiting")] +#[pd_host_function(name = "ngx::worker::exiting", contract = crate::host::catalog::ngx_worker_exiting_contract)] pub fn worker_exiting() -> VmResult { // SAFETY: `ngx_exiting` is a worker-local NGINX global. let exiting = unsafe { core::ptr::addr_of!(ngx_exiting).read() }; @@ -334,7 +453,7 @@ pub fn worker_exiting() -> VmResult { } /// OpenResty `ngx.worker.pid`. -#[pd_host_function(name = "ngx::worker::pid")] +#[pd_host_function(name = "ngx::worker::pid", contract = crate::host::catalog::ngx_worker_pid_contract)] pub fn worker_pid() -> VmResult { // SAFETY: `ngx_pid` is the current process pid published by NGINX. let pid = unsafe { core::ptr::addr_of!(ngx_pid).read() }; @@ -348,7 +467,7 @@ pub fn worker_pid() -> VmResult { /// entries. Invalid `ngx_process_slot` values are ignored. Duplicate pids are /// collapsed so a mixed table and a `master_process off` worker both stay /// unique. -#[pd_host_function(name = "ngx::worker::pids")] +#[pd_host_function(name = "ngx::worker::pids", contract = crate::host::catalog::ngx_worker_pids_contract)] pub fn worker_pids() -> VmResult { let mut pids = Vec::new(); let mut seen = std::collections::BTreeSet::new(); @@ -369,7 +488,7 @@ pub fn worker_pids() -> VmResult { } /// OpenResty `ngx.worker.count`. -#[pd_host_function(name = "ngx::worker::count")] +#[pd_host_function(name = "ngx::worker::count", contract = crate::host::catalog::ngx_worker_count_contract)] pub fn worker_count() -> VmResult { let cycle = live_cycle().map_err(|_| { VmError::HostError("ngx::worker::count requires a live NGINX cycle".into()) @@ -383,9 +502,98 @@ pub fn worker_count() -> VmResult { } /// OpenResty `ngx.worker.id`. -#[pd_host_function(name = "ngx::worker::id")] +#[pd_host_function(name = "ngx::worker::id", contract = crate::host::catalog::ngx_worker_id_contract)] pub fn worker_id() -> VmResult { // SAFETY: `ngx_worker` is the worker ordinal assigned at process init. let id = unsafe { core::ptr::addr_of!(ngx_worker).read() }; ret(id as i64) } + + +const META_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + core_ok_descriptor, + core_error_descriptor, + core_again_descriptor, + core_done_descriptor, + core_declined_descriptor, + http_get_descriptor, + http_head_descriptor, + http_put_descriptor, + http_post_descriptor, + http_delete_descriptor, + http_options_descriptor, + http_mkcol_descriptor, + http_copy_descriptor, + http_move_descriptor, + http_propfind_descriptor, + http_proppatch_descriptor, + http_lock_descriptor, + http_unlock_descriptor, + http_patch_descriptor, + http_trace_descriptor, + http_status_continue_descriptor, + http_status_switching_protocols_descriptor, + http_status_ok_descriptor, + http_status_created_descriptor, + http_status_accepted_descriptor, + http_status_no_content_descriptor, + http_status_partial_content_descriptor, + http_status_special_response_descriptor, + http_status_moved_permanently_descriptor, + http_status_moved_temporarily_descriptor, + http_status_see_other_descriptor, + http_status_not_modified_descriptor, + http_status_temporary_redirect_descriptor, + http_status_permanent_redirect_descriptor, + http_status_bad_request_descriptor, + http_status_unauthorized_descriptor, + http_status_payment_required_descriptor, + http_status_forbidden_descriptor, + http_status_not_found_descriptor, + http_status_not_allowed_descriptor, + http_status_not_acceptable_descriptor, + http_status_request_timeout_descriptor, + http_status_conflict_descriptor, + http_status_gone_descriptor, + http_status_upgrade_required_descriptor, + http_status_too_many_requests_descriptor, + http_status_illegal_descriptor, + http_status_close_descriptor, + http_status_internal_server_error_descriptor, + http_status_not_implemented_descriptor, + http_status_bad_gateway_descriptor, + http_status_service_unavailable_descriptor, + http_status_gateway_timeout_descriptor, + http_status_version_not_supported_descriptor, + http_status_insufficient_storage_descriptor, + log_level_stderr_descriptor, + log_level_emerg_descriptor, + log_level_alert_descriptor, + log_level_crit_descriptor, + log_level_error_descriptor, + log_level_warn_descriptor, + log_level_notice_descriptor, + log_level_info_descriptor, + log_level_debug_descriptor, + core_null_value_descriptor, + get_phase_descriptor, + config_subsystem_descriptor, + config_debug_descriptor, + config_prefix_descriptor, + config_nginx_version_descriptor, + config_nginx_configure_descriptor, + config_ngx_rustscript_version_descriptor, + worker_exiting_descriptor, + worker_pid_descriptor, + worker_pids_descriptor, + worker_count_descriptor, + worker_id_descriptor, +]; + +pub fn meta_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.meta", + functions: META_HOST_MODULE_FUNCTIONS, + resources: &[], + } +} diff --git a/src/host/mod.rs b/src/host/mod.rs index a8fa72b..f39cd55 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -1191,64 +1191,16 @@ pub fn ngx_host_registry() -> Result { fn build_ngx_host_registry() -> Result { let catalog = catalog::compile_host_catalog(); - // The catalog fingerprint is a whole-catalog hash. Compute it once and - // reuse it for every exact import schema instead of paying it per host. - let fingerprint = catalog.fingerprint(); - // Build from `empty()`, not `new()`: `new()` pre-registers pd-vm's own - // standard catalog snapshot (HTTP named-record schemas with the *standard* - // catalog fingerprint). This registry owns every entry itself — the ngx - // hosts and timer family from the ngx catalog, and the standard surface - // through the composition — so a program's exact import schemas resolve - // against exactly the catalog it was compiled with, and a standard import - // that the compiler leaves dynamic resolves through the staged standard - // composition by name. let mut registry = vm::HostFunctionRegistry::empty(); - registry - .install_named_struct_schemas(vm::catalog_named_struct_schemas(&catalog)) - .map_err(|error| format!("install named struct schemas: {error}"))?; registry.set_standard_composition(vm::standard_composition()); - for (name, wrapper) in ngx_host_bindings() { - let functions = catalog.functions_named(name); - if functions.is_empty() { - return Err(format!("{name} is missing from the ngx compile catalog")); - } - for function in functions { - let schema = import_schema_with_fingerprint(function, &fingerprint); - let result = match wrapper { - NgxHostWrapper::Static(host) => registry.register_catalog_static(schema, *host), - NgxHostWrapper::Args(host) => registry.register_catalog_static_args(schema, *host), - }; - result.map_err(|error| format!("register {name}: {error}"))?; - } + for module in ngx_host_modules() { + module + .install_from_catalog(&mut registry, &catalog) + .map_err(|error| format!("install {}: {error}", module.name))?; } - crate::host::timer::register_timer_hosts(&mut registry, &catalog)?; Ok(registry) } -/// Builds the exact import schema the compiler embeds for `function`. -/// -/// Identical to [`vm::HostImportSchema::from_function`] except that the -/// caller-supplied whole-catalog fingerprint is reused. -fn import_schema_with_fingerprint( - function: &vm::HostFunctionSchema, - fingerprint: &vm::HostApiFingerprint, -) -> vm::HostImportSchema { - vm::HostImportSchema { - name: function.name.clone(), - params: function - .params - .iter() - .map(|param| vm::HostImportParam { - name: param.name.clone(), - schema: param.ty.clone(), - passing: param.passing, - }) - .collect(), - return_type: function.return_type.clone(), - fingerprint: *fingerprint, - } -} - #[cfg(test)] pub(crate) fn bind_test_hosts(vm: &mut Vm) { bind_ngx_hosts(vm); @@ -1258,6 +1210,7 @@ pub(crate) fn bind_test_hosts(vm: &mut Vm) { } mod layout { + #[allow(unused_imports)] pub use vm::{Value, Vm, VmError, VmResult, resource}; pub mod helpers { @@ -1389,7 +1342,7 @@ mod layout { /// `seconds` is an OpenResty-style number (integer or float). The /// generated wrapper submits an owned host future; this body only /// validates and awaits Tokio. - #[pd_host_function(name = "ngx::sleep")] + #[pd_host_function(name = "ngx::sleep", contract = crate::host::catalog::ngx_sleep_contract)] pub async fn sleep(seconds: NumberValue) -> VmResult<()> { let duration = duration_from_seconds(seconds)?; tokio::time::sleep(duration).await; @@ -1400,7 +1353,7 @@ mod layout { /// /// `ms` must be a nonnegative integer. The generated wrapper submits /// an owned host future; this body only validates and awaits Tokio. - #[pd_host_function(name = "ngx::sleep_ms")] + #[pd_host_function(name = "ngx::sleep_ms", contract = crate::host::catalog::ngx_sleep_ms_contract)] pub async fn sleep_ms(ms: i64) -> VmResult<()> { if ms < 0 { return Err(VmError::HostError(format!( @@ -1438,10 +1391,46 @@ mod layout { pub async fn fail() -> VmResult { Err(VmError::HostError("ngx::fail".into())) } + + const FUNCTIONS_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = + &[sleep_descriptor, sleep_ms_descriptor]; + + pub fn functions_host_module() -> vm::HostModuleDescriptor { + vm::HostModuleDescriptor { + name: "ngx.sleep", + functions: FUNCTIONS_HOST_MODULE_FUNCTIONS, + resources: &[], + } + } } } } +/// Deterministic ngx host module composition. +/// +/// Catalog schemas, exact adapters, resource types, and named structs are +/// derived from these descriptors. `bind_ngx_hosts` remains a name-binding +/// compatibility path that does not install owned timer adapters. +pub fn ngx_host_modules() -> Vec { + vec![ + layout::helpers::functions::functions_host_module(), + layout::helpers::codec::codec_host_module(), + layout::helpers::time::time_host_module(), + layout::helpers::meta::meta_host_module(), + layout::helpers::log::log_host_module(), + layout::helpers::request::request_host_module(), + layout::helpers::body::body_host_module(), + layout::helpers::variables::variables_host_module(), + layout::helpers::response_meta::response_meta_host_module(), + layout::helpers::request_ctx::request_ctx_host_module(), + layout::helpers::shared_dict::shared_dict_host_module(), + layout::helpers::socket::socket_host_module(), + layout::helpers::semaphore::semaphore_host_module(), + layout::helpers::output::output_host_module(), + crate::host::timer::timer_host_module(), + ] +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/host/output.rs b/src/host/output.rs index 62d603f..93dd7a9 100644 --- a/src/host/output.rs +++ b/src/host/output.rs @@ -22,6 +22,7 @@ use nginx_sys::{ }; use ngx::http::Request; use pd_host_function::pd_host_function; +use vm::HostModuleDescriptor; use vm::{Value, VmError}; use super::super::VmResult; @@ -772,7 +773,7 @@ unsafe extern "C" fn flush_write_handler(r: *mut ngx_http_request_t) { } /// Send response headers. Idempotent after the first successful send. -#[pd_host_function(name = "ngx::send_headers")] +#[pd_host_function(name = "ngx::send_headers", contract = crate::host::catalog::ngx_send_headers_contract)] pub fn send_headers() -> VmResult { with_request(|raw| { with_output(raw, |state| { @@ -786,7 +787,7 @@ pub fn send_headers() -> VmResult { } /// Write response body without a trailing newline. -#[pd_host_function(name = "ngx::print")] +#[pd_host_function(name = "ngx::print", contract = crate::host::catalog::ngx_print_contract)] pub fn print(data: &Value) -> VmResult { let mut payload = Vec::new(); append_output(&mut payload, data, 0)?; @@ -795,7 +796,7 @@ pub fn print(data: &Value) -> VmResult { } /// Write response body and a trailing newline. -#[pd_host_function(name = "ngx::say")] +#[pd_host_function(name = "ngx::say", contract = crate::host::catalog::ngx_say_contract)] pub fn say(data: &Value) -> VmResult { let mut payload = Vec::new(); append_output(&mut payload, data, 0)?; @@ -804,7 +805,7 @@ pub fn say(data: &Value) -> VmResult { } /// Flush buffered output. `wait=true` yields on NGINX write backpressure. -#[pd_host_function(name = "ngx::flush")] +#[pd_host_function(name = "ngx::flush", contract = crate::host::catalog::ngx_flush_contract)] pub async fn flush(wait: bool) -> VmResult<()> { if let Some(pending) = prepare_flush(wait)? { pending.await.map_err(host_err)?; @@ -813,7 +814,7 @@ pub async fn flush(wait: bool) -> VmResult<()> { } /// Send the last buffer and reject further output. -#[pd_host_function(name = "ngx::eof")] +#[pd_host_function(name = "ngx::eof", contract = crate::host::catalog::ngx_eof_contract)] pub fn eof() -> VmResult { with_request(send_eof)?; ret(()) @@ -867,3 +868,20 @@ mod output_stub_tests { "/src/host/output_stub_tests.rs" )); } + + +const OUTPUT_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + send_headers_descriptor, + print_descriptor, + say_descriptor, + flush_descriptor, + eof_descriptor, +]; + +pub fn output_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.output", + functions: OUTPUT_HOST_MODULE_FUNCTIONS, + resources: &[], + } +} diff --git a/src/host/request.rs b/src/host/request.rs index 100e498..78f5a5e 100644 --- a/src/host/request.rs +++ b/src/host/request.rs @@ -13,7 +13,8 @@ use nginx_sys::{ }; use ngx::http::Request; use pd_host_function::pd_host_function; -use vm::{Value, VmError, VmMap}; +use vm::HostModuleDescriptor; +use vm::{HostNamedStruct, HostStructField, HostTypeSchema, Value, VmError, VmMap}; use super::super::VmResult; use super::{CallOutcome, IntoHostCallOutcome}; @@ -45,6 +46,34 @@ struct RequestArgsResult { truncated: bool, } +impl HostNamedStruct for RequestHeadersResult { + const NAME: &'static str = "RequestHeadersResult"; + + fn host_struct_fields() -> Vec { + vec![ + HostStructField::new( + "headers", + HostTypeSchema::Map(Box::new(HostTypeSchema::Unknown)), + ), + HostStructField::new("truncated", HostTypeSchema::Bool), + ] + } +} + +impl HostNamedStruct for RequestArgsResult { + const NAME: &'static str = "RequestArgsResult"; + + fn host_struct_fields() -> Vec { + vec![ + HostStructField::new( + "args", + HostTypeSchema::Map(Box::new(HostTypeSchema::Unknown)), + ), + HostStructField::new("truncated", HostTypeSchema::Bool), + ] + } +} + fn map_envelope_outcome(field: &'static str, values: VmMap, truncated: bool) -> CallOutcome { let mut carrier = VmMap::new(); carrier.insert( @@ -691,7 +720,7 @@ fn copy_raw_header(raw: &ngx_http_request_t, no_request_line: bool) -> VmResult< } /// Current request method name. Custom methods keep the textual token. -#[pd_host_function(name = "ngx::req::get_method")] +#[pd_host_function(name = "ngx::req::get_method", contract = crate::host::catalog::ngx_req_get_method_contract)] pub fn get_method() -> VmResult { with_request(|request| { let raw = request.as_ref(); @@ -701,20 +730,20 @@ pub fn get_method() -> VmResult { } /// HTTP version as 0.9/1.0/1.1/2.0/3.0, or `null` if unrecognized. -#[pd_host_function(name = "ngx::req::http_version")] +#[pd_host_function(name = "ngx::req::http_version", contract = crate::host::catalog::ngx_req_http_version_contract)] pub fn http_version() -> VmResult { with_request(|request| ret(http_version_number(request.as_ref().http_version))) } /// Original request header bytes. `no_request_line` is `null`/`false`/`true`. -#[pd_host_function(name = "ngx::req::raw_header")] +#[pd_host_function(name = "ngx::req::raw_header", contract = crate::host::catalog::ngx_req_raw_header_contract)] pub fn raw_header(no_request_line: &Value) -> VmResult { let skip = optional_bool(no_request_line, "ngx::req::raw_header no_request_line")?.unwrap_or(false); with_request(|request| ret(copy_raw_header(request.as_ref(), skip)?)) } /// Request headers as `{ headers, truncated }`. `raw` keeps original names. -#[pd_host_function(name = "ngx::req::get_headers")] +#[pd_host_function(name = "ngx::req::get_headers", contract = crate::host::catalog::ngx_req_get_headers_contract)] pub fn get_headers(max_headers: &Value, raw: &Value) -> VmResult { let max = clamp_count( max_headers, @@ -730,7 +759,7 @@ pub fn get_headers(max_headers: &Value, raw: &Value) -> VmResult { } /// URI query args as `{ args, truncated }`, matching `ngx::decode_args`. -#[pd_host_function(name = "ngx::req::get_uri_args")] +#[pd_host_function(name = "ngx::req::get_uri_args", contract = crate::host::catalog::ngx_req_get_uri_args_contract)] pub fn get_uri_args(max_args: &Value) -> VmResult { let max = clamp_count( max_args, @@ -746,7 +775,7 @@ pub fn get_uri_args(max_args: &Value) -> VmResult { } /// POST args from a completed request body (memory or bounded temp file). -#[pd_host_function(name = "ngx::req::get_post_args")] +#[pd_host_function(name = "ngx::req::get_post_args", contract = crate::host::catalog::ngx_req_get_post_args_contract)] pub fn get_post_args(max_args: &Value) -> VmResult { let max = clamp_count( max_args, @@ -767,7 +796,7 @@ pub fn get_post_args(max_args: &Value) -> VmResult { } /// True when NGINX marked the request internal. -#[pd_host_function(name = "ngx::req::is_internal")] +#[pd_host_function(name = "ngx::req::is_internal", contract = crate::host::catalog::ngx_req_is_internal_contract)] pub fn is_internal() -> VmResult { with_request(|request| { ret(request_flag( @@ -778,7 +807,7 @@ pub fn is_internal() -> VmResult { } /// Request start time as seconds + milliseconds / 1000. -#[pd_host_function(name = "ngx::req::start_time")] +#[pd_host_function(name = "ngx::req::start_time", contract = crate::host::catalog::ngx_req_start_time_contract)] pub fn start_time() -> VmResult { with_request(|request| { let raw = request.as_ref(); @@ -787,7 +816,7 @@ pub fn start_time() -> VmResult { } /// True when the current request is not the main request. -#[pd_host_function(name = "ngx::is_subrequest")] +#[pd_host_function(name = "ngx::is_subrequest", contract = crate::host::catalog::ngx_is_subrequest_contract)] pub fn is_subrequest() -> VmResult { with_request(|request| { let raw = request.as_ref(); @@ -802,7 +831,7 @@ pub fn is_subrequest() -> VmResult { } /// True when NGINX has already sent response headers. -#[pd_host_function(name = "ngx::headers_sent")] +#[pd_host_function(name = "ngx::headers_sent", contract = crate::host::catalog::ngx_headers_sent_contract)] pub fn headers_sent() -> VmResult { with_request(|request| { ret(request_flag( @@ -1242,7 +1271,7 @@ fn set_or_clear_request_header( } /// Override the current request method using OpenResty method IDs. -#[pd_host_function(name = "ngx::req::set_method")] +#[pd_host_function(name = "ngx::req::set_method", contract = crate::host::catalog::ngx_req_set_method_contract)] pub fn set_method(method_id: i64) -> VmResult { let (name, id) = method_spec(method_id).ok_or_else(|| { VmError::HostError(format!("unsupported method id {method_id}")) @@ -1260,7 +1289,7 @@ pub fn set_method(method_id: i64) -> VmResult { } /// Rewrite the parsed URI. First release rejects `jump=true` before mutation. -#[pd_host_function(name = "ngx::req::set_uri")] +#[pd_host_function(name = "ngx::req::set_uri", contract = crate::host::catalog::ngx_req_set_uri_contract)] pub fn set_uri(uri: String, jump: &Value) -> VmResult { if uri.is_empty() { return Err(VmError::HostError("uri cannot be empty".into())); @@ -1292,7 +1321,7 @@ pub fn set_uri(uri: String, jump: &Value) -> VmResult { } /// Replace the query string from a raw string or map. -#[pd_host_function(name = "ngx::req::set_uri_args")] +#[pd_host_function(name = "ngx::req::set_uri_args", contract = crate::host::catalog::ngx_req_set_uri_args_contract)] pub fn set_uri_args(args: &Value) -> VmResult { let encoded = match args { Value::String(text) => { @@ -1326,7 +1355,7 @@ pub fn set_uri_args(args: &Value) -> VmResult { } /// Set or replace a request header. `null` clears. -#[pd_host_function(name = "ngx::req::set_header")] +#[pd_host_function(name = "ngx::req::set_header", contract = crate::host::catalog::ngx_req_set_header_contract)] pub fn set_header(name: String, value: &Value) -> VmResult { let values = parse_header_values(value)?; with_request_write(|request| { @@ -1336,10 +1365,37 @@ pub fn set_header(name: String, value: &Value) -> VmResult { } /// Clear a request header, including dedicated special-field pointers. -#[pd_host_function(name = "ngx::req::clear_header")] +#[pd_host_function(name = "ngx::req::clear_header", contract = crate::host::catalog::ngx_req_clear_header_contract)] pub fn clear_header(name: String) -> VmResult { with_request_write(|request| { set_or_clear_request_header(request.as_mut(), &name, None)?; ret(()) }) } + + +const REQUEST_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + get_method_descriptor, + http_version_descriptor, + raw_header_descriptor, + get_headers_descriptor, + get_uri_args_descriptor, + get_post_args_descriptor, + is_internal_descriptor, + start_time_descriptor, + is_subrequest_descriptor, + headers_sent_descriptor, + set_method_descriptor, + set_uri_descriptor, + set_uri_args_descriptor, + set_header_descriptor, + clear_header_descriptor, +]; + +pub fn request_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.req", + functions: REQUEST_HOST_MODULE_FUNCTIONS, + resources: &[], + } +} diff --git a/src/host/request_ctx.rs b/src/host/request_ctx.rs index 19d9b2b..87ad786 100644 --- a/src/host/request_ctx.rs +++ b/src/host/request_ctx.rs @@ -14,6 +14,7 @@ use std::collections::{BTreeMap, HashSet}; use std::sync::Arc; use pd_host_function::pd_host_function; +use vm::HostModuleDescriptor; use vm::{Value, VmError, VmMap}; use super::super::VmResult; @@ -393,7 +394,7 @@ fn ret(value: impl IntoHostCallOutcome) -> VmResult { } /// OpenResty-adapted `ngx.ctx[key]` read. Missing keys return `null`. -#[pd_host_function(name = "ngx::ctx::get")] +#[pd_host_function(name = "ngx::ctx::get", contract = crate::host::catalog::ngx_ctx_get_contract)] pub fn get(key: &str) -> VmResult { let identity = current_identity().map_err(scope_err)?; let value = with_current_ctx(identity, |ctx| ctx.get(key)).map_err(scope_err)?; @@ -401,7 +402,7 @@ pub fn get(key: &str) -> VmResult { } /// OpenResty-adapted `ngx.ctx[key] = value`. Returns `null`. -#[pd_host_function(name = "ngx::ctx::set")] +#[pd_host_function(name = "ngx::ctx::set", contract = crate::host::catalog::ngx_ctx_set_contract)] pub fn set(key: &str, value: &Value) -> VmResult { let identity = current_identity().map_err(scope_err)?; with_current_ctx_mut(identity, |ctx| ctx.set(key, value)) @@ -411,7 +412,7 @@ pub fn set(key: &str, value: &Value) -> VmResult { } /// Adapted delete: returns the removed value, or `null` if missing. -#[pd_host_function(name = "ngx::ctx::delete")] +#[pd_host_function(name = "ngx::ctx::delete", contract = crate::host::catalog::ngx_ctx_delete_contract)] pub fn delete(key: &str) -> VmResult { let identity = current_identity().map_err(scope_err)?; let value = with_current_ctx_mut(identity, |ctx| ctx.delete(key)) @@ -421,13 +422,28 @@ pub fn delete(key: &str) -> VmResult { } /// Clear every ctx entry. Returns `null`. -#[pd_host_function(name = "ngx::ctx::clear")] +#[pd_host_function(name = "ngx::ctx::clear", contract = crate::host::catalog::ngx_ctx_clear_contract)] pub fn clear() -> VmResult { let identity = current_identity().map_err(scope_err)?; with_current_ctx_mut(identity, |ctx| ctx.clear()).map_err(scope_err)?; ret(()) } +const REQUEST_CTX_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + get_descriptor, + set_descriptor, + delete_descriptor, + clear_descriptor, +]; + +pub fn request_ctx_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.ctx", + functions: REQUEST_CTX_HOST_MODULE_FUNCTIONS, + resources: &[], + } +} + #[cfg(test)] mod ctx_store_tests { use super::*; diff --git a/src/host/response_meta.rs b/src/host/response_meta.rs index f37546d..e7f4033 100644 --- a/src/host/response_meta.rs +++ b/src/host/response_meta.rs @@ -8,6 +8,7 @@ use nginx_sys::{ }; use ngx::http::Request; use pd_host_function::pd_host_function; +use vm::HostModuleDescriptor; use vm::{Value, VmError, VmMap}; use super::super::VmResult; @@ -566,7 +567,7 @@ fn allowed_redirect_status(status: i64) -> bool { /// Current response status. Prefers `err_status`, then `headers_out.status`, /// then HTTP/0.9 maps to `9`. Unset HTTP/1.x is `0`. -#[pd_host_function(name = "ngx::status::get")] +#[pd_host_function(name = "ngx::status::get", contract = crate::host::catalog::ngx_status_get_contract)] pub fn status_get() -> VmResult { with_response_read(|request| { let status = unsafe { ngx_rustscript_c_resp_status(request.as_ref()) }; @@ -575,7 +576,7 @@ pub fn status_get() -> VmResult { } /// Set `headers_out.status`. Rejected after headers are sent. -#[pd_host_function(name = "ngx::status::set")] +#[pd_host_function(name = "ngx::status::set", contract = crate::host::catalog::ngx_status_set_contract)] pub fn status_set(status: i64) -> VmResult { if !(0..=999).contains(&status) { return Err(VmError::HostError("invalid response status".into())); @@ -589,7 +590,7 @@ pub fn status_set(status: i64) -> VmResult { } /// Read one response header. Missing is `null`; duplicates are an array. -#[pd_host_function(name = "ngx::header::get")] +#[pd_host_function(name = "ngx::header::get", contract = crate::host::catalog::ngx_header_get_contract)] pub fn header_get(name: String) -> VmResult { validate_header_name(&name)?; with_response_read(|request| { @@ -602,7 +603,7 @@ pub fn header_get(name: String) -> VmResult { } /// Set or replace a response header. `null` clears. -#[pd_host_function(name = "ngx::header::set")] +#[pd_host_function(name = "ngx::header::set", contract = crate::host::catalog::ngx_header_set_contract)] pub fn header_set(name: String, value: &Value) -> VmResult { let values = parse_header_values(value)?; with_response_write(|request| { @@ -612,7 +613,7 @@ pub fn header_set(name: String, value: &Value) -> VmResult { } /// Atomically replace every response header from a map. -#[pd_host_function(name = "ngx::header::set_all")] +#[pd_host_function(name = "ngx::header::set_all", contract = crate::host::catalog::ngx_header_set_all_contract)] pub fn header_set_all(headers: &VmMap) -> VmResult { let mut prepared = Vec::new(); for (key, value) in headers.iter() { @@ -668,7 +669,7 @@ pub fn header_set_all(headers: &VmMap) -> VmResult { } /// Clear every response header and dedicated special fields. -#[pd_host_function(name = "ngx::header::clear")] +#[pd_host_function(name = "ngx::header::clear", contract = crate::host::catalog::ngx_header_clear_contract)] pub fn header_clear() -> VmResult { with_response_write(|request| { let raw = request.as_mut(); @@ -680,7 +681,7 @@ pub fn header_clear() -> VmResult { } /// Stop the VM and finalize with `status`. First disposition wins. -#[pd_host_function(name = "ngx::exit")] +#[pd_host_function(name = "ngx::exit", contract = crate::host::catalog::ngx_exit_contract)] pub fn exit(status: i64) -> VmResult { if !allowed_exit_status(status) { return Err(VmError::HostError(format!("invalid exit status {status}"))); @@ -689,7 +690,7 @@ pub fn exit(status: i64) -> VmResult { } /// Write Location and stop the VM with an external redirect. -#[pd_host_function(name = "ngx::redirect")] +#[pd_host_function(name = "ngx::redirect", contract = crate::host::catalog::ngx_redirect_contract)] pub fn redirect(uri: String, status: &Value) -> VmResult { if uri.is_empty() { return Err(VmError::HostError("redirect uri cannot be empty".into())); @@ -734,3 +735,23 @@ pub fn redirect(uri: String, status: &Value) -> VmResult { .map_err(scope_err)??; Err(control_stop_error()) } + + +const RESPONSE_META_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + status_get_descriptor, + status_set_descriptor, + header_get_descriptor, + header_set_descriptor, + header_set_all_descriptor, + header_clear_descriptor, + exit_descriptor, + redirect_descriptor, +]; + +pub fn response_meta_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.resp", + functions: RESPONSE_META_HOST_MODULE_FUNCTIONS, + resources: &[], + } +} diff --git a/src/host/semaphore.rs b/src/host/semaphore.rs index b9741da..70ead2f 100644 --- a/src/host/semaphore.rs +++ b/src/host/semaphore.rs @@ -7,6 +7,7 @@ // future is submitted; no request, pool, VM, or raw pointer crosses Tokio. use pd_host_function::pd_host_function; +use vm::HostModuleDescriptor; use vm::{Value, Vm}; use super::super::VmResult; @@ -39,7 +40,7 @@ fn named(host: &str, error: vm::VmError) -> vm::VmError { /// `count` must be an integer in `0..=MAX_SEMAPHORE_COUNT`. The handle lives in /// the **current** VM resource table; the permit counter and waiter queue live /// in the worker registry. -#[pd_host_function(name = "ngx::semaphore::new")] +#[pd_host_function(name = "ngx::semaphore::new", contract = crate::host::catalog::ngx_semaphore_new_contract)] pub fn new(vm: &mut Vm, count: i64) -> VmResult { let count = semaphore::parse_new_count(count, NGX_SEMAPHORE_NEW_NAME)?; let state = semaphore::create_worker_semaphore(count).map_err(|error| named(NGX_SEMAPHORE_NEW_NAME, error))?; @@ -56,7 +57,7 @@ pub fn new(vm: &mut Vm, count: i64) -> VmResult { /// a timer and `0` performs an immediate acquisition attempt. The generated /// wrapper submits an owned host future; this body only awaits worker-owned /// state and the owned timeout. -#[pd_host_function(name = "ngx::semaphore::wait")] +#[pd_host_function(name = "ngx::semaphore::wait", contract = crate::host::catalog::ngx_semaphore_wait_contract)] pub async fn wait( #[pd_host_context] context: SemaphoreWaitContext, _sem: i64, @@ -66,7 +67,7 @@ pub async fn wait( } /// Adds `count` permits (`null` means one) and wakes FIFO waiters. -#[pd_host_function(name = "ngx::semaphore::post")] +#[pd_host_function(name = "ngx::semaphore::post", contract = crate::host::catalog::ngx_semaphore_post_contract)] pub fn post(vm: &mut Vm, sem: i64, count: Value) -> VmResult { let amount = semaphore::parse_post_amount(&count, NGX_SEMAPHORE_POST_NAME)?; let state = semaphore::resolve_handle(vm, sem, NGX_SEMAPHORE_POST_NAME)?; @@ -77,7 +78,7 @@ pub fn post(vm: &mut Vm, sem: i64, count: Value) -> VmResult { } /// Returns the current count: permits ready minus queued waiters. -#[pd_host_function(name = "ngx::semaphore::count")] +#[pd_host_function(name = "ngx::semaphore::count", contract = crate::host::catalog::ngx_semaphore_count_contract)] pub fn count(vm: &mut Vm, sem: i64) -> VmResult { let state = semaphore::resolve_handle(vm, sem, NGX_SEMAPHORE_COUNT_NAME)?; ret( @@ -86,3 +87,23 @@ pub fn count(vm: &mut Vm, sem: i64) -> VmResult { .map_err(|error| named(NGX_SEMAPHORE_COUNT_NAME, error))?, ) } + + +fn semaphore_resource() -> vm::HostResourceTypeMeta { + vm::HostResourceTypeMeta::of::() +} + +const SEMAPHORE_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + new_descriptor, + wait_descriptor, + post_descriptor, + count_descriptor, +]; + +pub fn semaphore_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.semaphore", + functions: SEMAPHORE_HOST_MODULE_FUNCTIONS, + resources: &[semaphore_resource], + } +} diff --git a/src/host/shared_dict.rs b/src/host/shared_dict.rs index 11bf742..8b0d769 100644 --- a/src/host/shared_dict.rs +++ b/src/host/shared_dict.rs @@ -7,8 +7,9 @@ use std::sync::Arc; use pd_host_function::pd_host_function; +use vm::HostModuleDescriptor; use vm::resource::HostResource; -use vm::{ResourceRef, ResourceTypeKey, Value, Vm, VmError, VmMap}; +use vm::{HostResourceType, ResourceRef, ResourceTypeKey, Value, Vm, VmError, VmMap}; use super::{CallOutcome, IntoHostCallOutcome}; use super::super::VmResult; @@ -25,6 +26,11 @@ impl HostResource for SharedDictHandle { } } +impl HostResourceType for SharedDictHandle { + const KEY: &'static str = "ngx.shared.dict"; + const DESCRIPTION: &'static str = "Shared dictionary zone handle"; +} + fn ret(value: T) -> VmResult { Ok(value.into_host_call_outcome()) } @@ -285,7 +291,7 @@ fn zone_op( } /// Fetch a typed handle for the named `rss_shared_dict` zone. -#[pd_host_function(name = "ngx::shared::dict")] +#[pd_host_function(name = "ngx::shared::dict", contract = crate::host::catalog::ngx_shared_dict_contract)] fn dict(vm: &mut Vm, name: String) -> VmResult { let handle = shared_dict::open_handle(&name).map_err(|err| { if err == "not found" { @@ -302,7 +308,7 @@ fn dict(vm: &mut Vm, name: String) -> VmResult { } /// `value, flags = dict:get(key)` as `{ value, flags, stale, error }`. -#[pd_host_function(name = "ngx::shared::get")] +#[pd_host_function(name = "ngx::shared::get", contract = crate::host::catalog::ngx_shared_get_contract)] fn get(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult { let key = key_bytes(&key)?; let result = zone_op(&dict, |name| shared_dict::dict_get(name, key, false))?; @@ -310,7 +316,7 @@ fn get(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult, key: String) -> VmResult { let key = key_bytes(&key)?; let result = zone_op(&dict, |name| shared_dict::dict_get(name, key, true))?; @@ -348,7 +354,7 @@ fn store_host( } /// `success, err, forcible = dict:set(key, value, exptime?, flags?)`. -#[pd_host_function(name = "ngx::shared::set")] +#[pd_host_function(name = "ngx::shared::set", contract = crate::host::catalog::ngx_shared_set_contract)] fn set( dict: ResourceRef<'_, SharedDictHandle>, key: String, @@ -367,7 +373,7 @@ fn set( } /// `success, err = dict:safe_set(...)` without LRU eviction. -#[pd_host_function(name = "ngx::shared::safe_set")] +#[pd_host_function(name = "ngx::shared::safe_set", contract = crate::host::catalog::ngx_shared_safe_set_contract)] fn safe_set( dict: ResourceRef<'_, SharedDictHandle>, key: String, @@ -386,7 +392,7 @@ fn safe_set( } /// `success, err, forcible = dict:add(...)` if the key is absent. -#[pd_host_function(name = "ngx::shared::add")] +#[pd_host_function(name = "ngx::shared::add", contract = crate::host::catalog::ngx_shared_add_contract)] fn add( dict: ResourceRef<'_, SharedDictHandle>, key: String, @@ -405,7 +411,7 @@ fn add( } /// `success, err = dict:safe_add(...)` without LRU eviction. -#[pd_host_function(name = "ngx::shared::safe_add")] +#[pd_host_function(name = "ngx::shared::safe_add", contract = crate::host::catalog::ngx_shared_safe_add_contract)] fn safe_add( dict: ResourceRef<'_, SharedDictHandle>, key: String, @@ -424,7 +430,7 @@ fn safe_add( } /// `success, err, forcible = dict:replace(...)` if the key exists. -#[pd_host_function(name = "ngx::shared::replace")] +#[pd_host_function(name = "ngx::shared::replace", contract = crate::host::catalog::ngx_shared_replace_contract)] fn replace( dict: ResourceRef<'_, SharedDictHandle>, key: String, @@ -443,7 +449,7 @@ fn replace( } /// Unconditionally removes `key`. -#[pd_host_function(name = "ngx::shared::delete")] +#[pd_host_function(name = "ngx::shared::delete", contract = crate::host::catalog::ngx_shared_delete_contract)] fn delete(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult { let key = key_bytes(&key)?; zone_op(&dict, |name| shared_dict::dict_delete(name, key))?; @@ -451,7 +457,7 @@ fn delete(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult, key: String, @@ -478,7 +484,7 @@ fn incr( } /// Remaining TTL in seconds, or `not found`. -#[pd_host_function(name = "ngx::shared::ttl")] +#[pd_host_function(name = "ngx::shared::ttl", contract = crate::host::catalog::ngx_shared_ttl_contract)] fn ttl(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult { let key = key_bytes(&key)?; let result = zone_op(&dict, |name| shared_dict::dict_ttl(name, key))?; @@ -500,7 +506,7 @@ fn ttl(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult, key: String, @@ -515,14 +521,14 @@ fn expire( } /// Zone capacity in bytes. -#[pd_host_function(name = "ngx::shared::capacity")] +#[pd_host_function(name = "ngx::shared::capacity", contract = crate::host::catalog::ngx_shared_capacity_contract)] fn capacity(dict: ResourceRef<'_, SharedDictHandle>) -> VmResult { let bytes = zone_op(&dict, shared_dict::dict_capacity)?; ret(bytes as i64) } /// Approximate free space in bytes. -#[pd_host_function(name = "ngx::shared::free_space")] +#[pd_host_function(name = "ngx::shared::free_space", contract = crate::host::catalog::ngx_shared_free_space_contract)] fn free_space(dict: ResourceRef<'_, SharedDictHandle>) -> VmResult { let bytes = zone_op(&dict, shared_dict::dict_free_space)?; ret(bytes as i64) @@ -583,7 +589,7 @@ fn list_push_host( } /// `len, err = dict:lpush(key, value)` — prepend a list item. -#[pd_host_function(name = "ngx::shared::lpush")] +#[pd_host_function(name = "ngx::shared::lpush", contract = crate::host::catalog::ngx_shared_lpush_contract)] fn lpush( dict: ResourceRef<'_, SharedDictHandle>, key: String, @@ -593,7 +599,7 @@ fn lpush( } /// `len, err = dict:rpush(key, value)` — append a list item. -#[pd_host_function(name = "ngx::shared::rpush")] +#[pd_host_function(name = "ngx::shared::rpush", contract = crate::host::catalog::ngx_shared_rpush_contract)] fn rpush( dict: ResourceRef<'_, SharedDictHandle>, key: String, @@ -603,7 +609,7 @@ fn rpush( } /// `value, err = dict:lpop(key)`. -#[pd_host_function(name = "ngx::shared::lpop")] +#[pd_host_function(name = "ngx::shared::lpop", contract = crate::host::catalog::ngx_shared_lpop_contract)] fn lpop(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult { let key = key_bytes(&key)?; let result = zone_op(&dict, |name| shared_dict::dict_lpop(name, key))?; @@ -611,7 +617,7 @@ fn lpop(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult, key: String) -> VmResult { let key = key_bytes(&key)?; let result = zone_op(&dict, |name| shared_dict::dict_rpop(name, key))?; @@ -619,7 +625,7 @@ fn rpop(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult, key: String) -> VmResult { let key = key_bytes(&key)?; let result = zone_op(&dict, |name| shared_dict::dict_llen(name, key))?; @@ -627,14 +633,14 @@ fn llen(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult) -> VmResult { zone_op(&dict, shared_dict::dict_flush_all)?; ret(()) } /// Delete up to `max` expired keys. `null`/`0` still obey the project batch cap. -#[pd_host_function(name = "ngx::shared::flush_expired")] +#[pd_host_function(name = "ngx::shared::flush_expired", contract = crate::host::catalog::ngx_shared_flush_expired_contract)] fn flush_expired( dict: ResourceRef<'_, SharedDictHandle>, max: Value, @@ -645,7 +651,7 @@ fn flush_expired( } /// Enumerate keys oldest-first. `null`/`0` still obey `MAX_GET_KEYS`. -#[pd_host_function(name = "ngx::shared::get_keys")] +#[pd_host_function(name = "ngx::shared::get_keys", contract = crate::host::catalog::ngx_shared_get_keys_contract)] fn get_keys(dict: ResourceRef<'_, SharedDictHandle>, max: Value) -> VmResult { let max = optional_max(&max)?; let keys = zone_op(&dict, |name| shared_dict::dict_get_keys(name, max))?; @@ -656,6 +662,43 @@ fn get_keys(dict: ResourceRef<'_, SharedDictHandle>, max: Value) -> VmResult vm::HostResourceTypeMeta { + vm::HostResourceTypeMeta::of::() +} + +const SHARED_DICT_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + dict_descriptor, + get_descriptor, + get_stale_descriptor, + set_descriptor, + safe_set_descriptor, + add_descriptor, + safe_add_descriptor, + replace_descriptor, + delete_descriptor, + incr_descriptor, + ttl_descriptor, + expire_descriptor, + capacity_descriptor, + free_space_descriptor, + lpush_descriptor, + rpush_descriptor, + lpop_descriptor, + rpop_descriptor, + llen_descriptor, + flush_all_descriptor, + flush_expired_descriptor, + get_keys_descriptor, +]; + +pub fn shared_dict_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.shared", + functions: SHARED_DICT_HOST_MODULE_FUNCTIONS, + resources: &[shared_dict_resource], + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/host/socket.rs b/src/host/socket.rs index 036a475..5c986ea 100644 --- a/src/host/socket.rs +++ b/src/host/socket.rs @@ -5,6 +5,7 @@ // surface. Async futures still hold no request, pool, VM, or raw pointer. use pd_host_function::pd_host_function; +use vm::HostModuleDescriptor; use vm::{Value, Vm}; use super::super::VmResult; @@ -26,7 +27,7 @@ fn host_ctx(error: vm::HostContextError) -> vm::VmError { } /// Allocates an unbound UDP cosocket resource handle. -#[pd_host_function(name = "ngx::socket::udp")] +#[pd_host_function(name = "ngx::socket::udp", contract = crate::host::catalog::ngx_socket_udp_contract)] pub fn udp(vm: &mut Vm) -> VmResult { let token = vm .host_context() @@ -36,7 +37,7 @@ pub fn udp(vm: &mut Vm) -> VmResult { } /// Binds the UDP socket to a local address. `host` is a string or `null`. -#[pd_host_function(name = "ngx::socket::udp::bind")] +#[pd_host_function(name = "ngx::socket::udp::bind", contract = crate::host::catalog::ngx_socket_udp_bind_contract)] pub async fn bind( #[pd_host_context] ctx: UdpOpContext, _sock: i64, @@ -47,7 +48,7 @@ pub async fn bind( } /// Sets the default peer, resolving host names through Tokio DNS. -#[pd_host_function(name = "ngx::socket::udp::setpeername")] +#[pd_host_function(name = "ngx::socket::udp::setpeername", contract = crate::host::catalog::ngx_socket_udp_setpeername_contract)] pub async fn setpeername( #[pd_host_context] ctx: UdpOpContext, _sock: i64, @@ -58,7 +59,7 @@ pub async fn setpeername( } /// Sends one datagram to the connected peer. -#[pd_host_function(name = "ngx::socket::udp::send")] +#[pd_host_function(name = "ngx::socket::udp::send", contract = crate::host::catalog::ngx_socket_udp_send_contract)] pub async fn send( #[pd_host_context] ctx: UdpOpContext, _sock: i64, @@ -68,7 +69,7 @@ pub async fn send( } /// Receives one datagram. `size` is an int or `null` (default 8192). -#[pd_host_function(name = "ngx::socket::udp::receive")] +#[pd_host_function(name = "ngx::socket::udp::receive", contract = crate::host::catalog::ngx_socket_udp_receive_contract)] pub async fn receive( #[pd_host_context] ctx: UdpOpContext, _sock: i64, @@ -78,21 +79,21 @@ pub async fn receive( } /// Closes the UDP resource. A second close is an error. -#[pd_host_function(name = "ngx::socket::udp::close")] +#[pd_host_function(name = "ngx::socket::udp::close", contract = crate::host::catalog::ngx_socket_udp_close_contract)] pub fn close(vm: &mut Vm, sock: i64) -> VmResult { udp::close(vm, sock)?; ret(()) } /// Sets the timeout in milliseconds for subsequent UDP operations. -#[pd_host_function(name = "ngx::socket::udp::settimeout")] +#[pd_host_function(name = "ngx::socket::udp::settimeout", contract = crate::host::catalog::ngx_socket_udp_settimeout_contract)] pub fn settimeout(vm: &mut Vm, sock: i64, ms: i64) -> VmResult { udp::settimeout(vm, sock, ms)?; ret(()) } /// Allocates an unconnected TCP cosocket resource handle. -#[pd_host_function(name = "ngx::socket::tcp")] +#[pd_host_function(name = "ngx::socket::tcp", contract = crate::host::catalog::ngx_socket_tcp_contract)] pub fn tcp(vm: &mut Vm) -> VmResult { let token = vm .host_context() @@ -103,7 +104,7 @@ pub fn tcp(vm: &mut Vm) -> VmResult { /// Connects an existing TCP resource. IPv4/IPv6 via Tokio DNS; `unix:` paths /// with port 0 for Unix sockets. -#[pd_host_function(name = "ngx::socket::tcp::connect")] +#[pd_host_function(name = "ngx::socket::tcp::connect", contract = crate::host::catalog::ngx_socket_tcp_connect_contract)] pub async fn tcp_connect( #[pd_host_context] ctx: TcpWriteOpContext, _sock: i64, @@ -114,7 +115,7 @@ pub async fn tcp_connect( } /// Convenience constructor: create, connect, return the TCP resource. -#[pd_host_function(name = "ngx::socket::connect")] +#[pd_host_function(name = "ngx::socket::connect", contract = crate::host::catalog::ngx_socket_connect_contract)] pub async fn connect( #[pd_host_context] ctx: TcpConnectOwnedContext, host: String, @@ -124,7 +125,7 @@ pub async fn connect( } /// Sends the full payload, looping until every byte is written. -#[pd_host_function(name = "ngx::socket::tcp::send")] +#[pd_host_function(name = "ngx::socket::tcp::send", contract = crate::host::catalog::ngx_socket_tcp_send_contract)] pub async fn tcp_send( #[pd_host_context] ctx: TcpWriteOpContext, _sock: i64, @@ -134,7 +135,7 @@ pub async fn tcp_send( } /// Receives by mode: exact size, `null`/`"*l"` line, or `"*a"` until close. -#[pd_host_function(name = "ngx::socket::tcp::receive")] +#[pd_host_function(name = "ngx::socket::tcp::receive", contract = crate::host::catalog::ngx_socket_tcp_receive_contract)] pub async fn tcp_receive( #[pd_host_context] ctx: TcpReadOpContext, _sock: i64, @@ -144,7 +145,7 @@ pub async fn tcp_receive( } /// Receives at most `max` bytes without waiting for a full buffer. -#[pd_host_function(name = "ngx::socket::tcp::receiveany")] +#[pd_host_function(name = "ngx::socket::tcp::receiveany", contract = crate::host::catalog::ngx_socket_tcp_receiveany_contract)] pub async fn tcp_receiveany( #[pd_host_context] ctx: TcpReadOpContext, _sock: i64, @@ -154,7 +155,7 @@ pub async fn tcp_receiveany( } /// Receives until `delimiter`, exclusive of the delimiter, preserving unread. -#[pd_host_function(name = "ngx::socket::tcp::receiveuntil")] +#[pd_host_function(name = "ngx::socket::tcp::receiveuntil", contract = crate::host::catalog::ngx_socket_tcp_receiveuntil_contract)] pub async fn tcp_receiveuntil( #[pd_host_context] ctx: TcpReadOpContext, _sock: i64, @@ -164,21 +165,21 @@ pub async fn tcp_receiveuntil( } /// Closes the TCP resource. A second close is an error. -#[pd_host_function(name = "ngx::socket::tcp::close")] +#[pd_host_function(name = "ngx::socket::tcp::close", contract = crate::host::catalog::ngx_socket_tcp_close_contract)] pub fn tcp_close(vm: &mut Vm, sock: i64) -> VmResult { tcp::close(vm, sock)?; ret(()) } /// Sets the timeout in milliseconds for subsequent TCP operations. -#[pd_host_function(name = "ngx::socket::tcp::settimeout")] +#[pd_host_function(name = "ngx::socket::tcp::settimeout", contract = crate::host::catalog::ngx_socket_tcp_settimeout_contract)] pub fn tcp_settimeout(vm: &mut Vm, sock: i64, ms: i64) -> VmResult { tcp::settimeout(vm, sock, ms)?; ret(()) } /// Puts a connected TCP/TLS socket into the worker-local keepalive pool. -#[pd_host_function(name = "ngx::socket::tcp::setkeepalive")] +#[pd_host_function(name = "ngx::socket::tcp::setkeepalive", contract = crate::host::catalog::ngx_socket_tcp_setkeepalive_contract)] pub fn tcp_setkeepalive( vm: &mut Vm, sock: i64, @@ -190,13 +191,13 @@ pub fn tcp_setkeepalive( } /// Returns how many times this socket was reused from the keepalive pool. -#[pd_host_function(name = "ngx::socket::tcp::getreusedtimes")] +#[pd_host_function(name = "ngx::socket::tcp::getreusedtimes", contract = crate::host::catalog::ngx_socket_tcp_getreusedtimes_contract)] pub fn tcp_getreusedtimes(vm: &mut Vm, sock: i64) -> VmResult { ret(tcp::getreusedtimes(vm, sock)?) } /// Performs a rustls client handshake on a connected TCP socket. -#[pd_host_function(name = "ngx::socket::tcp::sslhandshake")] +#[pd_host_function(name = "ngx::socket::tcp::sslhandshake", contract = crate::host::catalog::ngx_socket_tcp_sslhandshake_contract)] pub async fn tcp_sslhandshake( #[pd_host_context] ctx: TcpHandshakeOpContext, _sock: i64, @@ -209,21 +210,21 @@ pub async fn tcp_sslhandshake( } /// Installs a typed trusted-store resource on a TCP socket. -#[pd_host_function(name = "ngx::socket::tcp::settrustedstore")] +#[pd_host_function(name = "ngx::socket::tcp::settrustedstore", contract = crate::host::catalog::ngx_socket_tcp_settrustedstore_contract)] pub fn tcp_settrustedstore(vm: &mut Vm, sock: i64, store: i64) -> VmResult { tls::settrustedstore(vm, sock, store)?; ret(()) } /// Installs a typed client-cert resource on a TCP socket. -#[pd_host_function(name = "ngx::socket::tcp::setclientcert")] +#[pd_host_function(name = "ngx::socket::tcp::setclientcert", contract = crate::host::catalog::ngx_socket_tcp_setclientcert_contract)] pub fn tcp_setclientcert(vm: &mut Vm, sock: i64, cert: i64) -> VmResult { tls::setclientcert(vm, sock, cert)?; ret(()) } /// Parses PEM into a VM-owned trusted-store resource. -#[pd_host_function(name = "ngx::socket::tcp::trustedstore")] +#[pd_host_function(name = "ngx::socket::tcp::trustedstore", contract = crate::host::catalog::ngx_socket_tcp_trustedstore_contract)] pub fn tcp_trustedstore(vm: &mut Vm, pem: Value) -> VmResult { let store = tls::trustedstore_from_pem(pem)?; let token = vm.host_context().push_resource(store).map_err(host_ctx)?; @@ -231,9 +232,60 @@ pub fn tcp_trustedstore(vm: &mut Vm, pem: Value) -> VmResult { } /// Parses a client certificate and private key into a VM-owned resource. -#[pd_host_function(name = "ngx::socket::tcp::clientcert")] +#[pd_host_function(name = "ngx::socket::tcp::clientcert", contract = crate::host::catalog::ngx_socket_tcp_clientcert_contract)] pub fn tcp_clientcert(vm: &mut Vm, cert: Value, pkey: Value) -> VmResult { let client = tls::clientcert_from_pem(cert, pkey)?; let token = vm.host_context().push_resource(client).map_err(host_ctx)?; ret(token.handle().raw() as i64) } + + +fn tcp_socket_resource() -> vm::HostResourceTypeMeta { + vm::HostResourceTypeMeta::of::() +} + +fn udp_socket_resource() -> vm::HostResourceTypeMeta { + vm::HostResourceTypeMeta::of::() +} + +fn tls_trustedstore_resource() -> vm::HostResourceTypeMeta { + vm::HostResourceTypeMeta::of::() +} + +fn tls_clientcert_resource() -> vm::HostResourceTypeMeta { + vm::HostResourceTypeMeta::of::() +} + +const SOCKET_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + udp_descriptor, + bind_descriptor, + setpeername_descriptor, + send_descriptor, + receive_descriptor, + close_descriptor, + settimeout_descriptor, + tcp_descriptor, + tcp_connect_descriptor, + connect_descriptor, + tcp_send_descriptor, + tcp_receive_descriptor, + tcp_receiveany_descriptor, + tcp_receiveuntil_descriptor, + tcp_close_descriptor, + tcp_settimeout_descriptor, + tcp_setkeepalive_descriptor, + tcp_getreusedtimes_descriptor, + tcp_sslhandshake_descriptor, + tcp_settrustedstore_descriptor, + tcp_setclientcert_descriptor, + tcp_trustedstore_descriptor, + tcp_clientcert_descriptor, +]; + +pub fn socket_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.socket", + functions: SOCKET_HOST_MODULE_FUNCTIONS, + resources: &[tcp_socket_resource, udp_socket_resource, tls_trustedstore_resource, tls_clientcert_resource], + } +} diff --git a/src/host/time.rs b/src/host/time.rs index 0d13e1c..69a4f88 100644 --- a/src/host/time.rs +++ b/src/host/time.rs @@ -23,6 +23,7 @@ use std::cell::Cell; use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use pd_host_function::pd_host_function; +use vm::HostModuleDescriptor; use vm::VmError; use super::{CallOutcome, IntoHostCallOutcome}; @@ -318,60 +319,60 @@ fn parse_year(text: &str) -> Option { } /// Local date `yyyy-mm-dd` from the cached clock. -#[pd_host_function(name = "ngx::today")] +#[pd_host_function(name = "ngx::today", contract = crate::host::catalog::ngx_today_contract)] pub fn today() -> VmResult { let clock = read_clock(); ret(format_civil_date(&broken_down(clock.sec, false)?)) } /// Cached unix seconds as an integer. Distinct from [`now`]. -#[pd_host_function(name = "ngx::time")] +#[pd_host_function(name = "ngx::time", contract = crate::host::catalog::ngx_time_contract)] pub fn time() -> VmResult { ret(read_clock().sec) } /// Cached unix time as seconds plus milliseconds (`float`). -#[pd_host_function(name = "ngx::now")] +#[pd_host_function(name = "ngx::now", contract = crate::host::catalog::ngx_now_contract)] pub fn now() -> VmResult { let clock = read_clock(); ret(clock.sec as f64 + f64::from(clock.msec) / 1000.0) } /// Refreshes the cached clock from the OS. This is a real side effect. -#[pd_host_function(name = "ngx::update_time")] +#[pd_host_function(name = "ngx::update_time", contract = crate::host::catalog::ngx_update_time_contract)] pub fn update_time() -> VmResult { refresh_clock(); ret(()) } /// Local `yyyy-mm-dd hh:mm:ss` from the cached clock. -#[pd_host_function(name = "ngx::localtime")] +#[pd_host_function(name = "ngx::localtime", contract = crate::host::catalog::ngx_localtime_contract)] pub fn localtime() -> VmResult { let clock = read_clock(); ret(format_civil_datetime(&broken_down(clock.sec, false)?)) } /// UTC `yyyy-mm-dd hh:mm:ss` from the cached clock. -#[pd_host_function(name = "ngx::utctime")] +#[pd_host_function(name = "ngx::utctime", contract = crate::host::catalog::ngx_utctime_contract)] pub fn utctime() -> VmResult { let clock = read_clock(); ret(format_civil_datetime(&broken_down(clock.sec, true)?)) } /// Cookie date for `sec` (`Thu, 18-Nov-10 11:27:35 GMT`). -#[pd_host_function(name = "ngx::cookie_time")] +#[pd_host_function(name = "ngx::cookie_time", contract = crate::host::catalog::ngx_cookie_time_contract)] pub fn cookie_time(sec: i64) -> VmResult { ret(http_date(sec, true)?) } /// HTTP date for `sec` (`Thu, 18 Nov 2010 11:27:35 GMT`). -#[pd_host_function(name = "ngx::http_time")] +#[pd_host_function(name = "ngx::http_time", contract = crate::host::catalog::ngx_http_time_contract)] pub fn http_time(sec: i64) -> VmResult { ret(http_date(sec, false)?) } /// Parses an HTTP date into unix seconds, or `null` when invalid. -#[pd_host_function(name = "ngx::parse_http_time")] +#[pd_host_function(name = "ngx::parse_http_time", contract = crate::host::catalog::ngx_parse_http_time_contract)] pub fn parse_http_time(text: &str) -> VmResult { if text.len() > 128 { return Err(VmError::HostError( @@ -380,3 +381,24 @@ pub fn parse_http_time(text: &str) -> VmResult { } ret(parse_http_date(text)) } + + +const TIME_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + today_descriptor, + time_descriptor, + now_descriptor, + update_time_descriptor, + localtime_descriptor, + utctime_descriptor, + cookie_time_descriptor, + http_time_descriptor, + parse_http_time_descriptor, +]; + +pub fn time_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.time", + functions: TIME_HOST_MODULE_FUNCTIONS, + resources: &[], + } +} diff --git a/src/host/timer.rs b/src/host/timer.rs index 1b7226b..d84efc2 100644 --- a/src/host/timer.rs +++ b/src/host/timer.rs @@ -11,15 +11,13 @@ use std::time::Duration; use vm::{ - CallOutcome, CallReturn, HostApiCatalog, HostFunctionRegistry, HostImportSchema, + CallOutcome, CallReturn, HostAdapterDescriptor, HostBindingDescriptor, HostBindingKind, + HostFunctionDescriptor, HostFunctionRegistry, HostModuleDescriptor, HostOwnedAdapterFactory, HostOwnedFunction, OwnedHostCall, OwnedHostContext, TIMER_CALLBACK_ARG, Value, Vm, VmError, VmResult, installed_timer_counts, register_owned_timer, }; -use crate::host::catalog::{ - NGX_TIMER_AT_NAME, NGX_TIMER_EVERY_NAME, NGX_TIMER_PENDING_COUNT_NAME, - NGX_TIMER_RUNNING_COUNT_NAME, -}; +use crate::host::catalog::{NGX_TIMER_AT_NAME, NGX_TIMER_EVERY_NAME}; /// One `ngx::timer::{at,every}` registration adapter. struct TimerRegister { @@ -119,63 +117,94 @@ pub(crate) fn running_count(vm: &mut Vm, _args: &[Value]) -> VmResult Result<(), String> { - let mut schemas: Vec<(&'static str, HostImportSchema)> = Vec::new(); - for name in [ - NGX_TIMER_AT_NAME, - NGX_TIMER_EVERY_NAME, - NGX_TIMER_PENDING_COUNT_NAME, - NGX_TIMER_RUNNING_COUNT_NAME, - ] { - let found = vm::catalog_import_schemas(catalog, name); - if found.is_empty() { - return Err(format!("{name} is missing from the compile catalog")); - } - for schema in found { - schemas.push((name, schema)); - } - } - registry - .transactionally(|staged| { - for (name, schema) in schemas { - match name { - NGX_TIMER_AT_NAME | NGX_TIMER_EVERY_NAME => { - let repeating = name == NGX_TIMER_EVERY_NAME; - staged.register_exact_owned( - name, - 2, - schema, - move |context: OwnedHostContext<'_>| -> Box { - Box::new(TimerRegister { - registry: context.registry().clone(), - name, - repeating, - }) - }, - )?; - } - NGX_TIMER_PENDING_COUNT_NAME => { - staged.register_exact_static(name, 0, schema, pending_count)?; - } - NGX_TIMER_RUNNING_COUNT_NAME => { - staged.register_exact_static(name, 0, schema, running_count)?; - } - _ => { - return Err(VmError::HostError(format!("unexpected timer host {name}"))); - } - } - } - Ok(()) +struct TimerRegisterFactory { + name: &'static str, + repeating: bool, +} + +impl HostOwnedAdapterFactory for TimerRegisterFactory { + fn create(&self, context: OwnedHostContext<'_>) -> Box { + Box::new(TimerRegister { + registry: context.registry().clone(), + name: self.name, + repeating: self.repeating, }) - .map_err(|error| error.to_string()) + } +} + +static TIMER_AT_FACTORY: TimerRegisterFactory = TimerRegisterFactory { + name: NGX_TIMER_AT_NAME, + repeating: false, +}; +static TIMER_EVERY_FACTORY: TimerRegisterFactory = TimerRegisterFactory { + name: NGX_TIMER_EVERY_NAME, + repeating: true, +}; + +fn timer_at_descriptor() -> HostFunctionDescriptor { + let schema = crate::host::catalog::ngx_timer_at_contract(); + HostFunctionDescriptor { + schema: schema.clone(), + binding: HostBindingDescriptor { + kind: HostBindingKind::Owned, + }, + effects: Vec::new(), + adapter: HostAdapterDescriptor::Owned(&TIMER_AT_FACTORY), + resource_types: Vec::new(), + } +} + +fn timer_every_descriptor() -> HostFunctionDescriptor { + let schema = crate::host::catalog::ngx_timer_every_contract(); + HostFunctionDescriptor { + schema: schema.clone(), + binding: HostBindingDescriptor { + kind: HostBindingKind::Owned, + }, + effects: Vec::new(), + adapter: HostAdapterDescriptor::Owned(&TIMER_EVERY_FACTORY), + resource_types: Vec::new(), + } +} + +fn timer_pending_count_descriptor() -> HostFunctionDescriptor { + let schema = crate::host::catalog::ngx_timer_pending_count_contract(); + HostFunctionDescriptor { + schema: schema.clone(), + binding: HostBindingDescriptor { + kind: HostBindingKind::Static, + }, + effects: Vec::new(), + adapter: HostAdapterDescriptor::Static(pending_count), + resource_types: Vec::new(), + } +} + +fn timer_running_count_descriptor() -> HostFunctionDescriptor { + let schema = crate::host::catalog::ngx_timer_running_count_contract(); + HostFunctionDescriptor { + schema: schema.clone(), + binding: HostBindingDescriptor { + kind: HostBindingKind::Static, + }, + effects: Vec::new(), + adapter: HostAdapterDescriptor::Static(running_count), + resource_types: Vec::new(), + } +} + +const TIMER_HOST_MODULE_FUNCTIONS: &[fn() -> HostFunctionDescriptor] = &[ + timer_at_descriptor, + timer_every_descriptor, + timer_pending_count_descriptor, + timer_running_count_descriptor, +]; + +/// Explicit `ngx::timer` module: owned `at`/`every` plus static counters. +pub fn timer_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.timer", + functions: TIMER_HOST_MODULE_FUNCTIONS, + resources: &[], + } } diff --git a/src/host/variables.rs b/src/host/variables.rs index 0dbab26..018a2e6 100644 --- a/src/host/variables.rs +++ b/src/host/variables.rs @@ -5,6 +5,7 @@ use nginx_sys::{ }; use ngx::http::Request; use pd_host_function::pd_host_function; +use vm::HostModuleDescriptor; use vm::{Value, VmError}; use super::super::VmResult; @@ -62,7 +63,7 @@ fn hash_strlow(dst: &mut [u8], src: &[u8]) -> ngx_uint_t { } /// Read an NGINX variable. Missing/`not_found` is `null`; empty is `""`. -#[pd_host_function(name = "ngx::var::get")] +#[pd_host_function(name = "ngx::var::get", contract = crate::host::catalog::ngx_var_get_contract)] pub fn get(name: &str) -> VmResult { if name.is_empty() { return Err(VmError::HostError( @@ -114,7 +115,7 @@ pub fn get(name: &str) -> VmResult { } /// Set an NGINX variable. `null` clears (`not_found`); empty is a present empty string. -#[pd_host_function(name = "ngx::var::set")] +#[pd_host_function(name = "ngx::var::set", contract = crate::host::catalog::ngx_var_set_contract)] pub fn set(name: String, value: &Value) -> VmResult { if name.is_empty() { return Err(VmError::HostError( @@ -180,3 +181,17 @@ pub fn set(name: String, value: &Value) -> VmResult { } }) } + + +const VARIABLES_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ + get_descriptor, + set_descriptor, +]; + +pub fn variables_host_module() -> HostModuleDescriptor { + HostModuleDescriptor { + name: "ngx.var", + functions: VARIABLES_HOST_MODULE_FUNCTIONS, + resources: &[], + } +} diff --git a/src/semaphore.rs b/src/semaphore.rs index b9f2517..814f3dd 100644 --- a/src/semaphore.rs +++ b/src/semaphore.rs @@ -49,7 +49,8 @@ use std::time::Duration; use vm::resource::{CloseProgress, HostResource, ResourceResult}; use vm::{ - CaptureAsyncHostContext, ResourceCloseReason, ResourceTypeKey, Value, Vm, VmError, VmResult, + CaptureAsyncHostContext, HostResourceType, ResourceCloseReason, ResourceTypeKey, Value, Vm, + VmError, VmResult, }; /// Resource type key of the VM-local semaphore handle entry. @@ -336,6 +337,11 @@ impl HostResource for SemaphoreHandle { } } +impl HostResourceType for SemaphoreHandle { + const KEY: &'static str = NGX_SEMAPHORE_RESOURCE_KEY; + const DESCRIPTION: &'static str = "VM-local handle to a worker-owned counting semaphore"; +} + struct WorkerSemaphores { next_id: u64, live: HashMap>, diff --git a/src/socket/tcp.rs b/src/socket/tcp.rs index b5470bc..538c2e6 100644 --- a/src/socket/tcp.rs +++ b/src/socket/tcp.rs @@ -287,6 +287,11 @@ impl TcpCosocket { } } +impl vm::HostResourceType for TcpCosocket { + const KEY: &'static str = NGX_SOCKET_TCP_RESOURCE_KEY; + const DESCRIPTION: &'static str = "OpenResty ngx.socket.tcp handle"; +} + impl HostResource for TcpCosocket { fn resource_type_key() -> Option where diff --git a/src/socket/tls.rs b/src/socket/tls.rs index ed96157..f0f14a0 100644 --- a/src/socket/tls.rs +++ b/src/socket/tls.rs @@ -120,6 +120,11 @@ impl ClientCertInner { } } +impl vm::HostResourceType for TrustedStore { + const KEY: &'static str = NGX_SOCKET_TLS_TRUSTEDSTORE_RESOURCE_KEY; + const DESCRIPTION: &'static str = "VM-owned TLS trusted-store (parsed rustls RootCertStore)"; +} + impl HostResource for TrustedStore { fn resource_type_key() -> Option where @@ -141,6 +146,11 @@ impl HostResource for TrustedStore { } } +impl vm::HostResourceType for ClientCert { + const KEY: &'static str = NGX_SOCKET_TLS_CLIENTCERT_RESOURCE_KEY; + const DESCRIPTION: &'static str = "VM-owned TLS client certificate and private key"; +} + impl HostResource for ClientCert { fn resource_type_key() -> Option where diff --git a/src/socket/udp.rs b/src/socket/udp.rs index e6df94e..abceec8 100644 --- a/src/socket/udp.rs +++ b/src/socket/udp.rs @@ -55,6 +55,11 @@ impl UdpCosocket { } } +impl vm::HostResourceType for UdpCosocket { + const KEY: &'static str = NGX_SOCKET_UDP_RESOURCE_KEY; + const DESCRIPTION: &'static str = "OpenResty ngx.socket.udp handle"; +} + impl HostResource for UdpCosocket { fn resource_type_key() -> Option where From 589f29d20559973b8f94e7126c19d4acab71c6ba Mon Sep 17 00:00:00 2001 From: fffonion Date: Thu, 17 Sep 2026 11:13:11 +0800 Subject: [PATCH 3/6] test(host): cover descriptor install, named envelopes, and timer bind cost Add architecture and restricted-install negatives, document retained dynamic Unknown slots, and time only the timer host call so first-bind cost is not mistaken for waiting on the deadline. --- docs/host-descriptors.md | 74 ++++++++++++++ docs/index.md | 1 + tests/host_catalog.rs | 71 ++++++++----- tests/host_descriptor_arch.rs | 168 +++++++++++++++++++++++++++++++ tests/host_descriptor_install.rs | 115 +++++++++++++++++++++ tests/ngx_timer.rs | 7 +- 6 files changed, 410 insertions(+), 26 deletions(-) create mode 100644 docs/host-descriptors.md create mode 100644 tests/host_descriptor_arch.rs create mode 100644 tests/host_descriptor_install.rs diff --git a/docs/host-descriptors.md b/docs/host-descriptors.md new file mode 100644 index 0000000..add2a5c --- /dev/null +++ b/docs/host-descriptors.md @@ -0,0 +1,74 @@ +# ngx host descriptors + +ngx-rustscript host functions are owned by explicit +`HostModuleDescriptor` values returned from `ngx_host_modules()`. Each +production function has one descriptor source for schema, binding, +adapter, guest resource effects, and private state. + +## Authoring + +1. Implement the host with `#[pd_host_function(name = "...", contract = crate::host::catalog::_contract)]`. +2. Keep the guest schema in `src/host/catalog.rs` as a zero-argument + `*_contract()` wrapper around `host_function_contract`. +3. List the generated `*_descriptor` factory on the family module slice + (`codec_host_module`, `timer_host_module`, …). +4. Install through `HostModuleDescriptor::install_from_catalog`. Do not + add parallel `HostApiBuilder::function` / `register_exact_*` loops. + +Timer `at`/`every` use `HostAdapterDescriptor::Owned` so callback +transfer stays on the owned adapter path. `bind_ngx_hosts` remains a +name-binding compatibility path for tests that do not need owned timer +dispatch; it does not install those adapters. + +## Typed resources + +Handle types implement `HostResourceType` plus `HostResource`: + +| Type | Key | +| --- | --- | +| `SharedDictHandle` | `ngx.shared.dict` | +| `SemaphoreHandle` | `ngx.semaphore` | +| `TcpCosocket` | `ngx.socket.tcp` | +| `UdpCosocket` | `ngx.socket.udp` | +| `TrustedStore` | `ngx.socket.tls.trustedstore` | +| `ClientCert` | `ngx.socket.tls.clientcert` | + +Guest resource effects follow descriptor passing modes +(borrow / borrow-mut / take / create). Raw NGINX C ABI integers that +are not VM resources stay out of `Resource`. + +## Named structs + +Fixed envelopes are catalog `HostTypeSchema::Named` and Rust +`HostNamedStruct` types. Runtime values remain maps. + +| Struct | Functions | +| --- | --- | +| `DecodedArgsResult` | `ngx::decode_args` | +| `RequestHeadersResult` | `ngx::req::get_headers` | +| `RequestArgsResult` | `ngx::req::get_uri_args`, `ngx::req::get_post_args` | + +This named-envelope conversion is a catalog fingerprint change versus +the previous `Unknown` returns. Guest scripts that already named those +structs keep compiling; inner `args`/`headers` maps stay dynamic. + +Shared-dictionary multi-returns stay `Unknown` so guest code can bind +`SharedGetResult` and the other generic result structs. That family +is an intentional dynamic exception, not a `Map(Unknown)` catalog type. + +## Intentional dynamic slots + +These positions stay `HostTypeSchema::Unknown` because OpenResty accepts +`null` or several runtime shapes at the same argument: + +- optional trailing arguments (`max_args`, `max_headers`, `raw`, + `no_padding`, `escape_type`, `timeout`, `exptime`, `flags`, `jump`, + `auto_clean`, `init`, `init_ttl`, `count`, `max`, `host`, `data`, + `value` where OpenResty is untyped) +- open dictionaries (`ngx::encode_args` `args`, header-set maps, + request ctx payloads) +- shared-dictionary result carriers (generic `value` / `ok` / `error` + envelopes) + +Do not add new `Unknown` or `Map(Unknown)` returns for a fixed outer +shape. Add a named struct and a catalog test instead. diff --git a/docs/index.md b/docs/index.md index b7af80d..50b2645 100644 --- a/docs/index.md +++ b/docs/index.md @@ -22,6 +22,7 @@ metadata on each directive. The prose is ngx-rustscript's own. | [Security and policy](security.md) | Denied-by-default grants, one HTTP allow pair, SQLite root, fail-closed log | | [Examples](examples/index.md) | Rate limiter, HMAC API-key auth, async pipeline | | [Compatibility](ngx-api-compatibility.md) | OpenResty v0.10.29 mapping, typed results, out of scope | +| [Host descriptors](host-descriptors.md) | Module descriptors, typed resources, named envelopes, retained Unknown slots | | [Scheduler design](design/ngx-scheduler.md) | Callback execution contract: blocked thread/`on_abort`/`run_worker_thread` families, plus the landed semaphore and timer families and their lifecycle | ## Runnable phases diff --git a/tests/host_catalog.rs b/tests/host_catalog.rs index 0df2d9c..c5f63a1 100644 --- a/tests/host_catalog.rs +++ b/tests/host_catalog.rs @@ -462,21 +462,6 @@ fn dynamic_map_envelopes_compile_as_explicit_struct_types() { r#" use ngx; - struct DecodedArgsResult { - args: map, - truncated: bool - } - - struct RequestHeadersResult { - headers: map, - truncated: bool - } - - struct RequestArgsResult { - args: map, - truncated: bool - } - let decoded: DecodedArgsResult = ngx::decode_args("a=1", null); if !decoded.truncated { ngx::encode_args(decoded.args); @@ -500,17 +485,24 @@ fn dynamic_map_envelopes_compile_as_explicit_struct_types() { #[test] fn dynamic_map_envelopes_are_not_declared_as_maps() { let catalog = ngx_host_catalog(); - for name in [ - "ngx::decode_args", - "ngx::req::get_headers", - "ngx::req::get_uri_args", - "ngx::req::get_post_args", + for (name, expected) in [ + ("ngx::decode_args", "DecodedArgsResult"), + ("ngx::req::get_headers", "RequestHeadersResult"), + ("ngx::req::get_uri_args", "RequestArgsResult"), + ("ngx::req::get_post_args", "RequestArgsResult"), ] { - assert_eq!( - catalog.function(name).expect("host schema").return_type, - HostTypeSchema::Unknown, - "{name} has a fixed outer result shape" - ); + match &catalog.function(name).expect("host schema").return_type { + HostTypeSchema::Named { + name: struct_name, + fields, + } => { + assert_eq!(struct_name, expected, "{name} named envelope"); + assert_eq!(fields.len(), 2, "{name} envelope field count"); + assert_eq!(fields[1].name, "truncated"); + assert_eq!(fields[1].ty, HostTypeSchema::Bool); + } + other => panic!("{name} must return a named envelope, got {other:?}"), + } } } @@ -1077,3 +1069,32 @@ fn nginx_version_allows_init_worker_and_exit_worker_unlike_nginx_configure() { NgxPhase::Init )); } + +#[test] +fn intentional_dynamic_unknown_slots_are_allowlisted() { + let catalog = ngx_host_catalog(); + let unknown_returns = [ + "ngx::shared::get", + "ngx::shared::get_stale", + "ngx::shared::set", + "ngx::shared::incr", + "ngx::ctx::get", + "ngx::encode_args", + ]; + for name in unknown_returns { + let schema = catalog.function(name).expect(name); + if name == "ngx::encode_args" { + assert_eq!( + schema.params[0].ty, + HostTypeSchema::Unknown, + "{name} args stay an open dictionary" + ); + continue; + } + assert_eq!( + schema.return_type, + HostTypeSchema::Unknown, + "{name} stays Unknown so guest generic/result structs keep compiling" + ); + } +} diff --git a/tests/host_descriptor_arch.rs b/tests/host_descriptor_arch.rs new file mode 100644 index 0000000..59601a6 --- /dev/null +++ b/tests/host_descriptor_arch.rs @@ -0,0 +1,168 @@ +//! Architecture guard: ngx hosts are owned by explicit module descriptors. +//! +//! Catalog schemas, exact adapters, resource declarations, and named structs +//! must come from those descriptors. Hand-written parallel `schema_for` / +//! `builder.function` / `register_exact_*` loops for migrated functions are +//! forbidden. + +use std::fs; +use std::path::{Path, PathBuf}; + +fn repo_root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) +} + +fn read(path: impl AsRef) -> String { + fs::read_to_string(repo_root().join(path)).expect("source file") +} + +fn rust_sources_under(dir: &str) -> Vec<(String, String)> { + let mut files = Vec::new(); + let root = repo_root().join(dir); + for entry in fs::read_dir(&root).unwrap_or_else(|_| panic!("read {dir}")) { + let entry = entry.expect("dirent"); + let path = entry.path(); + if path.extension().and_then(|ext| ext.to_str()) == Some("rs") { + let rel = path + .strip_prefix(repo_root()) + .expect("relative") + .to_string_lossy() + .into_owned(); + files.push((rel, fs::read_to_string(path).expect("read rust"))); + } + } + files.sort_by(|a, b| a.0.cmp(&b.0)); + files +} + +fn pd_host_function_names(source: &str) -> Vec { + let mut names = Vec::new(); + for line in source.lines() { + let trimmed = line.trim(); + if let Some(rest) = trimmed.strip_prefix("#[pd_host_function(name = \"") { + if let Some(end) = rest.find('"') { + names.push(rest[..end].to_string()); + } + } + } + names +} + +#[test] +fn production_host_functions_are_owned_by_explicit_module_descriptors() { + let host_mod = read("src/host/mod.rs"); + assert!( + host_mod.contains("HostModuleDescriptor"), + "src/host/mod.rs must compose explicit HostModuleDescriptor values" + ); + assert!( + host_mod.contains("ngx_host_modules"), + "src/host/mod.rs must expose ngx_host_modules() as the deterministic composition list" + ); + + let mut production = Vec::new(); + for (path, source) in rust_sources_under("src/host") { + if path.ends_with("output_stub_tests.rs") { + continue; + } + for name in pd_host_function_names(&source) { + if name == "ngx::probe" || name == "ngx::ready" || name == "ngx::fail" { + continue; + } + production.push((path.clone(), name)); + } + } + assert!( + production.len() >= 130, + "expected the production ngx host surface, found {}", + production.len() + ); + + let composed = format!("{}{}", host_mod, read("src/host/timer.rs")); + for (path, name) in &production { + let _ = path; + assert!( + composed.contains(&format!("name = \"{name}\"")) + || composed.contains(&format!("\"{name}\"")) + || rust_sources_under("src/host") + .iter() + .any(|(_, src)| src.contains(name) && src.contains("_descriptor")), + "{name} must be listed in an explicit module descriptor" + ); + } +} + +#[test] +fn catalog_is_not_built_from_a_parallel_schema_for_table() { + let catalog = read("src/host/catalog.rs"); + assert!( + !catalog.contains("fn schema_for("), + "src/host/catalog.rs must not keep a parallel schema_for table" + ); + assert!( + !catalog.contains("builder.function("), + "ngx catalog must not loop HostApiBuilder::function for migrated hosts" + ); + assert!( + !catalog.contains("builder.resource("), + "resource declarations must come from HostResourceType / module resources, not builder.resource" + ); +} + +#[test] +fn registry_installs_descriptors_transactionally_instead_of_binding_loops() { + let host_mod = read("src/host/mod.rs"); + assert!( + host_mod.contains("install_from_catalog") || host_mod.contains(".install("), + "ngx_host_registry must install module descriptors transactionally" + ); + assert!( + !host_mod.contains("register_catalog_static("), + "migrated hosts must not register through a parallel register_catalog_static loop" + ); + assert!( + !host_mod.contains("register_catalog_static_args("), + "migrated hosts must not register through a parallel register_catalog_static_args loop" + ); +} + +#[test] +fn timer_owned_adapters_come_from_descriptors() { + let timer = read("src/host/timer.rs"); + assert!( + timer.contains("HostOwnedAdapterFactory") || timer.contains("HostAdapterDescriptor::Owned"), + "timer at/every must select the owned descriptor adapter path" + ); + assert!( + timer.contains("HostFunctionDescriptor") || timer.contains("_descriptor"), + "timer hosts must have descriptor sources" + ); + assert!( + !timer.contains("register_exact_owned("), + "timer must not keep a parallel register_exact_owned loop" + ); +} + +#[test] +fn handle_types_declare_host_resource_type() { + let mut impls = 0usize; + for (path, source) in [ + ("src/host/shared_dict.rs", read("src/host/shared_dict.rs")), + ("src/semaphore.rs", read("src/semaphore.rs")), + ("src/socket/tcp.rs", read("src/socket/tcp.rs")), + ("src/socket/udp.rs", read("src/socket/udp.rs")), + ("src/socket/tls.rs", read("src/socket/tls.rs")), + ] { + assert!( + source.contains("impl HostResourceType for") + || source.contains("impl vm::HostResourceType for"), + "{path} must implement HostResourceType" + ); + impls += source.matches("impl HostResourceType for").count() + + source.matches("impl vm::HostResourceType for").count(); + } + assert!( + impls >= 6, + "expected HostResourceType for dict/semaphore/tcp/udp/tls store/cert, found {impls}" + ); +} diff --git a/tests/host_descriptor_install.rs b/tests/host_descriptor_install.rs new file mode 100644 index 0000000..59b0737 --- /dev/null +++ b/tests/host_descriptor_install.rs @@ -0,0 +1,115 @@ +//! Restricted exact-install negatives for ngx module descriptors. +//! +//! Production uses [`ngx_rustscript::host::ngx_host_registry`] (empty registry +//! plus transactional `install_from_catalog`). These tests prove deny, +//! mismatch, and rollback stay fail-closed. + +use ngx_rustscript_nginx_test_stubs as _; + +use ngx_rustscript::config::compile_rss_source; +use ngx_rustscript::host::catalog::ngx_host_catalog; +use ngx_rustscript::host::{ngx_host_modules, ngx_host_registry}; +use vm::{ + HostApiBuilder, HostBindingDescriptor, HostBindingKind, HostFunctionDescriptor, + HostFunctionRegistry, HostFunctionSchema, HostParamSchema, HostTypeSchema, Vm, +}; + +fn codec_module() -> vm::HostModuleDescriptor { + ngx_host_modules() + .into_iter() + .find(|module| module.name == "ngx.codec") + .expect("ngx.codec module") +} + +#[test] +fn production_registry_installs_every_catalogued_name() { + let catalog = ngx_host_catalog(); + let registry = ngx_host_registry().expect("ngx host registry"); + for schema in catalog.functions() { + assert!( + registry.contains_name(&schema.name), + "installed registry must contain {}", + schema.name + ); + } +} + +#[test] +fn restricted_registry_denies_undeclared_ngx_admission() { + let program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", null);"#) + .expect("ngx::escape_uri should compile against the catalog"); + let mut vm = Vm::new_shared(program); + let error = HostFunctionRegistry::restricted() + .bind_vm_cached(&mut vm) + .expect_err("restricted registry must deny undeclared ngx hosts"); + let message = error.to_string(); + assert!( + message.contains("unbound") + || message.contains("capability") + || message.contains("restricted") + || message.contains("not allow") + || message.contains("undeclared") + || message.contains("denied"), + "restricted deny message: {message}" + ); +} + +#[test] +fn skewed_caller_catalog_rolls_back_without_partial_install() { + let module = codec_module(); + let mut skewed = HostApiBuilder::new(); + skewed.function(HostFunctionSchema::with_return( + "ngx::escape_uri", + vec![HostParamSchema::value("uri", HostTypeSchema::Int)], + HostTypeSchema::String, + )); + let skewed = skewed.build().expect("skewed catalog validates"); + let mut registry = HostFunctionRegistry::empty(); + let error = module + .install_from_catalog(&mut registry, &skewed) + .expect_err("a catalog that disagrees with descriptors must fail"); + assert!( + !registry.contains_name("ngx::escape_uri"), + "rejected install must leave the registry unchanged: {error}" + ); +} + +#[test] +fn binding_adapter_mismatch_fails_closed() { + let mut descriptors: Vec = codec_module().descriptors(); + let first = descriptors + .first_mut() + .expect("ngx.codec has at least one descriptor"); + first.binding = HostBindingDescriptor { + kind: HostBindingKind::Owned, + }; + let mut registry = HostFunctionRegistry::empty(); + let error = vm::HostModuleDescriptor::install_descriptors(&mut registry, &descriptors) + .expect_err("binding/adapter mismatch must fail closed"); + assert!( + error.to_string().contains("mismatch") || error.to_string().contains("binding"), + "{error}" + ); + assert!( + !registry.contains_name("ngx::escape_uri"), + "mismatch must not install a partial codec module" + ); +} + +#[test] +fn duplicate_module_install_does_not_leak_a_second_declaration() { + let catalog = ngx_host_catalog(); + let module = codec_module(); + let mut registry = HostFunctionRegistry::empty(); + module + .install_from_catalog(&mut registry, &catalog) + .expect("first codec install"); + let error = module + .install_from_catalog(&mut registry, &catalog) + .expect_err("duplicate declaration must fail"); + let message = error.to_string(); + assert!( + message.contains("duplicate") || message.contains("already"), + "duplicate install message: {message}" + ); +} diff --git a/tests/ngx_timer.rs b/tests/ngx_timer.rs index ea907b2..7de0ab2 100644 --- a/tests/ngx_timer.rs +++ b/tests/ngx_timer.rs @@ -196,8 +196,13 @@ fn at_registers_one_fake_request_with_an_ordinary_identity() { let mut worker = WorkerRuntime::start().expect("worker runtime"); assert_eq!(live_timer_count(), 0); + // Catalog/registry install is paid during VM construction. Time only the + // host call so a slow first bind cannot be mistaken for waiting on the + // 10s deadline. + let mut vm = timer_vm("use ngx;\nngx::timer::at(10, |premature| null);\n", &worker); let start = Instant::now(); - run_script("use ngx;\nngx::timer::at(10, |premature| null);\n", &worker) + vm.run() + .map(|_| ()) .expect("registration returns immediately"); assert!( start.elapsed() < Duration::from_secs(2), From a26099a53736a7adfcffbf2a42ded3c752ae448b Mon Sep 17 00:00:00 2001 From: fffonion Date: Thu, 17 Sep 2026 16:22:43 +0800 Subject: [PATCH 4/6] feat(host): bind production VMs only through the descriptor registry Make ngx_host_registry the single install path for request, pool, and timer VMs. bind_ngx_hosts is a thin wrapper; standard policy no longer rebinds names after the exact catalog plan. --- src/async_bridge.rs | 31 +- src/host/mod.rs | 1212 ++++++------------------------------------ src/host/standard.rs | 30 +- src/request_state.rs | 20 +- src/runtime.rs | 8 + src/timer.rs | 56 +- src/vm_pool.rs | 2 +- 7 files changed, 211 insertions(+), 1148 deletions(-) diff --git a/src/async_bridge.rs b/src/async_bridge.rs index 537259c..823970e 100644 --- a/src/async_bridge.rs +++ b/src/async_bridge.rs @@ -131,36 +131,7 @@ mod tests { } fn test_catalog() -> vm::HostApiCatalog { - let mut builder = vm::HostApiBuilder::new(); - builder.function(vm::HostFunctionSchema::with_return( - "ngx::sleep", - vec![vm::HostParamSchema::value( - "seconds", - vm::HostTypeSchema::Number, - )], - vm::HostTypeSchema::Null, - )); - builder.function(vm::HostFunctionSchema::with_return( - "ngx::sleep_ms", - vec![vm::HostParamSchema::value("ms", vm::HostTypeSchema::Int)], - vm::HostTypeSchema::Null, - )); - builder.function(vm::HostFunctionSchema::with_return( - "ngx::probe", - vec![vm::HostParamSchema::value("ms", vm::HostTypeSchema::Int)], - vm::HostTypeSchema::Int, - )); - builder.function(vm::HostFunctionSchema::with_return( - "ngx::ready", - vec![], - vm::HostTypeSchema::Int, - )); - builder.function(vm::HostFunctionSchema::with_return( - "ngx::fail", - vec![], - vm::HostTypeSchema::Int, - )); - builder.build().expect("test catalog") + crate::host::test_bind_catalog() } fn bind_vm(program: vm::Program, handle: Handle, test_hosts: bool) -> Vm { diff --git a/src/host/mod.rs b/src/host/mod.rs index f39cd55..e0d6d02 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -19,25 +19,27 @@ pub use catalog::{ NGX_DECODE_BASE64_NAME, NGX_DECODE_BASE64MIME_NAME, NGX_ENCODE_ARGS_NAME, NGX_ENCODE_BASE64_NAME, NGX_EOF_NAME, NGX_ESCAPE_URI_NAME, NGX_EXIT_NAME, NGX_FLUSH_NAME, NGX_HEADER_CLEAR_NAME, NGX_HEADER_GET_NAME, NGX_HEADER_SET_ALL_NAME, NGX_HEADER_SET_NAME, - NGX_HEADERS_SENT_NAME, NGX_HMAC_SHA1_NAME, NGX_HTTP_TIME_NAME, NGX_IS_SUBREQUEST_NAME, - NGX_LOCALTIME_NAME, NGX_MD5_BIN_NAME, NGX_MD5_NAME, NGX_NOW_NAME, NGX_PARSE_HTTP_TIME_NAME, - NGX_PRINT_NAME, NGX_QUOTE_SQL_STR_NAME, NGX_REDIRECT_NAME, NGX_REQ_APPEND_BODY_NAME, - NGX_REQ_CLEAR_HEADER_NAME, NGX_REQ_DISCARD_BODY_NAME, NGX_REQ_FINISH_BODY_NAME, - NGX_REQ_GET_BODY_DATA_NAME, NGX_REQ_GET_BODY_FILE_NAME, NGX_REQ_GET_HEADERS_NAME, - NGX_REQ_GET_METHOD_NAME, NGX_REQ_GET_POST_ARGS_NAME, NGX_REQ_GET_URI_ARGS_NAME, - NGX_REQ_HTTP_VERSION_NAME, NGX_REQ_INIT_BODY_NAME, NGX_REQ_IS_INTERNAL_NAME, - NGX_REQ_RAW_HEADER_NAME, NGX_REQ_READ_BODY_NAME, NGX_REQ_SET_BODY_DATA_NAME, - NGX_REQ_SET_BODY_FILE_NAME, NGX_REQ_SET_HEADER_NAME, NGX_REQ_SET_METHOD_NAME, - NGX_REQ_SET_URI_ARGS_NAME, NGX_REQ_SET_URI_NAME, NGX_REQ_START_TIME_NAME, NGX_SAY_NAME, - NGX_SEMAPHORE_COUNT_NAME, NGX_SEMAPHORE_NEW_NAME, NGX_SEMAPHORE_POST_NAME, - NGX_SEMAPHORE_RESOURCE_KEY, NGX_SEMAPHORE_WAIT_NAME, NGX_SEND_HEADERS_NAME, NGX_SHA1_BIN_NAME, - NGX_SHARED_ADD_NAME, NGX_SHARED_CAPACITY_NAME, NGX_SHARED_DELETE_NAME, NGX_SHARED_DICT_NAME, - NGX_SHARED_EXPIRE_NAME, NGX_SHARED_FLUSH_ALL_NAME, NGX_SHARED_FLUSH_EXPIRED_NAME, - NGX_SHARED_FREE_SPACE_NAME, NGX_SHARED_GET_KEYS_NAME, NGX_SHARED_GET_NAME, - NGX_SHARED_GET_STALE_NAME, NGX_SHARED_INCR_NAME, NGX_SHARED_LLEN_NAME, NGX_SHARED_LPOP_NAME, - NGX_SHARED_LPUSH_NAME, NGX_SHARED_REPLACE_NAME, NGX_SHARED_RPOP_NAME, NGX_SHARED_RPUSH_NAME, - NGX_SHARED_SAFE_ADD_NAME, NGX_SHARED_SAFE_SET_NAME, NGX_SHARED_SET_NAME, NGX_SHARED_TTL_NAME, - NGX_SLEEP_MS_NAME, NGX_SLEEP_NAME, NGX_SOCKET_CONNECT_NAME, NGX_SOCKET_TCP_CLIENTCERT_NAME, + NGX_HEADERS_SENT_NAME, NGX_HMAC_SHA1_NAME, NGX_HOST_CATALOG_FINGERPRINT, + NGX_HOST_CATALOG_FINGERPRINT_V1, NGX_HOST_CATALOG_SCHEMA_VERSION, NGX_HTTP_TIME_NAME, + NGX_IS_SUBREQUEST_NAME, NGX_LOCALTIME_NAME, NGX_MD5_BIN_NAME, NGX_MD5_NAME, NGX_NOW_NAME, + NGX_PARSE_HTTP_TIME_NAME, NGX_PRINT_NAME, NGX_QUOTE_SQL_STR_NAME, NGX_REDIRECT_NAME, + NGX_REQ_APPEND_BODY_NAME, NGX_REQ_CLEAR_HEADER_NAME, NGX_REQ_DISCARD_BODY_NAME, + NGX_REQ_FINISH_BODY_NAME, NGX_REQ_GET_BODY_DATA_NAME, NGX_REQ_GET_BODY_FILE_NAME, + NGX_REQ_GET_HEADERS_NAME, NGX_REQ_GET_METHOD_NAME, NGX_REQ_GET_POST_ARGS_NAME, + NGX_REQ_GET_URI_ARGS_NAME, NGX_REQ_HTTP_VERSION_NAME, NGX_REQ_INIT_BODY_NAME, + NGX_REQ_IS_INTERNAL_NAME, NGX_REQ_RAW_HEADER_NAME, NGX_REQ_READ_BODY_NAME, + NGX_REQ_SET_BODY_DATA_NAME, NGX_REQ_SET_BODY_FILE_NAME, NGX_REQ_SET_HEADER_NAME, + NGX_REQ_SET_METHOD_NAME, NGX_REQ_SET_URI_ARGS_NAME, NGX_REQ_SET_URI_NAME, + NGX_REQ_START_TIME_NAME, NGX_SAY_NAME, NGX_SEMAPHORE_COUNT_NAME, NGX_SEMAPHORE_NEW_NAME, + NGX_SEMAPHORE_POST_NAME, NGX_SEMAPHORE_RESOURCE_KEY, NGX_SEMAPHORE_WAIT_NAME, + NGX_SEND_HEADERS_NAME, NGX_SHA1_BIN_NAME, NGX_SHARED_ADD_NAME, NGX_SHARED_CAPACITY_NAME, + NGX_SHARED_DELETE_NAME, NGX_SHARED_DICT_NAME, NGX_SHARED_EXPIRE_NAME, + NGX_SHARED_FLUSH_ALL_NAME, NGX_SHARED_FLUSH_EXPIRED_NAME, NGX_SHARED_FREE_SPACE_NAME, + NGX_SHARED_GET_KEYS_NAME, NGX_SHARED_GET_NAME, NGX_SHARED_GET_STALE_NAME, NGX_SHARED_INCR_NAME, + NGX_SHARED_LLEN_NAME, NGX_SHARED_LPOP_NAME, NGX_SHARED_LPUSH_NAME, NGX_SHARED_REPLACE_NAME, + NGX_SHARED_RPOP_NAME, NGX_SHARED_RPUSH_NAME, NGX_SHARED_SAFE_ADD_NAME, + NGX_SHARED_SAFE_SET_NAME, NGX_SHARED_SET_NAME, NGX_SHARED_TTL_NAME, NGX_SLEEP_MS_NAME, + NGX_SLEEP_NAME, NGX_SOCKET_CONNECT_NAME, NGX_SOCKET_TCP_CLIENTCERT_NAME, NGX_SOCKET_TCP_CLOSE_NAME, NGX_SOCKET_TCP_CONNECT_NAME, NGX_SOCKET_TCP_GETREUSEDTIMES_NAME, NGX_SOCKET_TCP_NAME, NGX_SOCKET_TCP_RECEIVE_NAME, NGX_SOCKET_TCP_RECEIVEANY_NAME, NGX_SOCKET_TCP_RECEIVEUNTIL_NAME, NGX_SOCKET_TCP_RESOURCE_KEY, NGX_SOCKET_TCP_SEND_NAME, @@ -76,7 +78,7 @@ pub use layout::helpers::request_ctx::{ MAX_CTX_CONTAINER_ELEMS, MAX_CTX_DEPTH, MAX_CTX_KEY_BYTES, MAX_CTX_KEYS, MAX_CTX_SCALAR_BYTES, MAX_CTX_TOTAL_BYTES, RequestLocalCtx, }; -pub use standard::{StandardHostPolicy, install_standard_runtime}; +pub use standard::{StandardHostPolicy, install_standard_policy, install_standard_runtime}; /// Override the OS clock sampled by time hosts. Used by tests and by /// `notify_cached_clock_event_boundary` to model nginx's event-loop refresh @@ -146,1021 +148,45 @@ pub(crate) fn is_control_stop(err: &VmError) -> bool { matches!(err, VmError::HostError(message) if message == CONTROL_STOP_MESSAGE) } -/// Names bound by [`bind_ngx_hosts`]. Kept next to the explicit bind list so -/// catalog correspondence can be tested without generating dispatch from -/// catalog metadata. +/// Production ngx host names, in catalog/manifest order. +/// +/// This is the single name list for the descriptor registry. Every entry is +/// installed by [`ngx_host_registry`] through exact catalog adapters. pub fn production_runtime_host_names() -> &'static [&'static str] { - &[ - NGX_SLEEP_NAME, - NGX_SLEEP_MS_NAME, - NGX_SEMAPHORE_NEW_NAME, - NGX_SEMAPHORE_WAIT_NAME, - NGX_SEMAPHORE_POST_NAME, - NGX_SEMAPHORE_COUNT_NAME, - NGX_ESCAPE_URI_NAME, - NGX_UNESCAPE_URI_NAME, - NGX_ENCODE_ARGS_NAME, - NGX_DECODE_ARGS_NAME, - NGX_ENCODE_BASE64_NAME, - NGX_DECODE_BASE64_NAME, - NGX_DECODE_BASE64MIME_NAME, - NGX_CRC32_SHORT_NAME, - NGX_CRC32_LONG_NAME, - NGX_HMAC_SHA1_NAME, - NGX_MD5_NAME, - NGX_MD5_BIN_NAME, - NGX_SHA1_BIN_NAME, - NGX_QUOTE_SQL_STR_NAME, - NGX_TODAY_NAME, - NGX_TIME_NAME, - NGX_NOW_NAME, - NGX_UPDATE_TIME_NAME, - NGX_LOCALTIME_NAME, - NGX_UTCTIME_NAME, - NGX_COOKIE_TIME_NAME, - NGX_HTTP_TIME_NAME, - NGX_PARSE_HTTP_TIME_NAME, - "ngx::get_phase", - "ngx::config::subsystem", - "ngx::config::debug", - "ngx::config::prefix", - "ngx::config::nginx_version", - "ngx::config::nginx_configure", - "ngx::config::ngx_rustscript_version", - "ngx::worker::exiting", - "ngx::worker::pid", - "ngx::worker::pids", - "ngx::worker::count", - "ngx::worker::id", - "ngx::log", - "ngx::core::ok", - "ngx::core::error", - "ngx::core::again", - "ngx::core::done", - "ngx::core::declined", - "ngx::core::null_value", - "ngx::http::get", - "ngx::http::head", - "ngx::http::put", - "ngx::http::post", - "ngx::http::delete", - "ngx::http::options", - "ngx::http::mkcol", - "ngx::http::copy", - "ngx::http::move", - "ngx::http::propfind", - "ngx::http::proppatch", - "ngx::http::lock", - "ngx::http::unlock", - "ngx::http::patch", - "ngx::http::trace", - "ngx::http::status_continue", - "ngx::http::status_switching_protocols", - "ngx::http::status_ok", - "ngx::http::status_created", - "ngx::http::status_accepted", - "ngx::http::status_no_content", - "ngx::http::status_partial_content", - "ngx::http::status_special_response", - "ngx::http::status_moved_permanently", - "ngx::http::status_moved_temporarily", - "ngx::http::status_see_other", - "ngx::http::status_not_modified", - "ngx::http::status_temporary_redirect", - "ngx::http::status_permanent_redirect", - "ngx::http::status_bad_request", - "ngx::http::status_unauthorized", - "ngx::http::status_payment_required", - "ngx::http::status_forbidden", - "ngx::http::status_not_found", - "ngx::http::status_not_allowed", - "ngx::http::status_not_acceptable", - "ngx::http::status_request_timeout", - "ngx::http::status_conflict", - "ngx::http::status_gone", - "ngx::http::status_upgrade_required", - "ngx::http::status_too_many_requests", - "ngx::http::status_illegal", - "ngx::http::status_close", - "ngx::http::status_internal_server_error", - "ngx::http::status_not_implemented", - "ngx::http::status_bad_gateway", - "ngx::http::status_service_unavailable", - "ngx::http::status_gateway_timeout", - "ngx::http::status_version_not_supported", - "ngx::http::status_insufficient_storage", - "ngx::log_level::stderr", - "ngx::log_level::emerg", - "ngx::log_level::alert", - "ngx::log_level::crit", - "ngx::log_level::error", - "ngx::log_level::warn", - "ngx::log_level::notice", - "ngx::log_level::info", - "ngx::log_level::debug", - NGX_REQ_GET_METHOD_NAME, - NGX_REQ_HTTP_VERSION_NAME, - NGX_REQ_RAW_HEADER_NAME, - NGX_REQ_GET_HEADERS_NAME, - NGX_REQ_GET_URI_ARGS_NAME, - NGX_REQ_GET_POST_ARGS_NAME, - NGX_REQ_READ_BODY_NAME, - NGX_REQ_DISCARD_BODY_NAME, - NGX_REQ_GET_BODY_DATA_NAME, - NGX_REQ_GET_BODY_FILE_NAME, - NGX_REQ_SET_BODY_DATA_NAME, - NGX_REQ_SET_BODY_FILE_NAME, - NGX_REQ_INIT_BODY_NAME, - NGX_REQ_APPEND_BODY_NAME, - NGX_REQ_FINISH_BODY_NAME, - NGX_REQ_IS_INTERNAL_NAME, - NGX_REQ_START_TIME_NAME, - NGX_IS_SUBREQUEST_NAME, - NGX_HEADERS_SENT_NAME, - NGX_VAR_GET_NAME, - NGX_REQ_SET_METHOD_NAME, - NGX_REQ_SET_URI_NAME, - NGX_REQ_SET_URI_ARGS_NAME, - NGX_REQ_SET_HEADER_NAME, - NGX_REQ_CLEAR_HEADER_NAME, - NGX_VAR_SET_NAME, - NGX_STATUS_GET_NAME, - NGX_STATUS_SET_NAME, - NGX_HEADER_GET_NAME, - NGX_HEADER_SET_NAME, - NGX_HEADER_SET_ALL_NAME, - NGX_HEADER_CLEAR_NAME, - NGX_EXIT_NAME, - NGX_REDIRECT_NAME, - NGX_CTX_GET_NAME, - NGX_CTX_SET_NAME, - NGX_CTX_DELETE_NAME, - NGX_CTX_CLEAR_NAME, - NGX_SHARED_DICT_NAME, - NGX_SHARED_GET_NAME, - NGX_SHARED_GET_STALE_NAME, - NGX_SHARED_SET_NAME, - NGX_SHARED_SAFE_SET_NAME, - NGX_SHARED_ADD_NAME, - NGX_SHARED_SAFE_ADD_NAME, - NGX_SHARED_REPLACE_NAME, - NGX_SHARED_DELETE_NAME, - NGX_SHARED_INCR_NAME, - NGX_SHARED_TTL_NAME, - NGX_SHARED_EXPIRE_NAME, - NGX_SHARED_CAPACITY_NAME, - NGX_SHARED_FREE_SPACE_NAME, - NGX_SHARED_LPUSH_NAME, - NGX_SHARED_RPUSH_NAME, - NGX_SHARED_LPOP_NAME, - NGX_SHARED_RPOP_NAME, - NGX_SHARED_LLEN_NAME, - NGX_SHARED_FLUSH_ALL_NAME, - NGX_SHARED_FLUSH_EXPIRED_NAME, - NGX_SHARED_GET_KEYS_NAME, - NGX_SOCKET_UDP_NAME, - NGX_SOCKET_UDP_BIND_NAME, - NGX_SOCKET_UDP_SETPEERNAME_NAME, - NGX_SOCKET_UDP_SEND_NAME, - NGX_SOCKET_UDP_RECEIVE_NAME, - NGX_SOCKET_UDP_CLOSE_NAME, - NGX_SOCKET_UDP_SETTIMEOUT_NAME, - NGX_SOCKET_TCP_NAME, - NGX_SOCKET_TCP_CONNECT_NAME, - NGX_SOCKET_CONNECT_NAME, - NGX_SOCKET_TCP_SEND_NAME, - NGX_SOCKET_TCP_RECEIVE_NAME, - NGX_SOCKET_TCP_RECEIVEANY_NAME, - NGX_SOCKET_TCP_RECEIVEUNTIL_NAME, - NGX_SOCKET_TCP_CLOSE_NAME, - NGX_SOCKET_TCP_SETTIMEOUT_NAME, - NGX_SOCKET_TCP_SETKEEPALIVE_NAME, - NGX_SOCKET_TCP_GETREUSEDTIMES_NAME, - NGX_SOCKET_TCP_SSLHANDSHAKE_NAME, - NGX_SOCKET_TCP_SETTRUSTEDSTORE_NAME, - NGX_SOCKET_TCP_SETCLIENTCERT_NAME, - NGX_SOCKET_TCP_TRUSTEDSTORE_NAME, - NGX_SOCKET_TCP_CLIENTCERT_NAME, - NGX_SEND_HEADERS_NAME, - NGX_PRINT_NAME, - NGX_SAY_NAME, - NGX_FLUSH_NAME, - NGX_EOF_NAME, - ] + static NAMES: std::sync::OnceLock> = std::sync::OnceLock::new(); + NAMES + .get_or_init(|| { + catalog::ngx_host_manifest() + .iter() + .map(|entry| entry.name) + .collect() + }) + .as_slice() } -/// One production host wrapper shape. -#[derive(Clone, Copy)] -pub enum NgxHostWrapper { - /// `bind_static_function` shape: receives the VM and the call arguments. - Static(vm::StaticHostFunction), - /// `bind_static_args_function` shape: receives only the call arguments. - Args(vm::StaticHostArgsFunction), -} - -/// Every production host binding, in bind order. +/// Bind the calling VM through the production descriptor registry. /// -/// Single source of truth for [`bind_ngx_hosts`] and for the registry that -/// binds the VMs importing the `ngx::timer` family ([`ngx_host_registry`]). -pub fn ngx_host_bindings() -> &'static [(&'static str, NgxHostWrapper)] { - &[ - ( - NGX_SLEEP_NAME, - NgxHostWrapper::Static(layout::helpers::functions::sleep), - ), - ( - NGX_SLEEP_MS_NAME, - NgxHostWrapper::Static(layout::helpers::functions::sleep_ms), - ), - ( - NGX_SEMAPHORE_NEW_NAME, - NgxHostWrapper::Static(layout::helpers::semaphore::new), - ), - ( - NGX_SEMAPHORE_WAIT_NAME, - NgxHostWrapper::Static(layout::helpers::semaphore::wait), - ), - ( - NGX_SEMAPHORE_POST_NAME, - NgxHostWrapper::Static(layout::helpers::semaphore::post), - ), - ( - NGX_SEMAPHORE_COUNT_NAME, - NgxHostWrapper::Static(layout::helpers::semaphore::count), - ), - ( - NGX_ESCAPE_URI_NAME, - NgxHostWrapper::Args(layout::helpers::codec::escape_uri), - ), - ( - NGX_UNESCAPE_URI_NAME, - NgxHostWrapper::Args(layout::helpers::codec::unescape_uri), - ), - ( - NGX_ENCODE_ARGS_NAME, - NgxHostWrapper::Args(layout::helpers::codec::encode_args), - ), - ( - NGX_DECODE_ARGS_NAME, - NgxHostWrapper::Args(layout::helpers::codec::decode_args), - ), - ( - NGX_ENCODE_BASE64_NAME, - NgxHostWrapper::Args(layout::helpers::codec::encode_base64), - ), - ( - NGX_DECODE_BASE64_NAME, - NgxHostWrapper::Args(layout::helpers::codec::decode_base64), - ), - ( - NGX_DECODE_BASE64MIME_NAME, - NgxHostWrapper::Args(layout::helpers::codec::decode_base64mime), - ), - ( - NGX_CRC32_SHORT_NAME, - NgxHostWrapper::Args(layout::helpers::codec::crc32_short), - ), - ( - NGX_CRC32_LONG_NAME, - NgxHostWrapper::Args(layout::helpers::codec::crc32_long), - ), - ( - NGX_HMAC_SHA1_NAME, - NgxHostWrapper::Args(layout::helpers::codec::hmac_sha1), - ), - ( - NGX_MD5_NAME, - NgxHostWrapper::Args(layout::helpers::codec::md5), - ), - ( - NGX_MD5_BIN_NAME, - NgxHostWrapper::Args(layout::helpers::codec::md5_bin), - ), - ( - NGX_SHA1_BIN_NAME, - NgxHostWrapper::Args(layout::helpers::codec::sha1_bin), - ), - ( - NGX_QUOTE_SQL_STR_NAME, - NgxHostWrapper::Args(layout::helpers::codec::quote_sql_str), - ), - ( - NGX_TODAY_NAME, - NgxHostWrapper::Args(layout::helpers::time::today), - ), - ( - NGX_TIME_NAME, - NgxHostWrapper::Args(layout::helpers::time::time), - ), - ( - NGX_NOW_NAME, - NgxHostWrapper::Args(layout::helpers::time::now), - ), - ( - NGX_UPDATE_TIME_NAME, - NgxHostWrapper::Args(layout::helpers::time::update_time), - ), - ( - NGX_LOCALTIME_NAME, - NgxHostWrapper::Args(layout::helpers::time::localtime), - ), - ( - NGX_UTCTIME_NAME, - NgxHostWrapper::Args(layout::helpers::time::utctime), - ), - ( - NGX_COOKIE_TIME_NAME, - NgxHostWrapper::Args(layout::helpers::time::cookie_time), - ), - ( - NGX_HTTP_TIME_NAME, - NgxHostWrapper::Args(layout::helpers::time::http_time), - ), - ( - NGX_PARSE_HTTP_TIME_NAME, - NgxHostWrapper::Args(layout::helpers::time::parse_http_time), - ), - ( - "ngx::get_phase", - NgxHostWrapper::Args(layout::helpers::meta::get_phase), - ), - ( - "ngx::config::subsystem", - NgxHostWrapper::Args(layout::helpers::meta::config_subsystem), - ), - ( - "ngx::config::debug", - NgxHostWrapper::Args(layout::helpers::meta::config_debug), - ), - ( - "ngx::config::prefix", - NgxHostWrapper::Args(layout::helpers::meta::config_prefix), - ), - ( - "ngx::config::nginx_version", - NgxHostWrapper::Args(layout::helpers::meta::config_nginx_version), - ), - ( - "ngx::config::nginx_configure", - NgxHostWrapper::Args(layout::helpers::meta::config_nginx_configure), - ), - ( - "ngx::config::ngx_rustscript_version", - NgxHostWrapper::Args(layout::helpers::meta::config_ngx_rustscript_version), - ), - ( - "ngx::worker::exiting", - NgxHostWrapper::Args(layout::helpers::meta::worker_exiting), - ), - ( - "ngx::worker::pid", - NgxHostWrapper::Args(layout::helpers::meta::worker_pid), - ), - ( - "ngx::worker::pids", - NgxHostWrapper::Args(layout::helpers::meta::worker_pids), - ), - ( - "ngx::worker::count", - NgxHostWrapper::Args(layout::helpers::meta::worker_count), - ), - ( - "ngx::worker::id", - NgxHostWrapper::Args(layout::helpers::meta::worker_id), - ), - ("ngx::log", NgxHostWrapper::Args(layout::helpers::log::log)), - ( - "ngx::core::ok", - NgxHostWrapper::Args(layout::helpers::meta::core_ok), - ), - ( - "ngx::core::error", - NgxHostWrapper::Args(layout::helpers::meta::core_error), - ), - ( - "ngx::core::again", - NgxHostWrapper::Args(layout::helpers::meta::core_again), - ), - ( - "ngx::core::done", - NgxHostWrapper::Args(layout::helpers::meta::core_done), - ), - ( - "ngx::core::declined", - NgxHostWrapper::Args(layout::helpers::meta::core_declined), - ), - ( - "ngx::core::null_value", - NgxHostWrapper::Args(layout::helpers::meta::core_null_value), - ), - ( - "ngx::http::get", - NgxHostWrapper::Args(layout::helpers::meta::http_get), - ), - ( - "ngx::http::head", - NgxHostWrapper::Args(layout::helpers::meta::http_head), - ), - ( - "ngx::http::put", - NgxHostWrapper::Args(layout::helpers::meta::http_put), - ), - ( - "ngx::http::post", - NgxHostWrapper::Args(layout::helpers::meta::http_post), - ), - ( - "ngx::http::delete", - NgxHostWrapper::Args(layout::helpers::meta::http_delete), - ), - ( - "ngx::http::options", - NgxHostWrapper::Args(layout::helpers::meta::http_options), - ), - ( - "ngx::http::mkcol", - NgxHostWrapper::Args(layout::helpers::meta::http_mkcol), - ), - ( - "ngx::http::copy", - NgxHostWrapper::Args(layout::helpers::meta::http_copy), - ), - ( - "ngx::http::move", - NgxHostWrapper::Args(layout::helpers::meta::http_move), - ), - ( - "ngx::http::propfind", - NgxHostWrapper::Args(layout::helpers::meta::http_propfind), - ), - ( - "ngx::http::proppatch", - NgxHostWrapper::Args(layout::helpers::meta::http_proppatch), - ), - ( - "ngx::http::lock", - NgxHostWrapper::Args(layout::helpers::meta::http_lock), - ), - ( - "ngx::http::unlock", - NgxHostWrapper::Args(layout::helpers::meta::http_unlock), - ), - ( - "ngx::http::patch", - NgxHostWrapper::Args(layout::helpers::meta::http_patch), - ), - ( - "ngx::http::trace", - NgxHostWrapper::Args(layout::helpers::meta::http_trace), - ), - ( - "ngx::http::status_continue", - NgxHostWrapper::Args(layout::helpers::meta::http_status_continue), - ), - ( - "ngx::http::status_switching_protocols", - NgxHostWrapper::Args(layout::helpers::meta::http_status_switching_protocols), - ), - ( - "ngx::http::status_ok", - NgxHostWrapper::Args(layout::helpers::meta::http_status_ok), - ), - ( - "ngx::http::status_created", - NgxHostWrapper::Args(layout::helpers::meta::http_status_created), - ), - ( - "ngx::http::status_accepted", - NgxHostWrapper::Args(layout::helpers::meta::http_status_accepted), - ), - ( - "ngx::http::status_no_content", - NgxHostWrapper::Args(layout::helpers::meta::http_status_no_content), - ), - ( - "ngx::http::status_partial_content", - NgxHostWrapper::Args(layout::helpers::meta::http_status_partial_content), - ), - ( - "ngx::http::status_special_response", - NgxHostWrapper::Args(layout::helpers::meta::http_status_special_response), - ), - ( - "ngx::http::status_moved_permanently", - NgxHostWrapper::Args(layout::helpers::meta::http_status_moved_permanently), - ), - ( - "ngx::http::status_moved_temporarily", - NgxHostWrapper::Args(layout::helpers::meta::http_status_moved_temporarily), - ), - ( - "ngx::http::status_see_other", - NgxHostWrapper::Args(layout::helpers::meta::http_status_see_other), - ), - ( - "ngx::http::status_not_modified", - NgxHostWrapper::Args(layout::helpers::meta::http_status_not_modified), - ), - ( - "ngx::http::status_temporary_redirect", - NgxHostWrapper::Args(layout::helpers::meta::http_status_temporary_redirect), - ), - ( - "ngx::http::status_permanent_redirect", - NgxHostWrapper::Args(layout::helpers::meta::http_status_permanent_redirect), - ), - ( - "ngx::http::status_bad_request", - NgxHostWrapper::Args(layout::helpers::meta::http_status_bad_request), - ), - ( - "ngx::http::status_unauthorized", - NgxHostWrapper::Args(layout::helpers::meta::http_status_unauthorized), - ), - ( - "ngx::http::status_payment_required", - NgxHostWrapper::Args(layout::helpers::meta::http_status_payment_required), - ), - ( - "ngx::http::status_forbidden", - NgxHostWrapper::Args(layout::helpers::meta::http_status_forbidden), - ), - ( - "ngx::http::status_not_found", - NgxHostWrapper::Args(layout::helpers::meta::http_status_not_found), - ), - ( - "ngx::http::status_not_allowed", - NgxHostWrapper::Args(layout::helpers::meta::http_status_not_allowed), - ), - ( - "ngx::http::status_not_acceptable", - NgxHostWrapper::Args(layout::helpers::meta::http_status_not_acceptable), - ), - ( - "ngx::http::status_request_timeout", - NgxHostWrapper::Args(layout::helpers::meta::http_status_request_timeout), - ), - ( - "ngx::http::status_conflict", - NgxHostWrapper::Args(layout::helpers::meta::http_status_conflict), - ), - ( - "ngx::http::status_gone", - NgxHostWrapper::Args(layout::helpers::meta::http_status_gone), - ), - ( - "ngx::http::status_upgrade_required", - NgxHostWrapper::Args(layout::helpers::meta::http_status_upgrade_required), - ), - ( - "ngx::http::status_too_many_requests", - NgxHostWrapper::Args(layout::helpers::meta::http_status_too_many_requests), - ), - ( - "ngx::http::status_illegal", - NgxHostWrapper::Args(layout::helpers::meta::http_status_illegal), - ), - ( - "ngx::http::status_close", - NgxHostWrapper::Args(layout::helpers::meta::http_status_close), - ), - ( - "ngx::http::status_internal_server_error", - NgxHostWrapper::Args(layout::helpers::meta::http_status_internal_server_error), - ), - ( - "ngx::http::status_not_implemented", - NgxHostWrapper::Args(layout::helpers::meta::http_status_not_implemented), - ), - ( - "ngx::http::status_bad_gateway", - NgxHostWrapper::Args(layout::helpers::meta::http_status_bad_gateway), - ), - ( - "ngx::http::status_service_unavailable", - NgxHostWrapper::Args(layout::helpers::meta::http_status_service_unavailable), - ), - ( - "ngx::http::status_gateway_timeout", - NgxHostWrapper::Args(layout::helpers::meta::http_status_gateway_timeout), - ), - ( - "ngx::http::status_version_not_supported", - NgxHostWrapper::Args(layout::helpers::meta::http_status_version_not_supported), - ), - ( - "ngx::http::status_insufficient_storage", - NgxHostWrapper::Args(layout::helpers::meta::http_status_insufficient_storage), - ), - ( - "ngx::log_level::stderr", - NgxHostWrapper::Args(layout::helpers::meta::log_level_stderr), - ), - ( - "ngx::log_level::emerg", - NgxHostWrapper::Args(layout::helpers::meta::log_level_emerg), - ), - ( - "ngx::log_level::alert", - NgxHostWrapper::Args(layout::helpers::meta::log_level_alert), - ), - ( - "ngx::log_level::crit", - NgxHostWrapper::Args(layout::helpers::meta::log_level_crit), - ), - ( - "ngx::log_level::error", - NgxHostWrapper::Args(layout::helpers::meta::log_level_error), - ), - ( - "ngx::log_level::warn", - NgxHostWrapper::Args(layout::helpers::meta::log_level_warn), - ), - ( - "ngx::log_level::notice", - NgxHostWrapper::Args(layout::helpers::meta::log_level_notice), - ), - ( - "ngx::log_level::info", - NgxHostWrapper::Args(layout::helpers::meta::log_level_info), - ), - ( - "ngx::log_level::debug", - NgxHostWrapper::Args(layout::helpers::meta::log_level_debug), - ), - ( - NGX_REQ_GET_METHOD_NAME, - NgxHostWrapper::Args(layout::helpers::request::get_method), - ), - ( - NGX_REQ_HTTP_VERSION_NAME, - NgxHostWrapper::Args(layout::helpers::request::http_version), - ), - ( - NGX_REQ_RAW_HEADER_NAME, - NgxHostWrapper::Args(layout::helpers::request::raw_header), - ), - ( - NGX_REQ_GET_HEADERS_NAME, - NgxHostWrapper::Args(layout::helpers::request::get_headers), - ), - ( - NGX_REQ_GET_URI_ARGS_NAME, - NgxHostWrapper::Args(layout::helpers::request::get_uri_args), - ), - ( - NGX_REQ_GET_POST_ARGS_NAME, - NgxHostWrapper::Args(layout::helpers::request::get_post_args), - ), - ( - NGX_REQ_READ_BODY_NAME, - NgxHostWrapper::Static(layout::helpers::body::read_body), - ), - ( - NGX_REQ_DISCARD_BODY_NAME, - NgxHostWrapper::Args(layout::helpers::body::discard_body), - ), - ( - NGX_REQ_GET_BODY_DATA_NAME, - NgxHostWrapper::Args(layout::helpers::body::get_body_data), - ), - ( - NGX_REQ_GET_BODY_FILE_NAME, - NgxHostWrapper::Args(layout::helpers::body::get_body_file), - ), - ( - NGX_REQ_SET_BODY_DATA_NAME, - NgxHostWrapper::Args(layout::helpers::body::set_body_data), - ), - ( - NGX_REQ_SET_BODY_FILE_NAME, - NgxHostWrapper::Args(layout::helpers::body::set_body_file), - ), - ( - NGX_REQ_INIT_BODY_NAME, - NgxHostWrapper::Args(layout::helpers::body::init_body), - ), - ( - NGX_REQ_APPEND_BODY_NAME, - NgxHostWrapper::Args(layout::helpers::body::append_body), - ), - ( - NGX_REQ_FINISH_BODY_NAME, - NgxHostWrapper::Args(layout::helpers::body::finish_body), - ), - ( - NGX_REQ_IS_INTERNAL_NAME, - NgxHostWrapper::Args(layout::helpers::request::is_internal), - ), - ( - NGX_REQ_START_TIME_NAME, - NgxHostWrapper::Args(layout::helpers::request::start_time), - ), - ( - NGX_IS_SUBREQUEST_NAME, - NgxHostWrapper::Args(layout::helpers::request::is_subrequest), - ), - ( - NGX_HEADERS_SENT_NAME, - NgxHostWrapper::Args(layout::helpers::request::headers_sent), - ), - ( - NGX_VAR_GET_NAME, - NgxHostWrapper::Args(layout::helpers::variables::get), - ), - ( - NGX_REQ_SET_METHOD_NAME, - NgxHostWrapper::Args(layout::helpers::request::set_method), - ), - ( - NGX_REQ_SET_URI_NAME, - NgxHostWrapper::Args(layout::helpers::request::set_uri), - ), - ( - NGX_REQ_SET_URI_ARGS_NAME, - NgxHostWrapper::Args(layout::helpers::request::set_uri_args), - ), - ( - NGX_REQ_SET_HEADER_NAME, - NgxHostWrapper::Args(layout::helpers::request::set_header), - ), - ( - NGX_REQ_CLEAR_HEADER_NAME, - NgxHostWrapper::Args(layout::helpers::request::clear_header), - ), - ( - NGX_VAR_SET_NAME, - NgxHostWrapper::Args(layout::helpers::variables::set), - ), - ( - NGX_STATUS_GET_NAME, - NgxHostWrapper::Args(layout::helpers::response_meta::status_get), - ), - ( - NGX_STATUS_SET_NAME, - NgxHostWrapper::Args(layout::helpers::response_meta::status_set), - ), - ( - NGX_HEADER_GET_NAME, - NgxHostWrapper::Args(layout::helpers::response_meta::header_get), - ), - ( - NGX_HEADER_SET_NAME, - NgxHostWrapper::Args(layout::helpers::response_meta::header_set), - ), - ( - NGX_HEADER_SET_ALL_NAME, - NgxHostWrapper::Args(layout::helpers::response_meta::header_set_all), - ), - ( - NGX_HEADER_CLEAR_NAME, - NgxHostWrapper::Args(layout::helpers::response_meta::header_clear), - ), - ( - NGX_EXIT_NAME, - NgxHostWrapper::Args(layout::helpers::response_meta::exit), - ), - ( - NGX_REDIRECT_NAME, - NgxHostWrapper::Args(layout::helpers::response_meta::redirect), - ), - ( - NGX_CTX_GET_NAME, - NgxHostWrapper::Args(layout::helpers::request_ctx::get), - ), - ( - NGX_CTX_SET_NAME, - NgxHostWrapper::Args(layout::helpers::request_ctx::set), - ), - ( - NGX_CTX_DELETE_NAME, - NgxHostWrapper::Args(layout::helpers::request_ctx::delete), - ), - ( - NGX_CTX_CLEAR_NAME, - NgxHostWrapper::Args(layout::helpers::request_ctx::clear), - ), - ( - NGX_SHARED_DICT_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::dict), - ), - ( - NGX_SHARED_GET_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::get), - ), - ( - NGX_SHARED_GET_STALE_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::get_stale), - ), - ( - NGX_SHARED_SET_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::set), - ), - ( - NGX_SHARED_SAFE_SET_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::safe_set), - ), - ( - NGX_SHARED_ADD_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::add), - ), - ( - NGX_SHARED_SAFE_ADD_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::safe_add), - ), - ( - NGX_SHARED_REPLACE_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::replace), - ), - ( - NGX_SHARED_DELETE_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::delete), - ), - ( - NGX_SHARED_INCR_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::incr), - ), - ( - NGX_SHARED_TTL_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::ttl), - ), - ( - NGX_SHARED_EXPIRE_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::expire), - ), - ( - NGX_SHARED_CAPACITY_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::capacity), - ), - ( - NGX_SHARED_FREE_SPACE_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::free_space), - ), - ( - NGX_SHARED_LPUSH_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::lpush), - ), - ( - NGX_SHARED_RPUSH_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::rpush), - ), - ( - NGX_SHARED_LPOP_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::lpop), - ), - ( - NGX_SHARED_RPOP_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::rpop), - ), - ( - NGX_SHARED_LLEN_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::llen), - ), - ( - NGX_SHARED_FLUSH_ALL_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::flush_all), - ), - ( - NGX_SHARED_FLUSH_EXPIRED_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::flush_expired), - ), - ( - NGX_SHARED_GET_KEYS_NAME, - NgxHostWrapper::Static(layout::helpers::shared_dict::get_keys), - ), - ( - NGX_SOCKET_UDP_NAME, - NgxHostWrapper::Static(layout::helpers::socket::udp), - ), - ( - NGX_SOCKET_UDP_BIND_NAME, - NgxHostWrapper::Static(layout::helpers::socket::bind), - ), - ( - NGX_SOCKET_UDP_SETPEERNAME_NAME, - NgxHostWrapper::Static(layout::helpers::socket::setpeername), - ), - ( - NGX_SOCKET_UDP_SEND_NAME, - NgxHostWrapper::Static(layout::helpers::socket::send), - ), - ( - NGX_SOCKET_UDP_RECEIVE_NAME, - NgxHostWrapper::Static(layout::helpers::socket::receive), - ), - ( - NGX_SOCKET_UDP_CLOSE_NAME, - NgxHostWrapper::Static(layout::helpers::socket::close), - ), - ( - NGX_SOCKET_UDP_SETTIMEOUT_NAME, - NgxHostWrapper::Static(layout::helpers::socket::settimeout), - ), - ( - NGX_SOCKET_TCP_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp), - ), - ( - NGX_SOCKET_TCP_CONNECT_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_connect), - ), - ( - NGX_SOCKET_CONNECT_NAME, - NgxHostWrapper::Static(layout::helpers::socket::connect), - ), - ( - NGX_SOCKET_TCP_SEND_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_send), - ), - ( - NGX_SOCKET_TCP_RECEIVE_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_receive), - ), - ( - NGX_SOCKET_TCP_RECEIVEANY_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_receiveany), - ), - ( - NGX_SOCKET_TCP_RECEIVEUNTIL_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_receiveuntil), - ), - ( - NGX_SOCKET_TCP_CLOSE_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_close), - ), - ( - NGX_SOCKET_TCP_SETTIMEOUT_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_settimeout), - ), - ( - NGX_SOCKET_TCP_SETKEEPALIVE_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_setkeepalive), - ), - ( - NGX_SOCKET_TCP_GETREUSEDTIMES_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_getreusedtimes), - ), - ( - NGX_SOCKET_TCP_SSLHANDSHAKE_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_sslhandshake), - ), - ( - NGX_SOCKET_TCP_SETTRUSTEDSTORE_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_settrustedstore), - ), - ( - NGX_SOCKET_TCP_SETCLIENTCERT_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_setclientcert), - ), - ( - NGX_SOCKET_TCP_TRUSTEDSTORE_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_trustedstore), - ), - ( - NGX_SOCKET_TCP_CLIENTCERT_NAME, - NgxHostWrapper::Static(layout::helpers::socket::tcp_clientcert), - ), - ( - NGX_SEND_HEADERS_NAME, - NgxHostWrapper::Args(layout::helpers::output::send_headers), - ), - ( - NGX_PRINT_NAME, - NgxHostWrapper::Args(layout::helpers::output::print), - ), - ( - NGX_SAY_NAME, - NgxHostWrapper::Args(layout::helpers::output::say), - ), - ( - NGX_FLUSH_NAME, - NgxHostWrapper::Static(layout::helpers::output::flush), - ), - ( - NGX_EOF_NAME, - NgxHostWrapper::Args(layout::helpers::output::eof), - ), - ] +/// Catalog-compiled programs resolve hosts by exact adapter. There is no +/// name/arity fallback table. +pub fn bind_production_hosts(vm: &mut Vm) -> Result<(), String> { + ngx_host_registry()? + .bind_vm_cached(vm) + .map_err(|error| format!("bind ngx host registry: {error}")) } -/// Bind every production host using the macro-generated wrapper. +/// Bind the calling VM through [`ngx_host_registry`]. +/// +/// Tests and examples that model production use this wrapper; the only +/// binding authority is the restricted catalog registry. pub fn bind_ngx_hosts(vm: &mut Vm) { - for (name, wrapper) in ngx_host_bindings() { - match *wrapper { - NgxHostWrapper::Static(function) => vm.bind_static_function(*name, function), - NgxHostWrapper::Args(function) => vm.bind_static_args_function(*name, function), - } - } + bind_production_hosts(vm).expect("bind ngx host registry"); } -/// Names of the `ngx::timer` family, bound through the timer-host registry. +/// Names of the `ngx::timer` family. /// -/// `ngx::timer::{at,every}` consume their callback through -/// `HostParamPassing::TakeOwned`, so they cannot be installed by -/// [`bind_ngx_hosts`]'s name/symbol path: the owned dispatch needs the -/// registry binding path. Every name below is catalogued in [`ngx_host_catalog`] -/// and resolves through [`ngx_host_registry`]. +/// These hosts are catalogued and installed by [`ngx_host_registry`] with +/// every other production ngx host. `ngx::timer::{at,every}` consume their +/// callback through `HostParamPassing::TakeOwned`. pub fn ngx_timer_host_names() -> &'static [&'static str] { &[ NGX_TIMER_AT_NAME, @@ -1170,19 +196,14 @@ pub fn ngx_timer_host_names() -> &'static [&'static str] { ] } -/// Registry that resolves every production ngx host plus the `ngx::timer` -/// family. -/// -/// A VM whose program imports a timer host is bound from this registry -/// (`bind_vm_cached`) instead of the per-name [`bind_ngx_hosts`] path, because -/// the owned `ngx::timer::{at,every}` entries have no name-binding API. The -/// same registry is handed to `register_owned_timer` through -/// `OwnedHostContext`, so the callback VMs spawn from it with the identical -/// host surface. +/// Restricted production registry for every catalogued ngx host. /// -/// The registry is built once per process and cloned per bind; a clone shares -/// the immutable entry table, capability profile, and standard-surface -/// composition, and reuses its host-binding plan cache. +/// Request VMs, pool VMs, and timer/callback VMs all bind through +/// [`vm::HostFunctionRegistry::bind_vm_cached`]. The registry is built once per +/// process and cloned per bind; a clone shares the immutable entry table, +/// capability profile, and standard-surface composition, and reuses its +/// host-binding plan cache. The same registry is handed to +/// `register_owned_timer` through `OwnedHostContext`. pub fn ngx_host_registry() -> Result { static REGISTRY: std::sync::OnceLock> = std::sync::OnceLock::new(); @@ -1191,22 +212,103 @@ pub fn ngx_host_registry() -> Result { fn build_ngx_host_registry() -> Result { let catalog = catalog::compile_host_catalog(); + // HostFunctionRegistry::restricted() starts from HostFunctionRegistry::new(), + // which catalog-installs HTTP adapters. Those catalog entries then reject + // ngx-compiled `http::client::*` imports that have no fingerprint — the ngx + // compile catalog deliberately omits HTTP so documented map literals keep + // compiling. empty() + deny_all is the same capability restriction without + // that HTTP catalog seed. Standard HTTP names are granted below; bind_vm_cached + // stages name-only adapters through standard_composition::ensure_surfaces. let mut registry = vm::HostFunctionRegistry::empty(); + registry.set_capability_profile(standard_capability_profile()); registry.set_standard_composition(vm::standard_composition()); - for module in ngx_host_modules() { - module - .install_from_catalog(&mut registry, &catalog) - .map_err(|error| format!("install {}: {error}", module.name))?; - } + registry + .transactionally(|registry| { + for module in ngx_host_modules() { + module + .install_from_catalog(registry, &catalog) + .map_err(|error| { + vm::VmError::HostError(format!("install {}: {error}", module.name)) + })?; + } + Ok(()) + }) + .map_err(|error| error.to_string())?; Ok(registry) } +fn standard_capability_profile() -> vm::CapabilityProfile { + let mut builder = vm::CapabilityProfile::builder(); + for catalog in [ + vm::http_host_catalog(), + vm::io_host_catalog(), + vm::sqlite_host_catalog(), + ] { + for function in catalog.functions() { + builder = builder.allow_host_import(function.name.clone()); + if let Some(builtin) = vm::BuiltinFunction::from_namespaced_name(&function.name) { + builder = builder.allow_builtin(builtin); + } + } + } + for spec in vm::builtin_namespace_specs() { + for member in spec.members { + let name = format!("{}::{}", spec.namespace, member.name); + if let Some(builtin) = vm::BuiltinFunction::from_namespaced_name(&name) { + builder = builder.allow_builtin(builtin); + } + } + } + builder.build() +} + +#[cfg(test)] +pub(crate) fn test_hosts_module() -> vm::HostModuleDescriptor { + layout::helpers::functions::test_hosts_module() +} + +#[cfg(test)] +pub(crate) fn test_bind_catalog() -> vm::HostApiCatalog { + let mut builder = vm::HostApiBuilder::new(); + let test_hosts = test_hosts_module().catalog().expect("test host catalog"); + for catalog in [ngx_host_catalog(), test_hosts] { + for resource in catalog.resources() { + builder.resource(resource.clone()); + } + for schema in catalog.structs() { + builder.named_struct(schema.clone()); + } + for function in catalog.functions() { + builder.function(function.clone()); + } + } + builder.build().expect("merged ngx+test catalog") +} + #[cfg(test)] pub(crate) fn bind_test_hosts(vm: &mut Vm) { - bind_ngx_hosts(vm); - vm.bind_static_function("ngx::probe", layout::helpers::functions::probe); - vm.bind_static_function("ngx::ready", layout::helpers::functions::ready); - vm.bind_static_function("ngx::fail", layout::helpers::functions::fail); + let catalog = test_bind_catalog(); + let mut registry = vm::HostFunctionRegistry::empty(); + registry.set_capability_profile(standard_capability_profile()); + registry.set_standard_composition(vm::standard_composition()); + registry + .transactionally(|registry| { + for module in ngx_host_modules() { + module + .install_from_catalog(registry, &catalog) + .map_err(|error| { + vm::VmError::HostError(format!("install {}: {error}", module.name)) + })?; + } + test_hosts_module() + .install_from_catalog(registry, &catalog) + .map_err(|error| vm::VmError::HostError(format!("install ngx.test: {error}")))?; + Ok(()) + }) + .expect("install test hosts"); + registry + .bind_vm_cached(vm) + .expect("bind test host registry"); } mod layout { @@ -1402,6 +504,19 @@ mod layout { resources: &[], } } + + #[cfg(test)] + const TEST_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = + &[probe_descriptor, ready_descriptor, fail_descriptor]; + + #[cfg(test)] + pub fn test_hosts_module() -> vm::HostModuleDescriptor { + vm::HostModuleDescriptor { + name: "ngx.test", + functions: TEST_HOST_MODULE_FUNCTIONS, + resources: &[], + } + } } } } @@ -1409,8 +524,9 @@ mod layout { /// Deterministic ngx host module composition. /// /// Catalog schemas, exact adapters, resource types, and named structs are -/// derived from these descriptors. `bind_ngx_hosts` remains a name-binding -/// compatibility path that does not install owned timer adapters. +/// derived from these descriptors. `bind_ngx_hosts` is a thin wrapper around +/// [`ngx_host_registry`] so request, pool, and timer VMs share one restricted +/// exact-descriptor install. pub fn ngx_host_modules() -> Vec { vec![ layout::helpers::functions::functions_host_module(), diff --git a/src/host/standard.rs b/src/host/standard.rs index 22ee36f..6836fab 100644 --- a/src/host/standard.rs +++ b/src/host/standard.rs @@ -43,10 +43,11 @@ impl Default for StandardHostPolicy { } } -/// Install pd-vm's standard runtime composition and embedding policies. -pub fn install_standard_runtime(vm: &mut Vm, policy: &StandardHostPolicy) -> Result<(), String> { - let composition = standard_composition(); - vm.set_standard_composition(composition.clone()); +/// Apply I/O, HTTP, and SQLite policies without per-name binds. +/// +/// Production VMs resolve standard imports through the descriptor registry +/// plan. A later name bind would flip them back to name resolution. +pub fn install_standard_policy(vm: &mut Vm, policy: &StandardHostPolicy) -> Result<(), String> { vm.configure_io(IoPolicy { allowed_roots: policy.io_roots.clone(), allow_write: policy.io_write, @@ -67,17 +68,14 @@ pub fn install_standard_runtime(vm: &mut Vm, policy: &StandardHostPolicy) -> Res allow_unsafe_sql: false, ..SqlitePolicy::default() }); - let standard_imports: Vec = vm - .program() - .imports - .iter() - .filter(|import| composition.import_in_standard(import)) - .map(|import| import.name.clone()) - .collect(); - for name in standard_imports { - if !composition.bind_default_name(vm, &name) { - return Err(format!("cannot bind standard host import '{name}'")); - } - } Ok(()) } + +/// Install pd-vm's standard runtime composition and embedding policies. +/// +/// Production VMs already resolve standard imports through +/// [`crate::host::ngx_host_registry`]; this only applies the location policy. +pub fn install_standard_runtime(vm: &mut Vm, policy: &StandardHostPolicy) -> Result<(), String> { + vm.set_standard_composition(standard_composition()); + install_standard_policy(vm, policy) +} diff --git a/src/request_state.rs b/src/request_state.rs index 1133e56..63a087d 100644 --- a/src/request_state.rs +++ b/src/request_state.rs @@ -24,8 +24,8 @@ use vm::{Program, Vm, VmError, VmStatus}; use crate::async_bridge::NgxAsyncBridge; use crate::host::{ - AllowedOps, NgxPhase, RequestDisposition, RequestLocalCtx, StandardHostPolicy, bind_ngx_hosts, - install_standard_runtime, is_control_stop, with_vm_request_scope_full, + AllowedOps, NgxPhase, RequestDisposition, RequestLocalCtx, StandardHostPolicy, is_control_stop, + with_vm_request_scope_full, }; use crate::notify::{CompletionPermit, RequestIdentity}; @@ -324,17 +324,17 @@ impl Default for IdentityRegistry { /// Binds the production host surface for one program. /// -/// A program importing `ngx::timer` hosts resolves them from the timer-host -/// registry, because the owned `ngx::timer::{at,every}` entries have no -/// name-binding API; every other program keeps the per-name bind path. The -/// location's standard-host policy applies in both paths. +/// Every request VM installs catalog adapters from [`crate::host::ngx_host_registry`]. +/// Programs that import `ngx::timer` hosts also receive the worker timer +/// runtime. The location's standard-host policy is applied without per-name +/// binds so the registry plan stays exact. pub(crate) fn bind_program_hosts(vm: &mut Vm, policy: &StandardHostPolicy) -> Result<(), String> { + crate::host::bind_production_hosts(vm)?; + crate::host::install_standard_policy(vm, policy)?; if crate::timer::program_uses_ngx_timer_hosts(vm.program()) { - crate::timer::install_timer_vm(vm, policy) - } else { - bind_ngx_hosts(vm); - install_standard_runtime(vm, policy) + crate::timer::install_timer_runtime_on_vm(vm); } + Ok(()) } /// Wait-slot + waker factory used by [`AccessExec`]. diff --git a/src/runtime.rs b/src/runtime.rs index 0c56a7f..3ff74ba 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -36,6 +36,7 @@ thread_local! { pub enum RuntimeError { Notify(NotifyError), RuntimeBuild(String), + HostRegistry(String), AlreadyStarted, } @@ -44,6 +45,7 @@ impl fmt::Display for RuntimeError { match self { Self::Notify(err) => write!(f, "{err}"), Self::RuntimeBuild(msg) => write!(f, "tokio runtime build failed: {msg}"), + Self::HostRegistry(msg) => write!(f, "host registry: {msg}"), Self::AlreadyStarted => write!(f, "worker runtime is already started"), } } @@ -78,6 +80,7 @@ impl WorkerRuntime { } pub fn start_with_capacity(cap: usize) -> Result { + crate::host::ngx_host_registry().map_err(RuntimeError::HostRegistry)?; let runtime = tokio::runtime::Builder::new_multi_thread() .worker_threads(1) .enable_time() @@ -259,6 +262,11 @@ pub unsafe fn init_worker(cycle: *mut ngx_cycle_t) -> Result<(), RuntimeError> { if worker_is_active() { return Err(RuntimeError::AlreadyStarted); } + // Build the restricted catalog registry before the worker accepts + // connections. First `bind_vm_cached` is cheap once the process-wide + // table exists; constructing it on the first request would block the + // event loop for the entire install. + crate::host::ngx_host_registry().map_err(RuntimeError::HostRegistry)?; let mut worker = WorkerRuntime::start()?; unsafe { worker.register_nginx(cycle)? }; if !cycle.is_null() { diff --git a/src/timer.rs b/src/timer.rs index cec37d0..b1d4694 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -64,13 +64,12 @@ use std::time::Duration; use tokio::runtime::Handle; use vm::{ - HttpConfig, HttpHostExt, IoHostExt, IoPolicy, OwnedTimerCallback, SqliteHostExt, SqlitePolicy, - TimerBackend, TimerCallbackError, TimerCallbackState, TimerConfig, TimerHostExt, - TimerRegistration, Vm, VmError, VmResult, + OwnedTimerCallback, TimerBackend, TimerCallbackError, TimerCallbackState, TimerConfig, + TimerHostExt, TimerRegistration, Vm, VmError, VmResult, }; use crate::async_bridge::NgxAsyncBridge; -use crate::host::{AllowedOps, NgxPhase, StandardHostPolicy, ngx_host_registry}; +use crate::host::{AllowedOps, NgxPhase, StandardHostPolicy}; use crate::notify::{CompletionSender, RequestIdentity, eventfd_waker}; use crate::request_state::{IdentityRegistry, RegistryError}; @@ -195,7 +194,7 @@ pub(crate) fn install_worker_timers(handle: Handle, sender: CompletionSender) { /// True when `program` imports any `ngx::timer` host. /// -/// Such programs are bound from [`ngx_host_registry`] (owned dispatch) and must +/// Such programs are bound from [`crate::host::ngx_host_registry`] and must /// run on the request-owned VM path, so configuration marks them as requiring /// request state. pub fn program_uses_ngx_timer_hosts(program: &vm::Program) -> bool { @@ -320,48 +319,19 @@ fn with_timer_scope(identity: RequestIdentity, f: impl FnOnce() -> R) -> R { /// Installs the worker timer backend on a VM that imports the timer family. /// -/// The timer family contains owned-dispatch hosts, which can only be bound -/// through the registry path; the standard I/O, HTTP, and SQLite policies are -/// installed without per-name binds because the registry plan already resolved -/// every standard import. +/// Hosts are bound through [`crate::host::ngx_host_registry`]; this only +/// attaches the worker timer runtime after that bind. pub fn install_timer_vm(vm: &mut Vm, policy: &StandardHostPolicy) -> Result<(), String> { - let registry = ngx_host_registry()?; - registry - .bind_vm_cached(vm) - .map_err(|error| format!("bind ngx timer host registry: {error}"))?; - install_standard_policy(vm, policy)?; - let backend = worker_backend().unwrap_or_else(|| Arc::new(NgxTimerBackend)); - vm.install_timer_runtime(backend, TimerConfig::default()); + crate::host::bind_production_hosts(vm)?; + crate::host::install_standard_policy(vm, policy)?; + install_timer_runtime_on_vm(vm); Ok(()) } -/// Applies the location's standard-host policy without the per-name binds that -/// [`crate::host::install_standard_runtime`] performs. -/// -/// A plan-bound VM resolves imports by registry slot; a later name bind would -/// flip it back to name resolution and fail on the owned timer entries. -fn install_standard_policy(vm: &mut Vm, policy: &StandardHostPolicy) -> Result<(), String> { - vm.configure_io(IoPolicy { - allowed_roots: policy.io_roots.clone(), - allow_write: policy.io_write, - allow_process: false, - max_read_bytes: policy.io_max_read, - max_write_bytes: policy.io_max_write, - }); - vm.configure_http(HttpConfig { - allowed_schemes: policy.http_schemes.clone(), - allowed_hosts: policy.http_hosts.clone(), - allowed_ports: policy.http_ports.clone(), - allow_private_ips: policy.http_allow_private, - ..HttpConfig::default() - }) - .map_err(|err| err.to_string())?; - vm.configure_sqlite(SqlitePolicy { - database_root: policy.sqlite_root.clone(), - allow_unsafe_sql: false, - ..SqlitePolicy::default() - }); - Ok(()) +/// Attach the worker-owned timer runtime to an already registry-bound VM. +pub fn install_timer_runtime_on_vm(vm: &mut Vm) { + let backend = worker_backend().unwrap_or_else(|| Arc::new(NgxTimerBackend)); + vm.install_timer_runtime(backend, TimerConfig::default()); } /// Dispatches every ready identity that is a live timer registration. diff --git a/src/vm_pool.rs b/src/vm_pool.rs index e406471..81dc68a 100644 --- a/src/vm_pool.rs +++ b/src/vm_pool.rs @@ -66,7 +66,7 @@ fn construct(program: &Arc) -> Vm { #[cfg(test)] CREATED.with(|created| created.set(created.get().saturating_add(1))); let mut vm = Vm::new_shared(Arc::clone(program)); - crate::host::bind_ngx_hosts(&mut vm); + crate::host::bind_production_hosts(&mut vm).expect("bind ngx host registry"); vm } From 2ad42d42dd9bf9716ab42a9fe69f3a7066ba91d3 Mon Sep 17 00:00:00 2001 From: fffonion Date: Thu, 17 Sep 2026 16:22:48 +0800 Subject: [PATCH 5/6] feat(host): name shared-dict results and type semaphore handles Bump catalog schema to v2 with Shared*Result named structs. Sync methods take ResourceRef; constructors, async wait, and close keep raw guest tokens at the VM boundary. --- examples/async-pipeline.rss | 5 -- examples/rate-limit.rss | 11 ---- src/host/catalog.rs | 123 ++++++++++++++++++++++++++++++++---- src/host/semaphore.rs | 20 +++--- src/host/shared_dict.rs | 79 ++++++++++++++++++++++- 5 files changed, 201 insertions(+), 37 deletions(-) diff --git a/examples/async-pipeline.rss b/examples/async-pipeline.rss index 5abd416..a3e8e1c 100644 --- a/examples/async-pipeline.rss +++ b/examples/async-pipeline.rss @@ -5,11 +5,6 @@ use sqlite; use json; use bytes as builtin_bytes; -struct SharedStoreResult { - ok: bool, - error: string? -} - let url_file = ngx::var::get("rss_pipeline_url_file").unwrap_or(""); let database_file = ngx::var::get("rss_pipeline_database").unwrap_or(""); diff --git a/examples/rate-limit.rss b/examples/rate-limit.rss index b24ed21..197df29 100644 --- a/examples/rate-limit.rss +++ b/examples/rate-limit.rss @@ -1,16 +1,5 @@ use ngx; -struct SharedSafeStoreResult { - ok: bool, - error: string? -} - -struct SharedIncrResult { - value: number?, - error: string?, - forcible: bool -} - let LIMIT = 5; let WINDOW_SECONDS = 2; let RETRY_AFTER = "2"; diff --git a/src/host/catalog.rs b/src/host/catalog.rs index 7533dff..b9dbd51 100644 --- a/src/host/catalog.rs +++ b/src/host/catalog.rs @@ -2,7 +2,8 @@ //! //! The pinned `pd-host-function` macro does not emit catalog, phase, or //! capability data. This module is the import allowlist and config-time -//! validator. Runtime dispatch stays in [`crate::host::bind_ngx_hosts`]. +//! validator. Runtime dispatch is installed from [`crate::host::ngx_host_modules`] +//! through the restricted exact descriptor registry. use std::collections::{HashMap, HashSet}; use std::fmt; @@ -162,6 +163,19 @@ pub const NGX_SHARED_FLUSH_ALL_NAME: &str = "ngx::shared::flush_all"; pub const NGX_SHARED_FLUSH_EXPIRED_NAME: &str = "ngx::shared::flush_expired"; pub const NGX_SHARED_GET_KEYS_NAME: &str = "ngx::shared::get_keys"; +/// Catalog schema version for fingerprint goldens. Bump together with +/// [`NGX_HOST_CATALOG_FINGERPRINT`] when named structs, resource keys, or +/// function shapes change. +pub const NGX_HOST_CATALOG_SCHEMA_VERSION: u32 = 2; + +/// Fingerprint of schema version 1: named request/codec envelopes with +/// `Unknown` shared-dict result carriers. +pub const NGX_HOST_CATALOG_FINGERPRINT_V1: u64 = 0x5e37_8285_8fa0_b584; + +/// Fingerprint of the current schema version. The host_catalog drift gate +/// compares `ngx_host_catalog().fingerprint()` against this constant. +pub const NGX_HOST_CATALOG_FINGERPRINT: u64 = 0x10c4_07b2_b827_eaff; + pub const NGX_SEND_HEADERS_NAME: &str = "ngx::send_headers"; pub const NGX_PRINT_NAME: &str = "ngx::print"; pub const NGX_SAY_NAME: &str = "ngx::say"; @@ -1804,6 +1818,72 @@ fn request_args_result_schema() -> HostTypeSchema { ) } +fn optional_string() -> HostTypeSchema { + HostTypeSchema::Optional(Box::new(HostTypeSchema::String)) +} + +fn optional_unknown() -> HostTypeSchema { + HostTypeSchema::Optional(Box::new(HostTypeSchema::Unknown)) +} + +fn optional_number() -> HostTypeSchema { + HostTypeSchema::Optional(Box::new(HostTypeSchema::Number)) +} + +fn shared_get_result_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "SharedGetResult", + vec![ + HostStructField::new("value", optional_unknown()), + HostStructField::new("flags", HostTypeSchema::Int), + HostStructField::new("stale", HostTypeSchema::Bool), + HostStructField::new("error", optional_string()), + ], + ) +} + +fn shared_store_result_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "SharedStoreResult", + vec![ + HostStructField::new("ok", HostTypeSchema::Bool), + HostStructField::new("error", optional_string()), + HostStructField::new("forcible", HostTypeSchema::Bool), + ], + ) +} + +fn shared_safe_store_result_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "SharedSafeStoreResult", + vec![ + HostStructField::new("ok", HostTypeSchema::Bool), + HostStructField::new("error", optional_string()), + ], + ) +} + +fn shared_incr_result_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "SharedIncrResult", + vec![ + HostStructField::new("value", optional_number()), + HostStructField::new("error", optional_string()), + HostStructField::new("forcible", HostTypeSchema::Bool), + ], + ) +} + +fn shared_number_result_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "SharedNumberResult", + vec![ + HostStructField::new("value", optional_number()), + HostStructField::new("error", optional_string()), + ], + ) +} + fn dict_handle_param() -> HostParamSchema { HostParamSchema::with_passing( "dict", @@ -1819,7 +1899,7 @@ fn dict_get_schema(name: &'static str) -> HostFunctionSchema { dict_handle_param(), HostParamSchema::value("key", HostTypeSchema::String), ], - HostTypeSchema::Unknown, + shared_get_result_schema(), ) } @@ -1833,7 +1913,21 @@ fn dict_store_schema(name: &'static str) -> HostFunctionSchema { HostParamSchema::value("exptime", HostTypeSchema::Unknown), HostParamSchema::value("flags", HostTypeSchema::Unknown), ], - HostTypeSchema::Unknown, + shared_store_result_schema(), + ) +} + +fn dict_safe_store_schema(name: &'static str) -> HostFunctionSchema { + HostFunctionSchema::with_return( + name, + vec![ + dict_handle_param(), + HostParamSchema::value("key", HostTypeSchema::String), + HostParamSchema::value("value", HostTypeSchema::Unknown), + HostParamSchema::value("exptime", HostTypeSchema::Unknown), + HostParamSchema::value("flags", HostTypeSchema::Unknown), + ], + shared_safe_store_result_schema(), ) } @@ -2127,9 +2221,9 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { NGX_SHARED_GET_NAME => dict_get_schema(NGX_SHARED_GET_NAME), NGX_SHARED_GET_STALE_NAME => dict_get_schema(NGX_SHARED_GET_STALE_NAME), NGX_SHARED_SET_NAME => dict_store_schema(NGX_SHARED_SET_NAME), - NGX_SHARED_SAFE_SET_NAME => dict_store_schema(NGX_SHARED_SAFE_SET_NAME), + NGX_SHARED_SAFE_SET_NAME => dict_safe_store_schema(NGX_SHARED_SAFE_SET_NAME), NGX_SHARED_ADD_NAME => dict_store_schema(NGX_SHARED_ADD_NAME), - NGX_SHARED_SAFE_ADD_NAME => dict_store_schema(NGX_SHARED_SAFE_ADD_NAME), + NGX_SHARED_SAFE_ADD_NAME => dict_safe_store_schema(NGX_SHARED_SAFE_ADD_NAME), NGX_SHARED_REPLACE_NAME => dict_store_schema(NGX_SHARED_REPLACE_NAME), NGX_SHARED_DELETE_NAME => HostFunctionSchema::with_return( NGX_SHARED_DELETE_NAME, @@ -2148,7 +2242,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("init", HostTypeSchema::Unknown), HostParamSchema::value("init_ttl", HostTypeSchema::Unknown), ], - HostTypeSchema::Unknown, + shared_incr_result_schema(), ), NGX_SHARED_TTL_NAME => HostFunctionSchema::with_return( NGX_SHARED_TTL_NAME, @@ -2156,7 +2250,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { dict_handle_param(), HostParamSchema::value("key", HostTypeSchema::String), ], - HostTypeSchema::Unknown, + shared_number_result_schema(), ), NGX_SHARED_EXPIRE_NAME => HostFunctionSchema::with_return( NGX_SHARED_EXPIRE_NAME, @@ -2165,7 +2259,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("key", HostTypeSchema::String), HostParamSchema::value("exptime", HostTypeSchema::Unknown), ], - HostTypeSchema::Unknown, + shared_safe_store_result_schema(), ), NGX_SHARED_CAPACITY_NAME => HostFunctionSchema::with_return( NGX_SHARED_CAPACITY_NAME, @@ -2184,7 +2278,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("key", HostTypeSchema::String), HostParamSchema::value("value", HostTypeSchema::Unknown), ], - HostTypeSchema::Unknown, + shared_number_result_schema(), ), NGX_SHARED_RPUSH_NAME => HostFunctionSchema::with_return( NGX_SHARED_RPUSH_NAME, @@ -2193,11 +2287,18 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("key", HostTypeSchema::String), HostParamSchema::value("value", HostTypeSchema::Unknown), ], - HostTypeSchema::Unknown, + shared_number_result_schema(), ), NGX_SHARED_LPOP_NAME => dict_get_schema(NGX_SHARED_LPOP_NAME), NGX_SHARED_RPOP_NAME => dict_get_schema(NGX_SHARED_RPOP_NAME), - NGX_SHARED_LLEN_NAME => dict_get_schema(NGX_SHARED_LLEN_NAME), + NGX_SHARED_LLEN_NAME => HostFunctionSchema::with_return( + NGX_SHARED_LLEN_NAME, + vec![ + dict_handle_param(), + HostParamSchema::value("key", HostTypeSchema::String), + ], + shared_number_result_schema(), + ), NGX_SHARED_FLUSH_ALL_NAME => HostFunctionSchema::with_return( NGX_SHARED_FLUSH_ALL_NAME, vec![dict_handle_param()], diff --git a/src/host/semaphore.rs b/src/host/semaphore.rs index 70ead2f..025d7ca 100644 --- a/src/host/semaphore.rs +++ b/src/host/semaphore.rs @@ -8,7 +8,7 @@ use pd_host_function::pd_host_function; use vm::HostModuleDescriptor; -use vm::{Value, Vm}; +use vm::{ResourceRef, Value, Vm}; use super::super::VmResult; use super::{CallOutcome, IntoHostCallOutcome}; @@ -39,7 +39,9 @@ fn named(host: &str, error: vm::VmError) -> vm::VmError { /// /// `count` must be an integer in `0..=MAX_SEMAPHORE_COUNT`. The handle lives in /// the **current** VM resource table; the permit counter and waiter queue live -/// in the worker registry. +/// in the worker registry. The guest catalog type is `Resource(ngx.semaphore)`; +/// the constructor still returns the raw guest token because `push_resource` +/// requires `&mut Vm`. #[pd_host_function(name = "ngx::semaphore::new", contract = crate::host::catalog::ngx_semaphore_new_contract)] pub fn new(vm: &mut Vm, count: i64) -> VmResult { let count = semaphore::parse_new_count(count, NGX_SEMAPHORE_NEW_NAME)?; @@ -56,7 +58,9 @@ pub fn new(vm: &mut Vm, count: i64) -> VmResult { /// `timeout` is a number of seconds (fractions allowed); `null` waits without /// a timer and `0` performs an immediate acquisition attempt. The generated /// wrapper submits an owned host future; this body only awaits worker-owned -/// state and the owned timeout. +/// state and the owned timeout. Slot 0 stays a raw guest token because +/// `ResourceRef` cannot cross the async suspension boundary; the catalog +/// contract still declares `Resource(ngx.semaphore)` + Borrow. #[pd_host_function(name = "ngx::semaphore::wait", contract = crate::host::catalog::ngx_semaphore_wait_contract)] pub async fn wait( #[pd_host_context] context: SemaphoreWaitContext, @@ -68,10 +72,9 @@ pub async fn wait( /// Adds `count` permits (`null` means one) and wakes FIFO waiters. #[pd_host_function(name = "ngx::semaphore::post", contract = crate::host::catalog::ngx_semaphore_post_contract)] -pub fn post(vm: &mut Vm, sem: i64, count: Value) -> VmResult { +pub fn post(sem: ResourceRef<'_, SemaphoreHandle>, count: Value) -> VmResult { let amount = semaphore::parse_post_amount(&count, NGX_SEMAPHORE_POST_NAME)?; - let state = semaphore::resolve_handle(vm, sem, NGX_SEMAPHORE_POST_NAME)?; - state + sem.state() .post(amount) .map_err(|error| named(NGX_SEMAPHORE_POST_NAME, error))?; ret(()) @@ -79,10 +82,9 @@ pub fn post(vm: &mut Vm, sem: i64, count: Value) -> VmResult { /// Returns the current count: permits ready minus queued waiters. #[pd_host_function(name = "ngx::semaphore::count", contract = crate::host::catalog::ngx_semaphore_count_contract)] -pub fn count(vm: &mut Vm, sem: i64) -> VmResult { - let state = semaphore::resolve_handle(vm, sem, NGX_SEMAPHORE_COUNT_NAME)?; +pub fn count(sem: ResourceRef<'_, SemaphoreHandle>) -> VmResult { ret( - state + sem.state() .count() .map_err(|error| named(NGX_SEMAPHORE_COUNT_NAME, error))?, ) diff --git a/src/host/shared_dict.rs b/src/host/shared_dict.rs index 8b0d769..20830a6 100644 --- a/src/host/shared_dict.rs +++ b/src/host/shared_dict.rs @@ -9,7 +9,10 @@ use std::sync::Arc; use pd_host_function::pd_host_function; use vm::HostModuleDescriptor; use vm::resource::HostResource; -use vm::{HostResourceType, ResourceRef, ResourceTypeKey, Value, Vm, VmError, VmMap}; +use vm::{ + HostNamedStruct, HostResourceType, HostStructField, HostTypeSchema, ResourceRef, + ResourceTypeKey, Value, Vm, VmError, VmMap, +}; use super::{CallOutcome, IntoHostCallOutcome}; use super::super::VmResult; @@ -70,6 +73,31 @@ impl From for SharedGetResult { } } +fn optional_string_schema() -> HostTypeSchema { + HostTypeSchema::Optional(Box::new(HostTypeSchema::String)) +} + +fn optional_unknown_schema() -> HostTypeSchema { + HostTypeSchema::Optional(Box::new(HostTypeSchema::Unknown)) +} + +fn optional_number_schema() -> HostTypeSchema { + HostTypeSchema::Optional(Box::new(HostTypeSchema::Number)) +} + +impl HostNamedStruct for SharedGetResult { + const NAME: &'static str = "SharedGetResult"; + + fn host_struct_fields() -> Vec { + vec![ + HostStructField::new("value", optional_unknown_schema()), + HostStructField::new("flags", HostTypeSchema::Int), + HostStructField::new("stale", HostTypeSchema::Bool), + HostStructField::new("error", optional_string_schema()), + ] + } +} + impl IntoHostCallOutcome for SharedGetResult { fn into_host_call_outcome(self) -> CallOutcome { struct_outcome([ @@ -98,6 +126,18 @@ impl From for SharedStoreResult { } } +impl HostNamedStruct for SharedStoreResult { + const NAME: &'static str = "SharedStoreResult"; + + fn host_struct_fields() -> Vec { + vec![ + HostStructField::new("ok", HostTypeSchema::Bool), + HostStructField::new("error", optional_string_schema()), + HostStructField::new("forcible", HostTypeSchema::Bool), + ] + } +} + impl IntoHostCallOutcome for SharedStoreResult { fn into_host_call_outcome(self) -> CallOutcome { struct_outcome([ @@ -123,6 +163,17 @@ impl From for SharedSafeStoreResult { } } +impl HostNamedStruct for SharedSafeStoreResult { + const NAME: &'static str = "SharedSafeStoreResult"; + + fn host_struct_fields() -> Vec { + vec![ + HostStructField::new("ok", HostTypeSchema::Bool), + HostStructField::new("error", optional_string_schema()), + ] + } +} + impl IntoHostCallOutcome for SharedSafeStoreResult { fn into_host_call_outcome(self) -> CallOutcome { struct_outcome([ @@ -149,6 +200,18 @@ impl From for SharedIncrResult { } } +impl HostNamedStruct for SharedIncrResult { + const NAME: &'static str = "SharedIncrResult"; + + fn host_struct_fields() -> Vec { + vec![ + HostStructField::new("value", optional_number_schema()), + HostStructField::new("error", optional_string_schema()), + HostStructField::new("forcible", HostTypeSchema::Bool), + ] + } +} + impl IntoHostCallOutcome for SharedIncrResult { fn into_host_call_outcome(self) -> CallOutcome { struct_outcome([ @@ -183,6 +246,17 @@ impl From for SharedNumberResult { } } +impl HostNamedStruct for SharedNumberResult { + const NAME: &'static str = "SharedNumberResult"; + + fn host_struct_fields() -> Vec { + vec![ + HostStructField::new("value", optional_number_schema()), + HostStructField::new("error", optional_string_schema()), + ] + } +} + impl IntoHostCallOutcome for SharedNumberResult { fn into_host_call_outcome(self) -> CallOutcome { struct_outcome([("value", self.value), ("error", error_value(self.error))]) @@ -291,6 +365,9 @@ fn zone_op( } /// Fetch a typed handle for the named `rss_shared_dict` zone. +/// +/// The guest catalog type is `Resource(ngx.shared.dict)`; the constructor still +/// returns the raw guest token because `push_resource` requires `&mut Vm`. #[pd_host_function(name = "ngx::shared::dict", contract = crate::host::catalog::ngx_shared_dict_contract)] fn dict(vm: &mut Vm, name: String) -> VmResult { let handle = shared_dict::open_handle(&name).map_err(|err| { From 3735344a3e39a3be5f6cfb6f0af22367c3efe8a5 Mon Sep 17 00:00:00 2001 From: fffonion Date: Thu, 17 Sep 2026 16:22:58 +0800 Subject: [PATCH 6/6] test(host): cover named catalog, typed handles, and worker readiness Guest scripts use host named types. Harness waits for worker HTTP after registry install instead of TCP accept. UDP echo starts after the driver is ready so cold-registry bind cannot consume the receive timeout. --- docs/api/shared-dict.md | 22 +- docs/host-descriptors.md | 55 ++--- docs/ngx-api-compatibility.md | 4 +- tests/access_async.rs | 57 +++-- tests/access_nginx.rs | 13 +- tests/body_hooks.rs | 11 +- tests/content_nginx.rs | 57 +++-- tests/example_key_auth.rs | 15 +- tests/example_rate_limit.rs | 19 +- tests/fixtures/task7_mutation.rss | 4 - tests/host_catalog.rs | 317 +++++++++++++++++++--------- tests/host_descriptor_arch.rs | 113 ++++++++++ tests/host_descriptor_handles.rs | 328 +++++++++++++++++++++++++++++ tests/host_descriptor_install.rs | 295 +++++++++++++++++++++++++- tests/lifecycle.rs | 11 +- tests/log_hooks.rs | 11 +- tests/log_nginx.rs | 11 +- tests/ngx_control_flow.rs | 11 +- tests/ngx_meta.rs | 11 +- tests/ngx_request_body.rs | 15 +- tests/ngx_request_body_mutation.rs | 18 +- tests/ngx_request_mutation.rs | 17 +- tests/ngx_request_read.rs | 39 ++-- tests/ngx_scheduler_contract.rs | 139 ++++-------- tests/ngx_semaphore.rs | 22 +- tests/ngx_shared_dict.rs | 22 +- tests/ngx_shared_dict_list.rs | 20 +- tests/ngx_tcp_keepalive.rs | 2 +- tests/ngx_tcp_socket.rs | 2 +- tests/ngx_timer.rs | 2 +- tests/ngx_udp_socket.rs | 16 +- tests/output_nginx.rs | 11 +- tests/shared_dict_nginx.rs | 119 ++++------- tests/support/nginx.rs | 94 +++++++++ tests/support/nginx_harness.rs | 45 +++- tests/timer_nginx.rs | 40 +--- tests/worker_runtime.rs | 11 +- 37 files changed, 1384 insertions(+), 615 deletions(-) create mode 100644 tests/host_descriptor_handles.rs diff --git a/docs/api/shared-dict.md b/docs/api/shared-dict.md index 182b977..7787211 100644 --- a/docs/api/shared-dict.md +++ b/docs/api/shared-dict.md @@ -8,11 +8,11 @@ bytes. Duplicate names (same or different size) are rejected. The zone is NGINX shared memory with a slab-backed rbtree/LRU store, the `init(shm_zone, old_data)` reload contract, and a cross-worker mutex. `ngx::shared::dict(name)` returns a typed handle; later methods take -that handle. Multi-return OpenResty values use fixed result structs at the -RustScript boundary. The current host catalog exposes these return slots as -`unknown`, so callers declare the matching struct and annotate the assignment; -field access then uses `.value` / `.flags` / `.stale` / `.ok` / `.error` / -`.forcible`. The VM's internal carrier is not part of the script-facing type. +that handle. Multi-return OpenResty values use host-catalog named structs +(`SharedGetResult`, `SharedStoreResult`, `SharedSafeStoreResult`, +`SharedIncrResult`, `SharedNumberResult`). Do not redeclare them in +guest scripts. `SharedGetResult.value` is `unknown?` because stored +values are heterogeneous; numeric results use `number?`. Truly dynamic-key collections remain `map` fields inside typed envelopes, such as `DecodedArgsResult.args` and `RequestHeadersResult.headers`. Optional `exptime` and `flags` are explicit `null`. Values are bool, int, @@ -22,8 +22,8 @@ null. The shared-dict result shapes are: ```rss -struct SharedGetResult { - value: T?, +struct SharedGetResult { + value: unknown?, flags: int, stale: bool, error: string? @@ -69,8 +69,8 @@ request-owned VMs. | OpenResty API | RustScript callable | Status | Phases | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | | `ngx.shared.DICT` | `ngx::shared::dict(name: string) -> resource` | adapted | `PHASE_DICT_LOOKUP` | unknown zone is a host error | Typed `ngx.shared.dict` handle, not `ngx.shared.NAME` field | `tests/ngx_shared_dict.rs` | -| `ngx.shared.DICT.get` | `ngx::shared::get(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_GET` | missing → `{value: null}` | Fields `value`, `flags`, `stale`, `error` | `tests/ngx_shared_dict.rs` | -| `ngx.shared.DICT.get_stale` | `ngx::shared::get_stale(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_GET` | stale expired values | `stale` is bool | `tests/ngx_shared_dict.rs` | +| `ngx.shared.DICT.get` | `ngx::shared::get(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_GET` | missing → `{value: null}` | Fields `value`, `flags`, `stale`, `error` | `tests/ngx_shared_dict.rs` | +| `ngx.shared.DICT.get_stale` | `ngx::shared::get_stale(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_GET` | stale expired values | `stale` is bool | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.set` | `ngx::shared::set(dict, key, value, exptime, flags) -> SharedStoreResult` | adapted | `PHASE_DICT_WRITE` | LRU up to 30 | Fields `ok`, `error`, `forcible` | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.safe_set` | `ngx::shared::safe_set(dict, key, value, exptime, flags) -> SharedSafeStoreResult` | adapted | `PHASE_DICT_WRITE` | no eviction | Fields `ok`, `error` | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.add` | `ngx::shared::add(dict, key, value, exptime, flags) -> SharedStoreResult` | adapted | `PHASE_DICT_WRITE` | exists → error | Fields `ok`, `error`, `forcible` | `tests/ngx_shared_dict.rs` | @@ -84,8 +84,8 @@ request-owned VMs. | `ngx.shared.DICT.free_space` | `ngx::shared::free_space(dict) -> int` | adapted | `PHASE_DICT_WRITE` | approximate | Process-local: capacity − used; shm: `pfree * pagesize` | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.lpush` | `ngx::shared::lpush(dict, key, value) -> SharedNumberResult` | adapted | `PHASE_DICT_WRITE` | list len ≤ 65536; no LRU eviction | Fields `value`, `error`; bool/null/map → `bad value type`; scalar conflict → `value not a list` without mutating the node | `tests/ngx_shared_dict_list.rs` | | `ngx.shared.DICT.rpush` | `ngx::shared::rpush(dict, key, value) -> SharedNumberResult` | adapted | `PHASE_DICT_WRITE` | same | Same result type as lpush; appends | `tests/ngx_shared_dict_list.rs` | -| `ngx.shared.DICT.lpop` | `ngx::shared::lpop(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_WRITE` | decode before unlink | Fields `value`, `flags`, `stale`, `error`; empty/missing → `{value: null}`; decode failure → `{error: "bad value type"}` and the item stays queued | `tests/ngx_shared_dict_list.rs` | -| `ngx.shared.DICT.rpop` | `ngx::shared::rpop(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_WRITE` | same | Same result type as lpop; pops tail | `tests/ngx_shared_dict_list.rs` | +| `ngx.shared.DICT.lpop` | `ngx::shared::lpop(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_WRITE` | decode before unlink | Fields `value`, `flags`, `stale`, `error`; empty/missing → `{value: null}`; decode failure → `{error: "bad value type"}` and the item stays queued | `tests/ngx_shared_dict_list.rs` | +| `ngx.shared.DICT.rpop` | `ngx::shared::rpop(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_WRITE` | same | Same result type as lpop; pops tail | `tests/ngx_shared_dict_list.rs` | | `ngx.shared.DICT.llen` | `ngx::shared::llen(dict, key) -> SharedNumberResult` | adapted | `PHASE_DICT_WRITE` | — | Missing/expired → `{value: 0}`; scalar → `value not a list` | `tests/ngx_shared_dict_list.rs` | | `ngx.shared.DICT.flush_all` | `ngx::shared::flush_all(dict) -> null` | adapted | `PHASE_DICT_WRITE` | LRU; ≤1024 visits/call | Each call first frees already-expired keys (visit-capped), then marks live LRU keys with `expires=1` (visit-capped), then `expire(0)`. Repeated calls make monotonic progress across >1024 keys; one call does not wipe the zone. | `tests/ngx_shared_dict_list.rs` | | `ngx.shared.DICT.flush_expired` | `ngx::shared::flush_expired(dict, max) -> int` | adapted | `PHASE_DICT_WRITE` | `max` 0/null still ≤ 1024 results; visits ≤ 1024 | Freed count. Walk stops at the result cap *or* the hard visit cap, whichever comes first. OpenResty `0` is unlimited. | `tests/ngx_shared_dict_list.rs` | diff --git a/docs/host-descriptors.md b/docs/host-descriptors.md index add2a5c..bc21a13 100644 --- a/docs/host-descriptors.md +++ b/docs/host-descriptors.md @@ -15,10 +15,10 @@ adapter, guest resource effects, and private state. 4. Install through `HostModuleDescriptor::install_from_catalog`. Do not add parallel `HostApiBuilder::function` / `register_exact_*` loops. -Timer `at`/`every` use `HostAdapterDescriptor::Owned` so callback -transfer stays on the owned adapter path. `bind_ngx_hosts` remains a -name-binding compatibility path for tests that do not need owned timer -dispatch; it does not install those adapters. +Timer `at`/`every` use `HostAdapterDescriptor::Owned`. Request, pool, and +timer VMs install through `ngx_host_registry()` (restricted transactional +exact catalog). `bind_ngx_hosts` is a thin wrapper around that registry +and is not a second name/arity binding authority. ## Typed resources @@ -34,8 +34,15 @@ Handle types implement `HostResourceType` plus `HostResource`: | `ClientCert` | `ngx.socket.tls.clientcert` | Guest resource effects follow descriptor passing modes -(borrow / borrow-mut / take / create). Raw NGINX C ABI integers that -are not VM resources stay out of `Resource`. +(borrow / borrow-mut / take / create). Request-local `ngx::ctx` is not a +VM resource: payloads stay request-owned maps. + +Sync methods that do not need `&mut Vm` take `ResourceRef` (shared-dict +ops, `ngx::semaphore::{post,count}`). Constructors still return a raw +guest token because `push_resource` requires `&mut Vm`. Async methods and +`close` keep a raw guest token: `ResourceRef` cannot cross suspension, +and close cannot combine `&mut Vm` with a borrowed resource. Those +slots still advertise `Resource` + passing mode on the catalog contract. ## Named structs @@ -47,28 +54,26 @@ Fixed envelopes are catalog `HostTypeSchema::Named` and Rust | `DecodedArgsResult` | `ngx::decode_args` | | `RequestHeadersResult` | `ngx::req::get_headers` | | `RequestArgsResult` | `ngx::req::get_uri_args`, `ngx::req::get_post_args` | +| `SharedGetResult` | `ngx::shared::{get,get_stale,lpop,rpop}` | +| `SharedStoreResult` | `ngx::shared::{set,add,replace}` | +| `SharedSafeStoreResult` | `ngx::shared::{safe_set,safe_add,expire}` | +| `SharedIncrResult` | `ngx::shared::incr` | +| `SharedNumberResult` | `ngx::shared::{ttl,lpush,rpush,llen}` | -This named-envelope conversion is a catalog fingerprint change versus -the previous `Unknown` returns. Guest scripts that already named those -structs keep compiling; inner `args`/`headers` maps stay dynamic. - -Shared-dictionary multi-returns stay `Unknown` so guest code can bind -`SharedGetResult` and the other generic result structs. That family -is an intentional dynamic exception, not a `Map(Unknown)` catalog type. +Guest scripts use the host named types. Inner `value` fields on shared +results stay `Unknown` because stored dictionary values are heterogeneous. ## Intentional dynamic slots -These positions stay `HostTypeSchema::Unknown` because OpenResty accepts -`null` or several runtime shapes at the same argument: +Positions that stay `HostTypeSchema::Unknown` or `Map(Unknown)` are +listed exactly in `tests/host_catalog.rs` +(`intentional_dynamic_slots_match_the_allowlist`). Typical reasons: -- optional trailing arguments (`max_args`, `max_headers`, `raw`, - `no_padding`, `escape_type`, `timeout`, `exptime`, `flags`, `jump`, - `auto_clean`, `init`, `init_ttl`, `count`, `max`, `host`, `data`, - `value` where OpenResty is untyped) -- open dictionaries (`ngx::encode_args` `args`, header-set maps, - request ctx payloads) -- shared-dictionary result carriers (generic `value` / `ok` / `error` - envelopes) +- optional trailing OpenResty arguments (`null` or several runtime shapes) +- open dictionaries (`ngx::encode_args` `args`, header-set maps, ctx payloads) +- heterogeneous stored shared-dict values and `ngx::header::get` (string, + string array, or null) -Do not add new `Unknown` or `Map(Unknown)` returns for a fixed outer -shape. Add a named struct and a catalog test instead. +Do not add a new `Unknown` or `Map(Unknown)` return for a fixed outer +shape. Add a named struct, bump `NGX_HOST_CATALOG_SCHEMA_VERSION`, and +update the fingerprint goldens. diff --git a/docs/ngx-api-compatibility.md b/docs/ngx-api-compatibility.md index fd2c841..79a6b11 100644 --- a/docs/ngx-api-compatibility.md +++ b/docs/ngx-api-compatibility.md @@ -70,8 +70,8 @@ struct RequestArgsResult { truncated: bool } -struct SharedGetResult { - value: T?, +struct SharedGetResult { + value: unknown?, flags: int, stale: bool, error: string? diff --git a/tests/access_async.rs b/tests/access_async.rs index 2f9fd16..eddfd59 100644 --- a/tests/access_async.rs +++ b/tests/access_async.rs @@ -70,40 +70,57 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_get(port: u16, path: &str) -> (u16, String) { - let mut stream = TcpStream::connect(("127.0.0.1", port)).unwrap(); + try_http_get(port, path, Duration::from_secs(5)).unwrap_or_else(|err| panic!("{err}")) +} + +fn try_http_get(port: u16, path: &str, timeout: Duration) -> Result<(u16, String), String> { + let mut stream = TcpStream::connect(("127.0.0.1", port)) + .map_err(|err| format!("connect 127.0.0.1:{port}: {err}"))?; stream - .set_read_timeout(Some(Duration::from_secs(5))) - .unwrap(); + .set_read_timeout(Some(timeout)) + .map_err(|err| format!("read timeout: {err}"))?; stream - .set_write_timeout(Some(Duration::from_secs(5))) - .unwrap(); + .set_write_timeout(Some(timeout)) + .map_err(|err| format!("write timeout: {err}"))?; write!( stream, "GET {path} HTTP/1.0\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n" ) - .unwrap(); + .map_err(|err| format!("write: {err}"))?; let _ = stream.flush(); let mut body = String::new(); - stream.read_to_string(&mut body).unwrap(); + stream + .read_to_string(&mut body) + .map_err(|err| format!("read: {err}"))?; let status = body .split_whitespace() .nth(1) .and_then(|s| s.parse().ok()) .unwrap_or(0); - (status, body) + Ok((status, body)) +} + +fn wait_for_http_ok(port: u16, path: &str, timeout: Duration) { + let start = Instant::now(); + loop { + match try_http_get(port, path, Duration::from_secs(2)) { + Ok((200, _)) => return, + other => { + let last = match other { + Ok((status, body)) => format!("HTTP {status}: {body}"), + Err(err) => err, + }; + if start.elapsed() >= timeout { + panic!("nginx worker did not serve {path} within {timeout:?}: {last}"); + } + } + } + std::thread::sleep(Duration::from_millis(20)); + } } fn http_body(response: &str) -> &str { @@ -318,6 +335,10 @@ fn synchronous_route_remains_unchanged() { let _child = spawn_nginx(&nginx, &prefix); wait_for_port(port, Duration::from_secs(5)); + // Master can listen before worker `init_process` finishes installing the + // host registry. Probe static content so the latency assertion measures + // the request path, not worker start. + wait_for_http_ok(port, "/plain/", Duration::from_secs(15)); let started = Instant::now(); let (status, response) = http_get(port, "/plain/"); diff --git a/tests/access_nginx.rs b/tests/access_nginx.rs index 000c29f..d5701c4 100644 --- a/tests/access_nginx.rs +++ b/tests/access_nginx.rs @@ -7,7 +7,7 @@ use std::io::{Read, Write}; use std::net::{TcpListener, TcpStream}; use std::path::{Path, PathBuf}; use std::process::{Child, Stdio}; -use std::time::{Duration, Instant}; +use std::time::Duration; use support::artifact::module_cdylib; use support::nginx::{ @@ -100,16 +100,7 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_get(port: u16, path: &str) -> (u16, String) { diff --git a/tests/body_hooks.rs b/tests/body_hooks.rs index a06be64..4e2d12b 100644 --- a/tests/body_hooks.rs +++ b/tests/body_hooks.rs @@ -89,16 +89,7 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() > timeout { - panic!("nginx did not listen on {port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn inject_main_env(conf: &str, vars: &[(&str, &str)]) -> String { diff --git a/tests/content_nginx.rs b/tests/content_nginx.rs index 4fc7dd1..f724b0e 100644 --- a/tests/content_nginx.rs +++ b/tests/content_nginx.rs @@ -78,40 +78,57 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_get(port: u16, path: &str) -> (u16, String) { - let mut stream = TcpStream::connect(("127.0.0.1", port)).unwrap(); + try_http_get(port, path, Duration::from_secs(5)).unwrap_or_else(|err| panic!("{err}")) +} + +fn try_http_get(port: u16, path: &str, timeout: Duration) -> Result<(u16, String), String> { + let mut stream = TcpStream::connect(("127.0.0.1", port)) + .map_err(|err| format!("connect 127.0.0.1:{port}: {err}"))?; stream - .set_read_timeout(Some(Duration::from_secs(5))) - .unwrap(); + .set_read_timeout(Some(timeout)) + .map_err(|err| format!("read timeout: {err}"))?; stream - .set_write_timeout(Some(Duration::from_secs(5))) - .unwrap(); + .set_write_timeout(Some(timeout)) + .map_err(|err| format!("write timeout: {err}"))?; write!( stream, "GET {path} HTTP/1.0\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n" ) - .unwrap(); + .map_err(|err| format!("write: {err}"))?; let _ = stream.flush(); let mut body = String::new(); - stream.read_to_string(&mut body).unwrap(); + stream + .read_to_string(&mut body) + .map_err(|err| format!("read: {err}"))?; let status = body .split_whitespace() .nth(1) .and_then(|s| s.parse().ok()) .unwrap_or(0); - (status, body) + Ok((status, body)) +} + +fn wait_for_http_ok(port: u16, path: &str, timeout: Duration) { + let start = Instant::now(); + loop { + match try_http_get(port, path, Duration::from_secs(2)) { + Ok((200, _)) => return, + other => { + let last = match other { + Ok((status, body)) => format!("HTTP {status}: {body}"), + Err(err) => err, + }; + if start.elapsed() >= timeout { + panic!("nginx worker did not serve {path} within {timeout:?}: {last}"); + } + } + } + std::thread::sleep(Duration::from_millis(20)); + } } fn http_body(response: &str) -> &str { @@ -685,6 +702,10 @@ fn content_sleep_does_not_block_a_concurrent_static_request() { ); let _child = spawn_nginx(&nginx, &prefix); wait_for_port(port, Duration::from_secs(5)); + // Master can listen before `init_process` finishes installing the host + // registry. Probe static content so the concurrency measurement starts + // after the worker is in its event loop, without warming the sleep script. + wait_for_http_ok(port, "/plain/", Duration::from_secs(15)); let slow = std::thread::spawn(move || { let start = Instant::now(); diff --git a/tests/example_key_auth.rs b/tests/example_key_auth.rs index 7cc2a53..138cdb0 100644 --- a/tests/example_key_auth.rs +++ b/tests/example_key_auth.rs @@ -192,16 +192,7 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_exchange(port: u16, request: &str) -> (u16, String) { @@ -455,8 +446,8 @@ fn assert_ok_200(label: &str, status: u16, response: &str, secrets: &[String]) { } #[test] -fn production_host_count_stays_195() { - assert_eq!(production_runtime_host_names().len(), 195); +fn production_host_count_stays_199() { + assert_eq!(production_runtime_host_names().len(), 199); } #[test] diff --git a/tests/example_rate_limit.rs b/tests/example_rate_limit.rs index a890b80..3f277a9 100644 --- a/tests/example_rate_limit.rs +++ b/tests/example_rate_limit.rs @@ -226,17 +226,8 @@ impl Keepalive { } } -fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - thread::sleep(Duration::from_millis(20)); - } +fn wait_for_port(port: u16, timeout: Duration, prefix: &Path) { + support::nginx::wait_for_workers_ready(port, timeout, Some(&prefix.join("logs/error.log"))); } fn error_log(tmp: &OwnedTempDir) -> String { @@ -357,7 +348,7 @@ fn setup(spec: HarnessSpec<'_>) -> (PathBuf, u16, OwnedTempDir, RunningNginx, Pa let out = nginx_test(&nginx, tmp.path()); assert!(out.status.success(), "nginx -t failed: {}", combined(&out)); let running = spawn_nginx(&nginx, tmp.path()); - wait_for_port(port, Duration::from_secs(5)); + wait_for_port(port, Duration::from_secs(5), tmp.path()); (nginx, port, tmp, running, script) } @@ -406,8 +397,6 @@ fn assert_documented_constants() { "allocating incr init must not create windows: {EXAMPLE_RSS}" ); for required in [ - "struct SharedSafeStoreResult", - "struct SharedIncrResult", "let added: SharedSafeStoreResult", "let counted: SharedIncrResult", "added.ok", @@ -861,7 +850,7 @@ fn reload_preserves_window_count() { "reload failed: {}", combined(&reload) ); - wait_for_port(port, Duration::from_secs(5)); + wait_for_port(port, Duration::from_secs(5), tmp.path()); let start = Instant::now(); let mut remaining = None; diff --git a/tests/fixtures/task7_mutation.rss b/tests/fixtures/task7_mutation.rss index 125786b..3f6b131 100644 --- a/tests/fixtures/task7_mutation.rss +++ b/tests/fixtures/task7_mutation.rss @@ -1,8 +1,4 @@ use ngx; -struct RequestHeadersResult { - headers: map, - truncated: bool -} ngx::req::set_method(ngx::http::post()); ngx::req::set_uri("/mutated", false); ngx::req::set_uri_args("k=v"); diff --git a/tests/host_catalog.rs b/tests/host_catalog.rs index c5f63a1..237c17a 100644 --- a/tests/host_catalog.rs +++ b/tests/host_catalog.rs @@ -5,7 +5,7 @@ use ngx_rustscript_nginx_test_stubs as _; -use std::collections::HashSet; +use std::collections::{BTreeSet, HashSet}; use std::sync::Arc; use ngx_rustscript::config::{ @@ -16,11 +16,12 @@ use ngx_rustscript::host::{ HostCapability, HostFamily, HostSyncKind, NGX_CRC32_LONG_NAME, NGX_CRC32_SHORT_NAME, NGX_DECODE_ARGS_NAME, NGX_DECODE_BASE64_NAME, NGX_DECODE_BASE64MIME_NAME, NGX_ENCODE_ARGS_NAME, NGX_ENCODE_BASE64_NAME, NGX_ESCAPE_URI_NAME, NGX_HEADER_GET_NAME, NGX_HMAC_SHA1_NAME, + NGX_HOST_CATALOG_FINGERPRINT, NGX_HOST_CATALOG_FINGERPRINT_V1, NGX_HOST_CATALOG_SCHEMA_VERSION, NGX_MD5_BIN_NAME, NGX_MD5_NAME, NGX_NOW_NAME, NGX_QUOTE_SQL_STR_NAME, NGX_SHA1_BIN_NAME, NGX_SLEEP_MS_NAME, NGX_SLEEP_NAME, NGX_STATUS_GET_NAME, NGX_TIME_NAME, NGX_UNESCAPE_URI_NAME, NGX_UPDATE_TIME_NAME, NgxPhase, StandardHostPolicy, bind_ngx_hosts, compile_host_catalog, host_allowed_in_phase, install_standard_runtime, is_standard_host_import, ngx_host_catalog, - ngx_host_manifest, production_runtime_host_names, + ngx_host_manifest, ngx_host_registry, production_runtime_host_names, }; use vm::{HostTypeSchema, Program, Vm}; @@ -396,37 +397,8 @@ fn shared_fixed_results_compile_as_explicit_struct_types() { r#" use ngx; - struct SharedGetResult { - value: T?, - flags: int, - stale: bool, - error: string? - } - - struct SharedStoreResult { - ok: bool, - error: string?, - forcible: bool - } - - struct SharedSafeStoreResult { - ok: bool, - error: string? - } - - struct SharedIncrResult { - value: number?, - error: string?, - forcible: bool - } - - struct SharedNumberResult { - value: number?, - error: string? - } - let dict = ngx::shared::dict("ratelimit"); - let got: SharedGetResult = ngx::shared::get(dict, "key"); + let got: SharedGetResult = ngx::shared::get(dict, "key"); if got.error == null && !got.stale { got.value; got.flags; @@ -509,29 +481,37 @@ fn dynamic_map_envelopes_are_not_declared_as_maps() { #[test] fn shared_fixed_results_are_not_declared_as_dynamic_maps() { let catalog = ngx_host_catalog(); - for name in [ - "ngx::shared::get", - "ngx::shared::get_stale", - "ngx::shared::set", - "ngx::shared::safe_set", - "ngx::shared::add", - "ngx::shared::safe_add", - "ngx::shared::replace", - "ngx::shared::incr", - "ngx::shared::ttl", - "ngx::shared::expire", - "ngx::shared::lpush", - "ngx::shared::rpush", - "ngx::shared::lpop", - "ngx::shared::rpop", - "ngx::shared::llen", + for (name, expected) in [ + ("ngx::shared::get", "SharedGetResult"), + ("ngx::shared::get_stale", "SharedGetResult"), + ("ngx::shared::set", "SharedStoreResult"), + ("ngx::shared::safe_set", "SharedSafeStoreResult"), + ("ngx::shared::add", "SharedStoreResult"), + ("ngx::shared::safe_add", "SharedSafeStoreResult"), + ("ngx::shared::replace", "SharedStoreResult"), + ("ngx::shared::incr", "SharedIncrResult"), + ("ngx::shared::ttl", "SharedNumberResult"), + ("ngx::shared::expire", "SharedSafeStoreResult"), + ("ngx::shared::lpush", "SharedNumberResult"), + ("ngx::shared::rpush", "SharedNumberResult"), + ("ngx::shared::lpop", "SharedGetResult"), + ("ngx::shared::rpop", "SharedGetResult"), + ("ngx::shared::llen", "SharedNumberResult"), ] { - let schema = catalog.function(name).expect("shared host schema"); - assert_eq!( - schema.return_type, - HostTypeSchema::Unknown, - "{name} has a fixed result shape and must use an explicit RustScript struct type" - ); + match &catalog + .function(name) + .expect("shared host schema") + .return_type + { + HostTypeSchema::Named { + name: struct_name, .. + } => { + assert_eq!(struct_name, expected, "{name} named envelope"); + } + other => panic!( + "{name} has a fixed result shape and must use an explicit RustScript struct type, got {other:?}" + ), + } } } @@ -551,8 +531,8 @@ fn runtime_host_names_are_unique() { ); assert_eq!( names.len(), - 195, - "production runtime host count must stay 195" + 199, + "production runtime host count must stay 199" ); } @@ -579,25 +559,10 @@ fn manifest_catalog_and_runtime_names_correspond() { manifest_names.len(), "catalog must not declare overloads for production ngx hosts" ); - // The owned `ngx::timer::{at,every}` registrations have no name-binding API - // (they transfer their callback through `HostParamPassing::TakeOwned`), so - // the four timer hosts resolve through the timer-host registry - // (`ngx_host_registry`) instead of the per-name bind list. Every other - // manifest entry keeps a 1:1 per-name runtime binding. - let (timer_names, per_name_expected): (Vec<&str>, Vec<&str>) = manifest_names - .iter() - .copied() - .partition(|name| name.starts_with("ngx::timer::")); - assert_eq!(timer_names.len(), 4, "the timer family has four hosts"); assert_eq!( runtime_names, - per_name_expected.as_slice(), - "per-name runtime bindings must match the manifest minus the timer registry family" - ); - assert_eq!( - runtime_names.len() + timer_names.len(), - manifest_names.len(), - "per-name bindings plus the timer family must cover the manifest" + manifest_names.as_slice(), + "production runtime names must match the catalog/manifest order exactly" ); } @@ -805,15 +770,23 @@ fn catalog_sleep_ms_schema_preserves_millisecond_int() { #[test] fn bound_vm_has_every_catalogued_runtime_host_and_no_test_hosts() { - let program = compile("let x = 1;\n"); - let mut vm = Vm::try_new((*program).clone()).expect("construct vm"); - bind_ngx_hosts(&mut vm); + let registry = ngx_host_registry().expect("production registry"); for name in production_runtime_host_names() { assert!( - vm.has_bound_function(name), - "runtime VM must bind catalogued host {name}" + registry.contains_name(name), + "production registry must contain catalogued host {name}" + ); + } + for banned in ["ngx::probe", "ngx::fail", "ngx::ready"] { + assert!( + !registry.contains_name(banned), + "production registry must not install {banned}" ); } + + let program = compile("let x = 1;\n"); + let mut vm = Vm::try_new((*program).clone()).expect("construct vm"); + bind_ngx_hosts(&mut vm); for banned in [ "ngx::probe", "ngx::fail", @@ -826,10 +799,13 @@ fn bound_vm_has_every_catalogued_runtime_host_and_no_test_hosts() { "production bind must not install {banned}" ); } - assert!( - vm.has_bound_function(NGX_NOW_NAME), - "production bind must install ngx::now as the time host" - ); + + let now_program = compile_rss_source("use ngx;\nngx::now();\n").expect("ngx::now program"); + let mut now_vm = Vm::new_shared(now_program); + bind_ngx_hosts(&mut now_vm); + now_vm + .run() + .expect("production bind must execute imported ngx::now"); } #[test] @@ -1070,31 +1046,164 @@ fn nginx_version_allows_init_worker_and_exit_worker_unlike_nginx_configure() { )); } -#[test] -fn intentional_dynamic_unknown_slots_are_allowlisted() { +fn collect_dynamic_slots(ty: &HostTypeSchema, path: &str, out: &mut BTreeSet) { + match ty { + HostTypeSchema::Unknown => { + out.insert(format!("{path}:Unknown")); + } + HostTypeSchema::Map(inner) => { + out.insert(format!("{path}:Map")); + collect_dynamic_slots(inner, &format!("{path}."), out); + } + HostTypeSchema::Array(inner) => { + collect_dynamic_slots(inner, &format!("{path}[]"), out); + } + HostTypeSchema::Optional(inner) => { + collect_dynamic_slots(inner, &format!("{path}?"), out); + } + HostTypeSchema::Named { name, fields } => { + for field in fields { + collect_dynamic_slots(&field.ty, &format!("{path}.{name}.{}", field.name), out); + } + } + _ => {} + } +} + +fn catalog_dynamic_slots() -> BTreeSet { let catalog = ngx_host_catalog(); - let unknown_returns = [ - "ngx::shared::get", - "ngx::shared::get_stale", - "ngx::shared::set", - "ngx::shared::incr", - "ngx::ctx::get", - "ngx::encode_args", - ]; - for name in unknown_returns { - let schema = catalog.function(name).expect(name); - if name == "ngx::encode_args" { - assert_eq!( - schema.params[0].ty, - HostTypeSchema::Unknown, - "{name} args stay an open dictionary" + let mut slots = BTreeSet::new(); + for schema in catalog.functions() { + for param in &schema.params { + collect_dynamic_slots( + ¶m.ty, + &format!("{}#{}", schema.name, param.name), + &mut slots, ); - continue; } - assert_eq!( - schema.return_type, - HostTypeSchema::Unknown, - "{name} stays Unknown so guest generic/result structs keep compiling" + collect_dynamic_slots( + &schema.return_type, + &format!("{}#return", schema.name), + &mut slots, ); } + slots +} + +/// Exact per-function/slot allowlist for remaining Unknown/Map catalog types. +/// +/// Rationales: +/// - `header::get` return: OpenResty can yield a string or a multi-value array. +/// - request/codec maps: query/header bags are open-keyed. +/// - shared-dict `value`/store payloads: heterogeneous zone values. +/// - optional OpenResty trailing args (`exptime`, `flags`, `max_*`, timeouts). +/// - print/say/socket payloads: mixed printable or buffer values. +/// - request ctx: opaque per-request table, not a VM resource. +const ALLOWED_DYNAMIC_SLOTS: &[&str] = &[ + "ngx::ctx::delete#return:Unknown", + "ngx::ctx::get#return:Unknown", + "ngx::ctx::set#value:Unknown", + "ngx::decode_args#max_args:Unknown", + "ngx::decode_args#return.DecodedArgsResult.args.:Unknown", + "ngx::decode_args#return.DecodedArgsResult.args:Map", + "ngx::encode_args#args:Unknown", + "ngx::encode_base64#input:Unknown", + "ngx::encode_base64#no_padding:Unknown", + "ngx::escape_uri#escape_type:Unknown", + "ngx::header::get#return:Unknown", + "ngx::header::set#value:Unknown", + "ngx::header::set_all#headers:Unknown", + "ngx::print#data:Unknown", + "ngx::redirect#status:Unknown", + "ngx::req::get_headers#max_headers:Unknown", + "ngx::req::get_headers#raw:Unknown", + "ngx::req::get_headers#return.RequestHeadersResult.headers.:Unknown", + "ngx::req::get_headers#return.RequestHeadersResult.headers:Map", + "ngx::req::get_post_args#max_args:Unknown", + "ngx::req::get_post_args#return.RequestArgsResult.args.:Unknown", + "ngx::req::get_post_args#return.RequestArgsResult.args:Map", + "ngx::req::get_uri_args#max_args:Unknown", + "ngx::req::get_uri_args#return.RequestArgsResult.args.:Unknown", + "ngx::req::get_uri_args#return.RequestArgsResult.args:Map", + "ngx::req::init_body#buffer_size:Unknown", + "ngx::req::raw_header#no_request_line:Unknown", + "ngx::req::set_body_file#auto_clean:Unknown", + "ngx::req::set_header#value:Unknown", + "ngx::req::set_uri#jump:Unknown", + "ngx::req::set_uri_args#args:Unknown", + "ngx::say#data:Unknown", + "ngx::semaphore::post#count:Unknown", + "ngx::semaphore::wait#timeout:Unknown", + "ngx::shared::add#exptime:Unknown", + "ngx::shared::add#flags:Unknown", + "ngx::shared::add#value:Unknown", + "ngx::shared::expire#exptime:Unknown", + "ngx::shared::flush_expired#max:Unknown", + "ngx::shared::get#return.SharedGetResult.value?:Unknown", + "ngx::shared::get_keys#max:Unknown", + "ngx::shared::get_stale#return.SharedGetResult.value?:Unknown", + "ngx::shared::incr#init:Unknown", + "ngx::shared::incr#init_ttl:Unknown", + "ngx::shared::incr#value:Unknown", + "ngx::shared::lpop#return.SharedGetResult.value?:Unknown", + "ngx::shared::lpush#value:Unknown", + "ngx::shared::replace#exptime:Unknown", + "ngx::shared::replace#flags:Unknown", + "ngx::shared::replace#value:Unknown", + "ngx::shared::rpop#return.SharedGetResult.value?:Unknown", + "ngx::shared::rpush#value:Unknown", + "ngx::shared::safe_add#exptime:Unknown", + "ngx::shared::safe_add#flags:Unknown", + "ngx::shared::safe_add#value:Unknown", + "ngx::shared::safe_set#exptime:Unknown", + "ngx::shared::safe_set#flags:Unknown", + "ngx::shared::safe_set#value:Unknown", + "ngx::shared::set#exptime:Unknown", + "ngx::shared::set#flags:Unknown", + "ngx::shared::set#value:Unknown", + "ngx::socket::tcp::clientcert#cert:Unknown", + "ngx::socket::tcp::clientcert#pkey:Unknown", + "ngx::socket::tcp::receive#mode:Unknown", + "ngx::socket::tcp::receiveuntil#delimiter:Unknown", + "ngx::socket::tcp::send#data:Unknown", + "ngx::socket::tcp::setkeepalive#pool_size:Unknown", + "ngx::socket::tcp::setkeepalive#timeout:Unknown", + "ngx::socket::tcp::sslhandshake#reused_session:Unknown", + "ngx::socket::tcp::sslhandshake#send_status_req:Unknown", + "ngx::socket::tcp::sslhandshake#server_name:Unknown", + "ngx::socket::tcp::sslhandshake#ssl_verify:Unknown", + "ngx::socket::tcp::trustedstore#pem:Unknown", + "ngx::socket::udp::bind#host:Unknown", + "ngx::socket::udp::receive#size:Unknown", + "ngx::socket::udp::send#data:Unknown", + "ngx::var::set#value:Unknown", +]; + +#[test] +fn catalog_schema_version_and_fingerprint_goldens() { + let catalog = ngx_host_catalog(); + assert_eq!(NGX_HOST_CATALOG_SCHEMA_VERSION, 2); + assert_eq!(NGX_HOST_CATALOG_FINGERPRINT_V1, 0x5e37_8285_8fa0_b584); + assert_eq!( + catalog.fingerprint().as_u64(), + NGX_HOST_CATALOG_FINGERPRINT, + "catalog fingerprint drifted; bump schema version and goldens" + ); + assert_ne!( + NGX_HOST_CATALOG_FINGERPRINT, NGX_HOST_CATALOG_FINGERPRINT_V1, + "current fingerprint must differ from the v1 golden" + ); +} + +#[test] +fn exact_dynamic_unknown_and_map_slots_are_allowlisted() { + let actual = catalog_dynamic_slots(); + let expected: BTreeSet = ALLOWED_DYNAMIC_SLOTS + .iter() + .map(|slot| (*slot).to_string()) + .collect(); + assert_eq!( + actual, expected, + "Unknown/Map catalog slots drifted; update ALLOWED_DYNAMIC_SLOTS with rationale" + ); } diff --git a/tests/host_descriptor_arch.rs b/tests/host_descriptor_arch.rs index 59601a6..635fccf 100644 --- a/tests/host_descriptor_arch.rs +++ b/tests/host_descriptor_arch.rs @@ -166,3 +166,116 @@ fn handle_types_declare_host_resource_type() { "expected HostResourceType for dict/semaphore/tcp/udp/tls store/cert, found {impls}" ); } + +#[test] +fn production_has_one_descriptor_binding_authority() { + let host_mod = read("src/host/mod.rs"); + assert!( + !host_mod.contains("fn ngx_host_bindings("), + "production must not keep a parallel ngx_host_bindings table" + ); + assert!( + !host_mod.contains("enum NgxHostWrapper"), + "production must not keep a parallel NgxHostWrapper bind table" + ); + let build = extract_fn(&host_mod, "fn build_ngx_host_registry"); + let build_code: String = build + .lines() + .filter(|line| !line.trim_start().starts_with("//")) + .collect::>() + .join("\n"); + assert!( + build_code.contains("HostFunctionRegistry::empty()") + && build_code.contains("set_capability_profile") + && build_code.contains("transactionally"), + "ngx_host_registry must install from empty() plus an explicit capability profile:\n{build}" + ); + assert!( + !build_code.contains("HostFunctionRegistry::new()"), + "ngx_host_registry must not seed HTTP adapters from HostFunctionRegistry::new():\n{build}" + ); + assert!( + host_mod.contains("install_from_catalog"), + "ngx_host_registry must install exact catalog adapters" + ); + assert!( + host_mod.contains("authorize_registered_builtin_import") + || host_mod.contains("install_from_catalog"), + "restricted install must authorize catalogued hosts in the same transaction" + ); + + let bind_ngx = extract_fn(&host_mod, "fn bind_ngx_hosts"); + let bind_production = extract_fn(&host_mod, "fn bind_production_hosts"); + assert!( + (bind_ngx.contains("ngx_host_registry") && bind_ngx.contains("bind_vm_cached")) + || (bind_ngx.contains("bind_production_hosts") + && bind_production.contains("ngx_host_registry") + && bind_production.contains("bind_vm_cached")), + "bind_ngx_hosts must be a registry wrapper, not a name/arity bind loop:\n{bind_ngx}\n{bind_production}" + ); + assert!( + !bind_ngx.contains("bind_static_function") + && !bind_ngx.contains("bind_static_args_function") + && !bind_production.contains("bind_static_function") + && !bind_production.contains("bind_static_args_function"), + "bind_ngx_hosts must not name-bind wrappers:\n{bind_ngx}\n{bind_production}" + ); + + let request_state = read("src/request_state.rs"); + let bind_program = extract_fn(&request_state, "fn bind_program_hosts"); + assert!( + bind_program.contains("ngx_host_registry") + || bind_program.contains("bind_production_hosts") + || bind_program.contains("install_timer_vm") + || bind_program.contains("bind_ngx_hosts"), + "request VMs must bind through the production registry:\n{bind_program}" + ); + assert!( + !bind_program.contains("bind_ngx_hosts(vm)") || bind_ngx.contains("bind_vm_cached"), + "request VMs must not keep a second name-bind authority" + ); + + let vm_pool = read("src/vm_pool.rs"); + assert!( + vm_pool.contains("ngx_host_registry") + || vm_pool.contains("bind_production_hosts") + || (vm_pool.contains("bind_ngx_hosts") && bind_ngx.contains("bind_vm_cached")), + "pool VMs must bind through the production registry" + ); + assert!( + !vm_pool.contains("bind_static_function"), + "pool construction must not name-bind hosts" + ); + + let timer = read("src/timer.rs"); + assert!( + (timer.contains("ngx_host_registry") && timer.contains("bind_vm_cached")) + || timer.contains("bind_production_hosts"), + "timer/callback VMs must bind through ngx_host_registry" + ); +} + +fn extract_fn(source: &str, signature: &str) -> String { + let start = source + .find(signature) + .unwrap_or_else(|| panic!("missing {signature}")); + let rest = &source[start..]; + let mut depth = 0i32; + let mut seen_body = false; + for (idx, ch) in rest.char_indices() { + match ch { + '{' => { + depth += 1; + seen_body = true; + } + '}' => { + depth -= 1; + if seen_body && depth == 0 { + return rest[..=idx].to_string(); + } + } + _ => {} + } + } + rest.to_string() +} diff --git a/tests/host_descriptor_handles.rs b/tests/host_descriptor_handles.rs new file mode 100644 index 0000000..5da17be --- /dev/null +++ b/tests/host_descriptor_handles.rs @@ -0,0 +1,328 @@ +//! Typed-handle and raw-guest-token contract tests. +//! +//! Guest resource effects must match catalog passing modes. Sync methods that +//! do not need `&mut Vm` take `ResourceRef`. Unavoidable raw guest tokens +//! (constructors, async, close) still advertise Resource + mode on the +//! descriptor. + +use ngx_rustscript_nginx_test_stubs as _; + +use ngx_rustscript::host::{ + NGX_CTX_CLEAR_NAME, NGX_CTX_DELETE_NAME, NGX_CTX_GET_NAME, NGX_CTX_SET_NAME, + NGX_SEMAPHORE_COUNT_NAME, NGX_SEMAPHORE_NEW_NAME, NGX_SEMAPHORE_POST_NAME, + NGX_SEMAPHORE_WAIT_NAME, NGX_SHARED_DICT_NAME, NGX_SHARED_GET_NAME, + NGX_SOCKET_TCP_CLIENTCERT_NAME, NGX_SOCKET_TCP_NAME, NGX_SOCKET_TCP_TRUSTEDSTORE_NAME, + NGX_SOCKET_UDP_NAME, ngx_host_catalog, ngx_host_modules, +}; +use vm::{ + HostEffect, HostFunctionDescriptor, HostParamPassing, HostTypeSchema, ResourceEffect, + ResourceTypeKey, +}; + +fn all_descriptors() -> Vec { + ngx_host_modules() + .into_iter() + .flat_map(|module| module.descriptors()) + .collect() +} + +fn descriptor_named(name: &str) -> HostFunctionDescriptor { + all_descriptors() + .into_iter() + .find(|descriptor| descriptor.schema.name == name) + .unwrap_or_else(|| panic!("missing descriptor {name}")) +} + +fn guest_effects(descriptor: &HostFunctionDescriptor) -> Vec { + descriptor + .effects + .iter() + .filter_map(HostEffect::guest_resource) + .cloned() + .collect() +} + +fn resource_param_slots( + schema: &vm::HostFunctionSchema, +) -> Vec<(usize, HostParamPassing, ResourceTypeKey)> { + schema + .params + .iter() + .enumerate() + .filter_map(|(index, param)| match ¶m.ty { + HostTypeSchema::Resource(key) => Some((index, param.passing, key.clone())), + _ => None, + }) + .collect() +} + +fn expected_effects(schema: &vm::HostFunctionSchema) -> Vec { + let mut expected = Vec::new(); + for param in &schema.params { + if let HostTypeSchema::Resource(key) = ¶m.ty { + match param.passing { + HostParamPassing::Borrow => expected.push(ResourceEffect::borrow(key.clone())), + HostParamPassing::BorrowMut => { + expected.push(ResourceEffect::borrow_mut(key.clone())) + } + HostParamPassing::TakeOwned => { + expected.push(ResourceEffect::take_owned(key.clone())) + } + HostParamPassing::Value => {} + } + } + } + if let HostTypeSchema::Resource(key) = &schema.return_type { + expected.push(ResourceEffect::create(key.clone())); + } + expected +} + +#[test] +fn guest_resource_effects_match_every_descriptor_schema() { + for descriptor in all_descriptors() { + let mut expected = expected_effects(&descriptor.schema); + let mut actual = guest_effects(&descriptor); + let sort_key = |effect: &ResourceEffect| format!("{effect:?}"); + expected.sort_by_key(|left| sort_key(left)); + actual.sort_by_key(|left| sort_key(left)); + assert_eq!( + expected, actual, + "{} guest resource effects must follow the catalog contract", + descriptor.schema.name + ); + } +} + +#[test] +fn constructors_create_typed_resources() { + for (name, key) in [ + (NGX_SEMAPHORE_NEW_NAME, "ngx.semaphore"), + (NGX_SHARED_DICT_NAME, "ngx.shared.dict"), + (NGX_SOCKET_UDP_NAME, "ngx.socket.udp"), + (NGX_SOCKET_TCP_NAME, "ngx.socket.tcp"), + ( + NGX_SOCKET_TCP_TRUSTEDSTORE_NAME, + "ngx.socket.tls.trustedstore", + ), + (NGX_SOCKET_TCP_CLIENTCERT_NAME, "ngx.socket.tls.clientcert"), + ] { + let descriptor = descriptor_named(name); + match &descriptor.schema.return_type { + HostTypeSchema::Resource(resource) => assert_eq!(resource.as_str(), key, "{name}"), + other => panic!("{name} must return Resource({key}), got {other:?}"), + } + assert!( + guest_effects(&descriptor) + .iter() + .any(|effect| matches!(effect, ResourceEffect::Create { key: created } if created.as_str() == key)), + "{name} must advertise Create({key})" + ); + } +} + +#[test] +fn semaphore_post_and_count_borrow_typed_handles() { + for name in [NGX_SEMAPHORE_POST_NAME, NGX_SEMAPHORE_COUNT_NAME] { + let descriptor = descriptor_named(name); + let slots = resource_param_slots(&descriptor.schema); + assert_eq!(slots.len(), 1, "{name} has one semaphore handle slot"); + assert_eq!(slots[0].0, 0); + assert_eq!(slots[0].1, HostParamPassing::Borrow); + assert_eq!(slots[0].2.as_str(), "ngx.semaphore"); + } +} + +#[test] +fn shared_dict_ops_borrow_typed_handles() { + let descriptor = descriptor_named(NGX_SHARED_GET_NAME); + let slots = resource_param_slots(&descriptor.schema); + assert_eq!(slots[0].1, HostParamPassing::Borrow); + assert_eq!(slots[0].2.as_str(), "ngx.shared.dict"); +} + +/// Raw guest token boundaries: function, slot index, passing mode, resource key. +/// +/// These Rust signatures still take `i64` because async `ResourceRef` cannot +/// cross suspension, close/`push_resource` need `&mut Vm` (which cannot combine +/// with borrowed resources), or the function is a constructor return token. +const RAW_GUEST_TOKEN_PARAMS: &[(&str, usize, HostParamPassing, &str)] = &[ + ( + NGX_SEMAPHORE_WAIT_NAME, + 0, + HostParamPassing::Borrow, + "ngx.semaphore", + ), + ( + "ngx::socket::udp::bind", + 0, + HostParamPassing::Borrow, + "ngx.socket.udp", + ), + ( + "ngx::socket::udp::setpeername", + 0, + HostParamPassing::Borrow, + "ngx.socket.udp", + ), + ( + "ngx::socket::udp::send", + 0, + HostParamPassing::Borrow, + "ngx.socket.udp", + ), + ( + "ngx::socket::udp::receive", + 0, + HostParamPassing::Borrow, + "ngx.socket.udp", + ), + ( + "ngx::socket::udp::close", + 0, + HostParamPassing::Borrow, + "ngx.socket.udp", + ), + ( + "ngx::socket::udp::settimeout", + 0, + HostParamPassing::Borrow, + "ngx.socket.udp", + ), + ( + "ngx::socket::tcp::connect", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::send", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::receive", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::receiveany", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::receiveuntil", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::close", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::settimeout", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::setkeepalive", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::getreusedtimes", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::sslhandshake", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::settrustedstore", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::settrustedstore", + 1, + HostParamPassing::Borrow, + "ngx.socket.tls.trustedstore", + ), + ( + "ngx::socket::tcp::setclientcert", + 0, + HostParamPassing::Borrow, + "ngx.socket.tcp", + ), + ( + "ngx::socket::tcp::setclientcert", + 1, + HostParamPassing::Borrow, + "ngx.socket.tls.clientcert", + ), +]; + +#[test] +fn raw_guest_token_slots_are_descriptor_tied() { + let catalog = ngx_host_catalog(); + for (name, slot, passing, key) in RAW_GUEST_TOKEN_PARAMS { + let schema = catalog + .function(name) + .unwrap_or_else(|| panic!("missing {name}")); + let param = schema + .params + .get(*slot) + .unwrap_or_else(|| panic!("{name} missing slot {slot}")); + match ¶m.ty { + HostTypeSchema::Resource(resource) => { + assert_eq!(resource.as_str(), *key, "{name} slot {slot}"); + } + other => panic!("{name} slot {slot} must be Resource({key}), got {other:?}"), + } + assert_eq!(param.passing, *passing, "{name} slot {slot} passing mode"); + let descriptor = descriptor_named(name); + assert!( + guest_effects(&descriptor) + .iter() + .any(|effect| match effect { + ResourceEffect::Borrow { key: advertised } => advertised.as_str() == *key, + _ => false, + }), + "{name} slot {slot} must advertise Borrow({key})" + ); + } +} + +#[test] +fn request_ctx_is_not_a_vm_resource() { + let catalog = ngx_host_catalog(); + for name in [ + NGX_CTX_GET_NAME, + NGX_CTX_SET_NAME, + NGX_CTX_DELETE_NAME, + NGX_CTX_CLEAR_NAME, + ] { + let schema = catalog.function(name).expect(name); + assert!( + resource_param_slots(schema).is_empty(), + "{name} must not take a VM resource handle" + ); + assert!( + !matches!(schema.return_type, HostTypeSchema::Resource(_)), + "{name} must not return a VM resource handle" + ); + } +} diff --git a/tests/host_descriptor_install.rs b/tests/host_descriptor_install.rs index 59b0737..2d2a37d 100644 --- a/tests/host_descriptor_install.rs +++ b/tests/host_descriptor_install.rs @@ -1,17 +1,22 @@ //! Restricted exact-install negatives for ngx module descriptors. //! -//! Production uses [`ngx_rustscript::host::ngx_host_registry`] (empty registry -//! plus transactional `install_from_catalog`). These tests prove deny, -//! mismatch, and rollback stay fail-closed. +//! Production uses [`ngx_rustscript::host::ngx_host_registry`] (restricted +//! registry plus transactional `install_from_catalog` and exact authorization). +//! These tests prove deny, mismatch, and rollback stay fail-closed. use ngx_rustscript_nginx_test_stubs as _; +use std::collections::{BTreeMap, HashMap}; +use std::sync::Arc; +use std::time::Instant; + use ngx_rustscript::config::compile_rss_source; use ngx_rustscript::host::catalog::ngx_host_catalog; -use ngx_rustscript::host::{ngx_host_modules, ngx_host_registry}; +use ngx_rustscript::host::{bind_ngx_hosts, ngx_host_modules, ngx_host_registry}; use vm::{ HostApiBuilder, HostBindingDescriptor, HostBindingKind, HostFunctionDescriptor, - HostFunctionRegistry, HostFunctionSchema, HostParamSchema, HostTypeSchema, Vm, + HostFunctionRegistry, HostFunctionSchema, HostParamSchema, HostResourceTypeMeta, + HostStructField, HostTypeSchema, ResourceTypeKey, ResourceTypeSchema, TypeSchema, Vm, }; fn codec_module() -> vm::HostModuleDescriptor { @@ -34,6 +39,94 @@ fn production_registry_installs_every_catalogued_name() { } } +#[test] +fn production_registry_is_restricted_and_authorizes_only_catalogued_hosts() { + let catalog = ngx_host_catalog(); + let deny_program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", null);"#) + .expect("ngx::escape_uri should compile against the catalog"); + let mut denied = Vm::new_shared(deny_program); + let error = HostFunctionRegistry::restricted() + .bind_vm_cached(&mut denied) + .expect_err("restricted registry must deny ngx hosts before install"); + let message = error.to_string(); + assert!( + message.contains("unbound") + || message.contains("capability") + || message.contains("not allow") + || message.contains("denied"), + "restricted deny-before message: {message}" + ); + + let registry = ngx_host_registry().expect("production registry"); + let allow_program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", null);"#) + .expect("ngx::escape_uri should compile against the catalog"); + let mut allowed = Vm::new_shared(allow_program); + registry + .bind_vm_cached(&mut allowed) + .expect("catalogued ngx::escape_uri must bind after exact install"); + allowed + .run() + .expect("ngx::escape_uri must execute after exact registry bind"); + + let unrelated = compile_rss_source("let x = 1;\n").expect("import-free program"); + let mut unrelated_vm = Vm::new_shared(unrelated); + registry + .bind_vm_cached(&mut unrelated_vm) + .expect("import-free programs still bind"); + unrelated_vm.run().expect("import-free program runs"); + assert!( + !registry.contains_name("vendor::evil") && !registry.contains_name("ngx::not_a_host"), + "unlisted names must stay out of the production registry" + ); + assert!( + catalog.function("ngx::escape_uri").is_some(), + "deny-before/allow-after uses a real catalogued host" + ); +} + +#[test] +fn request_pool_and_timer_bind_through_the_same_descriptor_registry() { + let registry = ngx_host_registry().expect("production registry"); + let catalog = ngx_host_catalog(); + + let request_program = compile_rss_source(r#"use ngx; ngx::md5("x");"#).expect("md5 program"); + let mut request_vm = Vm::new_shared(request_program); + registry + .bind_vm_cached(&mut request_vm) + .expect("request VM bind"); + request_vm.run().expect("request VM executes ngx::md5"); + + let pool_program = compile_rss_source(r#"use ngx; ngx::md5("x");"#).expect("md5 program"); + let mut pool_vm = Vm::new_shared(pool_program); + ngx_rustscript::host::bind_ngx_hosts(&mut pool_vm); + pool_vm.run().expect("pool VM executes ngx::md5"); + + let timer_program = compile_rss_source( + r#" + use ngx; + ngx::timer::pending_count(); + "#, + ) + .expect("timer program"); + let mut timer_vm = Vm::new_shared(timer_program); + registry + .bind_vm_cached(&mut timer_vm) + .expect("timer VM bind"); + ngx_rustscript::timer::install_timer_runtime_on_vm(&mut timer_vm); + timer_vm + .run() + .expect("timer VM executes ngx::timer::pending_count"); + assert!(registry.contains_name("ngx::md5")); + + for schema in catalog.functions() { + assert!( + registry.contains_name(&schema.name), + "one registry must own {}", + schema.name + ); + } +} + #[test] fn restricted_registry_denies_undeclared_ngx_admission() { let program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", null);"#) @@ -113,3 +206,195 @@ fn duplicate_module_install_does_not_leak_a_second_declaration() { "duplicate install message: {message}" ); } + +struct RegistrySnapshot { + names: Vec, + named_structs: BTreeMap, + allows_escape_uri: bool, + plan: *const (), +} + +fn catalog_names_in(registry: &HostFunctionRegistry) -> Vec { + let catalog = ngx_host_catalog(); + let mut names: Vec = catalog + .functions() + .iter() + .filter(|schema| registry.contains_name(&schema.name)) + .map(|schema| schema.name.clone()) + .collect(); + for extra in [ + "ngx::probe", + "ngx::fail", + "vendor::evil", + "ngx::decode_args_conflict", + ] { + if registry.contains_name(extra) { + names.push(extra.to_string()); + } + } + names.sort(); + names +} + +fn allows_escape_uri(registry: &HostFunctionRegistry) -> bool { + let program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", null);"#) + .expect("ngx::escape_uri compiles"); + let mut vm = Vm::new_shared(program); + registry.bind_vm_cached(&mut vm).is_ok() +} + +fn snapshot(registry: &HostFunctionRegistry) -> RegistrySnapshot { + let mut named_structs = BTreeMap::new(); + for (name, schema) in registry.named_struct_schemas() { + named_structs.insert(name.clone(), format!("{schema:?}")); + } + let plan = registry + .prepare_shared_plan(&[]) + .expect("empty-import plan"); + RegistrySnapshot { + names: catalog_names_in(registry), + named_structs, + allows_escape_uri: allows_escape_uri(registry), + plan: Arc::as_ptr(&plan) as *const (), + } +} + +fn assert_snapshot_eq(before: &RegistrySnapshot, after: &RegistrySnapshot, why: &str) { + assert_eq!(before.names, after.names, "{why}: entries"); + assert_eq!( + before.named_structs, after.named_structs, + "{why}: named structs" + ); + assert_eq!( + before.allows_escape_uri, after.allows_escape_uri, + "{why}: capabilities" + ); + assert_eq!(before.plan, after.plan, "{why}: generation token"); +} + +fn semaphore_key() -> ResourceTypeKey { + ResourceTypeKey::new("ngx.semaphore").expect("semaphore resource key") +} + +fn resource_meta(description: &str) -> HostResourceTypeMeta { + HostResourceTypeMeta { + schema: ResourceTypeSchema::new(semaphore_key(), description), + type_id: std::any::TypeId::of::(), + type_name: std::any::type_name::(), + } +} + +struct SemaphoreTypeA; +struct SemaphoreTypeB; + +#[test] +fn skewed_caller_catalog_snapshot_is_unchanged() { + let module = codec_module(); + let mut skewed = HostApiBuilder::new(); + skewed.function(HostFunctionSchema::with_return( + "ngx::escape_uri", + vec![HostParamSchema::value("uri", HostTypeSchema::Int)], + HostTypeSchema::String, + )); + let skewed = skewed.build().expect("skewed catalog validates"); + let mut registry = HostFunctionRegistry::restricted(); + let before = snapshot(®istry); + module + .install_from_catalog(&mut registry, &skewed) + .expect_err("a catalog that disagrees with descriptors must fail"); + assert_snapshot_eq(&before, &snapshot(®istry), "skewed catalog rollback"); +} + +#[test] +fn binding_adapter_mismatch_snapshot_is_unchanged() { + let mut descriptors: Vec = codec_module().descriptors(); + let first = descriptors + .first_mut() + .expect("ngx.codec has at least one descriptor"); + first.binding = HostBindingDescriptor { + kind: HostBindingKind::Owned, + }; + let mut registry = HostFunctionRegistry::restricted(); + let before = snapshot(®istry); + vm::HostModuleDescriptor::install_descriptors(&mut registry, &descriptors) + .expect_err("binding/adapter mismatch must fail closed"); + assert_snapshot_eq(&before, &snapshot(®istry), "adapter mismatch rollback"); +} + +#[test] +fn named_struct_shape_conflict_rolls_back() { + let catalog = ngx_host_catalog(); + let module = codec_module(); + let mut registry = HostFunctionRegistry::restricted(); + module + .install_from_catalog(&mut registry, &catalog) + .expect("codec install"); + let before = snapshot(®istry); + + let mut descriptors = module.descriptors(); + for descriptor in &mut descriptors { + if descriptor.schema.name == "ngx::escape_uri" { + descriptor.schema.return_type = HostTypeSchema::named_struct( + "DecodedArgsResult", + vec![HostStructField::new("wrong", HostTypeSchema::Int)], + ); + } + } + vm::HostModuleDescriptor::install_descriptors(&mut registry, &descriptors) + .expect_err("named struct shape conflict must fail"); + assert_snapshot_eq(&before, &snapshot(®istry), "named struct shape rollback"); + + let mut conflict = HashMap::new(); + conflict.insert("DecodedArgsResult".to_string(), TypeSchema::Int); + let error = registry.install_named_struct_schemas(conflict); + assert!(error.is_err(), "registry named-struct shape conflict"); + assert_snapshot_eq( + &before, + &snapshot(®istry), + "named struct registry shape rollback", + ); +} + +#[test] +fn resource_type_and_doc_conflicts_roll_back() { + let mut registry = HostFunctionRegistry::restricted(); + let before = snapshot(®istry); + + let mut type_conflict = codec_module().descriptors(); + type_conflict[0] + .resource_types + .push(resource_meta::("semaphore type A")); + type_conflict[0] + .resource_types + .push(resource_meta::("semaphore type B")); + vm::HostModuleDescriptor::install_descriptors(&mut registry, &type_conflict) + .expect_err("resource type_id conflict must fail"); + assert_snapshot_eq(&before, &snapshot(®istry), "resource type rollback"); + + let mut doc_conflict = codec_module().descriptors(); + doc_conflict[0] + .resource_types + .push(resource_meta::("docs A")); + doc_conflict[0] + .resource_types + .push(resource_meta::("docs B")); + vm::HostModuleDescriptor::install_descriptors(&mut registry, &doc_conflict) + .expect_err("resource documentation conflict must fail"); + assert_snapshot_eq(&before, &snapshot(®istry), "resource doc rollback"); +} + +#[test] +fn first_bind_after_registry_install_stays_off_the_request_path() { + use std::time::Duration; + + ngx_host_registry().expect("registry install"); + let program = compile_rss_source(r#"use ngx; ngx::sleep_ms(1);"#).expect("compile"); + let mut vm = Vm::new_shared(program); + let started = Instant::now(); + bind_ngx_hosts(&mut vm); + let elapsed = started.elapsed(); + assert!( + elapsed < Duration::from_millis(20), + "bind after worker-init registry install must not block a request: {elapsed:?}" + ); +} diff --git a/tests/lifecycle.rs b/tests/lifecycle.rs index 73528db..e9bc640 100644 --- a/tests/lifecycle.rs +++ b/tests/lifecycle.rs @@ -447,16 +447,7 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_get(port: u16, path: &str) -> (u16, String) { diff --git a/tests/log_hooks.rs b/tests/log_hooks.rs index bd27483..c8fb0ee 100644 --- a/tests/log_hooks.rs +++ b/tests/log_hooks.rs @@ -135,16 +135,7 @@ fn inject_main_env(conf: &str, vars: &[(&str, &str)]) -> String { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_get(port: u16, path: &str) -> (u16, String) { diff --git a/tests/log_nginx.rs b/tests/log_nginx.rs index 39c3c29..9b766ed 100644 --- a/tests/log_nginx.rs +++ b/tests/log_nginx.rs @@ -84,16 +84,7 @@ fn inject_main_env(conf: &str, vars: &[(&str, &str)]) -> String { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_get(port: u16, path: &str) -> (u16, String) { diff --git a/tests/ngx_control_flow.rs b/tests/ngx_control_flow.rs index d920eda..1b3769f 100644 --- a/tests/ngx_control_flow.rs +++ b/tests/ngx_control_flow.rs @@ -308,16 +308,7 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_exchange(port: u16, request: &str) -> (u16, String) { diff --git a/tests/ngx_meta.rs b/tests/ngx_meta.rs index 1336460..a21615e 100644 --- a/tests/ngx_meta.rs +++ b/tests/ngx_meta.rs @@ -181,19 +181,16 @@ fn catalog_runtime_and_manifest_agree_on_task4_hosts() { catalog, manifest, "compiler catalog and manifest must agree" ); - // The owned `ngx::timer::{at,every}` registrations bind through the - // timer-host registry instead of the per-name path. + assert_eq!( + runtime, manifest, + "descriptor registry names must match the catalog/manifest" + ); let timer_family: BTreeSet<&str> = manifest .iter() .copied() .filter(|name| name.starts_with("ngx::timer::")) .collect(); assert_eq!(timer_family.len(), 4, "the timer family has four hosts"); - let per_name: BTreeSet<&str> = manifest.difference(&timer_family).copied().collect(); - assert_eq!( - runtime, per_name, - "per-name bindings must match the manifest minus the timer family" - ); for name in [ "ngx::get_phase", "ngx::config::prefix", diff --git a/tests/ngx_request_body.rs b/tests/ngx_request_body.rs index 37c0550..1f5642c 100644 --- a/tests/ngx_request_body.rs +++ b/tests/ngx_request_body.rs @@ -506,16 +506,7 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() > timeout { - panic!("nginx did not listen on {port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_exchange(port: u16, request: &str) -> (u16, String) { @@ -698,10 +689,6 @@ if file == null { &post, r#" use ngx; -struct RequestArgsResult { - args: map, - truncated: bool -} ngx::req::read_body(); let parsed: RequestArgsResult = ngx::req::get_post_args(null); ngx::say(ngx::encode_args(parsed.args)); diff --git a/tests/ngx_request_body_mutation.rs b/tests/ngx_request_body_mutation.rs index 5cb6ae2..eb5ad39 100644 --- a/tests/ngx_request_body_mutation.rs +++ b/tests/ngx_request_body_mutation.rs @@ -188,8 +188,8 @@ fn install_in_memory_body(req: &mut ngx_http_request_t, chunks: &mut [Vec]) } #[test] -fn production_host_count_is_195_with_body_mutation_hosts() { - assert_eq!(production_runtime_host_names().len(), 195); +fn production_host_count_is_199_with_body_mutation_hosts() { + assert_eq!(production_runtime_host_names().len(), 199); assert_eq!(ngx_host_catalog().functions().len(), 199); assert_eq!(ngx_host_manifest().len(), 199); let runtime: std::collections::BTreeSet<&str> = @@ -708,18 +708,8 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration, prefix: &Path) { - let start = Instant::now(); - while start.elapsed() < timeout { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - std::thread::sleep(Duration::from_millis(20)); - } - panic!( - "port {port} did not open; spawn.err:\n{}\nerror.log:\n{}", - fs::read_to_string(prefix.join("logs/nginx.spawn.err")).unwrap_or_default(), - fs::read_to_string(prefix.join("logs/error.log")).unwrap_or_default() - ); + let _ = prefix; + support::nginx::wait_for_worker_http(port, timeout); } fn http_exchange(port: u16, request: &str) -> (u16, String) { diff --git a/tests/ngx_request_mutation.rs b/tests/ngx_request_mutation.rs index da9cad4..6dcd325 100644 --- a/tests/ngx_request_mutation.rs +++ b/tests/ngx_request_mutation.rs @@ -10,7 +10,7 @@ use std::net::{TcpListener, TcpStream}; use std::path::Path; use std::process::{Child, Stdio}; use std::sync::Arc; -use std::time::{Duration, Instant}; +use std::time::Duration; use nginx_sys::{NGX_HTTP_GET, NGX_HTTP_POST, ngx_http_request_t, ngx_str_t, ngx_uint_t}; use ngx_rustscript::config::{ConfigError, compile_rss_source, program_requires_request_state}; @@ -458,16 +458,7 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_exchange(port: u16, request: &str) -> (u16, String) { @@ -575,10 +566,6 @@ ngx::var::set("uri", "/nope"); &header_script, r#" use ngx; -struct RequestHeadersResult { - headers: map, - truncated: bool -} ngx::req::set_header("X-Dup", "first"); ngx::req::set_header("X-Dup", ["a", "b"]); ngx::req::clear_header("X-Remove"); diff --git a/tests/ngx_request_read.rs b/tests/ngx_request_read.rs index 9e67350..260c1ea 100644 --- a/tests/ngx_request_read.rs +++ b/tests/ngx_request_read.rs @@ -11,7 +11,7 @@ use std::net::{TcpListener, TcpStream}; use std::path::Path; use std::process::{Child, Stdio}; use std::sync::Arc; -use std::time::{Duration, Instant}; +use std::time::Duration; use nginx_sys::{ NGX_HTTP_VERSION_10, NGX_HTTP_VERSION_11, NGX_HTTP_VERSION_20, NGX_HTTP_VERSION_30, ngx_buf_t, @@ -363,16 +363,28 @@ fn request_read_schemas_match_callable_namespaces() { assert_eq!(headers.params.len(), 2); assert_eq!(headers.params[0].ty, HostTypeSchema::Unknown); assert_eq!(headers.params[1].ty, HostTypeSchema::Unknown); - assert_eq!(headers.return_type, HostTypeSchema::Unknown); + match &headers.return_type { + HostTypeSchema::Named { name, fields } => { + assert_eq!(name, "RequestHeadersResult"); + assert_eq!(fields.len(), 2); + } + other => panic!("ngx::req::get_headers must return RequestHeadersResult, got {other:?}"), + } let args = schema_named(GET_URI_ARGS); assert_eq!(args.params.len(), 1); assert_eq!(args.params[0].ty, HostTypeSchema::Unknown); - assert_eq!(args.return_type, HostTypeSchema::Unknown); + match &args.return_type { + HostTypeSchema::Named { name, .. } => assert_eq!(name, "RequestArgsResult"), + other => panic!("ngx::req::get_uri_args must return RequestArgsResult, got {other:?}"), + } let post = schema_named(GET_POST_ARGS); assert_eq!(post.params.len(), 1); - assert_eq!(post.return_type, HostTypeSchema::Unknown); + match &post.return_type { + HostTypeSchema::Named { name, .. } => assert_eq!(name, "RequestArgsResult"), + other => panic!("ngx::req::get_post_args must return RequestArgsResult, got {other:?}"), + } assert_eq!(schema_named(IS_INTERNAL).return_type, HostTypeSchema::Bool); assert_eq!(schema_named(START_TIME).return_type, HostTypeSchema::Float); @@ -1148,16 +1160,7 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_exchange(port: u16, request: &str) -> (u16, String) { @@ -1259,14 +1262,6 @@ fn nginx_request_read_semantics_methods_headers_args_vars_and_internal() { &script, r#" use ngx; -struct RequestHeadersResult { - headers: map, - truncated: bool -} -struct RequestArgsResult { - args: map, - truncated: bool -} ngx::log(ngx::log_level::notice(), "REQ_METHOD=" + ngx::req::get_method()); ngx::log(ngx::log_level::notice(), "REQ_VERSION=" + ngx::encode_args({ v: ngx::req::http_version() })); if ngx::req::is_internal() { diff --git a/tests/ngx_scheduler_contract.rs b/tests/ngx_scheduler_contract.rs index 4a21850..37570c8 100644 --- a/tests/ngx_scheduler_contract.rs +++ b/tests/ngx_scheduler_contract.rs @@ -3,17 +3,14 @@ //! Light threads, `on_abort` callables, and `run_worker_thread` stay absent //! from the production surface. Two worker-owned families are landed: //! -//! - `ngx::semaphore::{new,wait,post,count}` — callable-free, so its four -//! callables bind through the ordinary per-name path; -//! - `ngx::timer::{at,every,pending_count,running_count}` — the registration -//! hosts consume their callback through `HostParamPassing::TakeOwned`, so the -//! family binds through the timer-host registry (`ngx_host_registry`), not -//! through `bind_ngx_hosts`'s name/symbol path. +//! - `ngx::semaphore::{new,wait,post,count}` — callable-free; +//! - `ngx::timer::{at,every,pending_count,running_count}` — registration hosts +//! consume their callback through `HostParamPassing::TakeOwned`. //! -//! Both keep their bare namespace markers (`ngx::semaphore`, `ngx::timer`) -//! unregistered. Configuration compile rejects every remaining candidate in -//! Access, Content, and Log. Do not register the callable families without -//! revisiting `docs/design/ngx-scheduler.md`. +//! Both families bind through [`ngx_host_registry`]. Bare namespace markers +//! (`ngx::semaphore`, `ngx::timer`) stay unregistered. Configuration compile +//! rejects every remaining candidate in Access, Content, and Log. Do not +//! register the callable families without revisiting `docs/design/ngx-scheduler.md`. use ngx_rustscript_nginx_test_stubs as _; @@ -21,11 +18,12 @@ use std::collections::HashSet; use std::path::Path; use ngx_rustscript::config::{ - ConfigError, compile_rss_source_for_phase, validate_program_imports_for_phase, + ConfigError, compile_rss_source, compile_rss_source_for_phase, + validate_program_imports_for_phase, }; use ngx_rustscript::host::{ - HostCapability, HostFamily, NgxHostWrapper, NgxPhase, bind_ngx_hosts, ngx_host_bindings, - ngx_host_catalog, ngx_host_manifest, ngx_host_registry, production_runtime_host_names, + HostCapability, HostFamily, NgxPhase, bind_ngx_hosts, ngx_host_catalog, ngx_host_manifest, + ngx_host_registry, production_runtime_host_names, }; use ngx_rustscript::notify::{CompletionSender, NgxWake, RequestIdentity}; use vm::{ @@ -63,12 +61,6 @@ const REGISTERED_TIMER_HOSTS: &[&str] = &[ "ngx::timer::running_count", ]; -/// Hosts that must stay bound through [`bind_ngx_hosts`]'s per-name path. -/// -/// The timer family is deliberately not here: owned-dispatch hosts have no -/// name-binding API, so they resolve from the timer-host registry instead. -const TIMER_REGISTRY_ONLY_HOSTS: &[&str] = REGISTERED_TIMER_HOSTS; - const CONFIG_PHASES: [NgxPhase; 3] = [NgxPhase::Access, NgxPhase::Content, NgxPhase::Log]; fn candidate_source(name: &str, arity: usize) -> String { @@ -177,8 +169,8 @@ fn production_host_count_reaches_199_with_the_timer_family() { assert_eq!(ngx_host_catalog().functions().len(), 199); assert_eq!( production_runtime_host_names().len(), - 195, - "the per-name bind list keeps the 191 baseline plus the semaphore family" + 199, + "the production name list is the full catalog including timers" ); assert_eq!( ngx_host_manifest().len(), @@ -187,26 +179,19 @@ fn production_host_count_reaches_199_with_the_timer_family() { semaphore and timer families" ); let manifest: HashSet<&str> = ngx_host_manifest().iter().map(|entry| entry.name).collect(); - let mut partitioned: HashSet<&str> = production_runtime_host_names().iter().copied().collect(); - partitioned.extend(REGISTERED_TIMER_HOSTS.iter().copied()); + let runtime: HashSet<&str> = production_runtime_host_names().iter().copied().collect(); assert_eq!( - partitioned.len(), - manifest.len(), - "the per-name bind list plus the timer-family registry list must \ - partition the manifest" + runtime, manifest, + "production runtime names must equal the manifest" ); - for name in &manifest { - assert!(partitioned.contains(name), "{name} must be bound"); - } } -/// Positional (ordered) parity of the four production name sources. +/// Positional (ordered) parity of the production name sources. /// -/// `ngx_host_manifest` and `ngx_host_catalog` are compared entry-for-entry, the -/// per-name binding table (`ngx_host_bindings`) must equal the manifest minus -/// the registry-bound timer family in the same order, and -/// `production_runtime_host_names` must repeat that binding order exactly. A -/// swapped or transcribed entry in any of the four lists fails the comparison. +/// `ngx_host_manifest`, `ngx_host_catalog`, and +/// `production_runtime_host_names` are compared entry-for-entry. The +/// restricted descriptor registry contains every catalogued name, including +/// the timer family. A swapped or transcribed entry fails the comparison. #[test] fn manifest_catalog_bindings_and_runtime_names_are_ordered_and_partitioned() { let manifest: Vec<&str> = ngx_host_manifest().iter().map(|entry| entry.name).collect(); @@ -220,79 +205,38 @@ fn manifest_catalog_bindings_and_runtime_names_are_ordered_and_partitioned() { catalog, manifest, "the compiler catalog must follow the manifest order exactly" ); - - let bindings: Vec<&str> = ngx_host_bindings().iter().map(|(name, _)| *name).collect(); - let registry_bound: Vec<&str> = manifest - .iter() - .copied() - .filter(|name| name.starts_with("ngx::timer::")) - .collect(); - assert_eq!( - registry_bound, REGISTERED_TIMER_HOSTS, - "the registry-bound family is exactly the four timer hosts in manifest order" - ); - let per_name_expected: Vec<&str> = manifest - .iter() - .copied() - .filter(|name| !name.starts_with("ngx::timer::")) - .collect(); - assert_eq!( - bindings, per_name_expected, - "the per-name binding table must be the manifest minus the timer family, in order" - ); assert_eq!( production_runtime_host_names(), - bindings.as_slice(), - "the documented runtime name list must repeat the binding order" + manifest.as_slice(), + "the documented runtime name list must repeat the catalog order" ); - // No two names may share one wrapper: a transcribed entry would bind the - // same host function twice under different names. - let mut wrapper_addresses = HashSet::new(); - for (name, wrapper) in ngx_host_bindings() { - let address = match wrapper { - NgxHostWrapper::Static(function) => *function as usize, - NgxHostWrapper::Args(function) => *function as usize, - }; - assert!( - wrapper_addresses.insert(address), - "{name} reuses a host wrapper address already bound by another name" - ); - } - - // The registry genuinely carries the registry-bound family, and the - // per-name lists genuinely do not. - let registry = ngx_host_registry().expect("timer host registry"); - for name in ®istry_bound { + let registry = ngx_host_registry().expect("production host registry"); + for name in &manifest { assert!( registry.contains_name(name), "{name} must resolve through ngx_host_registry" ); } - for name in &bindings { + for name in REGISTERED_TIMER_HOSTS { assert!( - !name.starts_with("ngx::timer::"), - "{name} must stay out of the per-name bind list" + registry.contains_name(name), + "{name} must resolve through the same production registry" ); } - // The comparisons above are positional: a swap in any list must fail. let mut swapped_catalog = catalog.clone(); swapped_catalog.swap(0, 1); assert_ne!( swapped_catalog, manifest, "a swapped catalog entry must fail the ordered parity check" ); - let mut swapped_bindings = bindings.clone(); - swapped_bindings.swap(0, 1); - assert_ne!( - swapped_bindings, per_name_expected, - "a swapped binding entry must fail the ordered parity check" - ); + let mut swapped_runtime = production_runtime_host_names().to_vec(); + swapped_runtime.swap(0, 1); assert_ne!( - swapped_bindings.as_slice(), - production_runtime_host_names(), - "a swapped binding entry must fail against the runtime name list" + swapped_runtime.as_slice(), + manifest.as_slice(), + "a swapped runtime name must fail against the manifest" ); } @@ -428,16 +372,19 @@ fn scheduler_candidates_do_not_bind_on_a_production_vm() { "bind_ngx_hosts must not install {name}" ); } - for name in TIMER_REGISTRY_ONLY_HOSTS { + + let md5_program = compile_rss_source("use ngx;\nngx::md5(\"x\");\n").expect("md5 program"); + let mut md5_vm = Vm::new_shared(md5_program); + bind_ngx_hosts(&mut md5_vm); + md5_vm + .run() + .expect("production bind must execute imported ngx::md5"); + for name in SCHEDULER_CANDIDATES { assert!( - !vm.has_bound_function(name), - "{name} binds through the timer-host registry, not the name path" + !md5_vm.has_bound_function(name), + "production bind must not install {name}" ); } - assert!( - vm.has_bound_function("ngx::sleep"), - "production bind must still install ngx::sleep" - ); } #[test] diff --git a/tests/ngx_semaphore.rs b/tests/ngx_semaphore.rs index 9c2f45e..cd1625f 100644 --- a/tests/ngx_semaphore.rs +++ b/tests/ngx_semaphore.rs @@ -1,6 +1,6 @@ //! `ngx::semaphore` family: worker-owned state, VM-local handles, FIFO waits. //! -//! Covers catalog/manifest/runtime registration at the exact `195` baseline, +//! Covers catalog/manifest/runtime registration at the exact `199` baseline, //! resource-key typing, phase and Log fail-closed policy, handle validation, //! OpenResty-style `count` semantics, timeout, request cleanup, worker //! shutdown, and the shared `DEFAULT_MAX_IN_FLIGHT` reservation path. @@ -20,7 +20,7 @@ use ngx_rustscript::host::{ HostCapability, HostFamily, HostSyncKind, NGX_SEMAPHORE_COUNT_NAME, NGX_SEMAPHORE_NEW_NAME, NGX_SEMAPHORE_POST_NAME, NGX_SEMAPHORE_RESOURCE_KEY, NGX_SEMAPHORE_WAIT_NAME, NgxPhase, bind_ngx_hosts, host_allowed_in_phase, manifest_entry, ngx_host_catalog, ngx_host_manifest, - production_runtime_host_names, + ngx_host_registry, production_runtime_host_names, }; use ngx_rustscript::notify::DEFAULT_MAX_IN_FLIGHT; use ngx_rustscript::request_state::{AccessStatus, IdentityRegistry, ReadyEffect}; @@ -161,12 +161,11 @@ impl Driver { #[test] fn semaphore_catalog_is_typed_and_phase_closed() { let catalog = ngx_host_catalog(); - // 191 callable baseline + 4 semaphore + 4 timer hosts; the per-name bind - // list stays 195 because the owned `ngx::timer::{at,every}` registrations - // bind through the timer-host registry. + // 191 callable baseline + 4 semaphore + 4 timer hosts, all bound through + // the production descriptor registry. assert_eq!(ngx_host_manifest().len(), 199); assert_eq!(catalog.functions().len(), 199); - assert_eq!(production_runtime_host_names().len(), 195); + assert_eq!(production_runtime_host_names().len(), 199); assert!(catalog.resource(NGX_SEMAPHORE_RESOURCE_KEY).is_some()); for name in HOSTS { @@ -259,15 +258,16 @@ fn semaphore_catalog_is_typed_and_phase_closed() { #[test] fn binding_installs_the_family_and_keeps_gated_candidates_absent() { - let program = compile("let x = 1;\n"); - let mut vm = Vm::try_new((*program).clone()).expect("construct vm"); - bind_ngx_hosts(&mut vm); + let registry = ngx_host_registry().expect("production registry"); for name in HOSTS { assert!( - vm.has_bound_function(name), - "production bind must install {name}" + registry.contains_name(name), + "production registry must install {name}" ); } + let program = compile("let x = 1;\n"); + let mut vm = Vm::try_new((*program).clone()).expect("construct vm"); + bind_ngx_hosts(&mut vm); for name in GATED_CANDIDATES { assert!( !vm.has_bound_function(name), diff --git a/tests/ngx_shared_dict.rs b/tests/ngx_shared_dict.rs index cceb0cb..4c5faf0 100644 --- a/tests/ngx_shared_dict.rs +++ b/tests/ngx_shared_dict.rs @@ -23,6 +23,13 @@ fn compile(source: &str) -> Arc { compile_rss_source(source).unwrap_or_else(|err| panic!("compile failed: {err}\n{source}")) } +fn assert_named_return(schema: &vm::HostFunctionSchema, expected: &str) { + match &schema.return_type { + HostTypeSchema::Named { name, .. } => assert_eq!(name, expected), + other => panic!("expected named {expected}, got {other:?}"), + } +} + fn eval(source: &str) -> Value { reset_process_zones(); install_process_zone("dogs", 64 * 1024); @@ -143,7 +150,7 @@ fn shared_dict_hosts_are_catalogued() { assert_eq!(schema.params[0].passing, HostParamPassing::Borrow); assert_eq!(schema.params[0].ty, HostTypeSchema::Resource(key.clone())); assert_eq!(schema.params[1].ty, HostTypeSchema::String); - assert_eq!(schema.return_type, HostTypeSchema::Unknown); + assert_named_return(schema, "SharedGetResult"); } NGX_SHARED_SET_NAME | NGX_SHARED_SAFE_SET_NAME @@ -160,7 +167,12 @@ fn shared_dict_hosts_are_catalogued() { assert_eq!(schema.params[2].ty, HostTypeSchema::Unknown); assert_eq!(schema.params[3].ty, HostTypeSchema::Unknown); assert_eq!(schema.params[4].ty, HostTypeSchema::Unknown); - assert_eq!(schema.return_type, HostTypeSchema::Unknown); + match *name { + NGX_SHARED_SAFE_SET_NAME | NGX_SHARED_SAFE_ADD_NAME => { + assert_named_return(schema, "SharedSafeStoreResult"); + } + _ => assert_named_return(schema, "SharedStoreResult"), + } } NGX_SHARED_DELETE_NAME => { assert!(host_allowed_in_phase(name, NgxPhase::Init)); @@ -176,7 +188,7 @@ fn shared_dict_hosts_are_catalogued() { assert_eq!(schema.params.len(), 5); assert_eq!(schema.params[0].passing, HostParamPassing::Borrow); assert_eq!(schema.params[1].ty, HostTypeSchema::String); - assert_eq!(schema.return_type, HostTypeSchema::Unknown); + assert_named_return(schema, "SharedIncrResult"); } NGX_SHARED_TTL_NAME => { assert!(host_allowed_in_phase(name, NgxPhase::Init)); @@ -184,7 +196,7 @@ fn shared_dict_hosts_are_catalogued() { assert_eq!(schema.params.len(), 2); assert_eq!(schema.params[0].passing, HostParamPassing::Borrow); assert_eq!(schema.params[1].ty, HostTypeSchema::String); - assert_eq!(schema.return_type, HostTypeSchema::Unknown); + assert_named_return(schema, "SharedNumberResult"); } NGX_SHARED_EXPIRE_NAME => { assert!(host_allowed_in_phase(name, NgxPhase::Init)); @@ -192,7 +204,7 @@ fn shared_dict_hosts_are_catalogued() { assert_eq!(schema.params.len(), 3); assert_eq!(schema.params[0].passing, HostParamPassing::Borrow); assert_eq!(schema.params[1].ty, HostTypeSchema::String); - assert_eq!(schema.return_type, HostTypeSchema::Unknown); + assert_named_return(schema, "SharedSafeStoreResult"); } NGX_SHARED_CAPACITY_NAME | NGX_SHARED_FREE_SPACE_NAME => { assert!(host_allowed_in_phase(name, NgxPhase::Init)); diff --git a/tests/ngx_shared_dict_list.rs b/tests/ngx_shared_dict_list.rs index a7190cf..356381e 100644 --- a/tests/ngx_shared_dict_list.rs +++ b/tests/ngx_shared_dict_list.rs @@ -22,6 +22,13 @@ fn compile(source: &str) -> Arc { compile_rss_source(source).unwrap_or_else(|err| panic!("compile failed: {err}\n{source}")) } +fn assert_named_return(schema: &vm::HostFunctionSchema, expected: &str) { + match &schema.return_type { + HostTypeSchema::Named { name, .. } => assert_eq!(name, expected), + other => panic!("expected named {expected}, got {other:?}"), + } +} + fn eval(source: &str) -> Value { reset_process_zones(); install_process_zone("dogs", 64 * 1024); @@ -88,7 +95,7 @@ const LIST_HOSTS: &[&str] = &[ fn list_and_maintenance_hosts_are_catalogued() { assert_eq!( production_runtime_host_names().len(), - 195, + 199, "production host inventory must stay in sync" ); let key = ResourceTypeKey::new("ngx.shared.dict").unwrap(); @@ -113,12 +120,17 @@ fn list_and_maintenance_hosts_are_catalogued() { assert_eq!(schema.params.len(), 3); assert_eq!(schema.params[1].ty, HostTypeSchema::String); assert_eq!(schema.params[2].ty, HostTypeSchema::Unknown); - assert_eq!(schema.return_type, HostTypeSchema::Unknown); + assert_named_return(schema, "SharedNumberResult"); } - NGX_SHARED_LPOP_NAME | NGX_SHARED_RPOP_NAME | NGX_SHARED_LLEN_NAME => { + NGX_SHARED_LPOP_NAME | NGX_SHARED_RPOP_NAME => { + assert_eq!(schema.params.len(), 2); + assert_eq!(schema.params[1].ty, HostTypeSchema::String); + assert_named_return(schema, "SharedGetResult"); + } + NGX_SHARED_LLEN_NAME => { assert_eq!(schema.params.len(), 2); assert_eq!(schema.params[1].ty, HostTypeSchema::String); - assert_eq!(schema.return_type, HostTypeSchema::Unknown); + assert_named_return(schema, "SharedNumberResult"); } NGX_SHARED_FLUSH_ALL_NAME => { assert_eq!(schema.params.len(), 1); diff --git a/tests/ngx_tcp_keepalive.rs b/tests/ngx_tcp_keepalive.rs index ed9ee07..9bb1910 100644 --- a/tests/ngx_tcp_keepalive.rs +++ b/tests/ngx_tcp_keepalive.rs @@ -246,7 +246,7 @@ fn production_host_count_adds_keepalive_and_tls_hosts() { let runtime: BTreeSet<&str> = production_runtime_host_names().iter().copied().collect(); assert_eq!( production_runtime_host_names().len(), - 195, + 199, "production host inventory must stay in sync" ); assert_eq!(ngx_host_catalog().functions().len(), 199); diff --git a/tests/ngx_tcp_socket.rs b/tests/ngx_tcp_socket.rs index 0103d6c..83f0ace 100644 --- a/tests/ngx_tcp_socket.rs +++ b/tests/ngx_tcp_socket.rs @@ -330,7 +330,7 @@ fn production_host_count_includes_tcp_pool_and_tls_hosts() { let runtime: BTreeSet<&str> = production_runtime_host_names().iter().copied().collect(); assert_eq!( production_runtime_host_names().len(), - 195, + 199, "production host inventory must stay in sync" ); assert_eq!(ngx_host_catalog().functions().len(), 199); diff --git a/tests/ngx_timer.rs b/tests/ngx_timer.rs index 7de0ab2..7b5281c 100644 --- a/tests/ngx_timer.rs +++ b/tests/ngx_timer.rs @@ -122,7 +122,7 @@ fn timer_hosts_are_catalogued_and_manifested_in_access_and_content() { } assert_eq!(ngx_host_manifest().len(), 199); assert_eq!(catalog.functions().len(), 199); - assert_eq!(production_runtime_host_names().len(), 195); + assert_eq!(production_runtime_host_names().len(), 199); assert!(catalog.function("ngx::timer").is_none()); assert!(manifest_entry("ngx::timer").is_none()); } diff --git a/tests/ngx_udp_socket.rs b/tests/ngx_udp_socket.rs index 80cbcd4..67d7fe2 100644 --- a/tests/ngx_udp_socket.rs +++ b/tests/ngx_udp_socket.rs @@ -346,11 +346,11 @@ fn oversized_datagram_is_rejected() { fn udp_echo_round_trip() { let peer = bind_echo_peer(); let port = peer.local_addr().unwrap().port(); - let echo = spawn_echo(peer, b"pong"); - let status = Driver::new(compile(&format!( + let driver = Driver::new(compile(&format!( "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, null);\nngx::socket::udp::close(sock);\n" - ))) - .pump_until(Duration::from_secs(3)); + ))); + let echo = spawn_echo(peer, b"pong"); + let status = driver.pump_until(Duration::from_secs(3)); echo.join().expect("echo thread"); assert_eq!(status, AccessStatus::Ok); } @@ -443,11 +443,11 @@ fn worker_shutdown_drops_a_pending_receive() { fn bind_then_echo_round_trip() { let peer = bind_echo_peer(); let port = peer.local_addr().unwrap().port(); - let echo = spawn_echo(peer, b"pong"); - let status = Driver::new(compile(&format!( + let driver = Driver::new(compile(&format!( "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::bind(sock, \"127.0.0.1\", 0);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, null);\nngx::socket::udp::close(sock);\n" - ))) - .pump_until(Duration::from_secs(3)); + ))); + let echo = spawn_echo(peer, b"pong"); + let status = driver.pump_until(Duration::from_secs(3)); echo.join().expect("echo thread"); assert_eq!(status, AccessStatus::Ok); } diff --git a/tests/output_nginx.rs b/tests/output_nginx.rs index 62ac6e4..1bd3f69 100644 --- a/tests/output_nginx.rs +++ b/tests/output_nginx.rs @@ -62,16 +62,7 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() > timeout { - panic!("nginx did not listen on {port} within {timeout:?}"); - } - thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_exchange(port: u16, request: &str) -> String { diff --git a/tests/shared_dict_nginx.rs b/tests/shared_dict_nginx.rs index 7878e30..c03599f 100644 --- a/tests/shared_dict_nginx.rs +++ b/tests/shared_dict_nginx.rs @@ -9,7 +9,7 @@ use std::path::{Path, PathBuf}; use std::process::{Child, Command, Stdio}; use std::sync::atomic::{AtomicUsize, Ordering}; use std::thread; -use std::time::{Duration, Instant}; +use std::time::Duration; use support::artifact::module_cdylib; use support::nginx::{ @@ -50,50 +50,12 @@ fn write_conf(prefix: &Path, body: &str) { fs::write(prefix.join("conf/nginx.conf"), body).unwrap(); } -/// RustScript declarations for the ngx shared-dict fixed-shape results. -/// -/// The host catalog exposes every fixed multi-return shared-dict call as -/// `unknown`, so a script that consumes one must declare the matching struct -/// and annotate the binding; field access is then the only member access. -/// Declaring them once here keeps the embedded scripts focused on behavior. -const RSS_SHARED_RESULT_STRUCTS: &str = r#" -struct SharedGetResult { - value: T?, - flags: int, - stale: bool, - error: string? -} - -struct SharedStoreResult { - ok: bool, - error: string?, - forcible: bool -} - -struct SharedSafeStoreResult { - ok: bool, - error: string? -} - -struct SharedIncrResult { - value: number?, - error: string?, - forcible: bool -} - -struct SharedNumberResult { - value: number?, - error: string? -} -"#; - -/// Write an RSS script with the shared-dict result structs prepended. +/// Shared-dict result types now come from the host catalog. fn write_script(path: &Path, body: &str) { if let Some(parent) = path.parent() { fs::create_dir_all(parent).unwrap(); } - let source = format!("{}{}", RSS_SHARED_RESULT_STRUCTS, body); - fs::write(path, source).unwrap(); + fs::write(path, body).unwrap(); } fn conf_body(module: &Path, prefix: &Path, port: u16, locations: &str) -> String { @@ -156,17 +118,8 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } } -fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - thread::sleep(Duration::from_millis(20)); - } +fn wait_for_port(port: u16, timeout: Duration, prefix: &Path) { + support::nginx::wait_for_workers_ready(port, timeout, Some(&prefix.join("logs/error.log"))); } fn error_log(tmp: &OwnedTempDir) -> String { @@ -229,7 +182,7 @@ ngx::exit(200); r#" use ngx; let d = ngx::shared::dict("dogs"); -let r: SharedGetResult = ngx::shared::get(d, "Jim"); +let r: SharedGetResult = ngx::shared::get(d, "Jim"); ngx::header::set("X-Value", r.value); ngx::exit(200); "#, @@ -249,7 +202,7 @@ ngx::exit(200); r#" use ngx; let d = ngx::shared::dict("dogs"); -let r: SharedGetResult = ngx::shared::get(d, "hits"); +let r: SharedGetResult = ngx::shared::get(d, "hits"); ngx::header::set("X-Value", ngx::encode_args({ n: r.value })); ngx::exit(200); "#, @@ -263,10 +216,10 @@ ngx::shared::set(d, "b", true, null, 3); ngx::shared::set(d, "i", 42, null, null); ngx::shared::set(d, "f", 1.5, null, null); ngx::shared::set(d, "s", "hi", null, null); -let gb: SharedGetResult = ngx::shared::get(d, "b"); -let gi: SharedGetResult = ngx::shared::get(d, "i"); -let gf: SharedGetResult = ngx::shared::get(d, "f"); -let gs: SharedGetResult = ngx::shared::get(d, "s"); +let gb: SharedGetResult = ngx::shared::get(d, "b"); +let gi: SharedGetResult = ngx::shared::get(d, "i"); +let gf: SharedGetResult = ngx::shared::get(d, "f"); +let gs: SharedGetResult = ngx::shared::get(d, "s"); if gb.value { ngx::header::set("X-Bool", "1"); } else { @@ -294,7 +247,7 @@ if add1.ok { ngx::header::set("X-Add1", "0"); } ngx::header::set("X-Add2", add2.error); -let replaced: SharedGetResult = ngx::shared::get(d, "once"); +let replaced: SharedGetResult = ngx::shared::get(d, "once"); ngx::header::set("X-Rep", replaced.value); ngx::header::set("X-Nil", niladd.error); ngx::exit(200); @@ -331,8 +284,8 @@ let dogs = ngx::shared::dict("dogs"); let cats = ngx::shared::dict("cats"); ngx::shared::set(dogs, "k", "woof", null, null); ngx::shared::set(cats, "k", "meow", null, null); -let dog: SharedGetResult = ngx::shared::get(dogs, "k"); -let cat: SharedGetResult = ngx::shared::get(cats, "k"); +let dog: SharedGetResult = ngx::shared::get(dogs, "k"); +let cat: SharedGetResult = ngx::shared::get(cats, "k"); ngx::header::set("X-Dogs", dog.value); ngx::header::set("X-Cats", cat.value); ngx::exit(200); @@ -377,7 +330,7 @@ ngx::exit(200); combined(&out) ); let running = spawn_nginx(&nginx, tmp.path()); - wait_for_port(port, Duration::from_secs(5)); + wait_for_port(port, Duration::from_secs(5), tmp.path()); (nginx, port, tmp, running) } @@ -436,7 +389,7 @@ fn reload_preserves_zone() { "reload failed: {}", combined(&reload) ); - wait_for_port(port, Duration::from_secs(5)); + wait_for_port(port, Duration::from_secs(5), tmp.path()); let mut ok = false; for _ in 0..20 { thread::sleep(Duration::from_millis(50)); @@ -542,8 +495,8 @@ ngx::exit(200); use ngx; ngx::update_time(); let d = ngx::shared::dict("dogs"); -let live: SharedGetResult = ngx::shared::get(d, "k"); -let stale: SharedGetResult = ngx::shared::get_stale(d, "k"); +let live: SharedGetResult = ngx::shared::get(d, "k"); +let stale: SharedGetResult = ngx::shared::get_stale(d, "k"); ngx::header::set("X-Live", ngx::encode_args({ n: live.flags })); if stale.stale { ngx::header::set("X-Stale", "1"); @@ -621,7 +574,7 @@ http {{ combined(&out) ); let _running = spawn_nginx(&nginx, tmp.path()); - wait_for_port(port, Duration::from_secs(5)); + wait_for_port(port, Duration::from_secs(5), tmp.path()); let (status, body) = http_get(port, "/stale_set"); assert_eq!(status, 200, "stale_set failed: {body}\n{}", error_log(&tmp)); @@ -685,8 +638,8 @@ ngx::exit(200); r#" use ngx; let d = ngx::shared::dict("dogs"); -let left: SharedGetResult = ngx::shared::lpop(d, "q"); -let right: SharedGetResult = ngx::shared::rpop(d, "q"); +let left: SharedGetResult = ngx::shared::lpop(d, "q"); +let right: SharedGetResult = ngx::shared::rpop(d, "q"); ngx::header::set("X-Left", left.value); ngx::header::set("X-Right", right.value); let len: SharedNumberResult = ngx::shared::llen(d, "q"); @@ -702,7 +655,7 @@ use ngx; let d = ngx::shared::dict("dogs"); ngx::shared::set(d, "scalar", "keep", null, null); let push: SharedNumberResult = ngx::shared::lpush(d, "scalar", "x"); -let got: SharedGetResult = ngx::shared::get(d, "scalar"); +let got: SharedGetResult = ngx::shared::get(d, "scalar"); ngx::header::set("X-Err", push.error); ngx::header::set("X-Val", got.value); ngx::exit(200); @@ -714,7 +667,7 @@ ngx::exit(200); use ngx; let d = ngx::shared::dict("dogs"); ngx::shared::rpush(d, "lst", "a"); -let got: SharedGetResult = ngx::shared::get(d, "lst"); +let got: SharedGetResult = ngx::shared::get(d, "lst"); ngx::header::set("X-Err", got.error); let list_len: SharedNumberResult = ngx::shared::llen(d, "lst"); ngx::header::set("X-Len", ngx::encode_args({ n: list_len.value })); @@ -746,9 +699,9 @@ ngx::header::set("X-0", keys[0]); ngx::header::set("X-1", keys[1]); ngx::header::set("X-2", keys[2]); ngx::header::set("X-3", keys[3]); -let ga: SharedGetResult = ngx::shared::get(d, "a"); -let gb2: SharedGetResult = ngx::shared::get(d, "b"); -let gc: SharedGetResult = ngx::shared::get(d, "c"); +let ga: SharedGetResult = ngx::shared::get(d, "a"); +let gb2: SharedGetResult = ngx::shared::get(d, "b"); +let gc: SharedGetResult = ngx::shared::get(d, "c"); ngx::header::set("X-A", ga.value); ngx::header::set("X-B", gb2.value); ngx::header::set("X-C", gc.value); @@ -765,7 +718,7 @@ let d = ngx::shared::dict("dogs"); ngx::shared::set(d, "live", "1", 1, null); ngx::shared::flush_all(d); let n = ngx::shared::flush_expired(d, 0); -let live: SharedGetResult = ngx::shared::get(d, "live"); +let live: SharedGetResult = ngx::shared::get(d, "live"); ngx::header::set("X-Freed", ngx::encode_args({ n: n })); if live.stale { ngx::header::set("X-Stale", "1"); @@ -819,7 +772,7 @@ use ngx; let d = ngx::shared::dict("dogs"); let n: SharedNumberResult = ngx::shared::llen(d, "reload"); ngx::header::set("X-Len", ngx::encode_args({ n: n.value })); -let popped: SharedGetResult = ngx::shared::lpop(d, "reload"); +let popped: SharedGetResult = ngx::shared::lpop(d, "reload"); ngx::header::set("X-Pop", popped.value); ngx::exit(200); "#, @@ -885,7 +838,7 @@ http {{ combined(&out) ); let running = spawn_nginx(&nginx, tmp.path()); - wait_for_port(port, Duration::from_secs(5)); + wait_for_port(port, Duration::from_secs(5), tmp.path()); let (status, body) = http_get(port, "/push"); assert_eq!(status, 200, "push failed: {body}\n{}", error_log(&tmp)); @@ -1036,7 +989,7 @@ use ngx; let d = ngx::shared::dict("dogs"); let n: SharedNumberResult = ngx::shared::llen(d, "reload"); ngx::header::set("X-Len", ngx::encode_args({ n: n.value })); -let popped: SharedGetResult = ngx::shared::lpop(d, "reload"); +let popped: SharedGetResult = ngx::shared::lpop(d, "reload"); ngx::header::set("X-Pop", popped.value); ngx::exit(200); "#, @@ -1163,8 +1116,8 @@ ngx::exit(200); r#" use ngx; let d = ngx::shared::dict("dogs"); -let k0: SharedGetResult = ngx::shared::get(d, "k0000"); -let kn: SharedGetResult = ngx::shared::get(d, "k1099"); +let k0: SharedGetResult = ngx::shared::get(d, "k0000"); +let kn: SharedGetResult = ngx::shared::get(d, "k1099"); if k0.value == null { ngx::header::set("X-K0", "missing"); } else { @@ -1199,7 +1152,7 @@ ngx::update_time(); let d = ngx::shared::dict("visit"); let keys = ngx::shared::get_keys(d, 10); ngx::header::set("X-N", ngx::encode_args({ n: keys.length })); -let live: SharedGetResult = ngx::shared::get(d, "live"); +let live: SharedGetResult = ngx::shared::get(d, "live"); ngx::header::set("X-Live", live.value); ngx::exit(200); "#, @@ -1229,7 +1182,7 @@ use ngx; ngx::update_time(); let d = ngx::shared::dict("cats"); let n = ngx::shared::flush_expired(d, 10); -let stale: SharedGetResult = ngx::shared::get_stale(d, "expired-mru-4"); +let stale: SharedGetResult = ngx::shared::get_stale(d, "expired-mru-4"); ngx::header::set("X-Freed", ngx::encode_args({ n: n })); if stale.value == null { ngx::header::set("X-Stale", "missing"); @@ -1246,7 +1199,7 @@ use ngx; let d = ngx::shared::dict("dogs"); ngx::shared::rpush(d, "decode", "keep"); ngx::shared::rpush(d, "decode", "tail"); -let popped: SharedGetResult = ngx::shared::lpop(d, "decode"); +let popped: SharedGetResult = ngx::shared::lpop(d, "decode"); let rest: SharedNumberResult = ngx::shared::llen(d, "decode"); ngx::header::set("X-Pop", popped.value); if popped.error == null { @@ -1318,7 +1271,7 @@ http {{ combined(&out) ); let running = spawn_nginx(&nginx, tmp.path()); - wait_for_port(port, Duration::from_secs(5)); + wait_for_port(port, Duration::from_secs(5), tmp.path()); let (status, body) = http_get_timeout(port, "/seed_many", Duration::from_secs(30)); assert_eq!(status, 200, "seed_many failed: {body}\n{}", error_log(&tmp)); diff --git a/tests/support/nginx.rs b/tests/support/nginx.rs index 2ebafff..1607b8c 100644 --- a/tests/support/nginx.rs +++ b/tests/support/nginx.rs @@ -4,6 +4,8 @@ use std::ffi::OsStr; use std::fs; +use std::io::{Read, Write}; +use std::net::TcpStream; use std::os::unix::ffi::OsStrExt; use std::os::unix::process::CommandExt; use std::path::{Path, PathBuf}; @@ -69,6 +71,98 @@ pub fn terminate_process_group(child: &mut Child, timeout: Duration) { } } +/// Wait until a worker answers HTTP. +/// +/// Master listen sockets accept TCP before `init_process` finishes installing +/// the host registry. Latency and short-timeout HTTP helpers must not start +/// until a worker has entered its event loop. +pub fn wait_for_worker_http(port: u16, timeout: Duration) { + wait_for_workers_ready(port, timeout, None); +} + +/// Wait until every forked worker has logged `worker runtime ready`. +/// +/// One worker can answer HTTP while siblings are still in `init_process`. +/// Multi-worker tests must not start until `error.log` shows a ready line for +/// each `start worker process`. +pub fn wait_for_workers_ready(port: u16, timeout: Duration, error_log: Option<&Path>) { + let timeout = timeout.max(Duration::from_secs(15)); + let start = Instant::now(); + let mut last: Option = None; + let mut http_ok = false; + loop { + if !http_ok { + match probe_worker_http(port) { + Ok(()) => http_ok = true, + Err(err) => last = Some(err), + } + } + if http_ok { + match error_log { + None => return, + Some(path) => match worker_ready_progress(path) { + Ok(()) => return, + Err(err) => last = Some(err), + }, + } + } + if start.elapsed() >= timeout { + panic!( + "nginx worker did not become ready on 127.0.0.1:{port} within {timeout:?}: {}", + last.as_deref().unwrap_or("port not yet listening") + ); + } + std::thread::sleep(Duration::from_millis(20)); + } +} + +/// `Ok` when `error.log` shows every forked worker is ready. +/// +/// `master_process off` never logs `start worker process`; HTTP readiness is +/// then enough. +pub fn worker_ready_progress(error_log: &Path) -> Result<(), String> { + let text = fs::read_to_string(error_log).unwrap_or_default(); + let started = text.matches("start worker process ").count(); + let ready = text.matches("ngx-rustscript: worker runtime ready").count(); + if started == 0 || ready >= started { + Ok(()) + } else { + Err(format!( + "workers started={started} ready={ready} log={}", + error_log.display() + )) + } +} + +fn probe_worker_http(port: u16) -> Result<(), String> { + // Invalid Content-Length is rejected in header parsing (HTTP 400) before + // rewrite/access/content, so readiness does not run RSS or consume + // shared-dict / rate-limit quota. + let mut stream = TcpStream::connect(("127.0.0.1", port)) + .map_err(|err| format!("connect 127.0.0.1:{port}: {err}"))?; + stream + .set_read_timeout(Some(Duration::from_secs(2))) + .map_err(|err| format!("read timeout: {err}"))?; + stream + .set_write_timeout(Some(Duration::from_secs(1))) + .map_err(|err| format!("write timeout: {err}"))?; + write!( + stream, + "GET / HTTP/1.0\r\nContent-Length: not-a-number\r\nConnection: close\r\n\r\n" + ) + .map_err(|err| format!("write: {err}"))?; + let _ = stream.flush(); + let mut body = Vec::new(); + stream + .read_to_end(&mut body) + .map_err(|err| format!("read: {err}"))?; + if body.starts_with(b"HTTP/") { + Ok(()) + } else { + Err(format!("non-HTTP: {}", String::from_utf8_lossy(&body))) + } +} + pub fn nginx_binary() -> PathBuf { let source_dir = env_path("NGINX_SOURCE_DIR").unwrap_or_else(|| { panic!("NGINX_SOURCE_DIR must be set to the pinned configured NGINX 1.30.4 source tree") diff --git a/tests/support/nginx_harness.rs b/tests/support/nginx_harness.rs index 320d823..7e2b3ef 100644 --- a/tests/support/nginx_harness.rs +++ b/tests/support/nginx_harness.rs @@ -532,7 +532,11 @@ impl NginxHarness { } /// Timeout for the readiness loop (`start`). -pub const READY_TIMEOUT: Duration = Duration::from_secs(10); +/// +/// Master can listen before worker `init_process` finishes installing the host +/// registry (~a few seconds in debug). Readiness therefore waits for an HTTP +/// response, not only a TCP accept. +pub const READY_TIMEOUT: Duration = Duration::from_secs(15); /// Timeout granted to `SIGQUIT` before `terminate` escalates to `SIGKILL`. pub const GRACEFUL_QUIT_TIMEOUT: Duration = Duration::from_secs(5); /// Timeout used by `Drop` and `terminate` for the kill escalation. @@ -978,20 +982,49 @@ impl NginxHarness { } impl RunningNginx { - /// Poll `try_wait` and the loopback port until NGINX answers. + /// Poll `try_wait` until the worker answers HTTP, not only TCP. + /// + /// NGINX's master listen socket can accept connections before + /// `init_process` finishes. Latency tests must not start until a worker + /// has entered its event loop. fn wait_for_readiness(&mut self, timeout: Duration) -> Result<(), String> { let deadline = Instant::now() + timeout; + let mut port_ready = false; + let mut last = String::from("port not yet listening"); loop { if self.try_reap().is_some() { - return Err("exited before the port accepted connections".to_string()); + return Err("exited before the worker answered HTTP".to_string()); } - if TcpStream::connect_timeout(&loopback_socket(self.port), START_POLL_INTERVAL).is_ok() + if !port_ready + && TcpStream::connect_timeout(&loopback_socket(self.port), START_POLL_INTERVAL) + .is_ok() { - return Ok(()); + port_ready = true; + } + if port_ready { + let remaining = deadline.saturating_duration_since(Instant::now()); + if remaining.is_zero() { + return Err(format!( + "timed out after {timeout:?} waiting for a worker HTTP response: {last}" + )); + } + let probe = HttpRequest::get("/") + .version(HttpVersion::Http10) + .header("Content-Length", "not-a-number") + .connect_timeout(Duration::from_millis(200)) + .read_timeout(remaining.min(Duration::from_secs(2))) + .write_timeout(Duration::from_secs(1)); + match self.request(&probe) { + Ok(_) => match super::nginx::worker_ready_progress(&self.error_log_path()) { + Ok(()) => return Ok(()), + Err(err) => last = err, + }, + Err(err) => last = err, + } } if Instant::now() >= deadline { return Err(format!( - "timed out after {timeout:?} waiting for the port to accept connections" + "timed out after {timeout:?} waiting for a worker HTTP response: {last}" )); } std::thread::sleep(START_POLL_INTERVAL); diff --git a/tests/timer_nginx.rs b/tests/timer_nginx.rs index 96665bb..ca20832 100644 --- a/tests/timer_nginx.rs +++ b/tests/timer_nginx.rs @@ -49,34 +49,10 @@ fn get(path: &str) -> HttpRequest { HttpRequest::get(path) } -/// Shared-dict result structs. The host catalog exposes the fixed -/// multi-return shared-dict calls as `unknown`, so a script that consumes one -/// declares the matching struct and annotates the binding. -const RSS_SHARED_RESULT_STRUCTS: &str = r#" -struct SharedGetResult { - value: T?, - flags: int, - stale: bool, - error: string? -} - -struct SharedStoreResult { - ok: bool, - error: string?, - forcible: bool -} - -struct SharedIncrResult { - value: number?, - error: string?, - forcible: bool -} -"#; - +/// Shared-dict result types now come from the host catalog. fn write_script(harness: &NginxHarness, path: &str, contents: &str) { - let source = format!("{RSS_SHARED_RESULT_STRUCTS}{contents}"); harness - .write_file(path, source) + .write_file(path, contents) .unwrap_or_else(|err| panic!("write {path}: {err}")); } @@ -233,7 +209,7 @@ ngx::exit(200); r#" use ngx; let d = ngx::shared::dict("timers"); -let r: SharedGetResult = ngx::shared::get(d, "at_fired"); +let r: SharedGetResult = ngx::shared::get(d, "at_fired"); ngx::say(r.value); "#, ); @@ -420,7 +396,7 @@ ngx::exit(200); r#" use ngx; let d = ngx::shared::dict("timers"); -let r: SharedGetResult = ngx::shared::get(d, "rounds"); +let r: SharedGetResult = ngx::shared::get(d, "rounds"); if r.value == 3 { ngx::say("ROUNDS_3"); } else { ngx::say("ROUNDS_OTHER"); } "#, ); @@ -720,9 +696,9 @@ ngx::exit(200); r#" use ngx; let d = ngx::shared::dict("timers"); -let registered: SharedGetResult = ngx::shared::get(d, "registered_counts"); -let round_start: SharedGetResult = ngx::shared::get(d, "round_start_counts"); -let waiting: SharedGetResult = ngx::shared::get(d, "waiting_counts"); +let registered: SharedGetResult = ngx::shared::get(d, "registered_counts"); +let round_start: SharedGetResult = ngx::shared::get(d, "round_start_counts"); +let waiting: SharedGetResult = ngx::shared::get(d, "waiting_counts"); ngx::say(registered.value); ngx::say(round_start.value); ngx::say(waiting.value); @@ -845,7 +821,7 @@ ngx::exit(200); r#" use ngx; let d = ngx::shared::dict("timers"); -let r: SharedGetResult = ngx::shared::get(d, "timer_fired"); +let r: SharedGetResult = ngx::shared::get(d, "timer_fired"); ngx::say(r.value); "#, ); diff --git a/tests/worker_runtime.rs b/tests/worker_runtime.rs index f17b66f..e815415 100644 --- a/tests/worker_runtime.rs +++ b/tests/worker_runtime.rs @@ -84,16 +84,7 @@ fn spawn_nginx(nginx: &Path, prefix: &Path) -> RunningNginx { } fn wait_for_port(port: u16, timeout: Duration) { - let start = Instant::now(); - loop { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { - return; - } - if start.elapsed() >= timeout { - panic!("nginx did not listen on 127.0.0.1:{port} within {timeout:?}"); - } - std::thread::sleep(Duration::from_millis(20)); - } + support::nginx::wait_for_worker_http(port, timeout); } fn http_get(port: u16, path: &str) -> (u16, String) {