Skip to content

Commit c5a665c

Browse files
authored
Begin working towards Rust 2024 compatibility (#289)
This PR fixes up the easy issues with Rust 2024 compatibility and enables the lint for it. However it also scopes out some allows for the things that need some more work and focus. #288 is tracking them. Changes in this PR: - extern blocks are now unsafe - `gen` is becoming a reserved keyword, rename variables - mark unsafe attributes other than linkme's - resolve the ambiguity of `fn poll` in VmbusSerialDriver
1 parent c113573 commit c5a665c

File tree

28 files changed

+87
-78
lines changed

28 files changed

+87
-78
lines changed

.github/scripts/add_unsafe_reviewers/add-unsafe-reviewers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def contains_unsafe(change) -> bool:
1515
return False
1616

1717
with open(f'{repo_path}/{change.a_path}') as fd:
18-
return 'unsafe ' in fd.read()
18+
content = fd.read()
19+
return 'unsafe ' in content or 'unsafe(' in content
1920

2021
# Look for modified / added files
2122
repo = Repo(repo_path)

Cargo.toml

+11-3
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,17 @@ pbjson-build = { git = "https://github.com/jstarks/pbjson", branch = "aliases" }
562562

563563
[workspace.lints.rust]
564564
# Lint groups need a priority lower than individual lints so they get applied first.
565-
future_incompatible = { level = "deny", priority = -1 }
566-
nonstandard_style = { level = "deny", priority = -1 }
567-
rust_2018_idioms = { level = "deny", priority = -1 }
565+
future_incompatible = { level = "deny", priority = -2 }
566+
nonstandard_style = { level = "deny", priority = -2 }
567+
rust_2018_idioms = { level = "deny", priority = -2 }
568+
569+
rust-2024-compatibility = { level = "warn", priority = -1 }
570+
# TODO: Fix all of the below, https://github.com/microsoft/openvmm/issues/288
571+
edition_2024_expr_fragment_specifier = "allow"
572+
impl_trait_overcaptures = "allow"
573+
deprecated-safe-2024 = "allow"
574+
# Needed for linkme compatibility for now, https://github.com/dtolnay/linkme/issues/101
575+
unsafe-attr-outside-unsafe = "allow"
568576

569577
unused_qualifications = "warn"
570578

openhcl/build_info/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ impl BuildInfo {
6262

6363
// UNSAFETY: link_section and export_name are considered unsafe.
6464
#[allow(unsafe_code)]
65-
#[link_section = ".build_info"]
66-
#[export_name = "BUILD_INFO"]
65+
#[unsafe(link_section = ".build_info")]
66+
#[unsafe(export_name = "BUILD_INFO")]
6767
static BUILD_INFO: BuildInfo = BuildInfo::new();
6868

6969
pub fn get() -> &'static BuildInfo {

openhcl/minimal_rt/src/arch/aarch64/intrinsics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
/// Hand rolled implementation of memcpy.
77
#[cfg(minimal_rt)]
8-
#[no_mangle]
8+
#[unsafe(no_mangle)]
99
unsafe extern "C" fn memcpy(mut dest: *mut u8, src: *const u8, len: usize) -> *mut u8 {
1010
// SAFETY: the caller guarantees the pointers and length are correct.
1111
unsafe {
@@ -31,7 +31,7 @@ unsafe extern "C" fn memcpy(mut dest: *mut u8, src: *const u8, len: usize) -> *m
3131

3232
/// Hand rolled implementation of memset.
3333
#[cfg(minimal_rt)]
34-
#[no_mangle]
34+
#[unsafe(no_mangle)]
3535
unsafe extern "C" fn memset(mut ptr: *mut u8, val: i32, len: usize) -> *mut u8 {
3636
// SAFETY: the caller guarantees the pointer and length are correct.
3737
unsafe {

openhcl/minimal_rt/src/arch/x86_64/hypercall.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//!
66
//! The hypercall ABI for x64 is well documented in the TLFS.
77
8-
extern "C" {
8+
unsafe extern "C" {
99
/// The hypercall page. The actual hypercall page must be mapped on top of
1010
/// this page before it is used.
1111
pub static mut HYPERCALL_PAGE: [u8; 4096];

openhcl/minimal_rt/src/arch/x86_64/intrinsics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
/// Hand rolled implementation of memset.
77
#[cfg(minimal_rt)]
8-
#[no_mangle]
8+
#[unsafe(no_mangle)]
99
unsafe extern "C" fn memset(mut ptr: *mut u8, val: i32, len: usize) -> *mut u8 {
1010
// SAFETY: The caller guarantees that the pointer and length are correct.
1111
unsafe {
@@ -22,7 +22,7 @@ unsafe extern "C" fn memset(mut ptr: *mut u8, val: i32, len: usize) -> *mut u8 {
2222

2323
/// Hand rolled implementation of memcpy.
2424
#[cfg(minimal_rt)]
25-
#[no_mangle]
25+
#[unsafe(no_mangle)]
2626
unsafe extern "C" fn memcpy(mut dest: *mut u8, src: *const u8, len: usize) -> *mut u8 {
2727
// SAFETY: The caller guarantees that the pointers and length are correct.
2828
unsafe {

openhcl/minimal_rt/src/rt.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ mod instead_of_builtins {
1919
}
2020
}
2121

22-
extern "C" {
22+
unsafe extern "C" {
2323
fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
2424
}
2525

2626
/// Implementation cribbed from compiler_builtins.
27-
#[no_mangle]
27+
#[unsafe(no_mangle)]
2828
unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
2929
let delta = (dest as usize).wrapping_sub(src as usize);
3030
if delta >= n {
@@ -47,7 +47,7 @@ mod instead_of_builtins {
4747
/// This implementation is cribbed from compiler_builtins. It would be nice to
4848
/// use those implementation for all the above functions, but those require
4949
/// nightly as these are not yet stabilized.
50-
#[no_mangle]
50+
#[unsafe(no_mangle)]
5151
unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
5252
// SAFETY: The caller guarantees that the pointers and length are correct.
5353
unsafe {

openhcl/openhcl_boot/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ struct Fdt {
270270
/// where the shim is loaded. Return a ShimParams structure based on the raw
271271
/// offset based RawShimParams.
272272
fn shim_parameters(shim_params_raw_offset: isize) -> ShimParams {
273-
extern "C" {
273+
unsafe extern "C" {
274274
static __ehdr_start: u8;
275275
}
276276

openhcl/sidecar/src/arch/x86_64/init.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use x86defs::IdtEntry64;
5555
use x86defs::Pte;
5656
use zerocopy::FromZeroes;
5757

58-
extern "C" {
58+
unsafe extern "C" {
5959
static IMAGE_PDE: Pte;
6060
fn irq_entry();
6161
fn exc_gpf();

openhcl/sidecar/src/arch/x86_64/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mod addr_space {
3838

3939
const PAGE_SIZE: u64 = 0x1000;
4040

41-
extern "C" {
41+
unsafe extern "C" {
4242
static __ehdr_start: u8;
4343
}
4444

support/openssl_crypto_only/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use core::ffi::c_int;
1414
use core::ffi::c_void;
1515

16-
extern "C" {
16+
unsafe extern "C" {
1717
#[doc(hidden)]
1818
pub fn OPENSSL_init_crypto(opts: u64, settings: *const c_void) -> c_int;
1919
}
@@ -33,7 +33,7 @@ macro_rules! openssl_crypto_only {
3333
/// # Safety
3434
///
3535
/// The caller must call as documented for `OPENSSL_init_ssl`.
36-
#[no_mangle]
36+
#[unsafe(no_mangle)]
3737
unsafe extern "C" fn OPENSSL_init_ssl(
3838
opts: u64,
3939
settings: *const ::core::ffi::c_void,

support/openssl_kdf/src/sys/evp.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum KDF {}
1919

2020
pub enum KDF_CTX {}
2121

22-
extern "C" {
22+
unsafe extern "C" {
2323
pub fn EVP_MD_get0_name(md: *const EVP_MD) -> *const c_char;
2424
}
2525

@@ -31,7 +31,7 @@ pub const OSSL_PARAM_OCTET_STRING: c_uchar = 5;
3131
pub const OSSL_PARAM_UTF8_PTR: c_uchar = 6;
3232
pub const OSSL_PARAM_OCTET_PTR: c_uchar = 7;
3333

34-
extern "C" {
34+
unsafe extern "C" {
3535
pub fn EVP_KDF_fetch(
3636
libctx: *mut OSSL_LIB_CTX,
3737
algorithm: *const c_char,

support/openssl_kdf/src/sys/params.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use libc::time_t;
1818
use openssl_sys::BIGNUM;
1919
use std::ffi::CStr;
2020

21-
extern "C" {
21+
unsafe extern "C" {
2222
pub fn OSSL_PARAM_get_int(p: *const OSSL_PARAM, val: *mut c_int) -> c_int;
2323
pub fn OSSL_PARAM_get_uint(p: *const OSSL_PARAM, val: *mut c_uint) -> c_int;
2424
pub fn OSSL_PARAM_get_long(p: *const OSSL_PARAM, val: *mut c_long) -> c_int;

support/pal/src/windows/alpc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ mod ntlpcapi {
5757
pub const OB_ALL_OBJECT_TYPE_CODES: u32 = 0x00000ffd;
5858

5959
// This is defined incorrectly in ntapi 0.3.6.
60-
extern "C" {
60+
unsafe extern "C" {
6161
pub fn AlpcInitializeMessageAttribute(
6262
AttributeFlags: u32,
6363
Buffer: PALPC_MESSAGE_ATTRIBUTES,

support/pal/src/windows/security.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl SecurityDescriptor {
318318
}
319319

320320
#[link(name = "api-ms-win-security-base-private-l1-1-1")]
321-
extern "C" {
321+
unsafe extern "C" {
322322
fn CreateAppContainerToken(
323323
token: HANDLE,
324324
caps: LPSECURITY_CAPABILITIES,

support/sparse_mmap/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn initialize_try_copy() {
4747
}
4848
}
4949

50-
extern "C" {
50+
unsafe extern "C" {
5151
#[cfg(unix)]
5252
fn install_signal_handlers() -> i32;
5353

support/win_prng_support/src/lib.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ macro_rules! use_win10_prng_apis {
2525
$($crate::use_win10_prng_apis!(@x $lib);)*
2626
};
2727
(@x advapi32) => {
28-
#[no_mangle]
28+
#[unsafe(no_mangle)]
2929
pub unsafe extern "system" fn SystemFunction036(data: *mut u8, len: u32) -> u8 {
3030
// SAFETY: passing through guarantees.
3131
unsafe { $crate::private::SystemFunction036(data, len) }
3232
}
3333

3434
/// If a call to SystemFunction036 is marked as a dllimport, then it may be an indirect call
3535
/// through __imp_SystemFunction036 instead.
36-
#[no_mangle]
36+
#[unsafe(no_mangle)]
3737
pub static __imp_SystemFunction036: unsafe extern "system" fn(*mut u8, u32) -> u8 =
3838
SystemFunction036;
3939
};
4040
(@x bcrypt) => {
41-
#[no_mangle]
41+
#[unsafe(no_mangle)]
4242
pub unsafe extern "system" fn BCryptOpenAlgorithmProvider(
4343
handle: *mut ::core::ffi::c_void,
4444
psz_alg_id: *mut u16,
@@ -56,7 +56,7 @@ macro_rules! use_win10_prng_apis {
5656
}
5757
}
5858

59-
#[no_mangle]
59+
#[unsafe(no_mangle)]
6060
pub unsafe extern "system" fn BCryptCloseAlgorithmProvider(
6161
handle: *mut ::core::ffi::c_void,
6262
flags: u32,
@@ -65,7 +65,7 @@ macro_rules! use_win10_prng_apis {
6565
unsafe { $crate::private::BCryptCloseAlgorithmProvider(handle, flags) }
6666
}
6767

68-
#[no_mangle]
68+
#[unsafe(no_mangle)]
6969
pub unsafe extern "system" fn BCryptGenRandom(
7070
algorithm: usize,
7171
data: *mut u8,
@@ -78,23 +78,23 @@ macro_rules! use_win10_prng_apis {
7878

7979
/// If a call to BCryptGenRandom is marked as a dllimport, then it may be an indirect call
8080
/// through __imp_BCryptGenRandom instead.
81-
#[no_mangle]
81+
#[unsafe(no_mangle)]
8282
pub static __imp_BCryptGenRandom: unsafe extern "system" fn(
8383
usize,
8484
*mut u8,
8585
u32,
8686
u32,
8787
) -> u32 = BCryptGenRandom;
8888

89-
#[no_mangle]
89+
#[unsafe(no_mangle)]
9090
pub static __imp_BCryptOpenAlgorithmProvider: unsafe extern "system" fn(
9191
*mut ::core::ffi::c_void,
9292
*mut u16,
9393
*mut u16,
9494
u32,
9595
) -> u32 = BCryptOpenAlgorithmProvider;
9696

97-
#[no_mangle]
97+
#[unsafe(no_mangle)]
9898
pub static __imp_BCryptCloseAlgorithmProvider: unsafe extern "system" fn(
9999
*mut ::core::ffi::c_void,
100100
u32,
@@ -115,7 +115,7 @@ pub mod private {
115115
const BCRYPT_MAGIC_ALGORITHM_HANDLE: usize = 0x1234abcd;
116116

117117
#[link(name = "ext-ms-win-cng-rng-l1-1-0")]
118-
extern "C" {
118+
unsafe extern "C" {
119119
/// The lowest-level PRNG API in Windows.
120120
fn ProcessPrng(data: *mut u8, len: usize) -> u32;
121121
}

0 commit comments

Comments
 (0)