Skip to content

Commit 9d6ee35

Browse files
committed
avoid overflow on functionality to print backtrace of safepoint straggler (JuliaLang#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 a85728e commit 9d6ee35

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ void jl_gc_wait_for_the_world(jl_ptls_t* gc_all_tls_states, int gc_n_threads)
254254
// We're currently also using atomic store release in mutator threads
255255
// (in jl_gc_state_set), but we may want to use signals to flush the
256256
// memory operations on those threads lazily instead.
257-
const int64_t timeout = jl_options.timeout_for_safepoint_straggler_s * 1000000000; // convert to nanoseconds
257+
const int64_t timeout = jl_options.timeout_for_safepoint_straggler_s * 1000000000LL; // convert to nanoseconds
258258
uint64_t t0 = jl_hrtime();
259259
while (!jl_atomic_load_relaxed(&ptls2->gc_state) || !jl_atomic_load_acquire(&ptls2->gc_state)) {
260260
jl_cpu_pause(); // yield?

test/threads_exec.jl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,25 +1317,27 @@ end
13171317
program = "
13181318
function main()
13191319
t = Threads.@spawn begin
1320-
ccall(:uv_sleep, Cvoid, (Cuint,), 5000)
1320+
ccall(:uv_sleep, Cvoid, (Cuint,), 20_000)
13211321
end
13221322
# Force a GC
1323-
ccall(:uv_sleep, Cvoid, (Cuint,), 1000)
1323+
ccall(:uv_sleep, Cvoid, (Cuint,), 1_000)
13241324
GC.gc()
13251325
wait(t)
13261326
end
13271327
main()
13281328
"
1329-
tmp_output_filename = tempname()
1330-
tmp_output_file = open(tmp_output_filename, "w")
1331-
if isnothing(tmp_output_file)
1332-
error("Failed to open file $tmp_output_filename")
1333-
end
1334-
run(pipeline(`$(Base.julia_cmd()) --threads=4 --timeout-for-safepoint-straggler=1 -e $program`, stderr=tmp_output_file))
1335-
# Check whether we printed the straggler's backtrace
1336-
@test !isempty(read(tmp_output_filename, String))
1337-
close(tmp_output_file)
1338-
rm(tmp_output_filename)
1329+
for timeout in ("1", "4", "16")
1330+
tmp_output_filename = tempname()
1331+
tmp_output_file = open(tmp_output_filename, "w")
1332+
if isnothing(tmp_output_file)
1333+
error("Failed to open file $tmp_output_filename")
1334+
end
1335+
run(pipeline(`$(Base.julia_cmd()) --threads=4 --timeout-for-safepoint-straggler=$(timeout) -e $program`, stderr=tmp_output_file))
1336+
# Check whether we printed the straggler's backtrace
1337+
@test !isempty(read(tmp_output_filename, String))
1338+
close(tmp_output_file)
1339+
rm(tmp_output_filename)
1340+
end
13391341
end
13401342

13411343
end # main testset

0 commit comments

Comments
 (0)