CFI: Fn closure called through dyn FnMut fails the CFI check

Abierto
#166,062 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
47/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
rust

Línea de trabajo

Start by reproducing the minimal example with the nightly rustc command and CFI flags shown in the issue, then read the referenced Rust issues #144641 and #160230 for related context. The report identifies a mismatch in the CFI check for a closure called through dyn FnMut, but does not name the compiler source files or tests involved. Done means the reduced example runs without the CFI failure and a regression test covers it.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

A-ABI A-control-flow-integrity A-dyn-trait C-bug requires-nightly T-compiler

Found when running ClickHouse with CFI which includes the ipnet crate, version 2.12.0:

contrib/rust_vendor/ipnet-2.12.0/src/lib.rs:1:1: runtime error: control flow integrity check for type
fn(&mut dyn for<'a, 'b> FnMut(&'a mut parser::Parser<'b>) -> Option<ipnet::IpNet>, &mut parser::Parser<'_>) -> Option<ipnet::IpNet>
failed during indirect function call
note: <<ipnet::parser::Parser>::read_ip_net::{closure#0} as FnOnce<(&mut ipnet::parser::Parser,)>>::call_once::{shim:vtable#0} defined here

Original code in ipnet but it's reduced to this code:

fn main() {
    let f = || std::env::args().count();
    let mut b: Box<dyn FnMut() -> usize> = Box::new(f);
    println!("{}", b());
}

I expected to see only the expected line (1), instead the binary crashes with SIGILL or, if we change cfi to show the warning instead of crashing, it shows a warning:

$ rustc +nightly --edition=2021 -Copt-level=1 -Clto -Ccodegen-units=1 \
    -Zsanitizer=cfi -Zsanitizer-cfi-diag -Zsanitizer-cfi-recover \
    -Cunsafe-allow-abi-mismatch=sanitizer fn_as_fnmut.rs
$ ./fn_as_fnmut 
fn_as_fnmut.rs:1:1: runtime error: control flow integrity check for type fn(&mut dyn std::ops::FnMut() -> usize) -> usize failed during indirect function call
fn_as_fnmut.625fcbea4de00cd8-cgu.0: note: fn_as_fnmut::main::{closure#0} defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior fn_as_fnmut.rs:1:1 
1

It seems related to https://github.com/rust-lang/rust/issues/144641 cc @rcvalle
https://github.com/rust-lang/rust/pull/160230 also looks related, but the repro fails with and without it

Meta

rustc --version --verbose:

$ rustc +nightly --version --verbose
rustc 1.102.0-nightly (5f4effd57 2026-11-13)
binary: rustc
commit-hash: 5f4effd579500175af844241eb179e756190f1e7
commit-date: 2026-11-13
host: x86_64-unknown-linux-gnu
release: 1.102.0-nightly
LLVM version: 23.1.5

Also tested with:

  • nightly-2024-04-01
  • nightly-2025-06-01
  • And stable 1.100.0 (2026-11-09), with RUSTC_BOOTSTRAP=1
Backtrace

Core was generated by `./fn_as_fnmut'.
Program terminated with signal SIGILL, Illegal instruction.
#0  0x0000562447661b6e in fn_as_fnmut::main ()
(gdb) bt
#0  0x0000562447661b6e in fn_as_fnmut::main ()
#1  0x0000562447661b42 in std::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()> ()
#2  0x0000562447661b59 in std::rt::lang_start::<()>::{closure#0} ()
#3  0x000056244769abd6 in core::ops::function::impls::{impl#2}::call_once<(), (dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe)>
    () at library/core/src/ops/function.rs:287
#4  std::panicking::catch_unwind::do_call<&(dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe), i32> ()
    at library/std/src/panicking.rs:573
#5  std::panicking::catch_unwind<i32, &(dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe)> () at library/std/src/panicking.rs:541
#6  std::panic::catch_unwind<&(dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe), i32> () at library/std/src/panic.rs:359
#7  std::rt::lang_start_internal::{closure#0} () at library/std/src/rt.rs:175
#8  std::panicking::catch_unwind::do_call<std::rt::lang_start_internal::{closure_env#0}, isize> () at library/std/src/panicking.rs:573
#9  std::panicking::catch_unwind<isize, std::rt::lang_start_internal::{closure_env#0}> () at library/std/src/panicking.rs:541
#10 std::panic::catch_unwind<std::rt::lang_start_internal::{closure_env#0}, isize> () at library/std/src/panic.rs:359
#11 std::rt::lang_start_internal () at library/std/src/rt.rs:171
#12 0x0000562447661b94 in main ()

Lenguaje dominante
Rust
Estrellas
122k
Forks
16.7k
Merge medio
2 d 16 h
PR fusionados (30 d)
515

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rust-lang/rust

Todos los issues de rust-lang/rust

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.