#![allow(unsafe_code)]
use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::c_void;
use std::mem::{offset_of, size_of};
use std::path::PathBuf;
use std::sync::{Mutex, PoisonError};
use alpha2_engine::extract::AudioInput;
use alpha2_plugin_abi::{ABI_MAJOR, HostV1, NATIVE_ENTRY, PluginV1};
use super::{
Call, Collected, LogBudget, MAX_FAIL_BYTES, MAX_LOG_BYTES, OutputCap, PluginError, Transport,
bounded_text, codes,
};
struct State {
collected: Collected,
caps: Vec<OutputCap>,
resources: HashMap<String, Vec<u8>>,
log: LogBudget,
}
fn guarded<T>(failed: T, body: impl FnOnce() -> T) -> T {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(body)).unwrap_or(failed)
}
pub(super) struct NativeTransport {
table: PluginV1,
instance: *mut c_void,
host: Box<(HostV1, RefCell<State>)>,
}
static LOADED: Mutex<Option<HashMap<String, &'static libloading::Library>>> = Mutex::new(None);
fn library(
sha256: &str,
prepare: impl FnOnce() -> Result<PathBuf, PluginError>,
) -> Result<&'static libloading::Library, PluginError> {
let mut loaded = LOADED.lock().unwrap_or_else(PoisonError::into_inner);
let loaded = loaded.get_or_insert_with(HashMap::new);
if let Some(library) = loaded.get(sha256) {
return Ok(library);
}
let failed = |why: String| PluginError::new(codes::LOAD_FAILED, why);
let path = std::path::absolute(prepare()?)
.map_err(|error| failed(format!("the library path: {error}")))?;
let library = unsafe { libloading::Library::new(&path) }
.map_err(|error| failed(format!("{}: {error}", path.display())))?;
let library: &'static libloading::Library = Box::leak(Box::new(library));
loaded.insert(sha256.to_owned(), library);
Ok(library)
}
unsafe fn borrowed<'a, T>(ptr: *const T, len: usize) -> &'a [T] {
if ptr.is_null() || len == 0 {
&[]
} else {
unsafe { std::slice::from_raw_parts(ptr, len) }
}
}
unsafe fn state<'a>(context: *mut c_void) -> &'a RefCell<State> {
unsafe { &*context.cast::<RefCell<State>>() }
}
unsafe extern "C" fn emit(context: *mut c_void, records: *const u8, len: usize) -> i32 {
let (state, batch) = unsafe { (state(context), borrowed(records, len)) };
guarded(alpha2_plugin_abi::status::FAILED, || {
let State {
collected, caps, ..
} = &mut *state.borrow_mut();
collected.emit(batch, caps)
})
}
unsafe extern "C" fn resource(
context: *mut c_void,
name: *const u8,
name_len: usize,
len: *mut usize,
) -> *const u8 {
let (state, name) = unsafe { (state(context), borrowed(name, name_len)) };
guarded(std::ptr::null(), || {
let state = state.borrow();
match std::str::from_utf8(name)
.ok()
.and_then(|name| state.resources.get(name))
{
Some(bytes) if !len.is_null() => {
unsafe { len.write(bytes.len()) };
bytes.as_ptr()
}
_ => std::ptr::null(),
}
})
}
unsafe extern "C" fn fail(context: *mut c_void, message: *const u8, len: usize) {
let (state, message) = unsafe {
(
state(context),
borrowed(message, len.min(MAX_FAIL_BYTES + 1)),
)
};
guarded((), || {
state.borrow_mut().collected.failure = Some(bounded_text(message, MAX_FAIL_BYTES));
});
}
unsafe extern "C" fn log(context: *mut c_void, _level: u32, message: *const u8, len: usize) {
let (state, message) = unsafe {
(
state(context),
borrowed(message, len.min(MAX_LOG_BYTES + 1)),
)
};
guarded((), || state.borrow_mut().log.log(message));
}
#[allow(clippy::cast_possible_truncation)]
const HOST_BYTES: u32 = size_of::<HostV1>() as u32;
#[allow(clippy::cast_possible_truncation)]
const PLUGIN_BYTES: u32 = size_of::<PluginV1>() as u32;
const ENTRIES: usize =
(size_of::<PluginV1>() - offset_of!(PluginV1, describe)) / size_of::<*const c_void>();
pub(super) unsafe fn checked(table: *const PluginV1) -> Result<PluginV1, PluginError> {
let failed = |why: String| PluginError::new(codes::LOAD_FAILED, why);
if table.is_null() {
return Err(failed("alpha2_plugin_v1 returned null".to_owned()));
}
let (abi, size) = unsafe {
(
(&raw const (*table).abi).read(),
(&raw const (*table).size).read(),
)
};
if abi != ABI_MAJOR {
return Err(PluginError::new(
codes::ABI_UNSUPPORTED,
format!("the library implements alpha2-plugin/{abi}"),
));
}
if size < PLUGIN_BYTES {
return Err(failed(format!(
"the plugin table is {size} bytes; alpha2-plugin/1 needs {PLUGIN_BYTES}"
)));
}
let entries = unsafe {
std::slice::from_raw_parts(
(&raw const (*table).describe).cast::<*const c_void>(),
ENTRIES,
)
};
if entries.iter().any(|entry| entry.is_null()) {
return Err(failed("the plugin table has a null entry".to_owned()));
}
Ok(unsafe { table.read() })
}
impl NativeTransport {
pub(super) fn new(
sha256: &str,
prepare: impl FnOnce() -> Result<PathBuf, PluginError>,
resources: HashMap<String, Vec<u8>>,
caps: Vec<OutputCap>,
) -> Result<Self, PluginError> {
let failed = |why: String| PluginError::new(codes::LOAD_FAILED, why);
let library = library(sha256, prepare)?;
let entry = unsafe {
library.get::<Option<unsafe extern "C" fn() -> *const PluginV1>>(NATIVE_ENTRY)
}
.map_err(|error| failed(format!("the library exports no alpha2_plugin_v1: {error}")))?;
let entry = (*entry).ok_or_else(|| failed("alpha2_plugin_v1 is null".to_owned()))?;
let table = unsafe { entry() };
let table = unsafe { checked(table) }?;
let mut host = Box::new((
HostV1 {
abi: ABI_MAJOR,
size: HOST_BYTES,
context: std::ptr::null_mut(),
emit,
resource,
fail,
log,
},
RefCell::new(State {
collected: Collected::default(),
caps,
resources,
log: LogBudget::default(),
}),
));
host.0.context = (&raw const host.1).cast_mut().cast();
let instance = unsafe { (table.create)(&raw const host.0) };
if instance.is_null() {
return Err(failed("the plugin could not create an instance".to_owned()));
}
Ok(Self {
table,
instance,
host,
})
}
fn outcome(&self, status: i32) -> Call {
self.host.1.borrow_mut().collected.outcome(status)
}
}
impl Transport for NativeTransport {
fn describe(&mut self) -> Result<String, PluginError> {
let mut len = 0;
let bytes = unsafe { borrowed((self.table.describe)(&raw mut len), len) };
String::from_utf8(bytes.to_vec()).map_err(|_| {
PluginError::new(codes::DESCRIPTOR_MISMATCH, "the descriptor is not UTF-8")
})
}
fn begin(&mut self, context: &[u8]) -> Call {
let status = unsafe { (self.table.begin)(self.instance, context.as_ptr(), context.len()) };
self.outcome(status)
}
fn pass(&mut self, pass: u32, direction: u32) -> Call {
let status = unsafe { (self.table.pass)(self.instance, pass, direction) };
self.outcome(status)
}
fn video(&mut self, time: i64, width: u32, height: u32, format: u32, data: &[u8]) -> Call {
let status = unsafe {
(self.table.video)(
self.instance,
time,
width,
height,
format,
data.as_ptr(),
data.len(),
)
};
self.outcome(status)
}
fn audio(&mut self, block: &AudioInput) -> Call {
let status = unsafe {
(self.table.audio)(
self.instance,
block.start.ticks(),
block.start_sample,
block.frames,
block.rate,
block.channels,
block.samples.as_ptr(),
block.samples.len(),
)
};
self.outcome(status)
}
fn end(&mut self) -> Call {
let status = unsafe { (self.table.end)(self.instance) };
self.outcome(status)
}
}
impl Drop for NativeTransport {
fn drop(&mut self) {
unsafe { (self.table.destroy)(self.instance) };
}
}