|
|
Log in / Subscribe / Register

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 23, 2022 19:10 UTC (Wed) by tialaramex (subscriber, #21167)
In reply to: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul by uecker
Parent article: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

I assume from your username combined with the fact you phrase it as "We have addressed" you are Martin Uecker. I don't agree that the problems Aria talks about are "of the same nature".

In C the "hole" is because the committee admits that some things (take an arbitrary integer you got down the back of the sofa, cast that address into a pointer, dereference it) are not in fact required to work (even though the address was somehow "correct"), but hasn't (unless your work gets into the actual standards text) fixed the standards document to explain provenance so why these aren't correct is left unstated. It is not possible to understand why some example C doesn't actually do what you might intuitively expect based on the standards document today.

Rust has the notion of provenance already, and the nomicon (the guide for unsafe Rust) tells you about this danger. You can't go around casting integers you found down the back of the sofa into pointers (in unsafe Rust, as in C, you aren't *prevented* from doing this, but that doesn't mean it will work). So that hole is fixed. But this also throws a lot of babies out with the bath water. Aria shows some examples where it seems reasonable to suppose you didn't break the rules, but for that to be true usize on CHERI ends up 128-bits which is pretty crazy, and also examples where it's at least very difficult to reason about whether you broke the rules, which is scarcely better than there not being any rules.

The proposals aim to fix this so that you can more often write code that everybody (including the compiler) agrees does not break the rules, and actually does what you intended.

Of course Aria's idea wouldn't break existing code, unsafe Rust from today (say Rust 2021) would potentially be invalid on CHERI hardware with 64-bit usize if it does naughty things with pointers, but that's not worsened by this change. It works today on typical hardware, and that would stay true. Instead new code (for say, Rust 2027 edition) in this hypothetical uses the new API and trying to write things the old way in that edition doesn't work.

In most cases (and I don't see any reason this should be an exception) you can optionally do things the new way in old Rust editions, but doing them the old way in new editions is either warned against, or outright forbidden (won't compile) as appropriate. As I wrote above this respects time's arrow, your old code stays working.

As to why you'd want ptr.with_addr(address) yes you're correct that this is similar to using the existing pointer offset methods, and yes the whole point is to preserve provenance. Rust already considers provenance in the pointer API (including the offset API), that's not an innovation here. If all you have is an address, you don't have any provenance and so the compiler can't reason about this pointer. I would anticipate that software which wanted to have store some raw addresses and then later conjure valid pointers (thus with provenance) from them would keep an appropriate neutral pointer around which could supply suitable provenance via ptr.with_addr(address). If you just mean why add APIs which are convenient rather than necessary, that's normal in Rust, Rust thought "ready or not".contains("ready") was a reasonable expression from 1.0, while it took C++ until 2020 to admit that programmers want contains(x) even though it is logically equivalent to find(x) != npos.

Though maybe I misunderstood badly - Does the N2676 document propose that somehow C will be able to just conjure correct, provenance intact pointers from only an address? Where does the provenance come from?


to post comments


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds