From cb134e73a2d75dce30908e1ee9539df052d9e612 Mon Sep 17 00:00:00 2001 From: ucpr Date: Thu, 26 Mar 2026 15:34:17 +0900 Subject: [PATCH] feat: add Rust and Go workflow templates --- README.md | 2 + docs/testing.md | 1 + docs/workflow-guest.md | 16 +++++++ samples/workflow-health.go | 57 +++++++++++++++++++++++ samples/workflow-health.rs | 58 ++++++++++++++++++++++++ scripts/build_wamr_lib.sh | 4 +- scripts/build_workflow_go_wasm.sh | 34 ++++++++++++++ scripts/build_workflow_rust_wasm.sh | 36 +++++++++++++++ scripts/test_workflow_template_matrix.sh | 57 +++++++++++++++++++++++ src/main.zig | 2 + src/runtime.zig | 1 + src/runtime/module.zig | 23 ++++++---- templates/workflow-guest.go | 57 +++++++++++++++++++++++ templates/workflow-guest.rs | 58 ++++++++++++++++++++++++ 14 files changed, 395 insertions(+), 11 deletions(-) create mode 100644 samples/workflow-health.go create mode 100644 samples/workflow-health.rs create mode 100644 scripts/build_workflow_go_wasm.sh create mode 100644 scripts/build_workflow_rust_wasm.sh create mode 100644 scripts/test_workflow_template_matrix.sh create mode 100644 templates/workflow-guest.go create mode 100644 templates/workflow-guest.rs diff --git a/README.md b/README.md index 24d23f6..e33008f 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,8 @@ More workflow guest details: - [docs/workflow-guest.md](docs/workflow-guest.md) - [include/xburner_workflow_guest.h](include/xburner_workflow_guest.h) - [templates/workflow-guest.c](templates/workflow-guest.c) +- [templates/workflow-guest.rs](templates/workflow-guest.rs) +- [templates/workflow-guest.go](templates/workflow-guest.go) - [hacks/scaffold_workflow.sh](hacks/scaffold_workflow.sh) ## Samples diff --git a/docs/testing.md b/docs/testing.md index b5e38c7..6506990 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -121,6 +121,7 @@ This exercises the workflow sample against: - [scripts/test_manual_tls_keepalive.sh](../scripts/test_manual_tls_keepalive.sh) - [scripts/test_http2_transport.sh](../scripts/test_http2_transport.sh) +- [scripts/test_workflow_template_matrix.sh](../scripts/test_workflow_template_matrix.sh) ## Release Smoke Profiles diff --git a/docs/workflow-guest.md b/docs/workflow-guest.md index 0fdbbd7..714eb49 100644 --- a/docs/workflow-guest.md +++ b/docs/workflow-guest.md @@ -220,8 +220,12 @@ See: - [include/xburner_workflow_guest.h](../include/xburner_workflow_guest.h) - [samples/workflow-health.c](../samples/workflow-health.c) +- [samples/workflow-health.rs](../samples/workflow-health.rs) +- [samples/workflow-health.go](../samples/workflow-health.go) - [samples/workflow-health.toml](../samples/workflow-health.toml) - [templates/workflow-guest.c](../templates/workflow-guest.c) +- [templates/workflow-guest.rs](../templates/workflow-guest.rs) +- [templates/workflow-guest.go](../templates/workflow-guest.go) - [templates/workflow-scenario.toml](../templates/workflow-scenario.toml) The generic build helper is: @@ -230,6 +234,18 @@ The generic build helper is: sh scripts/build_workflow_wasm.sh path/to/guest.c path/to/guest.wasm ``` +Rust and Go helpers are also available: + +```sh +sh scripts/build_workflow_rust_wasm.sh path/to/guest.rs path/to/guest.wasm +sh scripts/build_workflow_go_wasm.sh path/to/guest.go path/to/guest.wasm +``` + +Notes: + +- the Rust template is validated in the repository template matrix +- the Go template currently uses the standard `wasip1` toolchain and is compile-smoke-tested only + The sample guest can be rebuilt with: ```sh diff --git a/samples/workflow-health.go b/samples/workflow-health.go new file mode 100644 index 0000000..17cb0de --- /dev/null +++ b/samples/workflow-health.go @@ -0,0 +1,57 @@ +package main + +import "unsafe" + +const abiVersion uint16 = 1 + +type httpRequestActionV1 struct { + Version uint16 + Flags uint16 + RequestID uint32 + BodyPtr uint32 + BodyLen uint32 + TimeoutMS uint32 + CaptureBodyMaxBytes uint32 +} + +var action = httpRequestActionV1{ + Version: abiVersion, + RequestID: 1, +} + +//go:wasmexport get_abi_version +func getAbiVersion() uint32 { + return uint32(abiVersion) +} + +//go:wasmexport init +func xburnerInit(vuID uint32, seedHi uint32, seedLo uint32) int32 { + _ = vuID + _ = seedHi + _ = seedLo + return 0 +} + +//go:wasmexport start_iteration +func startIteration(inputPtr uint32, inputLen uint32) int32 { + _ = inputPtr + _ = inputLen + return 0 +} + +//go:wasmexport next_request +func nextRequest() uint64 { + ptr := uint32(uintptr(unsafe.Pointer(&action))) + return (uint64(ptr) << 32) | uint64(unsafe.Sizeof(action)) +} + +//go:wasmexport handle_response +func handleResponse(metaPtr uint32, metaLen uint32, bodyPtr uint32, bodyLen uint32) int32 { + _ = metaPtr + _ = metaLen + _ = bodyPtr + _ = bodyLen + return 1 +} + +func main() {} diff --git a/samples/workflow-health.rs b/samples/workflow-health.rs new file mode 100644 index 0000000..118a676 --- /dev/null +++ b/samples/workflow-health.rs @@ -0,0 +1,58 @@ +#![no_std] + +use core::mem::size_of; +use core::panic::PanicInfo; + +const ABI_VERSION: u16 = 1; + +#[repr(C)] +struct HttpRequestActionV1 { + version: u16, + flags: u16, + request_id: u32, + body_ptr: u32, + body_len: u32, + timeout_ms: u32, + capture_body_max_bytes: u32, +} + +static mut ACTION: HttpRequestActionV1 = HttpRequestActionV1 { + version: ABI_VERSION, + flags: 0, + request_id: 1, + body_ptr: 0, + body_len: 0, + timeout_ms: 0, + capture_body_max_bytes: 0, +}; + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} +} + +#[no_mangle] +pub extern "C" fn get_abi_version() -> u32 { + ABI_VERSION as u32 +} + +#[no_mangle] +pub extern "C" fn init(_vu_id: u32, _seed_hi: u32, _seed_lo: u32) -> i32 { + 0 +} + +#[no_mangle] +pub extern "C" fn start_iteration(_input_ptr: u32, _input_len: u32) -> i32 { + 0 +} + +#[no_mangle] +pub extern "C" fn next_request() -> u64 { + let ptr = (&raw const ACTION) as usize as u32; + ((ptr as u64) << 32) | size_of::() as u64 +} + +#[no_mangle] +pub extern "C" fn handle_response(_meta_ptr: u32, _meta_len: u32, _body_ptr: u32, _body_len: u32) -> i32 { + 1 +} diff --git a/scripts/build_wamr_lib.sh b/scripts/build_wamr_lib.sh index cd5fafd..2383d3c 100644 --- a/scripts/build_wamr_lib.sh +++ b/scripts/build_wamr_lib.sh @@ -53,7 +53,7 @@ WAMR_BUILD_AOT=0 WAMR_BUILD_JIT=0 WAMR_BUILD_FAST_JIT=0 WAMR_BUILD_LIBC_BUILTIN=0 -WAMR_BUILD_LIBC_WASI=0 +WAMR_BUILD_LIBC_WASI=1 WAMR_BUILD_SIMD=0 WAMR_BUILD_REF_TYPES=0 WAMR_BUILD_MULTI_MODULE=0 @@ -86,7 +86,7 @@ if [ "${need_configure}" -eq 1 ]; then -DWAMR_BUILD_JIT=0 \ -DWAMR_BUILD_FAST_JIT=0 \ -DWAMR_BUILD_LIBC_BUILTIN=0 \ - -DWAMR_BUILD_LIBC_WASI=0 \ + -DWAMR_BUILD_LIBC_WASI=1 \ -DWAMR_BUILD_SIMD=0 \ -DWAMR_BUILD_REF_TYPES=0 \ -DWAMR_BUILD_MULTI_MODULE=0 \ diff --git a/scripts/build_workflow_go_wasm.sh b/scripts/build_workflow_go_wasm.sh new file mode 100644 index 0000000..ab1f6d6 --- /dev/null +++ b/scripts/build_workflow_go_wasm.sh @@ -0,0 +1,34 @@ +#!/bin/sh +set -eu + +ROOT_DIR=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd) +export ZIG_LOCAL_CACHE_DIR="${ROOT_DIR}/.zig-cache" +export ZIG_GLOBAL_CACHE_DIR="${ROOT_DIR}/.zig-cache/global" +export GOCACHE="${ROOT_DIR}/.zig-cache/go-cache" + +SRC_PATH=${1:?usage: sh scripts/build_workflow_go_wasm.sh [output.wasm]} +OUT_PATH=${2:-} + +case "${SRC_PATH}" in + /*) ;; + *) SRC_PATH="$(pwd)/${SRC_PATH}" ;; +esac + +if [ -z "${OUT_PATH}" ]; then + case "${SRC_PATH}" in + *.go) OUT_PATH=${SRC_PATH%.go}.wasm ;; + *) OUT_PATH="${SRC_PATH}.wasm" ;; + esac +fi + +case "${OUT_PATH}" in + /*) ;; + *) OUT_PATH="$(pwd)/${OUT_PATH}" ;; +esac + +mkdir -p "$(dirname "${OUT_PATH}")" + +exec env GOOS=wasip1 GOARCH=wasm \ + go build -buildmode=c-shared \ + -o "${OUT_PATH}" \ + "${SRC_PATH}" diff --git a/scripts/build_workflow_rust_wasm.sh b/scripts/build_workflow_rust_wasm.sh new file mode 100644 index 0000000..2583ca3 --- /dev/null +++ b/scripts/build_workflow_rust_wasm.sh @@ -0,0 +1,36 @@ +#!/bin/sh +set -eu + +ROOT_DIR=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd) +export ZIG_LOCAL_CACHE_DIR="${ROOT_DIR}/.zig-cache" +export ZIG_GLOBAL_CACHE_DIR="${ROOT_DIR}/.zig-cache/global" + +SRC_PATH=${1:?usage: sh scripts/build_workflow_rust_wasm.sh [output.wasm]} +OUT_PATH=${2:-} + +case "${SRC_PATH}" in + /*) ;; + *) SRC_PATH="$(pwd)/${SRC_PATH}" ;; +esac + +if [ -z "${OUT_PATH}" ]; then + case "${SRC_PATH}" in + *.rs) OUT_PATH=${SRC_PATH%.rs}.wasm ;; + *) OUT_PATH="${SRC_PATH}.wasm" ;; + esac +fi + +case "${OUT_PATH}" in + /*) ;; + *) OUT_PATH="$(pwd)/${OUT_PATH}" ;; +esac + +mkdir -p "$(dirname "${OUT_PATH}")" + +exec rustc \ + --target wasm32-unknown-unknown \ + -C panic=abort \ + --crate-type cdylib \ + -O \ + "${SRC_PATH}" \ + -o "${OUT_PATH}" diff --git a/scripts/test_workflow_template_matrix.sh b/scripts/test_workflow_template_matrix.sh new file mode 100644 index 0000000..2f84d07 --- /dev/null +++ b/scripts/test_workflow_template_matrix.sh @@ -0,0 +1,57 @@ +#!/bin/sh +set -eu + +. "$(dirname "$0")/common.sh" +xburner_init + +TMP_DIR=$(xburner_mktemp_dir workflow-template-matrix) + +cleanup() { + rm -rf "${TMP_DIR}" +} +trap cleanup EXIT INT TERM + +if [ "${SKIP_ZIG_BUILD:-0}" != "1" ]; then + zig build run -- version >/dev/null +elif [ ! -x "${XBURNER_BIN}" ]; then + printf >&2 'missing %s; run `zig build run -- version` or unset SKIP_ZIG_BUILD\n' "${XBURNER_BIN}" + exit 1 +fi + +cat > "${TMP_DIR}/template.toml" </dev/null + +sh "${ROOT_DIR}/scripts/build_workflow_rust_wasm.sh" \ + "${ROOT_DIR}/templates/workflow-guest.rs" \ + "${TMP_DIR}/guest.wasm" +"${XBURNER_BIN}" validate "${TMP_DIR}/template-rust.toml" >/dev/null + +sh "${ROOT_DIR}/scripts/build_workflow_go_wasm.sh" \ + "${ROOT_DIR}/templates/workflow-guest.go" \ + "${TMP_DIR}/guest.wasm" + +printf 'workflow template matrix passed (Go compile smoke only)\n' diff --git a/src/main.zig b/src/main.zig index 1421122..4c75d5e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -25,6 +25,8 @@ pub fn main() !void { std.process.exit(xburner.contracts.ExitCode.invalid_config.code()); }; + xburner.runtime.deinitModuleCaches(gpa.allocator()); + try stdout.flush(); try stderr.flush(); diff --git a/src/runtime.zig b/src/runtime.zig index 50c7044..d318c71 100644 --- a/src/runtime.zig +++ b/src/runtime.zig @@ -19,3 +19,4 @@ pub const abstractionContract = module.abstractionContract; pub const capabilities = module.capabilities; pub const validateWorkflowModule = module.validateWorkflowModule; pub const validateWorkflowBytes = module.validateWorkflowBytes; +pub const deinitModuleCaches = module.deinitModuleCaches; diff --git a/src/runtime/module.zig b/src/runtime/module.zig index 927aa54..13729d0 100644 --- a/src/runtime/module.zig +++ b/src/runtime/module.zig @@ -115,6 +115,19 @@ const BytesCacheEntry = struct { var bytes_cache_mutex: std.Thread.Mutex = .{}; var bytes_cache: std.StringHashMapUnmanaged(BytesCacheEntry) = .empty; +pub fn deinitModuleCaches(allocator: std.mem.Allocator) void { + bytes_cache_mutex.lock(); + defer bytes_cache_mutex.unlock(); + + var iter = bytes_cache.iterator(); + while (iter.next()) |entry| { + allocator.free(entry.key_ptr.*); + allocator.free(entry.value_ptr.bytes); + } + bytes_cache.deinit(allocator); + bytes_cache = .empty; +} + pub fn phase1DefaultBackend() Backend { return .wamr; } @@ -605,15 +618,7 @@ test "module bytes cache reloads file after mtime change" { } fn resetModuleBytesCacheForTests() void { - bytes_cache_mutex.lock(); - defer bytes_cache_mutex.unlock(); - - var iter = bytes_cache.iterator(); - while (iter.next()) |entry| { - std.testing.allocator.free(entry.key_ptr.*); - std.testing.allocator.free(entry.value_ptr.bytes); - } - bytes_cache.clearAndFree(std.testing.allocator); + deinitModuleCaches(std.testing.allocator); } test "phase1 runtime abstraction fixes the required exports" { diff --git a/templates/workflow-guest.go b/templates/workflow-guest.go new file mode 100644 index 0000000..17cb0de --- /dev/null +++ b/templates/workflow-guest.go @@ -0,0 +1,57 @@ +package main + +import "unsafe" + +const abiVersion uint16 = 1 + +type httpRequestActionV1 struct { + Version uint16 + Flags uint16 + RequestID uint32 + BodyPtr uint32 + BodyLen uint32 + TimeoutMS uint32 + CaptureBodyMaxBytes uint32 +} + +var action = httpRequestActionV1{ + Version: abiVersion, + RequestID: 1, +} + +//go:wasmexport get_abi_version +func getAbiVersion() uint32 { + return uint32(abiVersion) +} + +//go:wasmexport init +func xburnerInit(vuID uint32, seedHi uint32, seedLo uint32) int32 { + _ = vuID + _ = seedHi + _ = seedLo + return 0 +} + +//go:wasmexport start_iteration +func startIteration(inputPtr uint32, inputLen uint32) int32 { + _ = inputPtr + _ = inputLen + return 0 +} + +//go:wasmexport next_request +func nextRequest() uint64 { + ptr := uint32(uintptr(unsafe.Pointer(&action))) + return (uint64(ptr) << 32) | uint64(unsafe.Sizeof(action)) +} + +//go:wasmexport handle_response +func handleResponse(metaPtr uint32, metaLen uint32, bodyPtr uint32, bodyLen uint32) int32 { + _ = metaPtr + _ = metaLen + _ = bodyPtr + _ = bodyLen + return 1 +} + +func main() {} diff --git a/templates/workflow-guest.rs b/templates/workflow-guest.rs new file mode 100644 index 0000000..118a676 --- /dev/null +++ b/templates/workflow-guest.rs @@ -0,0 +1,58 @@ +#![no_std] + +use core::mem::size_of; +use core::panic::PanicInfo; + +const ABI_VERSION: u16 = 1; + +#[repr(C)] +struct HttpRequestActionV1 { + version: u16, + flags: u16, + request_id: u32, + body_ptr: u32, + body_len: u32, + timeout_ms: u32, + capture_body_max_bytes: u32, +} + +static mut ACTION: HttpRequestActionV1 = HttpRequestActionV1 { + version: ABI_VERSION, + flags: 0, + request_id: 1, + body_ptr: 0, + body_len: 0, + timeout_ms: 0, + capture_body_max_bytes: 0, +}; + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} +} + +#[no_mangle] +pub extern "C" fn get_abi_version() -> u32 { + ABI_VERSION as u32 +} + +#[no_mangle] +pub extern "C" fn init(_vu_id: u32, _seed_hi: u32, _seed_lo: u32) -> i32 { + 0 +} + +#[no_mangle] +pub extern "C" fn start_iteration(_input_ptr: u32, _input_len: u32) -> i32 { + 0 +} + +#[no_mangle] +pub extern "C" fn next_request() -> u64 { + let ptr = (&raw const ACTION) as usize as u32; + ((ptr as u64) << 32) | size_of::() as u64 +} + +#[no_mangle] +pub extern "C" fn handle_response(_meta_ptr: u32, _meta_len: u32, _body_ptr: u32, _body_len: u32) -> i32 { + 1 +} -- knotmirror 0.0.1