|
|
Log in / Subscribe / Register

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 21, 2022 19:01 UTC (Mon) by wtarreau (subscriber, #51152)
Parent article: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Seems like the language is starting to face the real world of computers, in which absolutely nothing distinguishes a pointer from an integer, both are used and stored interchangeably in registers, it's only how they are _used_ that makes us consider one is arbitrarily a pointer or an integer. On x86 a number of instructions even support indirect memory accesses involving [reg1+reg2*scale+disp]. One would think that reg1 is always the pointer, reg2 an index and disp a relative displacement, but it can be used in any form, including as indexes from local variables whose address is known (hence the pointer in disp), and reg1/reg2 can easily be exchanged when the scale is 1.

IMHO that's what "volatile" and "register" address in C (and not in the most elegant way, admittedly). "volatile" may be aliased by anything and will always be reloaded when read. "register" may never be aliased at all and the compiler will happily optimise their accesses.

Ideally we'd need a simplified mechanism in a language to indicate that certain pointers may alias only their own type, nothing at all or everything, and that they may be aliased by the same factors. with this, developers could choose their constructs without having to worry about what the compiler does behind (exactly like they do in assembly). Having to pretend that something is a register to prevent it from being aliased is annoying and limited since you cannot take its pointer to pass it anywhere. But if we could say "this never aliases anything" some constructs could be more easily optimized. Maybe some scopes would be useful (sort of aliasing barriers for certain variables).


The LWN site is currently under high scraper load, so comment display has been suppressed for anonymous users. If you are a human, you may read the comments by clicking the button below:

Note: you can avoid this step in the future by logging into your LWN account.


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