Skip to content

allocations: document that they can be read-only#159503

Open
RalfJung wants to merge 2 commits into
rust-lang:mainfrom
RalfJung:allocations
Open

allocations: document that they can be read-only#159503
RalfJung wants to merge 2 commits into
rust-lang:mainfrom
RalfJung:allocations

Conversation

@RalfJung

Copy link
Copy Markdown
Member

Miri currently tracks "read-only" as an explicit flag on allocations that exists independent of provenance. It essentially corresponds to a read-only mapping in the page table.

Let's make this officially part of our model.
Cc @rust-lang/lang @rust-lang/opsem

@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 Jul 18, 2026
@rustbot

rustbot commented Jul 18, 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 12 candidates
  • Random selection from 6 candidates

@RalfJung

Copy link
Copy Markdown
Member Author

@rfcbot merge opsem

@rust-rfcbot

rust-rfcbot commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

@RalfJung has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jul 18, 2026

@Mark-Simulacrum Mark-Simulacrum left a comment

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.

r=me with FCP complete, unless you think there's something to be done about the comment

View changes since this review

Comment thread library/core/src/ptr/mod.rs
@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Jul 22, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@joshlf

joshlf commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Is there a reason we can't model this more simply as just only handing out read-only provenance pointers to these allocations without needing a separate notion of a read-only allocation?

Checking my box anyway – if there's a reason that a provenance-based approach isn't viable (or isn't genuinely simpler) then I'm happy to merge as-is.

@RalfJung

Copy link
Copy Markdown
Member Author

Is there a reason we can't model this more simply as just only handing out read-only provenance pointers to these allocations without needing a separate notion of a read-only allocation?

I think with the restrictions we have on atomics, that does not work. Doing a load(Ordering::Acquire) with read-only provenance is entirely fine, but doing it on a read-only allocation is not.

@joshlf

joshlf commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Our docs on atomic accesses to read-only memory say:

In general, all atomic accesses on read-only memory are undefined behavior. For instance, attempting to do a compare_exchange that will definitely fail (making it conceptually a read-only operation) can still cause a segmentation fault if the underlying memory page is mapped read-only. Since atomic loads might be implemented using compare-exchange operations, even a load can fault on read-only memory.

IIUC, this is a platform-level detail, not an opsem thing? In other words, the fact that an atomic load is implemented using compare-exchange isn't visible to opsem, and thus, as you said:

Doing a load(Ordering::Acquire) with read-only provenance is entirely fine

...because opsem doesn't "know" that the underlying atomic load is implemented via compare-exchange?

@RalfJung

RalfJung commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

...because opsem doesn't "know" that the underlying atomic load is implemented via compare-exchange?

Correct. On the semantics / Abstract Machine level, a failing CAS and an atomic load are read-only operations, they don't change the contents of memory. (They do change some other state related to the concurrency memory model, but that's a separate question.)

In other words, this is sound, at least under Tree Borrows:

use std::sync::atomic::*;

fn main() { unsafe {
    let x = 0i32;
    let atomic_x = &*(&x as *const i32 as *const AtomicI32);
    // atomic_x is derived from &x, so it has read-only provenance.
    atomic_x.load(Ordering::SeqCst);
} }

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Co-authored-by: Jacob Lifshay <programmerjake@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants