Skip to content

Commit

Permalink
LibJS: Don't crash when dumping a backtrace
Browse files Browse the repository at this point in the history
Previously, we would crash when attempting to log a backtrace with the
`--log-all-js-exceptions` option enabled. A crash would occur if the
program counter value for the current frame was larger than the
current executable bytecode size, or if a source mapping couldn't be
found for the current program counter value.
  • Loading branch information
tcl3 committed Jul 24, 2024
1 parent 40f9c9f commit 0670da0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Userland/Libraries/LibJS/Bytecode/Executable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ Optional<Executable::ExceptionHandlers const&> Executable::exception_handlers_fo
return {};
}

UnrealizedSourceRange Executable::source_range_at(size_t offset) const
Optional<UnrealizedSourceRange> Executable::source_range_at(size_t offset) const
{
if (offset >= bytecode.size())
return {};
return OptionalNone {};
auto it = InstructionStreamIterator(bytecode.span().slice(offset), this);
VERIFY(!it.at_end());
auto mapping = source_map.get(offset);
if (!mapping.has_value())
return {};
return OptionalNone {};
return UnrealizedSourceRange {
.source_code = source_code,
.start_offset = mapping->source_start_offset,
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Bytecode/Executable.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Executable final : public Cell {

[[nodiscard]] Optional<ExceptionHandlers const&> exception_handlers_for_offset(size_t offset) const;

[[nodiscard]] UnrealizedSourceRange source_range_at(size_t offset) const;
[[nodiscard]] Optional<UnrealizedSourceRange> source_range_at(size_t offset) const;

void dump() const;

Expand Down
11 changes: 9 additions & 2 deletions Userland/Libraries/LibJS/Runtime/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,15 @@ void VM::dump_backtrace() const
for (ssize_t i = m_execution_context_stack.size() - 1; i >= 0; --i) {
auto& frame = m_execution_context_stack[i];
if (frame->executable && frame->program_counter.has_value()) {
auto source_range = frame->executable->source_range_at(frame->program_counter.value()).realize();
dbgln("-> {} @ {}:{},{}", frame->function_name ? frame->function_name->utf8_string() : ""_string, source_range.filename(), source_range.start.line, source_range.start.column);
auto unrealized_source_range = frame->executable->source_range_at(frame->program_counter.value());
if (unrealized_source_range.has_value()) {
auto source_range = unrealized_source_range->realize();
dbgln("-> {} @ {}:{},{}", frame->function_name ? frame->function_name->utf8_string() : ""_string, source_range.filename(), source_range.start.line, source_range.start.column);
continue;
}
}
if (frame->executable) {
dbgln("-> {} @ {}", frame->function_name ? frame->function_name->utf8_string() : ""_string, frame->executable->source_code->filename());
} else {
dbgln("-> {}", frame->function_name ? frame->function_name->utf8_string() : ""_string);
}
Expand Down

0 comments on commit 0670da0

Please sign in to comment.