]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #75971 - Amjad50:libstd-deny-unsafe_op_in_unsafe_fn, r=Mark-Simulacrum
authorbors <bors@rust-lang.org>
Thu, 3 Sep 2020 02:15:16 +0000 (02:15 +0000)
committerbors <bors@rust-lang.org>
Thu, 3 Sep 2020 02:15:16 +0000 (02:15 +0000)
Applied `#![deny(unsafe_op_in_unsafe_fn)]` in library/std/src/wasi

partial fix for #73904

There are still more that was not applied in [mod.rs]( https://github.com/rust-lang/rust/blob/38fab2ea92a48980219989817201bf2094ae826a/library/std/src/sys/wasi/mod.rs) and that is due to its using files from `../unsupported`

like:
```
#[path = "../unsupported/cmath.rs"]
pub mod cmath;
```

15 files changed:
library/std/src/sys/wasi/alloc.rs
library/std/src/sys/wasi/args.rs
library/std/src/sys/wasi/ext/fs.rs
library/std/src/sys/wasi/ext/io.rs
library/std/src/sys/wasi/ext/mod.rs
library/std/src/sys/wasi/fd.rs
library/std/src/sys/wasi/fs.rs
library/std/src/sys/wasi/io.rs
library/std/src/sys/wasi/net.rs
library/std/src/sys/wasi/os.rs
library/std/src/sys/wasi/pipe.rs
library/std/src/sys/wasi/process.rs
library/std/src/sys/wasi/stdio.rs
library/std/src/sys/wasi/thread.rs
library/std/src/sys/wasi/time.rs

index 57187851a14e37c57cff6a173f6f6dd4cc9c152a..4d0afe27bb8b2ed0943676ca253a180b92fae215 100644 (file)
@@ -1,26 +1,45 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 use crate::alloc::{GlobalAlloc, Layout, System};
 use crate::ptr;
 use crate::sys_common::alloc::{realloc_fallback, MIN_ALIGN};
 
+// SAFETY: All methods implemented follow the contract rules defined
+// in `GlobalAlloc`.
 #[stable(feature = "alloc_system_type", since = "1.28.0")]
 unsafe impl GlobalAlloc for System {
     #[inline]
     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
         if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
-            libc::malloc(layout.size()) as *mut u8
+            // SAFETY: `libc::malloc` is guaranteed to be safe, it will allocate
+            // `layout.size()` bytes of memory and return a pointer to it
+            unsafe { libc::malloc(layout.size()) as *mut u8 }
         } else {
-            libc::aligned_alloc(layout.align(), layout.size()) as *mut u8
+            // SAFETY: `libc::aligned_alloc` is guaranteed to be safe if
+            // `layout.size()` is a multiple of `layout.align()`. This
+            // constraint can be satisfied if `pad_to_align` is called,
+            // which creates a layout by rounding the size of this layout up
+            // to a multiple of the layout's alignment
+            let aligned_layout = layout.pad_to_align();
+            unsafe { libc::aligned_alloc(aligned_layout.align(), aligned_layout.size()) as *mut u8 }
         }
     }
 
     #[inline]
     unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
         if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
-            libc::calloc(layout.size(), 1) as *mut u8
+            // SAFETY: `libc::calloc` is safe as long that `layout.size() * 1`
+            // would not result in integer overflow which cannot happen,
+            // multiplying by one never overflows
+            unsafe { libc::calloc(layout.size(), 1) as *mut u8 }
         } else {
-            let ptr = self.alloc(layout.clone());
+            // SAFETY: The safety contract for `alloc` must be upheld by the caller
+            let ptr = unsafe { self.alloc(layout.clone()) };
             if !ptr.is_null() {
-                ptr::write_bytes(ptr, 0, layout.size());
+                // SAFETY: in the case of the `ptr` being not null
+                // it will be properly aligned and a valid ptr
+                // which satisfies `ptr::write_bytes` safety constrains
+                unsafe { ptr::write_bytes(ptr, 0, layout.size()) };
             }
             ptr
         }
@@ -28,15 +47,23 @@ unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
 
     #[inline]
     unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
-        libc::free(ptr as *mut libc::c_void)
+        // SAFETY: `libc::free` is guaranteed to be safe if `ptr` is allocated
+        // by this allocator or if `ptr` is NULL
+        unsafe { libc::free(ptr as *mut libc::c_void) }
     }
 
     #[inline]
     unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
         if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
-            libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
+            // SAFETY: `libc::realloc` is safe if `ptr` is allocated by this
+            // allocator or NULL
+            // - If `new_size` is 0 and `ptr` is not NULL, it will act as `libc::free`
+            // - If `new_size` is not 0 and `ptr` is NULL, it will act as `libc::malloc`
+            // - Else, it will resize the block accordingly
+            unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 }
         } else {
-            realloc_fallback(self, ptr, layout, new_size)
+            // SAFETY: The safety contract for `realloc_fallback` must be upheld by the caller
+            unsafe { realloc_fallback(self, ptr, layout, new_size) }
         }
     }
 }
