Skip to main content

return_address

Macro return_address 

Source
pub macro return_address() {
    ...
}
🔬This is a nightly-only experimental API. (return_address #154966)
Expand description

The core::arch::return_address!() macro returns a pointer with an address that corresponds to the caller of the function that invoked the return_address!() macro. The pointer has no provenance, as if created by core::ptr::without_provenance. It cannot be used to read memory (other than ZSTs).

The value returned by the macro depends highly on the architecture and compiler (including any options set). In particular, it is allowed to be wrong (particularly if inlining is involved), or even contain a nonsense value. The result of this macro must not be relied upon for soundness or correctness, only for debugging purposes.

As a best effort, if a useful value cannot be determined (for example, due to limitations on the current codegen), this macro tries to return a null pointer instead of nonsense (this cannot be relied upon for correctness, however).

Formally, this function returns a pointer with a non-deterministic address and no provenance.

This is equivalent to the gcc __builtin_return_address(0) intrinsic (other forms of the intrinsic are not supported). Because the operation can be always performed by the compiler without crashing or causing undefined behaviour, invoking the macro is a safe operation.

§Example

#![feature(return_address)]

let addr = core::arch::return_address!();
println!("Caller is {addr:p}");