Skip to content

Commit 9cafd35

Browse files
authored
avoid overflow on functionality to print backtrace of safepoint straggler (#57579)
In the line of C code: ```C const int64_t timeout = jl_options.timeout_for_safepoint_straggler_s * 1000000000; ``` `jl_options.timeout_for_safepoint_straggler_s` is an `int16_t` and `1000000000` is an `int32_t`. The result of `jl_options.timeout_for_safepoint_straggler_s * 1000000000` will be an `int32_t` which may not be large enough to hold the value of `jl_options.timeout_for_safepoint_straggler_s` after converting to nanoseconds, leading to overflow.
1 parent 7fa0c13 commit 9cafd35

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/safepoint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void jl_gc_wait_for_the_world(jl_ptls_t* gc_all_tls_states, int gc_n_threads)
157157
uv_mutex_unlock(&safepoint_lock);
158158
}
159159
else {
160-
const int64_t timeout = jl_options.timeout_for_safepoint_straggler_s * 1000000000; // convert to nanoseconds
160+
const int64_t timeout = jl_options.timeout_for_safepoint_straggler_s * 1000000000LL; // convert to nanoseconds
161161
int ret = 0;
162162
uv_mutex_lock(&safepoint_lock);
163163
if (!jl_atomic_load_relaxed(&ptls2->gc_state)) {

test/threads_exec.jl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,25 +1630,27 @@ end
16301630
program = "
16311631
function main()
16321632
t = Threads.@spawn begin
1633-
ccall(:uv_sleep, Cvoid, (Cuint,), 5000)
1633+
ccall(:uv_sleep, Cvoid, (Cuint,), 20_000)
16341634
end
16351635
# Force a GC
1636-
ccall(:uv_sleep, Cvoid, (Cuint,), 1000)
1636+
ccall(:uv_sleep, Cvoid, (Cuint,), 1_000)
16371637
GC.gc()
16381638
wait(t)
16391639
end
16401640
main()
16411641
"
1642-
tmp_output_filename = tempname()
1643-
tmp_output_file = open(tmp_output_filename, "w")
1644-
if isnothing(tmp_output_file)
1645-
error("Failed to open file $tmp_output_filename")
1646-
end
1647-
run(pipeline(`$(Base.julia_cmd()) --threads=4 --timeout-for-safepoint-straggler=1 -e $program`, stderr=tmp_output_file))
1648-
# Check whether we printed the straggler's backtrace
1649-
@test !isempty(read(tmp_output_filename, String))
1650-
close(tmp_output_file)
1651-
rm(tmp_output_filename)
1642+
for timeout in ("1", "4", "16")
1643+
tmp_output_filename = tempname()
1644+
tmp_output_file = open(tmp_output_filename, "w")
1645+
if isnothing(tmp_output_file)
1646+
error("Failed to open file $tmp_output_filename")
1647+
end
1648+
run(pipeline(`$(Base.julia_cmd()) --threads=4 --timeout-for-safepoint-straggler=$(timeout) -e $program`, stderr=tmp_output_file))
1649+
# Check whether we printed the straggler's backtrace
1650+
@test !isempty(read(tmp_output_filename, String))
1651+
close(tmp_output_file)
1652+
rm(tmp_output_filename)
1653+
end
16521654
end
16531655

16541656
end # main testset

0 commit comments

Comments
 (0)