| 1 | # sptr: The Strict Provenance Polyfill |
| 2 | |
| 3 | [](https://crates.io/crates/sptr) [](https://docs.rs/sptr)  |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | This library provides a stable polyfill for Rust's [Strict Provenance] experiment. |
| 9 | |
| 10 | # Mapping to STD APIs: |
| 11 | |
| 12 | This crate "overlays" a bunch of unstable std apis, here are the mappings: |
| 13 | |
| 14 | ## core::ptr (sptr) |
| 15 | |
| 16 | * `pub fn `[`invalid`]`<T>(addr: usize) -> *const T;` |
| 17 | * `pub fn `[`invalid_mut`]`<T>(addr: usize) -> *mut T;` |
| 18 | * `pub fn `[`from_exposed_addr`]`<T>(addr: usize) -> *const T;` |
| 19 | * `pub fn `[`from_exposed_addr_mut`]`<T>(addr: usize) -> *mut T;` |
| 20 | |
| 21 | |
| 22 | ## core::pointer (sptr::Strict) |
| 23 | |
| 24 | * `pub fn `[`addr`]`(self) -> usize;` |
| 25 | * `pub fn `[`expose_addr`]`(self) -> usize;` |
| 26 | * `pub fn `[`with_addr`]`(self, addr: usize) -> Self;` |
| 27 | * `pub fn `[`map_addr`]`(self, f: impl FnOnce(usize) -> usize) -> Self;` |
| 28 | |
| 29 | |
| 30 | ## NON-STANDARD EXTENSIONS (disabled by default, use at your own risk) |
| 31 | |
| 32 | * `sptr::`[`uptr`] (feature = uptr) |
| 33 | * `sptr::`[`iptr`] (feature = uptr) |
| 34 | * `sptr::`[`OpaqueFnPtr`] (feature = opaque_fn) |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | # Applying The Overlay |
| 40 | |
| 41 | Swapping between sptr and core::ptr should be as simple as switching between `sptr::` and `ptr::` |
| 42 | for static functions. For methods, you must import `sptr::Strict` into your module for |
| 43 | the extension trait's methods to overlay std. The compiler will (understandably) |
| 44 | complain that you are overlaying std, so you will need to also silence that as |
| 45 | seen in the following example: |
| 46 | |
| 47 | ```rust |
| 48 | #![allow(unstable_name_collisions)] |
| 49 | use sptr::Strict; |
| 50 | |
| 51 | let ptr = sptr::invalid_mut::<u8>(1); |
| 52 | println!("{}", ptr.addr()); |
| 53 | ``` |
| 54 | |
| 55 | By default, this crate will also mark methods on pointers as "deprecated" if they are |
| 56 | incompatible with strict_provenance. If you don't want this, set `default-features = false` |
| 57 | in your Cargo.toml. |
| 58 | |
| 59 | Rust is the canonical source of definitions for these APIs and semantics, but the docs |
| 60 | here will vaguely try to mirror the docs checked into Rust. |
| 61 | |
| 62 | |
| 63 | [`invalid`]: https://docs.rs/sptr/latest/sptr/fn.invalid.html |
| 64 | [`invalid_mut`]: https://docs.rs/sptr/latest/sptr/fn.invalid_mut.html |
| 65 | [`from_exposed_addr`]: https://docs.rs/sptr/latest/sptr/fn.from_exposed_addr.html |
| 66 | [`from_exposed_addr_mut`]: https://docs.rs/sptr/latest/sptr/fn.from_exposed_addr_mut.html |
| 67 | [`addr`]: https://docs.rs/sptr/latest/sptr/trait.Strict.html#tymethod.addr |
| 68 | [`expose_addr`]: https://docs.rs/sptr/latest/sptr/trait.Strict.html#tymethod.expose_addr |
| 69 | [`with_addr`]: https://docs.rs/sptr/latest/sptr/trait.Strict.html#tymethod.with_addr |
| 70 | [`map_addr`]: https://docs.rs/sptr/latest/sptr/trait.Strict.html#tymethod.map_addr |
| 71 | [`uptr`]: https://docs.rs/sptr/latest/sptr/int/struct.uptr.html |
| 72 | [`iptr`]: https://docs.rs/sptr/latest/sptr/int/struct.iptr.html |
| 73 | [`OpaqueFnPtr`]: https://docs.rs/sptr/latest/sptr/func/struct.OpaqueFnPtr.html |