From 6d2bef3ff80bb96455207852d506c5bf12a6a851 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Tue, 16 Jul 2024 19:19:34 -0400 Subject: [threads] Validate all features required by ref.null (#6757) `ref.null` of shared types should only be allowed when shared-everything is enabled, but we were previously checking only that reference types were enabled when validating `ref.null`. Update the code to check all features required by the null type and factor out shared logic for printing lists of missing feature options in error messages. --- src/wasm/wasm-validator.cpp | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'src/wasm/wasm-validator.cpp') diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp index 143baee41..e18191bea 100644 --- a/src/wasm/wasm-validator.cpp +++ b/src/wasm/wasm-validator.cpp @@ -223,6 +223,22 @@ struct ValidationInfo { } }; +std::string getMissingFeaturesList(Module& wasm, FeatureSet feats) { + std::stringstream ss; + bool first = true; + ss << '['; + (feats - wasm.features).iterFeatures([&](FeatureSet feat) { + if (first) { + first = false; + } else { + ss << " "; + } + ss << "--enable-" << feat.toString(); + }); + ss << ']'; + return ss.str(); +} + struct FunctionValidator : public WalkerPass> { bool isFunctionParallel() override { return true; } @@ -2188,9 +2204,12 @@ void FunctionValidator::visitRefNull(RefNull* curr) { // If we are not in a function, this is a global location like a table. We // allow RefNull there as we represent tables that way regardless of what // features are enabled. - shouldBeTrue(!getFunction() || getModule()->features.hasReferenceTypes(), - curr, - "ref.null requires reference-types [--enable-reference-types]"); + auto feats = curr->type.getFeatures(); + if (!shouldBeTrue(!getFunction() || feats <= getModule()->features, + curr, + "ref.null requires additional features")) { + getStream() << getMissingFeaturesList(*getModule(), feats) << '\n'; + } if (!shouldBeTrue( curr->type.isNullable(), curr, "ref.null types must be nullable")) { return; @@ -3711,18 +3730,9 @@ static void validateGlobals(Module& module, ValidationInfo& info) { for (auto& g : module.globals) { auto globalFeats = g->type.getFeatures(); if (!info.shouldBeTrue(globalFeats <= module.features, g->name, "")) { - auto& stream = info.getStream(nullptr); - stream << "global type requires additional features ["; - bool first = true; - (globalFeats - module.features).iterFeatures([&](FeatureSet feat) { - if (first) { - first = false; - } else { - stream << " "; - } - stream << "--enable-" << feat.toString(); - }); - stream << "]\n"; + info.getStream(nullptr) + << "global type requires additional features " + << getMissingFeaturesList(module, globalFeats) << '\n'; } } } -- cgit v1.2.3