Repository navigation
Open
Description
Bug description
Bitwise & on i8 or i16 omits sign extension (i32.extend8_s / i32.extend16_s) when one operand is already wrapped or is a negative constant, leaving upper bits from an unmasked arithmetic expression intact.
For example, in the reproduction below:
- In
f(),a + awraps to-2ini16, and-2 & -1should evaluate to-2, sof()should return-2n, but it returns65534n. - In
g(100),a + awraps to-56ini8, and-56 & -128should evaluate to-128, sog(100)should returntrue(1), but it returnsfalse(0).
Flow.canOverflow for BinaryOp.AndI32 treats x & y as non-overflowing if either operand cannot overflow or is a constant <= computeSmallIntegerMask(type), which only holds for non-negative values since negative small integers have 1s in their upper 16/24 bits.
Steps to reproduce
export function f(): i64 {
let a: i16 = 32767;
let m: i16 = -1;
let b: i16 = (a + a) & m; // a + a wraps to -2 in i16; -2 & -1 is -2
return <i64>b;
}
export function g(a: i8): bool {
let b: i8 = (a + a) & <i8>-128; // for a = 100: a + a wraps to -56; -56 & -128 is -128
return b == -128;
}Compile with asc repro.ts -O0 (or -O3) and call f() and g(100).
Credit: https://ucsbplse.github.io/
AssemblyScript version
v0.28.20
Playground
Click Run: f(255) prints true (case 1, expected false), h() prints 65534 (expected -2) and k(100) prints false (case 2, expected true).
Metadata
Metadata
Assignees
Labels
No labels
Activity