v8/test/cctest/wasm/test-run-wasm-relocation.cc
Ben L. Titzer c02f5e3ab3 [wasm] Store the globals_start in WasmContext.
This CL removes the code specialization for WASM functions that access
globals. Previously, we were embedding the start address of the globals
memory (globals_start) as a constant in the code, which required
patching for every instance. We now put this base in to the WasmContext,
which is available as a parameter to every WasmFunction.

R=ahaas@chromium.org,
CC=mtrofin@chromium.org

Bug: 
Change-Id: I04bb739e898cc5a3b7dd081cc166483022d113fd
Reviewed-on: https://chromium-review.googlesource.com/712595
Commit-Queue: Ben Titzer <titzer@chromium.org>
Reviewed-by: Mircea Trofin <mtrofin@chromium.org>
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Reviewed-by: Bill Budge <bbudge@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48581}
2017-10-16 09:35:47 +00:00

65 lines
2.0 KiB
C++

// 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/assembler-inl.h"
#include "src/objects-inl.h"
#include "src/v8.h"
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/c-signature.h"
#include "test/cctest/wasm/wasm-run-utils.h"
#include "test/common/wasm/wasm-macro-gen.h"
namespace v8 {
namespace internal {
namespace wasm {
namespace test_run_wasm_relocation {
TEST(RunPatchWasmContext) {
WasmRunner<uint32_t, uint32_t> r(kExecuteCompiled);
Isolate* isolate = CcTest::i_isolate();
r.builder().AddGlobal<uint32_t>();
r.builder().AddGlobal<uint32_t>();
BUILD(r, WASM_SET_GLOBAL(0, WASM_GET_LOCAL(0)), WASM_GET_GLOBAL(0));
CHECK_EQ(1, r.builder().CodeTableLength());
// Run with the old global data.
CHECK_EQ(113, r.Call(113));
WasmContext* old_wasm_context =
r.builder().instance_object()->wasm_context()->get();
Address old_wasm_context_address =
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)};
// Patch in a new WasmContext that points to the new global data.
int filter = 1 << RelocInfo::WASM_CONTEXT_REFERENCE;
bool patched = false;
Handle<Code> code = r.GetWrapperCode();
for (RelocIterator it(*code, filter); !it.done(); it.next()) {
CHECK_EQ(old_wasm_context_address, it.rinfo()->wasm_context_reference());
it.rinfo()->set_wasm_context_reference(
isolate, reinterpret_cast<Address>(&new_wasm_context));
patched = true;
}
CHECK(patched);
Assembler::FlushICache(isolate, code->instruction_start(),
code->instruction_size());
// Run with the new global data.
CHECK_EQ(115, r.Call(115));
CHECK_EQ(115, new_global_data[0]);
}
} // namespace test_run_wasm_relocation
} // namespace wasm
} // namespace internal
} // namespace v8