Skip to content

Commit ccb0211

Browse files
authored
Check that malformed allocations throw and don't stackoverflow (#576)
1 parent da32c2b commit ccb0211

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/KernelAbstractions.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,15 @@ Allocate a storage array appropriate for the computational backend.
533533
!!! note
534534
Backend implementations **must** implement `allocate(::NewBackend, T, dims::Tuple)`
535535
"""
536-
allocate(backend::Backend, T, dims...) = allocate(backend, T, dims)
537-
allocate(backend::Backend, T, dims::Tuple) = throw(MethodError(allocate, (backend, T, dims)))
536+
allocate(backend::Backend, T::Type, dims...) = allocate(backend, T, dims)
537+
allocate(backend::Backend, T::Type, dims::Tuple) = throw(MethodError(allocate, (backend, T, dims)))
538538

539539
"""
540540
zeros(::Backend, Type, dims...)::AbstractArray
541541
542542
Allocate a storage array appropriate for the computational backend filled with zeros.
543543
"""
544-
zeros(backend::Backend, T, dims...) = zeros(backend, T, dims)
544+
zeros(backend::Backend, T::Type, dims...) = zeros(backend, T, dims)
545545
function zeros(backend::Backend, ::Type{T}, dims::Tuple) where {T}
546546
data = allocate(backend, T, dims...)
547547
fill!(data, zero(T))
@@ -553,7 +553,7 @@ end
553553
554554
Allocate a storage array appropriate for the computational backend filled with ones.
555555
"""
556-
ones(backend::Backend, T, dims...) = ones(backend, T, dims)
556+
ones(backend::Backend, T::Type, dims...) = ones(backend, T, dims)
557557
function ones(backend::Backend, ::Type{T}, dims::Tuple) where {T}
558558
data = allocate(backend, T, dims)
559559
fill!(data, one(T))

test/test.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ function unittest_testsuite(Backend, backend_str, backend_mod, BackendArrayT; sk
307307
@test size(KernelAbstractions.zeros(backend, Float32, 0, 9)) == (0, 9)
308308
end
309309

310+
@testset "Malformed allocations" begin
311+
backend = Backend()
312+
@test_throws MethodError KernelAbstractions.zeros(backend, 2, 2)
313+
@test_throws MethodError KernelAbstractions.ones(backend, 2, 2)
314+
@test_throws MethodError KernelAbstractions.allocate(backend, 2, 2)
315+
end
316+
310317
@kernel cpu = false function gpu_return_kernel!(x)
311318
i = @index(Global)
312319
if i (length(x) ÷ 2)

0 commit comments

Comments
 (0)