ryancbuckley opened PR #15293 from ryancbuckley:fix/switch-i128-jump-table-offset to bytecodealliance:main:
This PR fixes a bug in which Switch jump tables could be incorrect for certain large i128 values.
Before this change, to compute the jump table index (val - first_index) for a contiguous range of cases, Switch::build_jump_table checked if first_index fit in u64 and negated it as an i64 to add in the immediate. In some cases, (i128 switches with first_index in (1 << 63, u64::MAX]), negating first_index as an i64 produces a positive value. This positive value then causes every case in the range to be out of bounds for the jump table. As a result, they are shunted to the default block.
Fun demo: you can see the miscompile in action in my cranelift-based fork of the Rust playground: link, screenshot.
This change ensures that for i128 switch indexes, iadd_imm_s will be used only when the negated 128-bit index fits in i64; otherwise it subtracts the full 128-bit constant with isub.
ryancbuckley requested wasmtime-compiler-reviewers for a review on PR #15293.
ryancbuckley removed the review request for wasmtime-compiler-reviewers from PR #15293.
ryancbuckley requested cfallin for a review on PR #15293.
ryancbuckley edited PR #15293:
This PR fixes a bug in which Switch jump tables could be incorrect for certain large i128 values.
Before this change, to compute the jump table index (val - first_index) for a contiguous range of cases, Switch::build_jump_table checked if first_index fit in u64. If it did, it negated first_index as an i64 to add in the immediate. In some cases, (i128 switches with first_index in (1 << 63, u64::MAX]), negating first_index as an i64 produces a positive value. This positive value then causes every case in the range to be out of bounds for the jump table. As a result, they are shunted to the default block.
Fun demo: you can see the miscompile in action in my cranelift-based fork of the Rust playground: link, screenshot.
This change ensures that for i128 switch indexes, iadd_imm_s will be used only when the negated 128-bit index fits in i64; otherwise it subtracts the full 128-bit constant with isub.
ryancbuckley edited PR #15293:
This PR fixes a bug in which Switch jump tables could be incorrect for certain large i128 values.
Before this change, to compute the jump table index (val - first_index) for a contiguous range of cases, Switch::build_jump_table checks if first_index fits in u64. If it does, it negates first_index as an i64 to add in the immediate. In some cases, (i128 switches with first_index in (1 << 63, u64::MAX]), negating first_index as an i64 produces a positive value. This positive value then causes every case in the range to be out of bounds for the jump table. As a result, they are shunted to the default block.
Fun demo: you can see the miscompile in action in my cranelift-based fork of the Rust playground: link, screenshot.
This change ensures that for i128 switch indexes, iadd_imm_s will be used only when the negated 128-bit index fits in i64; otherwise it subtracts the full 128-bit constant with isub.
ryancbuckley edited PR #15293:
This PR fixes a bug in which Switch jump tables could be incorrect for certain large i128 values.
Before this change, to compute the jump table index (val - first_index) for a contiguous range of cases, Switch::build_jump_table checks if first_index fits in u64. If it does, it negates first_index as an i64 to add in the immediate. In some cases, (i128 switches with first_index in (1 << 63, u64::MAX]), negating first_index as an i64 produces a positive value. This positive value then causes every case in the range to be out of bounds for the jump table. As a result, they are shunted to the default block.
Fun demo: you can see the miscompile in action in my cranelift-based fork of the Rust playground: link, screenshot.
This change fixes the bug by ensuring that for i128 switch indexes, iadd_imm_s will be used only when the negated 128-bit index fits in i64; otherwise it subtracts the full 128-bit constant with isub.
ryancbuckley edited PR #15293:
This PR fixes a bug in which Switch jump tables could be incorrect for certain large i128 values.
Before this change, to compute the jump table index (val - first_index) for a contiguous range of cases, Switch::build_jump_table checks if first_index fits in u64. If it does, it negates first_index as an i64 to add in the immediate. In some cases, (i128 switches with first_index in (1 << 63, u64::MAX]), negating first_index as an i64 produces a positive value. This positive value then causes every case in the range to be out of bounds for the jump table. As a result, they are shunted to the default block.
Fun demo: you can see the miscompile in action in my cranelift-based fork of the Rust playground: link, screenshot.
This change fixes the bug by ensuring that for i128 switch indexes, iadd_imm_s will be used only when the negated 128-bit index fits in i64; otherwise it uses isub to subtract the full 128-bit constant.
github-actions[bot] added the label cranelift on PR #15293.
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
I'm not sure I understand why this has to be as complex as it is. It seems to me that:
- We should always be able to use a subtract instructino to produce
val - first_index;- We can always use an
iconstto get the 64 bits that we want for widths <= 64 bits;- For the i128 case, we can use
iconstif the top 65 bits are zero (i.e., values in[0, 2^63-1]), otherwise we can always use two iconsts and an iconcat as below.That nicely decouples the computation (a subtract) and the type-conditional/value-conditional case (getting the 64 or 128 bits that we want into an SSA value).
What do you think?
:speech_balloon: cfallin edited PR review comment.
ryancbuckley updated PR #15293.
ryancbuckley edited PR #15293:
This PR fixes a bug in which Switch jump tables could be incorrect for certain large i128 values.
Before this change, to compute the jump table index (val - first_index) for a contiguous range of cases, Switch::build_jump_table checks if first_index fits in u64. If it does, it negates first_index as an i64 to add in the immediate. In some cases, (i128 switches with first_index in (1 << 63, u64::MAX]), negating first_index as an i64 produces a positive value. This positive value then causes every case in the range to be out of bounds for the jump table. As a result, they are shunted to the default block.
Fun demo: you can see the miscompile in action in my cranelift-based fork of the Rust playground: link, screenshot.
This change fixes the bug by computing the index with an isub with an appropriate iconst.
:memo: ryancbuckley submitted PR review.
:speech_balloon: ryancbuckley created PR review comment:
Great point. I was trying too hard to change the emitted instructions as little as possible. This is much better. Please take a look.
:thumbs_up: cfallin submitted PR review:
Thanks!
cfallin added PR #15293 cranelift-frontend: fix i128 Switch jump table indexing above 2^63 to the merge queue.
ryancbuckley commented on PR #15293:
Thank you for reviewing!
:check: cfallin merged PR #15293.
cfallin removed PR #15293 cranelift-frontend: fix i128 Switch jump table indexing above 2^63 from the merge queue.
Last updated: Nov 19 2026 at 20:05 UTC