Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_hir/src/weak_lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ weak_only_lang_items! {
MemCmp,
Bcmp,
StrLen,
Open,
Read,
Write,
Close,
Malloc,
Realloc,
Free,
Exit,
}
20 changes: 16 additions & 4 deletions compiler/rustc_lint/src/runtime_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...).
///
Expand All @@ -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,
Expand All @@ -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
///
Expand All @@ -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,
Expand All @@ -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)]
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ symbols! {
clone_closures,
clone_fn,
clone_from,
close_fn,
closure,
closure_lifetime_binder,
closure_to_fn_coercion,
Expand Down Expand Up @@ -898,6 +899,7 @@ symbols! {
exhaustive_integer_patterns,
exhaustive_patterns,
existential_type,
exit_fn,
exp2f16,
exp2f32,
exp2f64,
Expand Down Expand Up @@ -1009,6 +1011,7 @@ symbols! {
format_unsafe_arg,
fp,
framework,
free_fn,
freeze,
freeze_impls,
freg,
Expand Down Expand Up @@ -1248,6 +1251,7 @@ symbols! {
macro_vis_matcher,
macros_in_extern,
main,
malloc_fn,
managed_boxes,
manually_drop,
map,
Expand Down Expand Up @@ -1473,6 +1477,7 @@ symbols! {
on_unmatched_args,
opaque,
opaque_module_name_placeholder: "<opaque>",
open_fn,
ops,
opt_out_copy,
optimize,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2359,6 +2366,7 @@ symbols! {
write_box_via_move,
write_bytes,
write_fmt,
write_fn,
write_macro,
write_str,
write_via_move,
Expand Down
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
19 changes: 19 additions & 0 deletions library/std/src/sys/alloc/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
19 changes: 19 additions & 0 deletions library/std/src/sys/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" => {
Expand Down
22 changes: 22 additions & 0 deletions library/std/src/sys/fs/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 2 additions & 0 deletions src/tools/miri/tests/fail/function_calls/check_arg_abi.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(invalid_runtime_symbol_definitions)]

fn main() {
extern "Rust" {
fn malloc(size: usize) -> *mut std::ffi::c_void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(invalid_runtime_symbol_definitions)]

fn main() {
extern "C" {
fn malloc() -> *mut std::ffi::c_void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(invalid_runtime_symbol_definitions)]

fn main() {
extern "C" {
fn malloc(_: i32, _: i32) -> *mut std::ffi::c_void;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/tools/miri/tests/fail/shims/input_arg_mismatch.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
3 changes: 3 additions & 0 deletions src/tools/miri/tests/fail/shims/return_type_mismatch.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
3 changes: 3 additions & 0 deletions src/tools/miri/tests/fail/shims/wrong_fixed_arg_count.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
11 changes: 6 additions & 5 deletions tests/ui/abi/stack-protector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#![allow(function_casts_as_integer)]

use std::env;
use std::ffi::c_void;
use std::process::{Command, ExitStatus};

fn main() {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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());
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/cfg/conditional-compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ struct r {

#[cfg(false)]
fn r(i: isize) -> r {
r { i: i }
r { i }
}

struct r {
i: isize,
}

fn r(i: isize) -> r {
r { i: i }
r { i }
}

#[cfg(false)]
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/extern/extern-pub.rs
Original file line number Diff line number Diff line change
@@ -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() {}
3 changes: 1 addition & 2 deletions tests/ui/ffi/ffi-struct-size-alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {}
Loading
Loading