overdrivenpotato / rust-vst2

VST 2.4 API implementation in rust. Create plugins or hosts.
MIT License
223 stars 23 forks source link

Global LOAD_POINTER in call_main will leak host for multiple assignments. #46

Open labyrinth-ssr opened 1 year ago

labyrinth-ssr commented 1 year ago

https://github.com/overdrivenpotato/rust-vst2/blob/244e14bd28caff3b21aa27f26a57bf01f01b7780/src/host.rs#L340-L344

with Box::into_raw(Box::new(self.host.clone())), the pointee is on the heap. Multiple assignments will cause the old value to leak.

Probable fix is like: If call_main should only be called once, adding an Atomic to guarantee assigning only once.

const UNINITIALIZED: usize = 0;
const INITIALIZING: usize = 1;
const INITIALIZED: usize = 2;
static GLOBAL_INIT: AtomicUsize = AtomicUsize::new(UNINITIALIZED);
pub struct SetGlobalDefaultError {
    _no_construct: (),
}

unsafe fn call_main(&mut self) -> Result<*mut AEffect, SetGlobalDefaultError>  {
        if GLOBAL_INIT
                .compare_exchange(
                    UNINITIALIZED,
                    INITIALIZING,
                    Ordering::SeqCst,
                    Ordering::SeqCst,
                )
                .is_ok()
            {
              LOAD_POINTER = Box::into_raw(Box::new(self.host.clone())) as *mut c_void;
              (self.main)(callback_wrapper::<T>)
            }  else {
              Err(SetGlobalDefaultError { _no_construct: () })
            }
} 

Otherwise change the else branch to:

           else {
              drop(Box::from_raw(LOAD_POINTER));
              LOAD_POINTER = Box::into_raw(Box::new(self.host.clone())) as *mut c_void;
              (self.main)(callback_wrapper::<T>)
          }