-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add statements on intrinsics. #148
base: main
Are you sure you want to change the base?
Add statements on intrinsics. #148
Conversation
@vapdrs would you like to have a look at my minimal first version? |
Hey @vjonaswolf -- thanks for kicking things off :) Could you add in a small description to the PR summary, so we can have a bit of traceability later? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for putting this together. Overall I'd say this covers the necessary basics for this topic. Some "nice to have" additions would a code example or link to one for transmute
, and a statement on the considerations that should be taken for safety when using intrinics. If you would like me to draft some wording for those, let me know.
.../initiatives/safe-use-of-unsafe-guidelines/learn_unsafe_rust/src/core_unsafety/intrinsics.md
Outdated
Show resolved
Hide resolved
|
||
Thus, the clear recommendation is not using the experimental compiler intrinsics in a safety-critical project. | ||
|
||
With `transmute` there is one major exception. However, `transmute` is handled in other parts of the guidelines. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what the exception being referred to here is,
transmute
is exceptional because it isn't Experimental- This is different from most other intrinsics, but
copy
,copy_nonoverlapping
, andwrite_bytes
are also not marked as Experimental
- This is different from most other intrinsics, but
transmute
is exceptional because it doesn't mention a stabilized API in its documentation- There is
std::mem::transmute
, however I guess this is just a re-export of the instrinsic not an API copy
,copy_nonoverlapping
, andwrite_bytes
don't also reference a stabilized API, and have astd::ptr
re-export
- There is
transmute
is exceptional because it is an experimental compiler intrinsic that should not be avoided in a safety-critical project- It isn't experimental, so the above guidance doesn't really apply
- The Rustonomicon also lists uses for
copy
andcopy_nonoverlapping
's re-exports when assigning to a memory location without invoking a drop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @vjonaswolf for his feedback on @vapdrs's comment
…guidelines/learn_unsafe_rust/src/core_unsafety/intrinsics.md Co-authored-by: Douglas Deslauriers <[email protected]>
…lf/safety-critical-rust-consortium into guideline-on-intrinsics
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
closes #174 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @vjonaswolf -- thanks for taking this on! I've left a few notes poking around how much scope we see being in this chapter.
|
||
Most of them are marked as experimental, unstable and useable in nightly versions of the compiler. | ||
|
||
Many intrinsics are accessible though a stabilized API located elsewhere in the Standard or Core Library. The documentation for each intrinsic typically references the stabilized API. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be useful to break this out into its own section and highlight some examples. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there should be plenty of examples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really, I'd say there should be an ontology of intrinsics here: a categorization of the stdlib intrinsics into different types.
|
||
Many intrinsics are accessible though a stabilized API located elsewhere in the Standard or Core Library. The documentation for each intrinsic typically references the stabilized API. | ||
|
||
Thus, the clear recommendation is not using the experimental compiler intrinsics in a safety-critical project. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can understand this general recommendation! Perhaps those exposed via other core
or std
APIs would be useful to discuss and note where undefined behavior can crop up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is the right way to approach this. As mentioned before, these chapters must:
- Talk about how the safety works for this feature
- Talk about things to think about when using them
- Talk about when one might need to reach for them
- Talk about best practices when you are reaching for them (this is where you say "try using the stabilized APIs instead" for some of these APIs)
This is unsafe code: people are already in a weird situation. They may have a good reason for reaching for an intrinsic, perhaps it's new and unstabilized, perhaps it's one of the more niche ones that never got a stable API, perhaps it's one of the atomic ones which have higher level stable APIs but not the lower level direct atomic accesses.
It should not be our job to say "don't do this", it should be our job to say "if you need to do this, here's how you make sure you actually need to do this, and here's how to make sure you're doing it right".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unsafe code: people are already in a weird situation.
I think this is fair.
@vjonaswolf -- perhaps we should take the safety-critical lens off here for the moment and broaden to helping people in a weird situation.
As we talked about a bit with @adfernandes, some of the work he's doing on the safety-critical pamphlet can help with noting in resources we reference like Learn unsafe Rust the context and certain things / sections to avoid or suggest alternatives to like "hey try to use these stabilized APIs instead, if possible".
|
||
There are only few functions that are not marked as experimental: `copy`, `copy_nonoverlapping`, `write_bytes`, `transmute`. | ||
`transmute` is probably the most well-known and widely used of these APIs. | ||
`transmute` is handled in other parts of the guidelines. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you refer to the chapter on Invalid Values, particularly this section?
If so, it might be nice to make the link over there explicit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please cross link.
|
||
Rust provides a set of compiler intrinsics as part of `core` and `std` respectively (https://doc.rust-lang.org/std/intrinsics/index.html). | ||
|
||
Most of them are marked as experimental, unstable and useable in nightly versions of the compiler. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have expected some discussion of std::arch
, the "SIMD and vendor intrinsics module".
I know that most usage is behind crates which use std::arch
, but given the level we're working at in safety-critical software, we sometimes need to drop down to that level of specificity.
Do you think it's useful to include a std::arch
discussion here and how unsafe
factors in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think simd should be a separate chapter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This chapter can link to it.
|
||
# Intrinsics | ||
|
||
Rust provides a set of compiler intrinsics as part of `core` and `std` respectively (https://doc.rust-lang.org/std/intrinsics/index.html). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At a minimum I'd expect this chapter to go into some depth about what an intrinsic is, and how it is different from a regular function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello. I wrote suggestions to fix a few typos in case they may help.
|
||
Thus, the clear recommendation is not using the experimental compiler intrinsics in a safety-critical project. | ||
|
||
There are only few functions that are not marked as experimental: `copy`, `copy_nonoverlapping`, `write_bytes`, `transmute`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are only few functions that are not marked as experimental: `copy`, `copy_nonoverlapping`, `write_bytes`, `transmute`. | |
There are only a few functions that are not marked as experimental: `copy`, `copy_nonoverlapping`, `write_bytes`, `transmute`. |
|
||
Rust provides a set of compiler intrinsics as part of `core` and `std` respectively (https://doc.rust-lang.org/std/intrinsics/index.html). | ||
|
||
Most of them are marked as experimental, unstable and useable in nightly versions of the compiler. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of them are marked as experimental, unstable and useable in nightly versions of the compiler. | |
Most of them are marked as experimental, unstable and usable in nightly versions of the compiler. |
|
||
Most of them are marked as experimental, unstable and useable in nightly versions of the compiler. | ||
|
||
Many intrinsics are accessible though a stabilized API located elsewhere in the Standard or Core Library. The documentation for each intrinsic typically references the stabilized API. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many intrinsics are accessible though a stabilized API located elsewhere in the Standard or Core Library. The documentation for each intrinsic typically references the stabilized API. | |
Many intrinsics are accessible through a stabilized API located elsewhere in the Standard or Core Library. The documentation for each intrinsic typically references the stabilized API. |
No description provided.