index 02aa68d6f3ab75fc8b7f65fe708157e05dea7abd..9a27218e1fb70cb2510b36260277d8f7497ad81a 100644 (file)
@@ -1,3 +1,5 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 use crate::ffi::{CStr, OsStr, OsString};
 use crate::marker::PhantomData;
 use crate::os::wasi::ffi::OsStrExt;
index 501ad8ee7d6b3c390afc75128232db04752c973f..4f7cf6018d90fe99d9b2be792c86e84c6c71fe6b 100644 (file)
@@ -1,5 +1,6 @@
 //! WASI-specific extensions to primitives in the `std::fs` module.
 
+#![deny(unsafe_op_in_unsafe_fn)]
 #![unstable(feature = "wasi_ext", issue = "none")]
 
 use crate::fs::{self, File, Metadata, OpenOptions};
index e849400d67e7ac48d74d7eb14bf231df492e9390..4e8fa65eb20f093fe4906a1220582696e3deff09 100644 (file)
@@ -1,5 +1,6 @@
 //! WASI-specific extensions to general I/O primitives
 
+#![deny(unsafe_op_in_unsafe_fn)]
 #![unstable(feature = "wasi_ext", issue = "none")]
 
 use crate::fs;
index 58c8c46c969c180cf38f46107908de10445f577f..1cda30edcad0a4e17ffe18d9cdb399ae4f2d06ac 100644 (file)
@@ -1,3 +1,5 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 pub mod ffi;
 pub mod fs;
 pub mod io;
index 8458ded5db0b22b1e5400851ae44f98d41428d7f..ba66eba2ad38b7f605ffd1322d347eea3d4d5940 100644 (file)
@@ -1,3 +1,4 @@
+#![deny(unsafe_op_in_unsafe_fn)]
 #![allow(dead_code)]
 
 use super::err2io;
index 8408756f1b3be1cca5bff0c8e8dc031d7163895e..93a92b49cfc01bad1b8b663b0d5ee6715f681052 100644 (file)
@@ -1,3 +1,5 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 use crate::ffi::{CStr, CString, OsStr, OsString};
 use crate::fmt;
 use crate::io::{self, IoSlice, IoSliceMut, SeekFrom};
index 0ad2e152855b79dd937647354af773762e8d291f..ee017d13a4ca00a09007a2ad1f622ab09c1a6166 100644 (file)
@@ -1,3 +1,5 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 use crate::marker::PhantomData;
 use crate::slice;
 
index e186453588de5762af42ba6bdcd5c47a8f3870ce..8fd4bb76d854fe0d3f0e9a055cd6608f50af1f8f 100644 (file)
@@ -1,3 +1,5 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 use crate::convert::TryFrom;
 use crate::fmt;
 use crate::io::{self, IoSlice, IoSliceMut};
index 8052c0aa8a8d9bc59756c3920bc3559720779b5d..33c796ae9415deae4357c89783a65aa8f3016a78 100644 (file)
@@ -1,3 +1,5 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 use crate::any::Any;
 use crate::error::Error as StdError;
 use crate::ffi::{CStr, CString, OsStr, OsString};
index 10d0925823eb94660278ce2390c892c093d7e3c6..180fc114d86db2753290503ae629f66e58976792 100644 (file)
@@ -1,3 +1,5 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 use crate::io::{self, IoSlice, IoSliceMut};
 use crate::sys::Void;
 
index 7156c9ab92f2b6af3da9699a3618f76275280af1..c69d6376b0138d0a89b08143d47bcd1e0fe1f01b 100644 (file)
@@ -1,3 +1,5 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 use crate::ffi::OsStr;
 use crate::fmt;
 use crate::io;
index 23baafabf7933bd7ad116f0541a3466c687acb77..d82f6f411863d76e0a3907b03791ec7876ec3558 100644 (file)
@@ -1,3 +1,5 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 use crate::io::{self, IoSlice, IoSliceMut};
 use crate::mem::ManuallyDrop;
 use crate::sys::fd::WasiFd;
index 0d39b1cec328c5f5ece2af2202bdbac3e19bff24..8eaa5f09cb65691cae8944a93d7141bfc256cee9 100644 (file)
@@ -1,3 +1,5 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 use crate::ffi::CStr;
 use crate::io;
 use crate::mem;
index 80ec317b5a2c60520fc03c634455b24b0cf3aed9..2e720d11603a7239fa410276c8e2464cd0a6c16b 100644 (file)
@@ -1,3 +1,5 @@
+#![deny(unsafe_op_in_unsafe_fn)]
+
 use crate::time::Duration;
 
 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]