Fixed a bug that crashes the program when doing something very specific

It crashed while trying to do Config::default() aka probabbly allocate
into the very manually allocated struct (which is bad in rust)
This commit is contained in:
Gvidas Juknevičius 2024-06-23 14:15:51 +03:00
parent bd40dfe44a
commit 50782cd512
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
2 changed files with 34 additions and 15 deletions

View File

@ -1,5 +1,4 @@
#[macro_use] #[macro_use]
mod magic; mod magic;
mod c_buffer; mod c_buffer;
@ -11,7 +10,7 @@ pub use plugin_info::*;
pub use anyhow::Result; pub use anyhow::Result;
pub use context::*; pub use context::*;
pub trait DimPlugin { pub trait DimPlugin: Clone {
fn init(&mut self, ctx: Context); fn init(&mut self, ctx: Context);
fn pre_reload(&mut self); fn pre_reload(&mut self);
fn post_reload(&mut self); fn post_reload(&mut self);

View File

@ -7,7 +7,7 @@ macro_rules! plugin_info {
static ref PLUGIN_INFO: $crate::PluginInfo = $crate::PluginInfo::new($name, $version, $license); static ref PLUGIN_INFO: $crate::PluginInfo = $crate::PluginInfo::new($name, $version, $license);
); );
static mut PLUG: *mut $typ = std::ptr::null_mut() as *mut $typ; static mut PLUG: Option<$typ> = None as Option<$typ>;
#[no_mangle] #[no_mangle]
unsafe extern "C" fn plug_get_info() -> *const std::ffi::c_void { unsafe extern "C" fn plug_get_info() -> *const std::ffi::c_void {
@ -16,39 +16,59 @@ macro_rules! plugin_info {
#[no_mangle] #[no_mangle]
unsafe extern "C" fn plug_init(ctx: *const $crate::ContextRaw) { unsafe extern "C" fn plug_init(ctx: *const $crate::ContextRaw) {
PLUG = std::alloc::alloc(std::alloc::Layout::new::<$typ>()) as *mut $typ; //PLUG = std::alloc::alloc(std::alloc::Layout::new::<$typ>()) as *mut $typ;
*PLUG = Plug::new(); PLUG = Some(Plug::new());
let ctx = $crate::Context::new(ctx); let ctx = $crate::Context::new(ctx);
(&mut *PLUG).init(ctx); if let Some(p) = &mut PLUG {
p.init(ctx)
} else {
unreachable!();
}
} }
#[no_mangle] #[no_mangle]
unsafe extern "C" fn plug_pre_reload() -> *mut $typ { unsafe extern "C" fn plug_pre_reload() -> *mut $typ {
//TODO: Untested //TODO: Untested
(&mut *PLUG).pre_reload(); if let Some(p) = &mut PLUG {
return PLUG; p.pre_reload();
return p as *mut $typ;
}
unreachable!();
} }
#[no_mangle] #[no_mangle]
unsafe extern "C" fn plug_post_reload(state: *mut $typ) { unsafe extern "C" fn plug_post_reload(state: *mut $typ) {
//TODO: Untested PLUG = Some((*state).clone());
PLUG = state;
(&mut *PLUG).post_reload(); if let Some(p) = &mut PLUG {
p.post_reload();
} else {
unreachable!();
}
//TODO: Untested
} }
#[no_mangle] #[no_mangle]
unsafe extern "C" fn plug_poll(buf: *mut i8, len: std::ffi::c_uint) { unsafe extern "C" fn plug_poll(buf: *mut i8, len: std::ffi::c_uint) {
let mut buf = $crate::CBuffer::from_raw_parts_mut(buf, len as usize); let mut buf = $crate::CBuffer::from_raw_parts_mut(buf, len as usize);
if let Err(_e) = (&mut *PLUG).poll(&mut buf) { if let Some(p) = &mut PLUG {
// TODO: Handle error maybe? if let Err(_e) = p.poll(&mut buf) {
// TODO: Handle error maybe?
}
} else {
unreachable!();
} }
} }
#[no_mangle] #[no_mangle]
unsafe extern "C" fn plug_free() { unsafe extern "C" fn plug_free() {
std::alloc::dealloc(PLUG as *mut u8, std::alloc::Layout::new::<$typ>()); if let Some(p) = &mut PLUG {
(&mut *PLUG).free(); // std::alloc::dealloc(PLUG as *mut u8, std::alloc::Layout::new::<$typ>());
p.free();
} else {
unreachable!();
}
} }
}; };