Skip to content

Commit

Permalink
fix #32678, error due to Ptr constant in _reformat_bt (#33524)
Browse files Browse the repository at this point in the history
Also change `jl_get_backtrace` to return a pair of values instead of
mutating.
  • Loading branch information
JeffBezanson authored Oct 11, 2019
1 parent 2b1fc4c commit cf5957e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
12 changes: 5 additions & 7 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function _reformat_bt(bt, bt2)
i, j = 1, 1
while i <= length(bt)
ip = bt[i]::Ptr{Cvoid}
if ip == Ptr{Cvoid}(-1%UInt)
if UInt(ip) == (-1 % UInt)
# The next one is really a CodeInfo
push!(ret, InterpreterIP(
bt2[j],
Expand All @@ -95,8 +95,8 @@ function backtrace()
# skip frame for backtrace(). Note that for this to work properly,
# backtrace() itself must not be interpreted nor inlined.
skip = 1
bt1, bt2 = ccall(:jl_backtrace_from_here, Any, (Cint,Cint), false, skip)
_reformat_bt(bt1, bt2)
bt1, bt2 = ccall(:jl_backtrace_from_here, Ref{SimpleVector}, (Cint, Cint), false, skip)
return _reformat_bt(bt1::Vector{Ptr{Cvoid}}, bt2::Vector{Any})
end

"""
Expand All @@ -105,10 +105,8 @@ end
Get the backtrace of the current exception, for use within `catch` blocks.
"""
function catch_backtrace()
bt = Ref{Any}(nothing)
bt2 = Ref{Any}(nothing)
ccall(:jl_get_backtrace, Cvoid, (Ref{Any}, Ref{Any}), bt, bt2)
return _reformat_bt(bt[], bt2[])
bt, bt2 = ccall(:jl_get_backtrace, Ref{SimpleVector}, ())
return _reformat_bt(bt::Vector{Ptr{Cvoid}}, bt2::Vector{Any})
end

"""
Expand Down
2 changes: 1 addition & 1 deletion src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ size_t rec_backtrace_ctx(uintptr_t *bt_data, size_t maxsize, bt_context_t *ctx,
#ifdef LIBOSXUNWIND
size_t rec_backtrace_ctx_dwarf(uintptr_t *bt_data, size_t maxsize, bt_context_t *ctx, int add_interp_frames) JL_NOTSAFEPOINT;
#endif
JL_DLLEXPORT void jl_get_backtrace(jl_array_t **bt, jl_array_t **bt2);
JL_DLLEXPORT jl_value_t *jl_get_backtrace(void);
void jl_critical_error(int sig, bt_context_t *context, uintptr_t *bt_data, size_t *bt_size);
JL_DLLEXPORT void jl_raise_debugger(void);
int jl_getFunctionInfo(jl_frame_t **frames, uintptr_t pointer, int skipC, int noInline) JL_NOTSAFEPOINT;
Expand Down
21 changes: 11 additions & 10 deletions src/stackwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,17 @@ JL_DLLEXPORT jl_value_t *jl_backtrace_from_here(int returnsp, int skip)
return bt;
}

// note: btout and bt2out must be GC roots
void decode_backtrace(uintptr_t *bt_data, size_t bt_size,
jl_array_t **btout, jl_array_t **bt2out)
{
jl_array_t *bt = NULL;
jl_array_t *bt2 = NULL;
JL_GC_PUSH2(&bt, &bt2);
jl_array_t *bt, *bt2;
if (array_ptr_void_type == NULL) {
array_ptr_void_type = jl_apply_type2((jl_value_t*)jl_array_type, (jl_value_t*)jl_voidpointer_type, jl_box_long(1));
}
bt = jl_alloc_array_1d(array_ptr_void_type, bt_size);
bt = *btout = jl_alloc_array_1d(array_ptr_void_type, bt_size);
memcpy(bt->data, bt_data, bt_size * sizeof(void*));
bt2 = jl_alloc_array_1d(jl_array_any_type, 0);
bt2 = *bt2out = jl_alloc_array_1d(jl_array_any_type, 0);
// Scan the stack for any interpreter frames
size_t n = 0;
while (n < bt_size) {
Expand All @@ -261,12 +260,9 @@ void decode_backtrace(uintptr_t *bt_data, size_t bt_size,
}
n++;
}
*btout = bt;
*bt2out = bt2;
JL_GC_POP();
}

JL_DLLEXPORT void jl_get_backtrace(jl_array_t **btout, jl_array_t **bt2out)
JL_DLLEXPORT jl_value_t *jl_get_backtrace(void)
{
jl_excstack_t *s = jl_get_ptls_states()->current_task->excstack;
uintptr_t *bt_data = NULL;
Expand All @@ -275,7 +271,12 @@ JL_DLLEXPORT void jl_get_backtrace(jl_array_t **btout, jl_array_t **bt2out)
bt_data = jl_excstack_bt_data(s, s->top);
bt_size = jl_excstack_bt_size(s, s->top);
}
decode_backtrace(bt_data, bt_size, btout, bt2out);
jl_array_t *bt = NULL, *bt2 = NULL;
JL_GC_PUSH2(&bt, &bt2);
decode_backtrace(bt_data, bt_size, &bt, &bt2);
jl_svec_t *pair = jl_svec2(bt, bt2);
JL_GC_POP();
return (jl_value_t*)pair;
}

// Return data from the exception stack for `task` as an array of Any, starting
Expand Down

0 comments on commit cf5957e

Please sign in to comment.