From f8aecf51682f38d2f2648420d71570dff10199aa Mon Sep 17 00:00:00 2001 From: Urgau Date: Fri, 26 Jun 2026 19:10:51 +0200 Subject: [PATCH 1/3] Lint against invalid POSIX function definitions --- compiler/rustc_hir/src/lang_items.rs | 8 ++ compiler/rustc_hir/src/weak_lang_items.rs | 8 ++ compiler/rustc_lint/src/runtime_symbols.rs | 20 +++- compiler/rustc_span/src/symbol.rs | 8 ++ library/std/src/lib.rs | 1 + library/std/src/sys/alloc/unix.rs | 19 ++++ library/std/src/sys/exit.rs | 19 ++++ library/std/src/sys/fs/unix.rs | 22 ++++ tests/ui/lint/runtime-symbols-no-std.rs | 27 +++++ tests/ui/lint/runtime-symbols-unix.rs | 57 ++++++++++ tests/ui/lint/runtime-symbols-unix.stderr | 117 +++++++++++++++++++++ 11 files changed, 302 insertions(+), 4 deletions(-) create mode 100644 tests/ui/lint/runtime-symbols-no-std.rs create mode 100644 tests/ui/lint/runtime-symbols-unix.rs create mode 100644 tests/ui/lint/runtime-symbols-unix.stderr diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index 9c3f7789f9ed9..b33eb892eb22c 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -455,6 +455,14 @@ language_item_table! { MemCmp, sym::memcmp_fn, memcmp_fn, Target::Fn, GenericRequirement::None; Bcmp, sym::bcmp_fn, bcmp_fn, Target::Fn, GenericRequirement::None; StrLen, sym::strlen_fn, strlen_fn, Target::Fn, GenericRequirement::None; + Open, sym::open_fn, open_fn, Target::Fn, GenericRequirement::None; + Read, sym::read_fn, read_fn, Target::Fn, GenericRequirement::None; + Write, sym::write_fn, write_fn, Target::Fn, GenericRequirement::None; + Close, sym::close_fn, close_fn, Target::Fn, GenericRequirement::None; + Malloc, sym::malloc_fn, malloc_fn, Target::Fn, GenericRequirement::None; + Realloc, sym::realloc_fn, realloc_fn, Target::Fn, GenericRequirement::None; + Free, sym::free_fn, free_fn, Target::Fn, GenericRequirement::None; + Exit, sym::exit_fn, exit_fn, Target::Fn, GenericRequirement::None; } /// The requirement imposed on the generics of a lang item diff --git a/compiler/rustc_hir/src/weak_lang_items.rs b/compiler/rustc_hir/src/weak_lang_items.rs index f9d94bd505724..5b17eace85a95 100644 --- a/compiler/rustc_hir/src/weak_lang_items.rs +++ b/compiler/rustc_hir/src/weak_lang_items.rs @@ -47,4 +47,12 @@ weak_only_lang_items! { MemCmp, Bcmp, StrLen, + Open, + Read, + Write, + Close, + Malloc, + Realloc, + Free, + Exit, } diff --git a/compiler/rustc_lint/src/runtime_symbols.rs b/compiler/rustc_lint/src/runtime_symbols.rs index a583ee70074b6..d1a7bec10db12 100644 --- a/compiler/rustc_lint/src/runtime_symbols.rs +++ b/compiler/rustc_lint/src/runtime_symbols.rs @@ -11,7 +11,7 @@ use crate::{LateContext, LateLintPass, LintContext}; declare_lint! { /// The `invalid_runtime_symbol_definitions` lint checks the signature of items whose - /// symbol name is a runtime symbol expected by `core` differs significantly from the + /// symbol name is a runtime symbol expected by `core` or `std` differs significantly from the /// expected signature (like mismatch ABI, mismatch C variadics, mismatch argument count, /// missing return type, ...). /// @@ -32,7 +32,8 @@ declare_lint! { /// standard-library facility or undefined behavior may occur. /// /// The symbols currently checked are `memcpy`, `memmove`, `memset`, `memcmp`, - /// `bcmp` and `strlen`. + /// `bcmp`, `strlen`, as well as the following POSIX symbols: `open`, `read`, `write` + /// `close`, `malloc`, `realloc`, `free` and `exit`. /// /// [^1]: https://doc.rust-lang.org/core/index.html#how-to-use-the-core-library pub INVALID_RUNTIME_SYMBOL_DEFINITIONS, @@ -42,7 +43,7 @@ declare_lint! { declare_lint! { /// The `suspicious_runtime_symbol_definitions` lint checks the signature of items whose - /// symbol name is a runtime symbol expected by `core`. + /// symbol name is a runtime symbol expected by `core` or `std`. /// /// ### Example /// @@ -62,7 +63,8 @@ declare_lint! { /// standard-library facility or undefined behavior may occur. /// /// The symbols currently checked are `memcpy`, `memmove`, `memset`, `memcmp`, - /// `bcmp` and `strlen`. + /// `bcmp`, `strlen`, as well as the following POSIX symbols: `open`, `read`, `write` + /// `close`, `malloc`, `realloc`, `free` and `exit`. /// /// [^1]: https://doc.rust-lang.org/core/index.html#how-to-use-the-core-library pub SUSPICIOUS_RUNTIME_SYMBOL_DEFINITIONS, @@ -73,12 +75,22 @@ declare_lint! { declare_lint_pass!(RuntimeSymbols => [INVALID_RUNTIME_SYMBOL_DEFINITIONS, SUSPICIOUS_RUNTIME_SYMBOL_DEFINITIONS]); static EXPECTED_SYMBOLS: &[ExpectedSymbol] = &[ + // `core` symbols ExpectedSymbol { symbol: "memcpy", lang: LanguageItems::memcpy_fn }, ExpectedSymbol { symbol: "memmove", lang: LanguageItems::memmove_fn }, ExpectedSymbol { symbol: "memset", lang: LanguageItems::memset_fn }, ExpectedSymbol { symbol: "memcmp", lang: LanguageItems::memcmp_fn }, ExpectedSymbol { symbol: "bcmp", lang: LanguageItems::bcmp_fn }, ExpectedSymbol { symbol: "strlen", lang: LanguageItems::strlen_fn }, + // POSIX symbols + ExpectedSymbol { symbol: "open", lang: LanguageItems::open_fn }, + ExpectedSymbol { symbol: "read", lang: LanguageItems::read_fn }, + ExpectedSymbol { symbol: "write", lang: LanguageItems::write_fn }, + ExpectedSymbol { symbol: "close", lang: LanguageItems::close_fn }, + ExpectedSymbol { symbol: "malloc", lang: LanguageItems::malloc_fn }, + ExpectedSymbol { symbol: "realloc", lang: LanguageItems::realloc_fn }, + ExpectedSymbol { symbol: "free", lang: LanguageItems::free_fn }, + ExpectedSymbol { symbol: "exit", lang: LanguageItems::exit_fn }, ]; #[derive(Copy, Clone, Debug)] diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index c75da250efd5f..fb291747d9026 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -615,6 +615,7 @@ symbols! { clone_closures, clone_fn, clone_from, + close_fn, closure, closure_lifetime_binder, closure_to_fn_coercion, @@ -898,6 +899,7 @@ symbols! { exhaustive_integer_patterns, exhaustive_patterns, existential_type, + exit_fn, exp2f16, exp2f32, exp2f64, @@ -1009,6 +1011,7 @@ symbols! { format_unsafe_arg, fp, framework, + free_fn, freeze, freeze_impls, freg, @@ -1248,6 +1251,7 @@ symbols! { macro_vis_matcher, macros_in_extern, main, + malloc_fn, managed_boxes, manually_drop, map, @@ -1473,6 +1477,7 @@ symbols! { on_unmatched_args, opaque, opaque_module_name_placeholder: "", + open_fn, ops, opt_out_copy, optimize, @@ -1656,9 +1661,11 @@ symbols! { raw_identifiers, raw_ref_op, re_rebalance_coherence, + read_fn, read_via_copy, readonly, realloc, + realloc_fn, realtime, reason, reborrow, @@ -2359,6 +2366,7 @@ symbols! { write_box_via_move, write_bytes, write_fmt, + write_fn, write_macro, write_str, write_via_move, diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 1b27be8e2dde3..6225366b96818 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -321,6 +321,7 @@ #![feature(borrowed_buf_init)] #![feature(bstr)] #![feature(bstr_internals)] +#![feature(c_size_t)] #![feature(cast_maybe_uninit)] #![feature(char_internals)] #![feature(clone_to_uninit)] diff --git a/library/std/src/sys/alloc/unix.rs b/library/std/src/sys/alloc/unix.rs index 3d369b08abc77..fbf06e4382abb 100644 --- a/library/std/src/sys/alloc/unix.rs +++ b/library/std/src/sys/alloc/unix.rs @@ -2,6 +2,25 @@ use super::{MIN_ALIGN, realloc_fallback}; use crate::alloc::{GlobalAlloc, Layout, System}; use crate::ptr; +// Used by rustc for checking the definitions of other function with the same symbol names +// +// See the `invalid_runtime_symbols_definitions` lint. +#[cfg(not(test))] +mod runtime_symbols { + use core::ffi::{c_size_t, c_void}; + + unsafe extern "C" { + #[lang = "malloc_fn"] + fn malloc(size: c_size_t) -> *mut c_void; + + #[lang = "realloc_fn"] + fn realloc(ptr: *mut c_void, size: c_size_t) -> *mut c_void; + + #[lang = "free_fn"] + fn free(ptr: *mut c_void); + } +} + #[stable(feature = "alloc_system_type", since = "1.28.0")] unsafe impl GlobalAlloc for System { #[inline] diff --git a/library/std/src/sys/exit.rs b/library/std/src/sys/exit.rs index 53fb92ba077e0..c4fa3af803071 100644 --- a/library/std/src/sys/exit.rs +++ b/library/std/src/sys/exit.rs @@ -68,6 +68,25 @@ cfg_select! { } } +#[cfg(not(test))] +cfg_select! { + any( + target_family = "unix", + target_os = "wasi", + ) => { + // Used by rustc for checking the definitions of other function with the same symbol names + // + // See the `invalid_runtime_symbols_definitions` lint. + mod runtime_symbols { + unsafe extern "C" { + #[lang = "exit_fn"] + fn exit(status: core::ffi::c_int) -> !; + } + } + } + _ => {} +} + pub fn exit(code: i32) -> ! { cfg_select! { target_os = "hermit" => { diff --git a/library/std/src/sys/fs/unix.rs b/library/std/src/sys/fs/unix.rs index 3152a22534f6c..1ef051c8896c3 100644 --- a/library/std/src/sys/fs/unix.rs +++ b/library/std/src/sys/fs/unix.rs @@ -100,6 +100,28 @@ use crate::sys::weak::weak; use crate::sys::{AsInner, AsInnerMut, FromInner, IntoInner, cvt, cvt_r}; use crate::{mem, ptr}; +// Used by rustc for checking the definitions of other function with the same symbol names +// +// See the `invalid_runtime_symbols_definitions` lint. +#[cfg(not(test))] +mod runtime_symbols { + use core::ffi::{c_char, c_int, c_size_t, c_ssize_t, c_void}; + + unsafe extern "C" { + #[lang = "open_fn"] + fn open(pathname: *const c_char, flags: c_int, ...) -> c_int; + + #[lang = "read_fn"] + fn read(fd: c_int, buf: *mut c_void, count: c_size_t) -> c_ssize_t; + + #[lang = "write_fn"] + fn write(fd: c_int, buf: *const c_void, count: c_size_t) -> c_ssize_t; + + #[lang = "close_fn"] + fn close(fd: c_int) -> c_int; + } +} + pub struct File(FileDesc); // FIXME: This should be available on Linux with all `target_env`. diff --git a/tests/ui/lint/runtime-symbols-no-std.rs b/tests/ui/lint/runtime-symbols-no-std.rs new file mode 100644 index 0000000000000..3d7133e523f88 --- /dev/null +++ b/tests/ui/lint/runtime-symbols-no-std.rs @@ -0,0 +1,27 @@ +// This test makes sure that the following symbols are not linked against +// in a #[no_std] context. + +//@ check-pass + +#![no_std] +#![crate_type = "lib"] + +use core::ffi::{c_char, c_int, c_void}; + +#[no_mangle] +pub fn open() {} + +extern "C" { + pub fn read(); + pub fn write(); +} + +#[no_mangle] +pub static close: () = (); + +extern "C" { + pub fn malloc(); + pub fn realloc(); + pub fn free(); + pub fn exit(); +} diff --git a/tests/ui/lint/runtime-symbols-unix.rs b/tests/ui/lint/runtime-symbols-unix.rs new file mode 100644 index 0000000000000..517cf765195f3 --- /dev/null +++ b/tests/ui/lint/runtime-symbols-unix.rs @@ -0,0 +1,57 @@ +// This test checks the runtime symbols lint with the Unix symbols. + +//@ only-unix +//@ edition: 2021 +//@ normalize-stderr: "\*const [iu]8" -> "*const U8" + +#![feature(c_variadic)] +#![allow(clashing_extern_declarations)] // we are voluntarily testing different definitions + +use core::ffi::{c_char, c_int, c_void}; + +fn invalid() { + #[no_mangle] + pub fn open() {} + //~^ ERROR invalid definition of the runtime `open` symbol + + extern "C" { + pub fn read(); + //~^ ERROR invalid definition of the runtime `read` symbol + + pub fn write(); + //~^ ERROR invalid definition of the runtime `write` symbol + } + + #[no_mangle] + pub static close: () = (); + //~^ ERROR invalid definition of the runtime `close` symbol + + extern "C" { + pub fn malloc(); + //~^ ERROR invalid definition of the runtime `malloc` symbol + + pub fn realloc(); + //~^ ERROR invalid definition of the runtime `realloc` symbol + + pub fn free(); + //~^ ERROR invalid definition of the runtime `free` symbol + + pub fn exit(); + //~^ ERROR invalid definition of the runtime `exit` symbol + } +} + +fn suspicious() { + extern "C" { + pub fn open(path: *const u8, oflag: usize, ...) -> c_int; + //~^ WARN suspicious definition of the runtime `open` symbol + + pub fn free(ptr: *const u8); + //~^ WARN suspicious definition of the runtime `free` symbol + + pub fn exit(code: f32) -> !; + //~^ WARN suspicious definition of the runtime `exit` symbol + } +} + +fn main() {} diff --git a/tests/ui/lint/runtime-symbols-unix.stderr b/tests/ui/lint/runtime-symbols-unix.stderr new file mode 100644 index 0000000000000..7ef765c29684d --- /dev/null +++ b/tests/ui/lint/runtime-symbols-unix.stderr @@ -0,0 +1,117 @@ +error: invalid definition of the runtime `open` symbol used by the standard library + --> $DIR/runtime-symbols-unix.rs:14:5 + | +LL | pub fn open() {} + | ^^^^^^^^^^^^^ + | + = note: expected `unsafe extern "C" fn(*const U8, i32, ...) -> i32` + found `fn()` + = help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "open")]`, or `#[link_name = "open"]` + = note: `#[deny(invalid_runtime_symbol_definitions)]` on by default + +error: invalid definition of the runtime `read` symbol used by the standard library + --> $DIR/runtime-symbols-unix.rs:18:9 + | +LL | pub fn read(); + | ^^^^^^^^^^^^^^ + | + = note: expected `unsafe extern "C" fn(i32, *mut c_void, usize) -> isize` + found `unsafe extern "C" fn()` + = help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "read")]`, or `#[link_name = "read"]` + +error: invalid definition of the runtime `write` symbol used by the standard library + --> $DIR/runtime-symbols-unix.rs:21:9 + | +LL | pub fn write(); + | ^^^^^^^^^^^^^^^ + | + = note: expected `unsafe extern "C" fn(i32, *const c_void, usize) -> isize` + found `unsafe extern "C" fn()` + = help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "write")]`, or `#[link_name = "write"]` + +error: invalid definition of the runtime `close` symbol used by the standard library + --> $DIR/runtime-symbols-unix.rs:26:5 + | +LL | pub static close: () = (); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: expected `unsafe extern "C" fn(i32) -> i32` + found `static close: ()` + = help: either fix the signature or remove any attributes `#[unsafe(no_mangle)]` or `#[unsafe(export_name = "close")]` + +error: invalid definition of the runtime `malloc` symbol used by the standard library + --> $DIR/runtime-symbols-unix.rs:30:9 + | +LL | pub fn malloc(); + | ^^^^^^^^^^^^^^^^ + | + = note: expected `unsafe extern "C" fn(usize) -> *mut c_void` + found `unsafe extern "C" fn()` + = help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "malloc")]`, or `#[link_name = "malloc"]` + +error: invalid definition of the runtime `realloc` symbol used by the standard library + --> $DIR/runtime-symbols-unix.rs:33:9 + | +LL | pub fn realloc(); + | ^^^^^^^^^^^^^^^^^ + | + = note: expected `unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void` + found `unsafe extern "C" fn()` + = help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "realloc")]`, or `#[link_name = "realloc"]` + +error: invalid definition of the runtime `free` symbol used by the standard library + --> $DIR/runtime-symbols-unix.rs:36:9 + | +LL | pub fn free(); + | ^^^^^^^^^^^^^^ + | + = note: expected `unsafe extern "C" fn(*mut c_void)` + found `unsafe extern "C" fn()` + = help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "free")]`, or `#[link_name = "free"]` + +error: invalid definition of the runtime `exit` symbol used by the standard library + --> $DIR/runtime-symbols-unix.rs:39:9 + | +LL | pub fn exit(); + | ^^^^^^^^^^^^^^ + | + = note: expected `unsafe extern "C" fn(i32) -> !` + found `unsafe extern "C" fn()` + = help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "exit")]`, or `#[link_name = "exit"]` + +warning: suspicious definition of the runtime `open` symbol used by the standard library + --> $DIR/runtime-symbols-unix.rs:46:9 + | +LL | pub fn open(path: *const U8, oflag: usize, ...) -> c_int; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: expected `unsafe extern "C" fn(*const U8, i32, ...) -> i32` + found `unsafe extern "C" fn(*const U8, usize, ...) -> i32` + = help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "open")]`, or `#[link_name = "open"]` + = help: allow this lint if the signature is compatible + = note: `#[warn(suspicious_runtime_symbol_definitions)]` on by default + +warning: suspicious definition of the runtime `free` symbol used by the standard library + --> $DIR/runtime-symbols-unix.rs:49:9 + | +LL | pub fn free(ptr: *const U8); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: expected `unsafe extern "C" fn(*mut c_void)` + found `unsafe extern "C" fn(*const U8)` + = help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "free")]`, or `#[link_name = "free"]` + = help: allow this lint if the signature is compatible + +warning: suspicious definition of the runtime `exit` symbol used by the standard library + --> $DIR/runtime-symbols-unix.rs:52:9 + | +LL | pub fn exit(code: f32) -> !; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: expected `unsafe extern "C" fn(i32) -> !` + found `unsafe extern "C" fn(f32) -> !` + = help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "exit")]`, or `#[link_name = "exit"]` + = help: allow this lint if the signature is compatible + +error: aborting due to 8 previous errors; 3 warnings emitted + From 070c3d97220c7be829d1007aec49ada203bc262d Mon Sep 17 00:00:00 2001 From: Urgau Date: Sat, 27 Jun 2026 12:43:27 +0200 Subject: [PATCH 2/3] Fix UI tests for invalid POSIX function definitions --- tests/ui/abi/stack-protector.rs | 11 ++++++----- tests/ui/cfg/conditional-compile.rs | 8 ++++---- tests/ui/extern/extern-pub.rs | 2 +- tests/ui/ffi/ffi-struct-size-alignment.rs | 3 +-- tests/ui/lint/dead-code/lint-dead-code-3.rs | 14 +++++++++----- tests/ui/lint/dead-code/lint-dead-code-3.stderr | 14 +++++++------- tests/ui/lint/warn-ctypes-inhibit.rs | 4 ++-- tests/ui/privacy/pub-extern-privacy.rs | 3 +-- 8 files changed, 31 insertions(+), 28 deletions(-) diff --git a/tests/ui/abi/stack-protector.rs b/tests/ui/abi/stack-protector.rs index dd0d0d43182ea..6a2b0d0854c4a 100644 --- a/tests/ui/abi/stack-protector.rs +++ b/tests/ui/abi/stack-protector.rs @@ -9,6 +9,7 @@ #![allow(function_casts_as_integer)] use std::env; +use std::ffi::c_void; use std::process::{Command, ExitStatus}; fn main() { @@ -42,7 +43,9 @@ fn vulnerable_function() { let bad_code_ptr = malicious_code as usize; // Overwrite the on-stack return address with the address of `malicious_code()`, // thereby jumping to that function when returning from `vulnerable_function()`. - unsafe { fill(stackaddr, bad_code_ptr, 20); } + unsafe { + fill(stackaddr, bad_code_ptr, 20); + } // Capture the address, so the write is not optimized away. std::hint::black_box(stackaddr); } @@ -68,16 +71,15 @@ unsafe fn fill(addr: *mut usize, val: usize, count: usize) { fn malicious_code() { let msg = [112u8, 119u8, 110u8, 101u8, 100u8, 33u8, 0u8]; // "pwned!\0" ascii unsafe { - write(1, &msg as *const u8, msg.len()); + write(1, &msg as *const u8 as *const c_void, msg.len()); _exit(0); } } extern "C" { - fn write(fd: i32, buf: *const u8, count: usize) -> isize; + fn write(fd: i32, buf: *const c_void, count: usize) -> isize; fn _exit(status: i32) -> !; } - fn assert_stack_smash_prevented(cmd: &mut Command) { let (status, stdout, stderr) = run(cmd); assert!(!status.success()); @@ -92,7 +94,6 @@ fn assert_stack_smashed(cmd: &mut Command) { assert!(stderr.is_empty()); } - fn run(cmd: &mut Command) -> (ExitStatus, String, String) { let output = cmd.output().unwrap(); let stdout = String::from_utf8_lossy(&output.stdout); diff --git a/tests/ui/cfg/conditional-compile.rs b/tests/ui/cfg/conditional-compile.rs index 637bd0e12e8be..3cdd1d52f8881 100644 --- a/tests/ui/cfg/conditional-compile.rs +++ b/tests/ui/cfg/conditional-compile.rs @@ -47,7 +47,7 @@ struct r { #[cfg(false)] fn r(i: isize) -> r { - r { i: i } + r { i } } struct r { @@ -55,7 +55,7 @@ struct r { } fn r(i: isize) -> r { - r { i: i } + r { i } } #[cfg(false)] @@ -111,8 +111,8 @@ mod test_foreign_items { pub mod rustrt { extern "C" { #[cfg(false)] - pub fn write() -> String; - pub fn write() -> String; + pub fn foo() -> String; + pub fn foo() -> String; } } } diff --git a/tests/ui/extern/extern-pub.rs b/tests/ui/extern/extern-pub.rs index b272bc5359fd0..9e91ca7f3324f 100644 --- a/tests/ui/extern/extern-pub.rs +++ b/tests/ui/extern/extern-pub.rs @@ -1,7 +1,7 @@ //@ run-pass extern "C" { - pub fn free(p: *const u8); + pub fn free(p: *mut std::ffi::c_void); } pub fn main() {} diff --git a/tests/ui/ffi/ffi-struct-size-alignment.rs b/tests/ui/ffi/ffi-struct-size-alignment.rs index 15ad3232555bb..287ae7cad2b6d 100644 --- a/tests/ui/ffi/ffi-struct-size-alignment.rs +++ b/tests/ui/ffi/ffi-struct-size-alignment.rs @@ -5,7 +5,6 @@ // Incorrect struct size computation in the FFI, because of not taking // the alignment of elements into account. - use std::ffi::{c_uint, c_void}; pub struct KEYGEN { @@ -17,7 +16,7 @@ pub struct KEYGEN { extern "C" { // Bogus signature, just need to test if it compiles. - pub fn malloc(data: KEYGEN); + pub fn foo(data: KEYGEN); } pub fn main() {} diff --git a/tests/ui/lint/dead-code/lint-dead-code-3.rs b/tests/ui/lint/dead-code/lint-dead-code-3.rs index 20b568054dfb7..7e733fc11c1db 100644 --- a/tests/ui/lint/dead-code/lint-dead-code-3.rs +++ b/tests/ui/lint/dead-code/lint-dead-code-3.rs @@ -48,24 +48,28 @@ mod blah { enum c_void {} extern "C" { - fn free(p: *const c_void); - fn malloc(size: usize) -> *const c_void; + fn my_free(p: *const c_void); + fn my_malloc(size: usize) -> *const c_void; } pub fn baz() { - unsafe { free(malloc(4)); } + unsafe { + my_free(my_malloc(4)); + } } } enum c_void {} //~ ERROR: enum `c_void` is never used extern "C" { - fn free(p: *const c_void); //~ ERROR: function `free` is never used + fn my_free(p: *const c_void); //~ ERROR: function `my_free` is never used } // Check provided method mod inner { pub trait Trait { - fn f(&self) { f(); } + fn f(&self) { + f(); + } } impl Trait for isize {} diff --git a/tests/ui/lint/dead-code/lint-dead-code-3.stderr b/tests/ui/lint/dead-code/lint-dead-code-3.stderr index 5c68cf0e18b67..acc5de4a93786 100644 --- a/tests/ui/lint/dead-code/lint-dead-code-3.stderr +++ b/tests/ui/lint/dead-code/lint-dead-code-3.stderr @@ -25,28 +25,28 @@ LL | fn bar() { | ^^^ error: enum `c_void` is never used - --> $DIR/lint-dead-code-3.rs:60:6 + --> $DIR/lint-dead-code-3.rs:62:6 | LL | enum c_void {} | ^^^^^^ error: function `blah` is never used - --> $DIR/lint-dead-code-3.rs:77:8 + --> $DIR/lint-dead-code-3.rs:81:8 | LL | fn blah() {} | ^^^^ error: function `blah` is never used - --> $DIR/lint-dead-code-3.rs:81:12 + --> $DIR/lint-dead-code-3.rs:85:12 | LL | fn blah() {} | ^^^^ -error: function `free` is never used - --> $DIR/lint-dead-code-3.rs:62:8 +error: function `my_free` is never used + --> $DIR/lint-dead-code-3.rs:64:8 | -LL | fn free(p: *const c_void); - | ^^^^ +LL | fn my_free(p: *const c_void); + | ^^^^^^^ error: aborting due to 7 previous errors diff --git a/tests/ui/lint/warn-ctypes-inhibit.rs b/tests/ui/lint/warn-ctypes-inhibit.rs index 0f401150adf64..1de661a6994e6 100644 --- a/tests/ui/lint/warn-ctypes-inhibit.rs +++ b/tests/ui/lint/warn-ctypes-inhibit.rs @@ -1,9 +1,9 @@ //@ run-pass - -#![allow(dead_code)] //@ compile-flags:-D improper-ctypes +#![allow(dead_code)] #![allow(improper_ctypes)] +#![allow(suspicious_runtime_symbol_definitions)] mod libc { extern "C" { diff --git a/tests/ui/privacy/pub-extern-privacy.rs b/tests/ui/privacy/pub-extern-privacy.rs index 0f9131685b0e2..e01d86acf6547 100644 --- a/tests/ui/privacy/pub-extern-privacy.rs +++ b/tests/ui/privacy/pub-extern-privacy.rs @@ -1,11 +1,10 @@ //@ run-pass - use std::mem::transmute; mod a { extern "C" { - pub fn free(x: *const u8); + pub fn free(x: *mut std::ffi::c_void); } } From 02eca56fc54b2de606769fba048bd81f789fa73b Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 28 Jun 2026 16:57:07 +0200 Subject: [PATCH 3/3] Fix Miri fail tests by allowing the lints --- src/tools/miri/tests/fail/function_calls/check_arg_abi.rs | 2 ++ .../tests/fail/function_calls/check_arg_count_too_few_args.rs | 2 ++ .../tests/fail/function_calls/check_arg_count_too_many_args.rs | 2 ++ .../tests/fail/function_calls/exported_symbol_shim_clashing.rs | 2 ++ src/tools/miri/tests/fail/shims/input_arg_mismatch.rs | 3 +++ .../miri/tests/fail/shims/non_vararg_signature_mismatch.rs | 3 +++ src/tools/miri/tests/fail/shims/return_type_mismatch.rs | 3 +++ src/tools/miri/tests/fail/shims/wrong_fixed_arg_count.rs | 3 +++ 8 files changed, 20 insertions(+) diff --git a/src/tools/miri/tests/fail/function_calls/check_arg_abi.rs b/src/tools/miri/tests/fail/function_calls/check_arg_abi.rs index 0e91636416903..e36b516887962 100644 --- a/src/tools/miri/tests/fail/function_calls/check_arg_abi.rs +++ b/src/tools/miri/tests/fail/function_calls/check_arg_abi.rs @@ -1,3 +1,5 @@ +#![allow(invalid_runtime_symbol_definitions)] + fn main() { extern "Rust" { fn malloc(size: usize) -> *mut std::ffi::c_void; diff --git a/src/tools/miri/tests/fail/function_calls/check_arg_count_too_few_args.rs b/src/tools/miri/tests/fail/function_calls/check_arg_count_too_few_args.rs index 41aebea2d8add..ecdda9e509d4e 100644 --- a/src/tools/miri/tests/fail/function_calls/check_arg_count_too_few_args.rs +++ b/src/tools/miri/tests/fail/function_calls/check_arg_count_too_few_args.rs @@ -1,3 +1,5 @@ +#![allow(invalid_runtime_symbol_definitions)] + fn main() { extern "C" { fn malloc() -> *mut std::ffi::c_void; diff --git a/src/tools/miri/tests/fail/function_calls/check_arg_count_too_many_args.rs b/src/tools/miri/tests/fail/function_calls/check_arg_count_too_many_args.rs index 1f5c509c6666f..1d3fec0fe32f8 100644 --- a/src/tools/miri/tests/fail/function_calls/check_arg_count_too_many_args.rs +++ b/src/tools/miri/tests/fail/function_calls/check_arg_count_too_many_args.rs @@ -1,3 +1,5 @@ +#![allow(invalid_runtime_symbol_definitions)] + fn main() { extern "C" { fn malloc(_: i32, _: i32) -> *mut std::ffi::c_void; diff --git a/src/tools/miri/tests/fail/function_calls/exported_symbol_shim_clashing.rs b/src/tools/miri/tests/fail/function_calls/exported_symbol_shim_clashing.rs index dffae7adbb972..6260d55b8f553 100644 --- a/src/tools/miri/tests/fail/function_calls/exported_symbol_shim_clashing.rs +++ b/src/tools/miri/tests/fail/function_calls/exported_symbol_shim_clashing.rs @@ -1,3 +1,5 @@ +#![allow(suspicious_runtime_symbol_definitions)] + #[no_mangle] extern "C" fn malloc(_: usize) -> *mut std::ffi::c_void { //~^ HELP: the `malloc` symbol is defined here diff --git a/src/tools/miri/tests/fail/shims/input_arg_mismatch.rs b/src/tools/miri/tests/fail/shims/input_arg_mismatch.rs index 77699776aea66..d355da73174e9 100644 --- a/src/tools/miri/tests/fail/shims/input_arg_mismatch.rs +++ b/src/tools/miri/tests/fail/shims/input_arg_mismatch.rs @@ -1,5 +1,8 @@ //@ignore-target: windows # File handling is not implemented yet //@compile-flags: -Zmiri-disable-isolation + +#![allow(suspicious_runtime_symbol_definitions)] + use std::ffi::{CString, OsStr, c_char, c_int}; use std::os::unix::ffi::OsStrExt; diff --git a/src/tools/miri/tests/fail/shims/non_vararg_signature_mismatch.rs b/src/tools/miri/tests/fail/shims/non_vararg_signature_mismatch.rs index b920e6795f90d..ebbf0cc2bf331 100644 --- a/src/tools/miri/tests/fail/shims/non_vararg_signature_mismatch.rs +++ b/src/tools/miri/tests/fail/shims/non_vararg_signature_mismatch.rs @@ -1,5 +1,8 @@ //@ignore-target: windows # File handling is not implemented yet //@compile-flags: -Zmiri-disable-isolation + +#![allow(invalid_runtime_symbol_definitions)] + use std::ffi::{CString, OsStr, c_char, c_int}; use std::os::unix::ffi::OsStrExt; diff --git a/src/tools/miri/tests/fail/shims/return_type_mismatch.rs b/src/tools/miri/tests/fail/shims/return_type_mismatch.rs index 6dbdd3f539b38..bcde5d8040f59 100644 --- a/src/tools/miri/tests/fail/shims/return_type_mismatch.rs +++ b/src/tools/miri/tests/fail/shims/return_type_mismatch.rs @@ -1,5 +1,8 @@ //@ignore-target: windows # File handling is not implemented yet //@compile-flags: -Zmiri-disable-isolation + +#![allow(suspicious_runtime_symbol_definitions)] + use std::ffi::{CString, OsStr, c_char, c_int, c_short}; use std::os::unix::ffi::OsStrExt; diff --git a/src/tools/miri/tests/fail/shims/wrong_fixed_arg_count.rs b/src/tools/miri/tests/fail/shims/wrong_fixed_arg_count.rs index e9cb69418d22f..5453db57bbd8f 100644 --- a/src/tools/miri/tests/fail/shims/wrong_fixed_arg_count.rs +++ b/src/tools/miri/tests/fail/shims/wrong_fixed_arg_count.rs @@ -1,5 +1,8 @@ //@ignore-target: windows # File handling is not implemented yet //@compile-flags: -Zmiri-disable-isolation + +#![allow(invalid_runtime_symbol_definitions)] + use std::ffi::{CString, OsStr, c_char, c_int}; use std::os::unix::ffi::OsStrExt;