Stream: git-wasmtime

Topic: wasmtime / PR #15293 cranelift-frontend: fix i128 Switch ...


view this post on Zulip Wasmtime GitHub notifications bot (Nov 15 2026 at 04:36):

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.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 15 2026 at 04:36):

ryancbuckley requested wasmtime-compiler-reviewers for a review on PR #15293.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 15 2026 at 04:36):

ryancbuckley removed the review request for wasmtime-compiler-reviewers from PR #15293.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 15 2026 at 04:36):

ryancbuckley requested cfallin for a review on PR #15293.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 15 2026 at 04:42):

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.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 15 2026 at 04:42):

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.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 15 2026 at 04:43):

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.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 15 2026 at 04:44):

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.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 15 2026 at 07:44):

github-actions[bot] added the label cranelift on PR #15293.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 16 2026 at 22:48):

:memo: cfallin submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 16 2026 at 22:48):

: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:

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?

view this post on Zulip Wasmtime GitHub notifications bot (Nov 16 2026 at 22:48):

:speech_balloon: cfallin edited PR review comment.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 02:25):

ryancbuckley updated PR #15293.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 02:51):

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.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 02:54):

:memo: ryancbuckley submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 02:54):

: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.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 04:02):

:thumbs_up: cfallin submitted PR review:

Thanks!

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 04:02):

cfallin added PR #15293 cranelift-frontend: fix i128 Switch jump table indexing above 2^63 to the merge queue.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 04:12):

ryancbuckley commented on PR #15293:

Thank you for reviewing!

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 04:25):

:check: cfallin merged PR #15293.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 04:25):

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