Hi, this is a re-written version of the previously posted draft RFC#3700. Apologies for doing this slightly backwards, once there's some agreement here I'll update the previous PR with (an updated version of) the following. Accompanying implementation is posted at PR#130886.
- Feature Name:
ptr_simulate_realloc
- Start Date: 2024-09-26
- RFC PR: rust-lang/rfcs#3700
- Rust Issue: rust-lang/rust#0000
Summary
Add a helper for primitive pointer types to facilitate modifying the address of a pointer. This mechanism is intended to enable the use of architecture features such as AArch64 Top-Byte Ignore (TBI) to facilitate use-cases such as high-bit pointer tagging. An example application of this mechanism would be writing a tagging memory allocator.
Motivation
The term "pointer tagging" could be used to mean either high-bit tagging or low-bit tagging. Architecture extensions such as AArch64 Top-Byte Ignore make the CPU disregard the top bits of a pointer when determining the memory address, leaving them free for other uses.
This RFC is specifically concerned with creating those high-bit tagged pointers for systems which can make use of such architecture features. High-bit tagged pointers pose a somewhat tricky challenge for Rust, as the memory model still considers those high bits to be part of the address. Thus, from the memory model's perspective, changing those bits puts the pointer outside of its original allocation, despite it not being the case as far as the hardware & OS are concerned. This makes loads and stores using the pointer Undefined Behaviour, despite the fact that if such loads and stores were to be directly done in assembly they would be perfectly safe and valid.
Whenever this RFC refers to a "tagged pointer", it should be taken to mean a pointer that had some of its top bits set to non-0 values.
Tagged pointers are pointers in which the unused top bits are set to contain some metadata - the tag. No 64-bit architecture today actually uses a 64-bit address space. Most operating systems only use the lower 48 bits, leaving higher bits unused. The remaining bits are for the most part used to distinguish userspace pointers (0x00) from kernelspace pointers (0xff), at least on Linux. Certain architectures provide extensions, such as TBI on AArch64, that make it easier for programs to make use of those unused bits to insert custom metadata into the pointer without having to manually mask them out prior to every load and store. This tagging method can be used without said architecture extensions - by masking out the bits manually - albeit said extensions make it more efficient.
Currently, Rust does not support directly using TBI and related architecture extensions that
facilitate the use of tagged pointers. This could potentially cause issues in cases such as working
with TBI-enabled C/C++ components over FFI, or when writing a tagging memory allocator. While there
is no explicit support for this in C/C++, due to there not being Strict Provenance restrictions it
is straightforward to write a 'correct' pointer tagging implementation by simply doing a inttoptr
cast inside the memory allocator implementation, be it a custom C malloc
or using a custom C++
Allocator
. The goal of this effort is to create a Rust API for implementing this type of
functionality that is guaranteed to be free of Undefined Behaviour.
There needs to be a low-level helper in the standard library, despite the relatively niche use case and relative simplicity, so that there is a single known location where Miri hooks can be called to update the canonical address. This will make it easier to modify pointer addresses without breaking the Rust memory model in the process.
Guide-level explanation
This RFC adds one associated function to core::ptr
:
pub unsafe fn simulate_realloc<T>(mut original: *mut T, new_address: usize) -> *mut T
use core::ptr::simulate_realloc;
let tag = 63;
let new_addr = ptr as usize | tag << 56;
let tagged_ptr = unsafe { simulate_realloc(ptr, new_addr) };
The purpose of this function is to indicate to the compiler that an allocation that used to be
pointed to by a given pointer can now only be accessed by the new pointer with the provided new
address. This is supposed to be semantically equivalent to a realloc
from the untagged address to
the tagged address, and conceptually similar to a move - it is no longer valid to access the
allocation through the untagged pointer or any derived pointers. That being said, no actual
reallocation is done - the underlying memory does not change, it only changes within the Rust memory
model.
Reference-level explanation
As previously explained, the memory model we currently have is not fully compatible with memory tagging and tagged pointers. Setting the high bits of a pointer must be done with great care in order to avoid introducing Undefined Behaviour, which could arise as a result of violating pointer aliasing rules - using two 'live' pointers which have different 64-bit addresses but do point to the same chunk of memory would weaken alias analysis and related optimisations.
We can avoid this issue by simulating a realloc from the untagged address to the tagged address. To do so, we need the helper function to return a pointer that will be annotated in LLVM IR as noalias, as per the following excerpt.
On function return values, the noalias attribute indicates that the function acts like a system memory allocation function, returning a pointer to allocated storage disjoint from the storage for any other object accessible to the caller.
This will result in the new pointer getting a brand new provenance, disjoint from the provenance of the original pointer.
Every change to the high bits has to at least simulate a realloc and we must ensure the old pointers are invalidated. This is due to the aforementioned discrepancy between how Rust & LLVM see a memory address and how the OS & hardware see memory addresses. From the OS & hardware perspective, the high bits are reserved for metadata and do not actually form part of the address (in the sense of an 'address' being an index into the memory array). From the LLVM perspective, the high bits are part of the address and changing them means we are now dealing with a different address altogether. Having to reconcile those two views necessarily creates some friction and extra considerations.
Function signature, documentation and implementation:
/// Simulate a realloc to a new address
///
/// Intended for use with pointer tagging architecture features such as AArch64 TBI.
/// This function creates a new pointer with the address `new_address` and a brand new provenance,
/// simulating a realloc from the original address to the new address.
/// Note that this is only a simulated realloc - nothing actually gets moved or reallocated.
///
/// SAFETY: Users *must* ensure that `new_address` actually contains the same memory as the original.
/// The primary use-case is working with various architecture pointer tagging schemes, where two
/// different 64-bit addresses can point to the same chunk of memory due to some bits being ignored.
/// When used incorrectly, this function can be used to violate the memory model in arbitrary ways.
/// Furthermore, after using this function, users must ensure that the underlying memory is only ever
/// accessed through the newly created pointer. Any accesses through the original pointer
/// (or any pointers derived from it) would be Undefined Behaviour.
#[inline(never)]
#[unstable(feature = "ptr_simulate_realloc", issue = "none")]
#[cfg_attr(not(bootstrap), rustc_simulate_allocator)]
#[allow(fuzzy_provenance_casts)]
pub unsafe fn simulate_realloc<T>(original: *mut T, new_address: usize) -> *mut T {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
// How do we get a brand-new provenance for Strict Provenance?
let mut ptr = new_address as *mut T;
// SAFETY: This does not do anything
unsafe {
asm!("/* simulate realloc from {original} to {ptr} */",
original = in(reg) original, ptr = inout(reg) ptr);
}
// FIXME: call Miri hooks to update the address of the original allocation
ptr
}
To ensure that the function actually simulates a realloc, we need to make sure that it is treated
similarly to real allocator functions in the codegen stage. That is to say, the function return
value must be annotated with noalias
in LLVM, as explained earlier in this section. One way to do
so would be through a rustc built-in attribute similar to e.g. rustc_allocator
-
rustc_simulate_allocator
. This attribute will be passed down to the codegen stage so that the
codegen can appropriately annotate the function.
Drawbacks
Such a low-level helper is inherently highly unsafe and could be used to violate the memory model in many different ways, so it will have to be used with great care. The approach of simulating a realloc is unfortunate in that it makes the support we add to the language more restrictive than the actual hardware reality allows for, but this seems to be the only solution available for the time being as modifying the entire stack to support disregarding the top bits of a pointer would be a non-trivial endeavour.
Rationale and alternatives
Without having a dedicated library helper for modifying the address, users wanting to make use of high-bit tagging would have to resort to manually using bitwise operations and would be at risk of inadvertently introducing Undefined Behaviour. Having a helper for doing so in the library creates a place where e.g. Miri hooks can be called to let Miri know that a pointer's cannonical address has been updated.
It is most likely not feasible to make simulate_realloc()
safe to use regardless of the context,
hence the current approach is to make it an unsafe function with a safety notice about the user's
responsibilities.
Prior art
TBI already works in C, though mostly by default and care must be taken to make sure no Undefined Behaviour is introduced. The compiler does not take special steps to preserve the tags, but it doesn't try to remove them either. That being said, the C/C++ standard library does not take tagging schemes into account during alias analysis. With this proposal, Rust would have much better defined and safer support for TBI than C or C++.
Notably, Android already makes extensive use of TBI by tagging all heap allocations.
The idea is also not one specific to AArch64, as there are similar extensions present on other architectures that facilitate working with tagged pointers.
Unresolved questions
What is the best way to make this compatible with Strict Provenance? We want to be able to create a
pointer with an arbitrary address, detached from any existing pointers and with a brand-new
provenance. From the LLVM side this can be handled through generating inttoptr
which does not have
the same aliasing restrictions as getelementptr
alongside annotating the function return value as
noalias
which can be done with the aforementioned new built-in attribute. Is this enough for it to
fit within the Strict Provenance framework? If not, how can we make it fit?
What should the helper actually be called? Something like simulate_realloc
or change_addr
could
be useful at making it clear to the user what semantic implications using this helper has. There may
be better names that I have not thought of yet.
Future possibilities
With a low-level helper for changing the address such as the one proposed here, it would be trivial
to add helper functions for supporting specific tagging schemes to std::arch
. All of those
architecture-specific functions would internally use this helper.
Whilst the realloc-like approach is restrictive today, at some point in the future if the LLVM memory model gains an understanding that the address is only made up of the lower 56 bits, this restriction could be relaxed. It would then allow both the original and the tagged pointer to be valid and aliased at the same time.
On compatible platforms, interesting use-cases might be possible, e.g. tagging pointers when allocating memory in Rust in order to insert metadata that could be used in experiments with pointer strict provenance.