v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
disassembler.cc
Go to the documentation of this file.
1// Copyright 2011 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <algorithm>
8#include <iomanip>
9#include <memory>
10#include <sstream>
11#include <unordered_map>
12#include <vector>
13
14#include "src/base/memory.h"
15#include "src/base/strings.h"
16#include "src/base/vector.h"
22#include "src/debug/debug.h"
26#include "src/ic/ic.h"
31
32#ifdef V8_TARGET_ARCH_X64
34#endif // V8_TARGET_ARCH_X64
35
36#if V8_ENABLE_WEBASSEMBLY
39#endif // V8_ENABLE_WEBASSEMBLY
40
41namespace v8 {
42namespace internal {
43
44#ifdef ENABLE_DISASSEMBLER
45
46class V8NameConverter : public disasm::NameConverter {
47 public:
48 explicit V8NameConverter(Isolate* isolate, CodeReference code = {})
49 : isolate_(isolate), code_(code) {}
50 const char* NameOfAddress(uint8_t* pc) const override;
51 const char* NameInCode(uint8_t* addr) const override;
52 const char* RootRelativeName(int offset) const override;
53
54 const CodeReference& code() const { return code_; }
55
56 private:
57 void InitExternalRefsCache() const;
58
59 Isolate* isolate_;
60 CodeReference code_;
61
62 base::EmbeddedVector<char, 128> v8_buffer_;
63
64 // Map from root-register relative offset of the external reference value to
65 // the external reference name (stored in the external reference table).
66 // This cache is used to recognize [root_reg + offs] patterns as direct
67 // access to certain external reference's value.
68 mutable std::unordered_map<int, const char*> directly_accessed_external_refs_;
69};
70
71void V8NameConverter::InitExternalRefsCache() const {
72 ExternalReferenceTable* external_reference_table =
74 if (!external_reference_table->is_initialized()) return;
75
76 base::AddressRegion addressable_region =
78 Address isolate_root = isolate_->isolate_root();
79
80 for (uint32_t i = 0; i < ExternalReferenceTable::kSize; i++) {
81 Address address = external_reference_table->address(i);
82 if (addressable_region.contains(address)) {
83 int offset = static_cast<int>(address - isolate_root);
84 const char* name = external_reference_table->name(i);
85 directly_accessed_external_refs_.insert({offset, name});
86 }
87 }
88}
89
90const char* V8NameConverter::NameOfAddress(uint8_t* pc) const {
91 if (!code_.is_null()) {
92 const char* name =
93 isolate_ ? isolate_->builtins()->Lookup(reinterpret_cast<Address>(pc))
94 : nullptr;
95
96 if (name != nullptr) {
97 SNPrintF(v8_buffer_, "%p (%s)", static_cast<void*>(pc), name);
98 return v8_buffer_.begin();
99 }
100
101 int offs = static_cast<int>(reinterpret_cast<Address>(pc) -
102 code_.instruction_start());
103 // print as code offset, if it seems reasonable
104 if (0 <= offs && offs < code_.instruction_size()) {
105 SNPrintF(v8_buffer_, "%p <+0x%x>", static_cast<void*>(pc), offs);
106 return v8_buffer_.begin();
107 }
108
109#if V8_ENABLE_WEBASSEMBLY
110 if (auto* wasm_code = wasm::GetWasmCodeManager()->LookupCode(
111 isolate_, reinterpret_cast<Address>(pc))) {
112 SNPrintF(v8_buffer_, "%p (%s)", static_cast<void*>(pc),
113 wasm::GetWasmCodeKindAsString(wasm_code->kind()));
114 return v8_buffer_.begin();
115 }
116#endif // V8_ENABLE_WEBASSEMBLY
117 }
118
120}
121
122const char* V8NameConverter::NameInCode(uint8_t* addr) const {
123 // The V8NameConverter is used for well known code, so we can "safely"
124 // dereference pointers in generated code.
125 return code_.is_null() ? "" : reinterpret_cast<const char*>(addr);
126}
127
128const char* V8NameConverter::RootRelativeName(int offset) const {
129 if (isolate_ == nullptr) return nullptr;
130
131 const int kRootsTableStart = IsolateData::roots_table_offset();
132 const unsigned kRootsTableSize = sizeof(RootsTable);
133 const int kExtRefsTableStart = IsolateData::external_reference_table_offset();
134 const unsigned kExtRefsTableSize = ExternalReferenceTable::kSizeInBytes;
135 const int kBuiltinTier0TableStart = IsolateData::builtin_tier0_table_offset();
136 const unsigned kBuiltinTier0TableSize =
138 const int kBuiltinTableStart = IsolateData::builtin_table_offset();
139 const unsigned kBuiltinTableSize =
141
142 if (static_cast<unsigned>(offset - kRootsTableStart) < kRootsTableSize) {
143 uint32_t offset_in_roots_table = offset - kRootsTableStart;
144
145 // Fail safe in the unlikely case of an arbitrary root-relative offset.
146 if (offset_in_roots_table % kSystemPointerSize != 0) return nullptr;
147
148 RootIndex root_index =
149 static_cast<RootIndex>(offset_in_roots_table / kSystemPointerSize);
150
151 SNPrintF(v8_buffer_, "root (%s)", RootsTable::name(root_index));
152 return v8_buffer_.begin();
153 } else if (static_cast<unsigned>(offset - kExtRefsTableStart) <
154 kExtRefsTableSize) {
155 uint32_t offset_in_extref_table = offset - kExtRefsTableStart;
156
157 // Fail safe in the unlikely case of an arbitrary root-relative offset.
158 if (offset_in_extref_table % ExternalReferenceTable::kEntrySize != 0) {
159 return nullptr;
160 }
161
162 // Likewise if the external reference table is uninitialized.
164 return nullptr;
165 }
166
167 SNPrintF(v8_buffer_, "external reference (%s)",
169 offset_in_extref_table));
170 return v8_buffer_.begin();
171 } else if (static_cast<unsigned>(offset - kBuiltinTier0TableStart) <
172 kBuiltinTier0TableSize) {
173 uint32_t offset_in_builtins_table = (offset - kBuiltinTier0TableStart);
174
175 Builtin builtin =
176 Builtins::FromInt(offset_in_builtins_table / kSystemPointerSize);
177 const char* name = Builtins::name(builtin);
178 SNPrintF(v8_buffer_, "builtin (%s)", name);
179 return v8_buffer_.begin();
180 } else if (static_cast<unsigned>(offset - kBuiltinTableStart) <
181 kBuiltinTableSize) {
182 uint32_t offset_in_builtins_table = (offset - kBuiltinTableStart);
183
184 Builtin builtin =
185 Builtins::FromInt(offset_in_builtins_table / kSystemPointerSize);
186 const char* name = Builtins::name(builtin);
187 SNPrintF(v8_buffer_, "builtin (%s)", name);
188 return v8_buffer_.begin();
189 } else {
190 // It must be a direct access to one of the external values.
191 if (directly_accessed_external_refs_.empty()) {
192 InitExternalRefsCache();
193 }
194
195 auto iter = directly_accessed_external_refs_.find(offset);
196 if (iter != directly_accessed_external_refs_.end()) {
197 SNPrintF(v8_buffer_, "external value (%s)", iter->second);
198 return v8_buffer_.begin();
199 }
200 return nullptr;
201 }
202}
203
204// Output the contents of the string stream and empty it.
205static void DumpBuffer(std::ostream& os, std::ostringstream& out) {
206 os << out.str() << std::endl;
207 out.str("");
208}
209
210static const int kRelocInfoPosition = 57;
211
212static void PrintRelocInfo(std::ostringstream& out, Isolate* isolate,
213 const ExternalReferenceEncoder* ref_encoder,
214 std::ostream& os, CodeReference host,
215 RelocInfo* relocinfo, bool first_reloc_info = true) {
216 // Indent the printing of the reloc info.
217 int padding = kRelocInfoPosition;
218 if (first_reloc_info) {
219 // The first reloc info is printed after the disassembled instruction.
220 padding -= std::min(padding, static_cast<int>(out.tellp()));
221 } else {
222 // Additional reloc infos are printed on separate lines.
223 DumpBuffer(os, out);
224 }
225 std::fill_n(std::ostream_iterator<char>(out), padding, ' ');
226
227 RelocInfo::Mode rmode = relocinfo->rmode();
228 if (rmode == RelocInfo::DEOPT_SCRIPT_OFFSET) {
229 out << " ;; debug: deopt position, script offset '"
230 << static_cast<int>(relocinfo->data()) << "'";
231 } else if (rmode == RelocInfo::DEOPT_INLINING_ID) {
232 out << " ;; debug: deopt position, inlining id '"
233 << static_cast<int>(relocinfo->data()) << "'";
234 } else if (rmode == RelocInfo::DEOPT_REASON) {
235 DeoptimizeReason reason = static_cast<DeoptimizeReason>(relocinfo->data());
236 out << " ;; debug: deopt reason '" << DeoptimizeReasonToString(reason)
237 << "'";
238 } else if (rmode == RelocInfo::DEOPT_ID) {
239 out << " ;; debug: deopt index " << static_cast<int>(relocinfo->data());
240 } else if (rmode == RelocInfo::DEOPT_NODE_ID) {
241#ifdef DEBUG
242 out << " ;; debug: deopt node id "
243 << static_cast<uint32_t>(relocinfo->data());
244#else // DEBUG
245 UNREACHABLE();
246#endif // DEBUG
247 } else if (RelocInfo::IsEmbeddedObjectMode(rmode)) {
248 HeapStringAllocator allocator;
249 StringStream accumulator(&allocator);
250 ShortPrint(relocinfo->target_object(isolate), &accumulator);
251 std::unique_ptr<char[]> obj_name = accumulator.ToCString();
252 const bool is_compressed = RelocInfo::IsCompressedEmbeddedObject(rmode);
253 out << " ;; " << (is_compressed ? "(compressed) " : "")
254 << "object: " << obj_name.get();
255 } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
256 Address address = relocinfo->target_external_reference();
257 const char* reference_name =
258 ref_encoder
259 ? ref_encoder->NameOfAddress(isolate, address)
261 address, IsolateGroup::current()->external_ref_table());
262 out << " ;; external reference (" << reference_name << ")";
263 } else if (rmode == RelocInfo::JS_DISPATCH_HANDLE) {
264#ifdef V8_ENABLE_LEAPTIERING
265 out << " ;; js dispatch handle:0x" << std::hex
266 << relocinfo->js_dispatch_handle();
267 Tagged<Code> code = IsolateGroup::current()->js_dispatch_table()->GetCode(
268 relocinfo->js_dispatch_handle());
269 CodeKind kind = code->kind();
270 if (code->is_builtin()) {
271 out << " Builtin::" << Builtins::name(code->builtin_id());
272 } else {
273 out << " " << CodeKindToString(kind);
274 }
275#else
276 UNREACHABLE();
277#endif
278 } else if (RelocInfo::IsCodeTargetMode(rmode)) {
279 out << " ;; code:";
280 Tagged<Code> code =
281 isolate->heap()->FindCodeForInnerPointer(relocinfo->target_address());
282 CodeKind kind = code->kind();
283 if (code->is_builtin()) {
284 out << " Builtin::" << Builtins::name(code->builtin_id());
285 } else {
286 out << " " << CodeKindToString(kind);
287 }
288#if V8_ENABLE_WEBASSEMBLY
289 } else if (RelocInfo::IsWasmStubCall(rmode) && host.is_wasm_code()) {
290 // Host is isolate-independent, try wasm native module instead.
291 const char* runtime_stub_name = Builtins::name(
292 host.as_wasm_code()->native_module()->GetBuiltinInJumptableSlot(
293 relocinfo->wasm_stub_call_address()));
294 out << " ;; wasm stub: " << runtime_stub_name;
295#endif // V8_ENABLE_WEBASSEMBLY
296 } else {
297 out << " ;; " << RelocInfo::RelocModeName(rmode);
298 }
299}
300
301static int DecodeIt(Isolate* isolate, ExternalReferenceEncoder* ref_encoder,
302 std::ostream& os, CodeReference code,
303 const V8NameConverter& converter, uint8_t* begin,
304 uint8_t* end, Address current_pc, size_t range_limit) {
305 CHECK(!code.is_null());
307 std::ostringstream out;
308 uint8_t* pc = begin;
309 disasm::Disassembler d(converter,
311 RelocIterator rit(code);
312 CodeCommentsIterator cit(code.code_comments(), code.code_comments_size());
313
314#ifdef V8_TARGET_ARCH_X64
315 std::unique_ptr<BuiltinJumpTableInfoIterator> table_info_it = nullptr;
316 if (code.is_code() && code.as_code()->has_builtin_jump_table_info()) {
317 table_info_it = std::make_unique<BuiltinJumpTableInfoIterator>(
318 code.as_code()->builtin_jump_table_info(),
319 code.as_code()->builtin_jump_table_info_size());
320 }
321#endif // V8_TARGET_ARCH_X64
322
323 int constants = -1; // no constants being decoded at the start
324
325 while (pc < end) {
326 // First decode instruction so that we know its length.
327 uint8_t* prev_pc = pc;
328 bool decoding_constant_pool = constants > 0;
329 if (decoding_constant_pool) {
330 SNPrintF(
331 decode_buffer, "%08x constant",
332 base::ReadUnalignedValue<int32_t>(reinterpret_cast<Address>(pc)));
333 constants--;
334 pc += 4;
335 } else {
336 int num_const = d.ConstantPoolSizeAt(pc);
337 if (num_const >= 0) {
338 SNPrintF(
339 decode_buffer, "%08x constant pool begin (num_const = %d)",
340 base::ReadUnalignedValue<int32_t>(reinterpret_cast<Address>(pc)),
341 num_const);
342 constants = num_const;
343 pc += 4;
344 } else if (!rit.done() &&
345 rit.rinfo()->pc() == reinterpret_cast<Address>(pc) &&
346 rit.rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE) {
347 // A raw pointer embedded in code stream.
348 uint8_t* ptr =
350 SNPrintF(decode_buffer, "%08" V8PRIxPTR " jump table entry %4zu",
351 reinterpret_cast<intptr_t>(ptr),
352 static_cast<size_t>(ptr - begin));
353 pc += sizeof(ptr);
354#ifdef V8_TARGET_ARCH_X64
355 } else if (table_info_it && table_info_it->HasCurrent() &&
356 table_info_it->GetPCOffset() ==
357 static_cast<uint32_t>(pc - begin)) {
358 int32_t target_pc_offset = table_info_it->GetTarget();
359 static_assert(sizeof(target_pc_offset) ==
361 SNPrintF(decode_buffer, "jump table entry %08x", target_pc_offset);
363 table_info_it->Next();
364#endif // V8_TARGET_ARCH_X64
365 } else {
366 decode_buffer[0] = '\0';
367 pc += d.InstructionDecode(decode_buffer, pc);
368 }
369 }
370
371 Address pc_address = reinterpret_cast<Address>(pc);
372 if (range_limit != 0) {
373 if (pc_address > current_pc + range_limit) break;
374 if (pc_address <= current_pc - range_limit) continue;
375 }
376
377 // Collect RelocInfo for this instruction (prev_pc .. pc-1)
378 std::vector<const char*> comments;
379 std::vector<Address> pcs;
380 std::vector<RelocInfo::Mode> rmodes;
381 std::vector<intptr_t> datas;
382 while (!rit.done() && rit.rinfo()->pc() < reinterpret_cast<Address>(pc)) {
383 // Collect all data.
384 pcs.push_back(rit.rinfo()->pc());
385 rmodes.push_back(rit.rinfo()->rmode());
386 datas.push_back(rit.rinfo()->data());
387 rit.next();
388 }
389 while (cit.HasCurrent()) {
390 Address cur = cit.GetPCOffset();
391 if (cur >= static_cast<Address>(pc - begin)) break;
392 if (range_limit == 0 ||
393 cur + range_limit > current_pc - reinterpret_cast<Address>(begin)) {
394 comments.push_back(cit.GetComment());
395 }
396 cit.Next();
397 }
398
399 // Comments.
400 for (size_t i = 0; i < comments.size(); i++) {
401 if (v8_flags.log_colour) {
402 out << "\033[34m";
403 }
404 out << " " << comments[i];
405 if (v8_flags.log_colour) {
406 out << "\033[;m";
407 }
408 DumpBuffer(os, out);
409 }
410
411 // Instruction address and instruction offset.
412 if (v8_flags.log_colour &&
413 reinterpret_cast<Address>(prev_pc) == current_pc) {
414 // If this is the given "current" pc, make it yellow and bold.
415 out << "\033[33;1m";
416 }
417 out << static_cast<void*>(prev_pc) << " " << std::setw(4) << std::hex
418 << prev_pc - begin << " ";
419
420 // Instruction.
421 out << decode_buffer.begin();
422
423 // Print all the reloc info for this instruction which are not comments.
424 for (size_t i = 0; i < pcs.size(); i++) {
425 // Put together the reloc info.
426 const CodeReference& host = code;
428 host.is_null() ? kNullAddress : host.constant_pool();
429 if (host.is_code()) {
430 RelocInfo relocinfo(pcs[i], rmodes[i], datas[i], constant_pool);
431 bool first_reloc_info = (i == 0);
432 PrintRelocInfo(out, isolate, ref_encoder, os, code, &relocinfo,
433 first_reloc_info);
434 }
435 }
436
437 // If this is a constant pool load and we haven't found any RelocInfo
438 // already, check if we can find some RelocInfo for the target address in
439 // the constant pool.
440 // Make sure we're also not currently in the middle of decoding a constant
441 // pool itself, rather than a contant pool load. Since it can store any
442 // bytes, a constant could accidentally match with the bit-pattern checked
443 // by IsInConstantPool() below.
444 if (pcs.empty() && !code.is_null() && !decoding_constant_pool) {
445 RelocInfo dummy_rinfo(reinterpret_cast<Address>(prev_pc),
447 if (dummy_rinfo.IsInConstantPool()) {
448 Address constant_pool_entry_address =
449 dummy_rinfo.constant_pool_entry_address();
450 RelocIterator reloc_it(code);
451 while (!reloc_it.done()) {
452 if (reloc_it.rinfo()->IsInConstantPool() &&
453 (reloc_it.rinfo()->constant_pool_entry_address() ==
454 constant_pool_entry_address)) {
455 PrintRelocInfo(out, isolate, ref_encoder, os, code,
456 reloc_it.rinfo());
457 break;
458 }
459 reloc_it.next();
460 }
461 }
462 }
463
464 if (v8_flags.log_colour &&
465 reinterpret_cast<Address>(prev_pc) == current_pc) {
466 out << "\033[m";
467 }
468
469 DumpBuffer(os, out);
470 }
471
472 // Emit comments following the last instruction (if any).
473 while (cit.HasCurrent()) {
474 Address cur = cit.GetPCOffset();
475 if (range_limit == 0 ||
476 cur + range_limit == current_pc - reinterpret_cast<Address>(begin)) {
477 out << " " << cit.GetComment();
478 DumpBuffer(os, out);
479 }
480 cit.Next();
481 }
482
483 return static_cast<int>(pc - begin);
484}
485
486int Disassembler::Decode(Isolate* isolate, std::ostream& os, uint8_t* begin,
487 uint8_t* end, CodeReference code, Address current_pc,
488 size_t range_limit) {
489 DCHECK_WITH_MSG(v8_flags.text_is_readable,
490 "Builtins disassembly requires a readable .text section");
491 V8NameConverter v8NameConverter(isolate, code);
492 if (isolate) {
493 // We have an isolate, so support external reference names from V8 and
494 // embedder.
495 SealHandleScope shs(isolate);
497 ExternalReferenceEncoder ref_encoder(isolate);
498 return DecodeIt(isolate, &ref_encoder, os, code, v8NameConverter, begin,
499 end, current_pc, range_limit);
500 } else {
501 // No isolate => isolate-independent code. Only V8 External references
502 // available.
503 return DecodeIt(nullptr, nullptr, os, code, v8NameConverter, begin, end,
504 current_pc, range_limit);
505 }
506}
507
508#else // ENABLE_DISASSEMBLER
509
510int Disassembler::Decode(Isolate* isolate, std::ostream& os, uint8_t* begin,
511 uint8_t* end, CodeReference code, Address current_pc,
512 size_t range_limit) {
513 return 0;
514}
515
516#endif // ENABLE_DISASSEMBLER
517
518} // namespace internal
519} // namespace v8
Isolate * isolate_
Builtins::Kind kind
Definition builtins.cc:40
@ kContinueOnUnimplementedOpcode
Definition disasm.h:39
virtual const char * NameOfAddress(uint8_t *addr) const
constexpr T * begin() const
Definition vector.h:96
static constexpr int kBuiltinCount
Definition builtins.h:105
const char * Lookup(Address pc)
Definition builtins.cc:114
static constexpr int kBuiltinTier0Count
Definition builtins.h:108
static constexpr Builtin FromInt(int id)
Definition builtins.h:140
static V8_EXPORT_PRIVATE const char * name(Builtin builtin)
Definition builtins.cc:226
static V8_EXPORT_PRIVATE int Decode(Isolate *isolate, std::ostream &os, uint8_t *begin, uint8_t *end, CodeReference code={}, Address current_pc=kNullAddress, size_t range_limit=0)
const char * NameFromOffset(uint32_t offset)
static const char * NameOfIsolateIndependentAddress(Address address, MemorySpan< Address > shared_external_references)
static IsolateGroup * current()
Address isolate_root() const
Definition isolate.h:1242
ExternalReferenceTable * external_reference_table()
Definition isolate.h:1273
Builtins * builtins()
Definition isolate.h:1443
base::AddressRegion root_register_addressable_region() const
Definition isolate.h:1260
static constexpr bool IsCodeTargetMode(Mode mode)
Definition reloc-info.h:197
static constexpr bool IsCompressedEmbeddedObject(Mode mode)
Definition reloc-info.h:206
static constexpr bool IsWasmStubCall(Mode mode)
Definition reloc-info.h:214
static constexpr bool IsEmbeddedObjectMode(Mode mode)
Definition reloc-info.h:209
static const char * name(RootIndex root_index)
Definition roots.h:600
Handle< Code > code
int end
ZoneList< RegExpInstruction > code_
int32_t offset
int int32_t
Definition unicode.cc:40
static V ReadUnalignedValue(Address p)
Definition memory.h:28
int SNPrintF(Vector< char > str, const char *format,...)
Definition strings.cc:20
Node::Uses::const_iterator begin(const Node::Uses &uses)
Definition node.h:708
WasmCodeManager * GetWasmCodeManager()
const char * GetWasmCodeKindAsString(WasmCode::Kind kind)
PerThreadAssertScopeDebugOnly< false, SAFEPOINTS_ASSERT, HEAP_ALLOCATION_ASSERT > DisallowGarbageCollection
const char * CodeKindToString(CodeKind kind)
Definition code-kind.cc:10
Tagged(T object) -> Tagged< T >
char const * DeoptimizeReasonToString(DeoptimizeReason reason)
constexpr int kSystemPointerSize
Definition globals.h:410
void ShortPrint(Tagged< Object > obj, FILE *out)
Definition objects.cc:1865
V8_EXPORT_PRIVATE FlagValues v8_flags
static constexpr Address kNullAddress
Definition v8-internal.h:53
#define CHECK(condition)
Definition logging.h:124
#define DCHECK_WITH_MSG(condition, msg)
Definition logging.h:182
#define V8PRIxPTR
Definition macros.h:331