CFI: NonNull<T> is encoded as a pattern type, so C/C++ calls through a pointer fail the CFI check
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 18/100
Hướng nghiên cứu
Reproduce with the lib.rs/main.c pair and compare the --emit=llvm-ir type names (_ZTSFvP1SE versus _ZTSFvu3patIPK1S!nullEE). Then read PR 136006, which introduced the (*const T is !null) pattern type, and follow where rustc lowers types into LLVM CFI type metadata. Done means NonNull<T> is emitted as *mut T so the C and Rust sides agree and the CFI reproducer runs clean.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
I tried this code:
lib.rs:
use std::ptr::NonNull;
#[repr(C)]
pub struct S;
#[no_mangle]
pub extern "C" fn by_mut_ptr(_: *mut S) {}
#[no_mangle]
pub extern "C" fn by_nonnull(_: NonNull<S>) {}
main.c
#include <stdio.h>
struct S;
void by_mut_ptr(struct S *);
void by_nonnull(const struct S *);
int main(void) {
void (*volatile f)(struct S *) = by_mut_ptr;
f((struct S *)1);
fputs("by_mut_ptr: ok\n", stderr);
void (*volatile g)(const struct S *) = by_nonnull;
g((const struct S *)1);
fputs("by_nonnull: ok\n", stderr);
return 0;
}
Build and execute:
$ rustc +nightly --edition=2021 --crate-type=staticlib -Copt-level=1 -Clinker-plugin-lto \
-Zsanitizer=cfi -Cunsafe-allow-abi-mismatch=sanitizer lib.rs
$ clang -flto -fvisibility=hidden -fsanitize=cfi-icall -fuse-ld=lld -O1 main.c liblib.a -o main --target=x86_64-unknown-linux-gnu
$ ./main
I expect the binary to work, but it fails CFI:
$ ./main
by_mut_ptr: ok
Illegal instruction (core dumped) ./main
If I instead build with diagnostics the error shows:
$ rustc +nightly --edition=2021 --crate-type=staticlib -Copt-level=1 -Clinker-plugin-lto \
-Zsanitizer=cfi -Cunsafe-allow-abi-mismatch=sanitizer lib.rs
$ clang --target=x86_64-unknown-linux-gnu -flto -fvisibility=hidden -fsanitize=cfi-icall \
-fno-sanitize-trap=cfi-icall -fsanitize-recover=cfi-icall -fuse-ld=lld -O1 \
main.c liblib.a -o main
$ ./main
by_mut_ptr: ok
main.c:12:5: runtime error: control flow integrity check for type 'void (const struct S *)' failed during indirect function call
(/tmp/test/main+0x483c9): note: by_nonnull defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior main.c:12:5
by_nonnull: ok
--emit=llvm-ir shows that NonNull<S> is encoded with its pattern type, which has no C or C++ equivalent:
- by_mut_ptr:
_ZTSFvP1SE - by_nonnull:
_ZTSFvu3patIPK1S!nullEE
This is a regression from https://github.com/rust-lang/rust/pull/136006, which made NonNull a pattern type (*const T is !null): nightly-2026-04-14 encodes by_nonnull as _ZTSFvPK1SE and the program passes, nightly-2026-04-15 fails.
Since NonNull<T> has the same ABI as *mut T and is a common way to pass pointers across FFI, encoding it as *mut T would make it match C and C++ pointers in both directions. See also #155361 and https://github.com/rust-lang/unsafe-code-guidelines/issues/489.
Meta
rustc --version --verbose:
rustc 1.102.0-nightly (8eb778d04 2026-11-12)
binary: rustc
commit-hash: 8eb778d04094d3af4b26580cab327e0413eb1e02
commit-date: 2026-11-12
host: x86_64-unknown-linux-gnu
release: 1.102.0-nightly
LLVM version: 23.1.5
- Ngôn ngữ chính
- Rust
- Star
- 123k
- Fork
- 16.6k
- Merge trung bình
- 2 ngày 17 giờ
- Pull request đã merge (30 ngày)
- 532
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của rust-lang/rust
-
strictly_dominates missing a `=`Đang mởC-bug needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
rust-lang/rust#166722 · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
A-attributes relnotes relnotes-needs-review relnotes-tracking-issue T-compiler T-lang
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
A-run-make C-bug T-bootstrap
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Tracking issue for release notes of #161531: Remove `T: Send + Sync` bound from `Arc::downcast`Đang mởrelnotes relnotes-tracking-issue T-libs
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
A-str A-Unicode relnotes relnotes-tracking-issue T-libs
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của rust-lang/rust
Issue tương tự
-
bug P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug core-lang
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Windows microphone listing treats words in a device name as DirectShow labelsCó thể đã có người làm @Sujal-chaudhary đã nhận hôm nay. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
clipy-recorder/clipy#447 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
nightly-cleanup
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
max-sixty/worktrunk#4939 · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày