Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsafe_op_in_unsafe_fn when using dynamic libraries and wrap_unsafe_ops #2961

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
int foo(int x, int y);
int bar(void *x);
int baz();

const int FLUX;
30 changes: 21 additions & 9 deletions bindgen/codegen/dyngen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ impl DynamicItems {
let init_fields = &self.init_fields;
let struct_implementation = &self.struct_implementation;

let library_new = if ctx.options().wrap_unsafe_ops {
quote!(unsafe { ::libloading::Library::new(path) })
} else {
quote!(::libloading::Library::new(path))
};

let from_library = if ctx.options().wrap_unsafe_ops {
quote!(unsafe { Self::from_library(library) })
} else {
Expand All @@ -100,7 +106,7 @@ impl DynamicItems {
path: P
) -> Result<Self, ::libloading::Error>
where P: AsRef<::std::ffi::OsStr> {
let library = ::libloading::Library::new(path)?;
let library = #library_new?;
#from_library
}

Expand Down Expand Up @@ -202,6 +208,7 @@ impl DynamicItems {
ident: Ident,
ty: TokenStream,
is_required: bool,
wrap_unsafe_ops: bool,
) {
let member = if is_required {
quote! { *mut #ty }
Expand All @@ -225,15 +232,20 @@ impl DynamicItems {
});

let ident_str = codegen::helpers::ast_ty::cstr_expr(ident.to_string());
self.constructor_inits.push(if is_required {
quote! {
let #ident = __library.get::<*mut #ty>(#ident_str).map(|sym| *sym)?;
}

let library_get = if wrap_unsafe_ops {
quote!(unsafe { __library.get::<*mut #ty>(#ident_str) })
} else {
quote! {
let #ident = __library.get::<*mut #ty>(#ident_str).map(|sym| *sym);
}
});
quote!(__library.get::<*mut #ty>(#ident_str))
};

let qmark = if is_required { quote!(?) } else { quote!() };

let var_get = quote! {
let #ident = #library_get.map(|sym| *sym)#qmark;
};

self.constructor_inits.push(var_get);

self.init_fields.push(quote! {
#ident
Expand Down
1 change: 1 addition & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ impl CodeGenerator for Var {
.to_rust_ty_or_opaque(ctx, &())
.into_token_stream(),
ctx.options().dynamic_link_require_all,
ctx.options().wrap_unsafe_ops,
);
} else {
result.push(tokens);
Expand Down
Loading