mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-08 22:38:01 -06:00
Module object
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
#![allow(improper_ctypes_definitions)]
|
||||||
|
|
||||||
use log::{LevelFilter, Log, SetLoggerError};
|
use log::{LevelFilter, Log, SetLoggerError};
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
# cargo-features = ["trim-paths"]
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "unshell-module-load"
|
name = "unshell-module-load"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
@@ -9,3 +11,12 @@ log = "0.4.28"
|
|||||||
pretty_env_logger = "0.5.0"
|
pretty_env_logger = "0.5.0"
|
||||||
unshell-logger = {path = "../unshell-logger"}
|
unshell-logger = {path = "../unshell-logger"}
|
||||||
unshell-modules = {path = "../unshell-modules"}
|
unshell-modules = {path = "../unshell-modules"}
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
strip = true # Strip symbols from the binary
|
||||||
|
opt-level = "z" # Optimize for size
|
||||||
|
lto = true # Link tree optimization
|
||||||
|
codegen-units = 1
|
||||||
|
panic = "abort"
|
||||||
|
debug = false # Remove debug
|
||||||
|
# trim-paths="all"
|
||||||
|
|||||||
@@ -1,23 +1,10 @@
|
|||||||
use std::any::Any;
|
#[macro_use]
|
||||||
|
extern crate log;
|
||||||
|
|
||||||
use libloading::{Library, Symbol};
|
use libloading::{Library, Symbol};
|
||||||
use log::{info, warn};
|
use log::{info, warn};
|
||||||
use unshell_logger::SetupLogger;
|
use unshell_logger::SetupLogger;
|
||||||
use unshell_modules::{module, module_interface};
|
use unshell_modules::module_interface;
|
||||||
// use unshell_modules::IntoFunctionPtr;
|
|
||||||
// use unshell_modules::test;
|
|
||||||
// use unshell_modules::ExportFunction;
|
|
||||||
|
|
||||||
// fn test1() {
|
|
||||||
// warn!("Test1 not called");
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn test2() {
|
|
||||||
// warn!("Test2 not called");
|
|
||||||
// }
|
|
||||||
// fn test3() {
|
|
||||||
// warn!("Test3 not called");
|
|
||||||
// }
|
|
||||||
|
|
||||||
module_interface! {
|
module_interface! {
|
||||||
Interface {
|
Interface {
|
||||||
@@ -27,35 +14,70 @@ module_interface! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
#[allow(dead_code)]
|
||||||
// println!("Hello, world!");
|
#[derive(Debug)]
|
||||||
|
enum ModuleError {
|
||||||
|
LibLoadingError(libloading::Error),
|
||||||
|
LinkError(String),
|
||||||
|
}
|
||||||
|
|
||||||
// test();
|
struct Module {
|
||||||
|
name: String,
|
||||||
|
lib: Library,
|
||||||
|
}
|
||||||
|
|
||||||
pretty_env_logger::init();
|
impl Module {
|
||||||
|
pub fn new(path: &str) -> Result<Self, ModuleError> {
|
||||||
|
let lib = unsafe { Library::new(&path) }.map_err(|e| ModuleError::LibLoadingError(e))?;
|
||||||
|
|
||||||
warn!("Warning message");
|
Ok(Self {
|
||||||
|
name: path.to_owned(),
|
||||||
|
lib,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
pub fn get_symbol<T>(&self, symbol: &[u8]) -> Result<Symbol<'_, T>, ModuleError> {
|
||||||
|
let symbol = unsafe { self.lib.get::<T>(symbol) }
|
||||||
|
.map_err(|e| ModuleError::LinkError(format!("Failed to load symbol: {}", e)))?;
|
||||||
|
|
||||||
unsafe {
|
Ok(symbol)
|
||||||
let lib = Library::new("../unshell-module-test/target/release/libunshell_module_test.so")
|
}
|
||||||
.unwrap();
|
pub fn init_logger(&self) {
|
||||||
|
if let Ok(setup_logger) = self.get_symbol::<SetupLogger>(b"setup_logger") {
|
||||||
let ret = lib.get::<SetupLogger>(b"setup_logger");
|
|
||||||
|
|
||||||
if let Ok(setup_logger) = ret {
|
|
||||||
setup_logger(log::logger(), log::max_level()).unwrap();
|
setup_logger(log::logger(), log::max_level()).unwrap();
|
||||||
} else {
|
} else {
|
||||||
warn!("setup_logger not found");
|
warn!("setup_logger not found");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
let module = lib.get::<fn() -> Interface>(b"functions").unwrap();
|
pub fn get_interface<T>(&self) -> Result<T, ModuleError> {
|
||||||
|
if let Ok(interface_function) = self.get_symbol::<fn() -> T>(b"interface") {
|
||||||
let i = module();
|
Ok(interface_function())
|
||||||
|
} else {
|
||||||
// i.test1();
|
Err(ModuleError::LinkError(format!(
|
||||||
|
"Interface function not found!"
|
||||||
info!("Func: {:?}", i.test1);
|
)))
|
||||||
|
}
|
||||||
i.test1();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
pretty_env_logger::init();
|
||||||
|
|
||||||
|
info!("Initalized");
|
||||||
|
|
||||||
|
match || -> Result<(), ModuleError> {
|
||||||
|
let module =
|
||||||
|
Module::new("../unshell-module-test/target/release/libunshell_module_test.so")?;
|
||||||
|
module.init_logger();
|
||||||
|
|
||||||
|
let interface = module.get_interface::<Interface>()?;
|
||||||
|
|
||||||
|
interface.test1();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}() {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => {
|
||||||
|
error!("ERROR! {:?}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,14 @@
|
|||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
pub use unshell_logger::setup_logger;
|
pub use unshell_logger::setup_logger;
|
||||||
use unshell_modules::{module, module_interface};
|
use unshell_modules::module_interface;
|
||||||
|
|
||||||
// #[unsafe(no_mangle)]
|
|
||||||
extern "C" fn test1() {
|
extern "C" fn test1() {
|
||||||
warn!("Test1 called");
|
warn!("Test1 called");
|
||||||
}
|
}
|
||||||
// #[unsafe(no_mangle)]
|
|
||||||
extern "C" fn test2() {
|
extern "C" fn test2() {
|
||||||
warn!("Test2 called");
|
warn!("Test2 called");
|
||||||
}
|
}
|
||||||
// #[unsafe(no_mangle)]
|
|
||||||
extern "C" fn test3() {
|
extern "C" fn test3() {
|
||||||
warn!("Test3 called");
|
warn!("Test3 called");
|
||||||
}
|
}
|
||||||
@@ -27,19 +24,8 @@ module_interface! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
pub fn test() {
|
pub fn interface() -> Interface {
|
||||||
info!("Module loaded");
|
Interface::from_raw(test1, test2, test3)
|
||||||
}
|
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
|
||||||
pub fn functions() -> Interface {
|
|
||||||
info!("Module loaded");
|
|
||||||
// let m = TestModule::new();
|
|
||||||
let i = unsafe { Interface::from_raw(test1, test2, test3) };
|
|
||||||
|
|
||||||
i.test1();
|
|
||||||
|
|
||||||
i
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
|
|||||||
@@ -1,31 +1,3 @@
|
|||||||
#[macro_export]
|
|
||||||
macro_rules! module {
|
|
||||||
($module_name:ident { $(fn $fn_name:ident($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)?);* $(;)? }) => {
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
pub struct $module_name;
|
|
||||||
|
|
||||||
impl $module_name {
|
|
||||||
$(
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn $fn_name(&self, $($arg: $ty),*) $(-> $ret)? {
|
|
||||||
$fn_name($($arg),*)
|
|
||||||
}
|
|
||||||
)*
|
|
||||||
|
|
||||||
/// Create a new instance of this module
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for $module_name {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! module_interface {
|
macro_rules! module_interface {
|
||||||
($interface_name:ident { $(fn $fn_name:ident($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)?);* $(;)? }) => {
|
($interface_name:ident { $(fn $fn_name:ident($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)?);* $(;)? }) => {
|
||||||
@@ -39,25 +11,13 @@ macro_rules! module_interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl $interface_name {
|
impl $interface_name {
|
||||||
/// Unsafe cast from a module type to this interface
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// The caller must ensure that:
|
|
||||||
/// - The module has exactly the same function signatures in the same order
|
|
||||||
/// - The functions follow the C calling convention
|
|
||||||
/// - The module's memory layout matches this interface
|
|
||||||
pub unsafe fn from_module<T>(module: &T) -> Self {
|
|
||||||
*(module as *const T as *const Self)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create from raw function pointers
|
/// Create from raw function pointers
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
///
|
///
|
||||||
/// The caller must ensure all function pointers are valid and have
|
/// The caller must ensure all function pointers are valid and have
|
||||||
/// the correct signatures
|
/// the correct signatures
|
||||||
pub unsafe fn from_raw(
|
pub fn from_raw(
|
||||||
$($fn_name: extern "C" fn($($ty),*) $(-> $ret)?),*
|
$($fn_name: extern "C" fn($($ty),*) $(-> $ret)?),*
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
Reference in New Issue
Block a user