Something went wrong. Try again.
xburner is an experimental HTTP and gRPC load generator written in Zig.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236const std = @import("std");
const cache = @import("cache.zig");const types = @import("types.zig");const c = @cImport({ @cInclude("runtime/runtime_validate.h");});
pub fn phase1DefaultBackend() types.Backend { return .wamr;}
pub fn fromConfigBackend(backend: @import("../config.zig").WasmBackend) types.Backend { return switch (backend) { .wamr => .wamr, .wasm3 => .wasm3, };}
pub fn abstractionContract() types.AbstractionContract { return .{};}
pub fn capabilities(backend: types.Backend) types.Capabilities { return switch (backend) { .wasm3 => .{ .supports_packed_u64_results = true, .trap_handling = .recoverable_error, .hard_timeout_model = .worker_poisoning, .validates_linear_memory_bounds = true, }, .wamr => .{ .supports_packed_u64_results = true, .trap_handling = .recoverable_error, .hard_timeout_model = .worker_poisoning, .validates_linear_memory_bounds = true, }, };}
pub fn validateWorkflowModule( allocator: std.mem.Allocator, wasm_path: []const u8, backend: types.Backend,) (std.mem.Allocator.Error || std.fs.File.OpenError || std.fs.File.ReadError || types.ValidationError)!types.ModuleValidation { return (try validateWorkflowModuleDetailed(allocator, wasm_path, backend)).validation;}
pub fn validateWorkflowModuleDetailed( allocator: std.mem.Allocator, wasm_path: []const u8, backend: types.Backend,) (std.mem.Allocator.Error || std.fs.File.OpenError || std.fs.File.ReadError || types.ValidationError)!types.ValidationOutcome { const wasm_bytes = try cache.loadModuleBytesDetailed(allocator, wasm_path); const hash_hex = cache.hashHex(wasm_bytes.bytes);
const cached: cache.CachedValidationLoad = cache.loadCachedValidation(allocator, backend, hash_hex) catch .{ .validation = null, .state = .miss, }; if (cached.validation) |validation| { return .{ .validation = validation, .bytes_cache = wasm_bytes.state, .validation_cache = cached.state, }; }
const validation = try validateWorkflowBytes(wasm_bytes.bytes, backend); cache.storeCachedValidation(allocator, backend, hash_hex, validation) catch {}; return .{ .validation = validation, .bytes_cache = wasm_bytes.state, .validation_cache = cached.state, };}
pub fn validateWorkflowBytes(wasm_bytes: []const u8, backend: types.Backend) types.ValidationError!types.ModuleValidation { var result: c.xburner_validation_result_t = .{ .abi_version = 0, .has_init = 0, .has_start_iteration = 0, .has_next_request = 0, .has_handle_response = 0, .has_setup = 0, .has_teardown = 0, }; var err_buf: [256]u8 = undefined;
const rc = switch (backend) { .wasm3 => c.xburner_wasm3_validate(wasm_bytes.ptr, @intCast(wasm_bytes.len), &result, &err_buf, err_buf.len), .wamr => c.xburner_wamr_validate(wasm_bytes.ptr, @intCast(wasm_bytes.len), &result, &err_buf, err_buf.len), }; if (rc != 0) { if (fallbackWasiWorkflowValidation(wasm_bytes)) |validation| { return validation; } return error.WasmValidationFailed; }
const validation = types.ModuleValidation{ .abi_version = result.abi_version, .has_init = result.has_init != 0, .has_start_iteration = result.has_start_iteration != 0, .has_next_request = result.has_next_request != 0, .has_handle_response = result.has_handle_response != 0, .has_setup = result.has_setup != 0, .has_teardown = result.has_teardown != 0, }; return ensureSupportedValidation(validation);}
fn fallbackWasiWorkflowValidation(wasm_bytes: []const u8) ?types.ModuleValidation { const saw_wasi = std.mem.indexOf(u8, wasm_bytes, "wasi_snapshot_preview1") != null; const has_get_abi_version = std.mem.indexOf(u8, wasm_bytes, "get_abi_version") != null; const has_init = std.mem.indexOf(u8, wasm_bytes, "init") != null; const has_start_iteration = std.mem.indexOf(u8, wasm_bytes, "start_iteration") != null; const has_next_request = std.mem.indexOf(u8, wasm_bytes, "next_request") != null; const has_handle_response = std.mem.indexOf(u8, wasm_bytes, "handle_response") != null; const has_setup = std.mem.indexOf(u8, wasm_bytes, "setup") != null; const has_teardown = std.mem.indexOf(u8, wasm_bytes, "teardown") != null;
if (!saw_wasi or !has_get_abi_version or !has_init or !has_start_iteration or !has_next_request or !has_handle_response) { return null; }
return .{ .abi_version = types.expected_workflow_abi_version, .has_init = has_init, .has_start_iteration = has_start_iteration, .has_next_request = has_next_request, .has_handle_response = has_handle_response, .has_setup = has_setup, .has_teardown = has_teardown, };}
fn ensureSupportedValidation(validation: types.ModuleValidation) types.ValidationError!types.ModuleValidation { if (!types.isSupportedValidation(validation)) { if (validation.abi_version != types.expected_workflow_abi_version) return error.UnsupportedAbiVersion; return error.MissingRequiredExport; } return validation;}
test "fallback validation accepts go wasip1 guest exports" { var tmp_dir = std.testing.tmpDir(.{}); defer tmp_dir.cleanup();
try tmp_dir.dir.writeFile(.{ .sub_path = "main.go", .data = \\package main \\import _ "unsafe" \\//go:wasmimport env host_rand_u64 \\func hostRandU64() uint64 \\//go:wasmexport get_abi_version \\func getAbiVersion() uint32 { return 1 } \\//go:wasmexport init \\func initGuest(vuID uint32, seedHi uint32, seedLo uint32) int32 { return 0 } \\//go:wasmexport start_iteration \\func startIteration(ptr uint32, len uint32) int32 { return 0 } \\//go:wasmexport next_request \\func nextRequest() uint64 { return 0 } \\//go:wasmexport handle_response \\func handleResponse(metaPtr uint32, metaLen uint32, bodyPtr uint32, bodyLen uint32) int32 { return 1 } \\func main() {} , });
const temp_root = try std.fmt.allocPrint(std.testing.allocator, ".zig-cache/tmp/{s}", .{tmp_dir.sub_path}); defer std.testing.allocator.free(temp_root); const wasm_path = try std.fs.path.join(std.testing.allocator, &.{ temp_root, "guest.wasm" }); defer std.testing.allocator.free(wasm_path);
var env = try std.process.getEnvMap(std.testing.allocator); defer env.deinit(); try env.put("GOOS", "wasip1"); try env.put("GOARCH", "wasm");
const argv = [_][]const u8{ "go", "build", "-buildmode=c-shared", "-o", "guest.wasm", "main.go", }; const result = try std.process.Child.run(.{ .allocator = std.testing.allocator, .argv = &argv, .cwd = temp_root, .env_map = &env, }); defer std.testing.allocator.free(result.stdout); defer std.testing.allocator.free(result.stderr); try std.testing.expectEqual(@as(u8, 0), result.term.Exited);
const wasm_bytes = try std.fs.cwd().readFileAlloc(std.testing.allocator, wasm_path, 8 * 1024 * 1024); defer std.testing.allocator.free(wasm_bytes);
const fallback = fallbackWasiWorkflowValidation(wasm_bytes); try std.testing.expect(fallback != null); try std.testing.expect(fallback.?.has_handle_response); try std.testing.expect(fallback.?.has_next_request);}
test "phase1 runtime abstraction fixes the required exports" { const contract = abstractionContract();
try std.testing.expect(contract.requires_module_load); try std.testing.expect(contract.requires_instance_lifecycle); try std.testing.expect(contract.requires_import_registration); try std.testing.expect(contract.requires_memory_read_write); try std.testing.expectEqual(@as(usize, 5), contract.required_exports.len); try std.testing.expectEqual(types.RequiredExport.next_request, contract.required_exports[3]);}
test "wamr becomes the default backend" { try std.testing.expectEqual(types.Backend.wamr, phase1DefaultBackend());
const wamr_caps = capabilities(.wamr); try std.testing.expect(wamr_caps.supports_packed_u64_results); try std.testing.expectEqual(types.TrapHandling.recoverable_error, wamr_caps.trap_handling); try std.testing.expectEqual(types.HardTimeoutModel.worker_poisoning, wamr_caps.hard_timeout_model.?);}
test "wasm3 remains available as comparison backend" { const wasm3_caps = capabilities(.wasm3); try std.testing.expect(wasm3_caps.supports_packed_u64_results); try std.testing.expectEqual(types.TrapHandling.recoverable_error, wasm3_caps.trap_handling); try std.testing.expectEqual(types.HardTimeoutModel.worker_poisoning, wasm3_caps.hard_timeout_model.?);}
test "config backend maps to runtime backend" { const config = @import("../config.zig");
try std.testing.expectEqual(types.Backend.wamr, fromConfigBackend(config.WasmBackend.wamr)); try std.testing.expectEqual(types.Backend.wasm3, fromConfigBackend(config.WasmBackend.wasm3));}