Skip to content

Commit

Permalink
Implement stricter library version checking
Browse files Browse the repository at this point in the history
  • Loading branch information
thorio committed Jun 30, 2024
1 parent 05a169b commit 0aebe07
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ jobs:
- name: lint
run: cargo clippy -- -D warnings

- name: lint (dynamic plugins)
run: cargo clippy --no-default-features -- -D warnings

- name: test
run: cargo test --verbose

Expand All @@ -31,5 +34,8 @@ jobs:
- name: lint
run: cargo clippy -- -D warnings

- name: lint (dynamic plugins)
run: cargo clippy --no-default-features -- -D warnings

- name: test
run: cargo test --verbose
2 changes: 1 addition & 1 deletion gravel-ffi-macros/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn plugin(plugin_name: &str, with_fn_name: Ident) -> File {
#[cfg(not(feature = "no-root"))]
#[abi_stable::export_root_module]
pub fn __gravel_plugin_root() -> ::gravel_ffi::PluginLibRef {
let plugin = ::gravel_ffi::PluginLib { plugin: gravel_plugin };
let plugin = ::gravel_ffi::PluginLib { plugin: __gravel_plugin };
::abi_stable::prefix_type::PrefixTypeTrait::leak_into_prefix(plugin)
}

Expand Down
4 changes: 2 additions & 2 deletions gravel-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct PluginLib {
impl RootModule for PluginLibRef {
abi_stable::declare_root_module_statics! {PluginLibRef}

const BASE_NAME: &'static str = "example_library";
const NAME: &'static str = "example_library";
const BASE_NAME: &'static str = "gravel_ffi";
const NAME: &'static str = "gravel_ffi";
const VERSION_STRINGS: VersionStrings = package_version_strings!();
}
29 changes: 27 additions & 2 deletions gravel/src/init/plugins.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use abi_stable::library::{lib_header_from_path, LibHeader};
use abi_stable::{
library::{lib_header_from_path, LibHeader, LibraryError, RootModule},
sabi_types::VersionNumber,
};
use glob::{glob, Paths};
use gravel_core::{paths, plugin::PluginRegistry};
use gravel_ffi::PluginLibRef;
Expand Down Expand Up @@ -43,10 +46,32 @@ fn register_externals(registry: &mut PluginRegistry) {
.ok()
}

fn check_version<M>(header: &'static LibHeader) -> Result<&'static LibHeader, LibraryError>
where
M: RootModule,
{
let expected_version = VersionNumber::new(M::VERSION_STRINGS)?;
let actual_version = VersionNumber::new(header.version_strings())?;

if expected_version.major != actual_version.major
|| expected_version.minor < actual_version.minor
|| (expected_version.major == 0) && expected_version.minor > actual_version.minor
{
return Err(LibraryError::IncompatibleVersionNumber {
library_name: M::NAME,
expected_version,
actual_version,
});
}

Ok(header)
}

#[allow(clippy::print_stderr)]
fn load_lib(path: PathBuf) -> Option<PluginLibRef> {
lib_header_from_path(&path)
.and_then(LibHeader::init_root_module)
.and_then(check_version::<PluginLibRef>)
.and_then(LibHeader::check_layout)
.inspect_err(|e| {
// these errors tend to be huge, multiline monsters, so putting them in the logs would flood them
log::error!("unable to load plugin at {path:?}, writing diagnostics to stderr");
Expand Down

0 comments on commit 0aebe07

Please sign in to comment.