Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception::CallStack: avoid allocations in LibC.dl_iterate_phdr #12625

Merged
merged 1 commit into from
Nov 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/exception/call_stack/elf.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ require "crystal/elf"

struct Exception::CallStack
protected def self.load_debug_info_impl
base_address : LibC::Elf_Addr = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid using a TypeDeclaration here unless necessary:

Suggested change
base_address : LibC::Elf_Addr = 0
base_address = LibC::Elf_Addr.zero

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Later we're passing a pointer to this variable. So IMO it makes sense to pin its type in order to prevent an accidental union type tainting the pointer.

phdr_callback = LibC::DlPhdrCallback.new do |info, size, data|
# The first entry is the header for the current program
read_dwarf_sections(info.value.addr)
# The first entry is the header for the current program.
# Note that we avoid allocating here and just store the base address
# to be passed to self.read_dwarf_sections when dl_iterate_phdr returns.
# Calling self.read_dwarf_sections from this callback may lead to reallocations
# and deadlocks due to the internal lock held by dl_iterate_phdr (#10084).
data.as(Pointer(LibC::Elf_Addr)).value = info.value.addr
1
end

# GC needs to be disabled around dl_iterate_phdr in freebsd (#10084)
{% if flag?(:freebsd) %} GC.disable {% end %}
LibC.dl_iterate_phdr(phdr_callback, nil)
{% if flag?(:freebsd) %} GC.enable {% end %}
LibC.dl_iterate_phdr(phdr_callback, pointerof(base_address))
self.read_dwarf_sections(base_address)
end

protected def self.read_dwarf_sections(base_address = 0)
Expand Down