2016-03-16 18:13:43 +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 "src/v8.h"
|
|
|
|
|
|
|
|
#include "src/debug/debug.h"
|
|
|
|
#include "src/disasm.h"
|
|
|
|
#include "src/disassembler.h"
|
2017-08-03 09:35:57 +00:00
|
|
|
#include "src/frame-constants.h"
|
2016-03-16 18:13:43 +00:00
|
|
|
#include "src/ic/ic.h"
|
|
|
|
#include "src/macro-assembler.h"
|
2017-09-13 10:56:20 +00:00
|
|
|
#include "src/objects-inl.h"
|
|
|
|
#include "src/ostreams.h"
|
2016-03-16 18:13:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
#include "test/cctest/compiler/c-signature.h"
|
|
|
|
#include "test/cctest/compiler/call-tester.h"
|
|
|
|
|
2017-09-01 12:57:34 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
2016-03-16 18:13:43 +00:00
|
|
|
|
|
|
|
#define __ assm.
|
|
|
|
|
|
|
|
static int32_t DummyStaticFunction(Object* result) { return 1; }
|
|
|
|
|
[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
|
|
|
TEST(WasmRelocationIa32ContextReference) {
|
2016-03-16 18:13:43 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(isolate->allocator(), ZONE_NAME);
|
2016-03-16 18:13:43 +00:00
|
|
|
HandleScope scope(isolate);
|
|
|
|
v8::internal::byte buffer[4096];
|
|
|
|
Assembler assm(isolate, buffer, sizeof buffer);
|
2017-10-13 16:33:03 +00:00
|
|
|
DummyStaticFunction(nullptr);
|
2016-03-16 18:13:43 +00:00
|
|
|
int32_t imm = 1234567;
|
|
|
|
|
|
|
|
__ mov(eax, Immediate(reinterpret_cast<Address>(imm),
|
[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
|
|
|
RelocInfo::WASM_CONTEXT_REFERENCE));
|
2016-03-16 18:13:43 +00:00
|
|
|
__ nop();
|
|
|
|
__ ret(0);
|
|
|
|
|
2018-01-16 09:26:02 +00:00
|
|
|
compiler::CSignatureOf<int32_t> csig;
|
2016-03-16 18:13:43 +00:00
|
|
|
CodeDesc desc;
|
2017-05-31 14:00:11 +00:00
|
|
|
assm.GetCode(isolate, &desc);
|
2017-09-29 14:59:24 +00:00
|
|
|
Handle<Code> code =
|
|
|
|
isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
|
2016-03-16 18:13:43 +00:00
|
|
|
USE(code);
|
|
|
|
|
2017-09-01 12:57:34 +00:00
|
|
|
compiler::CodeRunner<int32_t> runnable(isolate, code, &csig);
|
2016-03-16 18:13:43 +00:00
|
|
|
int32_t ret_value = runnable.Call();
|
|
|
|
CHECK_EQ(ret_value, imm);
|
|
|
|
|
|
|
|
#ifdef OBJECT_PRINT
|
|
|
|
OFStream os(stdout);
|
|
|
|
code->Print(os);
|
|
|
|
byte* begin = code->instruction_start();
|
|
|
|
byte* end = begin + code->instruction_size();
|
|
|
|
disasm::Disassembler::Disassemble(stdout, begin, end);
|
|
|
|
#endif
|
|
|
|
|
2016-11-10 12:50:51 +00:00
|
|
|
int offset = 1234;
|
2016-03-16 18:13:43 +00:00
|
|
|
|
|
|
|
// Relocating references by offset
|
[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
|
|
|
int mode_mask = (1 << RelocInfo::WASM_CONTEXT_REFERENCE);
|
2016-03-16 18:13:43 +00:00
|
|
|
for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) {
|
2017-11-07 10:29:52 +00:00
|
|
|
// TODO(6792): No longer needed once WebAssembly code is off heap.
|
|
|
|
CodeSpaceMemoryModificationScope modification_scope(isolate->heap());
|
[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(RelocInfo::IsWasmContextReference(it.rinfo()->rmode()));
|
|
|
|
it.rinfo()->set_wasm_context_reference(
|
|
|
|
isolate, it.rinfo()->wasm_context_reference() + offset,
|
|
|
|
SKIP_ICACHE_FLUSH);
|
2016-03-16 18:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if immediate is updated correctly
|
|
|
|
ret_value = runnable.Call();
|
|
|
|
CHECK_EQ(ret_value, imm + offset);
|
|
|
|
|
|
|
|
#ifdef OBJECT_PRINT
|
|
|
|
code->Print(os);
|
|
|
|
begin = code->instruction_start();
|
|
|
|
end = begin + code->instruction_size();
|
|
|
|
disasm::Disassembler::Disassemble(stdout, begin, end);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef __
|
2017-09-01 12:57:34 +00:00
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|