disassembler.cc 16.9 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/diagnostics/disassembler.h"
6

7
#include <memory>
8
#include <unordered_map>
9
#include <vector>
10

11
#include "src/base/memory.h"
12 13 14 15
#include "src/codegen/assembler-inl.h"
#include "src/codegen/code-comments.h"
#include "src/codegen/code-reference.h"
#include "src/codegen/macro-assembler.h"
16
#include "src/debug/debug.h"
17
#include "src/deoptimizer/deoptimizer.h"
18
#include "src/diagnostics/disasm.h"
19
#include "src/execution/isolate-data.h"
20
#include "src/ic/ic.h"
21
#include "src/objects/objects-inl.h"
22
#include "src/snapshot/embedded/embedded-data.h"
23
#include "src/snapshot/serializer-common.h"
24
#include "src/strings/string-stream.h"
25
#include "src/utils/vector.h"
26
#include "src/wasm/wasm-code-manager.h"
27
#include "src/wasm/wasm-engine.h"
28

29 30
namespace v8 {
namespace internal {
31 32 33

#ifdef ENABLE_DISASSEMBLER

34
class V8NameConverter : public disasm::NameConverter {
35
 public:
36 37
  explicit V8NameConverter(Isolate* isolate, CodeReference code = {})
      : isolate_(isolate), code_(code) {}
38 39 40 41
  const char* NameOfAddress(byte* pc) const override;
  const char* NameInCode(byte* addr) const override;
  const char* RootRelativeName(int offset) const override;

42 43
  const CodeReference& code() const { return code_; }

44
 private:
45 46
  void InitExternalRefsCache() const;

47 48
  Isolate* isolate_;
  CodeReference code_;
49 50

  EmbeddedVector<char, 128> v8_buffer_;
51 52 53 54 55 56

  // Map from root-register relative offset of the external reference value to
  // the external reference name (stored in the external reference table).
  // This cache is used to recognize [root_reg + offs] patterns as direct
  // access to certain external reference's value.
  mutable std::unordered_map<int, const char*> directly_accessed_external_refs_;
57 58
};

59 60
void V8NameConverter::InitExternalRefsCache() const {
  ExternalReferenceTable* external_reference_table =
61
      isolate_->external_reference_table();
62 63 64 65
  if (!external_reference_table->is_initialized()) return;

  base::AddressRegion addressable_region =
      isolate_->root_register_addressable_region();
66
  Address isolate_root = isolate_->isolate_root();
67

68
  for (uint32_t i = 0; i < ExternalReferenceTable::kSize; i++) {
69 70
    Address address = external_reference_table->address(i);
    if (addressable_region.contains(address)) {
71
      int offset = static_cast<int>(address - isolate_root);
72 73 74 75 76
      const char* name = external_reference_table->name(i);
      directly_accessed_external_refs_.insert({offset, name});
    }
  }
}
77 78

const char* V8NameConverter::NameOfAddress(byte* pc) const {
79
  if (!code_.is_null()) {
80
    const char* name =
81 82
        isolate_ ? isolate_->builtins()->Lookup(reinterpret_cast<Address>(pc))
                 : nullptr;
83

84 85
    if (name != nullptr) {
      SNPrintF(v8_buffer_, "%p  (%s)", static_cast<void*>(pc), name);
86
      return v8_buffer_.begin();
87
    }
88

89
    int offs = static_cast<int>(reinterpret_cast<Address>(pc) -
90
                                code_.instruction_start());
91
    // print as code offset, if it seems reasonable
92
    if (0 <= offs && offs < code_.instruction_size()) {
93
      SNPrintF(v8_buffer_, "%p  <+0x%x>", static_cast<void*>(pc), offs);
94
      return v8_buffer_.begin();
95
    }
96

97
    wasm::WasmCodeRefScope wasm_code_ref_scope;
98
    wasm::WasmCode* wasm_code =
99 100 101
        isolate_ ? isolate_->wasm_engine()->code_manager()->LookupCode(
                       reinterpret_cast<Address>(pc))
                 : nullptr;
102 103
    if (wasm_code != nullptr) {
      SNPrintF(v8_buffer_, "%p  (%s)", static_cast<void*>(pc),
104
               wasm::GetWasmCodeKindAsString(wasm_code->kind()));
105
      return v8_buffer_.begin();
106
    }
107 108 109 110 111
  }

  return disasm::NameConverter::NameOfAddress(pc);
}

112 113 114
const char* V8NameConverter::NameInCode(byte* addr) const {
  // The V8NameConverter is used for well known code, so we can "safely"
  // dereference pointers in generated code.
115
  return code_.is_null() ? "" : reinterpret_cast<const char*>(addr);
116 117
}

118 119 120
const char* V8NameConverter::RootRelativeName(int offset) const {
  if (isolate_ == nullptr) return nullptr;

121 122 123
  const int kRootsTableStart = IsolateData::roots_table_offset();
  const unsigned kRootsTableSize = sizeof(RootsTable);
  const int kExtRefsTableStart = IsolateData::external_reference_table_offset();
124
  const unsigned kExtRefsTableSize = ExternalReferenceTable::kSizeInBytes;
125
  const int kBuiltinsTableStart = IsolateData::builtins_table_offset();
126 127
  const unsigned kBuiltinsTableSize =
      Builtins::builtin_count * kSystemPointerSize;
128

129 130
  if (static_cast<unsigned>(offset - kRootsTableStart) < kRootsTableSize) {
    uint32_t offset_in_roots_table = offset - kRootsTableStart;
131 132

    // Fail safe in the unlikely case of an arbitrary root-relative offset.
133
    if (offset_in_roots_table % kSystemPointerSize != 0) return nullptr;
134

135
    RootIndex root_index =
136
        static_cast<RootIndex>(offset_in_roots_table / kSystemPointerSize);
137

138
    SNPrintF(v8_buffer_, "root (%s)", RootsTable::name(root_index));
139
    return v8_buffer_.begin();
140

141 142 143
  } else if (static_cast<unsigned>(offset - kExtRefsTableStart) <
             kExtRefsTableSize) {
    uint32_t offset_in_extref_table = offset - kExtRefsTableStart;
144 145

    // Fail safe in the unlikely case of an arbitrary root-relative offset.
146
    if (offset_in_extref_table % ExternalReferenceTable::kEntrySize != 0) {
147 148 149 150
      return nullptr;
    }

    // Likewise if the external reference table is uninitialized.
151
    if (!isolate_->external_reference_table()->is_initialized()) {
152 153 154 155
      return nullptr;
    }

    SNPrintF(v8_buffer_, "external reference (%s)",
156
             isolate_->external_reference_table()->NameFromOffset(
157
                 offset_in_extref_table));
158
    return v8_buffer_.begin();
159

160 161 162
  } else if (static_cast<unsigned>(offset - kBuiltinsTableStart) <
             kBuiltinsTableSize) {
    uint32_t offset_in_builtins_table = (offset - kBuiltinsTableStart);
163

164 165
    Builtins::Name builtin_id = static_cast<Builtins::Name>(
        offset_in_builtins_table / kSystemPointerSize);
166 167 168

    const char* name = Builtins::name(builtin_id);
    SNPrintF(v8_buffer_, "builtin (%s)", name);
169
    return v8_buffer_.begin();
170

171
  } else {
172 173 174 175 176 177 178 179
    // It must be a direct access to one of the external values.
    if (directly_accessed_external_refs_.empty()) {
      InitExternalRefsCache();
    }

    auto iter = directly_accessed_external_refs_.find(offset);
    if (iter != directly_accessed_external_refs_.end()) {
      SNPrintF(v8_buffer_, "external value (%s)", iter->second);
180
      return v8_buffer_.begin();
181
    }
182
    return nullptr;
183 184
  }
}
185

186 187
static void DumpBuffer(std::ostream* os, StringBuilder* out) {
  (*os) << out->Finalize() << std::endl;
188
  out->Reset();
189 190
}

191
static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength;
192 193
static const int kRelocInfoPosition = 57;

194
static void PrintRelocInfo(StringBuilder* out, Isolate* isolate,
195
                           const ExternalReferenceEncoder* ref_encoder,
196 197
                           std::ostream* os, CodeReference host,
                           RelocInfo* relocinfo, bool first_reloc_info = true) {
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  // Indent the printing of the reloc info.
  if (first_reloc_info) {
    // The first reloc info is printed after the disassembled instruction.
    out->AddPadding(' ', kRelocInfoPosition - out->position());
  } else {
    // Additional reloc infos are printed on separate lines.
    DumpBuffer(os, out);
    out->AddPadding(' ', kRelocInfoPosition);
  }

  RelocInfo::Mode rmode = relocinfo->rmode();
  if (rmode == RelocInfo::DEOPT_SCRIPT_OFFSET) {
    out->AddFormatted("    ;; debug: deopt position, script offset '%d'",
                      static_cast<int>(relocinfo->data()));
  } else if (rmode == RelocInfo::DEOPT_INLINING_ID) {
    out->AddFormatted("    ;; debug: deopt position, inlining id '%d'",
                      static_cast<int>(relocinfo->data()));
  } else if (rmode == RelocInfo::DEOPT_REASON) {
    DeoptimizeReason reason = static_cast<DeoptimizeReason>(relocinfo->data());
    out->AddFormatted("    ;; debug: deopt reason '%s'",
                      DeoptimizeReasonToString(reason));
  } else if (rmode == RelocInfo::DEOPT_ID) {
    out->AddFormatted("    ;; debug: deopt index %d",
                      static_cast<int>(relocinfo->data()));
222
  } else if (RelocInfo::IsEmbeddedObjectMode(rmode)) {
223 224
    HeapStringAllocator allocator;
    StringStream accumulator(&allocator);
225
    relocinfo->target_object().ShortPrint(&accumulator);
226
    std::unique_ptr<char[]> obj_name = accumulator.ToCString();
227 228 229
    const bool is_compressed = RelocInfo::IsCompressedEmbeddedObject(rmode);
    out->AddFormatted("    ;; %sobject: %s",
                      is_compressed ? "(compressed) " : "", obj_name.get());
230
  } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
231 232 233 234
    const char* reference_name =
        ref_encoder ? ref_encoder->NameOfAddress(
                          isolate, relocinfo->target_external_reference())
                    : "unknown";
235
    out->AddFormatted("    ;; external reference (%s)", reference_name);
236
  } else if (RelocInfo::IsCodeTargetMode(rmode)) {
237
    out->AddFormatted("    ;; code:");
238
    Code code = isolate->heap()->GcSafeFindCodeForInnerPointer(
239
        relocinfo->target_address());
240 241 242
    Code::Kind kind = code.kind();
    if (code.is_builtin()) {
      out->AddFormatted(" Builtin::%s", Builtins::name(code.builtin_index()));
243
    } else {
244
      out->AddFormatted(" %s", Code::Kind2String(kind));
245
    }
246
  } else if (RelocInfo::IsWasmStubCall(rmode) && host.is_wasm_code()) {
247
    // Host is isolate-independent, try wasm native module instead.
248 249 250 251
    const char* runtime_stub_name =
        host.as_wasm_code()->native_module()->GetRuntimeStubName(
            relocinfo->wasm_stub_call_address());
    out->AddFormatted("    ;; wasm stub: %s", runtime_stub_name);
252
  } else if (RelocInfo::IsRuntimeEntry(rmode) && isolate &&
253
             isolate->deoptimizer_data() != nullptr) {
254
    // A runtime entry relocinfo might be a deoptimization bailout.
255
    Address addr = relocinfo->target_address();
256 257
    DeoptimizeKind type;
    if (Deoptimizer::IsDeoptimizationEntry(isolate, addr, &type)) {
258 259
      out->AddFormatted("    ;; %s deoptimization bailout",
                        Deoptimizer::MessageFor(type));
260
    } else {
261
      out->AddFormatted("    ;; %s", RelocInfo::RelocModeName(rmode));
262 263 264 265 266 267
    }
  } else {
    out->AddFormatted("    ;; %s", RelocInfo::RelocModeName(rmode));
  }
}

268
static int DecodeIt(Isolate* isolate, ExternalReferenceEncoder* ref_encoder,
269 270 271
                    std::ostream* os, CodeReference code,
                    const V8NameConverter& converter, byte* begin, byte* end,
                    Address current_pc) {
272
  CHECK(!code.is_null());
273 274
  v8::internal::EmbeddedVector<char, 128> decode_buffer;
  v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer;
275
  StringBuilder out(out_buffer.begin(), out_buffer.length());
276
  byte* pc = begin;
277 278
  disasm::Disassembler d(converter,
                         disasm::Disassembler::kContinueOnUnimplementedOpcode);
279
  RelocIterator* it = nullptr;
280
  CodeCommentsIterator cit(code.code_comments(), code.code_comments_size());
281 282 283 284
  // Relocation exists if we either have no isolate (wasm code),
  // or we have an isolate and it is not an off-heap instruction stream.
  if (!isolate ||
      !InstructionStream::PcIsOffHeap(isolate, bit_cast<Address>(begin))) {
285
    it = new RelocIterator(code);
286 287 288
  } else {
    // No relocation information when printing code stubs.
  }
289
  int constants = -1;  // no constants being decoded at the start
290 291 292 293

  while (pc < end) {
    // First decode instruction so that we know its length.
    byte* prev_pc = pc;
294
    if (constants > 0) {
295 296 297
      SNPrintF(
          decode_buffer, "%08x       constant",
          base::ReadUnalignedValue<int32_t>(reinterpret_cast<Address>(pc)));
298 299 300 301 302
      constants--;
      pc += 4;
    } else {
      int num_const = d.ConstantPoolSizeAt(pc);
      if (num_const >= 0) {
303 304 305 306
        SNPrintF(
            decode_buffer, "%08x       constant pool begin (num_const = %d)",
            base::ReadUnalignedValue<int32_t>(reinterpret_cast<Address>(pc)),
            num_const);
307 308
        constants = num_const;
        pc += 4;
309 310
      } else if (it != nullptr && !it->done() &&
                 it->rinfo()->pc() == reinterpret_cast<Address>(pc) &&
311
                 it->rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE) {
312
        // raw pointer embedded in code stream, e.g., jump table
313 314
        byte* ptr =
            base::ReadUnalignedValue<byte*>(reinterpret_cast<Address>(pc));
315 316 317
        SNPrintF(decode_buffer, "%08" V8PRIxPTR "      jump table entry %4zu",
                 reinterpret_cast<intptr_t>(ptr),
                 static_cast<size_t>(ptr - begin));
318
        pc += sizeof(ptr);
319 320
      } else {
        decode_buffer[0] = '\0';
321
        pc += d.InstructionDecode(decode_buffer, pc);
322 323
      }
    }
324 325

    // Collect RelocInfo for this instruction (prev_pc .. pc-1)
326
    std::vector<const char*> comments;
327
    std::vector<Address> pcs;
328 329
    std::vector<RelocInfo::Mode> rmodes;
    std::vector<intptr_t> datas;
330
    if (it != nullptr) {
331
      while (!it->done() && it->rinfo()->pc() < reinterpret_cast<Address>(pc)) {
332 333 334 335
        // Collect all data.
        pcs.push_back(it->rinfo()->pc());
        rmodes.push_back(it->rinfo()->rmode());
        datas.push_back(it->rinfo()->data());
336 337 338
        it->next();
      }
    }
339 340 341 342 343
    while (cit.HasCurrent() &&
           cit.GetPCOffset() < static_cast<Address>(pc - begin)) {
      comments.push_back(cit.GetComment());
      cit.Next();
    }
344 345

    // Comments.
346
    for (size_t i = 0; i < comments.size(); i++) {
347
      out.AddFormatted("                  %s", comments[i]);
348
      DumpBuffer(os, &out);
349 350 351
    }

    // Instruction address and instruction offset.
352
    if (FLAG_log_colour && reinterpret_cast<Address>(prev_pc) == current_pc) {
353 354 355
      // If this is the given "current" pc, make it yellow and bold.
      out.AddFormatted("\033[33;1m");
    }
356
    out.AddFormatted("%p  %4" V8PRIxPTRDIFF "  ", static_cast<void*>(prev_pc),
357
                     prev_pc - begin);
358

359
    // Instruction.
360
    out.AddFormatted("%s", decode_buffer.begin());
361 362

    // Print all the reloc info for this instruction which are not comments.
363
    for (size_t i = 0; i < pcs.size(); i++) {
364
      // Put together the reloc info
365
      const CodeReference& host = code;
366 367
      Address constant_pool =
          host.is_null() ? kNullAddress : host.constant_pool();
368 369 370 371 372 373 374
      Code code_pointer;
      if (!host.is_null() && host.is_js()) {
        code_pointer = *host.as_js_code();
      }

      RelocInfo relocinfo(pcs[i], rmodes[i], datas[i], code_pointer,
                          constant_pool);
375

376
      bool first_reloc_info = (i == 0);
377
      PrintRelocInfo(&out, isolate, ref_encoder, os, code, &relocinfo,
378 379
                     first_reloc_info);
    }
380

381 382 383
    // 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.
384
    if (pcs.empty() && !code.is_null()) {
385
      RelocInfo dummy_rinfo(reinterpret_cast<Address>(prev_pc), RelocInfo::NONE,
386
                            0, Code());
387
      if (dummy_rinfo.IsInConstantPool()) {
388
        Address constant_pool_entry_address =
389
            dummy_rinfo.constant_pool_entry_address();
390
        RelocIterator reloc_it(code);
391 392 393
        while (!reloc_it.done()) {
          if (reloc_it.rinfo()->IsInConstantPool() &&
              (reloc_it.rinfo()->constant_pool_entry_address() ==
394
               constant_pool_entry_address)) {
395 396
            PrintRelocInfo(&out, isolate, ref_encoder, os, code,
                           reloc_it.rinfo());
397
            break;
398
          }
399
          reloc_it.next();
400 401 402
        }
      }
    }
403

404
    if (FLAG_log_colour && reinterpret_cast<Address>(prev_pc) == current_pc) {
405 406 407
      out.AddFormatted("\033[m");
    }

408
    DumpBuffer(os, &out);
409 410
  }

411
  // Emit comments following the last instruction (if any).
412 413 414 415 416
  while (cit.HasCurrent() &&
         cit.GetPCOffset() < static_cast<Address>(pc - begin)) {
    out.AddFormatted("                  %s", cit.GetComment());
    DumpBuffer(os, &out);
    cit.Next();
417 418
  }

419
  delete it;
420
  return static_cast<int>(pc - begin);
421 422
}

423
int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
424
                         byte* end, CodeReference code, Address current_pc) {
425
  V8NameConverter v8NameConverter(isolate, code);
426 427 428 429 430
  if (isolate) {
    // We have an isolate, so support external reference names.
    SealHandleScope shs(isolate);
    DisallowHeapAllocation no_alloc;
    ExternalReferenceEncoder ref_encoder(isolate);
431
    return DecodeIt(isolate, &ref_encoder, os, code, v8NameConverter, begin,
432
                    end, current_pc);
433 434
  } else {
    // No isolate => isolate-independent code. No external reference names.
435
    return DecodeIt(nullptr, nullptr, os, code, v8NameConverter, begin, end,
436 437
                    current_pc);
  }
438 439
}

440 441
#else  // ENABLE_DISASSEMBLER

442
int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
443
                         byte* end, CodeReference code, Address current_pc) {
444 445
  return 0;
}
446

447 448
#endif  // ENABLE_DISASSEMBLER

449 450
}  // namespace internal
}  // namespace v8