NativeExecutable.cpp 6.8 KB

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