nagisa / rust_libloading

Bindings around the platform's dynamic library loading primitives with greatly improved memory safety.

Home Page:https://docs.rs/libloading

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

about parameter and return value

huanghu578 opened this issue · comments

fn call_dynamic() -> Result<u32, Box> {
unsafe {
let lib = libloading::Library::new("/path/to/liblibrary.so")?;
let func: libloading::Symbol<unsafe extern fn() -> u32> = lib.get(b"my_func")?;
Ok(func())
}
}

How can I pass parameters into func ,and get a return value?

Please consult the documentation. There are examples such as this one which exemplify loading and calling of parametrised functions.

@nagisa
fn call_dynamic() -> Result<u32, Box> {
unsafe {
let mut value = [0; 256];
let lib = libloading::Library::new("/path/to/liblibrary.so")?;
let func: libloading::Symbol<unsafe extern fn(&str, &mut [u8; 256]) -> u32> = lib.get(b"my_func")?;
Ok(func("key", value))
}
}

in this way, the second param "value" will get 0xb which is invalid in my_func, BUT when I swap them to like this

let func: libloading::Symbol<unsafe extern fn(&mut [u8; 256], &str) -> u32> = lib.get(b"my_func")?;
the first param "value" could get right memory address in my_func, so what is the root cause of this issue? thanks a lot

Please ensure you're maintaining the soundness invariants of the get call. In particular &str is not FFI safe.