nagisa / rust_libloading

Bindings around the platform's dynamic library loading primitives with greatly improved memory safety.
https://docs.rs/libloading
ISC License
1.22k stars 100 forks source link

Question: Is it possible to inject functions into libraries dynamically? #126

Closed phenax closed 1 year ago

phenax commented 1 year ago

Dumb question: Is something like this possible?

// library.c
unsigned int injectable() { return 0; }
// main.rs
fn injected_func() -> u32 { 5 }

let lib = libloading::Library::new("/path/to/liblibrary.so")?;
let func: libloading::Symbol<*mut fn() -> u32> = lib.get(b"injectable");
if let Ok(func) = func {
  **func = injected_func;
}
phenax commented 1 year ago

Defining it as a pointer does the trick

unsigned int (*injectable)() { return 0; }