Rust 插件化系統

前言

在 Rust 中實現一個插件化系統可以讓你的應用程序具備動態加載和擴展功能。這種系統常用於構建可擴展的框架、遊戲引擎、安全產品中的漏洞插件化掃描或其他需要運行時擴展功能的項目。以下讓我們來看看實現 Rust 插件化系統的常見方法有哪些吧。

基於 Trait 的靜態插件系統

思路: 使用 Rust 的 trait 定義插件的接口,插件通過實現該接口與主程序進行交互。

實現步驟

1. 一個 trait 作爲插件接口。

2. 編寫具體的插件實現。

3. 使用泛型或特徵對象動態調用插件。

示例代碼

// 定義插件接口
pub trait Plugin {
    fn name(&self) -> &str;
    fn execute(&self);
}
// 主程序
pub struct PluginManager {
    plugins: Vec<Box<dyn Plugin>>,
}
impl PluginManager {
    pub fn new() -> Self {
        Self { plugins: Vec::new() }
    }
    pub fn add_plugin(&mut self, plugin: Box<dyn Plugin>) {
        self.plugins.push(plugin);
    }
    pub fn run_all(&self) {
        for plugin in &self.plugins {
            println!("Running plugin: {}", plugin.name());
            plugin.execute();
        }
    }
}
// 一個插件實現
pub struct HelloWorldPlugin;
impl Plugin for HelloWorldPlugin {
    fn name(&self) -> &str {
        "HelloWorld"
    }
    fn execute(&self) {
        println!("Hello, World!");
    }
}
fn main() {
    let mut manager = PluginManager::new();
    // 添加插件
    manager.add_plugin(Box::new(HelloWorldPlugin));
    // 運行插件
    manager.run_all();
}

優點

缺點

基於動態庫的動態插件系統

思路: 通過加載動態鏈接庫(*.so 或 *.dll),實現插件的動態加載和卸載。

實現步驟

1. 使用 libloading 庫加載動態庫。

2. 在動態庫中導出統一接口。

3. 主程序通過 libloading 調用插件的導出函數。

示例代碼

先創建項目和添加依賴

cargo new plugin_sys
cd plugin_sys && cargo add libloading
mkdir src/plugins && touch src/plugins/plugin.rs

目錄結構如下所示:

plugin_sys/
├── Cargo.toml
├── src/
│   ├── main.rs         # 主程序代碼
│   ├── plugins/        # 插件源碼目錄
│   │   └── plugin.rs   # 插件實現代碼
├── target/             # 編譯生成的文件
└── README.md           # 項目描述

在 src/plugins/plugin.rs 鍵入如下代碼:

use std::os::raw::c_char;
use std::ffi::CStr;
#[no_mangle]
pub extern "C" fn plugin_name() -> *const c_char {
    "DynamicPlugin".as_ptr() as *const c_char
}
#[no_mangle]
pub extern "C" fn plugin_execute() {
    println!("hello world from dynamic plugin");
}

在 main.rs 文件的調用代碼如下所示:

use std::path::Path;
use libloading::{Library, Symbol};
fn main() {
    let plugin_path = Path::new("./target/debug/libplugin.so");
    // 使用 unsafe 塊加載動態庫
    let lib = unsafe{
        Library::new(plugin_path).expect("Could not load plugin")
    };
    // 加載動態庫中的函數
    unsafe {
        let plugin_name: Symbol<unsafe extern "C" fn() -> *const std::os::raw::c_char> = lib.get(b"plugin_name").unwrap();
        let plugin_execute: Symbol<unsafe fn()> = lib.get(b"plugin_execute").unwrap();
        // 調用插件函數
        let name = std::ffi::CStr::from_ptr(plugin_name()).to_str().unwrap();
        println!("{}", name);
        plugin_execute();
    }
}

編譯與運行

編譯插件

編譯插件爲動態庫:

rustc src/plugins/plugin.rs --crate-type=cdylib -o target/debug/libplugin.so

運行主程序

運行主程序,加載並調用插件:

cargo run

結果如下所示:

➜  plugin_sys git:(master) ✗ cargo run
   Compiling plugin_sys v0.1.0 (/Users/Alan/Workspaces/Rust/plugin_sys)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.70s
     Running `target/debug/plugin_sys`
DynamicPluginhello world from dynamic plugin
hello world from dynamic plugin

⚠️注意: 在 Rust 中,unsafe 關鍵字用來顯式標記可能引發未定義行爲的代碼塊。libloading::Library::new 被標記爲 unsafe,因爲動態加載庫的行爲可能導致運行時的未定義行爲(例如加載無效的庫文件、函數符號缺失等)。

爲什麼需要 unsafe?

1. 動態庫加載行爲:加載的庫可能不是預期的動態庫文件(例如,路徑指向了無效文件或非動態庫文件),可能導致崩潰或未定義行爲。

2. 符號解析:庫中可能缺少指定的符號(函數名或變量),同樣會引發運行時錯誤。

3. 手動內存管理:某些情況下需要處理從動態庫返回的指針或資源,錯誤的操作可能引發未定義行爲。

優點

缺點

基於 WebAssembly 的插件系統

