Line data Source code
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 :
5 : #include "src/disassembler.h"
6 :
7 : #include <memory>
8 : #include <unordered_map>
9 : #include <vector>
10 :
11 : #include "src/assembler-inl.h"
12 : #include "src/code-comments.h"
13 : #include "src/code-reference.h"
14 : #include "src/debug/debug.h"
15 : #include "src/deoptimizer.h"
16 : #include "src/disasm.h"
17 : #include "src/ic/ic.h"
18 : #include "src/isolate-data.h"
19 : #include "src/macro-assembler.h"
20 : #include "src/objects-inl.h"
21 : #include "src/snapshot/embedded-data.h"
22 : #include "src/snapshot/serializer-common.h"
23 : #include "src/string-stream.h"
24 : #include "src/wasm/wasm-code-manager.h"
25 : #include "src/wasm/wasm-engine.h"
26 :
27 : namespace v8 {
28 : namespace internal {
29 :
30 : #ifdef ENABLE_DISASSEMBLER
31 :
32 : class V8NameConverter: public disasm::NameConverter {
33 : public:
34 : explicit V8NameConverter(Isolate* isolate, CodeReference code = {})
35 : : isolate_(isolate), code_(code) {}
36 : const char* NameOfAddress(byte* pc) const override;
37 : const char* NameInCode(byte* addr) const override;
38 : const char* RootRelativeName(int offset) const override;
39 :
40 : const CodeReference& code() const { return code_; }
41 :
42 : private:
43 : void InitExternalRefsCache() const;
44 :
45 : Isolate* isolate_;
46 : CodeReference code_;
47 :
48 : EmbeddedVector<char, 128> v8_buffer_;
49 :
50 : // Map from root-register relative offset of the external reference value to
51 : // the external reference name (stored in the external reference table).
52 : // This cache is used to recognize [root_reg + offs] patterns as direct
53 : // access to certain external reference's value.
54 : mutable std::unordered_map<int, const char*> directly_accessed_external_refs_;
55 : };
56 :
57 : void V8NameConverter::InitExternalRefsCache() const {
58 : ExternalReferenceTable* external_reference_table =
59 : isolate_->external_reference_table();
60 : if (!external_reference_table->is_initialized()) return;
61 :
62 : base::AddressRegion addressable_region =
63 : isolate_->root_register_addressable_region();
64 : Address isolate_root = isolate_->isolate_root();
65 :
66 : for (uint32_t i = 0; i < ExternalReferenceTable::kSize; i++) {
67 : Address address = external_reference_table->address(i);
68 : if (addressable_region.contains(address)) {
69 : int offset = static_cast<int>(address - isolate_root);
70 : const char* name = external_reference_table->name(i);
71 : directly_accessed_external_refs_.insert({offset, name});
72 : }
73 : }
74 : }
75 :
76 : const char* V8NameConverter::NameOfAddress(byte* pc) const {
77 : if (!code_.is_null()) {
78 : const char* name =
79 : isolate_ ? isolate_->builtins()->Lookup(reinterpret_cast<Address>(pc))
80 : : nullptr;
81 :
82 : if (name != nullptr) {
83 : SNPrintF(v8_buffer_, "%p (%s)", static_cast<void*>(pc), name);
84 : return v8_buffer_.start();
85 : }
86 :
87 : int offs = static_cast<int>(reinterpret_cast<Address>(pc) -
88 : code_.instruction_start());
89 : // print as code offset, if it seems reasonable
90 : if (0 <= offs && offs < code_.instruction_size()) {
91 : SNPrintF(v8_buffer_, "%p <+0x%x>", static_cast<void*>(pc), offs);
92 : return v8_buffer_.start();
93 : }
94 :
95 : wasm::WasmCode* wasm_code =
96 : isolate_ ? isolate_->wasm_engine()->code_manager()->LookupCode(
97 : reinterpret_cast<Address>(pc))
98 : : nullptr;
99 : if (wasm_code != nullptr) {
100 : SNPrintF(v8_buffer_, "%p (%s)", static_cast<void*>(pc),
101 : wasm::GetWasmCodeKindAsString(wasm_code->kind()));
102 : return v8_buffer_.start();
103 : }
104 : }
105 :
106 : return disasm::NameConverter::NameOfAddress(pc);
107 : }
108 :
109 :
110 : const char* V8NameConverter::NameInCode(byte* addr) const {
111 : // The V8NameConverter is used for well known code, so we can "safely"
112 : // dereference pointers in generated code.
113 : return code_.is_null() ? "" : reinterpret_cast<const char*>(addr);
114 : }
115 :
116 : const char* V8NameConverter::RootRelativeName(int offset) const {
117 : if (isolate_ == nullptr) return nullptr;
118 :
119 : const int kRootsTableStart = IsolateData::roots_table_offset();
120 : const unsigned kRootsTableSize = sizeof(RootsTable);
121 : const int kExtRefsTableStart = IsolateData::external_reference_table_offset();
122 : const unsigned kExtRefsTableSize = ExternalReferenceTable::kSizeInBytes;
123 : const int kBuiltinsTableStart = IsolateData::builtins_table_offset();
124 : const unsigned kBuiltinsTableSize =
125 : Builtins::builtin_count * kSystemPointerSize;
126 :
127 : if (static_cast<unsigned>(offset - kRootsTableStart) < kRootsTableSize) {
128 : uint32_t offset_in_roots_table = offset - kRootsTableStart;
129 :
130 : // Fail safe in the unlikely case of an arbitrary root-relative offset.
131 : if (offset_in_roots_table % kSystemPointerSize != 0) return nullptr;
132 :
133 : RootIndex root_index =
134 : static_cast<RootIndex>(offset_in_roots_table / kSystemPointerSize);
135 :
136 : SNPrintF(v8_buffer_, "root (%s)", RootsTable::name(root_index));
137 : return v8_buffer_.start();
138 :
139 : } else if (static_cast<unsigned>(offset - kExtRefsTableStart) <
140 : kExtRefsTableSize) {
141 : uint32_t offset_in_extref_table = offset - kExtRefsTableStart;
142 :
143 : // Fail safe in the unlikely case of an arbitrary root-relative offset.
144 : if (offset_in_extref_table % ExternalReferenceTable::kEntrySize != 0) {
145 : return nullptr;
146 : }
147 :
148 : // Likewise if the external reference table is uninitialized.
149 : if (!isolate_->external_reference_table()->is_initialized()) {
150 : return nullptr;
151 : }
152 :
153 : SNPrintF(v8_buffer_, "external reference (%s)",
154 : isolate_->external_reference_table()->NameFromOffset(
155 : offset_in_extref_table));
156 : return v8_buffer_.start();
157 :
158 : } else if (static_cast<unsigned>(offset - kBuiltinsTableStart) <
159 : kBuiltinsTableSize) {
160 : uint32_t offset_in_builtins_table = (offset - kBuiltinsTableStart);
161 :
162 : Builtins::Name builtin_id = static_cast<Builtins::Name>(
163 : offset_in_builtins_table / kSystemPointerSize);
164 :
165 : const char* name = Builtins::name(builtin_id);
166 : SNPrintF(v8_buffer_, "builtin (%s)", name);
167 : return v8_buffer_.start();
168 :
169 : } else {
170 : // It must be a direct access to one of the external values.
171 : if (directly_accessed_external_refs_.empty()) {
172 : InitExternalRefsCache();
173 : }
174 :
175 : auto iter = directly_accessed_external_refs_.find(offset);
176 : if (iter != directly_accessed_external_refs_.end()) {
177 : SNPrintF(v8_buffer_, "external value (%s)", iter->second);
178 : return v8_buffer_.start();
179 : }
180 : return "WAAT??? What are we accessing here???";
181 : }
182 : }
183 :
184 : static void DumpBuffer(std::ostream* os, StringBuilder* out) {
185 : (*os) << out->Finalize() << std::endl;
186 : out->Reset();
187 : }
188 :
189 :
190 : static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength;
191 : static const int kRelocInfoPosition = 57;
192 :
193 : static void PrintRelocInfo(StringBuilder* out, Isolate* isolate,
194 : const ExternalReferenceEncoder* ref_encoder,
195 : std::ostream* os, CodeReference host,
196 : RelocInfo* relocinfo, bool first_reloc_info = true) {
197 : // Indent the printing of the reloc info.
198 : if (first_reloc_info) {
199 : // The first reloc info is printed after the disassembled instruction.
200 : out->AddPadding(' ', kRelocInfoPosition - out->position());
201 : } else {
202 : // Additional reloc infos are printed on separate lines.
203 : DumpBuffer(os, out);
204 : out->AddPadding(' ', kRelocInfoPosition);
205 : }
206 :
207 : RelocInfo::Mode rmode = relocinfo->rmode();
208 : if (rmode == RelocInfo::DEOPT_SCRIPT_OFFSET) {
209 : out->AddFormatted(" ;; debug: deopt position, script offset '%d'",
210 : static_cast<int>(relocinfo->data()));
211 : } else if (rmode == RelocInfo::DEOPT_INLINING_ID) {
212 : out->AddFormatted(" ;; debug: deopt position, inlining id '%d'",
213 : static_cast<int>(relocinfo->data()));
214 : } else if (rmode == RelocInfo::DEOPT_REASON) {
215 : DeoptimizeReason reason = static_cast<DeoptimizeReason>(relocinfo->data());
216 : out->AddFormatted(" ;; debug: deopt reason '%s'",
217 : DeoptimizeReasonToString(reason));
218 : } else if (rmode == RelocInfo::DEOPT_ID) {
219 : out->AddFormatted(" ;; debug: deopt index %d",
220 : static_cast<int>(relocinfo->data()));
221 : } else if (rmode == RelocInfo::EMBEDDED_OBJECT) {
222 : HeapStringAllocator allocator;
223 : StringStream accumulator(&allocator);
224 : relocinfo->target_object()->ShortPrint(&accumulator);
225 : std::unique_ptr<char[]> obj_name = accumulator.ToCString();
226 : out->AddFormatted(" ;; object: %s", obj_name.get());
227 : } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
228 : const char* reference_name =
229 : ref_encoder ? ref_encoder->NameOfAddress(
230 : isolate, relocinfo->target_external_reference())
231 : : "unknown";
232 : out->AddFormatted(" ;; external reference (%s)", reference_name);
233 : } else if (RelocInfo::IsCodeTargetMode(rmode)) {
234 : out->AddFormatted(" ;; code:");
235 : Code code = isolate->heap()->GcSafeFindCodeForInnerPointer(
236 : relocinfo->target_address());
237 : Code::Kind kind = code->kind();
238 : if (code->is_builtin()) {
239 : out->AddFormatted(" Builtin::%s", Builtins::name(code->builtin_index()));
240 : } else {
241 : out->AddFormatted(" %s", Code::Kind2String(kind));
242 : }
243 : } else if (RelocInfo::IsWasmStubCall(rmode) && host.is_wasm_code()) {
244 : // Host is isolate-independent, try wasm native module instead.
245 : const char* runtime_stub_name =
246 : host.as_wasm_code()->native_module()->GetRuntimeStubName(
247 : relocinfo->wasm_stub_call_address());
248 : out->AddFormatted(" ;; wasm stub: %s", runtime_stub_name);
249 : } else if (RelocInfo::IsRuntimeEntry(rmode) && isolate &&
250 : isolate->deoptimizer_data() != nullptr) {
251 : // A runtime entry relocinfo might be a deoptimization bailout.
252 : Address addr = relocinfo->target_address();
253 : DeoptimizeKind type;
254 : if (Deoptimizer::IsDeoptimizationEntry(isolate, addr, &type)) {
255 : out->AddFormatted(" ;; %s deoptimization bailout",
256 : Deoptimizer::MessageFor(type));
257 : } else {
258 : out->AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
259 : }
260 : } else {
261 : out->AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
262 : }
263 : }
264 :
265 : static int DecodeIt(Isolate* isolate, ExternalReferenceEncoder* ref_encoder,
266 : std::ostream* os, CodeReference code,
267 : const V8NameConverter& converter, byte* begin, byte* end,
268 : Address current_pc) {
269 : CHECK(!code.is_null());
270 : v8::internal::EmbeddedVector<char, 128> decode_buffer;
271 : v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer;
272 : StringBuilder out(out_buffer.start(), out_buffer.length());
273 : byte* pc = begin;
274 : disasm::Disassembler d(converter,
275 : disasm::Disassembler::kContinueOnUnimplementedOpcode);
276 : RelocIterator* it = nullptr;
277 : CodeCommentsIterator cit(code.code_comments());
278 : // Relocation exists if we either have no isolate (wasm code),
279 : // or we have an isolate and it is not an off-heap instruction stream.
280 : if (!isolate ||
281 : !InstructionStream::PcIsOffHeap(isolate, bit_cast<Address>(begin))) {
282 : it = new RelocIterator(code);
283 : } else {
284 : // No relocation information when printing code stubs.
285 : }
286 : int constants = -1; // no constants being decoded at the start
287 :
288 : while (pc < end) {
289 : // First decode instruction so that we know its length.
290 : byte* prev_pc = pc;
291 : if (constants > 0) {
292 : SNPrintF(decode_buffer,
293 : "%08x constant",
294 : *reinterpret_cast<int32_t*>(pc));
295 : constants--;
296 : pc += 4;
297 : } else {
298 : int num_const = d.ConstantPoolSizeAt(pc);
299 : if (num_const >= 0) {
300 : SNPrintF(decode_buffer,
301 : "%08x constant pool begin (num_const = %d)",
302 : *reinterpret_cast<int32_t*>(pc), num_const);
303 : constants = num_const;
304 : pc += 4;
305 : } else if (it != nullptr && !it->done() &&
306 : it->rinfo()->pc() == reinterpret_cast<Address>(pc) &&
307 : it->rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE) {
308 : // raw pointer embedded in code stream, e.g., jump table
309 : byte* ptr = *reinterpret_cast<byte**>(pc);
310 : SNPrintF(
311 : decode_buffer, "%08" V8PRIxPTR " jump table entry %4" PRIuS,
312 : reinterpret_cast<intptr_t>(ptr), static_cast<size_t>(ptr - begin));
313 : pc += sizeof(ptr);
314 : } else {
315 : decode_buffer[0] = '\0';
316 : pc += d.InstructionDecode(decode_buffer, pc);
317 : }
318 : }
319 :
320 : // Collect RelocInfo for this instruction (prev_pc .. pc-1)
321 : std::vector<const char*> comments;
322 : std::vector<Address> pcs;
323 : std::vector<RelocInfo::Mode> rmodes;
324 : std::vector<intptr_t> datas;
325 : if (it != nullptr) {
326 : while (!it->done() && it->rinfo()->pc() < reinterpret_cast<Address>(pc)) {
327 : // Collect all data.
328 : pcs.push_back(it->rinfo()->pc());
329 : rmodes.push_back(it->rinfo()->rmode());
330 : datas.push_back(it->rinfo()->data());
331 : it->next();
332 : }
333 : }
334 : while (cit.HasCurrent() &&
335 : cit.GetPCOffset() < static_cast<Address>(pc - begin)) {
336 : comments.push_back(cit.GetComment());
337 : cit.Next();
338 : }
339 :
340 : // Comments.
341 : for (size_t i = 0; i < comments.size(); i++) {
342 : out.AddFormatted(" %s", comments[i]);
343 : DumpBuffer(os, &out);
344 : }
345 :
346 : // Instruction address and instruction offset.
347 : if (FLAG_log_colour && reinterpret_cast<Address>(prev_pc) == current_pc) {
348 : // If this is the given "current" pc, make it yellow and bold.
349 : out.AddFormatted("\033[33;1m");
350 : }
351 : out.AddFormatted("%p %4" V8PRIxPTRDIFF " ", static_cast<void*>(prev_pc),
352 : prev_pc - begin);
353 :
354 : // Instruction.
355 : out.AddFormatted("%s", decode_buffer.start());
356 :
357 : // Print all the reloc info for this instruction which are not comments.
358 : for (size_t i = 0; i < pcs.size(); i++) {
359 : // Put together the reloc info
360 : const CodeReference& host = code;
361 : Address constant_pool =
362 : host.is_null() ? kNullAddress : host.constant_pool();
363 : RelocInfo relocinfo(pcs[i], rmodes[i], datas[i], Code(), constant_pool);
364 :
365 : bool first_reloc_info = (i == 0);
366 : PrintRelocInfo(&out, isolate, ref_encoder, os, code, &relocinfo,
367 : first_reloc_info);
368 : }
369 :
370 : // If this is a constant pool load and we haven't found any RelocInfo
371 : // already, check if we can find some RelocInfo for the target address in
372 : // the constant pool.
373 : if (pcs.empty() && !code.is_null()) {
374 : RelocInfo dummy_rinfo(reinterpret_cast<Address>(prev_pc),
375 : RelocInfo::NONE,
376 : 0, Code());
377 : if (dummy_rinfo.IsInConstantPool()) {
378 : Address constant_pool_entry_address =
379 : dummy_rinfo.constant_pool_entry_address();
380 : RelocIterator reloc_it(code);
381 : while (!reloc_it.done()) {
382 : if (reloc_it.rinfo()->IsInConstantPool() &&
383 : (reloc_it.rinfo()->constant_pool_entry_address() ==
384 : constant_pool_entry_address)) {
385 : PrintRelocInfo(&out, isolate, ref_encoder, os, code,
386 : reloc_it.rinfo());
387 : break;
388 : }
389 : reloc_it.next();
390 : }
391 : }
392 : }
393 :
394 : if (FLAG_log_colour && reinterpret_cast<Address>(prev_pc) == current_pc) {
395 : out.AddFormatted("\033[m");
396 : }
397 :
398 : DumpBuffer(os, &out);
399 : }
400 :
401 : // Emit comments following the last instruction (if any).
402 : while (cit.HasCurrent() &&
403 : cit.GetPCOffset() < static_cast<Address>(pc - begin)) {
404 : out.AddFormatted(" %s", cit.GetComment());
405 : DumpBuffer(os, &out);
406 : cit.Next();
407 : }
408 :
409 : delete it;
410 : return static_cast<int>(pc - begin);
411 : }
412 :
413 : int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
414 : byte* end, CodeReference code, Address current_pc) {
415 : V8NameConverter v8NameConverter(isolate, code);
416 : if (isolate) {
417 : // We have an isolate, so support external reference names.
418 : SealHandleScope shs(isolate);
419 : DisallowHeapAllocation no_alloc;
420 : ExternalReferenceEncoder ref_encoder(isolate);
421 : return DecodeIt(isolate, &ref_encoder, os, code, v8NameConverter, begin,
422 : end, current_pc);
423 : } else {
424 : // No isolate => isolate-independent code. No external reference names.
425 : return DecodeIt(nullptr, nullptr, os, code, v8NameConverter, begin, end,
426 : current_pc);
427 : }
428 : }
429 :
430 : #else // ENABLE_DISASSEMBLER
431 :
432 0 : int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
433 : byte* end, CodeReference code, Address current_pc) {
434 0 : return 0;
435 : }
436 :
437 : #endif // ENABLE_DISASSEMBLER
438 :
439 : } // namespace internal
440 178779 : } // namespace v8
|