Skip to content

Miri allows calling function item via null reference. #5226

Description

@theemathas

I'm not sure if this is a bug, but it does seem really weird.

The following code constructs a null reference pointing to a function item type. Then, it calls that null reference.

fn foo() {}

fn main() {
    let mut f = &foo;
    unsafe {
        (&raw mut f).cast::<usize>().write(0);
    }
    f();
}

According to Miri, the above code somehow doesn't have any UB. This seems wrong.

Strangely, adding another layer of reference causes Miri to report UB:

fn foo() {}

fn main() {
    let mut f = &&foo;
    unsafe {
        (&raw mut f).cast::<usize>().write(0);
    }
    f();
}
error: Undefined Behavior: constructing invalid value of type &&fn() {foo}: encountered a null reference
 --> src/main.rs:8:5
  |
8 |     f();
  |     ^ Undefined Behavior occurred here
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error

Reproducible on the playground with Miri version 0.1.0 (2026-07-22 6f72b5dd5f)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-misses-theoretical-UBImpact: Miri does not report UB when it should, but codegen also "misses" this UB

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions