disassembler.cc 17 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
#include "src/codegen/assembler-inl.h"
#include "src/codegen/code-comments.h"
#include "src/codegen/code-reference.h"
15
#include "src/codegen/external-reference-encoder.h"
16
#include "src/codegen/macro-assembler.h"
17
#include "src/debug/debug.h"
18
#include "src/deoptimizer/deoptimizer.h"
19
#include "src/diagnostics/disasm.h"
20
#include "src/execution/isolate-data.h"
21
#include "src/ic/ic.h"
22
#include "src/objects/objects-inl.h"
23
#include "src/snapshot/embedded/embedded-data.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 226 227 228 229
    if (relocinfo->host().is_null()) {
      relocinfo->target_object_no_host(isolate).ShortPrint(&accumulator);
    } else {
      relocinfo->target_object().ShortPrint(&accumulator);
    }
230
    std::unique_ptr<char[]> obj_name = accumulator.ToCString();
231 232 233
    const bool is_compressed = RelocInfo::IsCompressedEmbeddedObject(rmode);
    out->AddFormatted("    ;; %sobject: %s",
                      is_compressed ? "(compressed) " : "", obj_name.get());
234
  } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
235 236 237 238
    const char* reference_name =
        ref_encoder ? ref_encoder->NameOfAddress(
                          isolate, relocinfo->target_external_reference())
                    : "unknown";
239
    out->AddFormatted("    ;; external reference (%s)", reference_name);
240
  } else if (RelocInfo::IsCodeTargetMode(rmode)) {
241
    out->AddFormatted("    ;; code:");
242
    Code code = isolate->heap()->GcSafeFindCodeForInnerPointer(
243
        relocinfo->target_address());
244
    CodeKind kind = code.kind();
245 246
    if (code.is_builtin()) {
      out->AddFormatted(" Builtin::%s", Builtins::name(code.builtin_index()));
247
    } else {
248
      out->AddFormatted(" %s", CodeKindToString(kind));
249
    }
250
  } else if (RelocInfo::IsWasmStubCall(rmode) && host.is_wasm_code()) {
251
    // Host is isolate-independent, try wasm native module instead.
252 253 254
    const char* runtime_stub_name = GetRuntimeStubName(
        host.as_wasm_code()->native_module()->GetRuntimeStubId(
            relocinfo->wasm_stub_call_address()));
255
    out->AddFormatted("    ;; wasm stub: %s", runtime_stub_name);
256
  } else if (RelocInfo::IsRuntimeEntry(rmode) && isolate &&
257
             isolate->deoptimizer_data() != nullptr) {
258
    // A runtime entry relocinfo might be a deoptimization bailout.
259
    Address addr = relocinfo->target_address();
260 261
    DeoptimizeKind type;
    if (Deoptimizer::IsDeoptimizationEntry(isolate, addr, &type)) {
262
      out->AddFormatted("    ;; %s deoptimization bailout",
263
                        Deoptimizer::MessageFor(type, false));
264
    } else {
265
      out->AddFormatted("    ;; %s", RelocInfo::RelocModeName(rmode));
266 267 268 269 270 271
    }
  } else {
    out->AddFormatted("    ;; %s", RelocInfo::RelocModeName(rmode));
  }
}

272
static int DecodeIt(Isolate* isolate, ExternalReferenceEncoder* ref_encoder,
273 274 275
                    std::ostream* os, CodeReference code,
                    const V8NameConverter& converter, byte* begin, byte* end,
                    Address current_pc) {
276
  CHECK(!code.is_null());
277 278
  v8::internal::EmbeddedVector<char, 128> decode_buffer;
  v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer;
279
  StringBuilder out(out_buffer.begin(), out_buffer.length());
280
  byte* pc = begin;
281 282
  disasm::Disassembler d(converter,
                         disasm::Disassembler::kContinueOnUnimplementedOpcode);
283
  RelocIterator* it = nullptr;
284
  CodeCommentsIterator cit(code.code_comments(), code.code_comments_size());
285 286 287 288
  // 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))) {
289
    it = new RelocIterator(code);
290 291 292
  } else {
    // No relocation information when printing code stubs.
  }
293
  int constants = -1;  // no constants being decoded at the start
294 295 296 297

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

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

    // Comments.
350
    for (size_t i = 0; i < comments.size(); i++) {
351
      out.AddFormatted("                  %s", comments[i]);
352
      DumpBuffer(os, &out);
353 354 355
    }

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

363
    // Instruction.
364
    out.AddFormatted("%s", decode_buffer.begin());
365 366

    // Print all the reloc info for this instruction which are not comments.
367
    for (size_t i = 0; i < pcs.size(); i++) {
368
      // Put together the reloc info
369
      const CodeReference& host = code;
370 371
      Address constant_pool =
          host.is_null() ? kNullAddress : host.constant_pool();
372 373 374 375 376 377 378
      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);
379

380
      bool first_reloc_info = (i == 0);
381
      PrintRelocInfo(&out, isolate, ref_encoder, os, code, &relocinfo,
382 383
                     first_reloc_info);
    }
384

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

408
    if (FLAG_log_colour && reinterpret_cast<Address>(prev_pc) == current_pc) {
409 410 411
      out.AddFormatted("\033[m");
    }

412
    DumpBuffer(os, &out);
413 414
  }

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

423
  delete it;
424
  return static_cast<int>(pc - begin);
425 426
}

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

444 445
#else  // ENABLE_DISASSEMBLER

446
int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
447
                         byte* end, CodeReference code, Address current_pc) {
448 449
  return 0;
}
450

451 452
#endif  // ENABLE_DISASSEMBLER

453 454
}  // namespace internal
}  // namespace v8