Skip to content

Learn unsafe Rust: Provide initial content for the chapter "Dangling and unaligned pointers" #151

@PLeVasseur

Description

@PLeVasseur

Provide initial content for the Dangling and unaligned pointers chapter of the Learn unsafe Rust book.

Based on the guidelines for a chapter,

Dangling and unsafe pointers

Intro to what danging and unaligned pointers are

Usage:

  • Working across FFI bounds, needing to pass pointer and length of a buffer
  • Performance sensitive operations, e.g. I recall nalgebra uses unsafe for some things like this
  • ... others

How UB happen when dealing with them

  • Some operations we perform are OK to do and arrive at unaligned, others must be aligned
  • Good to have some definition here about alignment and references

Thinking about dangling and unaligned pointers is relevant (not always obvious)

  • Todo

Common techniques to work safely to avoid dangling and unaligned pointers

  • The stdlib seems to have some reasonable resources here
    • A fair number are marked unstable and nightly-only, would be good to see their usage in reality too (see e.g. ptr::as_uninit_slice_mut for a fairy comprehensive list of things that must be true to not get UB!)

Decided vs current gaps in specification vs direction of Rust / Rust's spec

For example ptr::read_volatile, which acknowledges:

Rust does not currently have a rigorously and formally defined memory model, so the precise semantics of what “volatile” means here is subject to change over time. That being said, the semantics will almost always end up pretty similar to C11’s definition of volatile.

General notes to me / team

  • In general, if there's a stdlib unsafe function to do something, we should lean towards recommending that when relevant
    • For someone new to this part of unsafe like me, they also seem to go into enough detail to learn

Resources

Filling in some resources that seem useful, feel free to sound off with more and I can add them to the first comment.

Folks interested

Metadata

Metadata

Assignees

Labels

coding guidelinesRelated to work in the Coding Guidelines Subcommitteetracking issueAn issue that gathers activities toward a larger goal

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions