Skip to content

Conversation

alexcrichton
Copy link
Member

This commit fixes a minor regression from #10782 found via fuzzing. The regression is 16-bit immediates were forced to fit from an i32 value into a u16 for 16-bit multiplication. This meant though that negative numbers failed this conversion which meant that ISLE would panic due to the value not being matched. This fixes the logic to first fit the i32 into an i16 and then cast that to a u16 where the first phase should hit all the constants that are possible in Cranelift.

This commit fixes a minor regression from bytecodealliance#10782 found via fuzzing. The
regression is 16-bit immediates were forced to fit from an `i32` value
into a `u16` for 16-bit multiplication. This meant though that negative
numbers failed this conversion which meant that ISLE would panic due to
the value not being matched. This fixes the logic to first fit the i32
into an i16 and then cast that to a u16 where the first phase should hit
all the constants that are possible in Cranelift.
@alexcrichton alexcrichton requested review from a team as code owners May 20, 2025 19:34
@alexcrichton alexcrichton requested review from cfallin and dicej and removed request for a team May 20, 2025 19:34
(rule 1 (x64_imul_imm $I16 src1 (u16_try_from_i32 src2)) (x64_imulw_rmi src1 src2))
(rule 1 (x64_imul_imm $I32 src1 (i32_as_u32 src2)) (x64_imull_rmi src1 src2))
(rule 1 (x64_imul_imm $I64 src1 src2) (x64_imulq_rmi_sxl src1 src2))
(rule 1 (x64_imul_imm $I16 src1 (i16_try_from_i32 src2)) (x64_imulw_rmi src1 (i16_as_u16 src2)))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll admit this is testing the limits of my CLIF knowledge. This will fail when the i32 value fits in a u16, but not an i16, meaning that we'll still panic during instruction selection from that. I don't know whether that's possible in CLIF though, or if iconst values are always sign-extended from their type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, missed that, sorry -- unfortunately we've standardized on zero extension instead (it keeps the majority of cases simpler). Fortunately if this extractor returns None then the rule just doesn't match -- no runtime panic unless there's not another fallback? Let's (i) add a test case with a value in that range (say 40000) and (ii) verify there is...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I've added a few CLIF tests for this and it looks like things are actually working as intended. Looks like when accessing the constant in ISLE it gets sign-extended based on the type of the constant itself, which is why this ended up working out. Yay!

@cfallin cfallin enabled auto-merge May 20, 2025 19:36
@cfallin cfallin added this pull request to the merge queue May 20, 2025
@cfallin cfallin removed this pull request from the merge queue due to a manual request May 20, 2025
@alexcrichton alexcrichton enabled auto-merge May 20, 2025 21:19
@alexcrichton alexcrichton added this pull request to the merge queue May 20, 2025
Merged via the queue into bytecodealliance:main with commit 6abe3c4 May 20, 2025
53 checks passed
@alexcrichton alexcrichton deleted the fix-imul-fuzz branch May 20, 2025 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants