2017-09-16 05:22:38 +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.
|
|
|
|
|
2018-02-02 12:05:19 +00:00
|
|
|
#ifndef V8_WASM_WASM_CODE_MANAGER_H_
|
|
|
|
#define V8_WASM_WASM_CODE_MANAGER_H_
|
2017-09-16 05:22:38 +00:00
|
|
|
|
2019-03-11 12:18:46 +00:00
|
|
|
#include <atomic>
|
2017-09-16 05:22:38 +00:00
|
|
|
#include <list>
|
2017-11-20 21:34:04 +00:00
|
|
|
#include <map>
|
2019-03-11 12:18:46 +00:00
|
|
|
#include <memory>
|
2019-03-25 12:23:37 +00:00
|
|
|
#include <unordered_set>
|
2019-03-11 12:18:46 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2017-09-16 05:22:38 +00:00
|
|
|
|
|
|
|
#include "src/base/macros.h"
|
2019-04-02 09:23:45 +00:00
|
|
|
#include "src/base/optional.h"
|
2018-08-22 09:14:23 +00:00
|
|
|
#include "src/builtins/builtins-definitions.h"
|
2017-11-20 21:34:04 +00:00
|
|
|
#include "src/handles.h"
|
|
|
|
#include "src/trap-handler/trap-handler.h"
|
2017-09-16 05:22:38 +00:00
|
|
|
#include "src/vector.h"
|
2018-10-23 11:37:58 +00:00
|
|
|
#include "src/wasm/compilation-environment.h"
|
2018-08-08 14:54:44 +00:00
|
|
|
#include "src/wasm/wasm-features.h"
|
2018-09-25 16:07:22 +00:00
|
|
|
#include "src/wasm/wasm-limits.h"
|
2018-03-19 09:22:23 +00:00
|
|
|
|
2017-09-16 05:22:38 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2017-11-20 21:34:04 +00:00
|
|
|
|
|
|
|
class Code;
|
2019-02-05 12:54:32 +00:00
|
|
|
class CodeDesc;
|
|
|
|
class Isolate;
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2017-09-16 05:22:38 +00:00
|
|
|
namespace wasm {
|
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
class NativeModule;
|
2018-04-05 14:14:24 +00:00
|
|
|
class WasmCodeManager;
|
2019-03-19 12:52:04 +00:00
|
|
|
struct WasmCompilationResult;
|
2019-01-25 10:35:37 +00:00
|
|
|
class WasmEngine;
|
2018-09-18 13:05:45 +00:00
|
|
|
class WasmMemoryTracker;
|
2018-10-10 12:22:19 +00:00
|
|
|
class WasmImportWrapperCache;
|
2017-11-20 21:34:04 +00:00
|
|
|
struct WasmModule;
|
|
|
|
|
2018-09-25 14:21:11 +00:00
|
|
|
// Sorted, disjoint and non-overlapping memory regions. A region is of the
|
2017-09-16 05:22:38 +00:00
|
|
|
// form [start, end). So there's no [start, end), [end, other_end),
|
|
|
|
// because that should have been reduced to [start, other_end).
|
|
|
|
class V8_EXPORT_PRIVATE DisjointAllocationPool final {
|
|
|
|
public:
|
2018-06-08 11:05:58 +00:00
|
|
|
DisjointAllocationPool() = default;
|
2017-09-16 05:22:38 +00:00
|
|
|
|
2018-09-25 14:21:11 +00:00
|
|
|
explicit DisjointAllocationPool(base::AddressRegion region)
|
|
|
|
: regions_({region}) {}
|
2017-09-16 05:22:38 +00:00
|
|
|
|
2018-12-20 18:26:00 +00:00
|
|
|
DisjointAllocationPool(DisjointAllocationPool&& other) V8_NOEXCEPT = default;
|
|
|
|
DisjointAllocationPool& operator=(DisjointAllocationPool&& other)
|
|
|
|
V8_NOEXCEPT = default;
|
2017-09-16 05:22:38 +00:00
|
|
|
|
2018-09-25 14:21:11 +00:00
|
|
|
// Merge the parameter region into this object while preserving ordering of
|
|
|
|
// the regions. The assumption is that the passed parameter is not
|
|
|
|
// intersecting this object - for example, it was obtained from a previous
|
|
|
|
// Allocate.
|
|
|
|
void Merge(base::AddressRegion);
|
2017-09-16 05:22:38 +00:00
|
|
|
|
2018-09-25 14:21:11 +00:00
|
|
|
// Allocate a contiguous region of size {size}. Return an empty pool on
|
2017-09-16 05:22:38 +00:00
|
|
|
// failure.
|
2018-09-25 14:21:11 +00:00
|
|
|
base::AddressRegion Allocate(size_t size);
|
2017-09-16 05:22:38 +00:00
|
|
|
|
2018-09-25 14:21:11 +00:00
|
|
|
bool IsEmpty() const { return regions_.empty(); }
|
|
|
|
const std::list<base::AddressRegion>& regions() const { return regions_; }
|
2017-09-16 05:22:38 +00:00
|
|
|
|
|
|
|
private:
|
2018-09-25 14:21:11 +00:00
|
|
|
std::list<base::AddressRegion> regions_;
|
2017-09-16 05:22:38 +00:00
|
|
|
|
2018-12-11 14:20:29 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(DisjointAllocationPool);
|
2017-09-16 05:22:38 +00:00
|
|
|
};
|
|
|
|
|
2019-04-02 09:23:45 +00:00
|
|
|
struct WasmCodeUpdate {
|
|
|
|
WasmCode* code = nullptr;
|
|
|
|
base::Optional<ExecutionTier> tier;
|
|
|
|
base::Optional<ExecutionTier> prior_tier;
|
|
|
|
};
|
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
class V8_EXPORT_PRIVATE WasmCode final {
|
|
|
|
public:
|
|
|
|
enum Kind {
|
2017-11-29 20:23:19 +00:00
|
|
|
kFunction,
|
|
|
|
kWasmToJsWrapper,
|
2018-06-04 10:12:54 +00:00
|
|
|
kRuntimeStub,
|
2018-05-15 07:18:34 +00:00
|
|
|
kInterpreterEntry,
|
2018-06-19 09:47:17 +00:00
|
|
|
kJumpTable
|
2017-11-20 21:34:04 +00:00
|
|
|
};
|
|
|
|
|
2018-06-04 10:12:54 +00:00
|
|
|
// Each runtime stub is identified by an id. This id is used to reference the
|
|
|
|
// stub via {RelocInfo::WASM_STUB_CALL} and gets resolved during relocation.
|
|
|
|
enum RuntimeStubId {
|
2018-06-06 13:22:09 +00:00
|
|
|
#define DEF_ENUM(Name) k##Name,
|
|
|
|
#define DEF_ENUM_TRAP(Name) kThrowWasm##Name,
|
|
|
|
WASM_RUNTIME_STUB_LIST(DEF_ENUM, DEF_ENUM_TRAP)
|
|
|
|
#undef DEF_ENUM_TRAP
|
2018-06-04 10:12:54 +00:00
|
|
|
#undef DEF_ENUM
|
|
|
|
kRuntimeStubCount
|
|
|
|
};
|
|
|
|
|
2018-02-28 15:49:53 +00:00
|
|
|
// kOther is used if we have WasmCode that is neither
|
|
|
|
// liftoff- nor turbofan-compiled, i.e. if Kind is
|
|
|
|
// not a kFunction.
|
2018-03-24 14:05:21 +00:00
|
|
|
enum Tier : int8_t { kLiftoff, kTurbofan, kOther };
|
2018-02-28 15:49:53 +00:00
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
Vector<byte> instructions() const { return instructions_; }
|
2018-04-13 22:28:05 +00:00
|
|
|
Address instruction_start() const {
|
|
|
|
return reinterpret_cast<Address>(instructions_.start());
|
|
|
|
}
|
2018-06-27 12:57:52 +00:00
|
|
|
Vector<const byte> reloc_info() const { return reloc_info_.as_vector(); }
|
2018-03-23 11:30:26 +00:00
|
|
|
Vector<const byte> source_positions() const {
|
2018-06-27 12:57:52 +00:00
|
|
|
return source_position_table_.as_vector();
|
2018-03-23 11:30:26 +00:00
|
|
|
}
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-09-25 16:07:22 +00:00
|
|
|
uint32_t index() const {
|
|
|
|
DCHECK(!IsAnonymous());
|
|
|
|
return index_;
|
|
|
|
}
|
2018-06-19 15:48:38 +00:00
|
|
|
// Anonymous functions are functions that don't carry an index.
|
2018-09-25 16:07:22 +00:00
|
|
|
bool IsAnonymous() const { return index_ == kAnonymousFuncIndex; }
|
2017-11-20 21:34:04 +00:00
|
|
|
Kind kind() const { return kind_; }
|
2018-03-15 12:53:10 +00:00
|
|
|
NativeModule* native_module() const { return native_module_; }
|
2018-02-28 15:49:53 +00:00
|
|
|
Tier tier() const { return tier_; }
|
2017-11-20 21:34:04 +00:00
|
|
|
Address constant_pool() const;
|
2018-12-13 19:30:56 +00:00
|
|
|
Address code_comments() const;
|
2017-11-20 21:34:04 +00:00
|
|
|
size_t constant_pool_offset() const { return constant_pool_offset_; }
|
|
|
|
size_t safepoint_table_offset() const { return safepoint_table_offset_; }
|
2018-02-27 09:23:48 +00:00
|
|
|
size_t handler_table_offset() const { return handler_table_offset_; }
|
2018-12-13 19:30:56 +00:00
|
|
|
size_t code_comments_offset() const { return code_comments_offset_; }
|
|
|
|
size_t unpadded_binary_size() const { return unpadded_binary_size_; }
|
2017-11-20 21:34:04 +00:00
|
|
|
uint32_t stack_slots() const { return stack_slots_; }
|
2019-01-21 16:38:01 +00:00
|
|
|
uint32_t tagged_parameter_slots() const { return tagged_parameter_slots_; }
|
2018-02-28 15:49:53 +00:00
|
|
|
bool is_liftoff() const { return tier_ == kLiftoff; }
|
2018-05-07 13:37:03 +00:00
|
|
|
bool contains(Address pc) const {
|
|
|
|
return reinterpret_cast<Address>(instructions_.start()) <= pc &&
|
|
|
|
pc < reinterpret_cast<Address>(instructions_.end());
|
|
|
|
}
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-06-27 10:26:30 +00:00
|
|
|
Vector<trap_handler::ProtectedInstructionData> protected_instructions()
|
|
|
|
const {
|
|
|
|
return protected_instructions_.as_vector();
|
2017-11-20 21:34:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 10:49:13 +00:00
|
|
|
void Validate() const;
|
2018-06-22 11:41:06 +00:00
|
|
|
void Print(const char* name = nullptr) const;
|
2019-01-16 15:29:52 +00:00
|
|
|
void MaybePrint(const char* name = nullptr) const;
|
2018-06-22 11:41:06 +00:00
|
|
|
void Disassemble(const char* name, std::ostream& os,
|
2018-04-23 07:21:06 +00:00
|
|
|
Address current_pc = kNullAddress) const;
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-03-22 12:24:51 +00:00
|
|
|
static bool ShouldBeLogged(Isolate* isolate);
|
|
|
|
void LogCode(Isolate* isolate) const;
|
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
~WasmCode();
|
|
|
|
|
2019-03-25 12:23:37 +00:00
|
|
|
void IncRef() {
|
|
|
|
int old_val = ref_count_.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
DCHECK_LE(1, old_val);
|
|
|
|
DCHECK_GT(kMaxInt, old_val);
|
|
|
|
USE(old_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
V8_WARN_UNUSED_RESULT bool DecRef() {
|
|
|
|
int old_count = ref_count_.fetch_sub(1, std::memory_order_relaxed);
|
|
|
|
DCHECK_LE(1, old_count);
|
|
|
|
return old_count == 1;
|
|
|
|
}
|
|
|
|
|
2018-03-22 11:20:00 +00:00
|
|
|
enum FlushICache : bool { kFlushICache = true, kNoFlushICache = false };
|
|
|
|
|
2018-11-15 10:15:13 +00:00
|
|
|
static constexpr uint32_t kAnonymousFuncIndex = 0xffffffff;
|
|
|
|
STATIC_ASSERT(kAnonymousFuncIndex > kV8MaxWasmFunctions);
|
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
private:
|
|
|
|
friend class NativeModule;
|
|
|
|
|
2018-09-25 16:07:22 +00:00
|
|
|
WasmCode(NativeModule* native_module, uint32_t index,
|
2018-06-28 09:45:22 +00:00
|
|
|
Vector<byte> instructions, uint32_t stack_slots,
|
2019-01-21 16:38:01 +00:00
|
|
|
uint32_t tagged_parameter_slots, size_t safepoint_table_offset,
|
|
|
|
size_t handler_table_offset, size_t constant_pool_offset,
|
|
|
|
size_t code_comments_offset, size_t unpadded_binary_size,
|
2018-06-27 10:26:30 +00:00
|
|
|
OwnedVector<trap_handler::ProtectedInstructionData>
|
|
|
|
protected_instructions,
|
2018-06-28 09:45:22 +00:00
|
|
|
OwnedVector<const byte> reloc_info,
|
|
|
|
OwnedVector<const byte> source_position_table, Kind kind, Tier tier)
|
2017-11-20 21:34:04 +00:00
|
|
|
: instructions_(instructions),
|
|
|
|
reloc_info_(std::move(reloc_info)),
|
2018-06-28 09:45:22 +00:00
|
|
|
source_position_table_(std::move(source_position_table)),
|
2018-03-15 12:53:10 +00:00
|
|
|
native_module_(native_module),
|
2017-11-20 21:34:04 +00:00
|
|
|
index_(index),
|
|
|
|
kind_(kind),
|
|
|
|
constant_pool_offset_(constant_pool_offset),
|
|
|
|
stack_slots_(stack_slots),
|
2019-01-21 16:38:01 +00:00
|
|
|
tagged_parameter_slots_(tagged_parameter_slots),
|
2017-11-20 21:34:04 +00:00
|
|
|
safepoint_table_offset_(safepoint_table_offset),
|
2018-02-27 09:23:48 +00:00
|
|
|
handler_table_offset_(handler_table_offset),
|
2018-12-13 19:30:56 +00:00
|
|
|
code_comments_offset_(code_comments_offset),
|
|
|
|
unpadded_binary_size_(unpadded_binary_size),
|
2018-01-08 12:05:08 +00:00
|
|
|
protected_instructions_(std::move(protected_instructions)),
|
2018-03-20 16:14:55 +00:00
|
|
|
tier_(tier) {
|
2018-12-13 19:30:56 +00:00
|
|
|
DCHECK_LE(safepoint_table_offset, unpadded_binary_size);
|
|
|
|
DCHECK_LE(handler_table_offset, unpadded_binary_size);
|
|
|
|
DCHECK_LE(code_comments_offset, unpadded_binary_size);
|
|
|
|
DCHECK_LE(constant_pool_offset, unpadded_binary_size);
|
2018-03-20 16:14:55 +00:00
|
|
|
}
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-04-06 13:27:34 +00:00
|
|
|
// Code objects that have been registered with the global trap handler within
|
|
|
|
// this process, will have a {trap_handler_index} associated with them.
|
|
|
|
size_t trap_handler_index() const;
|
|
|
|
void set_trap_handler_index(size_t);
|
|
|
|
bool HasTrapHandlerIndex() const;
|
2018-06-18 11:34:41 +00:00
|
|
|
|
|
|
|
// Register protected instruction information with the trap handler. Sets
|
|
|
|
// trap_handler_index.
|
|
|
|
void RegisterTrapHandlerData();
|
2018-04-06 13:27:34 +00:00
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
Vector<byte> instructions_;
|
2018-06-27 12:57:52 +00:00
|
|
|
OwnedVector<const byte> reloc_info_;
|
|
|
|
OwnedVector<const byte> source_position_table_;
|
2018-03-15 12:53:10 +00:00
|
|
|
NativeModule* native_module_ = nullptr;
|
2018-09-25 16:07:22 +00:00
|
|
|
uint32_t index_;
|
2017-11-20 21:34:04 +00:00
|
|
|
Kind kind_;
|
|
|
|
size_t constant_pool_offset_ = 0;
|
|
|
|
uint32_t stack_slots_ = 0;
|
2019-01-21 16:38:01 +00:00
|
|
|
// Number of tagged parameters passed to this function via the stack. This
|
|
|
|
// value is used by the stack walker (e.g. GC) to find references.
|
|
|
|
uint32_t tagged_parameter_slots_ = 0;
|
2017-11-20 21:34:04 +00:00
|
|
|
// we care about safepoint data for wasm-to-js functions,
|
|
|
|
// since there may be stack/register tagged values for large number
|
|
|
|
// conversions.
|
|
|
|
size_t safepoint_table_offset_ = 0;
|
2018-02-27 09:23:48 +00:00
|
|
|
size_t handler_table_offset_ = 0;
|
2018-12-13 19:30:56 +00:00
|
|
|
size_t code_comments_offset_ = 0;
|
|
|
|
size_t unpadded_binary_size_ = 0;
|
2017-11-20 21:34:04 +00:00
|
|
|
intptr_t trap_handler_index_ = -1;
|
2018-06-27 10:26:30 +00:00
|
|
|
OwnedVector<trap_handler::ProtectedInstructionData> protected_instructions_;
|
2018-02-28 15:49:53 +00:00
|
|
|
Tier tier_;
|
2018-03-21 13:00:04 +00:00
|
|
|
|
2019-03-25 12:23:37 +00:00
|
|
|
// WasmCode is ref counted. Counters are held by:
|
|
|
|
// 1) The jump table.
|
|
|
|
// 2) Function tables.
|
|
|
|
// 3) {WasmCodeRefScope}s.
|
|
|
|
// 4) Threads currently executing this code.
|
|
|
|
// If a decrement of (1) or (2) would drop the ref count to 0, that code
|
|
|
|
// becomes a candidate for garbage collection. At that point, we add
|
|
|
|
// ref counts for (4) *before* decrementing the counter to ensure the code
|
|
|
|
// stays alive as long as it's being used. Once the ref count drops to zero,
|
|
|
|
// the code object is deleted and the memory for the machine code is freed.
|
|
|
|
std::atomic<int> ref_count_{1};
|
|
|
|
|
2018-03-21 13:00:04 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WasmCode);
|
2017-11-20 21:34:04 +00:00
|
|
|
};
|
|
|
|
|
2017-11-29 17:53:43 +00:00
|
|
|
// Return a textual description of the kind.
|
|
|
|
const char* GetWasmCodeKindAsString(WasmCode::Kind);
|
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
class V8_EXPORT_PRIVATE NativeModule final {
|
|
|
|
public:
|
2018-06-28 13:23:24 +00:00
|
|
|
#if V8_TARGET_ARCH_X64 || V8_TARGET_ARCH_S390X || V8_TARGET_ARCH_ARM64
|
|
|
|
static constexpr bool kCanAllocateMoreMemory = false;
|
|
|
|
#else
|
|
|
|
static constexpr bool kCanAllocateMoreMemory = true;
|
|
|
|
#endif
|
|
|
|
|
2018-09-21 12:00:23 +00:00
|
|
|
// {AddCode} is thread safe w.r.t. other calls to {AddCode} or methods adding
|
|
|
|
// code below, i.e. it can be called concurrently from background threads.
|
[wasm] Split adding code from publishing it
This prepares a refactoring to add and publish compilation results in
batches. For this, we need to separate the two phases, so that we can
lock the module, allocate all the code space, release the lock, copy
the code, lock the module, publish the code, and release the lock
again.
In particular, this CL does the following:
1) It removes the {AddOwnedCode} method. The functionality of creating
the {WasmCode} and memcpy'ing the instruction into that is done in
the other {Add*Code} methods. Adding to {owned_code_} is done in
{PublishCode}.
2) {PublishInterpreterEntry} is now functionally equivalent to
{PublishCode}, so it's removed.
3) After {AddCode}, the caller has to call {PublishCode}. In a
follow-up CL, this will be called in batches (first {AddCode} them
all, then {PublishCode} them all).
4) {AddCompiledCode} now assumes that the {WasmCompilationResult}
succeeded. Otherwise, the caller should directly call {SetError} on
the {CompilationState}.
5) {PublishCode} is now the chokepoint for installing code to the code
table, the owned code vector, the jump table, and setting interpreter
redirections. It replaces previous direct calls to {InstallCode} or
explicitly adding to {owned_code_}.
6) Increasing the {generated_code_size_} counter is now done in
{AllocateForCode}, which is the chokepoint for allocating space for
generated code. This way, we will only increase this counter once
once we allocate in batches.
R=titzer@chromium.org
Bug: v8:8916
Change-Id: I71e02e3a838f21797915cee3ebd373804fb12237
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1530817
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Ben Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60369}
2019-03-20 13:04:33 +00:00
|
|
|
// The returned code still needs to be published via {PublishCode}.
|
|
|
|
std::unique_ptr<WasmCode> AddCode(
|
|
|
|
uint32_t index, const CodeDesc& desc, uint32_t stack_slots,
|
|
|
|
uint32_t tagged_parameter_slots,
|
|
|
|
OwnedVector<trap_handler::ProtectedInstructionData>
|
|
|
|
protected_instructions,
|
|
|
|
OwnedVector<const byte> source_position_table, WasmCode::Kind kind,
|
|
|
|
WasmCode::Tier tier);
|
|
|
|
|
|
|
|
// {PublishCode} makes the code available to the system by entering it into
|
|
|
|
// the code table and patching the jump table. It returns a raw pointer to the
|
|
|
|
// given {WasmCode} object.
|
2019-04-02 09:23:45 +00:00
|
|
|
WasmCodeUpdate PublishCode(std::unique_ptr<WasmCode>);
|
2019-03-20 13:05:25 +00:00
|
|
|
// Hold the {allocation_mutex_} when calling {PublishCodeLocked}.
|
2019-04-02 09:23:45 +00:00
|
|
|
WasmCodeUpdate PublishCodeLocked(std::unique_ptr<WasmCode>);
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-06-28 09:45:22 +00:00
|
|
|
WasmCode* AddDeserializedCode(
|
|
|
|
uint32_t index, Vector<const byte> instructions, uint32_t stack_slots,
|
2019-01-21 16:38:01 +00:00
|
|
|
uint32_t tagged_parameter_slots, size_t safepoint_table_offset,
|
|
|
|
size_t handler_table_offset, size_t constant_pool_offset,
|
|
|
|
size_t code_comments_offset, size_t unpadded_binary_size,
|
2018-06-28 09:45:22 +00:00
|
|
|
OwnedVector<trap_handler::ProtectedInstructionData>
|
|
|
|
protected_instructions,
|
|
|
|
OwnedVector<const byte> reloc_info,
|
2019-03-12 14:11:06 +00:00
|
|
|
OwnedVector<const byte> source_position_table, WasmCode::Kind kind,
|
|
|
|
WasmCode::Tier tier);
|
2018-06-28 09:45:22 +00:00
|
|
|
|
2018-09-21 12:00:23 +00:00
|
|
|
// Adds anonymous code for testing purposes.
|
|
|
|
WasmCode* AddCodeForTesting(Handle<Code> code);
|
|
|
|
|
2019-04-02 09:23:45 +00:00
|
|
|
// Use this to setup lazy compilation for the entire module ({UseLazyStubs})
|
|
|
|
// or for individual functions ({UseLazyStub}). It will use the existing
|
|
|
|
// {WasmCode::kWasmCompileLazy} runtime stub and populate the jump table with
|
|
|
|
// trampolines to that runtime stub.
|
|
|
|
void UseLazyStubs();
|
|
|
|
void UseLazyStub(uint32_t func_index);
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2019-02-19 15:36:42 +00:00
|
|
|
// Initializes all runtime stubs by setting up entry addresses in the runtime
|
|
|
|
// stub table. It must be called exactly once per native module before adding
|
|
|
|
// other WasmCode so that runtime stub ids can be resolved during relocation.
|
2018-06-04 10:12:54 +00:00
|
|
|
void SetRuntimeStubs(Isolate* isolate);
|
|
|
|
|
2018-08-01 11:04:55 +00:00
|
|
|
// Creates a snapshot of the current state of the code table. This is useful
|
|
|
|
// to get a consistent view of the table (e.g. used by the serializer).
|
|
|
|
std::vector<WasmCode*> SnapshotCodeTable() const;
|
|
|
|
|
2018-05-07 13:37:03 +00:00
|
|
|
WasmCode* code(uint32_t index) const {
|
2018-06-28 13:23:24 +00:00
|
|
|
DCHECK_LT(index, num_functions());
|
|
|
|
DCHECK_LE(module_->num_imported_functions, index);
|
|
|
|
return code_table_[index - module_->num_imported_functions];
|
2018-05-07 13:37:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 13:23:24 +00:00
|
|
|
bool has_code(uint32_t index) const { return code(index) != nullptr; }
|
2018-05-15 17:08:44 +00:00
|
|
|
|
2019-02-13 16:11:17 +00:00
|
|
|
Address runtime_stub_entry(WasmCode::RuntimeStubId index) const {
|
2018-06-04 10:12:54 +00:00
|
|
|
DCHECK_LT(index, WasmCode::kRuntimeStubCount);
|
2019-02-13 16:11:17 +00:00
|
|
|
Address entry_address = runtime_stub_entries_[index];
|
|
|
|
DCHECK_NE(kNullAddress, entry_address);
|
|
|
|
return entry_address;
|
2018-06-04 10:12:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 14:27:20 +00:00
|
|
|
Address jump_table_start() const {
|
|
|
|
return jump_table_ ? jump_table_->instruction_start() : kNullAddress;
|
|
|
|
}
|
|
|
|
|
2018-08-07 10:33:50 +00:00
|
|
|
ptrdiff_t jump_table_offset(uint32_t func_index) const {
|
|
|
|
DCHECK_GE(func_index, num_imported_functions());
|
|
|
|
return GetCallTargetForFunction(func_index) - jump_table_start();
|
|
|
|
}
|
|
|
|
|
2018-06-19 09:47:17 +00:00
|
|
|
bool is_jump_table_slot(Address address) const {
|
|
|
|
return jump_table_->contains(address);
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:16:26 +00:00
|
|
|
// Transition this module from code relying on trap handlers (i.e. without
|
|
|
|
// explicit memory bounds checks) to code that does not require trap handlers
|
|
|
|
// (i.e. code with explicit bounds checks).
|
|
|
|
// This method must only be called if {use_trap_handler()} is true (it will be
|
|
|
|
// false afterwards). All code in this {NativeModule} needs to be re-added
|
|
|
|
// after calling this method.
|
|
|
|
void DisableTrapHandler();
|
|
|
|
|
2018-06-19 09:47:17 +00:00
|
|
|
// Returns the target to call for the given function (returns a jump table
|
|
|
|
// slot within {jump_table_}).
|
|
|
|
Address GetCallTargetForFunction(uint32_t func_index) const;
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-08-17 12:35:29 +00:00
|
|
|
// Reverse lookup from a given call target (i.e. a jump table slot as the
|
|
|
|
// above {GetCallTargetForFunction} returns) to a function index.
|
|
|
|
uint32_t GetFunctionIndexFromJumpTableSlot(Address slot_address) const;
|
|
|
|
|
2017-12-04 16:41:22 +00:00
|
|
|
bool SetExecutable(bool executable);
|
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
// For cctests, where we build both WasmModule and the runtime objects
|
|
|
|
// on the fly, and bypass the instance builder pipeline.
|
2018-06-04 12:01:49 +00:00
|
|
|
void ReserveCodeTableForTesting(uint32_t max_functions);
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-06-15 12:00:30 +00:00
|
|
|
void LogWasmCodes(Isolate* isolate);
|
|
|
|
|
2018-03-19 09:22:23 +00:00
|
|
|
CompilationState* compilation_state() { return compilation_state_.get(); }
|
|
|
|
|
2019-01-23 11:49:36 +00:00
|
|
|
// Create a {CompilationEnv} object for compilation. The caller has to ensure
|
|
|
|
// that the {WasmModule} pointer stays valid while the {CompilationEnv} is
|
|
|
|
// being used.
|
2018-10-23 11:56:12 +00:00
|
|
|
CompilationEnv CreateCompilationEnv() const;
|
2018-10-23 11:43:07 +00:00
|
|
|
|
2018-06-28 13:23:24 +00:00
|
|
|
uint32_t num_functions() const {
|
|
|
|
return module_->num_declared_functions + module_->num_imported_functions;
|
|
|
|
}
|
|
|
|
uint32_t num_imported_functions() const {
|
|
|
|
return module_->num_imported_functions;
|
|
|
|
}
|
2018-10-23 11:39:15 +00:00
|
|
|
UseTrapHandler use_trap_handler() const { return use_trap_handler_; }
|
2018-04-30 14:47:44 +00:00
|
|
|
void set_lazy_compile_frozen(bool frozen) { lazy_compile_frozen_ = frozen; }
|
|
|
|
bool lazy_compile_frozen() const { return lazy_compile_frozen_; }
|
2019-04-02 09:23:45 +00:00
|
|
|
void set_lazy_compilation(bool lazy) { lazy_compilation_ = lazy; }
|
|
|
|
bool lazy_compilation() const { return lazy_compilation_; }
|
2019-01-21 13:52:01 +00:00
|
|
|
Vector<const uint8_t> wire_bytes() const { return wire_bytes_->as_vector(); }
|
2018-06-28 16:31:31 +00:00
|
|
|
const WasmModule* module() const { return module_.get(); }
|
2019-01-23 11:49:36 +00:00
|
|
|
std::shared_ptr<const WasmModule> shared_module() const { return module_; }
|
2018-11-12 14:00:26 +00:00
|
|
|
size_t committed_code_space() const { return committed_code_space_.load(); }
|
2019-02-05 12:54:32 +00:00
|
|
|
WasmEngine* engine() const { return engine_; }
|
2018-04-30 14:47:44 +00:00
|
|
|
|
2019-01-21 13:52:01 +00:00
|
|
|
void SetWireBytes(OwnedVector<const uint8_t> wire_bytes);
|
2018-11-07 10:27:10 +00:00
|
|
|
|
2018-06-19 09:47:17 +00:00
|
|
|
WasmCode* Lookup(Address) const;
|
|
|
|
|
2018-10-10 12:22:19 +00:00
|
|
|
WasmImportWrapperCache* import_wrapper_cache() const {
|
|
|
|
return import_wrapper_cache_.get();
|
|
|
|
}
|
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
~NativeModule();
|
|
|
|
|
2018-08-08 14:54:44 +00:00
|
|
|
const WasmFeatures& enabled_features() const { return enabled_features_; }
|
|
|
|
|
2019-02-13 16:11:17 +00:00
|
|
|
const char* GetRuntimeStubName(Address runtime_stub_entry) const;
|
|
|
|
|
2019-03-11 12:08:14 +00:00
|
|
|
// Sample the current code size of this modules to the given counters.
|
2019-03-11 14:51:56 +00:00
|
|
|
enum CodeSamplingTime : int8_t { kAfterBaseline, kAfterTopTier, kSampling };
|
2019-03-11 12:08:14 +00:00
|
|
|
void SampleCodeSize(Counters*, CodeSamplingTime) const;
|
|
|
|
|
2019-04-02 09:23:45 +00:00
|
|
|
WasmCodeUpdate AddCompiledCode(WasmCompilationResult);
|
|
|
|
std::vector<WasmCodeUpdate> AddCompiledCode(Vector<WasmCompilationResult>);
|
2019-03-19 12:52:04 +00:00
|
|
|
|
2019-03-25 12:23:37 +00:00
|
|
|
// Free a set of functions of this module. Uncommits whole pages if possible.
|
|
|
|
// The given vector must be ordered by the instruction start address, and all
|
|
|
|
// {WasmCode} objects must not be used any more.
|
|
|
|
void FreeCode(Vector<WasmCode* const>);
|
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
private:
|
2018-06-13 13:28:41 +00:00
|
|
|
friend class WasmCode;
|
2017-11-20 21:34:04 +00:00
|
|
|
friend class WasmCodeManager;
|
2018-02-21 11:42:57 +00:00
|
|
|
friend class NativeModuleModificationScope;
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2019-03-12 16:10:58 +00:00
|
|
|
// Private constructor, called via {WasmCodeManager::NewNativeModule()}.
|
2019-02-05 12:54:32 +00:00
|
|
|
NativeModule(WasmEngine* engine, const WasmFeatures& enabled_features,
|
|
|
|
bool can_request_more, VirtualMemory code_space,
|
2019-02-04 13:37:05 +00:00
|
|
|
std::shared_ptr<const WasmModule> module,
|
2019-03-12 16:10:58 +00:00
|
|
|
std::shared_ptr<Counters> async_counters,
|
|
|
|
std::shared_ptr<NativeModule>* shared_this);
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2019-03-20 13:05:25 +00:00
|
|
|
std::unique_ptr<WasmCode> AddCodeWithCodeSpace(
|
|
|
|
uint32_t index, const CodeDesc& desc, uint32_t stack_slots,
|
|
|
|
uint32_t tagged_parameter_slots,
|
|
|
|
OwnedVector<trap_handler::ProtectedInstructionData>
|
|
|
|
protected_instructions,
|
|
|
|
OwnedVector<const byte> source_position_table, WasmCode::Kind kind,
|
|
|
|
WasmCode::Tier tier, Vector<uint8_t> code_space);
|
|
|
|
|
[wasm] Split adding code from publishing it
This prepares a refactoring to add and publish compilation results in
batches. For this, we need to separate the two phases, so that we can
lock the module, allocate all the code space, release the lock, copy
the code, lock the module, publish the code, and release the lock
again.
In particular, this CL does the following:
1) It removes the {AddOwnedCode} method. The functionality of creating
the {WasmCode} and memcpy'ing the instruction into that is done in
the other {Add*Code} methods. Adding to {owned_code_} is done in
{PublishCode}.
2) {PublishInterpreterEntry} is now functionally equivalent to
{PublishCode}, so it's removed.
3) After {AddCode}, the caller has to call {PublishCode}. In a
follow-up CL, this will be called in batches (first {AddCode} them
all, then {PublishCode} them all).
4) {AddCompiledCode} now assumes that the {WasmCompilationResult}
succeeded. Otherwise, the caller should directly call {SetError} on
the {CompilationState}.
5) {PublishCode} is now the chokepoint for installing code to the code
table, the owned code vector, the jump table, and setting interpreter
redirections. It replaces previous direct calls to {InstallCode} or
explicitly adding to {owned_code_}.
6) Increasing the {generated_code_size_} counter is now done in
{AllocateForCode}, which is the chokepoint for allocating space for
generated code. This way, we will only increase this counter once
once we allocate in batches.
R=titzer@chromium.org
Bug: v8:8916
Change-Id: I71e02e3a838f21797915cee3ebd373804fb12237
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1530817
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Ben Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60369}
2019-03-20 13:04:33 +00:00
|
|
|
// Add and publish anonymous code.
|
|
|
|
WasmCode* AddAndPublishAnonymousCode(Handle<Code>, WasmCode::Kind kind,
|
|
|
|
const char* name = nullptr);
|
2018-09-27 09:20:06 +00:00
|
|
|
// Allocate code space. Returns a valid buffer or fails with OOM (crash).
|
|
|
|
Vector<byte> AllocateForCode(size_t size);
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2019-02-13 16:11:17 +00:00
|
|
|
WasmCode* CreateEmptyJumpTable(uint32_t jump_table_size);
|
2018-06-19 09:47:17 +00:00
|
|
|
|
2018-08-01 11:04:55 +00:00
|
|
|
Vector<WasmCode*> code_table() const {
|
|
|
|
return {code_table_.get(), module_->num_declared_functions};
|
|
|
|
}
|
2018-06-04 13:58:37 +00:00
|
|
|
|
2018-10-15 14:07:47 +00:00
|
|
|
// Hold the {mutex_} when calling this method.
|
|
|
|
bool has_interpreter_redirection(uint32_t func_index) {
|
|
|
|
DCHECK_LT(func_index, num_functions());
|
|
|
|
DCHECK_LE(module_->num_imported_functions, func_index);
|
|
|
|
if (!interpreter_redirections_) return false;
|
|
|
|
uint32_t bitset_idx = func_index - module_->num_imported_functions;
|
|
|
|
uint8_t byte = interpreter_redirections_[bitset_idx / kBitsPerByte];
|
|
|
|
return byte & (1 << (bitset_idx % kBitsPerByte));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold the {mutex_} when calling this method.
|
|
|
|
void SetInterpreterRedirection(uint32_t func_index) {
|
|
|
|
DCHECK_LT(func_index, num_functions());
|
|
|
|
DCHECK_LE(module_->num_imported_functions, func_index);
|
|
|
|
if (!interpreter_redirections_) {
|
|
|
|
interpreter_redirections_.reset(
|
|
|
|
new uint8_t[RoundUp<kBitsPerByte>(module_->num_declared_functions) /
|
2019-03-20 09:40:09 +00:00
|
|
|
kBitsPerByte]{});
|
2018-10-15 14:07:47 +00:00
|
|
|
}
|
|
|
|
uint32_t bitset_idx = func_index - module_->num_imported_functions;
|
|
|
|
uint8_t& byte = interpreter_redirections_[bitset_idx / kBitsPerByte];
|
|
|
|
byte |= 1 << (bitset_idx % kBitsPerByte);
|
|
|
|
}
|
|
|
|
|
2018-08-08 14:54:44 +00:00
|
|
|
// Features enabled for this module. We keep a copy of the features that
|
|
|
|
// were enabled at the time of the creation of this native module,
|
|
|
|
// to be consistent across asynchronous compilations later.
|
|
|
|
const WasmFeatures enabled_features_;
|
|
|
|
|
2019-01-23 11:49:36 +00:00
|
|
|
// The decoded module, stored in a shared_ptr such that background compile
|
|
|
|
// tasks can keep this alive.
|
2018-06-28 13:23:24 +00:00
|
|
|
std::shared_ptr<const WasmModule> module_;
|
|
|
|
|
2019-01-21 13:52:01 +00:00
|
|
|
// Wire bytes, held in a shared_ptr so they can be kept alive by the
|
|
|
|
// {WireBytesStorage}, held by background compile tasks.
|
|
|
|
std::shared_ptr<OwnedVector<const uint8_t>> wire_bytes_;
|
2018-06-22 14:34:47 +00:00
|
|
|
|
2019-02-13 16:11:17 +00:00
|
|
|
// Contains entry points for runtime stub calls via {WASM_STUB_CALL}.
|
|
|
|
Address runtime_stub_entries_[WasmCode::kRuntimeStubCount] = {kNullAddress};
|
|
|
|
|
|
|
|
// Jump table used for runtime stubs (i.e. trampolines to embedded builtins).
|
|
|
|
WasmCode* runtime_stub_table_ = nullptr;
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-06-19 09:47:17 +00:00
|
|
|
// Jump table used to easily redirect wasm function calls.
|
|
|
|
WasmCode* jump_table_ = nullptr;
|
|
|
|
|
2018-07-10 12:30:26 +00:00
|
|
|
// The compilation state keeps track of compilation tasks for this module.
|
|
|
|
// Note that its destructor blocks until all tasks are finished/aborted and
|
|
|
|
// hence needs to be destructed first when this native module dies.
|
2018-10-25 14:24:50 +00:00
|
|
|
std::unique_ptr<CompilationState> compilation_state_;
|
2018-03-19 09:22:23 +00:00
|
|
|
|
2018-10-10 12:22:19 +00:00
|
|
|
// A cache of the import wrappers, keyed on the kind and signature.
|
|
|
|
std::unique_ptr<WasmImportWrapperCache> import_wrapper_cache_;
|
|
|
|
|
2018-09-21 12:00:23 +00:00
|
|
|
// This mutex protects concurrent calls to {AddCode} and friends.
|
2018-07-10 12:30:26 +00:00
|
|
|
mutable base::Mutex allocation_mutex_;
|
|
|
|
|
2018-09-21 12:00:23 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Protected by {allocation_mutex_}:
|
|
|
|
|
2019-02-27 13:14:43 +00:00
|
|
|
// Holds all allocated code objects. Mutable because it might get sorted in
|
|
|
|
// {Lookup()}.
|
|
|
|
mutable std::vector<std::unique_ptr<WasmCode>> owned_code_;
|
|
|
|
|
|
|
|
// Keep track of the portion of {owned_code_} that is sorted.
|
|
|
|
// Entries [0, owned_code_sorted_portion_) are known to be sorted.
|
|
|
|
// Mutable because it might get modified in {Lookup()}.
|
|
|
|
mutable size_t owned_code_sorted_portion_ = 0;
|
2018-09-21 12:00:23 +00:00
|
|
|
|
|
|
|
std::unique_ptr<WasmCode* []> code_table_;
|
|
|
|
|
2018-10-15 14:07:47 +00:00
|
|
|
// Null if no redirections exist, otherwise a bitset over all functions in
|
|
|
|
// this module marking those functions that have been redirected.
|
|
|
|
std::unique_ptr<uint8_t[]> interpreter_redirections_;
|
|
|
|
|
2018-05-16 12:05:49 +00:00
|
|
|
DisjointAllocationPool free_code_space_;
|
|
|
|
DisjointAllocationPool allocated_code_space_;
|
|
|
|
std::list<VirtualMemory> owned_code_space_;
|
|
|
|
|
2018-09-21 12:00:23 +00:00
|
|
|
// End of fields protected by {allocation_mutex_}.
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-01-25 10:35:37 +00:00
|
|
|
WasmEngine* const engine_;
|
2018-07-24 15:58:31 +00:00
|
|
|
std::atomic<size_t> committed_code_space_{0};
|
2019-03-11 12:08:14 +00:00
|
|
|
std::atomic<size_t> generated_code_size_{0};
|
2018-04-30 14:47:44 +00:00
|
|
|
int modification_scope_depth_ = 0;
|
2017-11-20 21:34:04 +00:00
|
|
|
bool can_request_more_memory_;
|
2018-10-23 11:39:15 +00:00
|
|
|
UseTrapHandler use_trap_handler_ = kNoTrapHandler;
|
2017-12-04 16:41:22 +00:00
|
|
|
bool is_executable_ = false;
|
2018-04-30 14:47:44 +00:00
|
|
|
bool lazy_compile_frozen_ = false;
|
2019-04-02 09:23:45 +00:00
|
|
|
bool lazy_compilation_ = false;
|
2018-03-21 13:00:04 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(NativeModule);
|
2017-11-20 21:34:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class V8_EXPORT_PRIVATE WasmCodeManager final {
|
|
|
|
public:
|
2018-09-18 13:05:45 +00:00
|
|
|
explicit WasmCodeManager(WasmMemoryTracker* memory_tracker,
|
|
|
|
size_t max_committed);
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-06-19 09:47:17 +00:00
|
|
|
NativeModule* LookupNativeModule(Address pc) const;
|
2017-11-20 21:34:04 +00:00
|
|
|
WasmCode* LookupCode(Address pc) const;
|
2018-05-16 12:05:49 +00:00
|
|
|
size_t remaining_uncommitted_code_space() const;
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-10-25 13:29:13 +00:00
|
|
|
void SetMaxCommittedMemoryForTesting(size_t limit);
|
|
|
|
|
2018-11-12 14:00:26 +00:00
|
|
|
static size_t EstimateNativeModuleCodeSize(const WasmModule* module);
|
|
|
|
static size_t EstimateNativeModuleNonCodeSize(const WasmModule* module);
|
2018-05-16 12:39:18 +00:00
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
private:
|
|
|
|
friend class NativeModule;
|
2019-01-25 10:35:37 +00:00
|
|
|
friend class WasmEngine;
|
|
|
|
|
2019-03-12 16:10:58 +00:00
|
|
|
std::shared_ptr<NativeModule> NewNativeModule(
|
2019-01-25 10:35:37 +00:00
|
|
|
WasmEngine* engine, Isolate* isolate,
|
|
|
|
const WasmFeatures& enabled_features, size_t code_size_estimate,
|
|
|
|
bool can_request_more, std::shared_ptr<const WasmModule> module);
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-09-17 08:49:40 +00:00
|
|
|
V8_WARN_UNUSED_RESULT VirtualMemory TryAllocate(size_t size,
|
|
|
|
void* hint = nullptr);
|
2017-11-20 21:34:04 +00:00
|
|
|
bool Commit(Address, size_t);
|
|
|
|
// Currently, we uncommit a whole module, so all we need is account
|
2018-05-16 12:05:49 +00:00
|
|
|
// for the freed memory size. We do that in FreeNativeModule.
|
2017-11-20 21:34:04 +00:00
|
|
|
// There's no separate Uncommit.
|
|
|
|
|
2018-05-16 12:05:49 +00:00
|
|
|
void FreeNativeModule(NativeModule*);
|
2019-01-25 10:35:37 +00:00
|
|
|
|
2018-04-13 22:28:05 +00:00
|
|
|
void AssignRanges(Address start, Address end, NativeModule*);
|
2017-11-20 21:34:04 +00:00
|
|
|
|
2018-09-18 13:05:45 +00:00
|
|
|
WasmMemoryTracker* const memory_tracker_;
|
2018-09-21 09:23:31 +00:00
|
|
|
std::atomic<size_t> remaining_uncommitted_code_space_;
|
2018-12-28 13:51:59 +00:00
|
|
|
// If the remaining uncommitted code space falls below
|
|
|
|
// {critical_uncommitted_code_space_}, then we trigger a GC before creating
|
|
|
|
// the next module. This value is initialized to 50% of the available code
|
|
|
|
// space on creation and after each GC.
|
|
|
|
std::atomic<size_t> critical_uncommitted_code_space_;
|
2018-07-24 15:58:31 +00:00
|
|
|
mutable base::Mutex native_modules_mutex_;
|
2018-09-21 09:23:31 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Protected by {native_modules_mutex_}:
|
|
|
|
|
2017-11-20 21:34:04 +00:00
|
|
|
std::map<Address, std::pair<Address, NativeModule*>> lookup_map_;
|
2018-09-21 09:23:31 +00:00
|
|
|
|
|
|
|
// End of fields protected by {native_modules_mutex_}.
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2017-12-01 16:13:36 +00:00
|
|
|
|
2018-03-21 13:00:04 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WasmCodeManager);
|
2017-11-20 21:34:04 +00:00
|
|
|
};
|
|
|
|
|
2017-12-04 16:41:22 +00:00
|
|
|
// Within the scope, the native_module is writable and not executable.
|
|
|
|
// At the scope's destruction, the native_module is executable and not writable.
|
|
|
|
// The states inside the scope and at the scope termination are irrespective of
|
|
|
|
// native_module's state when entering the scope.
|
|
|
|
// We currently mark the entire module's memory W^X:
|
|
|
|
// - for AOT, that's as efficient as it can be.
|
|
|
|
// - for Lazy, we don't have a heuristic for functions that may need patching,
|
|
|
|
// and even if we did, the resulting set of pages may be fragmented.
|
|
|
|
// Currently, we try and keep the number of syscalls low.
|
|
|
|
// - similar argument for debug time.
|
|
|
|
class NativeModuleModificationScope final {
|
|
|
|
public:
|
|
|
|
explicit NativeModuleModificationScope(NativeModule* native_module);
|
|
|
|
~NativeModuleModificationScope();
|
|
|
|
|
|
|
|
private:
|
|
|
|
NativeModule* native_module_;
|
|
|
|
};
|
|
|
|
|
2019-03-25 12:23:37 +00:00
|
|
|
// {WasmCodeRefScope}s form a perfect stack. New {WasmCode} pointers generated
|
|
|
|
// by e.g. creating new code or looking up code by its address are added to the
|
|
|
|
// top-most {WasmCodeRefScope}.
|
|
|
|
class V8_EXPORT_PRIVATE WasmCodeRefScope {
|
|
|
|
public:
|
|
|
|
WasmCodeRefScope();
|
|
|
|
~WasmCodeRefScope();
|
|
|
|
|
|
|
|
// Register a {WasmCode} reference in the current {WasmCodeRefScope}. Fails if
|
|
|
|
// there is no current scope.
|
|
|
|
static void AddRef(WasmCode*);
|
|
|
|
|
|
|
|
private:
|
|
|
|
WasmCodeRefScope* const previous_scope_;
|
|
|
|
std::unordered_set<WasmCode*> code_ptrs_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(WasmCodeRefScope);
|
|
|
|
};
|
|
|
|
|
2019-03-25 12:24:27 +00:00
|
|
|
// Similarly to a global handle, a {GlobalWasmCodeRef} stores a single
|
|
|
|
// ref-counted pointer to a {WasmCode} object.
|
|
|
|
class GlobalWasmCodeRef {
|
|
|
|
public:
|
|
|
|
explicit GlobalWasmCodeRef(WasmCode* code,
|
|
|
|
std::shared_ptr<NativeModule> native_module)
|
|
|
|
: code_(code), native_module_(std::move(native_module)) {
|
|
|
|
code_->IncRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
~GlobalWasmCodeRef() {
|
|
|
|
if (code_->DecRef()) code_->native_module()->FreeCode(VectorOf(&code_, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a pointer to the contained {WasmCode} object. This is only guaranteed
|
|
|
|
// to exist as long as this {GlobalWasmCodeRef} exists.
|
|
|
|
WasmCode* code() const { return code_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
WasmCode* const code_;
|
|
|
|
// Also keep the {NativeModule} alive.
|
|
|
|
const std::shared_ptr<NativeModule> native_module_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(GlobalWasmCodeRef);
|
|
|
|
};
|
|
|
|
|
2017-09-16 05:22:38 +00:00
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2018-02-02 12:05:19 +00:00
|
|
|
|
|
|
|
#endif // V8_WASM_WASM_CODE_MANAGER_H_
|