From 0f64d1b728e213dd6665030e2a9a3fd07e5d5e60 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Sat, 16 May 2026 22:00:22 -0700 Subject: [PATCH] Fix multi-value operator result order. The localification (regalloc and local.get/local.set insertion) pass previously handled multi-value operators with results in the incorrect (reversed) order. This hadn't been caught yet as multi-value results had apparently not yet been used in a nontrivial use-case nor hit by a fuzzer. This PR fixes the order and adds a roundtrip test (the idempotency/fixpoint condition of the roundtrip test will fail if the results are reversed as they will flip on each roundtrip). --- src/backend/mod.rs | 2 +- tests/roundtrip/multi-value-call.wat | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/roundtrip/multi-value-call.wat diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 2f37d3d..1f933cd 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -268,7 +268,7 @@ impl<'a> WasmFuncBackend<'a> { } self.lower_op(op, func); if root { - for &local in &ctx.locals.values[value] { + for &local in ctx.locals.values[value].iter().rev() { func.instruction( &wasm_encoder::Instruction::LocalSet(local.index() as u32), ); diff --git a/tests/roundtrip/multi-value-call.wat b/tests/roundtrip/multi-value-call.wat new file mode 100644 index 0000000..9014a24 --- /dev/null +++ b/tests/roundtrip/multi-value-call.wat @@ -0,0 +1,9 @@ +(module + (func (export "f") (param i32 i32) (result i32 i32) + local.get 0 + local.get 1 + call 1) + + (func (param i32 i32) (result i32 i32) + local.get 0 + local.get 1))