Strengthening Tree Borrows so that we can discard Stacked Borrows

Đang mở
#573 7 bình luận 5 reaction 1 người được giao Xem trên GitHub

@RalfJung đang làm issue này rồi.

Từ ngày 8/11/2025.

Đánh giá

Issue này chưa được đánh giá.

Mô tả

A-aliasing-model

We now have SB and TB as two aliasing models with quite different structure. While there's a bunch of questions on which both give the same answer that we may still want to explore (https://github.com/rust-lang/unsafe-code-guidelines/issues/450, and whether nested references have any aliasing requirements), it'd be good to converge towards a single model on the questions where the two do differ.

One of the biggest differences is that TB manages to get away with giving raw pointers the same tag as their parent pointer. This has multiple nice consequences:

  • Within a function, a let mut x and &raw mut x are allowed to be interleaved arbitrarily. Without this, it is wrong to tell people "if you do not use references (nor Box), you do not have to worry about aliasing". Technically, it is correct to say "if you only use raw pointers, you are good", but most people would not realize that accessing x directly runs afoul of that idea. c2rust had to be specifically adjusted to this SB behavior.
  • Casting a &mut T to a *const T and a *mut T finally gives the same result.
  • On the implementation side, we no longer have to worry about finding all the places where a "safe" point gets turned into a raw pointer, which is quite tricky for Box due to its weird state as a hybrid of primitive type and library type.

@rust-lang/opsem if we manage to otherwise make TB basically as strict as SB, do we have general consensus that having a raw pointer share the tag of its parent reference is the right way forward? My vision here is that instead of 2 fairly different models, we have one model with a bunch of parameters that we can tweak.

The remaining differences I can think of right now are:

  • TB does not track UnsafeCell as precisely as SB.
  • TB allows using &T to access memory outside the T. Such "subobject provenance" could be useful for optimizations so we should not discard it prematurely. It should be fairly easy to add a flag to make this optional.
  • TB does not do implicit writes on &mut retags. Doing such implicit writes has been requested by Nikita so we should not discard this option lightly. It should be easy to add a flag for this but I don't know the consequences, it may well be that this breaks too much code without some of the other quirks of SB. So this is the biggest unknown here and likely the first experiment I would make to make progress here: function-entry retags (and only those!) could do an implicit write. This PR has some good testcases.
  • Foreign reads from an &mut freeze it rather than disabling it. That seems fine? I can't think of a good reason why we'd want to force-disable references in this case. Also see https://github.com/rust-lang/unsafe-code-guidelines/issues/351.
  • Raw pointers are allowed to arbitrarily alias with the place they are created from. I think that's a big win over SB.
  • Anything else I forgot?

(I have various student projects planned in that area so please coordinate before starting to work on this yourself.)

Ngôn ngữ chính
Không có dữ liệu ngôn ngữ
Star
760
Fork
68
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-lang/unsafe-code-guidelines

Tất cả issue của rust-lang/unsafe-code-guidelines

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.