Skip to content

Commit 054fcd1

Browse files
committed
Check that malformed allocations throw and don't stackoverflow (#576)
(cherry picked from commit ccb0211)
1 parent 087d9fa commit 054fcd1

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/KernelAbstractions.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -525,15 +525,15 @@ Allocate a storage array appropriate for the computational backend.
525525
!!! note
526526
Backend implementations **must** implement `allocate(::NewBackend, T, dims::Tuple)`
527527
"""
528-
allocate(backend::Backend, T, dims...) = allocate(backend, T, dims)
529-
allocate(backend::Backend, T, dims::Tuple) = throw(MethodError(allocate, (backend, T, dims)))
528+
allocate(backend::Backend, T::Type, dims...) = allocate(backend, T, dims)
529+
allocate(backend::Backend, T::Type, dims::Tuple) = throw(MethodError(allocate, (backend, T, dims)))
530530

531531
"""
532532
zeros(::Backend, Type, dims...)::AbstractArray
533533
534534
Allocate a storage array appropriate for the computational backend filled with zeros.
535535
"""
536-
zeros(backend::Backend, T, dims...) = zeros(backend, T, dims)
536+
zeros(backend::Backend, T::Type, dims...) = zeros(backend, T, dims)
537537
function zeros(backend::Backend, ::Type{T}, dims::Tuple) where {T}
538538
data = allocate(backend, T, dims...)
539539
fill!(data, zero(T))
@@ -545,7 +545,7 @@ end
545545
546546
Allocate a storage array appropriate for the computational backend filled with ones.
547547
"""
548-
ones(backend::Backend, T, dims...) = ones(backend, T, dims)
548+
ones(backend::Backend, T::Type, dims...) = ones(backend, T, dims)
549549
function ones(backend::Backend, ::Type{T}, dims::Tuple) where {T}
550550
data = allocate(backend, T, dims)
551551
fill!(data, one(T))

test/test.jl

+7
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,13 @@ function unittest_testsuite(Backend, backend_str, backend_mod, BackendArrayT; sk
280280
@test size(KernelAbstractions.zeros(backend, Float32, 0, 9)) == (0, 9)
281281
end
282282

283+
@testset "Malformed allocations" begin
284+
backend = Backend()
285+
@test_throws MethodError KernelAbstractions.zeros(backend, 2, 2)
286+
@test_throws MethodError KernelAbstractions.ones(backend, 2, 2)
287+
@test_throws MethodError KernelAbstractions.allocate(backend, 2, 2)
288+
end
289+
283290
@kernel cpu = false function gpu_return_kernel!(x)
284291
i = @index(Global)
285292
if i (length(x) ÷ 2)

0 commit comments

Comments
 (0)