Introduce CodeReference

Add a struct CodeReference that can be stack allocated to pass a
reference to either an on-heap code object or off-heap WasmCode object
in a gc safe manner. The struct also provides a common interface such
that code can be written independently of the kind of code object it
references.

Change-Id: I5a6f74462e6e141d167c7fd9bac8c21941fd83b1
Reviewed-on: https://chromium-review.googlesource.com/977905
Commit-Queue: Stephan Herhut <herhut@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52580}
This commit is contained in:
Stephan Herhut 2018-04-12 16:47:57 +02:00 committed by Commit Bot
parent 7176708dc5
commit 4d7ad46db4
8 changed files with 175 additions and 32 deletions

View File

@ -1438,6 +1438,8 @@ v8_source_set("v8_base") {
"src/code-events.h",
"src/code-factory.cc",
"src/code-factory.h",
"src/code-reference.cc",
"src/code-reference.h",
"src/code-stub-assembler.cc",
"src/code-stub-assembler.h",
"src/code-stubs-utils.h",

View File

@ -96,7 +96,7 @@ void AssemblerBase::FlushICache(void* start, size_t size) {
void AssemblerBase::Print(Isolate* isolate) {
OFStream os(stdout);
v8::internal::Disassembler::Decode(isolate, &os, buffer_, pc_, nullptr);
v8::internal::Disassembler::Decode(isolate, &os, buffer_, pc_);
}
// -----------------------------------------------------------------------------
@ -462,6 +462,17 @@ RelocIterator::RelocIterator(Code* code, int mode_mask)
next();
}
RelocIterator::RelocIterator(const CodeReference code_reference, int mode_mask)
: mode_mask_(mode_mask) {
rinfo_.pc_ = code_reference.instruction_start();
rinfo_.data_ = 0;
rinfo_.constant_pool_ = code_reference.constant_pool();
pos_ = code_reference.relocation_start() + code_reference.relocation_size();
end_ = code_reference.relocation_start();
if (mode_mask_ == 0) pos_ = end_;
next();
}
RelocIterator::RelocIterator(const CodeDesc& desc, int mode_mask)
: mode_mask_(mode_mask) {
rinfo_.pc_ = desc.buffer;

View File

@ -40,6 +40,7 @@
#include <map>
#include "src/allocation.h"
#include "src/code-reference.h"
#include "src/contexts.h"
#include "src/deoptimize-reason.h"
#include "src/double.h"
@ -683,6 +684,8 @@ class RelocIterator: public Malloced {
// iteration iff bit k of mode_mask is set.
explicit RelocIterator(Code* code, int mode_mask = -1);
explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
explicit RelocIterator(const CodeReference code_reference,
int mode_mask = -1);
explicit RelocIterator(Vector<byte> instructions,
Vector<const byte> reloc_info, Address const_pool,
int mode_mask = -1);

45
src/code-reference.cc Normal file
View File

@ -0,0 +1,45 @@
// Copyright 2018 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 "src/code-reference.h"
#include "src/handles-inl.h"
#include "src/objects-inl.h"
#include "src/wasm/wasm-code-manager.h"
namespace v8 {
namespace internal {
Address CodeReference::constant_pool() const {
return kind == JS ? code.js->constant_pool() : code.wasm->constant_pool();
}
Address CodeReference::instruction_start() const {
return kind == JS ? code.js->InstructionStart()
: code.wasm->instructions().start();
}
Address CodeReference::instruction_end() const {
return kind == JS ? code.js->InstructionEnd()
: code.wasm->instructions().start() +
code.wasm->instructions().size();
}
int CodeReference::instruction_size() const {
return kind == JS ? code.js->InstructionSize()
: static_cast<int>(code.wasm->instructions().size());
}
const byte* CodeReference::relocation_start() const {
return kind == JS ? code.js->relocation_start()
: code.wasm->reloc_info().start();
}
int CodeReference::relocation_size() const {
return kind == JS ? code.js->relocation_size()
: static_cast<int>(code.wasm->reloc_info().size());
}
} // namespace internal
} // namespace v8

52
src/code-reference.h Normal file
View File

@ -0,0 +1,52 @@
// Copyright 2018 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.
#ifndef V8_CODE_REFERENCE_H_
#define V8_CODE_REFERENCE_H_
#include "src/handles.h"
#include "src/objects.h"
namespace v8 {
namespace internal {
class Code;
namespace wasm {
class WasmCode;
}
struct CodeReference {
explicit CodeReference(const wasm::WasmCode* wasm_code = nullptr)
: kind(WASM), code(wasm_code) {}
explicit CodeReference(Handle<Code> js_code) : kind(JS), code(js_code) {}
Address constant_pool() const;
Address instruction_start() const;
Address instruction_end() const;
int instruction_size() const;
const byte* relocation_start() const;
int relocation_size() const;
bool is_null() const {
return kind == JS ? !code.js.is_null() : code.wasm != nullptr;
}
private:
enum { JS, WASM } kind;
union CodeUnion {
explicit CodeUnion(Handle<Code> js_code) : js(js_code) {}
explicit CodeUnion(const wasm::WasmCode* wasm_code) : wasm(wasm_code) {}
CodeUnion() : wasm(nullptr) {}
const wasm::WasmCode* wasm;
Handle<Code> js;
} code;
DISALLOW_NEW_AND_DELETE()
};
} // namespace internal
} // namespace v8
#endif // V8_CODE_REFERENCE_H_

View File

@ -8,6 +8,7 @@
#include <vector>
#include "src/assembler-inl.h"
#include "src/code-reference.h"
#include "src/code-stubs.h"
#include "src/debug/debug.h"
#include "src/deoptimizer.h"
@ -27,39 +28,45 @@ namespace internal {
class V8NameConverter: public disasm::NameConverter {
public:
explicit V8NameConverter(Code* code) : code_(code) {}
explicit V8NameConverter(Code* code)
: isolate_(code->GetIsolate()), code_(CodeReference(handle(code))) {}
V8NameConverter(Isolate* isolate, const wasm::WasmCode* code)
: isolate_(isolate), code_(CodeReference(code)) {}
explicit V8NameConverter(Isolate* isolate)
: isolate_(isolate), code_(CodeReference()) {}
virtual const char* NameOfAddress(byte* pc) const;
virtual const char* NameInCode(byte* addr) const;
Code* code() const { return code_; }
const CodeReference& code() const { return code_; }
private:
Code* code_;
Isolate* isolate_;
CodeReference code_;
EmbeddedVector<char, 128> v8_buffer_;
};
const char* V8NameConverter::NameOfAddress(byte* pc) const {
if (code_ != nullptr) {
Isolate* isolate = code_->GetIsolate();
const char* name = isolate->builtins()->Lookup(pc);
if (isolate_ != nullptr) {
const char* name = isolate_->builtins()->Lookup(pc);
if (name != nullptr) {
SNPrintF(v8_buffer_, "%p (%s)", static_cast<void*>(pc), name);
return v8_buffer_.start();
}
int offs = static_cast<int>(pc - code_->raw_instruction_start());
int offs = static_cast<int>(pc - code_.instruction_start());
// print as code offset, if it seems reasonable
if (0 <= offs && offs < code_->raw_instruction_size()) {
if (0 <= offs && offs < code_.instruction_size()) {
SNPrintF(v8_buffer_, "%p <+0x%x>", static_cast<void*>(pc), offs);
return v8_buffer_.start();
}
wasm::WasmCode* wasm_code =
isolate->wasm_engine()->code_manager()->LookupCode(pc);
isolate_->wasm_engine()->code_manager()->LookupCode(pc);
if (wasm_code != nullptr) {
SNPrintF(v8_buffer_, "%p (%s)", static_cast<void*>(pc),
GetWasmCodeKindAsString(wasm_code->kind()));
wasm::GetWasmCodeKindAsString(wasm_code->kind()));
return v8_buffer_.start();
}
}
@ -71,7 +78,7 @@ const char* V8NameConverter::NameOfAddress(byte* pc) const {
const char* V8NameConverter::NameInCode(byte* addr) const {
// The V8NameConverter is used for well known code, so we can "safely"
// dereference pointers in generated code.
return (code_ != nullptr) ? reinterpret_cast<const char*>(addr) : "";
return code_.is_null() ? "" : reinterpret_cast<const char*>(addr);
}
@ -124,21 +131,30 @@ static void PrintRelocInfo(StringBuilder* out, Isolate* isolate,
out->AddFormatted(" ;; external reference (%s)", reference_name);
} else if (RelocInfo::IsCodeTarget(rmode)) {
out->AddFormatted(" ;; code:");
Code* code = Code::GetCodeFromTargetAddress(relocinfo->target_address());
Code::Kind kind = code->kind();
if (kind == Code::STUB) {
// Get the STUB key and extract major and minor key.
uint32_t key = code->stub_key();
uint32_t minor_key = CodeStub::MinorKeyFromKey(key);
CodeStub::Major major_key = CodeStub::GetMajorKey(code);
DCHECK(major_key == CodeStub::MajorKeyFromKey(key));
out->AddFormatted(" %s, %s, ", Code::Kind2String(kind),
CodeStub::MajorName(major_key));
out->AddFormatted("minor: %d", minor_key);
} else if (code->is_builtin()) {
out->AddFormatted(" Builtin::%s", Builtins::name(code->builtin_index()));
wasm::WasmCode* wasmCode =
isolate->wasm_engine()->code_manager()->LookupCode(
relocinfo->target_address());
if (wasmCode) {
out->AddFormatted(" wasm(%s)",
wasm::GetWasmCodeKindAsString(wasmCode->kind()));
} else {
out->AddFormatted(" %s", Code::Kind2String(kind));
Code* code = Code::GetCodeFromTargetAddress(relocinfo->target_address());
Code::Kind kind = code->kind();
if (kind == Code::STUB) {
// Get the STUB key and extract major and minor key.
uint32_t key = code->stub_key();
uint32_t minor_key = CodeStub::MinorKeyFromKey(key);
CodeStub::Major major_key = CodeStub::GetMajorKey(code);
DCHECK(major_key == CodeStub::MajorKeyFromKey(key));
out->AddFormatted(" %s, %s, ", Code::Kind2String(kind),
CodeStub::MajorName(major_key));
out->AddFormatted("minor: %d", minor_key);
} else if (code->is_builtin()) {
out->AddFormatted(" Builtin::%s",
Builtins::name(code->builtin_index()));
} else {
out->AddFormatted(" %s", Code::Kind2String(kind));
}
}
} else if (RelocInfo::IsRuntimeEntry(rmode) &&
isolate->deoptimizer_data() != nullptr) {
@ -179,7 +195,7 @@ static int DecodeIt(Isolate* isolate, std::ostream* os,
byte* pc = begin;
disasm::Disassembler d(converter);
RelocIterator* it = nullptr;
if (converter.code() != nullptr) {
if (!converter.code().is_null()) {
it = new RelocIterator(converter.code());
} else {
// No relocation information when printing code stubs.
@ -258,9 +274,10 @@ static int DecodeIt(Isolate* isolate, std::ostream* os,
// Print all the reloc info for this instruction which are not comments.
for (size_t i = 0; i < pcs.size(); i++) {
// Put together the reloc info
Code* host = converter.code();
RelocInfo relocinfo(pcs[i], rmodes[i], datas[i], host);
relocinfo.set_constant_pool(host ? host->constant_pool() : nullptr);
const CodeReference& host = converter.code();
RelocInfo relocinfo(pcs[i], rmodes[i], datas[i], nullptr);
relocinfo.set_constant_pool(host.is_null() ? nullptr
: host.constant_pool());
bool first_reloc_info = (i == 0);
PrintRelocInfo(&out, isolate, ref_encoder, os, &relocinfo,
@ -270,7 +287,7 @@ static int DecodeIt(Isolate* isolate, std::ostream* os,
// If this is a constant pool load and we haven't found any RelocInfo
// already, check if we can find some RelocInfo for the target address in
// the constant pool.
if (pcs.empty() && converter.code() != nullptr) {
if (pcs.empty() && !converter.code().is_null()) {
RelocInfo dummy_rinfo(prev_pc, RelocInfo::NONE, 0, nullptr);
if (dummy_rinfo.IsInConstantPool()) {
byte* constant_pool_entry_address =
@ -316,6 +333,13 @@ int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
return DecodeIt(isolate, os, v8NameConverter, begin, end, current_pc);
}
int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
byte* end, const wasm::WasmCode* code,
void* current_pc) {
V8NameConverter v8NameConverter(isolate, code);
return DecodeIt(isolate, os, v8NameConverter, begin, end, current_pc);
}
#else // ENABLE_DISASSEMBLER
int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,

View File

@ -10,6 +10,10 @@
namespace v8 {
namespace internal {
namespace wasm {
class WasmCode;
}
class Disassembler : public AllStatic {
public:
// Decode instructions in the the interval [begin, end) and print the
@ -18,6 +22,8 @@ class Disassembler : public AllStatic {
// the code object is used for name resolution and may be null.
static int Decode(Isolate* isolate, std::ostream* os, byte* begin, byte* end,
Code* code = nullptr, void* current_pc = nullptr);
static int Decode(Isolate* isolate, std::ostream* os, byte* begin, byte* end,
const wasm::WasmCode* code, void* current_pc = nullptr);
};
} // namespace internal

View File

@ -256,7 +256,7 @@ void WasmCode::Disassemble(const char* name, Isolate* isolate,
// TODO(mtrofin): rework the dependency on isolate and code in
// Disassembler::Decode.
Disassembler::Decode(isolate, &os, instructions().start(),
instructions().start() + instruction_size, nullptr);
instructions().start() + instruction_size, this);
os << "\n";
if (!source_positions().is_empty()) {