Skip to content

Commit 1d67ab0

Browse files
committed
do not combine a load/store offset with a constant pointer if it would wrap a negative value to a positive one, as trapping is tricky
1 parent 4d46a7e commit 1d67ab0

6 files changed

Lines changed: 63 additions & 8 deletions

File tree

auto_update_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
print '..', t
9494
binary = '.wasm' in t
9595
passname = os.path.basename(t).replace('.wast', '').replace('.wasm', '')
96-
opts = ['-' + passname] if passname.startswith('O') else ['--' + p for p in passname.split('_')]
96+
opts = [('--' + p if not p.startswith('O') else '-' + p) for p in passname.split('_')]
9797
t = os.path.join('test', 'passes', t)
9898
actual = ''
9999
for module, asserts in split_wast(t):

check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
print '..', t
7979
binary = '.wasm' in t
8080
passname = os.path.basename(t).replace('.wast', '').replace('.wasm', '')
81-
opts = ['-' + passname] if passname.startswith('O') else ['--' + p for p in passname.split('_')]
81+
opts = [('--' + p if not p.startswith('O') else '-' + p) for p in passname.split('_')]
8282
t = os.path.join(options.binaryen_test, 'passes', t)
8383
actual = ''
8484
for module, asserts in split_wast(t):

src/passes/OptimizeInstructions.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,15 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
908908
// it's better to do the opposite for gzip purposes as well as for readability.
909909
auto* last = ptr->dynCast<Const>();
910910
if (last) {
911-
last->value = Literal(int32_t(last->value.geti32() + offset));
912-
offset = 0;
911+
// don't do this if it would wrap the pointer
912+
uint64_t value64 = last->value.geti32();
913+
uint64_t offset64 = offset;
914+
if (value64 <= std::numeric_limits<int32_t>::max() &&
915+
offset64 <= std::numeric_limits<int32_t>::max() &&
916+
value64 + offset64 <= std::numeric_limits<int32_t>::max()) {
917+
last->value = Literal(int32_t(value64 + offset64));
918+
offset = 0;
919+
}
913920
}
914921
}
915922

test/passes/fuzz-exec_O.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[fuzz-exec] 2 results noted
2+
(module
3+
(type $0 (func (result i64)))
4+
(type $1 (func (result i32)))
5+
(memory $0 1 1)
6+
(export "func_0" (func $func_0))
7+
(export "func_1" (func $func_1))
8+
(func $func_0 (type $0) (result i64)
9+
(block $label$0 (result i64)
10+
(br_if $label$0
11+
(i64.const 1234)
12+
(i32.load16_s offset=22 align=1
13+
(i32.const -1)
14+
)
15+
)
16+
)
17+
)
18+
(func $func_1 (type $1) (result i32)
19+
(i32.load16_s offset=22 align=1
20+
(i32.const -1)
21+
)
22+
)
23+
)
24+
[fuzz-exec] 2 results noted
25+
[fuzz-exec] results match

test/passes/fuzz-exec_O.wast

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(module
2+
(memory $0 1 1)
3+
(export "func_0" (func $func_0))
4+
(export "func_1" (func $func_1))
5+
(func $func_0 (result i64)
6+
(block $label$0 (result i64)
7+
(loop $label$1 (result i64)
8+
(br_if $label$0
9+
(i64.const 1234)
10+
(i32.load16_s offset=22 align=1
11+
(i32.const -1)
12+
)
13+
)
14+
)
15+
)
16+
)
17+
(func $func_1 (result i32)
18+
(i32.load16_s offset=22 align=1
19+
(i32.const -1)
20+
)
21+
)
22+
)
23+

test/passes/optimize-instructions.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,16 +462,16 @@
462462
(i32.const 4)
463463
(get_local $0)
464464
)
465-
(i32.store
466-
(i32.const 0)
465+
(i32.store offset=2
466+
(i32.const -2)
467467
(get_local $0)
468468
)
469469
(i32.store
470470
(i32.const 25)
471471
(get_local $0)
472472
)
473-
(i32.store
474-
(i32.const -23)
473+
(i32.store offset=2
474+
(i32.const -25)
475475
(get_local $0)
476476
)
477477
(drop

0 commit comments

Comments
 (0)