Skip to content

Commit

Permalink
Fix stack pointer retrieval in jl_backtrace_from_here (#42585)
Browse files Browse the repository at this point in the history
Co-authored-by: Jameson Nash <[email protected]>
  • Loading branch information
tkf and vtjnash authored Nov 18, 2021
1 parent 0ca6948 commit 455236e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/stackwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ static int jl_unw_stepn(bt_cursor_t *cursor, jl_bt_element_t *bt_data, size_t *b
from_signal_handler = 0;
continue;
}
if (sp)
sp[n] = thesp;
// For the purposes of looking up debug info for functions, we want
// to harvest addresses for the *call* instruction `call_ip` during
// stack walking. However, this information isn't directly
Expand Down Expand Up @@ -168,6 +166,8 @@ static int jl_unw_stepn(bt_cursor_t *cursor, jl_bt_element_t *bt_data, size_t *b
}
}
bt_entry->uintptr = call_ip;
if (sp)
sp[n] = thesp;
n++;
}
// NOTE: if we have some pgcstack entries remaining (because the
Expand Down Expand Up @@ -259,8 +259,8 @@ JL_DLLEXPORT jl_value_t *jl_backtrace_from_here(int returnsp, int skip)
jl_array_grow_end(ip, maxincr);
uintptr_t *sp_ptr = NULL;
if (returnsp) {
sp_ptr = (uintptr_t*)jl_array_data(sp) + offset;
jl_array_grow_end(sp, maxincr);
sp_ptr = (uintptr_t*)jl_array_data(sp) + offset;
}
size_t size_incr = 0;
have_more_frames = jl_unw_stepn(&cursor, (jl_bt_element_t*)jl_array_data(ip) + offset,
Expand Down
85 changes: 85 additions & 0 deletions test/backtrace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,88 @@ let code = """
@test occursin("InterpreterIP in top-level CodeInfo for Main.A", bt_str)
end

"""
_reformat_sp(bt_data...) -> sp::Vector{Ptr{Cvoid}}
Convert the output `bt_data` of `jl_backtrace_from_here` with `returnsp` flag set to a
vector of valid stack pointers `sp`; i.e., `sp` is a subset of `bt_data[3]`.
See also `Base._reformat_bt`.
"""
function _reformat_sp(
bt_raw::Array{Ptr{Cvoid},1},
bt2::Array{Any,1},
sp_raw::Array{Ptr{Cvoid},1},
)
bt = Base._reformat_bt(bt_raw, bt2)
sp = empty!(similar(sp_raw))
i = j = 0
while true
# Advance `i` such that `bt[i] isa Ptr{Cvoid}` (native pointer).
local ip
while true
if i == lastindex(bt)
return sp
end
i += 1
x = bt[i]
if x isa Ptr{Cvoid}
ip = x
break
end
end
# Advance `j` such that `bt_raw[j] == bt[i]` to find a valid stack pointer.
while true
if j == lastindex(bt_raw)
return sp
end
j += 1
if bt_raw[j] == ip
push!(sp, sp_raw[j])
break
end
end
end
end

"""
withframeaddress(f)
Call function `f` with an address `ptr::Ptr{Cvoid}` of an independent frame
immediately outer to `f`.
"""
withframeaddress
@eval @noinline function withframeaddress(f)
sp = Core.Intrinsics.llvmcall(
($"""
declare i8* @llvm.frameaddress(i32)
define private i$(Sys.WORD_SIZE) @frameaddr() {
%1 = call i8* @llvm.frameaddress(i32 0)
%2 = ptrtoint i8* %1 to i$(Sys.WORD_SIZE)
ret i$(Sys.WORD_SIZE) %2
}""", "frameaddr"),
UInt,
Tuple{},
)
@noinline f(Ptr{Cvoid}(sp))
end

function sandwiched_backtrace()
local ptr1, ptr2, bt
withframeaddress() do p1
ptr1 = p1
bt = ccall(:jl_backtrace_from_here, Ref{Base.SimpleVector}, (Cint, Cint), true, 0)
withframeaddress() do p2
ptr2 = p2
end
end
return ptr1, ptr2, bt
end

@testset "stack pointers" begin
ptr1, ptr2, bt_data = sandwiched_backtrace()
sp = _reformat_sp(bt_data...)
@test ptr2 < sp[2]
@test sp[1] < ptr1
@test all(diff(Int128.(UInt.(sp))) .> 0)
end

2 comments on commit 455236e

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your package evaluation job has completed - possible new issues were detected. A full report can be found here.

Please sign in to comment.