2017-09-01 12:57:34 +00:00
|
|
|
// Copyright 2017 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/cctest/wasm/wasm-run-utils.h"
|
|
|
|
|
2019-05-21 09:30:15 +00:00
|
|
|
#include "src/codegen/assembler-inl.h"
|
|
|
|
#include "src/diagnostics/code-tracer.h"
|
2019-02-14 21:10:30 +00:00
|
|
|
#include "src/heap/heap-inl.h"
|
2018-10-12 09:38:45 +00:00
|
|
|
#include "src/wasm/graph-builder-interface.h"
|
[wasm-simd] New macro to build using vector of bytes
Introduces a new macro BUILD_V (v is for vector) that pushes bytes into
a vector (instead of directly in an array initializer, see BUILD). This
has the positive effect of being able to handle opcodes of multiple
bytes (e.g. SIMD opcodes bigger that 0xfd80). Because of this "API"
change, our helper macros in test-run-wasm-simd.cc and wasm-run-utils.h
need to change too. So, we introduce new macros (suffixed by _V), that
will call the appropriate lambdas defined in BUILD_V, that knows how to
push bytes into the vector, and also can handle multi-byte opcodes.
This design has a bit of duplication and ugliness, but was chosen to
reduce the impact of existing tests. No restructuring of test code is
required, we only need to add suffix _V.
Note that we do not have multi-byte opcodes yet (in wasm-opcodes.h),
this change will be breaking, and requires all the tests to be updated
to use _V macros first.
Bug: v8:10258
Change-Id: I86638a548fe2f9714c1cfb3bd691fb7b49bfd652
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2107650
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Reviewed-by: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66812}
2020-03-20 16:38:37 +00:00
|
|
|
#include "src/wasm/leb-helper.h"
|
2019-05-23 15:12:58 +00:00
|
|
|
#include "src/wasm/module-compiler.h"
|
2019-03-22 12:41:21 +00:00
|
|
|
#include "src/wasm/wasm-import-wrapper-cache.h"
|
2017-09-08 08:39:19 +00:00
|
|
|
#include "src/wasm/wasm-objects-inl.h"
|
[wasm-simd] New macro to build using vector of bytes
Introduces a new macro BUILD_V (v is for vector) that pushes bytes into
a vector (instead of directly in an array initializer, see BUILD). This
has the positive effect of being able to handle opcodes of multiple
bytes (e.g. SIMD opcodes bigger that 0xfd80). Because of this "API"
change, our helper macros in test-run-wasm-simd.cc and wasm-run-utils.h
need to change too. So, we introduce new macros (suffixed by _V), that
will call the appropriate lambdas defined in BUILD_V, that knows how to
push bytes into the vector, and also can handle multi-byte opcodes.
This design has a bit of duplication and ugliness, but was chosen to
reduce the impact of existing tests. No restructuring of test code is
required, we only need to add suffix _V.
Note that we do not have multi-byte opcodes yet (in wasm-opcodes.h),
this change will be breaking, and requires all the tests to be updated
to use _V macros first.
Bug: v8:10258
Change-Id: I86638a548fe2f9714c1cfb3bd691fb7b49bfd652
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2107650
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Reviewed-by: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66812}
2020-03-20 16:38:37 +00:00
|
|
|
#include "src/wasm/wasm-opcodes.h"
|
2017-09-08 08:39:19 +00:00
|
|
|
|
2017-09-01 12:57:34 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
|
|
|
|
2017-09-12 12:39:42 +00:00
|
|
|
TestingModuleBuilder::TestingModuleBuilder(
|
2020-08-04 11:09:23 +00:00
|
|
|
Zone* zone, ManuallyImportedJSFunction* maybe_import,
|
|
|
|
TestExecutionTier tier, RuntimeExceptionSupport exception_support,
|
|
|
|
LowerSimd lower_simd)
|
2018-04-26 14:37:05 +00:00
|
|
|
: test_module_(std::make_shared<WasmModule>()),
|
|
|
|
test_module_ptr_(test_module_.get()),
|
2017-09-01 12:57:34 +00:00
|
|
|
isolate_(CcTest::InitIsolateOnce()),
|
2019-11-26 16:25:14 +00:00
|
|
|
enabled_features_(WasmFeatures::FromIsolate(isolate_)),
|
2018-08-21 15:01:31 +00:00
|
|
|
execution_tier_(tier),
|
2017-10-05 20:22:49 +00:00
|
|
|
runtime_exception_support_(exception_support),
|
2018-01-05 08:43:05 +00:00
|
|
|
lower_simd_(lower_simd) {
|
2017-09-04 12:15:18 +00:00
|
|
|
WasmJs::Install(isolate_, true);
|
2018-12-18 12:07:27 +00:00
|
|
|
test_module_->untagged_globals_buffer_size = kMaxGlobalsSize;
|
2017-09-01 12:57:34 +00:00
|
|
|
memset(globals_data_, 0, sizeof(globals_data_));
|
2018-04-06 10:18:18 +00:00
|
|
|
|
|
|
|
uint32_t maybe_import_index = 0;
|
|
|
|
if (maybe_import) {
|
|
|
|
// Manually add an imported function before any other functions.
|
2018-06-20 06:33:57 +00:00
|
|
|
// This must happen before the instance object is created, since the
|
2018-04-06 10:18:18 +00:00
|
|
|
// instance object allocates import entries.
|
2018-06-20 06:33:57 +00:00
|
|
|
maybe_import_index = AddFunction(maybe_import->sig, nullptr, kImport);
|
2018-04-06 10:18:18 +00:00
|
|
|
DCHECK_EQ(0, maybe_import_index);
|
|
|
|
}
|
|
|
|
|
2017-09-01 12:57:34 +00:00
|
|
|
instance_object_ = InitInstanceObject();
|
2019-03-21 15:55:09 +00:00
|
|
|
Handle<FixedArray> tables(isolate_->factory()->NewFixedArray(0));
|
|
|
|
instance_object_->set_tables(*tables);
|
2018-04-06 10:18:18 +00:00
|
|
|
|
|
|
|
if (maybe_import) {
|
2018-10-10 09:40:02 +00:00
|
|
|
// Manually compile an import wrapper and insert it into the instance.
|
2018-04-06 10:18:18 +00:00
|
|
|
CodeSpaceMemoryModificationScope modification_scope(isolate_->heap());
|
2019-08-13 12:48:18 +00:00
|
|
|
auto resolved = compiler::ResolveWasmImportCall(
|
2020-09-16 07:37:24 +00:00
|
|
|
maybe_import->js_function, maybe_import->sig,
|
|
|
|
instance_object_->module(), enabled_features_);
|
2019-07-09 12:36:15 +00:00
|
|
|
compiler::WasmImportCallKind kind = resolved.first;
|
|
|
|
Handle<JSReceiver> callable = resolved.second;
|
2019-05-23 15:12:58 +00:00
|
|
|
WasmImportWrapperCache::ModificationScope cache_scope(
|
|
|
|
native_module_->import_wrapper_cache());
|
2020-07-27 19:19:55 +00:00
|
|
|
WasmImportWrapperCache::CacheKey key(
|
|
|
|
kind, maybe_import->sig,
|
|
|
|
static_cast<int>(maybe_import->sig->parameter_count()));
|
2019-05-23 15:12:58 +00:00
|
|
|
auto import_wrapper = cache_scope[key];
|
|
|
|
if (import_wrapper == nullptr) {
|
|
|
|
import_wrapper = CompileImportWrapper(
|
|
|
|
isolate_->wasm_engine(), native_module_, isolate_->counters(), kind,
|
2020-07-27 19:19:55 +00:00
|
|
|
maybe_import->sig,
|
|
|
|
static_cast<int>(maybe_import->sig->parameter_count()), &cache_scope);
|
2019-05-23 15:12:58 +00:00
|
|
|
}
|
2018-04-06 10:18:18 +00:00
|
|
|
|
2018-04-27 13:18:30 +00:00
|
|
|
ImportedFunctionEntry(instance_object_, maybe_import_index)
|
2019-07-09 12:36:15 +00:00
|
|
|
.SetWasmToJs(isolate_, callable, import_wrapper);
|
2018-04-06 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:09:23 +00:00
|
|
|
if (tier == TestExecutionTier::kInterpreter) {
|
2020-06-08 20:47:45 +00:00
|
|
|
interpreter_ = std::make_unique<WasmInterpreter>(
|
|
|
|
isolate_, test_module_ptr_,
|
|
|
|
ModuleWireBytes{native_module_->wire_bytes()}, instance_object_);
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 12:13:26 +00:00
|
|
|
TestingModuleBuilder::~TestingModuleBuilder() {
|
|
|
|
// When the native module dies and is erased from the cache, it is expected to
|
|
|
|
// have either valid bytes or no bytes at all.
|
|
|
|
native_module_->SetWireBytes({});
|
|
|
|
}
|
|
|
|
|
2019-02-20 02:02:12 +00:00
|
|
|
byte* TestingModuleBuilder::AddMemory(uint32_t size, SharedFlag shared) {
|
2018-04-26 14:37:05 +00:00
|
|
|
CHECK(!test_module_->has_memory);
|
2017-09-01 12:57:34 +00:00
|
|
|
CHECK_NULL(mem_start_);
|
|
|
|
CHECK_EQ(0, mem_size_);
|
[wasm] Introduce the WasmContext
The WasmContext struct introduced in this CL is used to store the
mem_size and mem_start address of the wasm memory. These variables can
be accessed at C++ level at graph build time (e.g., initialized during
instance building). When the GrowMemory runtime is invoked, the context
variables can be changed in the WasmContext at C++ level so that the
generated code will load the correct values.
This requires to insert a relocatable pointer only in the
JSToWasmWrapper (and in the other wasm entry points), the value is then
passed from function to function as an automatically added additional
parameter. The WasmContext is then dropped when creating an Interpreter
Entry or when invoking a JavaScript function. This removes the need of
patching the generated code at runtime (i.e., when the memory grows)
with respect to WASM_MEMORY_REFERENCE and WASM_MEMORY_SIZE_REFERENCE.
However, we still need to patch the code at instance build time to patch
the JSToWasmWrappers; in fact the address of the WasmContext is not
known during compilation, but only when the instance is built.
The WasmContext address is passed as the first parameter. This has the
advantage of not having to move the WasmContext around if the function
does not use many registers. This CL also changes the wasm calling
convention so that the first parameter register is different from the
return value register. The WasmContext is attached to every
WasmMemoryObject, to share the same context with multiple instances
sharing the same memory. Moreover, the nodes representing the
WasmContext variables are cached in the SSA environment, similarly to
other local variables that might change during execution. The nodes are
created when initializing the SSA environment and refreshed every time a
grow_memory or a function call happens, so that we are sure that they
always represent the correct mem_size and mem_start variables.
This CL also removes the WasmMemorySize runtime (since it's now possible
to directly retrieve mem_size from the context) and simplifies the
GrowMemory runtime (since every instance now has a memory_object).
R=ahaas@chromium.org,clemensh@chromium.org
CC=gdeepti@chromium.org
Change-Id: I3f058e641284f5a1bbbfc35a64c88da6ff08e240
Reviewed-on: https://chromium-review.googlesource.com/671008
Commit-Queue: Enrico Bacis <enricobacis@google.com>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48209}
2017-09-28 14:59:37 +00:00
|
|
|
DCHECK(!instance_object_->has_memory_object());
|
2019-09-09 10:19:34 +00:00
|
|
|
uint32_t initial_pages = RoundUp(size, kWasmPageSize) / kWasmPageSize;
|
|
|
|
uint32_t maximum_pages = (test_module_->maximum_pages != 0)
|
|
|
|
? test_module_->maximum_pages
|
|
|
|
: initial_pages;
|
2018-04-26 14:37:05 +00:00
|
|
|
test_module_->has_memory = true;
|
2017-09-01 12:57:34 +00:00
|
|
|
|
[wasm] Introduce the WasmContext
The WasmContext struct introduced in this CL is used to store the
mem_size and mem_start address of the wasm memory. These variables can
be accessed at C++ level at graph build time (e.g., initialized during
instance building). When the GrowMemory runtime is invoked, the context
variables can be changed in the WasmContext at C++ level so that the
generated code will load the correct values.
This requires to insert a relocatable pointer only in the
JSToWasmWrapper (and in the other wasm entry points), the value is then
passed from function to function as an automatically added additional
parameter. The WasmContext is then dropped when creating an Interpreter
Entry or when invoking a JavaScript function. This removes the need of
patching the generated code at runtime (i.e., when the memory grows)
with respect to WASM_MEMORY_REFERENCE and WASM_MEMORY_SIZE_REFERENCE.
However, we still need to patch the code at instance build time to patch
the JSToWasmWrappers; in fact the address of the WasmContext is not
known during compilation, but only when the instance is built.
The WasmContext address is passed as the first parameter. This has the
advantage of not having to move the WasmContext around if the function
does not use many registers. This CL also changes the wasm calling
convention so that the first parameter register is different from the
return value register. The WasmContext is attached to every
WasmMemoryObject, to share the same context with multiple instances
sharing the same memory. Moreover, the nodes representing the
WasmContext variables are cached in the SSA environment, similarly to
other local variables that might change during execution. The nodes are
created when initializing the SSA environment and refreshed every time a
grow_memory or a function call happens, so that we are sure that they
always represent the correct mem_size and mem_start variables.
This CL also removes the WasmMemorySize runtime (since it's now possible
to directly retrieve mem_size from the context) and simplifies the
GrowMemory runtime (since every instance now has a memory_object).
R=ahaas@chromium.org,clemensh@chromium.org
CC=gdeepti@chromium.org
Change-Id: I3f058e641284f5a1bbbfc35a64c88da6ff08e240
Reviewed-on: https://chromium-review.googlesource.com/671008
Commit-Queue: Enrico Bacis <enricobacis@google.com>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48209}
2017-09-28 14:59:37 +00:00
|
|
|
// Create the WasmMemoryObject.
|
2019-02-23 00:45:57 +00:00
|
|
|
Handle<WasmMemoryObject> memory_object =
|
2019-09-09 10:19:34 +00:00
|
|
|
WasmMemoryObject::New(isolate_, initial_pages, maximum_pages, shared)
|
|
|
|
.ToHandleChecked();
|
[wasm] Introduce the WasmContext
The WasmContext struct introduced in this CL is used to store the
mem_size and mem_start address of the wasm memory. These variables can
be accessed at C++ level at graph build time (e.g., initialized during
instance building). When the GrowMemory runtime is invoked, the context
variables can be changed in the WasmContext at C++ level so that the
generated code will load the correct values.
This requires to insert a relocatable pointer only in the
JSToWasmWrapper (and in the other wasm entry points), the value is then
passed from function to function as an automatically added additional
parameter. The WasmContext is then dropped when creating an Interpreter
Entry or when invoking a JavaScript function. This removes the need of
patching the generated code at runtime (i.e., when the memory grows)
with respect to WASM_MEMORY_REFERENCE and WASM_MEMORY_SIZE_REFERENCE.
However, we still need to patch the code at instance build time to patch
the JSToWasmWrappers; in fact the address of the WasmContext is not
known during compilation, but only when the instance is built.
The WasmContext address is passed as the first parameter. This has the
advantage of not having to move the WasmContext around if the function
does not use many registers. This CL also changes the wasm calling
convention so that the first parameter register is different from the
return value register. The WasmContext is attached to every
WasmMemoryObject, to share the same context with multiple instances
sharing the same memory. Moreover, the nodes representing the
WasmContext variables are cached in the SSA environment, similarly to
other local variables that might change during execution. The nodes are
created when initializing the SSA environment and refreshed every time a
grow_memory or a function call happens, so that we are sure that they
always represent the correct mem_size and mem_start variables.
This CL also removes the WasmMemorySize runtime (since it's now possible
to directly retrieve mem_size from the context) and simplifies the
GrowMemory runtime (since every instance now has a memory_object).
R=ahaas@chromium.org,clemensh@chromium.org
CC=gdeepti@chromium.org
Change-Id: I3f058e641284f5a1bbbfc35a64c88da6ff08e240
Reviewed-on: https://chromium-review.googlesource.com/671008
Commit-Queue: Enrico Bacis <enricobacis@google.com>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48209}
2017-09-28 14:59:37 +00:00
|
|
|
instance_object_->set_memory_object(*memory_object);
|
2019-09-09 10:19:34 +00:00
|
|
|
|
|
|
|
mem_start_ =
|
|
|
|
reinterpret_cast<byte*>(memory_object->array_buffer().backing_store());
|
|
|
|
mem_size_ = size;
|
|
|
|
CHECK(size == 0 || mem_start_);
|
|
|
|
|
[wasm] Introduce the WasmContext
The WasmContext struct introduced in this CL is used to store the
mem_size and mem_start address of the wasm memory. These variables can
be accessed at C++ level at graph build time (e.g., initialized during
instance building). When the GrowMemory runtime is invoked, the context
variables can be changed in the WasmContext at C++ level so that the
generated code will load the correct values.
This requires to insert a relocatable pointer only in the
JSToWasmWrapper (and in the other wasm entry points), the value is then
passed from function to function as an automatically added additional
parameter. The WasmContext is then dropped when creating an Interpreter
Entry or when invoking a JavaScript function. This removes the need of
patching the generated code at runtime (i.e., when the memory grows)
with respect to WASM_MEMORY_REFERENCE and WASM_MEMORY_SIZE_REFERENCE.
However, we still need to patch the code at instance build time to patch
the JSToWasmWrappers; in fact the address of the WasmContext is not
known during compilation, but only when the instance is built.
The WasmContext address is passed as the first parameter. This has the
advantage of not having to move the WasmContext around if the function
does not use many registers. This CL also changes the wasm calling
convention so that the first parameter register is different from the
return value register. The WasmContext is attached to every
WasmMemoryObject, to share the same context with multiple instances
sharing the same memory. Moreover, the nodes representing the
WasmContext variables are cached in the SSA environment, similarly to
other local variables that might change during execution. The nodes are
created when initializing the SSA environment and refreshed every time a
grow_memory or a function call happens, so that we are sure that they
always represent the correct mem_size and mem_start variables.
This CL also removes the WasmMemorySize runtime (since it's now possible
to directly retrieve mem_size from the context) and simplifies the
GrowMemory runtime (since every instance now has a memory_object).
R=ahaas@chromium.org,clemensh@chromium.org
CC=gdeepti@chromium.org
Change-Id: I3f058e641284f5a1bbbfc35a64c88da6ff08e240
Reviewed-on: https://chromium-review.googlesource.com/671008
Commit-Queue: Enrico Bacis <enricobacis@google.com>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48209}
2017-09-28 14:59:37 +00:00
|
|
|
WasmMemoryObject::AddInstance(isolate_, memory_object, instance_object_);
|
2018-04-30 18:52:41 +00:00
|
|
|
// TODO(wasm): Delete the following two lines when test-run-wasm will use a
|
|
|
|
// multiple of kPageSize as memory size. At the moment, the effect of these
|
|
|
|
// two lines is used to shrink the memory for testing purposes.
|
|
|
|
instance_object_->SetRawMemory(mem_start_, mem_size_);
|
2017-09-01 12:57:34 +00:00
|
|
|
return mem_start_;
|
|
|
|
}
|
|
|
|
|
2020-02-25 20:00:50 +00:00
|
|
|
uint32_t TestingModuleBuilder::AddFunction(const FunctionSig* sig,
|
|
|
|
const char* name,
|
2018-06-20 06:33:57 +00:00
|
|
|
FunctionType type) {
|
2018-04-26 14:37:05 +00:00
|
|
|
if (test_module_->functions.size() == 0) {
|
2017-09-01 12:57:34 +00:00
|
|
|
// TODO(titzer): Reserving space here to avoid the underlying WasmFunction
|
|
|
|
// structs from moving.
|
2018-04-26 14:37:05 +00:00
|
|
|
test_module_->functions.reserve(kMaxFunctions);
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
2018-04-26 14:37:05 +00:00
|
|
|
uint32_t index = static_cast<uint32_t>(test_module_->functions.size());
|
2020-02-17 16:21:02 +00:00
|
|
|
test_module_->functions.push_back({sig, // sig
|
|
|
|
index, // func_index
|
|
|
|
0, // sig_index
|
|
|
|
{0, 0}, // code
|
|
|
|
false, // imported
|
|
|
|
false, // exported
|
|
|
|
false}); // declared
|
2018-06-20 06:33:57 +00:00
|
|
|
if (type == kImport) {
|
|
|
|
DCHECK_EQ(0, test_module_->num_declared_functions);
|
|
|
|
++test_module_->num_imported_functions;
|
|
|
|
test_module_->functions.back().imported = true;
|
|
|
|
} else {
|
|
|
|
++test_module_->num_declared_functions;
|
|
|
|
}
|
|
|
|
DCHECK_EQ(test_module_->functions.size(),
|
|
|
|
test_module_->num_imported_functions +
|
|
|
|
test_module_->num_declared_functions);
|
2017-09-01 12:57:34 +00:00
|
|
|
if (name) {
|
|
|
|
Vector<const byte> name_vec = Vector<const byte>::cast(CStrVector(name));
|
2020-04-06 16:53:53 +00:00
|
|
|
test_module_->lazily_generated_names.AddForTesting(
|
2018-03-13 16:14:01 +00:00
|
|
|
index, {AddBytes(name_vec), static_cast<uint32_t>(name_vec.length())});
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
if (interpreter_) {
|
2018-04-26 14:37:05 +00:00
|
|
|
interpreter_->AddFunctionForTesting(&test_module_->functions.back());
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
DCHECK_LT(index, kMaxFunctions); // limited for testing.
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2019-05-22 14:04:36 +00:00
|
|
|
void TestingModuleBuilder::FreezeSignatureMapAndInitializeWrapperCache() {
|
|
|
|
if (test_module_->signature_map.is_frozen()) return;
|
|
|
|
test_module_->signature_map.Freeze();
|
|
|
|
size_t max_num_sigs = MaxNumExportWrappers(test_module_.get());
|
|
|
|
Handle<FixedArray> export_wrappers =
|
|
|
|
isolate_->factory()->NewFixedArray(static_cast<int>(max_num_sigs));
|
|
|
|
instance_object_->module_object().set_export_wrappers(*export_wrappers);
|
|
|
|
}
|
|
|
|
|
2017-09-01 12:57:34 +00:00
|
|
|
Handle<JSFunction> TestingModuleBuilder::WrapCode(uint32_t index) {
|
2020-05-05 10:23:41 +00:00
|
|
|
CHECK(!interpreter_);
|
2019-05-22 14:04:36 +00:00
|
|
|
FreezeSignatureMapAndInitializeWrapperCache();
|
2019-01-28 14:23:10 +00:00
|
|
|
SetExecutable();
|
2019-08-22 10:54:51 +00:00
|
|
|
return WasmInstanceObject::GetOrCreateWasmExternalFunction(
|
2019-05-22 14:04:36 +00:00
|
|
|
isolate_, instance_object(), index);
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 12:25:31 +00:00
|
|
|
void TestingModuleBuilder::AddIndirectFunctionTable(
|
2020-10-02 05:31:50 +00:00
|
|
|
const uint16_t* function_indexes, uint32_t table_size,
|
|
|
|
ValueType table_type) {
|
2020-09-18 15:22:35 +00:00
|
|
|
Handle<WasmInstanceObject> instance = instance_object();
|
2019-03-21 15:55:09 +00:00
|
|
|
uint32_t table_index = static_cast<uint32_t>(test_module_->tables.size());
|
2018-07-10 12:14:06 +00:00
|
|
|
test_module_->tables.emplace_back();
|
|
|
|
WasmTable& table = test_module_->tables.back();
|
2017-09-01 12:57:34 +00:00
|
|
|
table.initial_size = table_size;
|
|
|
|
table.maximum_size = table_size;
|
|
|
|
table.has_maximum_size = true;
|
2020-10-02 05:31:50 +00:00
|
|
|
table.type = table_type;
|
2019-07-11 16:56:29 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Allocate the indirect function table.
|
|
|
|
Handle<FixedArray> old_tables =
|
|
|
|
table_index == 0
|
|
|
|
? isolate_->factory()->empty_fixed_array()
|
|
|
|
: handle(instance_object_->indirect_function_tables(), isolate_);
|
|
|
|
Handle<FixedArray> new_tables =
|
|
|
|
isolate_->factory()->CopyFixedArrayAndGrow(old_tables, 1);
|
|
|
|
Handle<WasmIndirectFunctionTable> table_obj =
|
|
|
|
WasmIndirectFunctionTable::New(isolate_, table.initial_size);
|
|
|
|
new_tables->set(table_index, *table_obj);
|
|
|
|
instance_object_->set_indirect_function_tables(*new_tables);
|
|
|
|
}
|
|
|
|
|
2018-04-06 10:18:18 +00:00
|
|
|
WasmInstanceObject::EnsureIndirectFunctionTableWithMinimumSize(
|
2019-07-11 16:56:29 +00:00
|
|
|
instance_object(), table_index, table_size);
|
2019-03-21 15:55:09 +00:00
|
|
|
Handle<WasmTableObject> table_obj =
|
2020-09-18 15:22:35 +00:00
|
|
|
WasmTableObject::New(isolate_, instance, table.type, table.initial_size,
|
2019-03-21 15:55:09 +00:00
|
|
|
table.has_maximum_size, table.maximum_size, nullptr);
|
|
|
|
|
|
|
|
WasmTableObject::AddDispatchTable(isolate_, table_obj, instance_object_,
|
|
|
|
table_index);
|
|
|
|
|
|
|
|
if (function_indexes) {
|
|
|
|
for (uint32_t i = 0; i < table_size; ++i) {
|
|
|
|
WasmFunction& function = test_module_->functions[function_indexes[i]];
|
|
|
|
int sig_id = test_module_->signature_map.Find(*function.sig);
|
2019-07-11 16:59:34 +00:00
|
|
|
IndirectFunctionTableEntry(instance, table_index, i)
|
2019-03-21 15:55:09 +00:00
|
|
|
.Set(sig_id, instance, function.func_index);
|
|
|
|
WasmTableObject::SetFunctionTablePlaceholder(
|
|
|
|
isolate_, table_obj, i, instance_object_, function_indexes[i]);
|
|
|
|
}
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
2019-03-21 15:55:09 +00:00
|
|
|
|
|
|
|
Handle<FixedArray> old_tables(instance_object_->tables(), isolate_);
|
2019-07-11 16:56:29 +00:00
|
|
|
Handle<FixedArray> new_tables =
|
|
|
|
isolate_->factory()->CopyFixedArrayAndGrow(old_tables, 1);
|
2019-03-21 15:55:09 +00:00
|
|
|
new_tables->set(old_tables->length(), *table_obj);
|
|
|
|
instance_object_->set_tables(*new_tables);
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t TestingModuleBuilder::AddBytes(Vector<const byte> bytes) {
|
2018-06-22 14:34:47 +00:00
|
|
|
Vector<const uint8_t> old_bytes = native_module_->wire_bytes();
|
|
|
|
uint32_t old_size = static_cast<uint32_t>(old_bytes.size());
|
2017-09-01 12:57:34 +00:00
|
|
|
// Avoid placing strings at offset 0, this might be interpreted as "not
|
|
|
|
// set", e.g. for function names.
|
|
|
|
uint32_t bytes_offset = old_size ? old_size : 1;
|
2018-06-22 14:34:47 +00:00
|
|
|
size_t new_size = bytes_offset + bytes.size();
|
2018-06-28 14:29:04 +00:00
|
|
|
OwnedVector<uint8_t> new_bytes = OwnedVector<uint8_t>::New(new_size);
|
2019-01-25 00:34:59 +00:00
|
|
|
if (old_size > 0) {
|
2019-04-29 11:06:49 +00:00
|
|
|
memcpy(new_bytes.start(), old_bytes.begin(), old_size);
|
2019-12-18 13:41:12 +00:00
|
|
|
} else {
|
|
|
|
// Set the unused byte. It is never decoded, but the bytes are used as the
|
|
|
|
// key in the native module cache.
|
|
|
|
new_bytes[0] = 0;
|
2019-01-25 00:34:59 +00:00
|
|
|
}
|
2019-04-29 11:06:49 +00:00
|
|
|
memcpy(new_bytes.start() + bytes_offset, bytes.begin(), bytes.length());
|
2018-11-07 10:27:10 +00:00
|
|
|
native_module_->SetWireBytes(std::move(new_bytes));
|
2017-09-01 12:57:34 +00:00
|
|
|
return bytes_offset;
|
|
|
|
}
|
|
|
|
|
2020-02-25 20:00:50 +00:00
|
|
|
uint32_t TestingModuleBuilder::AddException(const FunctionSig* sig) {
|
2019-01-28 13:59:04 +00:00
|
|
|
DCHECK_EQ(0, sig->return_count());
|
|
|
|
uint32_t index = static_cast<uint32_t>(test_module_->exceptions.size());
|
|
|
|
test_module_->exceptions.push_back(WasmException{sig});
|
|
|
|
Handle<WasmExceptionTag> tag = WasmExceptionTag::New(isolate_, index);
|
|
|
|
Handle<FixedArray> table(instance_object_->exceptions_table(), isolate_);
|
|
|
|
table = isolate_->factory()->CopyFixedArrayAndGrow(table, 1);
|
|
|
|
instance_object_->set_exceptions_table(*table);
|
|
|
|
table->set(index, *tag);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2019-03-13 19:22:40 +00:00
|
|
|
uint32_t TestingModuleBuilder::AddPassiveDataSegment(Vector<const byte> bytes) {
|
|
|
|
uint32_t index = static_cast<uint32_t>(test_module_->data_segments.size());
|
|
|
|
DCHECK_EQ(index, test_module_->data_segments.size());
|
|
|
|
DCHECK_EQ(index, data_segment_starts_.size());
|
|
|
|
DCHECK_EQ(index, data_segment_sizes_.size());
|
|
|
|
|
|
|
|
// Add a passive data segment. This isn't used by function compilation, but
|
|
|
|
// but it keeps the index in sync. The data segment's source will not be
|
|
|
|
// correct, since we don't store data in the module wire bytes.
|
|
|
|
test_module_->data_segments.emplace_back();
|
|
|
|
|
|
|
|
// The num_declared_data_segments (from the DataCount section) is used
|
|
|
|
// to validate the segment index, during function compilation.
|
|
|
|
test_module_->num_declared_data_segments = index + 1;
|
|
|
|
|
|
|
|
Address old_data_address =
|
|
|
|
reinterpret_cast<Address>(data_segment_data_.data());
|
|
|
|
size_t old_data_size = data_segment_data_.size();
|
|
|
|
data_segment_data_.resize(old_data_size + bytes.length());
|
|
|
|
Address new_data_address =
|
|
|
|
reinterpret_cast<Address>(data_segment_data_.data());
|
|
|
|
|
2019-04-29 11:06:49 +00:00
|
|
|
memcpy(data_segment_data_.data() + old_data_size, bytes.begin(),
|
2019-03-13 19:22:40 +00:00
|
|
|
bytes.length());
|
|
|
|
|
|
|
|
// The data_segment_data_ offset may have moved, so update all the starts.
|
|
|
|
for (Address& start : data_segment_starts_) {
|
|
|
|
start += new_data_address - old_data_address;
|
|
|
|
}
|
|
|
|
data_segment_starts_.push_back(new_data_address + old_data_size);
|
|
|
|
data_segment_sizes_.push_back(bytes.length());
|
|
|
|
|
|
|
|
// The vector pointers may have moved, so update the instance object.
|
|
|
|
instance_object_->set_data_segment_starts(data_segment_starts_.data());
|
|
|
|
instance_object_->set_data_segment_sizes(data_segment_sizes_.data());
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2019-04-05 19:04:37 +00:00
|
|
|
uint32_t TestingModuleBuilder::AddPassiveElementSegment(
|
|
|
|
const std::vector<uint32_t>& entries) {
|
|
|
|
uint32_t index = static_cast<uint32_t>(test_module_->elem_segments.size());
|
|
|
|
DCHECK_EQ(index, dropped_elem_segments_.size());
|
|
|
|
|
2020-02-17 16:21:02 +00:00
|
|
|
test_module_->elem_segments.emplace_back(false);
|
2019-04-05 19:04:37 +00:00
|
|
|
auto& elem_segment = test_module_->elem_segments.back();
|
|
|
|
elem_segment.entries = entries;
|
|
|
|
|
|
|
|
// The vector pointers may have moved, so update the instance object.
|
|
|
|
dropped_elem_segments_.push_back(0);
|
|
|
|
instance_object_->set_dropped_elem_segments(dropped_elem_segments_.data());
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2020-04-14 10:42:23 +00:00
|
|
|
CompilationEnv TestingModuleBuilder::CreateCompilationEnv() {
|
2019-03-27 11:53:26 +00:00
|
|
|
// This is a hack so we don't need to call
|
|
|
|
// trap_handler::IsTrapHandlerEnabled().
|
|
|
|
const bool is_trap_handler_enabled =
|
|
|
|
V8_TRAP_HANDLER_SUPPORTED && i::FLAG_wasm_trap_handler;
|
|
|
|
return {test_module_ptr_,
|
|
|
|
is_trap_handler_enabled ? kUseTrapHandler : kNoTrapHandler,
|
2020-04-14 10:42:23 +00:00
|
|
|
runtime_exception_support_, enabled_features_, lower_simd()};
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const WasmGlobal* TestingModuleBuilder::AddGlobal(ValueType type) {
|
2020-03-12 14:29:51 +00:00
|
|
|
byte size = type.element_size_bytes();
|
2017-09-01 12:57:34 +00:00
|
|
|
global_offset = (global_offset + size - 1) & ~(size - 1); // align
|
2018-04-26 14:37:05 +00:00
|
|
|
test_module_->globals.push_back(
|
2018-04-25 18:12:51 +00:00
|
|
|
{type, true, WasmInitExpr(), {global_offset}, false, false});
|
2017-09-01 12:57:34 +00:00
|
|
|
global_offset += size;
|
|
|
|
// limit number of globals.
|
|
|
|
CHECK_LT(global_offset, kMaxGlobalsSize);
|
2018-04-26 14:37:05 +00:00
|
|
|
return &test_module_->globals.back();
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle<WasmInstanceObject> TestingModuleBuilder::InitInstanceObject() {
|
2019-11-12 11:57:59 +00:00
|
|
|
const bool kUsesLiftoff = true;
|
2019-11-07 17:16:45 +00:00
|
|
|
size_t code_size_estimate =
|
2019-11-12 11:57:59 +00:00
|
|
|
wasm::WasmCodeManager::EstimateNativeModuleCodeSize(test_module_.get(),
|
|
|
|
kUsesLiftoff);
|
2019-07-30 14:58:41 +00:00
|
|
|
auto native_module = isolate_->wasm_engine()->NewNativeModule(
|
2019-11-07 17:16:45 +00:00
|
|
|
isolate_, enabled_features_, test_module_, code_size_estimate);
|
2019-07-30 14:58:41 +00:00
|
|
|
native_module->SetWireBytes(OwnedVector<const uint8_t>());
|
2020-04-09 13:53:20 +00:00
|
|
|
Handle<Script> script =
|
|
|
|
isolate_->wasm_engine()->GetOrCreateScript(isolate_, native_module);
|
2019-07-30 14:58:41 +00:00
|
|
|
|
2018-08-08 14:54:44 +00:00
|
|
|
Handle<WasmModuleObject> module_object =
|
2019-07-30 14:58:41 +00:00
|
|
|
WasmModuleObject::New(isolate_, std::move(native_module), script);
|
2017-11-28 22:25:36 +00:00
|
|
|
// This method is called when we initialize TestEnvironment. We don't
|
|
|
|
// have a memory yet, so we won't create it here. We'll update the
|
|
|
|
// interpreter when we get a memory. We do have globals, though.
|
2018-06-20 06:39:24 +00:00
|
|
|
native_module_ = module_object->native_module();
|
2018-06-04 12:01:49 +00:00
|
|
|
native_module_->ReserveCodeTableForTesting(kMaxFunctions);
|
2017-11-28 22:25:36 +00:00
|
|
|
|
2018-06-27 12:50:53 +00:00
|
|
|
auto instance = WasmInstanceObject::New(isolate_, module_object);
|
2019-01-28 13:59:04 +00:00
|
|
|
instance->set_exceptions_table(*isolate_->factory()->empty_fixed_array());
|
2018-04-06 10:18:18 +00:00
|
|
|
instance->set_globals_start(globals_data_);
|
2017-10-16 08:49:45 +00:00
|
|
|
return instance;
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 23:09:14 +00:00
|
|
|
void TestBuildingGraphWithBuilder(compiler::WasmGraphBuilder* builder,
|
2020-02-25 20:00:50 +00:00
|
|
|
Zone* zone, const FunctionSig* sig,
|
2017-12-05 23:09:14 +00:00
|
|
|
const byte* start, const byte* end) {
|
2018-08-08 14:54:44 +00:00
|
|
|
WasmFeatures unused_detected_features;
|
|
|
|
FunctionBody body(sig, 0, start, end);
|
2017-09-01 12:57:34 +00:00
|
|
|
DecodeResult result =
|
2019-11-26 16:25:14 +00:00
|
|
|
BuildTFGraph(zone->allocator(), WasmFeatures::All(), nullptr, builder,
|
2018-08-08 14:54:44 +00:00
|
|
|
&unused_detected_features, body, nullptr);
|
2017-09-01 12:57:34 +00:00
|
|
|
if (result.failed()) {
|
2017-12-01 15:22:00 +00:00
|
|
|
#ifdef DEBUG
|
2017-09-01 12:57:34 +00:00
|
|
|
if (!FLAG_trace_wasm_decoder) {
|
|
|
|
// Retry the compilation with the tracing flag on, to help in debugging.
|
|
|
|
FLAG_trace_wasm_decoder = true;
|
2019-11-26 16:25:14 +00:00
|
|
|
result = BuildTFGraph(zone->allocator(), WasmFeatures::All(), nullptr,
|
2018-08-08 14:54:44 +00:00
|
|
|
builder, &unused_detected_features, body, nullptr);
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
2017-12-01 15:22:00 +00:00
|
|
|
#endif
|
2017-09-01 12:57:34 +00:00
|
|
|
|
2019-01-14 17:51:56 +00:00
|
|
|
FATAL("Verification failed; pc = +%x, msg = %s", result.error().offset(),
|
|
|
|
result.error().message().c_str());
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
2019-08-20 12:55:43 +00:00
|
|
|
builder->LowerInt64(compiler::WasmGraphBuilder::kCalledFromWasm);
|
2017-09-01 12:57:34 +00:00
|
|
|
if (!CpuFeatures::SupportsWasmSimd128()) {
|
2017-12-05 23:09:14 +00:00
|
|
|
builder->SimdScalarLoweringForTesting();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 10:16:37 +00:00
|
|
|
void TestBuildingGraph(Zone* zone, compiler::JSGraph* jsgraph,
|
2020-02-25 20:00:50 +00:00
|
|
|
CompilationEnv* module, const FunctionSig* sig,
|
2018-04-19 10:16:37 +00:00
|
|
|
compiler::SourcePositionTable* source_position_table,
|
|
|
|
const byte* start, const byte* end) {
|
2018-06-12 16:43:02 +00:00
|
|
|
compiler::WasmGraphBuilder builder(module, zone, jsgraph, sig,
|
|
|
|
source_position_table);
|
2018-05-25 07:52:27 +00:00
|
|
|
TestBuildingGraphWithBuilder(&builder, zone, sig, start, end);
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WasmFunctionWrapper::WasmFunctionWrapper(Zone* zone, int num_params)
|
[wasm] Introduce the WasmContext
The WasmContext struct introduced in this CL is used to store the
mem_size and mem_start address of the wasm memory. These variables can
be accessed at C++ level at graph build time (e.g., initialized during
instance building). When the GrowMemory runtime is invoked, the context
variables can be changed in the WasmContext at C++ level so that the
generated code will load the correct values.
This requires to insert a relocatable pointer only in the
JSToWasmWrapper (and in the other wasm entry points), the value is then
passed from function to function as an automatically added additional
parameter. The WasmContext is then dropped when creating an Interpreter
Entry or when invoking a JavaScript function. This removes the need of
patching the generated code at runtime (i.e., when the memory grows)
with respect to WASM_MEMORY_REFERENCE and WASM_MEMORY_SIZE_REFERENCE.
However, we still need to patch the code at instance build time to patch
the JSToWasmWrappers; in fact the address of the WasmContext is not
known during compilation, but only when the instance is built.
The WasmContext address is passed as the first parameter. This has the
advantage of not having to move the WasmContext around if the function
does not use many registers. This CL also changes the wasm calling
convention so that the first parameter register is different from the
return value register. The WasmContext is attached to every
WasmMemoryObject, to share the same context with multiple instances
sharing the same memory. Moreover, the nodes representing the
WasmContext variables are cached in the SSA environment, similarly to
other local variables that might change during execution. The nodes are
created when initializing the SSA environment and refreshed every time a
grow_memory or a function call happens, so that we are sure that they
always represent the correct mem_size and mem_start variables.
This CL also removes the WasmMemorySize runtime (since it's now possible
to directly retrieve mem_size from the context) and simplifies the
GrowMemory runtime (since every instance now has a memory_object).
R=ahaas@chromium.org,clemensh@chromium.org
CC=gdeepti@chromium.org
Change-Id: I3f058e641284f5a1bbbfc35a64c88da6ff08e240
Reviewed-on: https://chromium-review.googlesource.com/671008
Commit-Queue: Enrico Bacis <enricobacis@google.com>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48209}
2017-09-28 14:59:37 +00:00
|
|
|
: GraphAndBuilders(zone),
|
|
|
|
inner_code_node_(nullptr),
|
|
|
|
context_address_(nullptr),
|
|
|
|
signature_(nullptr) {
|
2017-09-01 12:57:34 +00:00
|
|
|
// One additional parameter for the pointer to the return value memory.
|
|
|
|
Signature<MachineType>::Builder sig_builder(zone, 1, num_params + 1);
|
|
|
|
|
|
|
|
sig_builder.AddReturn(MachineType::Int32());
|
|
|
|
for (int i = 0; i < num_params + 1; i++) {
|
|
|
|
sig_builder.AddParam(MachineType::Pointer());
|
|
|
|
}
|
|
|
|
signature_ = sig_builder.Build();
|
|
|
|
}
|
|
|
|
|
2018-02-09 19:19:25 +00:00
|
|
|
void WasmFunctionWrapper::Init(CallDescriptor* call_descriptor,
|
2017-09-01 12:57:34 +00:00
|
|
|
MachineType return_type,
|
|
|
|
Vector<MachineType> param_types) {
|
2018-02-09 19:19:25 +00:00
|
|
|
DCHECK_NOT_NULL(call_descriptor);
|
2017-09-01 12:57:34 +00:00
|
|
|
DCHECK_EQ(signature_->parameter_count(), param_types.length() + 1);
|
|
|
|
|
|
|
|
// Create the TF graph for the wrapper.
|
|
|
|
|
[wasm] Introduce the WasmContext
The WasmContext struct introduced in this CL is used to store the
mem_size and mem_start address of the wasm memory. These variables can
be accessed at C++ level at graph build time (e.g., initialized during
instance building). When the GrowMemory runtime is invoked, the context
variables can be changed in the WasmContext at C++ level so that the
generated code will load the correct values.
This requires to insert a relocatable pointer only in the
JSToWasmWrapper (and in the other wasm entry points), the value is then
passed from function to function as an automatically added additional
parameter. The WasmContext is then dropped when creating an Interpreter
Entry or when invoking a JavaScript function. This removes the need of
patching the generated code at runtime (i.e., when the memory grows)
with respect to WASM_MEMORY_REFERENCE and WASM_MEMORY_SIZE_REFERENCE.
However, we still need to patch the code at instance build time to patch
the JSToWasmWrappers; in fact the address of the WasmContext is not
known during compilation, but only when the instance is built.
The WasmContext address is passed as the first parameter. This has the
advantage of not having to move the WasmContext around if the function
does not use many registers. This CL also changes the wasm calling
convention so that the first parameter register is different from the
return value register. The WasmContext is attached to every
WasmMemoryObject, to share the same context with multiple instances
sharing the same memory. Moreover, the nodes representing the
WasmContext variables are cached in the SSA environment, similarly to
other local variables that might change during execution. The nodes are
created when initializing the SSA environment and refreshed every time a
grow_memory or a function call happens, so that we are sure that they
always represent the correct mem_size and mem_start variables.
This CL also removes the WasmMemorySize runtime (since it's now possible
to directly retrieve mem_size from the context) and simplifies the
GrowMemory runtime (since every instance now has a memory_object).
R=ahaas@chromium.org,clemensh@chromium.org
CC=gdeepti@chromium.org
Change-Id: I3f058e641284f5a1bbbfc35a64c88da6ff08e240
Reviewed-on: https://chromium-review.googlesource.com/671008
Commit-Queue: Enrico Bacis <enricobacis@google.com>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48209}
2017-09-28 14:59:37 +00:00
|
|
|
// Function, context_address, effect, and control.
|
|
|
|
Node** parameters = zone()->NewArray<Node*>(param_types.length() + 4);
|
2018-06-20 06:03:44 +00:00
|
|
|
int start_value_output_count =
|
|
|
|
static_cast<int>(signature_->parameter_count()) + 1;
|
|
|
|
graph()->SetStart(
|
|
|
|
graph()->NewNode(common()->Start(start_value_output_count)));
|
2017-09-01 12:57:34 +00:00
|
|
|
Node* effect = graph()->start();
|
|
|
|
int parameter_count = 0;
|
|
|
|
|
|
|
|
// Dummy node which gets replaced in SetInnerCode.
|
|
|
|
inner_code_node_ = graph()->NewNode(common()->Int32Constant(0));
|
|
|
|
parameters[parameter_count++] = inner_code_node_;
|
|
|
|
|
[wasm] Introduce the WasmContext
The WasmContext struct introduced in this CL is used to store the
mem_size and mem_start address of the wasm memory. These variables can
be accessed at C++ level at graph build time (e.g., initialized during
instance building). When the GrowMemory runtime is invoked, the context
variables can be changed in the WasmContext at C++ level so that the
generated code will load the correct values.
This requires to insert a relocatable pointer only in the
JSToWasmWrapper (and in the other wasm entry points), the value is then
passed from function to function as an automatically added additional
parameter. The WasmContext is then dropped when creating an Interpreter
Entry or when invoking a JavaScript function. This removes the need of
patching the generated code at runtime (i.e., when the memory grows)
with respect to WASM_MEMORY_REFERENCE and WASM_MEMORY_SIZE_REFERENCE.
However, we still need to patch the code at instance build time to patch
the JSToWasmWrappers; in fact the address of the WasmContext is not
known during compilation, but only when the instance is built.
The WasmContext address is passed as the first parameter. This has the
advantage of not having to move the WasmContext around if the function
does not use many registers. This CL also changes the wasm calling
convention so that the first parameter register is different from the
return value register. The WasmContext is attached to every
WasmMemoryObject, to share the same context with multiple instances
sharing the same memory. Moreover, the nodes representing the
WasmContext variables are cached in the SSA environment, similarly to
other local variables that might change during execution. The nodes are
created when initializing the SSA environment and refreshed every time a
grow_memory or a function call happens, so that we are sure that they
always represent the correct mem_size and mem_start variables.
This CL also removes the WasmMemorySize runtime (since it's now possible
to directly retrieve mem_size from the context) and simplifies the
GrowMemory runtime (since every instance now has a memory_object).
R=ahaas@chromium.org,clemensh@chromium.org
CC=gdeepti@chromium.org
Change-Id: I3f058e641284f5a1bbbfc35a64c88da6ff08e240
Reviewed-on: https://chromium-review.googlesource.com/671008
Commit-Queue: Enrico Bacis <enricobacis@google.com>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48209}
2017-09-28 14:59:37 +00:00
|
|
|
// Dummy node that gets replaced in SetContextAddress.
|
|
|
|
context_address_ = graph()->NewNode(IntPtrConstant(0));
|
|
|
|
parameters[parameter_count++] = context_address_;
|
|
|
|
|
2017-09-01 12:57:34 +00:00
|
|
|
int param_idx = 0;
|
|
|
|
for (MachineType t : param_types) {
|
|
|
|
DCHECK_NE(MachineType::None(), t);
|
|
|
|
parameters[parameter_count] = graph()->NewNode(
|
|
|
|
machine()->Load(t),
|
|
|
|
graph()->NewNode(common()->Parameter(param_idx++), graph()->start()),
|
|
|
|
graph()->NewNode(common()->Int32Constant(0)), effect, graph()->start());
|
|
|
|
effect = parameters[parameter_count++];
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters[parameter_count++] = effect;
|
|
|
|
parameters[parameter_count++] = graph()->start();
|
2018-02-09 19:19:25 +00:00
|
|
|
Node* call = graph()->NewNode(common()->Call(call_descriptor),
|
|
|
|
parameter_count, parameters);
|
2017-09-01 12:57:34 +00:00
|
|
|
|
|
|
|
if (!return_type.IsNone()) {
|
|
|
|
effect = graph()->NewNode(
|
|
|
|
machine()->Store(compiler::StoreRepresentation(
|
2019-05-07 15:51:52 +00:00
|
|
|
return_type.representation(),
|
|
|
|
compiler::WriteBarrierKind::kNoWriteBarrier)),
|
2017-09-01 12:57:34 +00:00
|
|
|
graph()->NewNode(common()->Parameter(param_types.length()),
|
|
|
|
graph()->start()),
|
|
|
|
graph()->NewNode(common()->Int32Constant(0)), call, effect,
|
|
|
|
graph()->start());
|
|
|
|
}
|
|
|
|
Node* zero = graph()->NewNode(common()->Int32Constant(0));
|
|
|
|
Node* r = graph()->NewNode(
|
|
|
|
common()->Return(), zero,
|
|
|
|
graph()->NewNode(common()->Int32Constant(WASM_WRAPPER_RETURN_VALUE)),
|
|
|
|
effect, graph()->start());
|
|
|
|
graph()->SetEnd(graph()->NewNode(common()->End(1), r));
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Code> WasmFunctionWrapper::GetWrapperCode() {
|
2018-06-19 08:09:09 +00:00
|
|
|
Handle<Code> code;
|
|
|
|
if (!code_.ToHandle(&code)) {
|
2017-09-01 12:57:34 +00:00
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
|
2019-07-31 09:40:06 +00:00
|
|
|
auto call_descriptor = compiler::Linkage::GetSimplifiedCDescriptor(
|
|
|
|
zone(), signature_, CallDescriptor::kInitializeRootRegister);
|
2017-09-01 12:57:34 +00:00
|
|
|
|
2018-12-19 12:00:34 +00:00
|
|
|
if (kSystemPointerSize == 4) {
|
2017-09-01 12:57:34 +00:00
|
|
|
size_t num_params = signature_->parameter_count();
|
|
|
|
// One additional parameter for the pointer of the return value.
|
|
|
|
Signature<MachineRepresentation>::Builder rep_builder(zone(), 1,
|
|
|
|
num_params + 1);
|
|
|
|
|
|
|
|
rep_builder.AddReturn(MachineRepresentation::kWord32);
|
|
|
|
for (size_t i = 0; i < num_params + 1; i++) {
|
|
|
|
rep_builder.AddParam(MachineRepresentation::kWord32);
|
|
|
|
}
|
|
|
|
compiler::Int64Lowering r(graph(), machine(), common(), zone(),
|
|
|
|
rep_builder.Build());
|
|
|
|
r.LowerGraph();
|
|
|
|
}
|
|
|
|
|
2018-04-04 20:30:34 +00:00
|
|
|
OptimizedCompilationInfo info(ArrayVector("testing"), graph()->zone(),
|
2020-08-05 11:48:03 +00:00
|
|
|
CodeKind::C_WASM_ENTRY);
|
2017-11-15 14:36:57 +00:00
|
|
|
code_ = compiler::Pipeline::GenerateCodeForTesting(
|
2018-07-06 08:58:43 +00:00
|
|
|
&info, isolate, call_descriptor, graph(),
|
2018-09-04 11:55:58 +00:00
|
|
|
AssemblerOptions::Default(isolate));
|
2018-06-19 08:09:09 +00:00
|
|
|
code = code_.ToHandleChecked();
|
2017-09-01 12:57:34 +00:00
|
|
|
#ifdef ENABLE_DISASSEMBLER
|
|
|
|
if (FLAG_print_opt_code) {
|
2018-03-21 16:19:54 +00:00
|
|
|
CodeTracer::Scope tracing_scope(isolate->GetCodeTracer());
|
|
|
|
OFStream os(tracing_scope.file());
|
|
|
|
|
2019-09-25 10:03:40 +00:00
|
|
|
code->Disassemble("wasm wrapper", os, isolate);
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-06-19 08:09:09 +00:00
|
|
|
return code;
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 16:01:46 +00:00
|
|
|
// This struct is just a type tag for Zone::NewArray<T>(size_t) call.
|
|
|
|
struct WasmFunctionCompilerBuffer {};
|
|
|
|
|
2017-09-01 12:57:34 +00:00
|
|
|
void WasmFunctionCompiler::Build(const byte* start, const byte* end) {
|
|
|
|
size_t locals_size = local_decls.Size();
|
|
|
|
size_t total_size = end - start + locals_size + 1;
|
2020-07-16 16:01:46 +00:00
|
|
|
byte* buffer = zone()->NewArray<byte, WasmFunctionCompilerBuffer>(total_size);
|
2017-09-01 12:57:34 +00:00
|
|
|
// Prepend the local decls to the code.
|
|
|
|
local_decls.Emit(buffer);
|
|
|
|
// Emit the code.
|
|
|
|
memcpy(buffer + locals_size, start, end - start);
|
|
|
|
// Append an extra end opcode.
|
|
|
|
buffer[total_size - 1] = kExprEnd;
|
|
|
|
|
|
|
|
start = buffer;
|
|
|
|
end = buffer + total_size;
|
|
|
|
|
|
|
|
CHECK_GE(kMaxInt, end - start);
|
|
|
|
int len = static_cast<int>(end - start);
|
|
|
|
function_->code = {builder_->AddBytes(Vector<const byte>(start, len)),
|
|
|
|
static_cast<uint32_t>(len)};
|
|
|
|
|
|
|
|
if (interpreter_) {
|
2019-02-11 11:07:47 +00:00
|
|
|
// Add the code to the interpreter; do not generate compiled code.
|
2017-09-01 12:57:34 +00:00
|
|
|
interpreter_->SetFunctionCodeForTesting(function_, start, end);
|
2019-02-11 11:07:47 +00:00
|
|
|
return;
|
2018-08-21 15:01:31 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 14:34:47 +00:00
|
|
|
Vector<const uint8_t> wire_bytes = builder_->instance_object()
|
|
|
|
->module_object()
|
|
|
|
.native_module()
|
|
|
|
->wire_bytes();
|
2017-09-12 12:39:42 +00:00
|
|
|
|
2018-10-23 11:56:12 +00:00
|
|
|
CompilationEnv env = builder_->CreateCompilationEnv();
|
2017-09-12 12:39:42 +00:00
|
|
|
ScopedVector<uint8_t> func_wire_bytes(function_->code.length());
|
2019-04-29 11:06:49 +00:00
|
|
|
memcpy(func_wire_bytes.begin(), wire_bytes.begin() + function_->code.offset(),
|
2017-09-12 12:39:42 +00:00
|
|
|
func_wire_bytes.length());
|
|
|
|
|
|
|
|
FunctionBody func_body{function_->sig, function_->code.offset(),
|
2019-04-29 11:06:49 +00:00
|
|
|
func_wire_bytes.begin(), func_wire_bytes.end()};
|
2018-06-20 06:39:24 +00:00
|
|
|
NativeModule* native_module =
|
|
|
|
builder_->instance_object()->module_object().native_module();
|
2020-04-23 15:56:48 +00:00
|
|
|
ForDebugging for_debugging =
|
|
|
|
native_module->IsTieredDown() ? kForDebugging : kNoDebugging;
|
2020-04-14 10:42:23 +00:00
|
|
|
WasmCompilationUnit unit(function_->func_index, builder_->execution_tier(),
|
2020-04-23 15:56:48 +00:00
|
|
|
for_debugging);
|
2018-08-23 14:44:28 +00:00
|
|
|
WasmFeatures unused_detected_features;
|
2019-01-21 11:57:22 +00:00
|
|
|
WasmCompilationResult result = unit.ExecuteCompilation(
|
2019-04-10 14:58:46 +00:00
|
|
|
isolate()->wasm_engine(), &env,
|
|
|
|
native_module->compilation_state()->GetWireBytesStorage(),
|
2018-11-07 10:27:10 +00:00
|
|
|
isolate()->counters(), &unused_detected_features);
|
2020-04-03 09:35:33 +00:00
|
|
|
WasmCode* code = native_module->PublishCode(
|
|
|
|
native_module->AddCompiledCode(std::move(result)));
|
2019-01-21 11:57:22 +00:00
|
|
|
DCHECK_NOT_NULL(code);
|
|
|
|
if (WasmCode::ShouldBeLogged(isolate())) code->LogCode(isolate());
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 20:00:50 +00:00
|
|
|
WasmFunctionCompiler::WasmFunctionCompiler(Zone* zone, const FunctionSig* sig,
|
2017-09-12 12:39:42 +00:00
|
|
|
TestingModuleBuilder* builder,
|
|
|
|
const char* name)
|
2017-09-01 12:57:34 +00:00
|
|
|
: GraphAndBuilders(zone),
|
|
|
|
jsgraph(builder->isolate(), this->graph(), this->common(), nullptr,
|
|
|
|
nullptr, this->machine()),
|
|
|
|
sig(sig),
|
|
|
|
descriptor_(nullptr),
|
|
|
|
builder_(builder),
|
|
|
|
local_decls(zone, sig),
|
|
|
|
source_position_table_(this->graph()),
|
2017-09-12 12:39:42 +00:00
|
|
|
interpreter_(builder->interpreter()) {
|
2017-09-01 12:57:34 +00:00
|
|
|
// Get a new function from the testing module.
|
2018-06-20 06:33:57 +00:00
|
|
|
int index = builder->AddFunction(sig, name, TestingModuleBuilder::kWasm);
|
2017-09-01 12:57:34 +00:00
|
|
|
function_ = builder_->GetFunctionAt(index);
|
|
|
|
}
|
|
|
|
|
2018-09-13 09:27:26 +00:00
|
|
|
WasmFunctionCompiler::~WasmFunctionCompiler() = default;
|
2017-09-25 16:13:13 +00:00
|
|
|
|
2020-03-19 09:27:22 +00:00
|
|
|
/* static */
|
|
|
|
FunctionSig* WasmRunnerBase::CreateSig(Zone* zone, MachineType return_type,
|
|
|
|
Vector<MachineType> param_types) {
|
2017-09-01 12:57:34 +00:00
|
|
|
int return_count = return_type.IsNone() ? 0 : 1;
|
|
|
|
int param_count = param_types.length();
|
|
|
|
|
|
|
|
// Allocate storage array in zone.
|
2020-03-19 09:27:22 +00:00
|
|
|
ValueType* sig_types = zone->NewArray<ValueType>(return_count + param_count);
|
2017-09-01 12:57:34 +00:00
|
|
|
|
|
|
|
// Convert machine types to local types, and check that there are no
|
|
|
|
// MachineType::None()'s in the parameters.
|
|
|
|
int idx = 0;
|
2020-03-12 14:29:51 +00:00
|
|
|
if (return_count) sig_types[idx++] = ValueType::For(return_type);
|
2017-09-01 12:57:34 +00:00
|
|
|
for (MachineType param : param_types) {
|
|
|
|
CHECK_NE(MachineType::None(), param);
|
2020-03-12 14:29:51 +00:00
|
|
|
sig_types[idx++] = ValueType::For(param);
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
2020-07-09 11:51:58 +00:00
|
|
|
return zone->New<FunctionSig>(return_count, param_count, sig_types);
|
2017-09-01 12:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool WasmRunnerBase::trap_happened;
|
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|