Commit 6c44dd5f authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Relocate code before adding it to the module

This ensures that the actual instructions are in their final form when
adding them to the NativeModule.

R=titzer@chromium.org

Change-Id: Ia20698823e5a18a3c3ef7d2370769b70addfc4e2
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1532075
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60384}
parent 2970249a
...@@ -502,8 +502,9 @@ WasmCode* NativeModule::AddAndPublishAnonymousCode(Handle<Code> code, ...@@ -502,8 +502,9 @@ WasmCode* NativeModule::AddAndPublishAnonymousCode(Handle<Code> code,
// we do not apply the on-heap reloc info to the off-heap instructions. // we do not apply the on-heap reloc info to the off-heap instructions.
const size_t relocation_size = const size_t relocation_size =
code->is_off_heap_trampoline() ? 0 : code->relocation_size(); code->is_off_heap_trampoline() ? 0 : code->relocation_size();
OwnedVector<byte> reloc_info = OwnedVector<byte>::New(relocation_size); OwnedVector<byte> reloc_info;
if (relocation_size > 0) { if (relocation_size > 0) {
reloc_info = OwnedVector<byte>::New(relocation_size);
memcpy(reloc_info.start(), code->relocation_start(), relocation_size); memcpy(reloc_info.start(), code->relocation_start(), relocation_size);
} }
Handle<ByteArray> source_pos_table(code->SourcePositionTable(), Handle<ByteArray> source_pos_table(code->SourcePositionTable(),
...@@ -532,32 +533,19 @@ WasmCode* NativeModule::AddAndPublishAnonymousCode(Handle<Code> code, ...@@ -532,32 +533,19 @@ WasmCode* NativeModule::AddAndPublishAnonymousCode(Handle<Code> code,
const size_t code_comments_offset = const size_t code_comments_offset =
static_cast<size_t>(code->code_comments_offset()); static_cast<size_t>(code->code_comments_offset());
Vector<uint8_t> code_space = AllocateForCode(instructions.size()); Vector<uint8_t> dst_code_bytes = AllocateForCode(instructions.size());
memcpy(code_space.begin(), instructions.start(), instructions.size()); memcpy(dst_code_bytes.begin(), instructions.start(), instructions.size());
std::unique_ptr<WasmCode> new_code{new WasmCode{
this, // native_module
WasmCode::kAnonymousFuncIndex, // index
code_space, // instructions
stack_slots, // stack_slots
0, // tagged_parameter_slots
safepoint_table_offset, // safepoint_table_offset
handler_table_offset, // handler_table_offset
constant_pool_offset, // constant_pool_offset
code_comments_offset, // code_comments_offset
instructions.size(), // unpadded_binary_size
OwnedVector<ProtectedInstructionData>{}, // protected_instructions
std::move(reloc_info), // reloc_info
std::move(source_pos), // source positions
kind, // kind
WasmCode::kOther}}; // tier
// Apply the relocation delta by iterating over the RelocInfo. // Apply the relocation delta by iterating over the RelocInfo.
intptr_t delta = new_code->instruction_start() - code->InstructionStart(); intptr_t delta = reinterpret_cast<Address>(dst_code_bytes.begin()) -
code->InstructionStart();
int mode_mask = RelocInfo::kApplyMask | int mode_mask = RelocInfo::kApplyMask |
RelocInfo::ModeMask(RelocInfo::WASM_STUB_CALL); RelocInfo::ModeMask(RelocInfo::WASM_STUB_CALL);
Address constant_pool_start =
reinterpret_cast<Address>(dst_code_bytes.begin()) + constant_pool_offset;
RelocIterator orig_it(*code, mode_mask); RelocIterator orig_it(*code, mode_mask);
for (RelocIterator it(new_code->instructions(), new_code->reloc_info(), for (RelocIterator it(dst_code_bytes, reloc_info.as_vector(),
new_code->constant_pool(), mode_mask); constant_pool_start, mode_mask);
!it.done(); it.next(), orig_it.next()) { !it.done(); it.next(), orig_it.next()) {
RelocInfo::Mode mode = it.rinfo()->rmode(); RelocInfo::Mode mode = it.rinfo()->rmode();
if (RelocInfo::IsWasmStubCall(mode)) { if (RelocInfo::IsWasmStubCall(mode)) {
...@@ -572,8 +560,24 @@ WasmCode* NativeModule::AddAndPublishAnonymousCode(Handle<Code> code, ...@@ -572,8 +560,24 @@ WasmCode* NativeModule::AddAndPublishAnonymousCode(Handle<Code> code,
} }
// Flush the i-cache after relocation. // Flush the i-cache after relocation.
FlushInstructionCache(new_code->instructions().start(), FlushInstructionCache(dst_code_bytes.start(), dst_code_bytes.size());
new_code->instructions().size());
std::unique_ptr<WasmCode> new_code{new WasmCode{
this, // native_module
WasmCode::kAnonymousFuncIndex, // index
dst_code_bytes, // instructions
stack_slots, // stack_slots
0, // tagged_parameter_slots
safepoint_table_offset, // safepoint_table_offset
handler_table_offset, // handler_table_offset
constant_pool_offset, // constant_pool_offset
code_comments_offset, // code_comments_offset
instructions.size(), // unpadded_binary_size
OwnedVector<ProtectedInstructionData>{}, // protected_instructions
std::move(reloc_info), // reloc_info
std::move(source_pos), // source positions
kind, // kind
WasmCode::kOther}}; // tier
new_code->MaybePrint(name); new_code->MaybePrint(name);
new_code->Validate(); new_code->Validate();
...@@ -597,7 +601,7 @@ std::unique_ptr<WasmCode> NativeModule::AddCodeWithCodeSpace( ...@@ -597,7 +601,7 @@ std::unique_ptr<WasmCode> NativeModule::AddCodeWithCodeSpace(
uint32_t tagged_parameter_slots, uint32_t tagged_parameter_slots,
OwnedVector<ProtectedInstructionData> protected_instructions, OwnedVector<ProtectedInstructionData> protected_instructions,
OwnedVector<const byte> source_position_table, WasmCode::Kind kind, OwnedVector<const byte> source_position_table, WasmCode::Kind kind,
WasmCode::Tier tier, Vector<uint8_t> code_space) { WasmCode::Tier tier, Vector<uint8_t> dst_code_bytes) {
OwnedVector<byte> reloc_info; OwnedVector<byte> reloc_info;
if (desc.reloc_size > 0) { if (desc.reloc_size > 0) {
reloc_info = OwnedVector<byte>::New(desc.reloc_size); reloc_info = OwnedVector<byte>::New(desc.reloc_size);
...@@ -618,21 +622,18 @@ std::unique_ptr<WasmCode> NativeModule::AddCodeWithCodeSpace( ...@@ -618,21 +622,18 @@ std::unique_ptr<WasmCode> NativeModule::AddCodeWithCodeSpace(
static_cast<size_t>(desc.code_comments_offset); static_cast<size_t>(desc.code_comments_offset);
const size_t instr_size = static_cast<size_t>(desc.instr_size); const size_t instr_size = static_cast<size_t>(desc.instr_size);
memcpy(code_space.begin(), desc.buffer, static_cast<size_t>(desc.instr_size)); memcpy(dst_code_bytes.begin(), desc.buffer,
static_cast<size_t>(desc.instr_size));
std::unique_ptr<WasmCode> code{new WasmCode{
this, index, code_space, stack_slots, tagged_parameter_slots,
safepoint_table_offset, handler_table_offset, constant_pool_offset,
code_comments_offset, instr_size, std::move(protected_instructions),
std::move(reloc_info), std::move(source_position_table), kind, tier}};
// Apply the relocation delta by iterating over the RelocInfo. // Apply the relocation delta by iterating over the RelocInfo.
intptr_t delta = code->instructions().start() - desc.buffer; intptr_t delta = dst_code_bytes.begin() - desc.buffer;
int mode_mask = RelocInfo::kApplyMask | int mode_mask = RelocInfo::kApplyMask |
RelocInfo::ModeMask(RelocInfo::WASM_CALL) | RelocInfo::ModeMask(RelocInfo::WASM_CALL) |
RelocInfo::ModeMask(RelocInfo::WASM_STUB_CALL); RelocInfo::ModeMask(RelocInfo::WASM_STUB_CALL);
for (RelocIterator it(code->instructions(), code->reloc_info(), Address constant_pool_start =
code->constant_pool(), mode_mask); reinterpret_cast<Address>(dst_code_bytes.begin()) + constant_pool_offset;
for (RelocIterator it(dst_code_bytes, reloc_info.as_vector(),
constant_pool_start, mode_mask);
!it.done(); it.next()) { !it.done(); it.next()) {
RelocInfo::Mode mode = it.rinfo()->rmode(); RelocInfo::Mode mode = it.rinfo()->rmode();
if (RelocInfo::IsWasmCall(mode)) { if (RelocInfo::IsWasmCall(mode)) {
...@@ -651,8 +652,13 @@ std::unique_ptr<WasmCode> NativeModule::AddCodeWithCodeSpace( ...@@ -651,8 +652,13 @@ std::unique_ptr<WasmCode> NativeModule::AddCodeWithCodeSpace(
} }
// Flush the i-cache after relocation. // Flush the i-cache after relocation.
FlushInstructionCache(code->instructions().start(), FlushInstructionCache(dst_code_bytes.start(), dst_code_bytes.size());
code->instructions().size());
std::unique_ptr<WasmCode> code{new WasmCode{
this, index, dst_code_bytes, stack_slots, tagged_parameter_slots,
safepoint_table_offset, handler_table_offset, constant_pool_offset,
code_comments_offset, instr_size, std::move(protected_instructions),
std::move(reloc_info), std::move(source_position_table), kind, tier}};
code->MaybePrint(); code->MaybePrint();
code->Validate(); code->Validate();
...@@ -705,11 +711,11 @@ WasmCode* NativeModule::AddDeserializedCode( ...@@ -705,11 +711,11 @@ WasmCode* NativeModule::AddDeserializedCode(
OwnedVector<const byte> reloc_info, OwnedVector<const byte> reloc_info,
OwnedVector<const byte> source_position_table, WasmCode::Kind kind, OwnedVector<const byte> source_position_table, WasmCode::Kind kind,
WasmCode::Tier tier) { WasmCode::Tier tier) {
Vector<uint8_t> code_space = AllocateForCode(instructions.size()); Vector<uint8_t> dst_code_bytes = AllocateForCode(instructions.size());
memcpy(code_space.begin(), instructions.start(), instructions.size()); memcpy(dst_code_bytes.begin(), instructions.start(), instructions.size());
std::unique_ptr<WasmCode> code{new WasmCode{ std::unique_ptr<WasmCode> code{new WasmCode{
this, index, code_space, stack_slots, tagged_parameter_slots, this, index, dst_code_bytes, stack_slots, tagged_parameter_slots,
safepoint_table_offset, handler_table_offset, constant_pool_offset, safepoint_table_offset, handler_table_offset, constant_pool_offset,
code_comments_offset, unpadded_binary_size, code_comments_offset, unpadded_binary_size,
std::move(protected_instructions), std::move(reloc_info), std::move(protected_instructions), std::move(reloc_info),
......
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