Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GC-safe regions around ssl_read and ssl_write #265

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/ssl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,23 @@ closed.
https://tls.mbed.org/api/ssl_8h.html#aa2c29eeb1deaf5ad9f01a7515006ede5
"""
function ssl_read(ctx::SSLContext, ptr, n)::Int
ret = 0
@lockdata ctx begin
return ccall((:mbedtls_ssl_read, libmbedtls), Cint,
(Ptr{Cvoid}, Ptr{Cvoid}, Csize_t),
ctx.data, ptr, n)
# We want to allow GC to run while this thread is in the `ccall`.
# When https://github.com/JuliaLang/julia/pull/49933 is completed
# and lands, this should be changed to what is required by that.
ccd = Base.cconvert(Ptr{Cvoid}, ctx.data)
cptr = Base.cconvert(Ptr{Cvoid}, ptr)
GC.@preserve ccd cptr begin
ucd = Base.unsafe_convert(Ptr{Cvoid}, ccd)::Ptr{Cvoid}
ucptr = Base.unsafe_convert(Ptr{Cvoid}, cptr)::Ptr{Cvoid}
gc_state = @ccall(jl_gc_safe_enter()::Int8)
ret = ccall((:mbedtls_ssl_read, libmbedtls), Cint,
(Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), ucd, ucptr, n)
@ccall(jl_gc_safe_leave(gc_state::Int8)::Cvoid)
end
end
return ret
end

"""
Expand All @@ -726,11 +738,23 @@ connection; the current connection must be closed.
https://tls.mbed.org/api/ssl_8h.html#a5bbda87d484de82df730758b475f32e5
"""
function ssl_write(ctx::SSLContext, ptr, n)::Int
ret = 0
@lockdata ctx begin
return ccall((:mbedtls_ssl_write, libmbedtls), Cint,
(Ptr{Cvoid}, Ptr{Cvoid}, Csize_t),
ctx.data, ptr, n)
# We want to allow GC to run while this thread is in the `ccall`.
# When https://github.com/JuliaLang/julia/pull/49933 is completed
# and lands, this should be changed to what is required by that.
ccd = Base.cconvert(Ptr{Cvoid}, ctx.data)
cptr = Base.cconvert(Ptr{Cvoid}, ptr)
GC.@preserve ccd cptr begin
ucd = Base.unsafe_convert(Ptr{Cvoid}, ccd)::Ptr{Cvoid}
ucptr = Base.unsafe_convert(Ptr{Cvoid}, cptr)::Ptr{Cvoid}
gc_state = @ccall(jl_gc_safe_enter()::Int8)
ret = ccall((:mbedtls_ssl_write, libmbedtls), Cint,
(Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), ucd, ucptr, n)
@ccall(jl_gc_safe_leave(gc_state::Int8)::Cvoid)
end
end
return ret
end

"""
Expand Down