[wasm] Use WasmEngine's allocator for validation

We already exclusively use the WasmEngine's AccountingAllocator for all
decoded Wasm modules. Except for tests, the same allocator will also be
used for validation. Thus do not pass it down explicitly, but get it
from the WasmEngine when needed.

R=ahaas@chromium.org

Bug: v8:13447
Change-Id: Idaa9d6c3e0ab0051bf85bb2667accac89e8b5607
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4092738
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/main@{#84792}
This commit is contained in:
Clemens Backes 2022-12-09 16:30:37 +01:00 committed by V8 LUCI CQ
parent 96837869d6
commit 67377c13c0
8 changed files with 27 additions and 36 deletions

View File

@ -178,11 +178,11 @@ void WasmInliner::Finalize() {
function_bytes.end()};
// If the inlinee was not validated before, do that now.
if (!module()->function_was_validated(candidate.inlinee_index)) {
if (V8_UNLIKELY(
!module()->function_was_validated(candidate.inlinee_index))) {
wasm::WasmFeatures unused_detected_features;
if (ValidateFunctionBody(zone()->allocator(), env_->enabled_features,
module(), &unused_detected_features,
inlinee_body)
if (ValidateFunctionBody(env_->enabled_features, module(),
&unused_detected_features, inlinee_body)
.failed()) {
Trace(candidate, "function is invalid");
// At this point we cannot easily raise a compilation error any more.

View File

@ -7,6 +7,7 @@
#include "src/utils/ostreams.h"
#include "src/wasm/decoder.h"
#include "src/wasm/function-body-decoder-impl.h"
#include "src/wasm/wasm-engine.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-linkage.h"
#include "src/wasm/wasm-module.h"
@ -69,12 +70,11 @@ BytecodeIterator::BytecodeIterator(const byte* start, const byte* end,
if (pc_ > end_) pc_ = end_;
}
DecodeResult ValidateFunctionBody(AccountingAllocator* allocator,
const WasmFeatures& enabled,
DecodeResult ValidateFunctionBody(const WasmFeatures& enabled,
const WasmModule* module,
WasmFeatures* detected,
const FunctionBody& body) {
Zone zone(allocator, ZONE_NAME);
Zone zone(GetWasmEngine()->allocator(), ZONE_NAME);
WasmFullDecoder<Decoder::FullValidationTag, EmptyInterface> decoder(
&zone, module, enabled, detected, body);
decoder.Decode();

View File

@ -42,9 +42,10 @@ struct FunctionBody {
enum class LoadTransformationKind : uint8_t { kSplat, kExtend, kZeroExtend };
V8_EXPORT_PRIVATE DecodeResult ValidateFunctionBody(
AccountingAllocator* allocator, const WasmFeatures& enabled,
const WasmModule* module, WasmFeatures* detected, const FunctionBody& body);
V8_EXPORT_PRIVATE DecodeResult ValidateFunctionBody(const WasmFeatures& enabled,
const WasmModule* module,
WasmFeatures* detected,
const FunctionBody& body);
enum PrintLocals { kPrintLocals, kOmitLocals };
V8_EXPORT_PRIVATE

View File

@ -14,7 +14,6 @@
#include "src/wasm/baseline/liftoff-compiler.h"
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-debug.h"
#include "src/wasm/wasm-engine.h"
namespace v8::internal::wasm {
@ -133,9 +132,8 @@ WasmCompilationResult WasmCompilationUnit::ExecuteFunctionCompilation(
// Before executing TurboFan compilation, make sure that the function was
// validated (because TurboFan compilation assumes valid input).
if (V8_UNLIKELY(!env->module->function_was_validated(func_index_))) {
AccountingAllocator allocator;
if (ValidateFunctionBody(&allocator, env->enabled_features, env->module,
detected, func_body)
if (ValidateFunctionBody(env->enabled_features, env->module, detected,
func_body)
.failed()) {
return {};
}

View File

@ -1074,13 +1074,12 @@ class CompilationUnitBuilder {
DecodeResult ValidateSingleFunction(const WasmModule* module, int func_index,
base::Vector<const uint8_t> code,
AccountingAllocator* allocator,
WasmFeatures enabled_features) {
const WasmFunction* func = &module->functions[func_index];
FunctionBody body{func->sig, func->code.offset(), code.begin(), code.end()};
WasmFeatures detected_features;
return ValidateFunctionBody(allocator, enabled_features, module,
&detected_features, body);
return ValidateFunctionBody(enabled_features, module, &detected_features,
body);
}
enum OnlyLazyFunctions : bool {
@ -1199,10 +1198,9 @@ void ThrowLazyCompilationError(Isolate* isolate,
base::Vector<const uint8_t> code =
compilation_state->GetWireBytesStorage()->GetCode(func->code);
WasmEngine* engine = GetWasmEngine();
auto enabled_features = native_module->enabled_features();
DecodeResult decode_result = ValidateSingleFunction(
module, func_index, code, engine->allocator(), enabled_features);
DecodeResult decode_result =
ValidateSingleFunction(module, func_index, code, enabled_features);
CHECK(decode_result.failed());
wasm::ErrorThrower thrower(isolate, nullptr);
@ -2122,10 +2120,9 @@ class ValidateFunctionsStreamingJob final : public JobTask {
void Run(JobDelegate* delegate) override {
TRACE_EVENT0("v8.wasm", "wasm.ValidateFunctionsStreaming");
AccountingAllocator* allocator = GetWasmEngine()->allocator();
while (Unit unit = data_->GetUnit()) {
DecodeResult result = ValidateSingleFunction(
module_, unit.func_index, unit.code, allocator, enabled_features_);
module_, unit.func_index, unit.code, enabled_features_);
if (result.failed()) {
WasmError new_error = std::move(result).error();

View File

@ -1659,13 +1659,11 @@ class ModuleDecoderTemplate : public Decoder {
if (!ok()) return FunctionResult{std::move(error_)};
AccountingAllocator* allocator = zone->allocator();
FunctionBody body{function.sig, off(pc_), pc_, end_};
WasmFeatures unused_detected_features;
DecodeResult result = ValidateFunctionBody(
allocator, enabled_features_, module, &unused_detected_features, body);
DecodeResult result = ValidateFunctionBody(enabled_features_, module,
&unused_detected_features, body);
if (result.failed()) return FunctionResult{std::move(result).error()};

View File

@ -414,7 +414,6 @@ class ValidateFunctionsTask : public JobTask {
}
void Run(JobDelegate* delegate) override {
AccountingAllocator* allocator = GetWasmEngine()->allocator();
do {
// Get the index of the next function to validate.
// {fetch_add} might overrun {after_last_function_} by a bit. Since the
@ -429,7 +428,7 @@ class ValidateFunctionsTask : public JobTask {
} while ((filter_ && !filter_(func_index)) ||
module_->function_was_validated(func_index));
if (!ValidateFunction(allocator, func_index)) {
if (!ValidateFunction(func_index)) {
// No need to validate any more functions.
next_function_.store(after_last_function_, std::memory_order_relaxed);
return;
@ -443,14 +442,14 @@ class ValidateFunctionsTask : public JobTask {
}
private:
bool ValidateFunction(AccountingAllocator* allocator, int func_index) {
bool ValidateFunction(int func_index) {
WasmFeatures unused_detected_features;
const WasmFunction& function = module_->functions[func_index];
FunctionBody body{function.sig, function.code.offset(),
wire_bytes_.begin() + function.code.offset(),
wire_bytes_.begin() + function.code.end_offset()};
DecodeResult validation_result = ValidateFunctionBody(
allocator, enabled_features_, module_, &unused_detected_features, body);
enabled_features_, module_, &unused_detected_features, body);
if (V8_UNLIKELY(validation_result.failed())) {
SetError(func_index, std::move(validation_result).error());
return false;

View File

@ -258,9 +258,8 @@ class FunctionBodyDecoderTestBase : public WithZoneMixin<BaseTest> {
// Validate the code.
FunctionBody body(sig, 0, code.begin(), code.end());
WasmFeatures unused_detected_features = WasmFeatures::None();
DecodeResult result =
ValidateFunctionBody(this->zone()->allocator(), enabled_features_,
module, &unused_detected_features, body);
DecodeResult result = ValidateFunctionBody(enabled_features_, module,
&unused_detected_features, body);
std::ostringstream str;
if (result.failed()) {
@ -3287,9 +3286,8 @@ TEST_F(FunctionBodyDecoderTest, Regression709741) {
for (size_t i = 0; i < arraysize(code); ++i) {
FunctionBody body(sigs.v_v(), 0, code, code + i);
WasmFeatures unused_detected_features;
DecodeResult result =
ValidateFunctionBody(this->zone()->allocator(), WasmFeatures::All(),
nullptr, &unused_detected_features, body);
DecodeResult result = ValidateFunctionBody(WasmFeatures::All(), nullptr,
&unused_detected_features, body);
if (result.ok()) {
std::ostringstream str;
str << "Expected verification to fail";