Commit 68975751 authored by Ben L. Titzer's avatar Ben L. Titzer Committed by Commit Bot

[wasm] Simplify init by adding SetRawMemory() to WasmContext.

Bug: 
Change-Id: I1f4a9d06e91a0523e590a77f8073800d6f1994d6
Reviewed-on: https://chromium-review.googlesource.com/830393
Commit-Queue: Ben Titzer <titzer@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50140}
parent a449f09f
This diff is collapsed.
......@@ -399,8 +399,8 @@ Handle<JSArrayBuffer> GrowMemoryBuffer(Isolate* isolate,
void SetInstanceMemory(Isolate* isolate, Handle<WasmInstanceObject> instance,
Handle<JSArrayBuffer> buffer) {
auto wasm_context = instance->wasm_context()->get();
wasm_context->mem_start = reinterpret_cast<byte*>(buffer->backing_store());
wasm_context->mem_size = buffer->byte_length()->Number();
wasm_context->SetRawMemory(reinterpret_cast<byte*>(buffer->backing_store()),
buffer->byte_length()->Number());
#if DEBUG
// To flush out bugs earlier, in DEBUG mode, check that all pages of the
// memory are accessible by reading and writing one byte on each page.
......@@ -563,8 +563,7 @@ Handle<WasmInstanceObject> WasmInstanceObject::New(
reinterpret_cast<WasmInstanceObject*>(*instance_object), isolate);
auto wasm_context = Managed<WasmContext>::Allocate(isolate);
wasm_context->get()->mem_start = nullptr;
wasm_context->get()->mem_size = 0;
wasm_context->get()->SetRawMemory(nullptr, 0);
wasm_context->get()->globals_start = nullptr;
instance->set_wasm_context(*wasm_context);
......
......@@ -59,10 +59,18 @@ class WasmInstanceObject;
// grow_memory). The address of the WasmContext is provided to the wasm entry
// functions using a RelocatableIntPtrConstant, then the address is passed as
// parameter to the other wasm functions.
// Note that generated code can directly read from instances of this struct.
struct WasmContext {
byte* mem_start;
uint32_t mem_size;
byte* globals_start;
byte* mem_start = nullptr;
uint32_t mem_size = 0; // TODO(titzer): uintptr_t?
byte* globals_start = nullptr;
inline void SetRawMemory(void* mem_start, size_t mem_size) {
DCHECK_LE(mem_size,
wasm::kV8MaxWasmMemoryPages * wasm::kSpecMaxWasmMemoryPages);
this->mem_start = static_cast<byte*>(mem_start);
this->mem_size = static_cast<uint32_t>(mem_size);
}
};
// Representation of a WebAssembly.Module JavaScript-level object.
......
......@@ -50,7 +50,8 @@ static void RunLoadStoreRelocation(MachineType rep) {
CType new_buffer[kNumElems];
byte* raw = reinterpret_cast<byte*>(buffer);
byte* new_raw = reinterpret_cast<byte*>(new_buffer);
WasmContext wasm_context = {raw, sizeof(buffer), nullptr};
WasmContext wasm_context;
wasm_context.SetRawMemory(raw, sizeof(buffer));
for (size_t i = 0; i < sizeof(buffer); i++) {
raw[i] = static_cast<byte>((i + sizeof(CType)) ^ 0xAA);
new_raw[i] = static_cast<byte>((i + sizeof(CType)) ^ 0xAA);
......@@ -70,8 +71,7 @@ static void RunLoadStoreRelocation(MachineType rep) {
CHECK(buffer[0] != buffer[1]);
CHECK_EQ(OK, m.Call());
CHECK(buffer[0] == buffer[1]);
wasm_context.mem_size = sizeof(new_buffer);
wasm_context.mem_start = new_raw;
wasm_context.SetRawMemory(new_raw, sizeof(new_buffer));
CHECK(new_buffer[0] != new_buffer[1]);
CHECK_EQ(OK, m.Call());
CHECK(new_buffer[0] == new_buffer[1]);
......@@ -101,7 +101,7 @@ static void RunLoadStoreRelocationOffset(MachineType rep) {
int32_t y = kNumElems - x - 1;
// initialize the buffer with raw data.
byte* raw = reinterpret_cast<byte*>(buffer);
wasm_context = {raw, sizeof(buffer), nullptr};
wasm_context.SetRawMemory(raw, sizeof(buffer));
for (size_t i = 0; i < sizeof(buffer); i++) {
raw[i] = static_cast<byte>((i + sizeof(buffer)) ^ 0xAA);
}
......@@ -130,8 +130,7 @@ static void RunLoadStoreRelocationOffset(MachineType rep) {
new_raw[i] = static_cast<byte>((i + sizeof(buffer)) ^ 0xAA);
}
wasm_context.mem_size = sizeof(new_buffer);
wasm_context.mem_start = new_raw;
wasm_context.SetRawMemory(new_raw, sizeof(new_buffer));
CHECK(new_buffer[x] != new_buffer[y]);
CHECK_EQ(OK, m.Call());
......@@ -154,7 +153,8 @@ TEST(RunLoadStoreRelocationOffset) {
TEST(Uint32LessThanMemoryRelocation) {
RawMachineAssemblerTester<uint32_t> m;
RawMachineLabel within_bounds, out_of_bounds;
WasmContext wasm_context = {reinterpret_cast<Address>(1234), 0x200, nullptr};
WasmContext wasm_context;
wasm_context.SetRawMemory(reinterpret_cast<void*>(1234), 0x200);
Node* index = m.Int32Constant(0x200);
Node* wasm_context_node =
m.RelocatableIntPtrConstant(reinterpret_cast<uintptr_t>(&wasm_context),
......@@ -169,7 +169,7 @@ TEST(Uint32LessThanMemoryRelocation) {
m.Return(m.Int32Constant(0xDEADBEEF));
// Check that index is out of bounds with current size
CHECK_EQ(0xDEADBEEF, m.Call());
wasm_context.mem_size = 0x400;
wasm_context.SetRawMemory(wasm_context.mem_start, 0x400);
// Check that after limit is increased, index is within bounds.
CHECK_EQ(0xACEDu, m.Call());
}
......
......@@ -36,8 +36,8 @@ WASM_COMPILED_EXEC_TEST(RunPatchWasmContext) {
reinterpret_cast<Address>(old_wasm_context);
uint32_t new_global_data[3] = {0, 0, 0};
WasmContext new_wasm_context = {0, 0,
reinterpret_cast<byte*>(new_global_data)};
WasmContext new_wasm_context;
new_wasm_context.globals_start = reinterpret_cast<byte*>(new_global_data);
{
// TODO(6792): No longer needed once WebAssembly code is off heap.
......
......@@ -61,8 +61,7 @@ byte* TestingModuleBuilder::AddMemory(uint32_t size) {
// 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_->wasm_context()->get()->mem_start = mem_start_;
instance_object_->wasm_context()->get()->mem_size = mem_size_;
instance_object_->wasm_context()->get()->SetRawMemory(mem_start_, mem_size_);
return mem_start_;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment