NativeExecutable.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #if __has_include(<execinfo.h>)
  14. # include <execinfo.h>
  15. # define EXECINFO_BACKTRACE
  16. #endif
  17. #if defined(AK_OS_ANDROID) && (__ANDROID_API__ < 33)
  18. # undef EXECINFO_BACKTRACE
  19. #endif
  20. namespace JS::JIT {
  21. NativeExecutable::NativeExecutable(void* code, size_t size, Vector<BytecodeMapping> mapping)
  22. : m_code(code)
  23. , m_size(size)
  24. , m_mapping(move(mapping))
  25. {
  26. }
  27. NativeExecutable::~NativeExecutable()
  28. {
  29. munmap(m_code, m_size);
  30. }
  31. void NativeExecutable::run(VM& vm) const
  32. {
  33. typedef void (*JITCode)(VM&, Value* registers, Value* locals);
  34. ((JITCode)m_code)(vm,
  35. vm.bytecode_interpreter().registers().data(),
  36. vm.running_execution_context().local_variables.data());
  37. }
  38. #if ARCH(X86_64)
  39. class JITSymbolProvider : public X86::SymbolProvider {
  40. public:
  41. JITSymbolProvider(NativeExecutable const& executable)
  42. : m_executable(executable)
  43. {
  44. }
  45. virtual ~JITSymbolProvider() override = default;
  46. virtual DeprecatedString symbolicate(FlatPtr address, u32* offset = nullptr) const override
  47. {
  48. auto base = bit_cast<FlatPtr>(m_executable.code_bytes().data());
  49. auto native_offset = static_cast<u32>(address - base);
  50. if (native_offset >= m_executable.code_bytes().size())
  51. return {};
  52. auto const& entry = m_executable.find_mapping_entry(native_offset);
  53. if (offset)
  54. *offset = native_offset - entry.native_offset;
  55. if (entry.block_index == BytecodeMapping::EXECUTABLE)
  56. return BytecodeMapping::EXECUTABLE_LABELS[entry.bytecode_offset];
  57. if (entry.bytecode_offset == 0)
  58. return DeprecatedString::formatted("Block {}", entry.block_index + 1);
  59. return DeprecatedString::formatted("{}:{:x}", entry.block_index + 1, entry.bytecode_offset);
  60. }
  61. private:
  62. NativeExecutable const& m_executable;
  63. };
  64. #endif
  65. void NativeExecutable::dump_disassembly([[maybe_unused]] Bytecode::Executable const& executable) const
  66. {
  67. #if ARCH(X86_64)
  68. auto const* code_bytes = static_cast<u8 const*>(m_code);
  69. auto stream = X86::SimpleInstructionStream { code_bytes, m_size };
  70. auto disassembler = X86::Disassembler(stream);
  71. auto symbol_provider = JITSymbolProvider(*this);
  72. auto mapping = m_mapping.begin();
  73. auto first_instruction = Bytecode::InstructionStreamIterator { executable.basic_blocks[0]->instruction_stream(), &executable };
  74. auto source_range = first_instruction.source_range().realize();
  75. dbgln("Disassembly of '{}' ({}:{}:{}):", executable.name, source_range.filename(), source_range.start.line, source_range.start.column);
  76. while (true) {
  77. auto offset = stream.offset();
  78. auto virtual_offset = bit_cast<size_t>(m_code) + offset;
  79. while (!mapping.is_end() && offset > mapping->native_offset)
  80. ++mapping;
  81. if (!mapping.is_end() && offset == mapping->native_offset) {
  82. if (mapping->block_index == BytecodeMapping::EXECUTABLE) {
  83. dbgln("{}:", BytecodeMapping::EXECUTABLE_LABELS[mapping->bytecode_offset]);
  84. } else {
  85. auto const& block = *executable.basic_blocks[mapping->block_index];
  86. if (mapping->bytecode_offset == 0)
  87. dbgln("\nBlock {}:", mapping->block_index + 1);
  88. VERIFY(mapping->bytecode_offset < block.size());
  89. auto const& instruction = *reinterpret_cast<Bytecode::Instruction const*>(block.data() + mapping->bytecode_offset);
  90. dbgln("{}:{:x} {}:", mapping->block_index + 1, mapping->bytecode_offset, instruction.to_deprecated_string(executable));
  91. }
  92. }
  93. auto insn = disassembler.next();
  94. if (!insn.has_value())
  95. break;
  96. StringBuilder builder;
  97. builder.appendff("{:p} ", virtual_offset);
  98. auto length = insn.value().length();
  99. for (size_t i = 0; i < 7; i++) {
  100. if (i < length)
  101. builder.appendff("{:02x} ", code_bytes[offset + i]);
  102. else
  103. builder.append(" "sv);
  104. }
  105. builder.append(" "sv);
  106. builder.append(insn.value().to_deprecated_string(virtual_offset, &symbol_provider));
  107. dbgln("{}", builder.string_view());
  108. for (size_t bytes_printed = 7; bytes_printed < length; bytes_printed += 7) {
  109. builder.clear();
  110. builder.appendff("{:p} ", virtual_offset + bytes_printed);
  111. for (size_t i = bytes_printed; i < bytes_printed + 7 && i < length; i++)
  112. builder.appendff(" {:02x}", code_bytes[offset + i]);
  113. dbgln("{}", builder.string_view());
  114. }
  115. }
  116. dbgln();
  117. #endif
  118. }
  119. BytecodeMapping const& NativeExecutable::find_mapping_entry(size_t native_offset) const
  120. {
  121. size_t nearby_index = 0;
  122. AK::binary_search(
  123. m_mapping,
  124. native_offset,
  125. &nearby_index,
  126. [](FlatPtr needle, BytecodeMapping const& mapping_entry) {
  127. if (needle > mapping_entry.native_offset)
  128. return 1;
  129. if (needle == mapping_entry.native_offset)
  130. return 0;
  131. return -1;
  132. });
  133. return m_mapping[nearby_index];
  134. }
  135. Optional<Bytecode::InstructionStreamIterator const&> NativeExecutable::instruction_stream_iterator([[maybe_unused]] Bytecode::Executable const& executable) const
  136. {
  137. #ifdef EXECINFO_BACKTRACE
  138. void* buffer[10];
  139. auto count = backtrace(buffer, 10);
  140. auto start = bit_cast<FlatPtr>(m_code);
  141. auto end = start + m_size;
  142. for (auto i = 0; i < count; i++) {
  143. auto address = bit_cast<FlatPtr>(buffer[i]);
  144. if (address < start || address >= end)
  145. continue;
  146. // return address points after the call
  147. // let's subtract 1 to make sure we don't hit the next bytecode
  148. // (in practice that's not necessary, because our native_call() sequence continues)
  149. auto offset = address - start - 1;
  150. auto& entry = find_mapping_entry(offset);
  151. if (entry.block_index < executable.basic_blocks.size()) {
  152. auto const& block = *executable.basic_blocks[entry.block_index];
  153. if (entry.bytecode_offset < block.size()) {
  154. // This is rather clunky, but Interpreter::instruction_stream_iterator() gives out references, so we need to keep it alive.
  155. m_instruction_stream_iterator = make<Bytecode::InstructionStreamIterator>(block.instruction_stream(), &executable, entry.bytecode_offset);
  156. return *m_instruction_stream_iterator;
  157. }
  158. }
  159. }
  160. #endif
  161. return {};
  162. }
  163. }