Skip to content

Add safety section for intrinsic volatile operations - #166399

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
yilin0518:fix_volatile
Nov 24, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
yilin0518:fix_volatile

Conversation

@yilin0518

Copy link
Copy Markdown
Contributor

This PR adds safety section for volatile operations in intrinsics/mod.rs. I find that these apis lack safety comments. I refer to the format of volatile_copy_nonoverlapping_memory and size_of_val.

For volatile_copy_memory, I think this api should follow the safety requirement of ptr::copy.

For unaligned_volatile_load(store), I add the text that they should refer to read(write)_voaltile but don't need to follow the align requirement.

Thank you for your suggestion.

@rustbot

rustbot commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

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

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

@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 Nov 23, 2026
@rustbot

rustbot commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 38 candidates
  • Random selection from JohnTitor, Mark-Simulacrum

Comment thread library/core/src/intrinsics/mod.rs Outdated
Comment on lines +1239 to +1243
/// # Safety
///
/// The safety requirements are consistent with [`copy`]
/// while the read and write behaviors are volatile,
/// which means it will not be optimized out unless size is equal to zero.

@RalfJung RalfJung Nov 23, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not correct. Volatile operations are complicated and have different (weaker) safety requirements.

We will document them properly if/when we ever expose these operations publicly. For internal-only docs, I don't think it is worth spending the effort.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I understand. I read the definition of volatile in LLVM, but it doe not give a clear explanation about the behaviour of volatile operation. Maybe similar to read_volatile and write_volatile, the safety requirement needs rewriting.

Comment on lines 1228 to 1229
/// The safety requirements are consistent with [`copy_nonoverlapping`]
/// while the read and write behaviors are volatile,

@RalfJung RalfJung Nov 23, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This old text is in fact also wrong.

View changes since the review

@RalfJung

RalfJung commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Thanks! For the unaligned ones, this looks good. But for the volatile copy intrinsics, I don't think this is correct.

r? me
@rustbot author

@rustbot rustbot assigned RalfJung and unassigned Mark-Simulacrum Nov 23, 2026
@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 Nov 23, 2026
@rustbot

rustbot commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

/// This intrinsic does not have a stable counterpart.
/// # Safety
///
/// The safety requirements are consistent with [`write_bytes`] while the write behavior is volatile,

@RalfJung RalfJung Nov 23, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This old text is also not correct for the same reason.

View changes since the review

@yilin0518

Copy link
Copy Markdown
Contributor Author

I update this PR and only contains changes about (un)aligned ones. @rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 24, 2026
@RalfJung

Copy link
Copy Markdown
Member

Thanks. :)
@bors r+ rollup

@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 8778c7d has been approved by RalfJung

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 24, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 24, 2026
Add safety section for intrinsic volatile operations

This PR adds safety section for volatile operations in `intrinsics/mod.rs`. I find that these apis lack safety comments. I refer to the format of `volatile_copy_nonoverlapping_memory` and `size_of_val`.

For `volatile_copy_memory`, I think this api should follow the safety requirement of `ptr::copy`.

For `unaligned_volatile_load(store)`, I add the text that they should refer to `read(write)_voaltile` but don't need to follow the align requirement.

Thank you for your suggestion.
rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - #165538 (Supertraits returns Unnormalized)
 - #166033 (Abby: move span out of leaf region constraint)
 - #166399 (Add safety section for intrinsic volatile operations)
 - #166420 ([rustdoc] Fix search index generation: if crate content didn't change, it should generate the same search index)
rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #166385 ([PERF] Inline some attribute parsing code)
 - #166013 (make `ReturnsVisitor` more accurate )
 - #166033 (Abby: move span out of leaf region constraint)
 - #166088 (Make rigid alias invalidation explicit when transitioning to PostAnalysis)
 - #166365 (Drop references to codebuild)
 - #166399 (Add safety section for intrinsic volatile operations)
 - #166439 (remove some incorrect docs from volatile intrinsics)
 - #166445 (fix test enum-repr-abi)
@rust-bors
rust-bors Bot merged commit c51c443 into rust-lang:main Nov 24, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 24, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
Rollup merge of #166399 - yilin0518:fix_volatile, r=RalfJung

Add safety section for intrinsic volatile operations

This PR adds safety section for volatile operations in `intrinsics/mod.rs`. I find that these apis lack safety comments. I refer to the format of `volatile_copy_nonoverlapping_memory` and `size_of_val`.

For `volatile_copy_memory`, I think this api should follow the safety requirement of `ptr::copy`.

For `unaligned_volatile_load(store)`, I add the text that they should refer to `read(write)_voaltile` but don't need to follow the align requirement.

Thank you for your suggestion.
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Nov 26, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#166385 ([PERF] Inline some attribute parsing code)
 - rust-lang/rust#166013 (make `ReturnsVisitor` more accurate )
 - rust-lang/rust#166033 (Abby: move span out of leaf region constraint)
 - rust-lang/rust#166088 (Make rigid alias invalidation explicit when transitioning to PostAnalysis)
 - rust-lang/rust#166365 (Drop references to codebuild)
 - rust-lang/rust#166399 (Add safety section for intrinsic volatile operations)
 - rust-lang/rust#166439 (remove some incorrect docs from volatile intrinsics)
 - rust-lang/rust#166445 (fix test enum-repr-abi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

4 participants