Skip to content

Commit ebbcde7

Browse files
authored
Bysyncify: ensure memory exists (#2188)
We need memory in order to read and write rewinding info, so add it if the module didn't have any memory at all. Found by the fuzzer.
1 parent 78287ce commit ebbcde7

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

src/passes/Bysyncify.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202

203203
#include "ir/effects.h"
204204
#include "ir/literal-utils.h"
205+
#include "ir/memory-utils.h"
205206
#include "ir/module-utils.h"
206207
#include "ir/utils.h"
207208
#include "pass.h"
@@ -948,6 +949,10 @@ struct BysyncifyLocals : public WalkerPass<PostWalker<BysyncifyLocals>> {
948949
struct Bysyncify : public Pass {
949950
void run(PassRunner* runner, Module* module) override {
950951
bool optimize = runner->options.optimizeLevel > 0;
952+
953+
// Ensure there is a memory, as we need it.
954+
MemoryUtils::ensureExists(module->memory);
955+
951956
// Find which things can change the state.
952957
auto stateChangingImports =
953958
runner->options.getArgumentOrDefault("bysyncify-imports", "");

test/passes/bysyncify.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,3 +2835,60 @@
28352835
)
28362836
)
28372837
)
2838+
(module
2839+
(memory $0 1 1)
2840+
(global $__bysyncify_state (mut i32) (i32.const 0))
2841+
(global $__bysyncify_data (mut i32) (i32.const 0))
2842+
(export "bysyncify_start_unwind" (func $bysyncify_start_unwind))
2843+
(export "bysyncify_stop_unwind" (func $bysyncify_stop_unwind))
2844+
(export "bysyncify_start_rewind" (func $bysyncify_start_rewind))
2845+
(export "bysyncify_stop_rewind" (func $bysyncify_stop_rewind))
2846+
(func $bysyncify_start_unwind (; 0 ;) (param $0 i32)
2847+
(if
2848+
(i32.gt_u
2849+
(i32.load
2850+
(local.get $0)
2851+
)
2852+
(i32.load offset=4
2853+
(local.get $0)
2854+
)
2855+
)
2856+
(unreachable)
2857+
)
2858+
(global.set $__bysyncify_state
2859+
(i32.const 1)
2860+
)
2861+
(global.set $__bysyncify_data
2862+
(local.get $0)
2863+
)
2864+
)
2865+
(func $bysyncify_stop_unwind (; 1 ;)
2866+
(global.set $__bysyncify_state
2867+
(i32.const 0)
2868+
)
2869+
)
2870+
(func $bysyncify_start_rewind (; 2 ;) (param $0 i32)
2871+
(if
2872+
(i32.gt_u
2873+
(i32.load
2874+
(local.get $0)
2875+
)
2876+
(i32.load offset=4
2877+
(local.get $0)
2878+
)
2879+
)
2880+
(unreachable)
2881+
)
2882+
(global.set $__bysyncify_state
2883+
(i32.const 2)
2884+
)
2885+
(global.set $__bysyncify_data
2886+
(local.get $0)
2887+
)
2888+
)
2889+
(func $bysyncify_stop_rewind (; 3 ;)
2890+
(global.set $__bysyncify_state
2891+
(i32.const 0)
2892+
)
2893+
)
2894+
)

test/passes/bysyncify.wast

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,7 @@
146146
(call $import)
147147
)
148148
)
149+
;; empty module, in particular with no memory
150+
(module
151+
)
149152

0 commit comments

Comments
 (0)