|
|
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 2:45 UTC (Tue) by atnot (guest, #124910)
Parent article: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

I think the fact that it is even feasible to discuss changes like this is testament to the benefits a somewhat more restrictive but also more rigorously defined system can have. It's very hard to imagine, for example, the likes of C pointers being reworked to give them better formal properties.


to post comments

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 5:04 UTC (Tue) by jhoblitt (subscriber, #77733) [Link] (4 responses)

It is also relevant that there are not billions of lines of Rust code which have been "working" for decades. Languages tend to become ossified as the user base grows.

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 13:01 UTC (Tue) by tialaramex (subscriber, #21167) [Link] (3 responses)

Avoiding this problem as much as possible is the goal of Rust's Editions system, even for Unsafe, and that's called out in this article. Rust has seen the last of big sweeping changes like garbage collection, but Editions enable it to be more agile than languages like C or C++ without the trauma associated with something like Python 3.x

Importantly Editions respect time's arrow. You write new code the new way, and your old code is unchanged. 10M lines or 10B lines doesn't matter, you aren't required to touch any of it. But Editions change how people think about what's possible and that means both that more adventurous changes are considered (knowing Editions might make the change practicable), and so often changes which wouldn't have been conceived at all without editions, ultimately turn out not to be incompatible and so the benefits accrue to everybody, not just on new editions.

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 16:40 UTC (Tue) by jhoblitt (subscriber, #77733) [Link] (2 responses)

Supporting multiple different language versions is not a Rust innovation. I don't know what language can claim the original invention but perl 5 had `use 5.024_001;` in the 90s.

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 19:01 UTC (Tue) by khim (subscriber, #9252) [Link]

Technically nothing in Rust is Rust innovation and most ideas it uses were already old when it was conceived. Heck, it was presented to the world with words technology from the past come to save the future from itself!

But most compileable “mainstream” language are based on ideas so ancient that even these, pretty old and well-tested ideas are looking like some kind of radical revelation to C/C++/ObjectPascal/etc developers (Swift took some of these ideas, though).

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 24, 2022 7:28 UTC (Thu) by roc (subscriber, #30627) [Link]

Editions aren't quite the same as multiple language versions. A significant amount of effort is required to ensure that a module using edition X links with and works correctly with any module using edition Y. This somewhat constrains what changes are allowed between editions, but it's a super important property.

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 7:16 UTC (Tue) by wahern (subscriber, #37304) [Link] (3 responses)

> It's very hard to imagine, for example, the likes of C pointers being reworked to give them better formal properties.

The very topic of discussion is literally evidence to the contrary. C already has intptr_t precisely to avoid the very problem Rust now has. (Relatedly, intptr_t is *optional* in Standard C, understanding that there might be some systems where data pointer to integer conversions aren't supportable; and Standard C doesn't support function pointer to integer conversions at all.) Moreover, FreeBSD has already been ported to CHERI, so claims that "real-world" C code is too riddled with non-standard pointer to integer conversions isn't very persuasive, particularly relative to Rust.

IIRC, porting the entire POSIX API to CHERI required only two significant changes: dlsym and signals. Both are areas where POSIX (much like Rust) required assumptions that Standard C doesn't. There was some ugliness related to memcpy, but Rust takes memcpy abuse to an entirely different level.

While C is far from the ideal language for a memory capability system, it certainly was more prepared for it than Rust. It's not surprising, though, as Rust was largely designed to workaround the lack of ABI- or ISA-enforced memory protections, whereas that possibility has always been at the back of the minds of C committee members. If you assume those things aren't on the horizon (and it's still not a given will see commercial success, let alone ubiquity), playing fast-and-loose with pointer types under the hood is an easy simplification. If common platforms implemented CHERI or something like it, Rust probably would have never been conceived in the first place.

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 17:24 UTC (Tue) by tialaramex (subscriber, #21167) [Link] (1 responses)

The point which you claim has been contradicted is that it's hard to imagine - "The likes of C pointers being reworked to give them better formal properties"

But you don't present any evidence of such a reworking, only that people have managed to run some C software on CHERI which of course you'd expect since CHERI has been under development for some time explicitly to run C software. Here's an excerpt from Cambridge's description of CHERI, "The CHERI memory-protection features allow historically memory-unsafe programming languages such as C and C++ to be adapted to provide strong, compatible, and efficient protection against many currently widely exploited vulnerabilities". Nothing in there about formal properties, no proposals to the ISO committee, instead they are being pragmatic, what choice do they have after decades of C programmers resolute disinterest.

> If common platforms implemented CHERI or something like it, Rust probably would have never been conceived in the first place.

If "something like it" managed to make the vague semantics of C's logical machine better match the reality of a modern computer by adjusting the computer instead, perhaps you'd even be right. Maybe if this had happened in the 1990s, the elevator Graydon was annoyed by in 2005 would have actually worked.

CHERI is a long way from this fantasy, many grave C problems are orthogonal to CHERI but are completely solved in (safe) Rust. Which doesn't make CHERI a bad idea, it just highlights that Graydon's problem wasn't something a lot the lines of "there's this one thing about C I don't like, so I guess I will write an entirely new programming language" but rather that systematically none of the useful lessons of past decades of programming language theory had been adopted into systems programming languages people actually use.

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 19:06 UTC (Tue) by excors (subscriber, #95769) [Link]

> Nothing in there about formal properties, no proposals to the ISO committee, instead they are being pragmatic, what choice do they have after decades of C programmers resolute disinterest.

They appear to have plenty about formal properties at https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/ch... , including papers like "Exploring C Semantics and Pointer Provenance" which adds CHERI-like semantics to a subset of C, based on (if I understand it correctly) the Cerberus tool which carefully transforms C programs into a 'Core' language that makes the memory model explicit and has well-defined operational semantics. The Core code can then be analysed for pointer provenance violations etc. And https://www.cl.cam.ac.uk/~pes20/cerberus/ lists many proposals submitted to ISO by that research group. (Of course they're still a long way from a complete semantics for C, despite working on this for well over a decade with many PhDs, so it's far from a solved (or even solvable) problem in general.)

(Hmm, actually Cerberus seems to be somewhat more relaxed than CHERI, because it doesn't require you to use intptr_t. See https://cerberus.cl.cam.ac.uk/?short/2eaa24 , select "Model > Integer provenance (PVI)", "Search > Random", and it complains of undefined behaviour when dereferencing the pointer, because it gets understandably confused about provenance. But comment out lines 9-10 (which are a noop in regular C) and it works okay, because it can still track provenance through the cast to long and back. If you step "Forward" enough times then you can see the allocation number associated with each pointer.)

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 19:30 UTC (Tue) by NYKevin (subscriber, #129325) [Link]

C arguably solves this problem by ignoring it. The standard already says it's illegal to even evaluate an invalid not-null not-one-past-the-end pointer, regardless of whether you dereference it, and integer types are similarly allowed to have trap representations, so you just declare "all the weird CHERI stuff" to be UB or implementation details that "portable" code can't rely on, and then you've "ported" C to CHERI by making CHERI look like just another architecture. The most you get out of this is "If your program otherwise would have performed certain very specific kinds of UB, on CHERI it will *probably* trap at runtime instead." "Probably" because who knows what the optimizer will do. Contrast this with the much stronger guarantees you get out of safe Rust, where it will fail at compile time, every time.

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 13:37 UTC (Tue) by uecker (guest, #157556) [Link] (6 responses)

There is a proposed technical specification which formulates this
for C and comes with precise formal semantics.

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2676.pdf

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 19:05 UTC (Tue) by tialaramex (subscriber, #21167) [Link] (3 responses)

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.

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?

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 19:10 UTC (Tue) by khim (subscriber, #9252) [Link] (1 responses)

It's one of half-dozen attempts. It's not yet fully finished and it's not clear if it even can be finished and adopted.

Rust's issue lies precisely with the fact that there are no C or C++ memory model which can be used to write code which would then be actually compiled (yes, there are some memory model specified by standard, but we know that compilers are happy to break certain valid programs based on that memory model, examples are actually in the proposal you are linking).

If there would have been some memory model which would have matched what the actual compilers are doing unsafe Rust would have just used that. But there are nothing, just a DR260 resolutions which prompts compiler developers to develop something and include it in the standard… and lots of handwaving.

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

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

It is an attempt which addresses provenance and has been agreed on to become a technical specification. If compilers support it (they already do to a large extend - modulo some known optimizer bugs and some cases where they already do not follow the standard) and no serious objections come up this will likely be adopted to be the C semantics for provenance.


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