Skip to content

Commit

Permalink
Entry::new returns Err when entry point isn't found (#390)
Browse files Browse the repository at this point in the history
* Check for entry point in EntryCustom::new and return error

* Split off into load_checked

* Remove string from MissingEntryPoint

* Change MissingEntryPoint error message
  • Loading branch information
Rua authored Apr 18, 2021
1 parent 5eb39fe commit 90b0531
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
44 changes: 40 additions & 4 deletions ash/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::prelude::*;
use crate::vk;
use crate::RawPtr;
use std::error::Error;
use std::ffi::CStr;
use std::fmt;
use std::mem;
use std::os::raw::c_char;
Expand Down Expand Up @@ -163,11 +164,15 @@ impl<L> EntryV1_2 for EntryCustom<L> {
}

impl<L> EntryCustom<L> {
pub fn new_custom<Load>(mut lib: L, mut load: Load) -> Self
pub fn new_custom<Load>(
mut lib: L,
mut load: Load,
) -> std::result::Result<Self, MissingEntryPoint>
where
Load: FnMut(&mut L, &::std::ffi::CStr) -> *const c_void,
{
let static_fn = vk::StaticFn::load(|name| load(&mut lib, name));
// Bypass the normal StaticFn::load so we can return an error
let static_fn = vk::StaticFn::load_checked(|name| load(&mut lib, name))?;

let entry_fn_1_0 = vk::EntryFnV1_0::load(|name| unsafe {
mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr()))
Expand All @@ -181,13 +186,13 @@ impl<L> EntryCustom<L> {
mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr()))
});

EntryCustom {
Ok(EntryCustom {
static_fn,
entry_fn_1_0,
entry_fn_1_1,
entry_fn_1_2,
lib,
}
})
}

#[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkEnumerateInstanceVersion.html>"]
Expand Down Expand Up @@ -226,3 +231,34 @@ impl<L> EntryCustom<L> {
}
}
}

impl vk::StaticFn {
pub fn load_checked<F>(mut _f: F) -> Result<Self, MissingEntryPoint>
where
F: FnMut(&::std::ffi::CStr) -> *const c_void,
{
// TODO: Make this a &'static CStr once CStr::from_bytes_with_nul_unchecked is const
static ENTRY_POINT: &[u8] = b"vkGetInstanceProcAddr\0";

Ok(vk::StaticFn {
get_instance_proc_addr: unsafe {
let cname = CStr::from_bytes_with_nul_unchecked(ENTRY_POINT);
let val = _f(cname);
if val.is_null() {
return Err(MissingEntryPoint);
} else {
::std::mem::transmute(val)
}
},
})
}
}

#[derive(Clone, Debug)]
pub struct MissingEntryPoint;
impl std::fmt::Display for MissingEntryPoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "Cannot load `vkGetInstanceProcAddr` symbol from library")
}
}
impl std::error::Error for MissingEntryPoint {}
28 changes: 23 additions & 5 deletions ash/src/entry_libloading.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::entry::EntryCustom;
use crate::entry::MissingEntryPoint;
use libloading::Library;
use std::error::Error;
use std::ffi::OsStr;
Expand All @@ -25,17 +26,32 @@ const LIB_PATH: &str = "libvulkan.dylib";
pub type Entry = EntryCustom<Arc<Library>>;

#[derive(Debug)]
pub struct LoadingError(libloading::Error);
pub enum LoadingError {
LibraryLoadFailure(libloading::Error),
MissingEntryPoint(MissingEntryPoint),
}

impl fmt::Display for LoadingError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
match self {
LoadingError::LibraryLoadFailure(err) => fmt::Display::fmt(err, f),
LoadingError::MissingEntryPoint(err) => fmt::Display::fmt(err, f),
}
}
}

impl Error for LoadingError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Error::source(&self.0)
Some(match self {
LoadingError::LibraryLoadFailure(err) => err,
LoadingError::MissingEntryPoint(err) => err,
})
}
}

impl From<MissingEntryPoint> for LoadingError {
fn from(err: MissingEntryPoint) -> Self {
LoadingError::MissingEntryPoint(err)
}
}

Expand Down Expand Up @@ -71,13 +87,15 @@ impl EntryCustom<Arc<Library>> {
/// `dlopen`ing native libraries is inherently unsafe. The safety guidelines
/// for [`Library::new`] and [`Library::get`] apply here.
pub unsafe fn with_library(path: impl AsRef<OsStr>) -> Result<Entry, LoadingError> {
let lib = Library::new(path).map_err(LoadingError).map(Arc::new)?;
let lib = Library::new(path)
.map_err(LoadingError::LibraryLoadFailure)
.map(Arc::new)?;

Ok(Self::new_custom(lib, |vk_lib, name| {
vk_lib
.get(name.to_bytes_with_nul())
.map(|symbol| *symbol)
.unwrap_or(ptr::null_mut())
}))
})?)
}
}

0 comments on commit 90b0531

Please sign in to comment.