Skip to content

Handle new caller_location intrinsic the right way #374

Description

@adpaco

Rebase from #346 found a new caller_location intrinsic in the process. In order to unblock, we codegen this intrinsic as:

            "caller_location" => {
                codegen_unimplemented_intrinsic!("https://github.com/model-checking/rmc/issues/374")
            }

But we can probably codegen it the same way Rust does.

Activity

  1. added
    [C] Feature / Enhancement
    [E] Unsupported Construct
    and removed
    [F] Soundness
    on Nov 9, 2022
  2. celinval commented on Feb 21, 2023

    @celinval
    Contributor
  3. adpaco commented on Mar 2, 2023

    @adpaco
    ContributorAuthor

    Thanks for the update, @celinval !

    If adding support for caller_location would allow us to avoid panic messages such as thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', that'd be a major improvement in how we report such failures.

    One thing that I don't see mentioned there is the relation to panic strategies. This isn't mentioned in the RFC neither, so I guess it's independent.

  4. celinval commented on Mar 3, 2023

    @celinval
    Contributor

    I was wondering if it would help with: #1740

  5. celinval commented on Jul 3, 2023

    @celinval
    Contributor

    For the current Rust implementation, the compiler will add an extra argument to functions that contain #[track_caller]. Unfortunately, this operation is done in the codegen stage, instead of a MIR pass. :(

    My guess is that MIR passes can't really change the function signature, so they push it down to the codegen stage. I'm tempted to just encode this as a global variable for now.

    @adpaco-aws I don't think CBMC really supports formatting error messages, so I'm not sure implementing this intrinsic will make much of a difference on how we report failures.

    In order to fix that, we would have to either inline these functions or generate a different copy per location they are invoked from.

  6. celinval commented on Jul 3, 2023

    @celinval
    Contributor

    Implementation notes for myself:

    1. To build a constant that represents a location, use query const_caller_location [1].
    2. To get the type of the caller location, use function: pub fn caller_location_ty(self) -> Ty<'tcx> [2]
    3. We can check if a function requires caller location using requires_caller_location [3]
    4. If we are passing the caller location as an argument, we will need to create a ReifyShim for cases that require dynamic dispatch.
  7. joshlf commented on Aug 27, 2025

    @joshlf

    Ran into this when implementing zstd decompression (RFC 8878). Should I consider the proof function to be an incomplete proof as a result?

    Source:

    // https://datatracker.ietf.org/doc/html/rfc8878#name-window-descriptor
    #[repr(transparent)]
    struct WindowDescriptor(u8);
    
    impl WindowDescriptor {
        fn window_size(self) -> u64 {
            #[cfg(kani)]
            #[kani::proof]
            fn proof() {
                let desc = WindowDescriptor(kani::any());
                let size = desc.window_size();
                assert!(size <= (1u64 << 41) + 7*(1u64 << 38));
            }
    
            let exponent = self.0 >> 3;
            let mantissa = self.0 & 0b111;
    
            // SAFETY: Proven sound by Kani proof.
            let window_log = unsafe { 10u32.unchecked_add(u32::from(exponent)) };
            let window_base = 1u64 << window_log;
            // SAFETY: Proven sound by Kani proof.
            let window_add = unsafe { (window_base / 8).unchecked_mul(u64::from(mantissa)) };
            // SAFETY: Proven sound by Kani proof.
            unsafe { window_base.unchecked_add(window_add) }
        }
    }
  8. zhassan-aws commented on Sep 5, 2025

    @zhassan-aws
    Contributor

    Should I consider the proof function to be an incomplete proof as a result?

    No, it shouldn't. Verification would fail if the intrinsic is reachable from the proof harness.

  9. celinval commented on Jul 22, 2026

    @celinval
    Contributor

    This CR might be of interest here. It adds support to compute caller location using rustc_public APIs: rust-lang/rust#159204

  10. added
    Z-Kani Compiler
    on Nov 5, 2026
  11. self-assigned this
    on Nov 5, 2026
  12. feliperodri commented on Nov 6, 2026

    @feliperodri
    Member

    More evidence for this issue. Kani hit it while verifying these 2 crate(s):

    Minimal reproducer (Kani 0.68.0):

    pub fn loc() -> u32 {
        let l = std::panic::Location::caller();
        l.line()
    }
    pub fn clock(x: u8) -> u8 {
        let _t = std::time::Instant::now();
        x
    }
    pub fn ok_fn(x: u8, y: u8) -> u8 { x.wrapping_mul(y) }
    $ cargo kani autoharness -Z autoharness
    Thread 60: VERIFICATION RESULT:
    Thread 60: Failed Checks: caller_location is not currently supported by Kani. Please post your example at https://github.com/model-checking/kani/issues/374
    Thread 60: VERIFICATION:- FAILED
    Thread 62: VERIFICATION RESULT:
    Thread 62: VERIFICATION:- SUCCESSFUL
    Thread 59: VERIFICATION RESULT:
    

    Context: we ran AI agents that use the Kani agent plugin (#5059) to verify 101 of the most downloaded crates on crates.io with Kani 0.68.0, CBMC 6.11.0 and nightly-2026-10-22. The minimal reproducer above was re-run by hand with the same versions. This text was written with AI assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Z-Kani Compiler[C] Feature / Enhancement[E] Unsupported Construct

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions