From ae4f1c1c3a05634f7b0a5a5b67469e20e136eee3 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Mon, 14 Aug 2023 12:37:22 -0300 Subject: [PATCH 1/4] Make cong safe in the presence of 0 --- src/julia_internal.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/julia_internal.h b/src/julia_internal.h index 7833ccfdc544e..a61951a53ec91 100644 --- a/src/julia_internal.h +++ b/src/julia_internal.h @@ -1220,6 +1220,8 @@ void jl_push_excstack(jl_excstack_t **stack JL_REQUIRE_ROOTED_SLOT JL_ROOTING_AR STATIC_INLINE uint64_t cong(uint64_t max, uint64_t *seed) JL_NOTSAFEPOINT { + if (max == 0) + return 0; uint64_t mask = ~(uint64_t)0; --max; mask >>= __builtin_clzll(max|1); From b02f50a01c16bc0fa991d4a722e9787f80658588 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Mon, 14 Aug 2023 12:45:51 -0300 Subject: [PATCH 2/4] Add a test and make it safer --- base/partr.jl | 2 +- test/threads.jl | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/base/partr.jl b/base/partr.jl index 68534f7a53979..0da3720504181 100644 --- a/base/partr.jl +++ b/base/partr.jl @@ -20,7 +20,7 @@ const heaps = [Vector{taskheap}(undef, 0), Vector{taskheap}(undef, 0)] const heaps_lock = [SpinLock(), SpinLock()] -cong(max::UInt32) = ccall(:jl_rand_ptls, UInt32, (UInt32,), max) + UInt32(1) +cong(max::UInt32) = max == UInt32(0) ? UInt32(0) : ccall(:jl_rand_ptls, UInt32, (UInt32,), max) + UInt32(1) function multiq_sift_up(heap::taskheap, idx::Int32) diff --git a/test/threads.jl b/test/threads.jl index 8189311739e31..90c9f1506dfbf 100644 --- a/test/threads.jl +++ b/test/threads.jl @@ -327,3 +327,7 @@ end @test_throws ArgumentError @macroexpand(@threads 1) # arg isn't an Expr @test_throws ArgumentError @macroexpand(@threads if true 1 end) # arg doesn't start with for end + +@testset "rand_ptls underflow" begin + @test Base.Partr.cong(UInt32(0)) == 0 +end \ No newline at end of file From a8a97685a1775f8f5c0e49df4ff6ec19bef311df Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Mon, 14 Aug 2023 13:12:35 -0300 Subject: [PATCH 3/4] Whitespace --- test/threads.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/threads.jl b/test/threads.jl index 90c9f1506dfbf..e705256bac652 100644 --- a/test/threads.jl +++ b/test/threads.jl @@ -330,4 +330,4 @@ end @testset "rand_ptls underflow" begin @test Base.Partr.cong(UInt32(0)) == 0 -end \ No newline at end of file +end From b4e733f667d8224f2bd8b9a357a0fb9a051d928a Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Tue, 15 Aug 2023 15:28:16 -0300 Subject: [PATCH 4/4] Update base/partr.jl Co-authored-by: Sukera <11753998+Seelengrab@users.noreply.github.com> --- base/partr.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/partr.jl b/base/partr.jl index 0da3720504181..295be17e69f3f 100644 --- a/base/partr.jl +++ b/base/partr.jl @@ -20,7 +20,7 @@ const heaps = [Vector{taskheap}(undef, 0), Vector{taskheap}(undef, 0)] const heaps_lock = [SpinLock(), SpinLock()] -cong(max::UInt32) = max == UInt32(0) ? UInt32(0) : ccall(:jl_rand_ptls, UInt32, (UInt32,), max) + UInt32(1) +cong(max::UInt32) = iszero(max) ? UInt32(0) : ccall(:jl_rand_ptls, UInt32, (UInt32,), max) + UInt32(1) function multiq_sift_up(heap::taskheap, idx::Int32)