Skip to content

Fix metadata on PtrToPtr cast#941

Merged
automergerpr-permission-manager[bot] merged 4 commits intomasterfrom
dc/fix-pointer-cast
Feb 18, 2026
Merged

Fix metadata on PtrToPtr cast#941
automergerpr-permission-manager[bot] merged 4 commits intomasterfrom
dc/fix-pointer-cast

Conversation

@dkcumming
Copy link
Collaborator

PtrToPtr casts were mistakenly ignoring metadata ORIGIN_SIZE and setting it to noMetadataSize after the cast each time. This PR preserves the ORIGIN_SIZE across the cast.

More Details

This issue was discovered working with Iterators, in particular the relationship between std::slice::Iter<'_, T>::new and <std::slice::iter<'_, thing> as std::iter::Iterator>::next. When iterating through a collection there was no match at the end that should of terminated iterating. An Iter tracks position through the collection with 2 pointers:

pub struct Iter<'a, T: 'a> {
    /// The pointer to the next element to return, or the past-the-end location
    /// if the iterator is empty.
    ///
    /// This address will be used for all ZST elements, never changed.
    ptr: NonNull<T>,
    /// For non-ZSTs, the non-null pointer to the past-the-end element.
    ///
    /// For ZSTs, this is `ptr::without_provenance_mut(len)`.
    end_or_len: *const T,
    _marker: PhantomData<&'a T>,
}

In order to terminate ptr == end_or_len, however in KMIR this check was always failing due to the pointer representation:
image

Pointers in KMIR check equality on all metadata fields, so this equality would never pass. The correct ORIGIN_SIZE should be dynamicSize(2) as they are pointers to elements of a slice. The Iter is instantiated with the incorrect ORIGIN_SIZE.

The construction of an Iter by the new function does a series of pointer casts. The cast that strips the ORIGIN_SIZE in KMIR came from _5 = Cast-PtrToPtr mv(6) which is a cast of *mut Thing to *const Thing:
image

Copy link
Collaborator

@mariaKt mariaKt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you.

@automergerpr-permission-manager automergerpr-permission-manager bot merged commit 7453069 into master Feb 18, 2026
7 checks passed
@automergerpr-permission-manager automergerpr-permission-manager bot deleted the dc/fix-pointer-cast branch February 18, 2026 04:28
@dkcumming dkcumming mentioned this pull request Feb 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments