working proof of concept

This commit is contained in:
lever1209 2024-08-21 13:39:47 -03:00
commit 9efcf6b526
No known key found for this signature in database
GPG key ID: 0EA7B9E85212693C
6 changed files with 39 additions and 18 deletions

View file

@ -4,11 +4,9 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
tokio = { version = "1.39.2", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
libloading = "0.8.5" libloading = "0.8.5"
once_cell = "1.19.0" once_cell = "1.19.0"
host_tools = { path = "host_tools" }
[workspace] [workspace]
members = ["plugins/testing"] members = [ "host_tools","plugins/testing"]

13
host_tools/Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "host_tools"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["dylib"]
[dependencies]
async-trait = "0.1.81"
tokio = { version = "1.39.3", features = ["full"] }
tracing-subscriber = "0.3.18"
tracing = "0.1.40"

View file

@ -1,3 +1,7 @@
pub use tokio;
pub use tracing;
pub use tracing_subscriber;
use std::collections::HashMap; use std::collections::HashMap;
use tokio::runtime::Handle; use tokio::runtime::Handle;

View file

@ -7,8 +7,9 @@ edition = "2021"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
tokio = "1.39.3" #tokio = "1.39.3"
tracing = "0.1.40" #tracing = "0.1.40"
gpt-sandbox = { path = "../../" } host_tools = {path="../../host_tools"}
#gpt-sandbox = { path = "../../" }
async-trait = "0.1.81" async-trait = "0.1.81"
once_cell = "1.19.0" once_cell = "1.19.0"

View file

@ -1,7 +1,7 @@
use async_trait::async_trait; use async_trait::async_trait;
use gpt_sandbox::{AsyncFn, Plugin, PluginResult, RegisterFn}; use host_tools::{tokio::task::JoinHandle, tracing::info, AsyncFn, Plugin, PluginResult, RegisterFn};
use std::collections::HashMap; use std::collections::HashMap;
use tokio::runtime::Handle; use host_tools::tokio::runtime::Handle;
#[allow(improper_ctypes_definitions)] // will never be used by c programs, would use rust dylib if it existed #[allow(improper_ctypes_definitions)] // will never be used by c programs, would use rust dylib if it existed
#[no_mangle] #[no_mangle]
@ -24,11 +24,11 @@ impl Plugin for ExamplePlugin {
} }
} }
fn example_function(handle: Handle) -> tokio::task::JoinHandle<PluginResult> { fn example_function(handle: Handle) -> JoinHandle<PluginResult> {
println!("start of example_function"); info!("start of example_function");
handle.spawn(async { handle.spawn(async {
println!("task running"); info!("task running");
PluginResult::Ok PluginResult::Ok
}) })
} }

View file

@ -1,10 +1,10 @@
use gpt_sandbox::{AsyncFn, PluginResult, RegisterFn}; use host_tools::tokio::runtime::Handle;
use host_tools::tokio::sync::RwLock;
use host_tools::tracing::{error, info};
use host_tools::{AsyncFn, PluginResult, RegisterFn};
use libloading::{Library, Symbol}; use libloading::{Library, Symbol};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use tokio::runtime::Handle;
use tokio::sync::RwLock;
use tracing::{error, info};
fn get_name() -> String { fn get_name() -> String {
"example_function".to_string() "example_function".to_string()
@ -100,9 +100,14 @@ async fn unload_plugin(
// Ok(()) // Ok(())
//} //}
#[tokio::main(flavor = "multi_thread", worker_threads = 6)] #[host_tools::tokio::main(
crate = "host_tools::tokio",
flavor = "multi_thread",
worker_threads = 6
)]
async fn main() -> Result<(), PluginResult> { async fn main() -> Result<(), PluginResult> {
tracing::subscriber::set_global_default(tracing_subscriber::fmt().finish()).unwrap(); host_tools::tracing::subscriber::set_global_default(host_tools::tracing_subscriber::fmt().finish())
.unwrap();
let function_map: Arc<RwLock<HashMap<String, AsyncFn>>> = Arc::new(RwLock::new(HashMap::new())); let function_map: Arc<RwLock<HashMap<String, AsyncFn>>> = Arc::new(RwLock::new(HashMap::new()));
let plugin_lib = load_plugin("target/release/libtesting.so", Arc::clone(&function_map)).await?; let plugin_lib = load_plugin("target/release/libtesting.so", Arc::clone(&function_map)).await?;