diff --git a/crates/wasm-actor/tests/wasm_actor.rs b/crates/wasm-actor/tests/wasm_actor.rs index ad5eaea..7e4f0d9 100644 --- a/crates/wasm-actor/tests/wasm_actor.rs +++ b/crates/wasm-actor/tests/wasm_actor.rs @@ -10211,4 +10211,154 @@ fn runtime_dropped_with_active_wasm_actors() { // rt dropped here with live actors and unprocessed responses } // No panic — wasmtime Store cleanup is safe +} + +// ── Cycle 70 ───────────────────────────────────────────────────────────────── + +// Guest that reads i32 from payload and uses it as send count +#[test] +fn guest_dynamic_send_count_from_payload() { + let wat = r#"(module + (import "swactor" "send" (func $send (param i32 i32 i32))) + (memory (export "memory") 1) + (func (export "alloc") (param $len i32) (result i32) i32.const 1024) + (func (export "handle") (param $ptr i32) (param $len i32) + (local $count i32) + (local $i i32) + ;; Need at least 33 bytes: 32 dest + 1 count byte + (if (i32.lt_u (local.get $len) (i32.const 33)) (then return)) + ;; Read count from byte 32 + (local.set $count (i32.load8_u (i32.add (local.get $ptr) (i32.const 32)))) + ;; Cap at 10 to prevent excessive sends + (if (i32.gt_u (local.get $count) (i32.const 10)) + (then (local.set $count (i32.const 10))) + ) + ;; Send count times (empty payload from offset 0) + (local.set $i (i32.const 0)) + (block $exit + (loop $loop + (br_if $exit (i32.ge_u (local.get $i) (local.get $count))) + (call $send (local.get $ptr) (i32.const 0) (i32.const 0)) + (local.set $i (i32.add (local.get $i) (i32.const 1))) + (br $loop) + ) + ) + ) + )"#; + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, wat::parse_str(wat).unwrap()) + .build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let inbox = rt.new_inbox::().unwrap(); + let addr = rt.spawn(actor).unwrap(); + + // Send with count byte = 5 + let mut msg = Vec::new(); + msg.extend_from_slice(&inbox.addr().0); + msg.push(5); // count + rt.send_to(addr, ByteMessage(msg)).unwrap(); + rt.tick(); + + let msgs: Vec<_> = std::iter::from_fn(|| inbox.try_recv()).collect(); + assert_eq!(msgs.len(), 5, "should send exactly 5 messages"); +} + +// Three actors in pipeline: A → B → C → inbox +#[test] +fn three_actor_pipeline() { + let engine = SharedEngine::new().unwrap(); + let a = WasmActorBuilder::new(engine.clone(), guest_wasm("echo")).build().unwrap(); + let b = WasmActorBuilder::new(engine.clone(), guest_wasm("echo")).build().unwrap(); + let c = WasmActorBuilder::new(engine, guest_wasm("echo")).build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let inbox = rt.new_inbox::().unwrap(); + + let a_addr = rt.spawn(a).unwrap(); + let b_addr = rt.spawn(b).unwrap(); + let c_addr = rt.spawn(c).unwrap(); + + // A echoes to B, B echoes to C, C echoes to inbox + // But we need to set dest addresses in each message frame: + // Send to A with dest=B, payload that contains framed(C, framed(inbox, "hi")) + // Actually echo just echoes the payload portion to the dest — so: + // A receives: [B_addr][C_addr][inbox_addr]"hi" → sends [C_addr][inbox_addr]"hi" to B + // B receives: [C_addr][inbox_addr]"hi" → sends [inbox_addr]"hi" to C + // C receives: [inbox_addr]"hi" → sends "hi" to inbox + let mut payload = Vec::new(); + payload.extend_from_slice(&c_addr.0); + payload.extend_from_slice(&inbox.addr().0); + payload.extend_from_slice(b"hi"); + + rt.send_to(a_addr, framed_msg(&b_addr, &payload)).unwrap(); + rt.tick(); // A → B + rt.tick(); // B → C + rt.tick(); // C → inbox + + let resp = inbox.try_recv().expect("message should traverse 3-hop pipeline"); + assert_eq!(resp.0, b"hi"); +} + +// Build actors from 3 different guest types in tight loop +#[test] +fn rapid_build_three_guest_types() { + let engine = SharedEngine::new().unwrap(); + let echo_bytes = guest_wasm("echo"); + let double_bytes = guest_wasm("double"); + let silent_bytes = guest_wasm("silent"); + + for _ in 0..20 { + let _ = WasmActorBuilder::new(engine.clone(), echo_bytes.clone()).build().unwrap(); + let _ = WasmActorBuilder::new(engine.clone(), double_bytes.clone()).build().unwrap(); + let _ = WasmActorBuilder::new(engine.clone(), silent_bytes.clone()).build().unwrap(); + } + // 60 build+drop cycles — no leaks, no panics +} + +// Guest with i32.xor instruction +#[test] +fn guest_xor_payload_with_key() { + let wat = r#"(module + (import "swactor" "send" (func $send (param i32 i32 i32))) + (memory (export "memory") 1) + (func (export "alloc") (param $len i32) (result i32) i32.const 1024) + (func (export "handle") (param $ptr i32) (param $len i32) + (local $i i32) + ;; Need >= 33 bytes: 32 dest + at least 1 payload + (if (i32.lt_u (local.get $len) (i32.const 33)) (then return)) + ;; XOR each payload byte with 0xFF + (local.set $i (i32.const 32)) + (block $exit + (loop $loop + (br_if $exit (i32.ge_u (local.get $i) (local.get $len))) + (i32.store8 + (i32.add (local.get $ptr) (local.get $i)) + (i32.xor + (i32.load8_u (i32.add (local.get $ptr) (local.get $i))) + (i32.const 0xFF) + ) + ) + (local.set $i (i32.add (local.get $i) (i32.const 1))) + (br $loop) + ) + ) + ;; Send XORed payload + (call $send + (local.get $ptr) + (i32.add (local.get $ptr) (i32.const 32)) + (i32.sub (local.get $len) (i32.const 32)) + ) + ) + )"#; + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, wat::parse_str(wat).unwrap()) + .build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let inbox = rt.new_inbox::().unwrap(); + let addr = rt.spawn(actor).unwrap(); + + rt.send_to(addr, framed_msg(inbox.addr(), &[0x00, 0xFF, 0xAA])).unwrap(); + rt.tick(); + + let resp = inbox.try_recv().expect("XOR response"); + assert_eq!(resp.0, vec![0xFF, 0x00, 0x55], "each byte XORed with 0xFF"); } \ No newline at end of file