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

Memory-safety and allocations fixes for Handle{T} #446

Merged
merged 1 commit into from
Jan 27, 2024

Conversation

sjdaines
Copy link
Contributor

@sjdaines sjdaines commented Jan 27, 2024

Addresses #435 and also fixes a potential problem with memory safety:

  1. Allocations in convert(Ptr{T}, Handle{T})
    Fix is as suggested in Allocations in convert(Ptr{T}, Handle{T}) #435
    (Handle is now a mutable struct with a ptr field)

  2. Memory safety robustness fix
    Remove convert and use paired Base.cconvert / Base.unsafe_convert to get the ptr field from
    a Handle object h, so that the h is preserved from GC across the ccall
    This fix is analogous to that for the NVector wrapper in PR Workaround for N_Vector segfaults #380
    (NB: there is no actual problem at least when Sundials is used with the SciML interface,
    as the Handle{CVODEMem} and similar objects are held by a persistent solver data structure, but this
    change should reduce the risk of something going wrong in the future, or for eg test harnesses that don't use
    the SciML interface)

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

Addresses SciML#435 and
also fixes a potential problem with memory safety:

1) Allocations in convert(Ptr{T}, Handle{T})
   Fix is as suggested in SciML#435
   (Handle is now a mutable struct with a ptr field)

2) Memory safety robustness fix
   Remove convert and use paired Base.cconvert / Base.unsafe_convert to get the ptr field from
   a Handle object h, so that the h is preserved from GC across the ccall
   This fix is analogous to that for the NVector wrapper in PR SciML#380
   (NB: there is no actual problem at least when Sundials is used with the SciML interface,
   as the Handle{CVODEMem} and similar objects are held by a persistent solver data structure, but this
   change should reduce the risk of something going wrong in the future, or for eg test harnesses that don't use
   the SciML interface)
Copy link

codecov bot commented Jan 27, 2024

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (c717d8b) 81.09% compared to head (31ca0a2) 81.67%.

Files Patch % Lines
src/handle.jl 88.88% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #446      +/-   ##
==========================================
+ Coverage   81.09%   81.67%   +0.57%     
==========================================
  Files          11       11              
  Lines        1476     1468       -8     
==========================================
+ Hits         1197     1199       +2     
+ Misses        279      269      -10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ChrisRackauckas ChrisRackauckas merged commit ab0e08f into SciML:master Jan 27, 2024
4 of 5 checks passed
@ChrisRackauckas
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants