You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Check vtable validity when explicitly creating wide pointer #3738
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]
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:
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.
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.
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.
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.
Activity
miguelcorderopamphile commentedon Oct 14, 2026
Hi. I traced this through the codebase. Vtables are codegen'd as
Kani::CommonVTableglobals incodegen_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_partsgoes through normal MIR unchecked.Two things I'd like your input on before writing code:
--restrict-vtableanalysis, which has a known soundness issue (Function Pointer Restrictions Imprecision #802), so I'd rather not extend that path in an unsound way.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.miguelcorderopamphile commentedon Oct 14, 2026
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.miguelcorderopamphile commentedon Oct 14, 2026
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 Band used fordyn Ano 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.