NativeExecutable.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Simon Wanner <simon@skyrising.xyz>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/BinarySearch.h>
  8. #include <LibJS/Bytecode/Interpreter.h>
  9. #include <LibJS/JIT/NativeExecutable.h>
  10. #include <LibJS/Runtime/VM.h>
  11. #include <LibX86/Disassembler.h>
  12. #include <sys/mman.h>
  13. namespace JS::JIT {
  14. NativeExecutable::NativeExecutable(void* code, size_t size, Vector<BytecodeMapping> mapping)
  15. : m_code(code)
  16. , m_size(size)
  17. , m_mapping(move(mapping))
  18. {
  19. }
  20. NativeExecutable::~NativeExecutable()
  21. {
  22. munmap(m_code, m_size);
  23. }
  24. void NativeExecutable::run(VM& vm) const
  25. {
  26. typedef void (*JITCode)(VM&, Value* registers, Value* locals);
  27. ((JITCode)m_code)(vm,
  28. vm.bytecode_interpreter().registers().data(),
  29. vm.running_execution_context().local_variables.data());
  30. }
  31. class JITSymbolProvider : public X86::SymbolProvider {
  32. public:
  33. JITSymbolProvider(NativeExecutable const& executable)
  34. : m_executable(executable)
  35. {
  36. }
  37. virtual ~JITSymbolProvider() override = default;
  38. virtual DeprecatedString symbolicate(FlatPtr address, u32* offset = nullptr) const override
  39. {
  40. auto base = bit_cast<FlatPtr>(m_executable.code_bytes().data());
  41. auto native_offset = static_cast<u32>(address - base);
  42. if (native_offset >= m_executable.code_bytes().size())
  43. return {};
  44. auto const& entry = m_executable.find_mapping_entry(native_offset);
  45. if (offset)
  46. *offset = native_offset - entry.native_offset;
  47. if (entry.block_index == BytecodeMapping::EXECUTABLE)
  48. return BytecodeMapping::EXECUTABLE_LABELS[entry.bytecode_offset];
  49. if (entry.bytecode_offset == 0)
  50. return DeprecatedString::formatted("Block {}", entry.block_index + 1);
  51. return DeprecatedString::formatted("{}:{:x}", entry.block_index + 1, entry.bytecode_offset);
  52. }
  53. private:
  54. NativeExecutable const& m_executable;
  55. };
  56. void NativeExecutable::dump_disassembly(Bytecode::Executable const& executable) const
  57. {
  58. #if ARCH(X86_64)
  59. auto const* code_bytes = static_cast<u8 const*>(m_code);
  60. auto stream = X86::SimpleInstructionStream { code_bytes, m_size };
  61. auto disassembler = X86::Disassembler(stream);
  62. auto symbol_provider = JITSymbolProvider(*this);
  63. auto mapping = m_mapping.begin();
  64. auto first_instruction = Bytecode::InstructionStreamIterator { executable.basic_blocks[0]->instruction_stream(), &executable };
  65. auto source_range = first_instruction.source_range().realize();
  66. dbgln("Disassembly of '{}' ({}:{}:{}):", executable.name, source_range.filename(), source_range.start.line, source_range.start.column);
  67. while (true) {
  68. auto offset = stream.offset();
  69. auto virtual_offset = bit_cast<size_t>(m_code) + offset;
  70. while (!mapping.is_end() && offset > mapping->native_offset)
  71. ++mapping;
  72. if (!mapping.is_end() && offset == mapping->native_offset) {
  73. if (mapping->block_index == BytecodeMapping::EXECUTABLE) {
  74. dbgln("{}:", BytecodeMapping::EXECUTABLE_LABELS[mapping->bytecode_offset]);
  75. } else {
  76. auto const& block = *executable.basic_blocks[mapping->block_index];
  77. if (mapping->bytecode_offset == 0)
  78. dbgln("\nBlock {}:", mapping->block_index + 1);
  79. VERIFY(mapping->bytecode_offset < block.size());
  80. auto const& instruction = *reinterpret_cast<Bytecode::Instruction const*>(block.data() + mapping->bytecode_offset);
  81. dbgln("{}:{:x} {}:", mapping->block_index + 1, mapping->bytecode_offset, instruction.to_deprecated_string(executable));
  82. }
  83. }
  84. auto insn = disassembler.next();
  85. if (!insn.has_value())
  86. break;
  87. StringBuilder builder;
  88. builder.appendff("{:p} ", virtual_offset);
  89. auto length = insn.value().length();
  90. for (size_t i = 0; i < 7; i++) {
  91. if (i < length)
  92. builder.appendff("{:02x} ", code_bytes[offset + i]);
  93. else
  94. builder.append(" "sv);
  95. }
  96. builder.append(" "sv);
  97. builder.append(insn.value().to_deprecated_string(virtual_offset, &symbol_provider));
  98. dbgln("{}", builder.string_view());
  99. for (size_t bytes_printed = 7; bytes_printed < length; bytes_printed += 7) {
  100. builder.clear();
  101. builder.appendff("{:p} ", virtual_offset + bytes_printed);
  102. for (size_t i = bytes_printed; i < bytes_printed + 7 && i < length; i++)
  103. builder.appendff(" {:02x}", code_bytes[offset + i]);
  104. dbgln("{}", builder.string_view());
  105. }
  106. }
  107. dbgln();
  108. #endif
  109. }
  110. BytecodeMapping const& NativeExecutable::find_mapping_entry(size_t native_offset) const
  111. {
  112. size_t nearby_index = 0;
  113. AK::binary_search(
  114. m_mapping,
  115. native_offset,
  116. &nearby_index,
  117. [](FlatPtr needle, BytecodeMapping const& mapping_entry) {
  118. if (needle > mapping_entry.native_offset)
  119. return 1;
  120. if (needle == mapping_entry.native_offset)
  121. return 0;
  122. return -1;
  123. });
  124. return m_mapping[nearby_index];
  125. }
  126. }