Commit 6a36b2a0 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] [cleanup] Remove CompileInstantiateWasmModuleForTesting

This is a testing-only function, which is semantically equivalent to a
SyncCompile followed by SyncInstantiate.
We add a new SyncCompileAndInstantiate function to do those two steps
in one go, and use this method instead.
For AsmJs modules, a new testing function CompileAndRunAsmWasmModule is
introduced.

This is part of our effort to reduce the number of special paths for
testing. It is connected with
https://chromium-review.googlesource.com/529210, but should not
conflict with it.
After landing both CLs, we can later also get rid of
InstantiateModuleForTesting.

R=ahaas@chromium.org, mtrofin@chromium.org
BUG=v8:6474

Change-Id: I7891e968370d5eb68803076ce2639c65a2799dcc
Reviewed-on: https://chromium-review.googlesource.com/529844Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#45852}
parent 1d0a5824
...@@ -802,6 +802,21 @@ MaybeHandle<WasmInstanceObject> wasm::SyncInstantiate( ...@@ -802,6 +802,21 @@ MaybeHandle<WasmInstanceObject> wasm::SyncInstantiate(
return builder.Build(); return builder.Build();
} }
MaybeHandle<WasmInstanceObject> wasm::SyncCompileAndInstantiate(
Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes,
MaybeHandle<JSReceiver> imports, MaybeHandle<JSArrayBuffer> memory) {
MaybeHandle<WasmModuleObject> module =
wasm::SyncCompile(isolate, thrower, bytes);
DCHECK_EQ(thrower->error(), module.is_null());
if (module.is_null()) return {};
MaybeHandle<WasmInstanceObject> instance = wasm::SyncInstantiate(
isolate, thrower, module.ToHandleChecked(), Handle<JSReceiver>::null(),
Handle<JSArrayBuffer>::null());
DCHECK_EQ(thrower->error(), instance.is_null());
return instance;
}
namespace { namespace {
void RejectPromise(Isolate* isolate, Handle<Context> context, void RejectPromise(Isolate* isolate, Handle<Context> context,
......
...@@ -476,6 +476,10 @@ V8_EXPORT_PRIVATE MaybeHandle<WasmInstanceObject> SyncInstantiate( ...@@ -476,6 +476,10 @@ V8_EXPORT_PRIVATE MaybeHandle<WasmInstanceObject> SyncInstantiate(
Handle<WasmModuleObject> module_object, MaybeHandle<JSReceiver> imports, Handle<WasmModuleObject> module_object, MaybeHandle<JSReceiver> imports,
MaybeHandle<JSArrayBuffer> memory); MaybeHandle<JSArrayBuffer> memory);
V8_EXPORT_PRIVATE MaybeHandle<WasmInstanceObject> SyncCompileAndInstantiate(
Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes,
MaybeHandle<JSReceiver> imports, MaybeHandle<JSArrayBuffer> memory);
V8_EXPORT_PRIVATE void AsyncCompile(Isolate* isolate, Handle<JSPromise> promise, V8_EXPORT_PRIVATE void AsyncCompile(Isolate* isolate, Handle<JSPromise> promise,
const ModuleWireBytes& bytes); const ModuleWireBytes& bytes);
......
...@@ -43,8 +43,8 @@ void TestModule(Zone* zone, WasmModuleBuilder* builder, ...@@ -43,8 +43,8 @@ void TestModule(Zone* zone, WasmModuleBuilder* builder,
Isolate* isolate = CcTest::InitIsolateOnce(); Isolate* isolate = CcTest::InitIsolateOnce();
HandleScope scope(isolate); HandleScope scope(isolate);
testing::SetupIsolateForWasmModule(isolate); testing::SetupIsolateForWasmModule(isolate);
int32_t result = testing::CompileAndRunWasmModule( int32_t result =
isolate, buffer.begin(), buffer.end(), ModuleOrigin::kWasmOrigin); testing::CompileAndRunWasmModule(isolate, buffer.begin(), buffer.end());
CHECK_EQ(expected_result, result); CHECK_EQ(expected_result, result);
} }
...@@ -56,8 +56,7 @@ void TestModuleException(Zone* zone, WasmModuleBuilder* builder) { ...@@ -56,8 +56,7 @@ void TestModuleException(Zone* zone, WasmModuleBuilder* builder) {
HandleScope scope(isolate); HandleScope scope(isolate);
testing::SetupIsolateForWasmModule(isolate); testing::SetupIsolateForWasmModule(isolate);
v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate)); v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
testing::CompileAndRunWasmModule(isolate, buffer.begin(), buffer.end(), testing::CompileAndRunWasmModule(isolate, buffer.begin(), buffer.end());
ModuleOrigin::kWasmOrigin);
CHECK(try_catch.HasCaught()); CHECK(try_catch.HasCaught());
isolate->clear_pending_exception(); isolate->clear_pending_exception();
} }
...@@ -697,10 +696,10 @@ TEST(TestInterruptLoop) { ...@@ -697,10 +696,10 @@ TEST(TestInterruptLoop) {
testing::SetupIsolateForWasmModule(isolate); testing::SetupIsolateForWasmModule(isolate);
ErrorThrower thrower(isolate, "Test"); ErrorThrower thrower(isolate, "Test");
const Handle<WasmInstanceObject> instance = const Handle<WasmInstanceObject> instance =
testing::CompileInstantiateWasmModuleForTesting( SyncCompileAndInstantiate(isolate, &thrower,
isolate, &thrower, buffer.begin(), buffer.end(), ModuleWireBytes(buffer.begin(), buffer.end()),
ModuleOrigin::kWasmOrigin); {}, {})
CHECK(!instance.is_null()); .ToHandleChecked();
Handle<JSArrayBuffer> memory(instance->memory_buffer(), isolate); Handle<JSArrayBuffer> memory(instance->memory_buffer(), isolate);
int32_t* memory_array = reinterpret_cast<int32_t*>(memory->backing_store()); int32_t* memory_array = reinterpret_cast<int32_t*>(memory->backing_store());
...@@ -779,10 +778,11 @@ TEST(Run_WasmModule_GrowMemOobFixedIndex) { ...@@ -779,10 +778,11 @@ TEST(Run_WasmModule_GrowMemOobFixedIndex) {
testing::SetupIsolateForWasmModule(isolate); testing::SetupIsolateForWasmModule(isolate);
ErrorThrower thrower(isolate, "Test"); ErrorThrower thrower(isolate, "Test");
Handle<JSObject> instance = testing::CompileInstantiateWasmModuleForTesting( Handle<WasmInstanceObject> instance =
isolate, &thrower, buffer.begin(), buffer.end(), SyncCompileAndInstantiate(isolate, &thrower,
ModuleOrigin::kWasmOrigin); ModuleWireBytes(buffer.begin(), buffer.end()),
CHECK(!instance.is_null()); {}, {})
.ToHandleChecked();
// Initial memory size is 16 pages, should trap till index > MemSize on // Initial memory size is 16 pages, should trap till index > MemSize on
// consecutive GrowMem calls // consecutive GrowMem calls
...@@ -827,11 +827,11 @@ TEST(Run_WasmModule_GrowMemOobVariableIndex) { ...@@ -827,11 +827,11 @@ TEST(Run_WasmModule_GrowMemOobVariableIndex) {
testing::SetupIsolateForWasmModule(isolate); testing::SetupIsolateForWasmModule(isolate);
ErrorThrower thrower(isolate, "Test"); ErrorThrower thrower(isolate, "Test");
Handle<JSObject> instance = testing::CompileInstantiateWasmModuleForTesting( Handle<WasmInstanceObject> instance =
isolate, &thrower, buffer.begin(), buffer.end(), SyncCompileAndInstantiate(isolate, &thrower,
ModuleOrigin::kWasmOrigin); ModuleWireBytes(buffer.begin(), buffer.end()),
{}, {})
CHECK(!instance.is_null()); .ToHandleChecked();
// Initial memory size is 16 pages, should trap till index > MemSize on // Initial memory size is 16 pages, should trap till index > MemSize on
// consecutive GrowMem calls // consecutive GrowMem calls
...@@ -959,9 +959,9 @@ TEST(InitDataAtTheUpperLimit) { ...@@ -959,9 +959,9 @@ TEST(InitDataAtTheUpperLimit) {
'c' // data bytes 'c' // data bytes
}; };
testing::CompileInstantiateWasmModuleForTesting(isolate, &thrower, data, SyncCompileAndInstantiate(isolate, &thrower,
data + arraysize(data), ModuleWireBytes(data, data + arraysize(data)), {},
ModuleOrigin::kWasmOrigin); {});
if (thrower.error()) { if (thrower.error()) {
thrower.Reify()->Print(); thrower.Reify()->Print();
CHECK(false); CHECK(false);
...@@ -996,9 +996,9 @@ TEST(EmptyMemoryNonEmptyDataSegment) { ...@@ -996,9 +996,9 @@ TEST(EmptyMemoryNonEmptyDataSegment) {
'c' // data bytes 'c' // data bytes
}; };
testing::CompileInstantiateWasmModuleForTesting(isolate, &thrower, data, SyncCompileAndInstantiate(isolate, &thrower,
data + arraysize(data), ModuleWireBytes(data, data + arraysize(data)), {},
ModuleOrigin::kWasmOrigin); {});
// It should not be possible to instantiate this module. // It should not be possible to instantiate this module.
CHECK(thrower.error()); CHECK(thrower.error());
} }
...@@ -1030,9 +1030,9 @@ TEST(EmptyMemoryEmptyDataSegment) { ...@@ -1030,9 +1030,9 @@ TEST(EmptyMemoryEmptyDataSegment) {
U32V_1(0), // source size U32V_1(0), // source size
}; };
testing::CompileInstantiateWasmModuleForTesting(isolate, &thrower, data, SyncCompileAndInstantiate(isolate, &thrower,
data + arraysize(data), ModuleWireBytes(data, data + arraysize(data)), {},
ModuleOrigin::kWasmOrigin); {});
// It should be possible to instantiate this module. // It should be possible to instantiate this module.
CHECK(!thrower.error()); CHECK(!thrower.error());
} }
...@@ -1064,9 +1064,9 @@ TEST(MemoryWithOOBEmptyDataSegment) { ...@@ -1064,9 +1064,9 @@ TEST(MemoryWithOOBEmptyDataSegment) {
U32V_1(0), // source size U32V_1(0), // source size
}; };
testing::CompileInstantiateWasmModuleForTesting(isolate, &thrower, data, SyncCompileAndInstantiate(isolate, &thrower,
data + arraysize(data), ModuleWireBytes(data, data + arraysize(data)), {},
ModuleOrigin::kWasmOrigin); {});
// It should not be possible to instantiate this module. // It should not be possible to instantiate this module.
CHECK(thrower.error()); CHECK(thrower.error());
} }
...@@ -1095,10 +1095,10 @@ TEST(Run_WasmModule_Buffer_Externalized_GrowMem) { ...@@ -1095,10 +1095,10 @@ TEST(Run_WasmModule_Buffer_Externalized_GrowMem) {
testing::SetupIsolateForWasmModule(isolate); testing::SetupIsolateForWasmModule(isolate);
ErrorThrower thrower(isolate, "Test"); ErrorThrower thrower(isolate, "Test");
const Handle<WasmInstanceObject> instance = const Handle<WasmInstanceObject> instance =
testing::CompileInstantiateWasmModuleForTesting( SyncCompileAndInstantiate(isolate, &thrower,
isolate, &thrower, buffer.begin(), buffer.end(), ModuleWireBytes(buffer.begin(), buffer.end()),
ModuleOrigin::kWasmOrigin); {}, {})
CHECK(!instance.is_null()); .ToHandleChecked();
Handle<JSArrayBuffer> memory(instance->memory_buffer(), isolate); Handle<JSArrayBuffer> memory(instance->memory_buffer(), isolate);
// Fake the Embedder flow by creating a memory object, externalize and grow. // Fake the Embedder flow by creating a memory object, externalize and grow.
......
...@@ -71,20 +71,6 @@ const Handle<WasmInstanceObject> InstantiateModuleForTesting( ...@@ -71,20 +71,6 @@ const Handle<WasmInstanceObject> InstantiateModuleForTesting(
return instance; return instance;
} }
const Handle<WasmInstanceObject> CompileInstantiateWasmModuleForTesting(
Isolate* isolate, ErrorThrower* thrower, const byte* module_start,
const byte* module_end, ModuleOrigin origin) {
std::unique_ptr<WasmModule> module = DecodeWasmModuleForTesting(
isolate, thrower, module_start, module_end, origin);
if (module == nullptr) {
thrower->CompileError("Wasm module decoding failed");
return Handle<WasmInstanceObject>::null();
}
return InstantiateModuleForTesting(isolate, thrower, module.get(),
ModuleWireBytes(module_start, module_end));
}
int32_t RunWasmModuleForTesting(Isolate* isolate, Handle<JSObject> instance, int32_t RunWasmModuleForTesting(Isolate* isolate, Handle<JSObject> instance,
int argc, Handle<Object> argv[], int argc, Handle<Object> argv[],
ModuleOrigin origin) { ModuleOrigin origin) {
...@@ -95,15 +81,36 @@ int32_t RunWasmModuleForTesting(Isolate* isolate, Handle<JSObject> instance, ...@@ -95,15 +81,36 @@ int32_t RunWasmModuleForTesting(Isolate* isolate, Handle<JSObject> instance,
} }
int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start,
const byte* module_end, ModuleOrigin origin) { const byte* module_end) {
HandleScope scope(isolate); HandleScope scope(isolate);
ErrorThrower thrower(isolate, "CompileAndRunWasmModule"); ErrorThrower thrower(isolate, "CompileAndRunWasmModule");
Handle<JSObject> instance = CompileInstantiateWasmModuleForTesting( MaybeHandle<WasmInstanceObject> instance = SyncCompileAndInstantiate(
isolate, &thrower, module_start, module_end, origin); isolate, &thrower, ModuleWireBytes(module_start, module_end), {}, {});
if (instance.is_null()) { if (instance.is_null()) {
return -1; return -1;
} }
return RunWasmModuleForTesting(isolate, instance, 0, nullptr, origin); return RunWasmModuleForTesting(isolate, instance.ToHandleChecked(), 0,
nullptr, kWasmOrigin);
}
int32_t CompileAndRunAsmWasmModule(Isolate* isolate, const byte* module_start,
const byte* module_end) {
HandleScope scope(isolate);
ErrorThrower thrower(isolate, "CompileAndRunAsmWasmModule");
MaybeHandle<WasmModuleObject> module = wasm::SyncCompileTranslatedAsmJs(
isolate, &thrower, ModuleWireBytes(module_start, module_end),
Handle<Script>::null(), Vector<const byte>());
DCHECK_EQ(thrower.error(), module.is_null());
if (module.is_null()) return -1;
MaybeHandle<WasmInstanceObject> instance = wasm::SyncInstantiate(
isolate, &thrower, module.ToHandleChecked(), Handle<JSReceiver>::null(),
Handle<JSArrayBuffer>::null());
DCHECK_EQ(thrower.error(), instance.is_null());
if (instance.is_null()) return -1;
return RunWasmModuleForTesting(isolate, instance.ToHandleChecked(), 0,
nullptr, kAsmJsOrigin);
} }
int32_t InterpretWasmModule(Isolate* isolate, int32_t InterpretWasmModule(Isolate* isolate,
Handle<WasmInstanceObject> instance, Handle<WasmInstanceObject> instance,
......
...@@ -40,7 +40,12 @@ int32_t CallWasmFunctionForTesting(Isolate* isolate, Handle<JSObject> instance, ...@@ -40,7 +40,12 @@ int32_t CallWasmFunctionForTesting(Isolate* isolate, Handle<JSObject> instance,
// Decode, verify, and run the function labeled "main" in the // Decode, verify, and run the function labeled "main" in the
// given encoded module. The module should have no imports. // given encoded module. The module should have no imports.
int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start,
const byte* module_end, ModuleOrigin origin); const byte* module_end);
// Decode, verify, and run the function labeled "main" in the given encoded
// module, originating from asm.js. The module should have no imports.
int32_t CompileAndRunAsmWasmModule(Isolate* isolate, const byte* module_start,
const byte* module_end);
// Interprets the given module, starting at the function specified by // Interprets the given module, starting at the function specified by
// {function_index}. The return type of the function has to be int32. The module // {function_index}. The return type of the function has to be int32. The module
...@@ -50,11 +55,6 @@ int32_t InterpretWasmModule(Isolate* isolate, ...@@ -50,11 +55,6 @@ int32_t InterpretWasmModule(Isolate* isolate,
ErrorThrower* thrower, int32_t function_index, ErrorThrower* thrower, int32_t function_index,
WasmVal* args, bool* possible_nondeterminism); WasmVal* args, bool* possible_nondeterminism);
// Compiles WasmModule bytes and return an instance of the compiled module.
const Handle<WasmInstanceObject> CompileInstantiateWasmModuleForTesting(
Isolate* isolate, ErrorThrower* thrower, const byte* module_start,
const byte* module_end, ModuleOrigin origin);
// Runs the module instance with arguments. // Runs the module instance with arguments.
int32_t RunWasmModuleForTesting(Isolate* isolate, Handle<JSObject> instance, int32_t RunWasmModuleForTesting(Isolate* isolate, Handle<JSObject> instance,
int argc, Handle<Object> argv[], int argc, Handle<Object> argv[],
......
...@@ -36,9 +36,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { ...@@ -36,9 +36,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
v8::Context::Scope context_scope(support->GetContext()); v8::Context::Scope context_scope(support->GetContext());
v8::TryCatch try_catch(isolate); v8::TryCatch try_catch(isolate);
v8::internal::wasm::testing::SetupIsolateForWasmModule(i_isolate); v8::internal::wasm::testing::SetupIsolateForWasmModule(i_isolate);
v8::internal::wasm::testing::CompileAndRunWasmModule( v8::internal::wasm::testing::CompileAndRunAsmWasmModule(i_isolate, data,
i_isolate, data, data + size, data + size);
v8::internal::wasm::ModuleOrigin::kAsmJsOrigin);
v8::internal::FLAG_wasm_max_mem_pages = max_mem_flag_value; v8::internal::FLAG_wasm_max_mem_pages = max_mem_flag_value;
v8::internal::FLAG_wasm_max_table_size = max_table_flag_value; v8::internal::FLAG_wasm_max_table_size = max_table_flag_value;
return 0; return 0;
......
...@@ -36,8 +36,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { ...@@ -36,8 +36,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
v8::Context::Scope context_scope(support->GetContext()); v8::Context::Scope context_scope(support->GetContext());
v8::TryCatch try_catch(isolate); v8::TryCatch try_catch(isolate);
v8::internal::wasm::testing::SetupIsolateForWasmModule(i_isolate); v8::internal::wasm::testing::SetupIsolateForWasmModule(i_isolate);
v8::internal::wasm::testing::CompileAndRunWasmModule( v8::internal::wasm::testing::CompileAndRunWasmModule(i_isolate, data,
i_isolate, data, data + size, v8::internal::wasm::kWasmOrigin); data + size);
v8::internal::FLAG_wasm_max_mem_pages = max_mem_flag_value; v8::internal::FLAG_wasm_max_mem_pages = max_mem_flag_value;
v8::internal::FLAG_wasm_max_table_size = max_table_flag_value; v8::internal::FLAG_wasm_max_table_size = max_table_flag_value;
return 0; return 0;
......
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