思路: 通過將插件編譯爲 WebAssembly(Wasm)模塊,在主程序中運行 Wasm 來實現插件化。

實現步驟

1. 編寫插件並編譯爲 Wasm。

2. 使用 wasmtime 或 wasmer 運行 Wasm 模塊。

3. 定義統一的接口標準,如導出函數名或內存佈局。

創建一個項目和添加依賴,具體如下所示:

cargo new plugin_sys
cd plugin_sys/
cargo new --lib plugin

添加 wasmtime 這個 crate 到主程序配置中

cargo add wasmtime

打開編輯插件項目的 plugin/src/lib.rs 實現 Wasm 插件邏輯:

extern "C" {
    fn host_printf(ptr: *const u8, len: usize);
}
#[no_mangle]
pub extern "C" fn hello() {
    let message = "Hello, World! from wasm";
    unsafe {
        host_printf(message.as_ptr(), message.len());
    }
}

打開編輯主項目的 src/main.rs 加載和運行 Wasm 插件:

use wasmtime::{Engine, Module, Store, Linker};
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 初始化Wasmtime引擎
    let engine = Engine::default();
    let wasm_path = "./plugin/target/wasm32-unknown-unknown/release/plugin.wasm";
    let wasm_bytes = std::fs::read(wasm_path)?;
    let module = Module::new(&engine, &wasm_bytes)?;
    let mut store = Store::new(&engine, ());
    let mut linker = Linker::new(&engine);
    // 創建 Linker 並添加主機的打印函數
    linker.func_wrap("env""host_printf", |msg_ptr: i32, msg_len: i32| {
        println!("msg_ptr:{},msg_len:{}", msg_ptr, msg_len);
    })?;
    // 實例化 WebAssembly 模塊
    let instance = linker.instantiate(&mut store, &module)?;
    let hello = instance.get_typed_func::<(), ()>(&mut store, "hello")?;
    // 調用插件的 `hello` 函數
    hello.call(&mut store, ())?;
    Ok(())
}

驗證效果

重新編譯和運行:

1. 編譯插件:

cd plugin/
cargo build --release --target wasm32-unknown-unknown

⚠️注意: 需要提前安裝好 wasm32-unknown-unknown 擴展,安裝命令如下所示:

rustup target add wasm32-unknown-unknown
  1. 運行主程序:
cargo run

結果如下所示:

➜  plugin_sys git:(master) ✗ cargo run
   Compiling plugin_sys v0.1.0 (/Users/Alan/Workspaces/Rust/plugin_sys)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.39s
     Running `target/debug/plugin_sys`
msg_ptr:1048576,msg_len:23

優點

缺點

基於腳本語言的插件系統

思路

通過嵌入腳本解釋器(如 Lua、Python、JavaScript 等),讓插件以腳本的形式存在。

實現步驟

1. 嵌入一個腳本語言解釋器(如 rlua、pyo3、deno_core)。

2. 定義主程序與腳本之間的交互接口。

3. 加載並運行腳本。

示例代碼

use rlua::{Function, Lua, Result};
fn main() -> Result<(){
    let lua = Lua::new();
    lua.context(|lua_ctx| {
        lua_ctx.load(
            r#"
            function plugin_name()
                return "Lua Plugin"
            end
            function execute()
                print("Hello from Lua plugin!")
            end
            "#,
        ).exec()?;
        let plugin_name_func: Function = lua_ctx.globals().get("plugin_name")?;
        let plugin_name: String = plugin_name_func.call(())?;
        println!("Hello from Lua plugin! {}", plugin_name);
        let execute: Function = lua_ctx.globals().get("execute")?;
        execute.call(())?;
        Ok(())
    })
}

優點

缺點

基於配置文件的插件系統

思路

將插件的邏輯定義在配置文件中(如 JSON、YAML),主程序解析配置並執行對應邏輯。

實現步驟

1. 定義一個通用的插件描述格式。

2. 使用配置文件描述插件邏輯。

3. 主程序解析配置並執行。

示例代碼

配置文件 (plugins.json)

[
    {
        "name""PrintPlugin",
        "action""print",
        "message""Hello from Print Plugin!"
    },
    {
        "name""SumPlugin",
        "action""sum",
        "operands": [1, 2, 3]
    }
]

主程序

use serde::Deserialize;
use std::fs;
#[derive(Deserialize)]
struct PluginConfig {
    name: String,
    action: String,
    message: Option<String>,
    operands: Option<Vec<i32>>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config_data = fs::read_to_string("plugins.json")?;
    let plugins: Vec<PluginConfig> = serde_json::from_str(&config_data)?;
    for plugin in plugins {
        println!("Executing plugin: {}", plugin.name);
        match plugin.action.as_str() {
            "print" ={
                if let Some(message) = plugin.message {
                    println!("{}", message);
                }
            }
            "sum" ={
                if let Some(operands) = plugin.operands {
                    let sum: i32 = operands.iter().sum();
                    println!("Sum: {}", sum);
                }
            }
            _ => println!("Unknown action: {}", plugin.action),
        }
    }
    Ok(())
}

優點

缺點

總結

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/hisii-o4kiMYi3IFsvMVfw