Skip to content

Check vtable validity when explicitly creating wide pointer #3738

Description

@celinval

Requested feature: Ensure that users do not incorrectly generate invalid vtable data.
Use case: Detect UB due to invalid wide pointer when using APIs such as from_raw_parts.
Link to relevant documentation (Rust reference, Nomicon, RFC):

  • dyn Trait metadata must be a pointer to a compiler-generated vtable for Trait. (For raw pointers, this requirement remains a subject of some debate. [ref]

Activity

  1. added
    [C] Feature / Enhancement
    [E] Unsupported UB
    on Nov 25, 2024
  2. miguelcorderopamphile commented on Oct 14, 2026

    @miguelcorderopamphile

    Hi. I traced this through the codebase. Vtables are codegen'd as Kani::CommonVTable globals in codegen_vtable (rvalue.rs) and get a size sanity check (check_vtable_size), but nothing validates the vtable metadata when a wide pointer is built from raw parts: ptr::from_raw_parts goes through normal MIR unchecked.

    Two things I'd like your input on before writing code:

    1. Where should the check live: at construction, or at the dyn-call site before dispatch? The call-site option overlaps with the --restrict-vtable analysis, which has a known soundness issue (Function Pointer Restrictions Imprecision #802), so I'd rather not extend that path in an unsound way.
    2. First-version scope: detect metadata that provably is not any vtable Kani knows about (a constant or arbitrary integer), and leave opaque pass-through of valid metadata alone. The reference marks raw-pointer validity as still under debate, so over-flagging seems worse than under-flagging for a first version.

    If that direction makes sense I can implement it with a regression test (from_raw_parts(data, bogus) + dyn call) and adapt to the design you prefer.

  3. miguelcorderopamphile commented on Oct 14, 2026

    @miguelcorderopamphile

    One more constraint I hit while mapping the mechanics: a virtual call site and the vtables it may legitimately see can live in different crates, so a per-crate codegen check can't know the full set of valid vtables. The data aggregated for --restrict-vtable (after linking) does have both the labeled call sites and the possible targets, so a check variant would seem to fit next to the restriction instrumentation rather than at codegen. I'll leave that as the design question; happy to prototype whichever direction you prefer.

  4. miguelcorderopamphile commented on Oct 14, 2026

    @miguelcorderopamphile

    Prototyped this and opened #5001. It adds the check at virtual call sites: if the vtable pointer matches a vtable generated for a different principal trait, verification fails, so metadata taken from dyn B and used for dyn A no longer verifies. It's the call-site variant, not the post-link one from the comment above, so metadata pointing to a cross-crate vtable is not caught yet — happy to extend it there if that's the preferred direction. The PR includes a regression test.

  5. added
    T-High Priority
    on Nov 5, 2026
  6. added this to the Soundness milestone on Nov 5, 2026
  7. self-assigned this
    on Nov 5, 2026
  8. added
    [F] Soundness
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

T-High Priority[C] Feature / Enhancement[E] Unsupported UB[F] Soundness

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions