Skip to content

Disallow --nominal with GC #4758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions scripts/fuzz_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

# feature options that are always passed to the tools.
CONSTANT_FEATURE_OPTS = ['--all-features']
CONSTANT_FEATURE_OPTS.append(TYPE_SYSTEM_FLAG)

INPUT_SIZE_MIN = 1024
INPUT_SIZE_MEAN = 40 * 1024
Expand Down Expand Up @@ -120,6 +119,9 @@ def randomize_feature_opts():
if possible in IMPLIED_FEATURE_OPTS:
FEATURE_OPTS.extend(IMPLIED_FEATURE_OPTS[possible])
print('randomized feature opts:', '\n ' + '\n '.join(FEATURE_OPTS))
# Type system flags only make sense when GC is enabled
if '--disable-gc' not in FEATURE_OPTS:
FEATURE_OPTS.append(TYPE_SYSTEM_FLAG)


ALL_FEATURE_OPTS = ['--all-features', '-all', '--mvp-features', '-mvp']
Expand Down Expand Up @@ -819,8 +821,8 @@ def handle_pair(self, input, before_wasm, after_wasm, opts):
b1 = open('b1.wasm', 'rb').read()
b2 = open('b2.wasm', 'rb').read()
if (b1 != b2):
run([in_bin('wasm-dis'), 'b1.wasm', '-o', 'b1.wat', TYPE_SYSTEM_FLAG])
run([in_bin('wasm-dis'), 'b2.wasm', '-o', 'b2.wat', TYPE_SYSTEM_FLAG])
run([in_bin('wasm-dis'), 'b1.wasm', '-o', 'b1.wat', FEATURE_OPTS])
run([in_bin('wasm-dis'), 'b2.wasm', '-o', 'b2.wat', FEATURE_OPTS])
t1 = open('b1.wat', 'r').read()
t2 = open('b2.wat', 'r').read()
compare(t1, t2, 'Output must be deterministic.', verbose=False)
Expand Down Expand Up @@ -1379,10 +1381,8 @@ def randomize_opt_flags():
with open('reduce.sh', 'w') as reduce_sh:
reduce_sh.write('''\
# check the input is even a valid wasm file
echo "At least one of the next two values should be 0:"
%(wasm_opt)s %(typesystem)s --detect-features %(temp_wasm)s
echo " " $?
%(wasm_opt)s %(typesystem)s --all-features %(temp_wasm)s
echo "The following value should be 0:"
%(wasm_opt)s %(features)s %(temp_wasm)s
echo " " $?

# run the command
Expand Down Expand Up @@ -1419,7 +1419,7 @@ def randomize_opt_flags():
'auto_init': auto_init,
'original_wasm': original_wasm,
'temp_wasm': os.path.abspath('t.wasm'),
'typesystem': TYPE_SYSTEM_FLAG,
'features': ' '.join(FEATURE_OPTS),
'reduce_sh': os.path.abspath('reduce.sh')})

print('''\
Expand All @@ -1441,17 +1441,16 @@ def randomize_opt_flags():
vvvv


%(wasm_reduce)s %(type_system_flag)s %(original_wasm)s '--command=bash %(reduce_sh)s' -t %(temp_wasm)s -w %(working_wasm)s
%(wasm_reduce)s %(features)s %(original_wasm)s '--command=bash %(reduce_sh)s' -t %(temp_wasm)s -w %(working_wasm)s


^^^^
||||

Make sure to verify by eye that the output says something like this:

At least one of the next two values should be 0:
The following value should be 0:
0
1
The following value should be 1:
1

Expand All @@ -1471,7 +1470,7 @@ def randomize_opt_flags():
'working_wasm': os.path.abspath('w.wasm'),
'wasm_reduce': in_bin('wasm-reduce'),
'reduce_sh': os.path.abspath('reduce.sh'),
'type_system_flag': TYPE_SYSTEM_FLAG})
'features': ' '.join(FEATURE_OPTS)})
break
if given_seed is not None:
break
Expand Down
3 changes: 3 additions & 0 deletions src/passes/ConstantFieldPropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ struct PCVScanner

struct ConstantFieldPropagation : public Pass {
void run(PassRunner* runner, Module* module) override {
if (!module->features.hasGC()) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

I guess this is needed now because the fuzzer may call this pass without --nominal, and we want to avoid the Fatal below? It is odd though to silently return on not having GC but error on nominal. How about making the fuzzer not run these passes? We could move the relevant passes from opt_choices to gc_opt_choices and only pick from the latter when gc (+ nominal) is enabled?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah that would be another good solution. I slightly prefer keeping these early returns because they seem like what we will want to do in the long term; if running GC optimization passes on non-GC modules gracefully does nothing, then we can run GC optimization passes by default without a problem. I hope that the fatal errors on nonsupported type systems are more temporary and that we eventually update the passes to do what they can with whatever type systems we choose to permanently maintain.

Copy link
Member

Choose a reason for hiding this comment

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

I guess both are reasonable. I'd prefer to not run GC passes by default, though, as they add clutter - for example in debug mode they'd get printed out in the list. Seems nicer to only print out the relevant passes, which we do now. Maybe we can add a mechanism to avoid that, though, like a hook "canRun" that would avoid even adding the pass - that would avoid needing to skip the pass both in pass.cpp and in the fuzzer.

Copy link
Member

Choose a reason for hiding this comment

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

Anyhow, this lgtm + filing an issue to figure out a better solution later.

if (getTypeSystem() != TypeSystem::Nominal) {
Fatal() << "ConstantFieldPropagation requires nominal typing";
}
Expand Down
3 changes: 3 additions & 0 deletions src/passes/GlobalRefining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ namespace {

struct GlobalRefining : public Pass {
void run(PassRunner* runner, Module* module) override {
if (!module->features.hasGC()) {
return;
}
if (getTypeSystem() != TypeSystem::Nominal) {
Fatal() << "GlobalRefining requires nominal typing";
}
Expand Down
3 changes: 3 additions & 0 deletions src/passes/GlobalStructInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ struct GlobalStructInference : public Pass {
std::unordered_map<HeapType, std::vector<Name>> typeGlobals;

void run(PassRunner* runner, Module* module) override {
if (!module->features.hasGC()) {
return;
}
if (getTypeSystem() != TypeSystem::Nominal) {
Fatal() << "GlobalStructInference requires nominal typing";
}
Expand Down
3 changes: 3 additions & 0 deletions src/passes/GlobalTypeOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ struct GlobalTypeOptimization : public Pass {
std::unordered_map<HeapType, std::vector<Index>> indexesAfterRemovals;

void run(PassRunner* runner, Module* module) override {
if (!module->features.hasGC()) {
return;
}
if (getTypeSystem() != TypeSystem::Nominal) {
Fatal() << "GlobalTypeOptimization requires nominal typing";
}
Expand Down
3 changes: 3 additions & 0 deletions src/passes/SignaturePruning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ struct SignaturePruning : public Pass {
std::unordered_map<HeapType, Signature> newSignatures;

void run(PassRunner* runner, Module* module) override {
if (!module->features.hasGC()) {
return;
}
if (getTypeSystem() != TypeSystem::Nominal) {
Fatal() << "SignaturePruning requires nominal typing";
}
Expand Down
3 changes: 3 additions & 0 deletions src/passes/SignatureRefining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ struct SignatureRefining : public Pass {
std::unordered_map<HeapType, Signature> newSignatures;

void run(PassRunner* runner, Module* module) override {
if (!module->features.hasGC()) {
return;
}
if (getTypeSystem() != TypeSystem::Nominal) {
Fatal() << "SignatureRefining requires nominal typing";
}
Expand Down
3 changes: 3 additions & 0 deletions src/passes/TypeRefining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ struct TypeRefining : public Pass {
StructUtils::StructValuesMap<FieldInfo> finalInfos;

void run(PassRunner* runner, Module* module) override {
if (!module->features.hasGC()) {
return;
}
if (getTypeSystem() != TypeSystem::Nominal) {
Fatal() << "TypeRefining requires nominal typing";
}
Expand Down
6 changes: 6 additions & 0 deletions src/tools/tool-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ struct ToolOptions : public Options {
void applyFeatures(Module& module) const {
module.features.enable(enabledFeatures);
module.features.disable(disabledFeatures);
// Non-default type systems only make sense with GC enabled. TODO: Error on
// non-GC equirecursive types as well once we make isorecursive the default
// if we don't remove equirecursive types entirely.
if (!module.features.hasGC() && getTypeSystem() == TypeSystem::Nominal) {
Fatal() << "Nominal typing is only allowed when GC is enabled";
}
}

private:
Expand Down
21 changes: 3 additions & 18 deletions test/lit/nominal-no-gc.wast
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
;; Write the module with --nominal but without GC
;; RUN: wasm-opt %s --nominal --disable-gc -g -o %t.wasm
;; Using --nominal without GC is not allowed.

;; We should not get any recursion groups even though we used --nominal. We use
;; --hybrid -all here to make sure that any rec groups from the binary will
;; actually show up in the output and cause the test to fail.
;; RUN: wasm-opt %t.wasm --hybrid -all -S -o - | filecheck %s
;; RUN: not wasm-opt %s --nominal --disable-gc -g -o %t.wasm 2>&1 | filecheck %s

;; Also check that we don't get a failure with the default configuration.
;; RUN: wasm-opt %t.wasm

;; CHECK-NOT: rec

(module
(type $foo (func))
(type $bar (func))

(func $f1 (type $foo))
(func $f2 (type $bar))
)
;; CHECK: Nominal typing is only allowed when GC is enabled