Skip to content

Commit d8efc91

Browse files
committed
Merge pull request #12185 from JuliaLang/yyc/gc-debug
Make sure the object is still alive during unsafe_load in ccall tests
2 parents fc31ef9 + 300413b commit d8efc91

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

test/ccall.jl

+23-12
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,41 @@ ccall_test_func(x) = ccall((:testUcharX, "./libccalltest"), Int32, (UInt8,), x %
1111

1212
# Test for proper round-trip of Ref{T} type
1313
ccall_echo_func{T,U}(x, ::Type{T}, ::Type{U}) = ccall((:test_echo_p, "./libccalltest"), T, (U,), x)
14+
# Make sure object x is still valid (rooted as argument)
15+
# when loading the pointer. This works as long as we still keep the argument
16+
# rooted but might fail if we are smarter about eliminating dead root.
17+
ccall_echo_load{T,U}(x, ::Type{T}, ::Type{U}) =
18+
unsafe_load(ccall_echo_func(x, T, U))
19+
ccall_echo_objref{T,U}(x, ::Type{T}, ::Type{U}) =
20+
unsafe_pointer_to_objref(ccall_echo_func(x, Ptr{T}, U))
1421
type IntLike
1522
x::Int
1623
end
17-
@test unsafe_load(ccall_echo_func(132, Ptr{Int}, Ref{Int})) === 132
18-
@test unsafe_load(ccall_echo_func(Ref(921), Ptr{Int}, Ref{Int})) === 921
19-
@test unsafe_load(ccall_echo_func(IntLike(993), Ptr{Int}, Ref{IntLike})) === 993
20-
@test unsafe_load(ccall_echo_func(IntLike(881), Ptr{IntLike}, Ref{IntLike})).x === 881
24+
@test ccall_echo_load(132, Ptr{Int}, Ref{Int}) === 132
25+
@test ccall_echo_load(Ref(921), Ptr{Int}, Ref{Int}) === 921
26+
@test ccall_echo_load(IntLike(993), Ptr{Int}, Ref{IntLike}) === 993
27+
@test ccall_echo_load(IntLike(881), Ptr{IntLike}, Ref{IntLike}).x === 881
2128
@test ccall_echo_func(532, Int, Int) === 532
2229
if WORD_SIZE == 64
2330
# this test is valid only for x86_64 and win64
2431
@test ccall_echo_func(164, IntLike, Int).x === 164
2532
end
2633
@test ccall_echo_func(IntLike(828), Int, IntLike) === 828
2734
@test ccall_echo_func(913, Any, Any) === 913
28-
@test unsafe_pointer_to_objref(ccall_echo_func(553, Ptr{Any}, Any)) === 553
35+
@test ccall_echo_objref(553, Ptr{Any}, Any) === 553
2936
@test ccall_echo_func(124, Ref{Int}, Any) === 124
30-
@test unsafe_load(ccall_echo_func(422, Ptr{Any}, Ref{Any})) === 422
31-
@test unsafe_load(ccall_echo_func([383], Ptr{Int}, Ref{Int})) === 383
32-
@test unsafe_load(ccall_echo_func(Ref([144,172],2), Ptr{Int}, Ref{Int})) === 172
33-
#@test unsafe_load(ccall_echo_func(Ref([8],1,1), Ptr{Int}, Ref{Int})) === 8
37+
@test ccall_echo_load(422, Ptr{Any}, Ref{Any}) === 422
38+
@test ccall_echo_load([383], Ptr{Int}, Ref{Int}) === 383
39+
@test ccall_echo_load(Ref([144,172],2), Ptr{Int}, Ref{Int}) === 172
40+
# @test ccall_echo_load(Ref([8],1,1), Ptr{Int}, Ref{Int}) === 8
3441

3542
# Tests for passing and returning structs
3643
ci = 20+51im
3744
b = ccall((:ctest, "./libccalltest"), Complex{Int}, (Complex{Int},), ci)
3845
@test b == ci + 1 - 2im
39-
b = unsafe_load(ccall((:cptest, "./libccalltest"), Ptr{Complex{Int}}, (Ptr{Complex{Int}},), [ci]))
46+
ci_ary = [ci] # Make sure the array is alive during unsafe_load
47+
b = unsafe_load(ccall((:cptest, "./libccalltest"), Ptr{Complex{Int}},
48+
(Ptr{Complex{Int}},), ci_ary))
4049
@test b == ci + 1 - 2im
4150
@test ci == 20+51im
4251

@@ -47,14 +56,16 @@ Libc.free(convert(Ptr{Void},b))
4756
cf64 = 2.84+5.2im
4857
b = ccall((:cgtest, "./libccalltest"), Complex128, (Complex128,), cf64)
4958
@test b == cf64 + 1 - 2im
50-
b = unsafe_load(ccall((:cgptest, "./libccalltest"), Ptr{Complex128}, (Ptr{Complex128},), [cf64]))
59+
cf64_ary = [cf64] # Make sure the array is alive during unsafe_load
60+
b = unsafe_load(ccall((:cgptest, "./libccalltest"), Ptr{Complex128}, (Ptr{Complex128},), cf64_ary))
5161
@test b == cf64 + 1 - 2im
5262
@test cf64 == 2.84+5.2im
5363

5464
cf32 = 3.34f0+53.2f0im
5565
b = ccall((:cftest, "./libccalltest"), Complex64, (Complex64,), cf32)
5666
@test b == cf32 + 1 - 2im
57-
b = unsafe_load(ccall((:cfptest, "./libccalltest"), Ptr{Complex64}, (Ptr{Complex64},), [cf32]))
67+
cf32_ary = [cf32] # Make sure the array is alive during unsafe_load
68+
b = unsafe_load(ccall((:cfptest, "./libccalltest"), Ptr{Complex64}, (Ptr{Complex64},), cf32_ary))
5869
@test b == cf32 + 1 - 2im
5970
@test cf32 == 3.34f0+53.2f0im
6071

0 commit comments

Comments
 (0)