1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:07:34 +00:00

LibJS/JIT: Provide source location information for JIT code

This works by walking a backtrace until the currently executing
native executable is found, and then mapping the native address
to its bytecode instruction.
This commit is contained in:
Simon Wanner 2023-10-30 22:06:27 +01:00 committed by Andreas Kling
parent 112eadc863
commit fb7b4b9c59
8 changed files with 59 additions and 6 deletions

View file

@ -12,6 +12,11 @@
#include <LibX86/Disassembler.h>
#include <sys/mman.h>
#if __has_include(<execinfo.h>)
# include <execinfo.h>
# define EXECINFO_BACKTRACE
#endif
namespace JS::JIT {
NativeExecutable::NativeExecutable(void* code, size_t size, Vector<BytecodeMapping> mapping)
@ -34,6 +39,7 @@ void NativeExecutable::run(VM& vm) const
vm.running_execution_context().local_variables.data());
}
#if ARCH(X86_64)
class JITSymbolProvider : public X86::SymbolProvider {
public:
JITSymbolProvider(NativeExecutable const& executable)
@ -67,8 +73,9 @@ public:
private:
NativeExecutable const& m_executable;
};
#endif
void NativeExecutable::dump_disassembly(Bytecode::Executable const& executable) const
void NativeExecutable::dump_disassembly([[maybe_unused]] Bytecode::Executable const& executable) const
{
#if ARCH(X86_64)
auto const* code_bytes = static_cast<u8 const*>(m_code);
@ -148,4 +155,33 @@ BytecodeMapping const& NativeExecutable::find_mapping_entry(size_t native_offset
return m_mapping[nearby_index];
}
Optional<Bytecode::InstructionStreamIterator const&> NativeExecutable::instruction_stream_iterator([[maybe_unused]] Bytecode::Executable const& executable) const
{
#ifdef EXECINFO_BACKTRACE
void* buffer[10];
auto count = backtrace(buffer, 10);
auto start = bit_cast<FlatPtr>(m_code);
auto end = start + m_size;
for (auto i = 0; i < count; i++) {
auto address = bit_cast<FlatPtr>(buffer[i]);
if (address < start || address >= end)
continue;
// return address points after the call
// let's subtract 1 to make sure we don't hit the next bytecode
// (in practice that's not necessary, because our native_call() sequence continues)
auto offset = address - start - 1;
auto& entry = find_mapping_entry(offset);
if (entry.block_index < executable.basic_blocks.size()) {
auto const& block = *executable.basic_blocks[entry.block_index];
if (entry.bytecode_offset < block.size()) {
// This is rather clunky, but Interpreter::instruction_stream_iterator() gives out references, so we need to keep it alive.
m_instruction_stream_iterator = make<Bytecode::InstructionStreamIterator>(block.instruction_stream(), &executable, entry.bytecode_offset);
return *m_instruction_stream_iterator;
}
}
}
#endif
return {};
}
}