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

Entry::new returns Err when entry point isn't found #390

Merged
merged 7 commits into from
Apr 18, 2021

Conversation

Rua
Copy link
Contributor

@Rua Rua commented Mar 8, 2021

Fixes #387. The error is returned by StaticFn::load and then propagated by EntryCustom::new_custom and Entry::new.

MarijnS95 added a commit to Traverse-Research/ash that referenced this pull request Mar 8, 2021
Unfortunately docs are not explicitly (build-)tested as part of
`--all-targets` allowing broken code to slip in as shown by ash-rs#390.

Also remove the `rust` listing type which is the default, leaving only
`no_run` (until the CI has a loadable Vulkan library).
Copy link
Collaborator

@MarijnS95 MarijnS95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks! Not as easy as it seems, one of the changes is in a generated file and will get lost on the next regeneration attempt. I think the CI lost its ability to run the generator and error if that results in a diff, or am I misremembering?

ash/src/vk/features.rs Outdated Show resolved Hide resolved
ash/src/vk/features.rs Outdated Show resolved Hide resolved
ash/src/vk/features.rs Outdated Show resolved Hide resolved
ash/src/entry.rs Outdated Show resolved Hide resolved
@MarijnS95
Copy link
Collaborator

CI now spots missed code regeneration in #393.

@Rua
Copy link
Contributor Author

Rua commented Mar 10, 2021

I reworked it a bit so only Entry does the error checking now. Unfortunately, in order to save the library name from within the closure, I had to use an owned CString because the argument to the closure is not &'static.

@Rua Rua changed the title StaticFn::load returns Err when entry point isn't found Entry::new returns Err when entry point isn't found Mar 10, 2021
Copy link
Collaborator

@MarijnS95 MarijnS95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested this change for your use-case? As it stands a failure to load any symbol is only handled after new_custom completes, meaning get_instance_proc_addr is inevitably called leading to the original panic. In other words, in its current form this PR makes no difference to the original issue you attempt to solve.

As stated before just move load to entry.rs and return a Result there, that can subsequently be forwarded in new_custom. Then you can also use a &'static str again, though holding on to the original libloading::Error (and pairing it with the symbol name) is even better.

ash/src/entry_libloading.rs Outdated Show resolved Hide resolved
ash/src/entry_libloading.rs Outdated Show resolved Hide resolved
@Rua
Copy link
Contributor Author

Rua commented Mar 11, 2021

Did you mean move StaticFn to entry.rs, or just an impl block with load in it?

@MarijnS95
Copy link
Collaborator

Did you mean move StaticFn to entry.rs, or just an impl block with load in it?

Only the impl block with fn load; you can either create a second load function and leave the existing one for what it is, or make an edgecase to not generate it here:

https://github.com/MaikKlein/ash/blob/b3a010a3154d0cd9d4943e794a0e90916da93936/generator/src/lib.rs#L2210-L2217

@Rua
Copy link
Contributor Author

Rua commented Mar 11, 2021

Unfortunately, it's proving impossible to create a &'static CStr because the functions needed for this aren't const. To do this would require lazy_static as an additional dependency. I can either make MissingEntryPoint contain a &'static [u8], or go back to owned CString.

@MarijnS95
Copy link
Collaborator

Since the name is hardcoded (and where possible autogenerated may you want to extend this error to other places in the future) I'm not opposed to &'static str and a simple "vkGetInstanceProcAddr". A &'static [u8] with b"vkGetInstanceProcAddr\0" isn't too bad either. That is, in fact, what I had locally when implementing your feature request initially.

@Rua
Copy link
Contributor Author

Rua commented Mar 11, 2021

Ok, another attempt...

@MarijnS95
Copy link
Collaborator

Should be good now, thanks :)

ash/src/entry.rs Outdated Show resolved Hide resolved
MaikKlein pushed a commit that referenced this pull request Mar 14, 2021
* ci: Test docs in addition to `cargo t --all-targets`

Unfortunately docs are not explicitly (build-)tested as part of
`--all-targets` allowing broken code to slip in as shown by #390.

Also remove the `rust` listing type which is the default, leaving only
`no_run` (until the CI has a loadable Vulkan library).

* ash: Fix errors and warnings in (now tested) documentation comments
ash/src/entry.rs Outdated Show resolved Hide resolved
ash/src/entry.rs Outdated Show resolved Hide resolved
@Rua
Copy link
Contributor Author

Rua commented Apr 18, 2021

Does anything else need to be done here?

@MaikKlein
Copy link
Member

Does anything else need to be done here?

Sorry I forgot about it, looks good. LGTM

@MaikKlein MaikKlein merged commit 90b0531 into ash-rs:master Apr 18, 2021
@Rua Rua deleted the staticfn-result branch April 30, 2021 15:18
Comment on lines +240 to +245
// 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, it's proving impossible to create a &'static CStr because the functions needed for this aren't const. To do this would require lazy_static as an additional dependency. I can either make MissingEntryPoint contain a &'static [u8], or go back to owned CString.

2 years later I'm rather curious why I didn't notice the TODO (and static vs const), as the ENTRY_POINT string literal could be inlined in the CStr::from_bytes_with_nul_unchecked() call as I had done myself in #379 just a week before this PR was opened 😬

At this point I'm assuming it doesn't matter whether to assign to a let binding or change this to const (will be more verbose with forced const CNAME: &CStr type), as the function call is hopefully inlined either way? (this does matter for public symbols though, but that's a different beast)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect all of the above to compile down the same, yeah. Might as well favor clarity.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ralith What would you consider more clear for these one-off inline named bindings, const or let?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best practice to favor let when it's applicable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I can't imagine the compiler treating a fully-const path any different no matter whether it's via a let binding, inlined completely, or assigned to a const.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return error if library functions aren't found, instead of panicking
4 participants