-
Notifications
You must be signed in to change notification settings - Fork 244
Add optional support for extern_item_impls
#786
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
base: master
Are you sure you want to change the base?
Conversation
Adds support for the nightly-only feature `extern_item_impls` gated behind a new fRust flag `getrandom_extern_item_impls`. This allows overriding the default backend implementation or providing one where it would otherwise not be available from safe Rust code in a user-friendly and idiomatic way. Offering this PR largely as a hypothetical since there are open questions (e.g., if this was stable, should it be optional or default? should there be a way to prevent overriding the default backend to guard against malicious libraries?, etc.)
|
Yes, it's fine.
I think yes, assuming it satisfies all our needs. But we probably should introduce such change only in a new breaking release.
I am not sure... On one hand, I would prefer if it was impossible to silently overwrite default source without downstream user knowledge, but on the other hand we have |
|
This would indeed be a nice feature to have. I would suggest using an externally-implementable trait. Certainly the default impls for
Do we have a real use-case for this yet? Once stable it should likely replace the current custom backend system; in the mean-time is there any actual reason to support two methods?
I'm not sure that has the same security implications. Personally I prefer that custom backends be off-by-default with only the binary being able to enable the functionality (which is approximately what rust flags achieve). |
Can it work with the
I think it's worth to test |
I don't think so... in any case, the |
Basically a copy of how the
I'll mark this PR as ready for review then!
I think so too. This feels like a big enough change to warrant a new breaking release, especially since it makes the
Another option is we could publicly export whatever the default backend is. So users who want to ensure the default backend is used can just call the default in their own |
extern_item_implsextern_item_impls
Yes. It also may require all 3 functions instead of just |
I do think that's a better fit, but I'm not sure how far along that feature is? It appears to me at least that externally implementable functions will land relatively soon.
I'm not sure if we can prevent that with the nightly feature as-is. We definitely could with extern traits though. We could also just advise libraries/users to override all 3 as best practice (maybe export
My thinking is it's be good to allow nightly users to battle test the feature before eventually (maybe) relying on it more heavily once stable. It does have the benefit of allowing the override of the u32 and u64 methods, which the current
Thankfully this question can be kicked down the road for now, since it's a nightly feature and will require a flag until stabilised. Personally, I think this should be on by default in a future breaking release. If it was, the experience of using a 3rd party library as a backend would be as simple as including it as a dependency; no RUSTFLAGS shenanigans, and no confusing linker failure like with the current custom backend. |
Objective
extern_item_implsingetrandom.Solution
RUSTFLAG,getrandom_extern_item_impls, which enables the nightly-onlyextern_item_implsfeature.backendsto use#[eii]onfill_inner,u32_inner, andu64_inner.u32_inner, andu64_inner.fill_innermust be provided to successfully compile a final artifact (will not fail for libraries, etc.)#[eii]in a newimplementationmodule.Example
With
getrandom_extern_item_implsenabled, users can provide (or override) the implementations of the 3 backend functions used bygetrandomwith a simple attribute macro:If an implementation is not provided by the user (or an appropriate library) the compiler will provide a helpful error message during compilation informing the user that they must provide an implementation:
If two implementations are provided (e.g., a library and the user) then another error message is provided:
Open Questions
getrandomwill continue to function correctly as long as the Rust flag isn't used. Noting as well that theefi_rngbackend also relies on a nightly feature,uefi_std.Notes
#[eii]attribute doesn't provide a way to conditionally provide a default implementation for an externally implementable item, I've added a macro to avoid repetition in thebackendsmodule. Not super happy with that solution, but I think it's preferable to the added verbosity of repeatedly defining thefill_innerfunction within thecfg_ifstatement.extern_item_implswill be stabilised. I hope relatively soon, since even the current implementation seems vastly cleaner than#[unsafe(no_mangle)] extern "Rust" fn ....