|
|
Log in / Subscribe / Register

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 19:05 UTC (Tue) 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

That paper does indeed acknowledge the necessity of provenance in C (and C++). But what they're doing is patching the hole in these standards - a hole made about twenty years ago, whereas the Rust article is about something a little different - that hole never existed in Rust. In _unsafe_ Rust the nomicon tells you already that if you violate the provenance rules that's undefined behaviour. Unlike safe Rust, but just like C, unsafe Rust doesn't _prevent_ you from picking some random bytes and insisting they're definitely a pointer to something, however you now have incoherent nonsense, don't do that (on CHERI you fault immediately in unsafe Rust or C if you do this).

Aria proposes that usize - Rust's built in unsigned integer type that's typically 64-bit on a modern computer - should formally be the same size as an address _not_ the same size as a pointer as it is defined today. As a side effect, something desirable (to me at least, but I believe others too) falls out, while we're acknowledging that a pointer isn't just an integer with intent, we abolish the (ab)use of as casts to turn one into the other. Instead the programmer is expected to write what they meant, e.g. ptr.with_addr(address) gets you a pointer (maybe 129-bits) made from an address (maybe 64-bits) plus your promise that what you are doing is OK. Did you lie? Same rules as before, now your program is meaningless.

The C proposal can't go around adding methods to pointers, not least because C doesn't have methods and if it did it wouldn't have them on pointers, it just changes the formal semantics of the language to acknowledge the practical need for provenance. Existing correct C will remain correct, the TS just says why it's correct (or rather, why other seemingly reasonable C that doesn't work is not correct).

Also I expect that the committee will nod wisely and say that they don't have time to take this up right now, but please bring it back again next time, which is roughly what it has been doing since at least 2016, if your plan is to wait for them to fix C rather than learn a new language, don't figure on that happening any time soon.


to post comments

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 23, 2022 13:26 UTC (Wed) by uecker (guest, #157556) [Link] (2 responses)

I do not understand why say you the hole does not exist in unsafe Rust. The article seems to indicate there are still problems in unsafe Rust of the same nature as we have addressed in the TS for C.

C could of course as easily add new way to combine a pointer with an address using some other syntax than a method. The problem with this is that it would break existing code (which for Rust somehow seems OK). The other problem is that the in most cases where you need to convert an integer to a pointer you do not have the pointer available, so you simply can not use ptr.with_addr(address). If you had the pointer you could also just to ptr + offset which is the same as ptr + (addr - base_addr), so I do not see how ptr.with_addr(address) solves the same problem.

C has a lot issues, but it also has many advantages: Widely supported, long-term stability, many existing tools, fast compilation, low complexity, emerging formal semantics, etc. And yes, it will take a long time fixing its many issues. It will also take a long time before Rust is ready (the long compile times and lack of stability rule it out for me at this time) and it is already too complex for my taste.

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 23, 2022 15:10 UTC (Wed) by farnz (subscriber, #17727) [Link]

There are two important differences between Rust and C that make breaking changes in handling raw pointers more palatable on the Rust side:

  1. Use of raw pointers in Rust is already gated behind unsafe, and Rust style says to keep the use of unsafe to a minimum. This allows you to use data in papers like this ACM paper on Unsafe Rust to judge the maximum blast radius of changes - the data supports the idea that at most 25% of Rust code could be affected (as around 75% of published crates contain no Unsafe Rust), and that 3% of Rust code would be a good estimate of the amount of code affected by changes to raw pointers (about 10% of Unsafe Rust deals with raw pointers). In contrast, because of the nature of C, it's harder to tell how much C code is likely to be affected by any given change to pointer semantics.
  2. Rust's module system allows you to have maintained legacy code in an older edition and modern Rust in the same binary - I can link Rust 2015 code with Rust 2027 code, and the compiler will give the Rust 2015 code the semantics that went with Rust 2015, while giving me modern semantics for Rust 2027 code. #include in C means that I can't clearly delineate code that has modern semantics from code that doesn't, because some code has to have the "right" semantics whether it's #included into a compilation unit that has C99 semantics or whether it's #included into a compilation unit that has C27 semantics.

Of these, I think the former is the hard one to overcome; fixing the latter is something that can be done by a sufficiently smart C standard committee and compiler implementation team, while the former is about gathering statistics easily on which code might be affected.

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 23, 2022 19:10 UTC (Wed) by tialaramex (subscriber, #21167) [Link]

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?


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