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

Document what a "dangling pointer" is #132286

Open
3 tasks
Urgau opened this issue Oct 28, 2024 · 10 comments
Open
3 tasks

Document what a "dangling pointer" is #132286

Urgau opened this issue Oct 28, 2024 · 10 comments
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. E-help-wanted Call for participation: Help is requested to fix this issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-opsem Relevant to the opsem team

Comments

@Urgau
Copy link
Member

Urgau commented Oct 28, 2024

Location

There are several places this applies, e.g.:

Summary

We have several place in the standard library or as part of lints where we use the term "dangling pointer" without defining it anywhere.

We should have a place where we define what that is, ideally in the standard library, in it's own section, maybe under std::ptr, so it can be referenced elsewhere.

It should probably have a definition, followed by some bad examples (and maybe how to fix them).

Wikipedia has article on the subject, I don't know if it could serve as a base for our documentation but it may be worth checking it.

Steps

  • Add std documentation
  • Update existing std docs
  • Update lints diagnostics
@Urgau Urgau added A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. E-help-wanted Call for participation: Help is requested to fix this issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-opsem Relevant to the opsem team labels Oct 28, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 28, 2024
@Urgau Urgau removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 28, 2024
@saethlin
Copy link
Member

cc @rust-lang/opsem

@RalfJung
Copy link
Member

RalfJung commented Oct 28, 2024

Also see rust-lang/unsafe-code-guidelines#478 (but note that some things changed since the issue was opened so part of the information there is outdated).

@theemathas
Copy link
Contributor

The reference has a definition of dangling pointers.

@RalfJung
Copy link
Member

Right, so somewhat confusingly ptr::dangling on a ZST does actually produce a non-dangling pointer.

@theemathas
Copy link
Contributor

theemathas commented Oct 29, 2024

The documentation for ptr::dangling even explicitly says that it produces a dangling pointer (which is maybe false?). And then it goes to say that "the pointer value may potentially represent a valid pointer to a T" (which is false for non-ZST types, due to provenance).

@theemathas
Copy link
Contributor

Documentation from the reference:

A reference/pointer is “dangling” if not all of the bytes it points to are part of the same live allocation (so in particular they all have to be part of some allocation).
If the size is 0, then the pointer is trivially never “dangling” (even if it is a null pointer).

This implies that a pointer points to a range of addresses, depending on the type, and whether a pointer is dangling depends on the type.

Documentation from std::ptr:

We say that a pointer is “dangling” if it is not valid for any non-zero-sized accesses. This means out-of-bounds pointers, pointers to freed memory, null pointers, and pointers created with NonNull::dangling are all dangling.

This implies that whether a pointer is dangling or not doesn't depend on the type.

These two definitions are contradictory.

@RalfJung
Copy link
Member

RalfJung commented Oct 29, 2024

"may potentially" is definitely true, it doesn't say "will always" after all.

But yeah "is dangling" in the ptr::dangling docs uses a different definition of "dangling" than the reference. There are two reasonable definitions and we don't have a clear term to distinguish them:

  • A pointer that doesn't point to allocated memory
  • A pointer to type T that doesn't point to at least size_of::<T> bytes of allocated memory

We need to choose a different term for one of these.

@Urgau
Copy link
Member Author

Urgau commented Oct 29, 2024

I just realized that ptr::dangling and the (recently merged) dangling_pointers_from_temporaries lint will both become stable in 1.84, with different uses of "dangling pointer", this is unfortunate, it would be good to clear this situation before they become stable.

@RalfJung
Copy link
Member

RalfJung commented Oct 29, 2024

NonNull::dangling is stable since forever, ptr::dangling just matches that. So I don't think there's any point in trying to pick a different name for ptr::dangling.

But Cc @rust-lang/libs-api in case they disagree

@Urgau
Copy link
Member Author

Urgau commented Oct 29, 2024

That's a good point. The lint name however can still be changed (even after 1.84).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. E-help-wanted Call for participation: Help is requested to fix this issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-opsem Relevant to the opsem team
Projects
None yet
Development

No branches or pull requests

5 participants