@@ -634,8 +634,8 @@ void TranslateToFuzzReader::setupGlobals() {
634634}
635635
636636void TranslateToFuzzReader::setupTags () {
637- // As in modifyInitialFunctions(), we can't allow tag imports as it would trap
638- // when the fuzzing infrastructure doesn't know what to provide.
637+ // As in modifyInitialFunctions(), we can't allow arbitrary tag imports, which
638+ // would trap when the fuzzing infrastructure doesn't know what to provide.
639639 for (auto & tag : wasm.tags ) {
640640 if (tag->imported ()) {
641641 tag->module = tag->base = Name ();
@@ -647,6 +647,15 @@ void TranslateToFuzzReader::setupTags() {
647647 for (size_t i = 0 ; i < num; i++) {
648648 addTag ();
649649 }
650+
651+ // Add the fuzzing support tag manually sometimes.
652+ if (oneIn (2 )) {
653+ auto tag = builder.makeTag (Names::getValidTagName (wasm, " tag" ),
654+ Signature (Type::i32 , Type::none));
655+ tag->module = " fuzzing-support" ;
656+ tag->base = " tag" ;
657+ wasm.addTag (std::move (tag));
658+ }
650659}
651660
652661void TranslateToFuzzReader::addTag () {
@@ -888,16 +897,14 @@ void TranslateToFuzzReader::addImportCallingSupport() {
888897}
889898
890899void TranslateToFuzzReader::addImportThrowingSupport () {
891- // Throw some kind of exception from JS.
892- // TODO: Send an index, which is which exported wasm Tag we should throw, or
893- // something not exported if out of bounds. First we must also export
894- // tags sometimes.
900+ // Throw some kind of exception from JS. If we send 0 then a pure JS
901+ // exception is thrown, and any other value is the value in a wasm tag.
895902 throwImportName = Names::getValidFunctionName (wasm, " throw" );
896903 auto func = std::make_unique<Function>();
897904 func->name = throwImportName;
898905 func->module = " fuzzing-support" ;
899906 func->base = " throw" ;
900- func->type = Signature (Type::none , Type::none);
907+ func->type = Signature (Type::i32 , Type::none);
901908 wasm.addFunction (std::move (func));
902909}
903910
@@ -1067,12 +1074,21 @@ Expression* TranslateToFuzzReader::makeImportLogging() {
10671074}
10681075
10691076Expression* TranslateToFuzzReader::makeImportThrowing (Type type) {
1077+ // TODO: This and makeThrow should probably be rare, as they halt the program.
1078+
10701079 // We throw from the import, so this call appears to be none and not
10711080 // unreachable.
10721081 assert (type == Type::none);
10731082
1074- // TODO: This and makeThrow should probably be rare, as they halt the program.
1075- return builder.makeCall (throwImportName, {}, Type::none);
1083+ // An argument of 0 means to throw a JS exception, and otherwise the value in
1084+ // a wasm tag. Emit 0 or non-zero with ~equal probability.
1085+ Expression* arg;
1086+ if (oneIn (2 )) {
1087+ arg = builder.makeConst (int32_t (0 ));
1088+ } else {
1089+ arg = makeConst (Type::i32 );
1090+ }
1091+ return builder.makeCall (throwImportName, {arg}, Type::none);
10761092}
10771093
10781094Expression* TranslateToFuzzReader::makeImportTableGet () {
0 commit comments