Skip to content

Commit

Permalink
Add GC-safe regions around ssl_read and ssl_write
Browse files Browse the repository at this point in the history
  • Loading branch information
kpamnany committed Oct 26, 2023
1 parent e84f973 commit 34be0ef
Showing 1 changed file with 30 additions and 6 deletions.
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)
ccd = Base.cconvert(Ptr{Cvoid}, ctx.data)
cptr = Base.cconvert(Ptr{Cvoid}, ptr)
GC.@preserve ccd cptr begin
# Allow Julia to GC while reading/decrypting
gc_state = @ccall(jl_gc_safe_enter()::Int8)
ret = ccall((:mbedtls_ssl_read, libmbedtls), Cint,
(Ptr{Cvoid}, Ptr{Cvoid}, Csize_t),
Base.unsafe_convert(Ptr{Cvoid}, ccd)::Ptr{Cvoid},
Base.unsafe_convert(Ptr{Cvoid}, cptr)::Ptr{Cvoid},
n)
# Leave GC-safe region, waiting for GC to complete if it's running
@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)
ccd = Base.cconvert(Ptr{Cvoid}, ctx.data)
cptr = Base.cconvert(Ptr{Cvoid}, ptr)
GC.@preserve ccd cptr begin
# Allow Julia to GC while writing/encrypting
gc_state = @ccall(jl_gc_safe_enter()::Int8)
ret = ccall((:mbedtls_ssl_write, libmbedtls), Cint,
(Ptr{Cvoid}, Ptr{Cvoid}, Csize_t),
Base.unsafe_convert(Ptr{Cvoid}, ccd)::Ptr{Cvoid},
Base.unsafe_convert(Ptr{Cvoid}, cptr)::Ptr{Cvoid},
n)
# Leave GC-safe region, waiting for GC to complete if it's running
@ccall(jl_gc_safe_leave(gc_state::Int8)::Cvoid)
end
end
return ret
end

"""
Expand Down

0 comments on commit 34be0ef

Please sign in to comment.