Skip to content

Add and modify safety descriptions in some of intrinsics APIs #135334

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

Closed
wants to merge 2 commits into from

Conversation

DiuDiu777
Copy link
Contributor

PR Description

Many public intrinsics unsafe APIs lack clear or sufficiently detailed # Safety descriptions. These updates aim to provide clearer and more accurate safety documentation for parts of these unsafe APIs.

This PR includes the following changes:

@rustbot
Copy link
Collaborator

rustbot commented Jan 10, 2025

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 10, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jan 10, 2025

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @rust-lang/wg-const-eval

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

These intrinsic docs are internal, they will never be stabilized and are not meant for users. So I don't quite understand what your goal here is. Why did you pick those 3 out of the dozens of intrinsics that don't have an explicit # Safety section? This already huge file will double in size if we do this systematically. So if you want to pursue the goal of improving our internal intrinsic safety docs, there should be some discussion with the relevant team (t-libs) first about how to best organize this.

Personally I think that yes we should document our intrinsics properly, but we don't need to make it as verbose as we do for user-facing docs. We can assume the reader of these docs to be quite knowledgeable and use advanced terminology without having to spell out everything in extensive detail.

@@ -3965,8 +3981,16 @@ pub const fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
/// The stabilized form of this intrinsic is [`crate::mem::swap`].
///
/// # Safety
/// Behavior is undefined if any of the following conditions are violated:
Copy link
Member

Choose a reason for hiding this comment

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

This is incomplete. The operation is typed, so the pointers must point to data that is valid at the given type.

///
/// * `_dst` must be properly aligned.
///
/// Note that even if `T` has size `0`, the pointer must be properly aligned.
Copy link
Member

Choose a reason for hiding this comment

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

You can just point at write_bytes here, the safety requirements are the same.

Except of course they aren't really -- these are volatile operations which can be used in ways that would be invalid for write_bytes. The same applies to copy_nonoverlapping vs volatile_copy_nonoverlapping_memory.

@@ -2042,6 +2046,18 @@ pub unsafe fn volatile_copy_memory<T>(_dst: *mut T, _src: *const T, _count: usiz
/// The volatile parameter is set to `true`, so it will not be optimized out
/// unless size is equal to zero.
///
/// # Safety
Copy link
Member

@RalfJung RalfJung Jan 11, 2025

Choose a reason for hiding this comment

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

What would be much more important do clarify here is whether both the read and the write are volatile, or only one of them. Also, "the volatile parameter is set to true" makes no sense, that's talking about a parameter inside the codegen backend that shouldn't show up in such docs. Same for llvm.memset.p0i8; maybe it's useful to mention it but the first summary should not refer to LLVM-specific concepts.

@thomcc
Copy link
Member

thomcc commented Jan 14, 2025

r? @rust-lang/opsem

@rustbot rustbot assigned RalfJung and unassigned thomcc Jan 14, 2025
@RalfJung
Copy link
Member

@rustbot author
Please post @rustbot ready when the PR is ready for the next round of review.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 14, 2025
@alex-semenyuk
Copy link
Member

@DiuDiu777
Thanks for your contribution
From wg-triage. Could you please take a look comments above

@DiuDiu777 DiuDiu777 closed this Mar 10, 2025
@DiuDiu777 DiuDiu777 deleted the intrinsics-doc branch March 10, 2025 02:18
@DiuDiu777 DiuDiu777 restored the intrinsics-doc branch March 10, 2025 02:19
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 16, 2025
Add missing doc for intrinsic (Fix PR135334)

The previous [PR135334](rust-lang#135334) mentioned that some of the intrinsic APIs were missing safety descriptions.

Among intrinsic APIs that miss safety specifications, most are related to numerical operations. They might need to be discussed and then seen how to organize.

Apart from them, only a few intrinsics lack safety. So this PR deals with the APIs with non-numerical operations in priority.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 16, 2025
Rollup merge of rust-lang#138309 - DiuDiu777:intrinsic-doc-fix, r=thomcc

Add missing doc for intrinsic (Fix PR135334)

The previous [PR135334](rust-lang#135334) mentioned that some of the intrinsic APIs were missing safety descriptions.

Among intrinsic APIs that miss safety specifications, most are related to numerical operations. They might need to be discussed and then seen how to organize.

Apart from them, only a few intrinsics lack safety. So this PR deals with the APIs with non-numerical operations in priority.
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Mar 19, 2025
Add missing doc for intrinsic (Fix PR135334)

The previous [PR135334](rust-lang#135334) mentioned that some of the intrinsic APIs were missing safety descriptions.

Among intrinsic APIs that miss safety specifications, most are related to numerical operations. They might need to be discussed and then seen how to organize.

Apart from them, only a few intrinsics lack safety. So this PR deals with the APIs with non-numerical operations in priority.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants