Skip to content

Commit e99af57

Browse files
authored
Fuzzer: Fix JS printing of large BigInts (#8445)
`Number(big) | 0` was wrong, as the Number conversion applies JS rounding when it turns i64 into double. To fix this, get the bits we want first using a precise i64 operation on BigInts.
1 parent 4ccffa1 commit e99af57

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

scripts/fuzz_shell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function printed(x, y) {
143143
} else if (typeof x === 'bigint') {
144144
// Print bigints in legalized form, which is two 32-bit numbers of the low
145145
// and high bits.
146-
return (Number(x) | 0) + ' ' + (Number(x >> 32n) | 0)
146+
return (Number(x & 0xffffffffn) | 0) + ' ' + (Number(x >> 32n) | 0)
147147
} else if (typeof x !== 'number') {
148148
// Something that is not a number or string, like a reference. We can't
149149
// print a reference because it could look different after opts - imagine
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
;; Test that bigints are printed correctly in JS, both the low and high bits.
2+
3+
(module
4+
(func $big (export "big") (result i64)
5+
(i64.const -1)
6+
)
7+
8+
(func $medium (export "medium") (result i64)
9+
;; A number big enough to hit the JS 2^53 precision limit. We must print it
10+
;; carefully.
11+
(i64.const 0x1000000000000001)
12+
)
13+
)
14+
15+
;; RUN: wasm-opt %s -o %t.wasm -q
16+
;; RUN: node %S/../../../scripts/fuzz_shell.js %t.wasm | filecheck %s
17+
18+
;; CHECK: [fuzz-exec] calling big
19+
;; CHECK-NEXT: [fuzz-exec] note result: big => -1 -1
20+
;; CHECK-NEXT: [fuzz-exec] calling medium
21+
;; CHECK-NEXT: [fuzz-exec] note result: medium => 1 268435456
22+

0 commit comments

Comments
 (0)