Skip to content
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

Emit a warning around thin &CStr interior mutability breaks #118513

Open
tgross35 opened this issue Dec 1, 2023 · 3 comments
Open

Emit a warning around thin &CStr interior mutability breaks #118513

tgross35 opened this issue Dec 1, 2023 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@tgross35
Copy link
Contributor

tgross35 commented Dec 1, 2023

From @chorman0773 https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/CStr.20as.20thin.20pointer/near/405432566

Passing an &UnsafeCell<CStr> to size_of_val is currently sound because it just returns the length parameter of the fat pointer. After making CStr thin however, size_of_val will need to call strlen on the data. This is not ok in a &UnsafeCell because another context could be writing the data, e.g. temporarily overwriting the \0.

This seems like something we may be able to emit a warning for?

Thin cstr: #59905

@rustbot label +T-libs +T-compiler +A-diagnostics

@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 1, 2023
@tgross35
Copy link
Contributor Author

tgross35 commented Dec 1, 2023

Quick search shows this is hopefully not very common https://github.com/search?q=language%3Arust+%2F%26.*UnsafeCell%3CCStr%3E%2F&type=code

@chorman0773
Copy link
Contributor

Incidentally, I wonder if &UnsafeCell<ThinCStr> itself is sound. I wonder if T-opsem should weigh in on that question. A write could happen and change the size of the memory the reference refers to, and the tag won't be correctly sized - maybe it will be shorter than the memory the reference refers to.

&mut ThinCStr might also have similar issues.

@jmillikin
Copy link
Contributor

Related: my RFC 3536 for !Sized thin pointers had to introduce a new ?Trait to prevent such types from being used with size_of_val(). In general the C concept of DSTs (pointer + computed value size) seems to be incompatible with the Rust semantics of T: ?Sized and size_of_val().

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants