Skip to content

Commit 67e8f79

Browse files
authored
Add safety comments to unsafe attributes (#592)
Part of #288. This is not yet enforced by clippy (rust-lang/rust-clippy#13316), but we should be doing it anyways.
1 parent 0d222a9 commit 67e8f79

File tree

7 files changed

+34
-1
lines changed

7 files changed

+34
-1
lines changed

openhcl/build_info/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ impl BuildInfo {
6060
// a debugger. With a debugger, the non-mangled name is easier
6161
// to use.
6262

63-
// UNSAFETY: link_section and export_name are considered unsafe.
63+
// UNSAFETY: link_section and export_name are unsafe.
6464
#[expect(unsafe_code)]
65+
// SAFETY: The build_info section is custom and carries no safety requirements.
6566
#[unsafe(link_section = ".build_info")]
67+
// SAFETY: The name "BUILD_INFO" is only declared here in OpenHCL and shouldn't
68+
// collide with any other symbols. It is a special symbol intended for
69+
// post-mortem debugging, and no runtime functionality should depend on it.
6670
#[unsafe(export_name = "BUILD_INFO")]
6771
static BUILD_INFO: BuildInfo = BuildInfo::new();
6872

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
66
/// Hand rolled implementation of memcpy.
77
#[cfg(minimal_rt)]
8+
// SAFETY: The minimal_rt_build crate ensures that when this code is compiled
9+
// there is no libc for this to conflict with.
810
#[unsafe(no_mangle)]
911
unsafe extern "C" fn memcpy(mut dest: *mut u8, src: *const u8, len: usize) -> *mut u8 {
1012
// SAFETY: the caller guarantees the pointers and length are correct.
@@ -31,6 +33,8 @@ unsafe extern "C" fn memcpy(mut dest: *mut u8, src: *const u8, len: usize) -> *m
3133

3234
/// Hand rolled implementation of memset.
3335
#[cfg(minimal_rt)]
36+
// SAFETY: The minimal_rt_build crate ensures that when this code is compiled
37+
// there is no libc for this to conflict with.
3438
#[unsafe(no_mangle)]
3539
unsafe extern "C" fn memset(mut ptr: *mut u8, val: i32, len: usize) -> *mut u8 {
3640
// SAFETY: the caller guarantees the pointer and length are correct.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
66
/// Hand rolled implementation of memset.
77
#[cfg(minimal_rt)]
8+
// SAFETY: The minimal_rt_build crate ensures that when this code is compiled
9+
// there is no libc for this to conflict with.
810
#[unsafe(no_mangle)]
911
unsafe extern "C" fn memset(mut ptr: *mut u8, val: i32, len: usize) -> *mut u8 {
1012
// SAFETY: The caller guarantees that the pointer and length are correct.
@@ -22,6 +24,8 @@ unsafe extern "C" fn memset(mut ptr: *mut u8, val: i32, len: usize) -> *mut u8 {
2224

2325
/// Hand rolled implementation of memcpy.
2426
#[cfg(minimal_rt)]
27+
// SAFETY: The minimal_rt_build crate ensures that when this code is compiled
28+
// there is no libc for this to conflict with.
2529
#[unsafe(no_mangle)]
2630
unsafe extern "C" fn memcpy(mut dest: *mut u8, src: *const u8, len: usize) -> *mut u8 {
2731
// SAFETY: The caller guarantees that the pointers and length are correct.

openhcl/minimal_rt/src/rt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ mod instead_of_builtins {
2424
}
2525

2626
/// Implementation cribbed from compiler_builtins.
27+
// SAFETY: The minimal_rt_build crate ensures that when this code is compiled
28+
// there is no libc for this to conflict with.
2729
#[unsafe(no_mangle)]
2830
unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
2931
let delta = (dest as usize).wrapping_sub(src as usize);
@@ -47,6 +49,8 @@ mod instead_of_builtins {
4749
/// This implementation is cribbed from compiler_builtins. It would be nice to
4850
/// use those implementation for all the above functions, but those require
4951
/// nightly as these are not yet stabilized.
52+
// SAFETY: The minimal_rt_build crate ensures that when this code is compiled
53+
// there is no libc for this to conflict with.
5054
#[unsafe(no_mangle)]
5155
unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
5256
// SAFETY: The caller guarantees that the pointers and length are correct.

support/openssl_crypto_only/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ macro_rules! openssl_crypto_only {
3333
/// # Safety
3434
///
3535
/// The caller must call as documented for `OPENSSL_init_ssl`.
36+
// SAFETY: We are purposefully overriding this symbol and we have made
37+
// sure the definition is compatible with the original.
3638
#[unsafe(no_mangle)]
3739
unsafe extern "C" fn OPENSSL_init_ssl(
3840
opts: u64,

support/win_prng_support/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Licensed under the MIT License.
33

44
//! Support for running in smaller Windows editions such as Win1.
5+
//!
6+
//! This crate purposefully overrides symbols normally provided by system DLLs.
7+
//! In order for this to be safe the definitions are compatible with the originals.
58
69
#![cfg(windows)]
710
// UNSAFETY: needed to call internal Windows functions and to export unmangled
@@ -25,6 +28,7 @@ macro_rules! use_win10_prng_apis {
2528
$($crate::use_win10_prng_apis!(@x $lib);)*
2629
};
2730
(@x advapi32) => {
31+
// SAFETY: see module level safety justification
2832
#[unsafe(no_mangle)]
2933
pub unsafe extern "system" fn SystemFunction036(data: *mut u8, len: u32) -> u8 {
3034
// SAFETY: passing through guarantees.
@@ -33,11 +37,13 @@ macro_rules! use_win10_prng_apis {
3337

3438
/// If a call to SystemFunction036 is marked as a dllimport, then it may be an indirect call
3539
/// through __imp_SystemFunction036 instead.
40+
// SAFETY: see module level safety justification
3641
#[unsafe(no_mangle)]
3742
pub static __imp_SystemFunction036: unsafe extern "system" fn(*mut u8, u32) -> u8 =
3843
SystemFunction036;
3944
};
4045
(@x bcrypt) => {
46+
// SAFETY: see module level safety justification
4147
#[unsafe(no_mangle)]
4248
pub unsafe extern "system" fn BCryptOpenAlgorithmProvider(
4349
handle: *mut ::core::ffi::c_void,
@@ -56,6 +62,7 @@ macro_rules! use_win10_prng_apis {
5662
}
5763
}
5864

65+
// SAFETY: see module level safety justification
5966
#[unsafe(no_mangle)]
6067
pub unsafe extern "system" fn BCryptCloseAlgorithmProvider(
6168
handle: *mut ::core::ffi::c_void,
@@ -65,6 +72,7 @@ macro_rules! use_win10_prng_apis {
6572
unsafe { $crate::private::BCryptCloseAlgorithmProvider(handle, flags) }
6673
}
6774

75+
// SAFETY: see module level safety justification
6876
#[unsafe(no_mangle)]
6977
pub unsafe extern "system" fn BCryptGenRandom(
7078
algorithm: usize,
@@ -78,6 +86,7 @@ macro_rules! use_win10_prng_apis {
7886

7987
/// If a call to BCryptGenRandom is marked as a dllimport, then it may be an indirect call
8088
/// through __imp_BCryptGenRandom instead.
89+
// SAFETY: see module level safety justification
8190
#[unsafe(no_mangle)]
8291
pub static __imp_BCryptGenRandom: unsafe extern "system" fn(
8392
usize,
@@ -86,6 +95,7 @@ macro_rules! use_win10_prng_apis {
8695
u32,
8796
) -> u32 = BCryptGenRandom;
8897

98+
// SAFETY: see module level safety justification
8999
#[unsafe(no_mangle)]
90100
pub static __imp_BCryptOpenAlgorithmProvider: unsafe extern "system" fn(
91101
*mut ::core::ffi::c_void,
@@ -94,6 +104,7 @@ macro_rules! use_win10_prng_apis {
94104
u32,
95105
) -> u32 = BCryptOpenAlgorithmProvider;
96106

107+
// SAFETY: see module level safety justification
97108
#[unsafe(no_mangle)]
98109
pub static __imp_BCryptCloseAlgorithmProvider: unsafe extern "system" fn(
99110
*mut ::core::ffi::c_void,

vm/vmgs/vmgs_lib/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub enum VmgsError {
4848
///
4949
/// `file_path` must point to a valid null-terminated utf-8 string.
5050
/// `in_len` must be the size of `in_buf` in bytes and match the value returned from query_size_vmgs
51+
// SAFETY: In this library this function name is unique.
5152
#[unsafe(no_mangle)]
5253
pub unsafe extern "C" fn read_vmgs(
5354
file_path: *const c_char,
@@ -152,6 +153,7 @@ async fn do_read(
152153
///
153154
/// `file_path` and `data_path` must point to valid null-terminated utf-8 strings.
154155
/// `encryption_key` must be null-terminated and nonnull if using encryption
156+
// SAFETY: In this library this function name is unique.
155157
#[unsafe(no_mangle)]
156158
pub unsafe extern "C" fn write_vmgs(
157159
file_path: *const c_char,
@@ -239,6 +241,7 @@ async fn do_write(
239241
/// # Safety
240242
///
241243
/// `path` must point to a valid null-terminated utf-8 string.
244+
// SAFETY: In this library this function name is unique.
242245
#[unsafe(no_mangle)]
243246
pub unsafe extern "C" fn create_vmgs(
244247
path: *const c_char,
@@ -335,6 +338,7 @@ async fn do_create(
335338
///
336339
/// `path` pointer must point to a valid, null-terminated utf-8 string.
337340
/// `out_size` pointer must be nonnull
341+
// SAFETY: In this library this function name is unique.
338342
#[unsafe(no_mangle)]
339343
pub unsafe extern "C" fn query_size_vmgs(
340344
path: *const c_char,

0 commit comments

Comments
 (0)