2015-12-11 12:26:16 +00:00
|
|
|
// Copyright 2015 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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-05-17 12:13:44 +00:00
|
|
|
#include "src/api/api-inl.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2016-10-14 14:24:59 +00:00
|
|
|
#include "src/snapshot/code-serializer.h"
|
2019-05-23 13:27:57 +00:00
|
|
|
#include "src/utils/version.h"
|
2016-08-16 08:07:11 +00:00
|
|
|
#include "src/wasm/module-decoder.h"
|
2018-01-18 10:52:52 +00:00
|
|
|
#include "src/wasm/wasm-engine.h"
|
2016-09-29 11:29:05 +00:00
|
|
|
#include "src/wasm/wasm-module-builder.h"
|
2015-12-11 12:26:16 +00:00
|
|
|
#include "src/wasm/wasm-module.h"
|
2017-08-30 13:53:15 +00:00
|
|
|
#include "src/wasm/wasm-objects-inl.h"
|
2015-12-11 12:26:16 +00:00
|
|
|
#include "src/wasm/wasm-opcodes.h"
|
|
|
|
|
|
|
|
#include "test/cctest/cctest.h"
|
2017-10-05 22:14:01 +00:00
|
|
|
#include "test/common/wasm/flag-utils.h"
|
2016-10-05 11:59:47 +00:00
|
|
|
#include "test/common/wasm/test-signatures.h"
|
2017-04-25 11:29:17 +00:00
|
|
|
#include "test/common/wasm/wasm-macro-gen.h"
|
2016-09-14 10:31:23 +00:00
|
|
|
#include "test/common/wasm/wasm-module-runner.h"
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2017-09-01 12:57:34 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
2018-06-21 10:58:58 +00:00
|
|
|
namespace test_run_wasm_module {
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2019-06-17 09:26:18 +00:00
|
|
|
using base::ReadLittleEndianValue;
|
|
|
|
using base::WriteLittleEndianValue;
|
2018-01-18 10:52:52 +00:00
|
|
|
using testing::CompileAndInstantiateForTesting;
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
namespace {
|
2018-06-21 10:58:58 +00:00
|
|
|
void Cleanup(Isolate* isolate = CcTest::InitIsolateOnce()) {
|
2016-11-08 10:33:42 +00:00
|
|
|
// By sending a low memory notifications, we will try hard to collect all
|
|
|
|
// garbage and will therefore also invoke all weak callbacks of actually
|
|
|
|
// unreachable persistent handles.
|
|
|
|
reinterpret_cast<v8::Isolate*>(isolate)->LowMemoryNotification();
|
|
|
|
}
|
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
void TestModule(Zone* zone, WasmModuleBuilder* builder,
|
|
|
|
int32_t expected_result) {
|
|
|
|
ZoneBuffer buffer(zone);
|
2019-07-08 09:16:39 +00:00
|
|
|
builder->WriteTo(&buffer);
|
2016-05-25 16:12:09 +00:00
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
2016-01-18 15:39:05 +00:00
|
|
|
HandleScope scope(isolate);
|
2016-09-17 01:30:09 +00:00
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
2017-06-09 18:37:54 +00:00
|
|
|
int32_t result =
|
|
|
|
testing::CompileAndRunWasmModule(isolate, buffer.begin(), buffer.end());
|
2015-12-11 12:26:16 +00:00
|
|
|
CHECK_EQ(expected_result, result);
|
|
|
|
}
|
2016-06-22 21:38:42 +00:00
|
|
|
|
2016-09-28 20:55:42 +00:00
|
|
|
void TestModuleException(Zone* zone, WasmModuleBuilder* builder) {
|
|
|
|
ZoneBuffer buffer(zone);
|
2019-07-08 09:16:39 +00:00
|
|
|
builder->WriteTo(&buffer);
|
2016-09-28 20:55:42 +00:00
|
|
|
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
|
|
|
v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
|
2017-06-09 18:37:54 +00:00
|
|
|
testing::CompileAndRunWasmModule(isolate, buffer.begin(), buffer.end());
|
2016-09-28 20:55:42 +00:00
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
isolate->clear_pending_exception();
|
|
|
|
}
|
|
|
|
|
2017-05-12 11:06:25 +00:00
|
|
|
void ExportAsMain(WasmFunctionBuilder* f) {
|
|
|
|
f->builder()->AddExport(CStrVector("main"), f);
|
|
|
|
}
|
2016-08-16 08:07:11 +00:00
|
|
|
|
2017-01-15 21:18:53 +00:00
|
|
|
#define EMIT_CODE_WITH_END(f, code) \
|
|
|
|
do { \
|
|
|
|
f->EmitCode(code, sizeof(code)); \
|
|
|
|
f->Emit(kExprEnd); \
|
|
|
|
} while (false)
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_Return114) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
static const int32_t kReturnValue = 114;
|
|
|
|
TestSignatures sigs;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f);
|
2017-01-09 13:57:26 +00:00
|
|
|
byte code[] = {WASM_I32V_2(kReturnValue)};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2016-11-08 10:33:42 +00:00
|
|
|
TestModule(&zone, builder, kReturnValue);
|
|
|
|
}
|
|
|
|
Cleanup();
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 17:55:36 +00:00
|
|
|
TEST(Run_WasmModule_CompilationHintsLazy) {
|
|
|
|
if (!FLAG_wasm_tier_up || !FLAG_liftoff) return;
|
|
|
|
{
|
|
|
|
EXPERIMENTAL_FLAG_SCOPE(compilation_hints);
|
|
|
|
|
|
|
|
static const int32_t kReturnValue = 114;
|
|
|
|
TestSignatures sigs;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
|
|
|
|
// Build module with one lazy function.
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2019-04-17 17:55:36 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {WASM_I32V_2(kReturnValue)};
|
|
|
|
EMIT_CODE_WITH_END(f, code);
|
|
|
|
f->SetCompilationHint(WasmCompilationHintStrategy::kLazy,
|
|
|
|
WasmCompilationHintTier::kBaseline,
|
|
|
|
WasmCompilationHintTier::kOptimized);
|
|
|
|
|
|
|
|
// Compile module. No function is actually compiled as the function is lazy.
|
|
|
|
ZoneBuffer buffer(&zone);
|
2019-07-08 09:16:39 +00:00
|
|
|
builder->WriteTo(&buffer);
|
2019-04-17 17:55:36 +00:00
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
|
|
|
ErrorThrower thrower(isolate, "CompileAndRunWasmModule");
|
|
|
|
MaybeHandle<WasmModuleObject> module = testing::CompileForTesting(
|
|
|
|
isolate, &thrower, ModuleWireBytes(buffer.begin(), buffer.end()));
|
|
|
|
CHECK(!module.is_null());
|
|
|
|
|
|
|
|
// Lazy function was not invoked and therefore not compiled yet.
|
2019-04-25 08:35:07 +00:00
|
|
|
static const int kFuncIndex = 0;
|
2019-04-17 17:55:36 +00:00
|
|
|
NativeModule* native_module = module.ToHandleChecked()->native_module();
|
2019-04-25 08:35:07 +00:00
|
|
|
CHECK(!native_module->HasCode(kFuncIndex));
|
|
|
|
auto* compilation_state = native_module->compilation_state();
|
|
|
|
CHECK(compilation_state->baseline_compilation_finished());
|
2019-04-17 17:55:36 +00:00
|
|
|
|
|
|
|
// Instantiate and invoke function.
|
|
|
|
MaybeHandle<WasmInstanceObject> instance =
|
|
|
|
isolate->wasm_engine()->SyncInstantiate(
|
|
|
|
isolate, &thrower, module.ToHandleChecked(), {}, {});
|
|
|
|
CHECK(!instance.is_null());
|
2020-08-11 08:48:31 +00:00
|
|
|
int32_t result = testing::CallWasmFunctionForTesting(
|
|
|
|
isolate, instance.ToHandleChecked(), "main", 0, nullptr);
|
2019-04-17 17:55:36 +00:00
|
|
|
CHECK_EQ(kReturnValue, result);
|
|
|
|
|
|
|
|
// Lazy function was invoked and therefore compiled.
|
2019-04-25 08:35:07 +00:00
|
|
|
CHECK(native_module->HasCode(kFuncIndex));
|
2019-04-17 17:55:36 +00:00
|
|
|
WasmCodeRefScope code_ref_scope;
|
2019-04-25 08:35:07 +00:00
|
|
|
ExecutionTier actual_tier = native_module->GetCode(kFuncIndex)->tier();
|
2020-08-04 11:09:23 +00:00
|
|
|
static_assert(ExecutionTier::kLiftoff < ExecutionTier::kTurbofan,
|
2019-04-17 17:55:36 +00:00
|
|
|
"Assume an order on execution tiers");
|
|
|
|
ExecutionTier baseline_tier = ExecutionTier::kLiftoff;
|
|
|
|
CHECK_LE(baseline_tier, actual_tier);
|
2019-04-25 08:35:07 +00:00
|
|
|
CHECK(compilation_state->baseline_compilation_finished());
|
2019-04-17 17:55:36 +00:00
|
|
|
}
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_CompilationHintsNoTiering) {
|
|
|
|
if (!FLAG_wasm_tier_up || !FLAG_liftoff) return;
|
|
|
|
{
|
|
|
|
EXPERIMENTAL_FLAG_SCOPE(compilation_hints);
|
|
|
|
|
|
|
|
static const int32_t kReturnValue = 114;
|
|
|
|
TestSignatures sigs;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
|
|
|
|
// Build module with regularly compiled function (no tiering).
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2019-04-17 17:55:36 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {WASM_I32V_2(kReturnValue)};
|
|
|
|
EMIT_CODE_WITH_END(f, code);
|
|
|
|
f->SetCompilationHint(WasmCompilationHintStrategy::kEager,
|
|
|
|
WasmCompilationHintTier::kBaseline,
|
|
|
|
WasmCompilationHintTier::kBaseline);
|
|
|
|
|
|
|
|
// Compile module.
|
|
|
|
ZoneBuffer buffer(&zone);
|
2019-07-08 09:16:39 +00:00
|
|
|
builder->WriteTo(&buffer);
|
2019-04-17 17:55:36 +00:00
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
|
|
|
ErrorThrower thrower(isolate, "CompileAndRunWasmModule");
|
|
|
|
MaybeHandle<WasmModuleObject> module = testing::CompileForTesting(
|
|
|
|
isolate, &thrower, ModuleWireBytes(buffer.begin(), buffer.end()));
|
|
|
|
CHECK(!module.is_null());
|
|
|
|
|
|
|
|
// Synchronous compilation finished and no tiering units were initialized.
|
2019-04-25 08:35:07 +00:00
|
|
|
static const int kFuncIndex = 0;
|
2019-04-17 17:55:36 +00:00
|
|
|
NativeModule* native_module = module.ToHandleChecked()->native_module();
|
2019-04-25 08:35:07 +00:00
|
|
|
CHECK(native_module->HasCode(kFuncIndex));
|
2019-04-17 17:55:36 +00:00
|
|
|
ExecutionTier expected_tier = ExecutionTier::kLiftoff;
|
2019-04-25 08:35:07 +00:00
|
|
|
WasmCodeRefScope code_ref_scope;
|
|
|
|
ExecutionTier actual_tier = native_module->GetCode(kFuncIndex)->tier();
|
2019-04-17 17:55:36 +00:00
|
|
|
CHECK_EQ(expected_tier, actual_tier);
|
2019-04-25 08:35:07 +00:00
|
|
|
auto* compilation_state = native_module->compilation_state();
|
|
|
|
CHECK(compilation_state->baseline_compilation_finished());
|
|
|
|
CHECK(compilation_state->top_tier_compilation_finished());
|
2019-04-17 17:55:36 +00:00
|
|
|
}
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_CompilationHintsTierUp) {
|
|
|
|
if (!FLAG_wasm_tier_up || !FLAG_liftoff) return;
|
|
|
|
{
|
|
|
|
EXPERIMENTAL_FLAG_SCOPE(compilation_hints);
|
|
|
|
|
|
|
|
static const int32_t kReturnValue = 114;
|
|
|
|
TestSignatures sigs;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
|
|
|
|
// Build module with tiering compilation hint.
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2019-04-17 17:55:36 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {WASM_I32V_2(kReturnValue)};
|
|
|
|
EMIT_CODE_WITH_END(f, code);
|
|
|
|
f->SetCompilationHint(WasmCompilationHintStrategy::kEager,
|
|
|
|
WasmCompilationHintTier::kBaseline,
|
|
|
|
WasmCompilationHintTier::kOptimized);
|
|
|
|
|
|
|
|
// Compile module.
|
|
|
|
ZoneBuffer buffer(&zone);
|
2019-07-08 09:16:39 +00:00
|
|
|
builder->WriteTo(&buffer);
|
2019-04-17 17:55:36 +00:00
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
|
|
|
ErrorThrower thrower(isolate, "CompileAndRunWasmModule");
|
|
|
|
MaybeHandle<WasmModuleObject> module = testing::CompileForTesting(
|
|
|
|
isolate, &thrower, ModuleWireBytes(buffer.begin(), buffer.end()));
|
|
|
|
CHECK(!module.is_null());
|
|
|
|
|
2019-04-25 08:35:07 +00:00
|
|
|
// Expect baseline or top tier code.
|
|
|
|
static const int kFuncIndex = 0;
|
2019-04-17 17:55:36 +00:00
|
|
|
NativeModule* native_module = module.ToHandleChecked()->native_module();
|
2019-04-25 08:35:07 +00:00
|
|
|
auto* compilation_state = native_module->compilation_state();
|
2020-08-04 11:09:23 +00:00
|
|
|
static_assert(ExecutionTier::kLiftoff < ExecutionTier::kTurbofan,
|
2019-04-17 17:55:36 +00:00
|
|
|
"Assume an order on execution tiers");
|
|
|
|
ExecutionTier baseline_tier = ExecutionTier::kLiftoff;
|
2019-04-25 08:35:07 +00:00
|
|
|
{
|
|
|
|
CHECK(native_module->HasCode(kFuncIndex));
|
|
|
|
WasmCodeRefScope code_ref_scope;
|
|
|
|
ExecutionTier actual_tier = native_module->GetCode(kFuncIndex)->tier();
|
|
|
|
CHECK_LE(baseline_tier, actual_tier);
|
|
|
|
CHECK(compilation_state->baseline_compilation_finished());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Busy wait for top tier compilation to finish.
|
|
|
|
while (!compilation_state->top_tier_compilation_finished()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect top tier code.
|
2019-04-17 17:55:36 +00:00
|
|
|
ExecutionTier top_tier = ExecutionTier::kTurbofan;
|
2019-04-25 08:35:07 +00:00
|
|
|
{
|
|
|
|
CHECK(native_module->HasCode(kFuncIndex));
|
|
|
|
WasmCodeRefScope code_ref_scope;
|
|
|
|
ExecutionTier actual_tier = native_module->GetCode(kFuncIndex)->tier();
|
|
|
|
CHECK_EQ(top_tier, actual_tier);
|
|
|
|
CHECK(compilation_state->baseline_compilation_finished());
|
|
|
|
CHECK(compilation_state->top_tier_compilation_finished());
|
|
|
|
}
|
2019-04-17 17:55:36 +00:00
|
|
|
}
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
2019-04-25 15:54:16 +00:00
|
|
|
TEST(Run_WasmModule_CompilationHintsLazyBaselineEagerTopTier) {
|
|
|
|
if (!FLAG_wasm_tier_up || !FLAG_liftoff) return;
|
|
|
|
{
|
|
|
|
EXPERIMENTAL_FLAG_SCOPE(compilation_hints);
|
|
|
|
|
|
|
|
static const int32_t kReturnValue = 114;
|
|
|
|
TestSignatures sigs;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
|
|
|
|
// Build module with tiering compilation hint.
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2019-04-25 15:54:16 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {WASM_I32V_2(kReturnValue)};
|
|
|
|
EMIT_CODE_WITH_END(f, code);
|
|
|
|
f->SetCompilationHint(
|
|
|
|
WasmCompilationHintStrategy::kLazyBaselineEagerTopTier,
|
|
|
|
WasmCompilationHintTier::kBaseline,
|
|
|
|
WasmCompilationHintTier::kOptimized);
|
|
|
|
|
|
|
|
// Compile module.
|
|
|
|
ZoneBuffer buffer(&zone);
|
2019-07-08 09:16:39 +00:00
|
|
|
builder->WriteTo(&buffer);
|
2019-04-25 15:54:16 +00:00
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
|
|
|
ErrorThrower thrower(isolate, "CompileAndRunWasmModule");
|
|
|
|
MaybeHandle<WasmModuleObject> module = testing::CompileForTesting(
|
|
|
|
isolate, &thrower, ModuleWireBytes(buffer.begin(), buffer.end()));
|
|
|
|
CHECK(!module.is_null());
|
|
|
|
|
|
|
|
NativeModule* native_module = module.ToHandleChecked()->native_module();
|
|
|
|
auto* compilation_state = native_module->compilation_state();
|
|
|
|
|
|
|
|
// Busy wait for top tier compilation to finish.
|
|
|
|
while (!compilation_state->top_tier_compilation_finished()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect top tier code.
|
2020-08-04 11:09:23 +00:00
|
|
|
static_assert(ExecutionTier::kLiftoff < ExecutionTier::kTurbofan,
|
2019-04-25 15:54:16 +00:00
|
|
|
"Assume an order on execution tiers");
|
|
|
|
static const int kFuncIndex = 0;
|
|
|
|
ExecutionTier top_tier = ExecutionTier::kTurbofan;
|
|
|
|
{
|
|
|
|
CHECK(native_module->HasCode(kFuncIndex));
|
|
|
|
WasmCodeRefScope code_ref_scope;
|
|
|
|
ExecutionTier actual_tier = native_module->GetCode(kFuncIndex)->tier();
|
|
|
|
CHECK_EQ(top_tier, actual_tier);
|
|
|
|
CHECK(compilation_state->baseline_compilation_finished());
|
|
|
|
CHECK(compilation_state->top_tier_compilation_finished());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
TEST(Run_WasmModule_CallAdd) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
TestSignatures sigs;
|
2016-05-17 17:53:46 +00:00
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-05-17 17:53:46 +00:00
|
|
|
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f1 = builder->AddFunction(sigs.i_ii());
|
|
|
|
uint16_t param1 = 0;
|
|
|
|
uint16_t param2 = 1;
|
|
|
|
byte code1[] = {
|
|
|
|
WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f1, code1);
|
2016-05-17 17:53:46 +00:00
|
|
|
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f2 = builder->AddFunction(sigs.i_v());
|
2016-05-17 17:53:46 +00:00
|
|
|
|
2016-11-08 10:33:42 +00:00
|
|
|
ExportAsMain(f2);
|
|
|
|
byte code2[] = {
|
2017-01-09 13:57:26 +00:00
|
|
|
WASM_CALL_FUNCTION(f1->func_index(), WASM_I32V_2(77), WASM_I32V_1(22))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f2, code2);
|
2016-11-08 10:33:42 +00:00
|
|
|
TestModule(&zone, builder, 99);
|
|
|
|
}
|
|
|
|
Cleanup();
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_ReadLoadedDataSegment) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
static const byte kDataSegmentDest0 = 12;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
TestSignatures sigs;
|
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
|
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {
|
2017-01-09 13:57:26 +00:00
|
|
|
WASM_LOAD_MEM(MachineType::Int32(), WASM_I32V_1(kDataSegmentDest0))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2017-12-02 00:30:37 +00:00
|
|
|
byte data[] = {0xAA, 0xBB, 0xCC, 0xDD};
|
2016-11-08 10:33:42 +00:00
|
|
|
builder->AddDataSegment(data, sizeof(data), kDataSegmentDest0);
|
2017-12-02 00:30:37 +00:00
|
|
|
TestModule(&zone, builder, 0xDDCCBBAA);
|
2016-11-08 10:33:42 +00:00
|
|
|
}
|
|
|
|
Cleanup();
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_CheckMemoryIsZero) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
static const int kCheckSize = 16 * 1024;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
TestSignatures sigs;
|
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
|
2016-12-21 13:43:00 +00:00
|
|
|
uint16_t localIndex = f->AddLocal(kWasmI32);
|
2016-11-08 10:33:42 +00:00
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {WASM_BLOCK_I(
|
|
|
|
WASM_WHILE(
|
|
|
|
WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)),
|
|
|
|
WASM_IF_ELSE(
|
|
|
|
WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)),
|
2017-01-09 13:57:26 +00:00
|
|
|
WASM_BRV(3, WASM_I32V_1(-1)),
|
|
|
|
WASM_INC_LOCAL_BY(localIndex, 4))),
|
|
|
|
WASM_I32V_1(11))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2016-11-08 10:33:42 +00:00
|
|
|
TestModule(&zone, builder, 11);
|
|
|
|
}
|
|
|
|
Cleanup();
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_CallMain_recursive) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
TestSignatures sigs;
|
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
|
2016-12-21 13:43:00 +00:00
|
|
|
uint16_t localIndex = f->AddLocal(kWasmI32);
|
2016-11-08 10:33:42 +00:00
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {
|
|
|
|
WASM_SET_LOCAL(localIndex,
|
|
|
|
WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)),
|
2017-01-09 13:57:26 +00:00
|
|
|
WASM_IF_ELSE_I(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_1(5)),
|
2016-11-08 10:33:42 +00:00
|
|
|
WASM_SEQ(WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO,
|
|
|
|
WASM_INC_LOCAL(localIndex)),
|
|
|
|
WASM_CALL_FUNCTION0(0)),
|
2017-01-09 13:57:26 +00:00
|
|
|
WASM_I32V_1(55))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2016-11-08 10:33:42 +00:00
|
|
|
TestModule(&zone, builder, 55);
|
|
|
|
}
|
|
|
|
Cleanup();
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_Global) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
TestSignatures sigs;
|
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2019-07-11 15:02:44 +00:00
|
|
|
uint32_t global1 = builder->AddGlobal(kWasmI32);
|
|
|
|
uint32_t global2 = builder->AddGlobal(kWasmI32);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f1 = builder->AddFunction(sigs.i_v());
|
|
|
|
byte code1[] = {
|
|
|
|
WASM_I32_ADD(WASM_GET_GLOBAL(global1), WASM_GET_GLOBAL(global2))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f1, code1);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f2 = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f2);
|
|
|
|
byte code2[] = {WASM_SET_GLOBAL(global1, WASM_I32V_1(56)),
|
|
|
|
WASM_SET_GLOBAL(global2, WASM_I32V_1(41)),
|
|
|
|
WASM_RETURN1(WASM_CALL_FUNCTION0(f1->func_index()))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f2, code2);
|
2016-11-08 10:33:42 +00:00
|
|
|
TestModule(&zone, builder, 97);
|
|
|
|
}
|
|
|
|
Cleanup();
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
2016-08-16 08:07:11 +00:00
|
|
|
|
2016-10-05 09:11:54 +00:00
|
|
|
TEST(MemorySize) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
// Initial memory size is 16, see wasm-module-builder.cc
|
|
|
|
static const int kExpectedValue = 16;
|
|
|
|
TestSignatures sigs;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {WASM_MEMORY_SIZE};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2016-11-08 10:33:42 +00:00
|
|
|
TestModule(&zone, builder, kExpectedValue);
|
|
|
|
}
|
|
|
|
Cleanup();
|
2016-10-05 09:11:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 01:38:10 +00:00
|
|
|
TEST(Run_WasmModule_MemSize_GrowMem) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
2018-10-26 17:28:37 +00:00
|
|
|
// Initial memory size = 16 + MemoryGrow(10)
|
2016-11-08 10:33:42 +00:00
|
|
|
static const int kExpectedValue = 26;
|
|
|
|
TestSignatures sigs;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f);
|
2017-01-09 13:57:26 +00:00
|
|
|
byte code[] = {WASM_GROW_MEMORY(WASM_I32V_1(10)), WASM_DROP,
|
|
|
|
WASM_MEMORY_SIZE};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2016-11-08 10:33:42 +00:00
|
|
|
TestModule(&zone, builder, kExpectedValue);
|
|
|
|
}
|
|
|
|
Cleanup();
|
2016-09-02 01:38:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
TEST(MemoryGrowZero) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
// Initial memory size is 16, see wasm-module-builder.cc
|
|
|
|
static const int kExpectedValue = 16;
|
|
|
|
TestSignatures sigs;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {WASM_GROW_MEMORY(WASM_I32V(0))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2016-11-08 10:33:42 +00:00
|
|
|
TestModule(&zone, builder, kExpectedValue);
|
|
|
|
}
|
|
|
|
Cleanup();
|
2016-10-06 11:44:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 10:56:48 +00:00
|
|
|
class InterruptThread : public v8::base::Thread {
|
|
|
|
public:
|
|
|
|
explicit InterruptThread(Isolate* isolate, int32_t* memory)
|
|
|
|
: Thread(Options("TestInterruptLoop")),
|
|
|
|
isolate_(isolate),
|
|
|
|
memory_(memory) {}
|
|
|
|
|
|
|
|
static void OnInterrupt(v8::Isolate* isolate, void* data) {
|
|
|
|
int32_t* m = reinterpret_cast<int32_t*>(data);
|
|
|
|
// Set the interrupt location to 0 to break the loop in {TestInterruptLoop}.
|
2018-04-13 22:28:05 +00:00
|
|
|
Address ptr = reinterpret_cast<Address>(&m[interrupt_location_]);
|
2016-11-07 23:25:24 +00:00
|
|
|
WriteLittleEndianValue<int32_t>(ptr, interrupt_value_);
|
2016-10-13 10:56:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 14:54:08 +00:00
|
|
|
void Run() override {
|
2016-10-13 10:56:48 +00:00
|
|
|
// Wait for the main thread to write the signal value.
|
2016-11-07 23:25:24 +00:00
|
|
|
int32_t val = 0;
|
|
|
|
do {
|
|
|
|
val = memory_[0];
|
2018-04-13 22:28:05 +00:00
|
|
|
val = ReadLittleEndianValue<int32_t>(reinterpret_cast<Address>(&val));
|
2016-11-07 23:25:24 +00:00
|
|
|
} while (val != signal_value_);
|
2016-10-13 10:56:48 +00:00
|
|
|
isolate_->RequestInterrupt(&OnInterrupt, const_cast<int32_t*>(memory_));
|
|
|
|
}
|
|
|
|
|
|
|
|
Isolate* isolate_;
|
|
|
|
volatile int32_t* memory_;
|
|
|
|
static const int32_t interrupt_location_ = 10;
|
|
|
|
static const int32_t interrupt_value_ = 154;
|
|
|
|
static const int32_t signal_value_ = 1221;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(TestInterruptLoop) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
// Do not dump the module of this test because it contains an infinite loop.
|
|
|
|
if (FLAG_dump_wasm_module) return;
|
|
|
|
|
|
|
|
// This test tests that WebAssembly loops can be interrupted, i.e. that if
|
|
|
|
// an
|
|
|
|
// InterruptCallback is registered by {Isolate::RequestInterrupt}, then the
|
|
|
|
// InterruptCallback is eventually called even if a loop in WebAssembly code
|
|
|
|
// is executed.
|
|
|
|
// Test setup:
|
|
|
|
// The main thread executes a WebAssembly function with a loop. In the loop
|
|
|
|
// {signal_value_} is written to memory to signal a helper thread that the
|
|
|
|
// main thread reached the loop in the WebAssembly program. When the helper
|
|
|
|
// thread reads {signal_value_} from memory, it registers the
|
|
|
|
// InterruptCallback. Upon exeution, the InterruptCallback write into the
|
|
|
|
// WebAssemblyMemory to end the loop in the WebAssembly program.
|
|
|
|
TestSignatures sigs;
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {
|
|
|
|
WASM_LOOP(
|
|
|
|
WASM_IFB(WASM_NOT(WASM_LOAD_MEM(
|
2016-10-13 10:56:48 +00:00
|
|
|
MachineType::Int32(),
|
|
|
|
WASM_I32V(InterruptThread::interrupt_location_ * 4))),
|
|
|
|
WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO,
|
|
|
|
WASM_I32V(InterruptThread::signal_value_)),
|
|
|
|
WASM_BR(1))),
|
2016-11-08 10:33:42 +00:00
|
|
|
WASM_I32V(121)};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2016-11-08 10:33:42 +00:00
|
|
|
ZoneBuffer buffer(&zone);
|
2019-07-08 09:16:39 +00:00
|
|
|
builder->WriteTo(&buffer);
|
2016-10-13 10:56:48 +00:00
|
|
|
|
2016-11-08 10:33:42 +00:00
|
|
|
HandleScope scope(isolate);
|
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
|
|
|
ErrorThrower thrower(isolate, "Test");
|
2016-11-16 16:17:22 +00:00
|
|
|
const Handle<WasmInstanceObject> instance =
|
2018-01-18 10:52:52 +00:00
|
|
|
CompileAndInstantiateForTesting(
|
|
|
|
isolate, &thrower, ModuleWireBytes(buffer.begin(), buffer.end()))
|
2017-06-09 18:37:54 +00:00
|
|
|
.ToHandleChecked();
|
2016-11-08 10:33:42 +00:00
|
|
|
|
2017-10-25 11:24:22 +00:00
|
|
|
Handle<JSArrayBuffer> memory(instance->memory_object().array_buffer(),
|
|
|
|
isolate);
|
2016-11-08 10:33:42 +00:00
|
|
|
int32_t* memory_array = reinterpret_cast<int32_t*>(memory->backing_store());
|
|
|
|
|
|
|
|
InterruptThread thread(isolate, memory_array);
|
2019-07-29 13:09:02 +00:00
|
|
|
CHECK(thread.Start());
|
2020-08-11 08:48:31 +00:00
|
|
|
testing::CallWasmFunctionForTesting(isolate, instance, "main", 0, nullptr);
|
2018-04-13 22:28:05 +00:00
|
|
|
Address address = reinterpret_cast<Address>(
|
|
|
|
&memory_array[InterruptThread::interrupt_location_]);
|
2016-11-08 10:33:42 +00:00
|
|
|
CHECK_EQ(InterruptThread::interrupt_value_,
|
2018-04-13 22:28:05 +00:00
|
|
|
ReadLittleEndianValue<int32_t>(address));
|
2016-11-08 10:33:42 +00:00
|
|
|
}
|
|
|
|
Cleanup();
|
2016-10-13 10:56:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
TEST(Run_WasmModule_MemoryGrowInIf) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
TestSignatures sigs;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {WASM_IF_ELSE_I(WASM_I32V(0), WASM_GROW_MEMORY(WASM_I32V(1)),
|
|
|
|
WASM_I32V(12))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2016-11-08 10:33:42 +00:00
|
|
|
TestModule(&zone, builder, 12);
|
|
|
|
}
|
|
|
|
Cleanup();
|
2016-08-31 12:32:13 +00:00
|
|
|
}
|
2016-09-28 20:55:42 +00:00
|
|
|
|
|
|
|
TEST(Run_WasmModule_GrowMemOobOffset) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
static const int kPageSize = 0x10000;
|
2018-10-26 17:28:37 +00:00
|
|
|
// Initial memory size = 16 + MemoryGrow(10)
|
2016-11-08 10:33:42 +00:00
|
|
|
static const int index = kPageSize * 17 + 4;
|
2017-12-02 00:30:37 +00:00
|
|
|
int value = 0xACED;
|
2016-11-08 10:33:42 +00:00
|
|
|
TestSignatures sigs;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
ExportAsMain(f);
|
2017-01-09 13:57:26 +00:00
|
|
|
byte code[] = {WASM_GROW_MEMORY(WASM_I32V_1(1)),
|
2016-11-08 10:33:42 +00:00
|
|
|
WASM_STORE_MEM(MachineType::Int32(), WASM_I32V(index),
|
|
|
|
WASM_I32V(value))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2016-11-08 10:33:42 +00:00
|
|
|
TestModuleException(&zone, builder);
|
|
|
|
}
|
|
|
|
Cleanup();
|
2016-09-28 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_GrowMemOobFixedIndex) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
static const int kPageSize = 0x10000;
|
2018-10-26 17:28:37 +00:00
|
|
|
// Initial memory size = 16 + MemoryGrow(10)
|
2016-11-08 10:33:42 +00:00
|
|
|
static const int index = kPageSize * 26 + 4;
|
2017-12-02 00:30:37 +00:00
|
|
|
int value = 0xACED;
|
2016-11-08 10:33:42 +00:00
|
|
|
TestSignatures sigs;
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
Zone zone(isolate->allocator(), ZONE_NAME);
|
2016-09-28 20:55:42 +00:00
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i());
|
|
|
|
ExportAsMain(f);
|
|
|
|
byte code[] = {WASM_GROW_MEMORY(WASM_GET_LOCAL(0)), WASM_DROP,
|
|
|
|
WASM_STORE_MEM(MachineType::Int32(), WASM_I32V(index),
|
|
|
|
WASM_I32V(value)),
|
|
|
|
WASM_LOAD_MEM(MachineType::Int32(), WASM_I32V(index))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2016-09-28 20:55:42 +00:00
|
|
|
|
2016-11-08 10:33:42 +00:00
|
|
|
HandleScope scope(isolate);
|
|
|
|
ZoneBuffer buffer(&zone);
|
2019-07-08 09:16:39 +00:00
|
|
|
builder->WriteTo(&buffer);
|
2016-11-08 10:33:42 +00:00
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
2016-09-28 20:55:42 +00:00
|
|
|
|
2016-11-08 10:33:42 +00:00
|
|
|
ErrorThrower thrower(isolate, "Test");
|
2017-06-09 18:37:54 +00:00
|
|
|
Handle<WasmInstanceObject> instance =
|
2018-01-18 10:52:52 +00:00
|
|
|
CompileAndInstantiateForTesting(
|
|
|
|
isolate, &thrower, ModuleWireBytes(buffer.begin(), buffer.end()))
|
2017-06-09 18:37:54 +00:00
|
|
|
.ToHandleChecked();
|
2016-11-08 10:33:42 +00:00
|
|
|
|
|
|
|
// Initial memory size is 16 pages, should trap till index > MemSize on
|
|
|
|
// consecutive GrowMem calls
|
|
|
|
for (uint32_t i = 1; i < 5; i++) {
|
|
|
|
Handle<Object> params[1] = {Handle<Object>(Smi::FromInt(i), isolate)};
|
|
|
|
v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
|
2020-08-11 08:48:31 +00:00
|
|
|
testing::CallWasmFunctionForTesting(isolate, instance, "main", 1, params);
|
2016-11-08 10:33:42 +00:00
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
isolate->clear_pending_exception();
|
|
|
|
}
|
2016-09-28 20:55:42 +00:00
|
|
|
|
2016-11-08 10:33:42 +00:00
|
|
|
Handle<Object> params[1] = {Handle<Object>(Smi::FromInt(1), isolate)};
|
2020-08-11 08:48:31 +00:00
|
|
|
int32_t result = testing::CallWasmFunctionForTesting(isolate, instance,
|
|
|
|
"main", 1, params);
|
2017-12-02 00:30:37 +00:00
|
|
|
CHECK_EQ(0xACED, result);
|
2016-09-28 20:55:42 +00:00
|
|
|
}
|
2016-11-08 10:33:42 +00:00
|
|
|
Cleanup();
|
2016-09-28 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_GrowMemOobVariableIndex) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
static const int kPageSize = 0x10000;
|
2017-12-02 00:30:37 +00:00
|
|
|
int value = 0xACED;
|
2016-11-08 10:33:42 +00:00
|
|
|
TestSignatures sigs;
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
2016-09-28 20:55:42 +00:00
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i());
|
|
|
|
ExportAsMain(f);
|
2017-01-09 13:57:26 +00:00
|
|
|
byte code[] = {WASM_GROW_MEMORY(WASM_I32V_1(1)), WASM_DROP,
|
2016-11-08 10:33:42 +00:00
|
|
|
WASM_STORE_MEM(MachineType::Int32(), WASM_GET_LOCAL(0),
|
|
|
|
WASM_I32V(value)),
|
|
|
|
WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f, code);
|
2016-09-28 20:55:42 +00:00
|
|
|
|
2016-11-08 10:33:42 +00:00
|
|
|
HandleScope scope(isolate);
|
|
|
|
ZoneBuffer buffer(&zone);
|
2019-07-08 09:16:39 +00:00
|
|
|
builder->WriteTo(&buffer);
|
2016-11-08 10:33:42 +00:00
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
2016-09-28 20:55:42 +00:00
|
|
|
|
2016-11-08 10:33:42 +00:00
|
|
|
ErrorThrower thrower(isolate, "Test");
|
2017-06-09 18:37:54 +00:00
|
|
|
Handle<WasmInstanceObject> instance =
|
2018-01-18 10:52:52 +00:00
|
|
|
CompileAndInstantiateForTesting(
|
|
|
|
isolate, &thrower, ModuleWireBytes(buffer.begin(), buffer.end()))
|
2017-06-09 18:37:54 +00:00
|
|
|
.ToHandleChecked();
|
2016-11-08 10:33:42 +00:00
|
|
|
|
|
|
|
// Initial memory size is 16 pages, should trap till index > MemSize on
|
|
|
|
// consecutive GrowMem calls
|
|
|
|
for (int i = 1; i < 5; i++) {
|
|
|
|
Handle<Object> params[1] = {
|
|
|
|
Handle<Object>(Smi::FromInt((16 + i) * kPageSize - 3), isolate)};
|
|
|
|
v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
|
2020-08-11 08:48:31 +00:00
|
|
|
testing::CallWasmFunctionForTesting(isolate, instance, "main", 1, params);
|
2016-11-08 10:33:42 +00:00
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
isolate->clear_pending_exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i < 5; i++) {
|
|
|
|
Handle<Object> params[1] = {
|
|
|
|
Handle<Object>(Smi::FromInt((20 + i) * kPageSize - 4), isolate)};
|
2020-08-11 08:48:31 +00:00
|
|
|
int32_t result = testing::CallWasmFunctionForTesting(isolate, instance,
|
|
|
|
"main", 1, params);
|
2017-12-02 00:30:37 +00:00
|
|
|
CHECK_EQ(0xACED, result);
|
2016-11-08 10:33:42 +00:00
|
|
|
}
|
2016-09-28 20:55:42 +00:00
|
|
|
|
|
|
|
v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
|
2016-11-08 10:33:42 +00:00
|
|
|
Handle<Object> params[1] = {
|
|
|
|
Handle<Object>(Smi::FromInt(25 * kPageSize), isolate)};
|
2020-08-11 08:48:31 +00:00
|
|
|
testing::CallWasmFunctionForTesting(isolate, instance, "main", 1, params);
|
2016-09-28 20:55:42 +00:00
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
isolate->clear_pending_exception();
|
|
|
|
}
|
2016-11-08 10:33:42 +00:00
|
|
|
Cleanup();
|
2016-09-28 20:55:42 +00:00
|
|
|
}
|
2016-10-06 15:43:10 +00:00
|
|
|
|
|
|
|
TEST(Run_WasmModule_Global_init) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
TestSignatures sigs;
|
2016-10-06 15:43:10 +00:00
|
|
|
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
uint32_t global1 =
|
2019-07-11 15:02:44 +00:00
|
|
|
builder->AddGlobal(kWasmI32, false, WasmInitExpr(777777));
|
2016-11-08 10:33:42 +00:00
|
|
|
uint32_t global2 =
|
2019-07-11 15:02:44 +00:00
|
|
|
builder->AddGlobal(kWasmI32, false, WasmInitExpr(222222));
|
2016-11-08 10:33:42 +00:00
|
|
|
WasmFunctionBuilder* f1 = builder->AddFunction(sigs.i_v());
|
|
|
|
byte code[] = {
|
|
|
|
WASM_I32_ADD(WASM_GET_GLOBAL(global1), WASM_GET_GLOBAL(global2))};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f1, code);
|
2016-10-06 15:43:10 +00:00
|
|
|
ExportAsMain(f1);
|
2016-11-08 10:33:42 +00:00
|
|
|
TestModule(&zone, builder, 999999);
|
2016-10-06 15:43:10 +00:00
|
|
|
}
|
2016-11-08 10:33:42 +00:00
|
|
|
Cleanup();
|
|
|
|
}
|
2016-10-06 15:43:10 +00:00
|
|
|
|
2016-11-08 10:33:42 +00:00
|
|
|
template <typename CType>
|
2016-12-21 13:43:00 +00:00
|
|
|
static void RunWasmModuleGlobalInitTest(ValueType type, CType expected) {
|
2016-11-08 10:33:42 +00:00
|
|
|
{
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
|
|
TestSignatures sigs;
|
2016-10-06 15:43:10 +00:00
|
|
|
|
2016-12-21 13:43:00 +00:00
|
|
|
ValueType types[] = {type};
|
2016-11-08 10:33:42 +00:00
|
|
|
FunctionSig sig(1, 0, types);
|
|
|
|
|
|
|
|
for (int padding = 0; padding < 5; padding++) {
|
|
|
|
// Test with a simple initializer
|
2020-07-09 11:51:58 +00:00
|
|
|
WasmModuleBuilder* builder = zone.New<WasmModuleBuilder>(&zone);
|
2016-11-08 10:33:42 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < padding; i++) { // pad global before
|
2019-07-11 15:02:44 +00:00
|
|
|
builder->AddGlobal(kWasmI32, false, WasmInitExpr(i + 20000));
|
2016-11-08 10:33:42 +00:00
|
|
|
}
|
2019-07-11 15:02:44 +00:00
|
|
|
uint32_t global = builder->AddGlobal(type, false, WasmInitExpr(expected));
|
2016-11-08 10:33:42 +00:00
|
|
|
for (int i = 0; i < padding; i++) { // pad global after
|
2019-07-11 15:02:44 +00:00
|
|
|
builder->AddGlobal(kWasmI32, false, WasmInitExpr(i + 30000));
|
2016-11-08 10:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WasmFunctionBuilder* f1 = builder->AddFunction(&sig);
|
|
|
|
byte code[] = {WASM_GET_GLOBAL(global)};
|
2017-01-15 21:18:53 +00:00
|
|
|
EMIT_CODE_WITH_END(f1, code);
|
2016-11-08 10:33:42 +00:00
|
|
|
ExportAsMain(f1);
|
|
|
|
TestModule(&zone, builder, expected);
|
2016-10-06 15:43:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-08 10:33:42 +00:00
|
|
|
Cleanup();
|
2016-10-06 15:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_Global_i32) {
|
2016-12-21 13:43:00 +00:00
|
|
|
RunWasmModuleGlobalInitTest<int32_t>(kWasmI32, -983489);
|
|
|
|
RunWasmModuleGlobalInitTest<int32_t>(kWasmI32, 11223344);
|
2016-10-06 15:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_Global_f32) {
|
2016-12-21 13:43:00 +00:00
|
|
|
RunWasmModuleGlobalInitTest<float>(kWasmF32, -983.9f);
|
|
|
|
RunWasmModuleGlobalInitTest<float>(kWasmF32, 1122.99f);
|
2016-10-06 15:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Run_WasmModule_Global_f64) {
|
2016-12-21 13:43:00 +00:00
|
|
|
RunWasmModuleGlobalInitTest<double>(kWasmF64, -833.9);
|
|
|
|
RunWasmModuleGlobalInitTest<double>(kWasmF64, 86374.25);
|
2016-10-06 15:43:10 +00:00
|
|
|
}
|
2016-11-09 11:38:30 +00:00
|
|
|
|
|
|
|
TEST(InitDataAtTheUpperLimit) {
|
|
|
|
{
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
|
|
|
|
|
|
|
ErrorThrower thrower(isolate, "Run_WasmModule_InitDataAtTheUpperLimit");
|
|
|
|
|
|
|
|
const byte data[] = {
|
2017-10-13 07:30:13 +00:00
|
|
|
WASM_MODULE_HEADER, // --
|
|
|
|
kMemorySectionCode, // --
|
|
|
|
U32V_1(4), // section size
|
|
|
|
ENTRY_COUNT(1), // --
|
2020-09-24 08:00:58 +00:00
|
|
|
kWithMaximum, // --
|
2017-10-13 07:30:13 +00:00
|
|
|
1, // initial size
|
|
|
|
2, // maximum size
|
|
|
|
kDataSectionCode, // --
|
|
|
|
U32V_1(9), // section size
|
|
|
|
ENTRY_COUNT(1), // --
|
|
|
|
0, // linear memory index
|
2017-12-02 00:30:37 +00:00
|
|
|
WASM_I32V_3(0xFFFF), // destination offset
|
2016-11-09 11:38:30 +00:00
|
|
|
kExprEnd,
|
|
|
|
U32V_1(1), // source size
|
|
|
|
'c' // data bytes
|
|
|
|
};
|
|
|
|
|
2018-01-18 10:52:52 +00:00
|
|
|
CompileAndInstantiateForTesting(
|
|
|
|
isolate, &thrower, ModuleWireBytes(data, data + arraysize(data)));
|
2016-11-09 11:38:30 +00:00
|
|
|
if (thrower.error()) {
|
2018-07-12 10:06:42 +00:00
|
|
|
thrower.Reify()->Print();
|
2017-12-19 11:39:50 +00:00
|
|
|
FATAL("compile or instantiate error");
|
2016-11-09 11:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Cleanup();
|
|
|
|
}
|
2016-11-11 13:02:59 +00:00
|
|
|
|
|
|
|
TEST(EmptyMemoryNonEmptyDataSegment) {
|
|
|
|
{
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
|
|
|
|
|
|
|
ErrorThrower thrower(isolate, "Run_WasmModule_InitDataAtTheUpperLimit");
|
|
|
|
|
|
|
|
const byte data[] = {
|
2017-10-13 07:30:13 +00:00
|
|
|
WASM_MODULE_HEADER, // --
|
|
|
|
kMemorySectionCode, // --
|
|
|
|
U32V_1(4), // section size
|
|
|
|
ENTRY_COUNT(1), // --
|
2020-09-24 08:00:58 +00:00
|
|
|
kWithMaximum, // --
|
2017-10-13 07:30:13 +00:00
|
|
|
0, // initial size
|
|
|
|
0, // maximum size
|
|
|
|
kDataSectionCode, // --
|
|
|
|
U32V_1(7), // section size
|
|
|
|
ENTRY_COUNT(1), // --
|
|
|
|
0, // linear memory index
|
|
|
|
WASM_I32V_1(8), // destination offset
|
2016-11-11 13:02:59 +00:00
|
|
|
kExprEnd,
|
|
|
|
U32V_1(1), // source size
|
|
|
|
'c' // data bytes
|
|
|
|
};
|
|
|
|
|
2018-01-18 10:52:52 +00:00
|
|
|
CompileAndInstantiateForTesting(
|
|
|
|
isolate, &thrower, ModuleWireBytes(data, data + arraysize(data)));
|
2016-11-11 13:02:59 +00:00
|
|
|
// It should not be possible to instantiate this module.
|
|
|
|
CHECK(thrower.error());
|
|
|
|
}
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(EmptyMemoryEmptyDataSegment) {
|
|
|
|
{
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
|
|
|
|
|
|
|
ErrorThrower thrower(isolate, "Run_WasmModule_InitDataAtTheUpperLimit");
|
|
|
|
|
|
|
|
const byte data[] = {
|
2017-10-13 07:30:13 +00:00
|
|
|
WASM_MODULE_HEADER, // --
|
|
|
|
kMemorySectionCode, // --
|
|
|
|
U32V_1(4), // section size
|
|
|
|
ENTRY_COUNT(1), // --
|
2020-09-24 08:00:58 +00:00
|
|
|
kWithMaximum, // --
|
2017-10-13 07:30:13 +00:00
|
|
|
0, // initial size
|
|
|
|
0, // maximum size
|
|
|
|
kDataSectionCode, // --
|
|
|
|
U32V_1(6), // section size
|
|
|
|
ENTRY_COUNT(1), // --
|
|
|
|
0, // linear memory index
|
|
|
|
WASM_I32V_1(0), // destination offset
|
2016-11-11 13:02:59 +00:00
|
|
|
kExprEnd,
|
|
|
|
U32V_1(0), // source size
|
|
|
|
};
|
|
|
|
|
2018-01-18 10:52:52 +00:00
|
|
|
CompileAndInstantiateForTesting(
|
|
|
|
isolate, &thrower, ModuleWireBytes(data, data + arraysize(data)));
|
2016-11-11 13:02:59 +00:00
|
|
|
// It should be possible to instantiate this module.
|
|
|
|
CHECK(!thrower.error());
|
|
|
|
}
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MemoryWithOOBEmptyDataSegment) {
|
|
|
|
{
|
2019-07-18 16:20:21 +00:00
|
|
|
FlagScope<bool> no_bulk_memory(
|
|
|
|
&v8::internal::FLAG_experimental_wasm_bulk_memory, false);
|
2016-11-11 13:02:59 +00:00
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
testing::SetupIsolateForWasmModule(isolate);
|
|
|
|
|
|
|
|
ErrorThrower thrower(isolate, "Run_WasmModule_InitDataAtTheUpperLimit");
|
|
|
|
|
|
|
|
const byte data[] = {
|
|
|
|
WASM_MODULE_HEADER, // --
|
|
|
|
kMemorySectionCode, // --
|
|
|
|
U32V_1(4), // section size
|
|
|
|
ENTRY_COUNT(1), // --
|
2020-09-24 08:00:58 +00:00
|
|
|
kWithMaximum, // --
|
2016-11-11 13:02:59 +00:00
|
|
|
1, // initial size
|
|
|
|
1, // maximum size
|
|
|
|
kDataSectionCode, // --
|
|
|
|
U32V_1(9), // section size
|
|
|
|
ENTRY_COUNT(1), // --
|
|
|
|
0, // linear memory index
|
2017-12-02 00:30:37 +00:00
|
|
|
WASM_I32V_4(0x2468ACE), // destination offset
|
2016-11-11 13:02:59 +00:00
|
|
|
kExprEnd,
|
|
|
|
U32V_1(0), // source size
|
|
|
|
};
|
|
|
|
|
2018-01-18 10:52:52 +00:00
|
|
|
CompileAndInstantiateForTesting(
|
|
|
|
isolate, &thrower, ModuleWireBytes(data, data + arraysize(data)));
|
2017-01-23 17:21:39 +00:00
|
|
|
// It should not be possible to instantiate this module.
|
|
|
|
CHECK(thrower.error());
|
2016-11-11 13:02:59 +00:00
|
|
|
}
|
|
|
|
Cleanup();
|
|
|
|
}
|
2017-03-17 01:12:07 +00:00
|
|
|
|
2017-09-08 13:59:05 +00:00
|
|
|
#undef EMIT_CODE_WITH_END
|
|
|
|
|
2018-06-21 10:58:58 +00:00
|
|
|
} // namespace test_run_wasm_module
|
2017-09-01 12:57:34 +00:00
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|