Commit 0f748aac authored by Hisham Muhammad's avatar Hisham Muhammad Committed by V8 LUCI CQ

[wasm][c-api] Add handle scope to fix wasm_module_validate

This adds a handle scope to Module::validate in C API
so that the wasm_module_validate can be usable from C.

Without the added handle scope, attempting to call
wasm_module_validate function from C code fails with:

    #
    # Fatal error in v8::HandleScope::CreateHandle()
    # Cannot create a handle without a HandleScope
    #

Bug: v8:12941
Change-Id: I2b4d5dccdaed9501f31447158ebf8e7906a1f8f0
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3692020Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81048}
parent a537be46
......@@ -124,6 +124,7 @@ Harshal Nandigramwar <pro.bbcom18@gmail.com>
Harshil Jain <twitharshil@gmail.com>
Henrique Ferreiro <henrique.ferreiro@gmail.com>
Hirofumi Mako <mkhrfm@gmail.com>
Hisham Muhammad <hisham@gobolinux.org>
Honggyu Kim <honggyu.kp@gmail.com>
Huáng Jùnliàng <jlhwung@gmail.com>
HyeockJin Kim <kherootz@gmail.com>
......
......@@ -1114,6 +1114,7 @@ auto Module::validate(Store* store_abs, const vec<byte_t>& binary) -> bool {
i::wasm::ModuleWireBytes bytes(
{reinterpret_cast<const uint8_t*>(binary.get()), binary.size()});
i::Isolate* isolate = impl(store_abs)->i_isolate();
i::HandleScope scope(isolate);
i::wasm::WasmFeatures features = i::wasm::WasmFeatures::FromIsolate(isolate);
return i::wasm::GetWasmEngine()->SyncValidate(isolate, features, bytes);
}
......
......@@ -36,6 +36,7 @@ v8_executable("wasm_api_tests") {
"memory.cc",
"multi-return.cc",
"reflect.cc",
"regressions.cc",
"run-all-wasm-api-tests.cc",
"serialize.cc",
"startup-errors.cc",
......
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "test/wasm-api-tests/wasm-api-test.h"
namespace v8 {
namespace internal {
namespace wasm {
TEST_F(WasmCapiTest, Regressions) {
FunctionSig sig(0, 0, nullptr);
byte code[] = {WASM_UNREACHABLE};
WasmFunctionBuilder* start_func = builder()->AddFunction(&sig);
start_func->EmitCode(code, static_cast<uint32_t>(sizeof(code)));
start_func->Emit(kExprEnd);
builder()->MarkStartFunction(start_func);
builder()->AddImport(base::CStrVector("dummy"), &sig);
// Ensure we can validate.
bool valid = Validate();
EXPECT_EQ(valid, true);
// Ensure we can compile after validating.
Compile();
}
} // namespace wasm
} // namespace internal
} // namespace v8
......@@ -46,6 +46,7 @@ class WasmCapiTest : public ::testing::Test {
wire_bytes_(zone_.get()),
builder_(zone_->New<WasmModuleBuilder>(zone_.get())),
exports_(ownvec<Extern>::make()),
binary_(vec<byte_t>::make()),
wasm_i_i_sig_(1, 1, wasm_i_i_sig_types_) {
store_ = Store::make(engine_.get());
cpp_i_i_sig_ =
......@@ -53,14 +54,28 @@ class WasmCapiTest : public ::testing::Test {
ownvec<ValType>::make(ValType::make(::wasm::I32)));
}
void Compile() {
builder_->WriteTo(&wire_bytes_);
size_t size = wire_bytes_.end() - wire_bytes_.begin();
vec<byte_t> binary = vec<byte_t>::make(
size,
reinterpret_cast<byte_t*>(const_cast<byte*>(wire_bytes_.begin())));
bool Validate() {
if (binary_.size() == 0) {
builder_->WriteTo(&wire_bytes_);
size_t size = wire_bytes_.end() - wire_bytes_.begin();
binary_ = vec<byte_t>::make(
size,
reinterpret_cast<byte_t*>(const_cast<byte*>(wire_bytes_.begin())));
}
return Module::validate(store_.get(), binary_);
}
module_ = Module::make(store_.get(), binary);
void Compile() {
if (binary_.size() == 0) {
builder_->WriteTo(&wire_bytes_);
size_t size = wire_bytes_.end() - wire_bytes_.begin();
binary_ = vec<byte_t>::make(
size,
reinterpret_cast<byte_t*>(const_cast<byte*>(wire_bytes_.begin())));
}
module_ = Module::make(store_.get(), binary_);
DCHECK_NE(module_.get(), nullptr);
}
......@@ -155,6 +170,7 @@ class WasmCapiTest : public ::testing::Test {
own<Module> module_;
own<Instance> instance_;
ownvec<Extern> exports_;
vec<byte_t> binary_;
own<FuncType> cpp_i_i_sig_;
ValueType wasm_i_i_sig_types_[2] = {kWasmI32, kWasmI32};
FunctionSig wasm_i_i_sig_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment