Skip to content

Commit 559679b

Browse files
committed
Applied #![deny(unsafe_op_in_unsafe_fn)] in library/std/src/wasi
All refactoring needed was only in `alloc.rs`, changed part of the code in `alloc` method to satisfy the SAFETY statement
1 parent 45a83e9 commit 559679b

File tree

15 files changed

+60
-8
lines changed

15 files changed

+60
-8
lines changed

library/std/src/sys/wasi/alloc.rs

+35-8
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,69 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::alloc::{GlobalAlloc, Layout, System};
24
use crate::ptr;
35
use crate::sys_common::alloc::{realloc_fallback, MIN_ALIGN};
46

7+
// SAFETY: All methods implemented follow the contract rules defined
8+
// in `GlobalAlloc`.
59
#[stable(feature = "alloc_system_type", since = "1.28.0")]
610
unsafe impl GlobalAlloc for System {
711
#[inline]
812
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
913
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
10-
libc::malloc(layout.size()) as *mut u8
14+
// SAFETY: `libc::malloc` is guaranteed to be safe, it will allocate
15+
// `layout.size()` bytes of memory and return a pointer to it
16+
unsafe { libc::malloc(layout.size()) as *mut u8 }
1117
} else {
12-
libc::aligned_alloc(layout.align(), layout.size()) as *mut u8
18+
// SAFETY: `libc::aligned_alloc` is guaranteed to be safe if
19+
// `layout.size()` is a multiple of `layout.align()`. This
20+
// constraint can be satisfied if `pad_to_align` is called,
21+
// which creates a layout by rounding the size of this layout up
22+
// to a multiple of the layout's alignment
23+
let aligned_layout = layout.pad_to_align();
24+
unsafe { libc::aligned_alloc(aligned_layout.align(), aligned_layout.size()) as *mut u8 }
1325
}
1426
}
1527

1628
#[inline]
1729
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
1830
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
19-
libc::calloc(layout.size(), 1) as *mut u8
31+
// SAFETY: `libc::calloc` is safe as long that `layout.size() * 1`
32+
// would not result in integer overflow which cannot happen,
33+
// multiplying by one never overflows
34+
unsafe { libc::calloc(layout.size(), 1) as *mut u8 }
2035
} else {
21-
let ptr = self.alloc(layout.clone());
36+
// SAFETY: The safety contract for `alloc` must be upheld by the caller
37+
let ptr = unsafe { self.alloc(layout.clone()) };
2238
if !ptr.is_null() {
23-
ptr::write_bytes(ptr, 0, layout.size());
39+
// SAFETY: in the case of the `ptr` being not null
40+
// it will be properly aligned and a valid ptr
41+
// which satisfies `ptr::write_bytes` safety constrains
42+
unsafe { ptr::write_bytes(ptr, 0, layout.size()) };
2443
}
2544
ptr
2645
}
2746
}
2847

2948
#[inline]
3049
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
31-
libc::free(ptr as *mut libc::c_void)
50+
// SAFETY: `libc::free` is guaranteed to be safe if `ptr` is allocated
51+
// by this allocator or if `ptr` is NULL
52+
unsafe { libc::free(ptr as *mut libc::c_void) }
3253
}
3354

3455
#[inline]
3556
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
3657
if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
37-
libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
58+
// SAFETY: `libc::realloc` is safe if `ptr` is allocated by this
59+
// allocator or NULL
60+
// - If `new_size` is 0 and `ptr` is not NULL, it will act as `libc::free`
61+
// - If `new_size` is not 0 and `ptr` is NULL, it will act as `libc::malloc`
62+
// - Else, it will resize the block accordingly
63+
unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 }
3864
} else {
39-
realloc_fallback(self, ptr, layout, new_size)
65+
// SAFETY: The safety contract for `realloc_fallback` must be upheld by the caller
66+
unsafe { realloc_fallback(self, ptr, layout, new_size) }
4067
}
4168
}
4269
}

library/std/src/sys/wasi/args.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::ffi::{CStr, OsStr, OsString};
24
use crate::marker::PhantomData;
35
use crate::os::wasi::ffi::OsStrExt;

library/std/src/sys/wasi/ext/fs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! WASI-specific extensions to primitives in the `std::fs` module.
22
3+
#![deny(unsafe_op_in_unsafe_fn)]
34
#![unstable(feature = "wasi_ext", issue = "none")]
45

56
use crate::fs::{self, File, Metadata, OpenOptions};

library/std/src/sys/wasi/ext/io.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! WASI-specific extensions to general I/O primitives
22
3+
#![deny(unsafe_op_in_unsafe_fn)]
34
#![unstable(feature = "wasi_ext", issue = "none")]
45

56
use crate::fs;

library/std/src/sys/wasi/ext/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
pub mod ffi;
24
pub mod fs;
35
pub mod io;

library/std/src/sys/wasi/fd.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
12
#![allow(dead_code)]
23

34
use super::err2io;

library/std/src/sys/wasi/fs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::ffi::{CStr, CString, OsStr, OsString};
24
use crate::fmt;
35
use crate::io::{self, IoSlice, IoSliceMut, SeekFrom};

library/std/src/sys/wasi/io.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::marker::PhantomData;
24
use crate::slice;
35

library/std/src/sys/wasi/net.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::convert::TryFrom;
24
use crate::fmt;
35
use crate::io::{self, IoSlice, IoSliceMut};

library/std/src/sys/wasi/os.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::any::Any;
24
use crate::error::Error as StdError;
35
use crate::ffi::{CStr, CString, OsStr, OsString};

library/std/src/sys/wasi/pipe.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::io::{self, IoSlice, IoSliceMut};
24
use crate::sys::Void;
35

library/std/src/sys/wasi/process.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::ffi::OsStr;
24
use crate::fmt;
35
use crate::io;

library/std/src/sys/wasi/stdio.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::io::{self, IoSlice, IoSliceMut};
24
use crate::mem::ManuallyDrop;
35
use crate::sys::fd::WasiFd;

library/std/src/sys/wasi/thread.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::ffi::CStr;
24
use crate::io;
35
use crate::mem;

library/std/src/sys/wasi/time.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::time::Duration;
24

35
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]

0 commit comments

Comments
 (0)