NativeExecutable.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <LibJIT/GDB.h>
  9. #include <LibJS/Bytecode/Interpreter.h>
  10. #include <LibJS/JIT/NativeExecutable.h>
  11. #include <LibJS/Runtime/VM.h>
  12. #include <LibX86/Disassembler.h>
  13. #include <sys/mman.h>
  14. namespace JS::JIT {
  15. NativeExecutable::NativeExecutable(void* code, size_t size, Vector<BytecodeMapping> mapping, Optional<FixedArray<u8>> gdb_object)
  16. : m_code(code)
  17. , m_size(size)
  18. , m_mapping(move(mapping))
  19. , m_gdb_object(move(gdb_object))
  20. {
  21. // Translate block index to instruction address, so the native code can just jump to it.
  22. for (auto const& entry : m_mapping) {
  23. if (entry.block_index == BytecodeMapping::EXECUTABLE)
  24. continue;
  25. if (entry.bytecode_offset == 0) {
  26. VERIFY(entry.block_index == m_block_entry_points.size());
  27. m_block_entry_points.append(bit_cast<FlatPtr>(m_code) + entry.native_offset);
  28. }
  29. }
  30. if (m_gdb_object.has_value())
  31. ::JIT::GDB::register_into_gdb(m_gdb_object.value().span());
  32. }
  33. NativeExecutable::~NativeExecutable()
  34. {
  35. if (m_gdb_object.has_value())
  36. ::JIT::GDB::unregister_from_gdb(m_gdb_object.value().span());
  37. munmap(m_code, m_size);
  38. }
  39. void NativeExecutable::run(VM& vm, size_t entry_point) const
  40. {
  41. FlatPtr entry_point_address = 0;
  42. if (entry_point != 0) {
  43. entry_point_address = m_block_entry_points[entry_point];
  44. VERIFY(entry_point_address != 0);
  45. }
  46. typedef void (*JITCode)(VM&, Value* registers, Value* locals, FlatPtr entry_point_address, ExecutionContext&);
  47. ((JITCode)m_code)(vm,
  48. vm.bytecode_interpreter().registers().data(),
  49. vm.running_execution_context().locals.data(),
  50. entry_point_address,
  51. vm.running_execution_context());
  52. }
  53. #if ARCH(X86_64)
  54. class JITSymbolProvider : public X86::SymbolProvider {
  55. public:
  56. JITSymbolProvider(NativeExecutable const& executable)
  57. : m_executable(executable)
  58. {
  59. }
  60. virtual ~JITSymbolProvider() override = default;
  61. virtual ByteString symbolicate(FlatPtr address, u32* offset = nullptr) const override
  62. {
  63. auto base = bit_cast<FlatPtr>(m_executable.code_bytes().data());
  64. auto native_offset = static_cast<u32>(address - base);
  65. if (native_offset >= m_executable.code_bytes().size())
  66. return {};
  67. auto const& entry = m_executable.find_mapping_entry(native_offset);
  68. if (offset)
  69. *offset = native_offset - entry.native_offset;
  70. if (entry.block_index == BytecodeMapping::EXECUTABLE)
  71. return BytecodeMapping::EXECUTABLE_LABELS[entry.bytecode_offset];
  72. if (entry.bytecode_offset == 0)
  73. return ByteString::formatted("Block {}", entry.block_index + 1);
  74. return ByteString::formatted("{}:{:x}", entry.block_index + 1, entry.bytecode_offset);
  75. }
  76. private:
  77. NativeExecutable const& m_executable;
  78. };
  79. #endif
  80. void NativeExecutable::dump_disassembly([[maybe_unused]] Bytecode::Executable const& executable) const
  81. {
  82. #if ARCH(X86_64)
  83. auto const* code_bytes = static_cast<u8 const*>(m_code);
  84. auto stream = X86::SimpleInstructionStream { code_bytes, m_size };
  85. auto disassembler = X86::Disassembler(stream);
  86. auto symbol_provider = JITSymbolProvider(*this);
  87. auto mapping = m_mapping.begin();
  88. if (!executable.basic_blocks.is_empty() && executable.basic_blocks[0]->size() != 0) {
  89. auto first_instruction = Bytecode::InstructionStreamIterator { executable.basic_blocks[0]->instruction_stream(), &executable };
  90. auto source_range = first_instruction.source_range().realize();
  91. dbgln("Disassembly of '{}' ({}:{}:{}):", executable.name, source_range.filename(), source_range.start.line, source_range.start.column);
  92. } else {
  93. dbgln("Disassembly of '{}':", executable.name);
  94. }
  95. while (true) {
  96. auto offset = stream.offset();
  97. auto virtual_offset = bit_cast<size_t>(m_code) + offset;
  98. while (!mapping.is_end() && offset > mapping->native_offset)
  99. ++mapping;
  100. if (!mapping.is_end() && offset == mapping->native_offset) {
  101. if (mapping->block_index == BytecodeMapping::EXECUTABLE) {
  102. dbgln("{}:", BytecodeMapping::EXECUTABLE_LABELS[mapping->bytecode_offset]);
  103. } else {
  104. auto const& block = *executable.basic_blocks[mapping->block_index];
  105. if (mapping->bytecode_offset == 0)
  106. dbgln("\nBlock {}:", mapping->block_index + 1);
  107. if (block.size() != 0) {
  108. VERIFY(mapping->bytecode_offset < block.size());
  109. auto const& instruction = *reinterpret_cast<Bytecode::Instruction const*>(block.data() + mapping->bytecode_offset);
  110. dbgln("{}:{:x} {}:", mapping->block_index + 1, mapping->bytecode_offset, instruction.to_byte_string(executable));
  111. }
  112. }
  113. }
  114. auto insn = disassembler.next();
  115. if (!insn.has_value())
  116. break;
  117. StringBuilder builder;
  118. builder.appendff("{:p} ", virtual_offset);
  119. auto length = insn.value().length();
  120. for (size_t i = 0; i < 7; i++) {
  121. if (i < length)
  122. builder.appendff("{:02x} ", code_bytes[offset + i]);
  123. else
  124. builder.append(" "sv);
  125. }
  126. builder.append(" "sv);
  127. builder.append(insn.value().to_byte_string(virtual_offset, &symbol_provider));
  128. dbgln("{}", builder.string_view());
  129. for (size_t bytes_printed = 7; bytes_printed < length; bytes_printed += 7) {
  130. builder.clear();
  131. builder.appendff("{:p} ", virtual_offset + bytes_printed);
  132. for (size_t i = bytes_printed; i < bytes_printed + 7 && i < length; i++)
  133. builder.appendff(" {:02x}", code_bytes[offset + i]);
  134. dbgln("{}", builder.string_view());
  135. }
  136. }
  137. dbgln();
  138. #endif
  139. }
  140. BytecodeMapping const& NativeExecutable::find_mapping_entry(size_t native_offset) const
  141. {
  142. size_t nearby_index = 0;
  143. AK::binary_search(
  144. m_mapping,
  145. native_offset,
  146. &nearby_index,
  147. [](FlatPtr needle, BytecodeMapping const& mapping_entry) {
  148. if (needle > mapping_entry.native_offset)
  149. return 1;
  150. if (needle == mapping_entry.native_offset)
  151. return 0;
  152. return -1;
  153. });
  154. return m_mapping[nearby_index];
  155. }
  156. Optional<UnrealizedSourceRange> NativeExecutable::get_source_range(Bytecode::Executable const& executable, FlatPtr address) const
  157. {
  158. auto start = bit_cast<FlatPtr>(m_code);
  159. auto end = start + m_size;
  160. if (address < start || address >= end)
  161. return {};
  162. auto const& entry = find_mapping_entry(address - start - 1);
  163. if (entry.block_index < executable.basic_blocks.size()) {
  164. auto const& block = *executable.basic_blocks[entry.block_index];
  165. if (entry.bytecode_offset < block.size()) {
  166. auto iterator = Bytecode::InstructionStreamIterator { block.instruction_stream(), &executable, entry.bytecode_offset };
  167. return iterator.source_range();
  168. }
  169. }
  170. return {};
  171. }
  172. }