Commit ded61e98 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Store reloc info and source positions in OwnedVectors

Replace two more pairs of {std::unique_ptr} and {size_t} by
{OwnedVector}.

R=mstarzinger@chromium.org

Change-Id: Ifdf03abf9759fbbb4adde76a494073625723a03d
Reviewed-on: https://chromium-review.googlesource.com/1116785
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54057}
parent fdf69d53
......@@ -189,6 +189,14 @@ class OwnedVector {
: data_(std::move(data)), length_(length) {
DCHECK_IMPLIES(length_ > 0, data_ != nullptr);
}
// Implicit conversion from {OwnedVector<U>} to {OwnedVector<T>}, instantiable
// if {std::unique_ptr<U>} can be converted to {std::unique_ptr<T>}.
// Can be used to convert {OwnedVector<T>} to {OwnedVector<const T>}.
template <typename U,
typename = typename std::enable_if<std::is_convertible<
std::unique_ptr<U>, std::unique_ptr<T>>::value>::type>
OwnedVector(OwnedVector<U>&& other)
: data_(other.ReleaseData()), length_(other.size()) {}
// Returns the length of the vector as a size_t.
constexpr size_t size() const { return length_; }
......
......@@ -262,7 +262,7 @@ void WasmCode::Disassemble(const char* name, std::ostream& os,
os << "\n";
}
os << "RelocInfo (size = " << reloc_size_ << ")\n";
os << "RelocInfo (size = " << reloc_info_.size() << ")\n";
for (RelocIterator it(instructions(), reloc_info(), constant_pool());
!it.done(); it.next()) {
it.rinfo()->Print(nullptr, os);
......@@ -359,12 +359,10 @@ void NativeModule::LogWasmCodes(Isolate* isolate) {
}
WasmCode* NativeModule::AddOwnedCode(
Vector<const byte> orig_instructions,
std::unique_ptr<const byte[]> reloc_info, size_t reloc_size,
std::unique_ptr<const byte[]> source_pos, size_t source_pos_size,
Maybe<uint32_t> index, WasmCode::Kind kind, size_t constant_pool_offset,
uint32_t stack_slots, size_t safepoint_table_offset,
size_t handler_table_offset,
Vector<const byte> orig_instructions, OwnedVector<const byte> reloc_info,
OwnedVector<const byte> source_pos, Maybe<uint32_t> index,
WasmCode::Kind kind, size_t constant_pool_offset, uint32_t stack_slots,
size_t safepoint_table_offset, size_t handler_table_offset,
OwnedVector<trap_handler::ProtectedInstructionData> protected_instructions,
WasmCode::Tier tier, WasmCode::FlushICache flush_icache) {
// both allocation and insertion in owned_code_ happen in the same critical
......@@ -379,10 +377,9 @@ WasmCode* NativeModule::AddOwnedCode(
orig_instructions.size());
std::unique_ptr<WasmCode> code(new WasmCode(
{reinterpret_cast<byte*>(executable_buffer), orig_instructions.size()},
std::move(reloc_info), reloc_size, std::move(source_pos), source_pos_size,
this, index, kind, constant_pool_offset, stack_slots,
safepoint_table_offset, handler_table_offset,
std::move(protected_instructions), tier));
std::move(reloc_info), std::move(source_pos), this, index, kind,
constant_pool_offset, stack_slots, safepoint_table_offset,
handler_table_offset, std::move(protected_instructions), tier));
WasmCode* ret = code.get();
// TODO(mtrofin): We allocate in increasing address order, and
......@@ -452,18 +449,14 @@ void NativeModule::SetRuntimeStubs(Isolate* isolate) {
WasmCode* NativeModule::AddAnonymousCode(Handle<Code> code,
WasmCode::Kind kind) {
std::unique_ptr<byte[]> reloc_info;
if (code->relocation_size() > 0) {
reloc_info.reset(new byte[code->relocation_size()]);
memcpy(reloc_info.get(), code->relocation_start(), code->relocation_size());
}
std::unique_ptr<byte[]> source_pos;
OwnedVector<byte> reloc_info =
OwnedVector<byte>::New(code->relocation_size());
memcpy(reloc_info.start(), code->relocation_start(), code->relocation_size());
Handle<ByteArray> source_pos_table(code->SourcePositionTable(),
code->GetIsolate());
if (source_pos_table->length() > 0) {
source_pos.reset(new byte[source_pos_table->length()]);
source_pos_table->copy_out(0, source_pos.get(), source_pos_table->length());
}
OwnedVector<byte> source_pos =
OwnedVector<byte>::New(source_pos_table->length());
source_pos_table->copy_out(0, source_pos.start(), source_pos_table->length());
Vector<const byte> orig_instructions(
reinterpret_cast<byte*>(code->InstructionStart()),
static_cast<size_t>(code->InstructionSize()));
......@@ -471,11 +464,9 @@ WasmCode* NativeModule::AddAnonymousCode(Handle<Code> code,
int safepoint_table_offset =
code->has_safepoint_info() ? code->safepoint_table_offset() : 0;
WasmCode* ret =
AddOwnedCode(orig_instructions, // instructions
std::move(reloc_info), // reloc_info
static_cast<size_t>(code->relocation_size()), // reloc_size
std::move(source_pos), // source positions
static_cast<size_t>(source_pos_table->length()),
AddOwnedCode(orig_instructions, // instructions
std::move(reloc_info), // reloc_info
std::move(source_pos), // source positions
Nothing<uint32_t>(), // index
kind, // kind
code->constant_pool_offset(), // constant_pool_offset
......@@ -521,16 +512,12 @@ WasmCode* NativeModule::AddCode(
size_t safepoint_table_offset, size_t handler_table_offset,
OwnedVector<trap_handler::ProtectedInstructionData> protected_instructions,
OwnedVector<byte> source_pos_table, WasmCode::Tier tier) {
std::unique_ptr<byte[]> reloc_info;
if (desc.reloc_size) {
reloc_info.reset(new byte[desc.reloc_size]);
memcpy(reloc_info.get(), desc.buffer + desc.buffer_size - desc.reloc_size,
desc.reloc_size);
}
OwnedVector<byte> reloc_info = OwnedVector<byte>::New(desc.reloc_size);
memcpy(reloc_info.start(), desc.buffer + desc.buffer_size - desc.reloc_size,
desc.reloc_size);
WasmCode* ret = AddOwnedCode(
{desc.buffer, static_cast<size_t>(desc.instr_size)},
std::move(reloc_info), static_cast<size_t>(desc.reloc_size),
source_pos_table.ReleaseData(), source_pos_table.size(), Just(index),
std::move(reloc_info), std::move(source_pos_table), Just(index),
WasmCode::kFunction, desc.instr_size - desc.constant_pool_size,
frame_slots, safepoint_table_offset, handler_table_offset,
std::move(protected_instructions), tier, WasmCode::kNoFlushICache);
......@@ -578,17 +565,14 @@ WasmCode* NativeModule::AddCode(
WasmCode* NativeModule::CreateEmptyJumpTable(uint32_t num_wasm_functions) {
// Only call this if we really need a jump table.
DCHECK_LT(0, num_wasm_functions);
size_t jump_table_size =
num_wasm_functions * JumpTableAssembler::kJumpTableSlotSize;
std::unique_ptr<byte[]> instructions(new byte[jump_table_size]);
memset(instructions.get(), 0, jump_table_size);
return AddOwnedCode({instructions.get(), jump_table_size}, // instructions
nullptr, // reloc_info
0, // reloc_size
nullptr, // source_pos
0, // source_pos_size
Nothing<uint32_t>(), // index
WasmCode::kJumpTable, // kind
OwnedVector<byte> instructions = OwnedVector<byte>::New(
num_wasm_functions * JumpTableAssembler::kJumpTableSlotSize);
memset(instructions.start(), 0, instructions.size());
return AddOwnedCode(instructions.as_vector(), // instructions
{}, // reloc_info
{}, // source_pos
Nothing<uint32_t>(), // index
WasmCode::kJumpTable, // kind
0, // constant_pool_offset
0, // stack_slots
0, // safepoint_table_offset
......
......@@ -117,11 +117,9 @@ class V8_EXPORT_PRIVATE WasmCode final {
Address instruction_start() const {
return reinterpret_cast<Address>(instructions_.start());
}
Vector<const byte> reloc_info() const {
return {reloc_info_.get(), reloc_size_};
}
Vector<const byte> reloc_info() const { return reloc_info_.as_vector(); }
Vector<const byte> source_positions() const {
return {source_position_table_.get(), source_position_size_};
return source_position_table_.as_vector();
}
uint32_t index() const { return index_.ToChecked(); }
......@@ -166,9 +164,8 @@ class V8_EXPORT_PRIVATE WasmCode final {
private:
friend class NativeModule;
WasmCode(Vector<byte> instructions, std::unique_ptr<const byte[]> reloc_info,
size_t reloc_size, std::unique_ptr<const byte[]> source_pos,
size_t source_pos_size, NativeModule* native_module,
WasmCode(Vector<byte> instructions, OwnedVector<const byte> reloc_info,
OwnedVector<const byte> source_pos, NativeModule* native_module,
Maybe<uint32_t> index, Kind kind, size_t constant_pool_offset,
uint32_t stack_slots, size_t safepoint_table_offset,
size_t handler_table_offset,
......@@ -177,9 +174,7 @@ class V8_EXPORT_PRIVATE WasmCode final {
Tier tier)
: instructions_(instructions),
reloc_info_(std::move(reloc_info)),
reloc_size_(reloc_size),
source_position_table_(std::move(source_pos)),
source_position_size_(source_pos_size),
native_module_(native_module),
index_(index),
kind_(kind),
......@@ -206,10 +201,8 @@ class V8_EXPORT_PRIVATE WasmCode final {
void RegisterTrapHandlerData();
Vector<byte> instructions_;
std::unique_ptr<const byte[]> reloc_info_;
size_t reloc_size_ = 0;
std::unique_ptr<const byte[]> source_position_table_;
size_t source_position_size_ = 0;
OwnedVector<const byte> reloc_info_;
OwnedVector<const byte> source_position_table_;
NativeModule* native_module_ = nullptr;
Maybe<uint32_t> index_;
Kind kind_;
......@@ -351,12 +344,11 @@ class V8_EXPORT_PRIVATE NativeModule final {
// code is obtained (CodeDesc vs, as a point in time, Code*), the kind,
// whether it has an index or is anonymous, etc.
WasmCode* AddOwnedCode(Vector<const byte> orig_instructions,
std::unique_ptr<const byte[]> reloc_info,
size_t reloc_size,
std::unique_ptr<const byte[]> source_pos,
size_t source_pos_size, Maybe<uint32_t> index,
WasmCode::Kind kind, size_t constant_pool_offset,
uint32_t stack_slots, size_t safepoint_table_offset,
OwnedVector<const byte> reloc_info,
OwnedVector<const byte> source_pos,
Maybe<uint32_t> index, WasmCode::Kind kind,
size_t constant_pool_offset, uint32_t stack_slots,
size_t safepoint_table_offset,
size_t handler_table_offset,
OwnedVector<trap_handler::ProtectedInstructionData>,
WasmCode::Tier, WasmCode::FlushICache);
......
......@@ -445,26 +445,19 @@ bool NativeModuleDeserializer::ReadCode(uint32_t fn_index, Reader* reader) {
Vector<const byte> code_buffer = {reader->current_location(), code_size};
reader->Skip(code_size);
std::unique_ptr<byte[]> reloc_info;
if (reloc_size > 0) {
reloc_info.reset(new byte[reloc_size]);
reader->ReadVector({reloc_info.get(), reloc_size});
}
std::unique_ptr<byte[]> source_pos;
if (source_position_size > 0) {
source_pos.reset(new byte[source_position_size]);
reader->ReadVector({source_pos.get(), source_position_size});
}
OwnedVector<byte> reloc_info = OwnedVector<byte>::New(reloc_size);
reader->ReadVector(reloc_info.as_vector());
OwnedVector<byte> source_pos = OwnedVector<byte>::New(source_position_size);
reader->ReadVector(source_pos.as_vector());
auto protected_instructions =
OwnedVector<trap_handler::ProtectedInstructionData>::New(
protected_instructions_size);
reader->ReadVector(Vector<byte>::cast(protected_instructions.as_vector()));
WasmCode* ret = native_module_->AddOwnedCode(
code_buffer, std::move(reloc_info), reloc_size, std::move(source_pos),
source_position_size, Just(fn_index), WasmCode::kFunction,
constant_pool_offset, stack_slot_count, safepoint_table_offset,
handler_table_offset, std::move(protected_instructions), tier,
WasmCode::kNoFlushICache);
code_buffer, std::move(reloc_info), std::move(source_pos), Just(fn_index),
WasmCode::kFunction, constant_pool_offset, stack_slot_count,
safepoint_table_offset, handler_table_offset,
std::move(protected_instructions), tier, WasmCode::kNoFlushICache);
native_module_->set_code(fn_index, ret);
native_module_->PatchJumpTable(fn_index, ret->instruction_start(),
WasmCode::kFlushICache);
......
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