Repository navigation
Description
Original title: Status of provenance/aliasing issues detected by Miri
Hi, I wish I were being helpful here but I'm just reporting the status of some hackery I've done. (hours wasted here, etc)
This is one of the most-downloaded crates that roundtrips pointer values through integers. I'm looking into most such crates to understand if it's possible to rewrite such roundtrips or if there's something better that can be done to support what libraries want to do.
For this crate, I do not think that it is possible to patch out the roundtrips, but the problem isn't the round-tripping. This crate wants to store pointers inside Atom, and to do that in a well-defined way we need a type which is capable of storing provenance and also plain old bytes, so we could use a raw pointer type or MaybeUninit. But we want to uphold the niche which it currently has, so the only type which is eligible is NonNull. But putting NonNull in that field breaks string-cache-codegen, because it puts Atom in a macro_rules! pattern:
error: to use a constant of type `std::ptr::NonNull` in a pattern, `std::ptr::NonNull` must be annotated with `#[derive(PartialEq, Eq)]`
--> /tmp/markup5ever-0.11.0/target/miri/x86_64-unknown-linux-gnu/debug/build/markup5ever-85445da718f099e3/out/generated.rs:3381:39
|
3381 | ...html") => { $ crate :: ATOM_NAMESPACE__68_74_74_70_3A_2F_2F_77_77_77_2E_77_33_2E_6F_72_67_2F_31_39_39_39_2F_78_68_74_6D_6C } ;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
::: interface/tree_builder.rs:89:9
|
89 | expanded_name!(html "template") => flags.template = true,
| ------------------------------- in this macro invocation
|
= note: this error originates in the macro `namespace_url` (in Nightly builds, run with -Z macro-backtrace for more info)
I've put up the changes that would remove the ptr-int-ptr roundtrips (as well as some aliasing issues with Box that current SB has) in a branch in case anyone is interested: master...saethlin:fix-aliasing
Activity
SimonSapin commentedon Aug 19, 2026
In addition to the constant-in-pattern issue, we want something that is 64 bits wide even of 32-bit platforms
As a work-around,
MIRIFLAGS=-Zmiri-permissive-provenancecan silence this warning:Use exposed provenance APIs instead of pointer/integer casts
Run Miri and Clippy on CI (servo#304)
SimonSapin commentedon Aug 20, 2026
This could be worked around with enough
cfgs. The real issue is the competing constraints of string provenance wants use to store a real pointer, and constant-in-pattern wants us to store an integer. Even though in practice the cases where we can use a pattern (string part of the static set) is disjoint from cases where we use a pointer (heap-allocated ref-counted strings)adamreichold commentedon Aug 20, 2026
Would using a
unionhelp with this? Meaning keeping a pointer a pointer and a (64-bit) integer a (64-bit) integer and figuring this out at the union access sites?RalfJung commentedon Aug 20, 2026
Could you summarize what the type looks like and what kinds of values should be used as constants? I assume it is this one?
I think we do allow some raw pointers to be used in patterns, so I guess we could allow
NonNullif we do things carefully enough.SimonSapin commentedon Aug 20, 2026
Yes this is about the
Atomstruct. Its representation is 64 bits (with a niche forOption<Atom>) whose two least significant bits form a tag to distinguish three cases:0b00: a heap allocated pointer. alignment ensures the lower pointer bits are indeed zero0b01: a short inline string stored in the 7 upper bytes. The upper nibble of the lower byte stores the length.0b10: a string in a static (indexed) set. The upper 32 bits give the index.For statically known strings, we generate an
atom!("example")macro that expands to a namedconstitem that can be used in either expression or patterns. Suchconsts use the inline or static representation, never pointer.SimonSapin commentedon Aug 20, 2026
This might look like:
ptr::NonNull#[repr(C)] struct { ptr::NonNull, u32 }#[repr(C)] struct { u32, ptr::NonNull }I would like to use a
unionto encode the triple representation in the type system, but I suspect it’d hinder both having a niche forOption<Atom>and being able to useconstitems in patterns.[-]Status of provenance/aliasing issues detected by Miri[/-][+]Make string-cache compatible with Strict Provenance[/+]Run Miri and Clippy on CI (#304)