Skip to content

Commit cf5957e

Browse files
authored
fix #32678, error due to Ptr constant in _reformat_bt (#33524)
Also change `jl_get_backtrace` to return a pair of values instead of mutating.
1 parent 2b1fc4c commit cf5957e

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

base/error.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function _reformat_bt(bt, bt2)
7070
i, j = 1, 1
7171
while i <= length(bt)
7272
ip = bt[i]::Ptr{Cvoid}
73-
if ip == Ptr{Cvoid}(-1%UInt)
73+
if UInt(ip) == (-1 % UInt)
7474
# The next one is really a CodeInfo
7575
push!(ret, InterpreterIP(
7676
bt2[j],
@@ -95,8 +95,8 @@ function backtrace()
9595
# skip frame for backtrace(). Note that for this to work properly,
9696
# backtrace() itself must not be interpreted nor inlined.
9797
skip = 1
98-
bt1, bt2 = ccall(:jl_backtrace_from_here, Any, (Cint,Cint), false, skip)
99-
_reformat_bt(bt1, bt2)
98+
bt1, bt2 = ccall(:jl_backtrace_from_here, Ref{SimpleVector}, (Cint, Cint), false, skip)
99+
return _reformat_bt(bt1::Vector{Ptr{Cvoid}}, bt2::Vector{Any})
100100
end
101101

102102
"""
@@ -105,10 +105,8 @@ end
105105
Get the backtrace of the current exception, for use within `catch` blocks.
106106
"""
107107
function catch_backtrace()
108-
bt = Ref{Any}(nothing)
109-
bt2 = Ref{Any}(nothing)
110-
ccall(:jl_get_backtrace, Cvoid, (Ref{Any}, Ref{Any}), bt, bt2)
111-
return _reformat_bt(bt[], bt2[])
108+
bt, bt2 = ccall(:jl_get_backtrace, Ref{SimpleVector}, ())
109+
return _reformat_bt(bt::Vector{Ptr{Cvoid}}, bt2::Vector{Any})
112110
end
113111

114112
"""

src/julia_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ size_t rec_backtrace_ctx(uintptr_t *bt_data, size_t maxsize, bt_context_t *ctx,
646646
#ifdef LIBOSXUNWIND
647647
size_t rec_backtrace_ctx_dwarf(uintptr_t *bt_data, size_t maxsize, bt_context_t *ctx, int add_interp_frames) JL_NOTSAFEPOINT;
648648
#endif
649-
JL_DLLEXPORT void jl_get_backtrace(jl_array_t **bt, jl_array_t **bt2);
649+
JL_DLLEXPORT jl_value_t *jl_get_backtrace(void);
650650
void jl_critical_error(int sig, bt_context_t *context, uintptr_t *bt_data, size_t *bt_size);
651651
JL_DLLEXPORT void jl_raise_debugger(void);
652652
int jl_getFunctionInfo(jl_frame_t **frames, uintptr_t pointer, int skipC, int noInline) JL_NOTSAFEPOINT;

src/stackwalk.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,17 @@ JL_DLLEXPORT jl_value_t *jl_backtrace_from_here(int returnsp, int skip)
240240
return bt;
241241
}
242242

243+
// note: btout and bt2out must be GC roots
243244
void decode_backtrace(uintptr_t *bt_data, size_t bt_size,
244245
jl_array_t **btout, jl_array_t **bt2out)
245246
{
246-
jl_array_t *bt = NULL;
247-
jl_array_t *bt2 = NULL;
248-
JL_GC_PUSH2(&bt, &bt2);
247+
jl_array_t *bt, *bt2;
249248
if (array_ptr_void_type == NULL) {
250249
array_ptr_void_type = jl_apply_type2((jl_value_t*)jl_array_type, (jl_value_t*)jl_voidpointer_type, jl_box_long(1));
251250
}
252-
bt = jl_alloc_array_1d(array_ptr_void_type, bt_size);
251+
bt = *btout = jl_alloc_array_1d(array_ptr_void_type, bt_size);
253252
memcpy(bt->data, bt_data, bt_size * sizeof(void*));
254-
bt2 = jl_alloc_array_1d(jl_array_any_type, 0);
253+
bt2 = *bt2out = jl_alloc_array_1d(jl_array_any_type, 0);
255254
// Scan the stack for any interpreter frames
256255
size_t n = 0;
257256
while (n < bt_size) {
@@ -261,12 +260,9 @@ void decode_backtrace(uintptr_t *bt_data, size_t bt_size,
261260
}
262261
n++;
263262
}
264-
*btout = bt;
265-
*bt2out = bt2;
266-
JL_GC_POP();
267263
}
268264

269-
JL_DLLEXPORT void jl_get_backtrace(jl_array_t **btout, jl_array_t **bt2out)
265+
JL_DLLEXPORT jl_value_t *jl_get_backtrace(void)
270266
{
271267
jl_excstack_t *s = jl_get_ptls_states()->current_task->excstack;
272268
uintptr_t *bt_data = NULL;
@@ -275,7 +271,12 @@ JL_DLLEXPORT void jl_get_backtrace(jl_array_t **btout, jl_array_t **bt2out)
275271
bt_data = jl_excstack_bt_data(s, s->top);
276272
bt_size = jl_excstack_bt_size(s, s->top);
277273
}
278-
decode_backtrace(bt_data, bt_size, btout, bt2out);
274+
jl_array_t *bt = NULL, *bt2 = NULL;
275+
JL_GC_PUSH2(&bt, &bt2);
276+
decode_backtrace(bt_data, bt_size, &bt, &bt2);
277+
jl_svec_t *pair = jl_svec2(bt, bt2);
278+
JL_GC_POP();
279+
return (jl_value_t*)pair;
279280
}
280281

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

0 commit comments

Comments
 (0)