JuliaTelecom / UHDBindings.jl

Julia C bindings for UHD to monitor USRP devices.
MIT License
5 stars 3 forks source link

Unsafe usage of pointers #8

Open mgw93 opened 1 year ago

mgw93 commented 1 year ago

I noticed that there are several instances where pointers obtained from unsafe_convert are used without preserving the underlying object from garbage collection.

E.g. in the uhd_usrp_create_stream function.

I’m pretty new to Julia myself, but my understanding is that this can lead to bugs when the garbage collector removes an object before the pointer is used.

RGerzaguet commented 1 year ago

Thanks for the feedback, This is true than unsafe_load can lead to un-protected memory but AFAIK it is prevented by the use of the Ref{} in the structure as it stores the result of the unsafe_load. So It should not lead to seg fault that way (even the approach here is unsafe :)). Did you encounter any problem of GC-ed variables with UHDBindings ?

mgw93 commented 1 year ago

I have not experienced any actual issues. However, the nature of memory safety issues is that they often only manifest themselves in rare circumstances. Most of the time (or possibly always until someone changes the implementation) the garbage collector does not get in the way. But unless it’s guaranteed that it won’t happen, it’s still an issue.

I’m still new to Julia and may therefore be misunderstanding the guarantees that are provided by the language, but the way I see it, if the garbage collector was triggered on line 126 of Rx.jl, it would be allowed to collect cpu_format, otw_format, args, and channels, all of which need to still be alive for the call to uhd_usrp_get_rx_stream on line 130. The fact that the variables are still in scope does not help, as the compiler can determine that they are not used (directly) anymore.