From 9093f6bc473919aabddfc5441c2992c0c7d7713f Mon Sep 17 00:00:00 2001
From: Peter Limkilde Svendsen <peter.limkilde@gmail.com>
Date: Thu, 29 Feb 2024 14:07:40 +0000
Subject: [PATCH] Use Cell representation for errno

---
 src/header/arpa_inet/mod.rs   |  6 ++---
 src/header/dirent/mod.rs      | 10 ++++----
 src/header/errno/mod.rs       |  6 ++---
 src/header/grp/mod.rs         |  4 +--
 src/header/netdb/mod.rs       | 20 +++++++--------
 src/header/pty/mod.rs         |  2 +-
 src/header/pwd/mod.rs         |  4 +--
 src/header/signal/mod.rs      | 14 +++-------
 src/header/stdio/getdelim.rs  |  8 +++---
 src/header/stdio/helpers.rs   |  2 +-
 src/header/stdio/mod.rs       | 19 +++++++-------
 src/header/stdio/printf.rs    |  4 +--
 src/header/stdlib/mod.rs      | 44 ++++++++++++++++----------------
 src/header/string/mod.rs      |  2 +-
 src/header/sys_ioctl/redox.rs |  2 +-
 src/header/sys_select/mod.rs  |  4 +--
 src/header/sys_uio/mod.rs     |  4 +--
 src/header/termios/mod.rs     | 10 ++++----
 src/header/time/mod.rs        |  2 +-
 src/header/unistd/brk.rs      |  4 +--
 src/header/unistd/mod.rs      | 32 ++++++++++-------------
 src/header/unistd/pathconf.rs |  4 +--
 src/header/unistd/sysconf.rs  |  4 +--
 src/header/wchar/mod.rs       | 14 +++++-----
 src/header/wchar/utf8.rs      |  8 +++---
 src/header/wchar/wprintf.rs   |  4 +--
 src/io/error.rs               |  2 +-
 src/ld_so/dso.rs              |  3 ++-
 src/macros.rs                 |  4 +--
 src/platform/linux/mod.rs     |  4 +--
 src/platform/mod.rs           |  4 +--
 src/platform/redox/epoll.rs   |  4 +--
 src/platform/redox/mod.rs     | 48 ++++++++++++++---------------------
 src/platform/redox/ptrace.rs  |  4 +--
 src/platform/redox/signal.rs  | 24 +++++++-----------
 src/platform/redox/socket.rs  | 22 ++++++++--------
 36 files changed, 159 insertions(+), 197 deletions(-)

diff --git a/src/header/arpa_inet/mod.rs b/src/header/arpa_inet/mod.rs
index 777430da..b439d7e8 100644
--- a/src/header/arpa_inet/mod.rs
+++ b/src/header/arpa_inet/mod.rs
@@ -56,7 +56,7 @@ pub unsafe extern "C" fn inet_ntoa(addr: in_addr) -> *const c_char {
 #[no_mangle]
 pub unsafe extern "C" fn inet_pton(domain: c_int, src: *const c_char, dest: *mut c_void) -> c_int {
     if domain != AF_INET {
-        platform::errno = EAFNOSUPPORT;
+        platform::errno.set(EAFNOSUPPORT);
         -1
     } else {
         let s_addr = slice::from_raw_parts_mut(
@@ -88,10 +88,10 @@ pub unsafe extern "C" fn inet_ntop(
     size: socklen_t,
 ) -> *const c_char {
     if domain != AF_INET {
-        platform::errno = EAFNOSUPPORT;
+        platform::errno.set(EAFNOSUPPORT);
         ptr::null()
     } else if size < 16 {
-        platform::errno = ENOSPC;
+        platform::errno.set(ENOSPC);
         ptr::null()
     } else {
         let s_addr = slice::from_raw_parts(
diff --git a/src/header/dirent/mod.rs b/src/header/dirent/mod.rs
index 76b061a3..ee6d6593 100644
--- a/src/header/dirent/mod.rs
+++ b/src/header/dirent/mod.rs
@@ -83,7 +83,7 @@ pub unsafe extern "C" fn readdir(dir: *mut DIR) -> *mut dirent {
         );
         if read <= 0 {
             if read != 0 && read != -errno::ENOENT {
-                platform::errno = -read;
+                platform::errno.set(-read);
             }
             return ptr::null_mut();
         }
@@ -145,8 +145,8 @@ pub unsafe extern "C" fn scandir(
         Err(err) => return -1,
     };
 
-    let old_errno = platform::errno;
-    platform::errno = 0;
+    let old_errno = platform::errno.get();
+    platform::errno.set(0);
 
     loop {
         let entry: *mut dirent = readdir(dir);
@@ -177,7 +177,7 @@ pub unsafe extern "C" fn scandir(
         return -1;
     }
 
-    if platform::errno != 0 {
+    if platform::errno.get() != 0 {
         for ptr in &mut vec {
             platform::free(*ptr as *mut c_void);
         }
@@ -185,7 +185,7 @@ pub unsafe extern "C" fn scandir(
     } else {
         *namelist = vec.leak();
 
-        platform::errno = old_errno;
+        platform::errno.set(old_errno);
         stdlib::qsort(
             *namelist as *mut c_void,
             len as size_t,
diff --git a/src/header/errno/mod.rs b/src/header/errno/mod.rs
index c42e76fc..b1748c6a 100644
--- a/src/header/errno/mod.rs
+++ b/src/header/errno/mod.rs
@@ -4,13 +4,13 @@ use crate::platform::{self, types::*};
 
 //TODO: Consider removing, provided for compatibility with newlib
 #[no_mangle]
-pub unsafe extern "C" fn __errno() -> *mut c_int {
+pub extern "C" fn __errno() -> *mut c_int {
     __errno_location()
 }
 
 #[no_mangle]
-pub unsafe extern "C" fn __errno_location() -> *mut c_int {
-    &mut platform::errno
+pub extern "C" fn __errno_location() -> *mut c_int {
+    platform::errno.as_ptr()
 }
 
 #[no_mangle]
diff --git a/src/header/grp/mod.rs b/src/header/grp/mod.rs
index d9e52120..55734757 100644
--- a/src/header/grp/mod.rs
+++ b/src/header/grp/mod.rs
@@ -224,9 +224,7 @@ fn parse_grp(line: String, destbuf: Option<DestBuffer>) -> Result<OwnedGrp, Erro
             let mut buf = MaybeAllocated::Borrowed(buf);
 
             if buf.len() < buf.len() {
-                unsafe {
-                    platform::errno = errno::ERANGE;
-                }
+                platform::errno.set(errno::ERANGE);
                 return Err(Error::BufTooSmall);
             }
 
diff --git a/src/header/netdb/mod.rs b/src/header/netdb/mod.rs
index 9bd4dfb7..fa48942b 100644
--- a/src/header/netdb/mod.rs
+++ b/src/header/netdb/mod.rs
@@ -244,7 +244,7 @@ pub unsafe extern "C" fn gethostbyaddr(
             &mut HOST_ENTRY
         }
         Err(e) => {
-            platform::errno = e;
+            platform::errno.set(e);
             ptr::null_mut()
         }
     }
@@ -307,14 +307,14 @@ pub unsafe extern "C" fn gethostbyname(name: *const c_char) -> *mut hostent {
     let mut host = match lookup_host(str::from_utf8_unchecked(name_cstr.to_bytes())) {
         Ok(lookuphost) => lookuphost,
         Err(e) => {
-            platform::errno = e;
+            platform::errno.set(e);
             return ptr::null_mut();
         }
     };
     let host_addr = match host.next() {
         Some(result) => result,
         None => {
-            platform::errno = ENOENT;
+            platform::errno.set(ENOENT);
             return ptr::null_mut();
         }
     };
@@ -363,7 +363,7 @@ pub unsafe extern "C" fn getnetbyname(name: *const c_char) -> *mut netent {
     }
     setnetent(NET_STAYOPEN);
 
-    platform::errno = ENOENT;
+    platform::errno.set(ENOENT);
     ptr::null_mut() as *mut netent
 }
 
@@ -459,7 +459,7 @@ pub unsafe extern "C" fn getprotobyname(name: *const c_char) -> *mut protoent {
     }
     setprotoent(PROTO_STAYOPEN);
 
-    platform::errno = ENOENT;
+    platform::errno.set(ENOENT);
     ptr::null_mut() as *mut protoent
 }
 
@@ -477,7 +477,7 @@ pub unsafe extern "C" fn getprotobynumber(number: c_int) -> *mut protoent {
         }
     }
     setprotoent(PROTO_STAYOPEN);
-    platform::errno = ENOENT;
+    platform::errno.set(ENOENT);
     ptr::null_mut() as *mut protoent
 }
 
@@ -566,7 +566,7 @@ pub unsafe extern "C" fn getservbyname(name: *const c_char, proto: *const c_char
         }
     }
     setservent(SERV_STAYOPEN);
-    platform::errno = ENOENT;
+    platform::errno.set(ENOENT);
     ptr::null_mut() as *mut servent
 }
 
@@ -596,7 +596,7 @@ pub unsafe extern "C" fn getservbyport(port: c_int, proto: *const c_char) -> *mu
         }
     }
     setservent(SERV_STAYOPEN);
-    platform::errno = ENOENT;
+    platform::errno.set(ENOENT);
     ptr::null_mut()
 }
 
@@ -762,7 +762,7 @@ pub unsafe extern "C" fn getaddrinfo(
         let lookuphost = match lookup_host(str::from_utf8_unchecked(node.to_bytes())) {
             Ok(lookuphost) => lookuphost,
             Err(e) => {
-                platform::errno = e;
+                platform::errno.set(e);
                 return EAI_SYSTEM;
             }
         };
@@ -840,7 +840,7 @@ pub unsafe extern "C" fn getnameinfo(
 
     eprintln!("getnameinfo({:p}, {}, {:#x})", addr, addrlen, flags);
 
-    platform::errno = ENOSYS;
+    platform::errno.set(ENOSYS);
     EAI_SYSTEM
 }
 
diff --git a/src/header/pty/mod.rs b/src/header/pty/mod.rs
index 592bf4af..fd0cf44e 100644
--- a/src/header/pty/mod.rs
+++ b/src/header/pty/mod.rs
@@ -106,7 +106,7 @@ pub unsafe extern "C" fn forkpty(
             let mut status = 0;
             sys_wait::waitpid(pid, &mut status, 0);
             pid = -1;
-            platform::errno = ec;
+            platform::errno.set(ec);
         }
         unistd::close(p[0]);
     }
diff --git a/src/header/pwd/mod.rs b/src/header/pwd/mod.rs
index 1919a887..e8da45f2 100644
--- a/src/header/pwd/mod.rs
+++ b/src/header/pwd/mod.rs
@@ -154,9 +154,7 @@ fn getpwent_r(
         Some(dst) => {
             let mut new = MaybeAllocated::Borrowed(dst);
             if new.len() < buf.len() {
-                unsafe {
-                    platform::errno = errno::ERANGE;
-                }
+                platform::errno.set(errno::ERANGE);
                 return Err(Cause::Other);
             }
             new[..buf.len()].copy_from_slice(&buf);
diff --git a/src/header/signal/mod.rs b/src/header/signal/mod.rs
index e56e67af..7ed401b4 100644
--- a/src/header/signal/mod.rs
+++ b/src/header/signal/mod.rs
@@ -92,7 +92,7 @@ pub unsafe extern "C" fn pthread_sigmask(
         0
     } else {
         //TODO: Fix race
-        unsafe { platform::errno }
+        platform::errno.get()
     }
 }
 
@@ -119,9 +119,7 @@ pub unsafe extern "C" fn sigaction(
 #[no_mangle]
 pub extern "C" fn sigaddset(set: *mut sigset_t, signo: c_int) -> c_int {
     if signo <= 0 || signo as usize > NSIG {
-        unsafe {
-            platform::errno = errno::EINVAL;
-        }
+        platform::errno.set(errno::EINVAL);
         return -1;
     }
 
@@ -148,9 +146,7 @@ pub unsafe extern "C" fn sigaltstack(ss: *const stack_t, old_ss: *mut stack_t) -
 #[no_mangle]
 pub extern "C" fn sigdelset(set: *mut sigset_t, signo: c_int) -> c_int {
     if signo <= 0 || signo as usize > NSIG {
-        unsafe {
-            platform::errno = errno::EINVAL;
-        }
+        platform::errno.set(errno::EINVAL);
         return -1;
     }
 
@@ -214,9 +210,7 @@ pub extern "C" fn siginterrupt(sig: c_int, flag: c_int) -> c_int {
 #[no_mangle]
 pub extern "C" fn sigismember(set: *const sigset_t, signo: c_int) -> c_int {
     if signo <= 0 || signo as usize > NSIG {
-        unsafe {
-            platform::errno = errno::EINVAL;
-        }
+        platform::errno.set(errno::EINVAL);
         return -1;
     }
 
diff --git a/src/header/stdio/getdelim.rs b/src/header/stdio/getdelim.rs
index c5242e31..f621c7ee 100644
--- a/src/header/stdio/getdelim.rs
+++ b/src/header/stdio/getdelim.rs
@@ -59,7 +59,7 @@ pub unsafe extern "C" fn getdelim(
         if let (Some(ptr), Some(n), Some(file)) = (lineptr.as_mut(), n.as_mut(), stream.as_mut()) {
             (ptr, n, file)
         } else {
-            errno = EINVAL;
+            errno.set(EINVAL);
             return -1 as ssize_t;
         };
 
@@ -72,7 +72,7 @@ pub unsafe extern "C" fn getdelim(
     let delim: u8 = if let Ok(delim) = delim.try_into() {
         delim
     } else {
-        errno = EINVAL;
+        errno.set(EINVAL);
         return -1;
     };
 
@@ -92,7 +92,7 @@ pub unsafe extern "C" fn getdelim(
     // "[EOVERFLOW]
     // The number of bytes to be written into the buffer, including the delimiter character (if encountered), would exceed {SSIZE_MAX}."
     if unlikely(count > ssize_t::MAX as usize) {
-        errno = EOVERFLOW;
+        errno.set(EOVERFLOW);
         return -1;
     }
 
@@ -124,7 +124,7 @@ pub unsafe extern "C" fn getdelim(
         *lineptr = stdlib::realloc(*lineptr as *mut c_void, *n) as *mut c_char;
         if unlikely(lineptr.is_null() && *n != 0usize) {
             // memory error; realloc returns NULL on alloc'ing 0 bytes
-            errno = ENOMEM;
+            errno.set(ENOMEM);
             return -1;
         }
 
diff --git a/src/header/stdio/helpers.rs b/src/header/stdio/helpers.rs
index 60b72143..7fcfcc8c 100644
--- a/src/header/stdio/helpers.rs
+++ b/src/header/stdio/helpers.rs
@@ -40,7 +40,7 @@ pub unsafe fn parse_mode_flags(mode_str: *const c_char) -> i32 {
 /// Open a file with the file descriptor `fd` in the mode `mode`
 pub unsafe fn _fdopen(fd: c_int, mode: *const c_char) -> Option<*mut FILE> {
     if *mode != b'r' as i8 && *mode != b'w' as i8 && *mode != b'a' as i8 {
-        platform::errno = errno::EINVAL;
+        platform::errno.set(errno::EINVAL);
         return None;
     }
 
diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs
index ca1ee2e4..23f659d2 100644
--- a/src/header/stdio/mod.rs
+++ b/src/header/stdio/mod.rs
@@ -498,7 +498,7 @@ pub unsafe extern "C" fn flockfile(file: *mut FILE) {
 pub unsafe extern "C" fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE {
     let initial_mode = *mode;
     if initial_mode != b'r' as i8 && initial_mode != b'w' as i8 && initial_mode != b'a' as i8 {
-        platform::errno = errno::EINVAL;
+        platform::errno.set(errno::EINVAL);
         return ptr::null_mut();
     }
 
@@ -821,7 +821,7 @@ pub unsafe extern "C" fn pclose(stream: *mut FILE) -> c_int {
         if let Some(pid) = stream.pid.take() {
             pid
         } else {
-            errno = errno::ECHILD;
+            errno.set(errno::ECHILD);
             return -1;
         }
     };
@@ -842,11 +842,12 @@ pub unsafe extern "C" fn perror(s: *const c_char) {
     let s_str = str::from_utf8_unchecked(s_cstr.to_bytes());
 
     let mut w = platform::FileWriter(2);
-    if errno >= 0 && errno < STR_ERROR.len() as c_int {
-        w.write_fmt(format_args!("{}: {}\n", s_str, STR_ERROR[errno as usize]))
+    let err = errno.get();
+    if err >= 0 && err < STR_ERROR.len() as c_int {
+        w.write_fmt(format_args!("{}: {}\n", s_str, STR_ERROR[err as usize]))
             .unwrap();
     } else {
-        w.write_fmt(format_args!("{}: Unknown error {}\n", s_str, errno))
+        w.write_fmt(format_args!("{}: Unknown error {}\n", s_str, err))
             .unwrap();
     }
 }
@@ -865,7 +866,7 @@ pub unsafe extern "C" fn popen(command: *const c_char, mode: *const c_char) -> *
             b'r' if write_opt.is_none() => write_opt = Some(false),
             b'w' if write_opt.is_none() => write_opt = Some(true),
             _ => {
-                errno = errno::EINVAL;
+                errno.set(errno::EINVAL);
                 return ptr::null_mut();
             }
         }
@@ -874,7 +875,7 @@ pub unsafe extern "C" fn popen(command: *const c_char, mode: *const c_char) -> *
     let write = match write_opt {
         Some(some) => some,
         None => {
-            errno = errno::EINVAL;
+            errno.set(errno::EINVAL);
             return ptr::null_mut();
         }
     };
@@ -1143,9 +1144,9 @@ unsafe extern "C" fn tmpnam_inner(buf: *mut c_char, offset: usize) -> *mut c_cha
     buf.add(offset)
         .copy_from_nonoverlapping(TEMPLATE.as_ptr() as _, TEMPLATE.len());
 
-    let err = platform::errno;
+    let err = platform::errno.get();
     stdlib::mktemp(buf);
-    platform::errno = err;
+    platform::errno.set(err);
 
     if *buf == 0 {
         ptr::null_mut()
diff --git a/src/header/stdio/printf.rs b/src/header/stdio/printf.rs
index d06570fa..e4897e74 100644
--- a/src/header/stdio/printf.rs
+++ b/src/header/stdio/printf.rs
@@ -886,7 +886,7 @@ unsafe fn inner_printf<W: Write>(w: W, format: *const c_char, mut ap: VaList) ->
                             let c = match char::from_u32(*ptr as _) {
                                 Some(c) => c,
                                 None => {
-                                    platform::errno = EILSEQ;
+                                    platform::errno.set(EILSEQ);
                                     return Err(io::last_os_error());
                                 }
                             };
@@ -922,7 +922,7 @@ unsafe fn inner_printf<W: Write>(w: W, format: *const c_char, mut ap: VaList) ->
                     let c = match char::from_u32(c as _) {
                         Some(c) => c,
                         None => {
-                            platform::errno = EILSEQ;
+                            platform::errno.set(EILSEQ);
                             return Err(io::last_os_error());
                         }
                     };
diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs
index 7acc6b42..52e506dc 100644
--- a/src/header/stdlib/mod.rs
+++ b/src/header/stdlib/mod.rs
@@ -125,7 +125,7 @@ pub unsafe extern "C" fn aligned_alloc(alignment: size_t, size: size_t) -> *mut
          * difference between aligned_alloc() and memalign(). */
         memalign(alignment, size)
     } else {
-        platform::errno = EINVAL;
+        platform::errno.set(EINVAL);
         ptr::null_mut()
     }
 }
@@ -242,7 +242,7 @@ pub unsafe extern "C" fn calloc(nelem: size_t, elsize: size_t) -> *mut c_void {
         }
         None => {
             // For overflowing multiplication, we have to set errno here
-            platform::errno = ENOMEM;
+            platform::errno.set(ENOMEM);
             ptr::null_mut()
         }
     }
@@ -545,7 +545,7 @@ pub unsafe extern "C" fn lrand48() -> c_long {
 pub unsafe extern "C" fn malloc(size: size_t) -> *mut c_void {
     let ptr = platform::alloc(size);
     if ptr.is_null() {
-        platform::errno = ENOMEM;
+        platform::errno.set(ENOMEM);
     }
     ptr
 }
@@ -555,11 +555,11 @@ pub unsafe extern "C" fn memalign(alignment: size_t, size: size_t) -> *mut c_voi
     if alignment.is_power_of_two() {
         let ptr = platform::alloc_align(size, alignment);
         if ptr.is_null() {
-            platform::errno = ENOMEM;
+            platform::errno.set(ENOMEM);
         }
         ptr
     } else {
-        platform::errno = EINVAL;
+        platform::errno.set(EINVAL);
         ptr::null_mut()
     }
 }
@@ -599,13 +599,13 @@ where
     let len = unsafe { strlen(name) as c_int };
 
     if len < 6 || suffix_len > len - 6 {
-        unsafe { platform::errno = errno::EINVAL };
+        platform::errno.set(errno::EINVAL);
         return None;
     }
 
     for i in (len - suffix_len - 6)..(len - suffix_len) {
         if unsafe { *name.offset(i as isize) } != b'X' as c_char {
-            unsafe { platform::errno = errno::EINVAL };
+            platform::errno.set(errno::EINVAL);
             return None;
         }
     }
@@ -630,7 +630,7 @@ where
         }
     }
 
-    unsafe { platform::errno = errno::EEXIST }
+    platform::errno.set(errno::EEXIST);
 
     None
 }
@@ -639,7 +639,7 @@ where
 pub unsafe extern "C" fn mktemp(name: *mut c_char) -> *mut c_char {
     if inner_mktemp(name, 0, || {
         let name = CStr::from_ptr(name);
-        if Sys::access(name, 0) != 0 && platform::errno == ENOENT {
+        if Sys::access(name, 0) != 0 && platform::errno.get() == ENOENT {
             Some(())
         } else {
             None
@@ -735,8 +735,8 @@ pub unsafe extern "C" fn posix_openpt(flags: c_int) -> c_int {
     #[cfg(target_os = "linux")]
     let r = open((b"/dev/ptmx\0" as *const u8).cast(), flags);
 
-    if r < 0 && platform::errno == ENOSPC {
-        platform::errno = EAGAIN;
+    if r < 0 && platform::errno.get() == ENOSPC {
+        platform::errno.set(EAGAIN);
     }
 
     return r;
@@ -756,7 +756,7 @@ unsafe extern "C" fn ptsname(fd: c_int) -> *mut c_char {
 #[no_mangle]
 unsafe extern "C" fn ptsname_r(fd: c_int, buf: *mut c_char, buflen: size_t) -> c_int {
     if buf.is_null() {
-        platform::errno = EINVAL;
+        platform::errno.set(EINVAL);
         EINVAL
     } else {
         __ptsname_r(fd, buf, buflen)
@@ -772,7 +772,7 @@ unsafe fn __ptsname_r(fd: c_int, buf: *mut c_char, buflen: size_t) -> c_int {
         if let Ok(name) = CStr::from_ptr(tty_ptr).to_str() {
             let len = name.len();
             if len > buflen {
-                platform::errno = ERANGE;
+                platform::errno.set(ERANGE);
                 return ERANGE;
             } else {
                 // we have checked the string will fit in the buffer
@@ -783,31 +783,31 @@ unsafe fn __ptsname_r(fd: c_int, buf: *mut c_char, buflen: size_t) -> c_int {
             }
         }
     }
-    platform::errno
+    platform::errno.get()
 }
 
 #[cfg(target_os = "linux")]
 #[inline(always)]
 unsafe fn __ptsname_r(fd: c_int, buf: *mut c_char, buflen: size_t) -> c_int {
     let mut pty = 0;
-    let err = platform::errno;
+    let err = platform::errno.get();
 
     if ioctl(fd, TIOCGPTN, &mut pty as *mut _ as *mut c_void) == 0 {
         let name = format!("/dev/pts/{}", pty);
         let len = name.len();
         if len > buflen {
-            platform::errno = ERANGE;
+            platform::errno.set(ERANGE);
             ERANGE
         } else {
             // we have checked the string will fit in the buffer
             // so can use strcpy safely
             let s = name.as_ptr().cast();
             ptr::copy_nonoverlapping(s, buf, len);
-            platform::errno = err;
+            platform::errno.set(err);
             0
         }
     } else {
-        platform::errno
+        platform::errno.get()
     }
 }
 
@@ -932,7 +932,7 @@ pub unsafe extern "C" fn random() -> c_long {
 pub unsafe extern "C" fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void {
     let new_ptr = platform::realloc(ptr, size);
     if new_ptr.is_null() {
-        platform::errno = ENOMEM;
+        platform::errno.set(ENOMEM);
     }
     new_ptr
 }
@@ -944,7 +944,7 @@ pub unsafe extern "C" fn reallocarray(ptr: *mut c_void, m: size_t, n: size_t) ->
         Some(size) => realloc(ptr, size),
         None => {
             // For overflowing multiplication, we have to set errno here
-            platform::errno = ENOMEM;
+            platform::errno.set(ENOMEM);
             ptr::null_mut()
         }
     }
@@ -1185,7 +1185,7 @@ pub unsafe fn convert_integer(s: *const c_char, base: c_int) -> Option<(c_ulong,
             {
                 num = res;
             } else {
-                platform::errno = ERANGE;
+                platform::errno.set(ERANGE);
                 num = c_ulong::max_value();
                 overflowed = true;
             }
@@ -1355,7 +1355,7 @@ pub unsafe extern "C" fn valloc(size: size_t) -> *mut c_void {
              * EINVAL, hence no call to memalign(). */
             let ptr = platform::alloc_align(size, page_size);
             if ptr.is_null() {
-                platform::errno = ENOMEM;
+                platform::errno.set(ENOMEM);
             }
             ptr
         }
diff --git a/src/header/string/mod.rs b/src/header/string/mod.rs
index fa916172..b06fd49b 100644
--- a/src/header/string/mod.rs
+++ b/src/header/string/mod.rs
@@ -214,7 +214,7 @@ pub unsafe extern "C" fn strndup(s1: *const c_char, size: size_t) -> *mut c_char
     // the "+ 1" is to account for the NUL byte
     let buffer = platform::alloc(len + 1) as *mut c_char;
     if buffer.is_null() {
-        platform::errno = ENOMEM as c_int;
+        platform::errno.set(ENOMEM as c_int);
     } else {
         //memcpy(buffer, s1, len)
         for i in 0..len {
diff --git a/src/header/sys_ioctl/redox.rs b/src/header/sys_ioctl/redox.rs
index 6355cb76..1f3a57ee 100644
--- a/src/header/sys_ioctl/redox.rs
+++ b/src/header/sys_ioctl/redox.rs
@@ -158,7 +158,7 @@ pub unsafe extern "C" fn ioctl(fd: c_int, request: c_ulong, out: *mut c_void) ->
             0
         }
         _ => {
-            platform::errno = errno::EINVAL;
+            platform::errno.set(errno::EINVAL);
             -1
         }
     }
diff --git a/src/header/sys_select/mod.rs b/src/header/sys_select/mod.rs
index 6303d5cc..8fd8ba6b 100644
--- a/src/header/sys_select/mod.rs
+++ b/src/header/sys_select/mod.rs
@@ -35,7 +35,7 @@ pub fn select_epoll(
     timeout: Option<&mut timeval>,
 ) -> c_int {
     if nfds < 0 || nfds > FD_SETSIZE as i32 {
-        unsafe { platform::errno = errno::EINVAL };
+        platform::errno.set(errno::EINVAL);
         return -1;
     };
 
@@ -81,7 +81,7 @@ pub fn select_epoll(
                 ..Default::default()
             };
             if epoll_ctl(*ep, EPOLL_CTL_ADD, fd, &mut event) < 0 {
-                if unsafe { platform::errno == errno::EPERM } {
+                if platform::errno.get() == errno::EPERM {
                     not_epoll += 1;
                 } else {
                     return -1;
diff --git a/src/header/sys_uio/mod.rs b/src/header/sys_uio/mod.rs
index 47a9b5a3..d715d73b 100644
--- a/src/header/sys_uio/mod.rs
+++ b/src/header/sys_uio/mod.rs
@@ -42,7 +42,7 @@ unsafe fn scatter(iovs: &[iovec], vec: Vec<u8>) {
 #[no_mangle]
 pub unsafe extern "C" fn readv(fd: c_int, iov: *const iovec, iovcnt: c_int) -> ssize_t {
     if iovcnt < 0 || iovcnt > IOV_MAX {
-        platform::errno = errno::EINVAL;
+        platform::errno.set(errno::EINVAL);
         return -1;
     }
 
@@ -59,7 +59,7 @@ pub unsafe extern "C" fn readv(fd: c_int, iov: *const iovec, iovcnt: c_int) -> s
 #[no_mangle]
 pub unsafe extern "C" fn writev(fd: c_int, iov: *const iovec, iovcnt: c_int) -> ssize_t {
     if iovcnt < 0 || iovcnt > IOV_MAX {
-        platform::errno = errno::EINVAL;
+        platform::errno.set(errno::EINVAL);
         return -1;
     }
 
diff --git a/src/header/termios/mod.rs b/src/header/termios/mod.rs
index feecc5a2..523521c9 100644
--- a/src/header/termios/mod.rs
+++ b/src/header/termios/mod.rs
@@ -69,7 +69,7 @@ pub unsafe extern "C" fn tcgetattr(fd: c_int, out: *mut termios) -> c_int {
 #[no_mangle]
 pub unsafe extern "C" fn tcsetattr(fd: c_int, act: c_int, value: *const termios) -> c_int {
     if act < 0 || act > 2 {
-        platform::errno = errno::EINVAL;
+        platform::errno.set(errno::EINVAL);
         return -1;
     }
     // This is safe because ioctl shouldn't modify the value
@@ -111,7 +111,7 @@ pub unsafe extern "C" fn cfsetispeed(termios_p: *mut termios, speed: speed_t) ->
             0
         }
         _ => {
-            platform::errno = errno::EINVAL;
+            platform::errno.set(errno::EINVAL);
             -1
         }
     }
@@ -121,7 +121,7 @@ pub unsafe extern "C" fn cfsetispeed(termios_p: *mut termios, speed: speed_t) ->
 #[no_mangle]
 pub unsafe extern "C" fn cfsetispeed(termios_p: *mut termios, speed: speed_t) -> c_int {
     //TODO
-    platform::errno = errno::EINVAL;
+    platform::errno.set(errno::EINVAL);
     -1
 }
 
@@ -134,7 +134,7 @@ pub unsafe extern "C" fn cfsetospeed(termios_p: *mut termios, speed: speed_t) ->
             0
         }
         _ => {
-            platform::errno = errno::EINVAL;
+            platform::errno.set(errno::EINVAL);
             -1
         }
     }
@@ -144,7 +144,7 @@ pub unsafe extern "C" fn cfsetospeed(termios_p: *mut termios, speed: speed_t) ->
 #[no_mangle]
 pub unsafe extern "C" fn cfsetospeed(termios_p: *mut termios, speed: speed_t) -> c_int {
     //TODO
-    platform::errno = errno::EINVAL;
+    platform::errno.set(errno::EINVAL);
     -1
 }
 
diff --git a/src/header/time/mod.rs b/src/header/time/mod.rs
index 97675840..6534bd32 100644
--- a/src/header/time/mod.rs
+++ b/src/header/time/mod.rs
@@ -396,7 +396,7 @@ pub unsafe extern "C" fn gmtime_r(clock: *const time_t, result: *mut tm) -> *mut
             result
         }
         Err(_) => {
-            platform::errno = EOVERFLOW;
+            platform::errno.set(EOVERFLOW);
             core::ptr::null_mut()
         }
     }
diff --git a/src/header/unistd/brk.rs b/src/header/unistd/brk.rs
index ab97d49a..9b473794 100644
--- a/src/header/unistd/brk.rs
+++ b/src/header/unistd/brk.rs
@@ -12,7 +12,7 @@ pub unsafe extern "C" fn brk(addr: *mut c_void) -> c_int {
     BRK = Sys::brk(addr);
 
     if BRK < addr {
-        platform::errno = ENOMEM;
+        platform::errno.set(ENOMEM);
         return -1;
     }
 
@@ -33,7 +33,7 @@ pub unsafe extern "C" fn sbrk(incr: intptr_t) -> *mut c_void {
         BRK = Sys::brk(addr);
 
         if BRK < addr {
-            platform::errno = ENOMEM;
+            platform::errno.set(ENOMEM);
             return -1isize as *mut c_void;
         }
     }
diff --git a/src/header/unistd/mod.rs b/src/header/unistd/mod.rs
index 2ec5fdde..1cb6236a 100644
--- a/src/header/unistd/mod.rs
+++ b/src/header/unistd/mod.rs
@@ -78,15 +78,13 @@ pub extern "C" fn alarm(seconds: c_uint) -> c_uint {
         },
         ..Default::default()
     };
-    let errno_backup = unsafe { platform::errno };
+    let errno_backup = platform::errno.get();
     let secs = if sys_time::setitimer(sys_time::ITIMER_REAL, &timer, &mut timer) < 0 {
         0
     } else {
         timer.it_value.tv_sec as c_uint + if timer.it_value.tv_usec > 0 { 1 } else { 0 }
     };
-    unsafe {
-        platform::errno = errno_backup;
-    }
+    platform::errno.set(errno_backup);
 
     secs
 }
@@ -100,9 +98,7 @@ pub unsafe extern "C" fn chdir(path: *const c_char) -> c_int {
 #[no_mangle]
 pub extern "C" fn chroot(path: *const c_char) -> c_int {
     // TODO: Implement
-    unsafe {
-        platform::errno = crate::header::errno::EPERM;
-    }
+    platform::errno.set(crate::header::errno::EPERM);
 
     -1
 }
@@ -162,12 +158,12 @@ unsafe fn with_argv(
         // TODO: Use ARG_MAX, not this hardcoded constant
         let ptr = crate::header::stdlib::malloc(argc * mem::size_of::<*const c_char>());
         if ptr.is_null() {
-            platform::errno = ENOMEM;
+            platform::errno.set(ENOMEM);
             return -1;
         }
         slice::from_raw_parts_mut(ptr.cast::<MaybeUninit<*const c_char>>(), argc)
     } else {
-        platform::errno = E2BIG;
+        platform::errno.set(E2BIG);
         return -1;
     };
     out[0].write(arg0);
@@ -266,14 +262,14 @@ pub unsafe extern "C" fn execvp(file: *const c_char, argv: *const *mut c_char) -
                 let program_c = CStr::from_bytes_with_nul(&program).unwrap();
                 execv(program_c.as_ptr(), argv);
 
-                match platform::errno {
+                match platform::errno.get() {
                     errno::ENOENT => (),
                     other => error = other,
                 }
             }
         }
 
-        platform::errno = error;
+        platform::errno.set(error);
         -1
     }
 }
@@ -437,7 +433,7 @@ pub unsafe extern "C" fn getlogin() -> *mut c_char {
 #[no_mangle]
 pub extern "C" fn getlogin_r(name: *mut c_char, namesize: size_t) -> c_int {
     //TODO: Determine correct getlogin result on Redox
-    unsafe { platform::errno = errno::ENOENT };
+    platform::errno.set(errno::ENOENT);
     -1
 }
 
@@ -531,7 +527,7 @@ pub unsafe extern "C" fn lockf(fildes: c_int, function: c_int, size: off_t) -> c
             if fl.l_type == fcntl::F_UNLCK as c_short || fl.l_pid == getpid() {
                 return 0;
             }
-            platform::errno = errno::EACCES;
+            platform::errno.set(errno::EACCES);
             return -1;
         }
         fcntl::F_ULOCK => {
@@ -545,7 +541,7 @@ pub unsafe extern "C" fn lockf(fildes: c_int, function: c_int, size: off_t) -> c
             return fcntl::fcntl(fildes, fcntl::F_SETLKW, &mut fl as *mut _ as c_ulonglong);
         }
         _ => {
-            platform::errno = errno::EINVAL;
+            platform::errno.set(errno::EINVAL);
             return -1;
         }
     };
@@ -801,7 +797,7 @@ pub extern "C" fn ttyname_r(fildes: c_int, name: *mut c_char, namesize: size_t)
 
     let len = Sys::fpath(fildes, &mut name[..namesize - 1]);
     if len < 0 {
-        return unsafe { -platform::errno };
+        return -platform::errno.get();
     }
     name[len as usize] = 0;
 
@@ -820,15 +816,13 @@ pub extern "C" fn ualarm(usecs: useconds_t, interval: useconds_t) -> useconds_t
             tv_usec: interval as suseconds_t,
         },
     };
-    let errno_backup = unsafe { platform::errno };
+    let errno_backup = platform::errno.get();
     let ret = if sys_time::setitimer(sys_time::ITIMER_REAL, &timer, &mut timer) < 0 {
         0
     } else {
         timer.it_value.tv_sec as useconds_t * 1_000_000 + timer.it_value.tv_usec as useconds_t
     };
-    unsafe {
-        platform::errno = errno_backup;
-    }
+    platform::errno.set(errno_backup);
 
     ret
 }
diff --git a/src/header/unistd/pathconf.rs b/src/header/unistd/pathconf.rs
index 2dd6ee36..7974e042 100644
--- a/src/header/unistd/pathconf.rs
+++ b/src/header/unistd/pathconf.rs
@@ -50,9 +50,7 @@ fn pc(name: c_int) -> c_long {
         _PC_SYMLINK_MAX => -1,
         _PC_2_SYMLINKS => 1,
         _ => {
-            unsafe {
-                platform::errno = errno::EINVAL;
-            }
+            platform::errno.set(errno::EINVAL);
             -1
         }
     }
diff --git a/src/header/unistd/sysconf.rs b/src/header/unistd/sysconf.rs
index 7afdbec8..3fce2930 100644
--- a/src/header/unistd/sysconf.rs
+++ b/src/header/unistd/sysconf.rs
@@ -53,9 +53,7 @@ pub extern "C" fn sysconf(name: c_int) -> c_long {
         _SC_SYMLOOP_MAX => -1,
         _SC_HOST_NAME_MAX => 64,
         _ => {
-            unsafe {
-                platform::errno = errno::EINVAL;
-            }
+            platform::errno.set(errno::EINVAL);
             -1
         }
     }
diff --git a/src/header/wchar/mod.rs b/src/header/wchar/mod.rs
index dcbad579..34fd1697 100644
--- a/src/header/wchar/mod.rs
+++ b/src/header/wchar/mod.rs
@@ -33,10 +33,10 @@ pub unsafe extern "C" fn btowc(c: c_int) -> wint_t {
     let c = uc as c_char;
     let mut ps: mbstate_t = mbstate_t;
     let mut wc: wchar_t = 0;
-    let saved_errno = platform::errno;
+    let saved_errno = platform::errno.get();
     let status = mbrtowc(&mut wc, &c as *const c_char, 1, &mut ps);
     if status == usize::max_value() || status == usize::max_value() - 1 {
-        platform::errno = saved_errno;
+        platform::errno.set(saved_errno);
         return WEOF;
     }
     wc as wint_t
@@ -59,7 +59,7 @@ pub unsafe extern "C" fn fgetwc(stream: *mut FILE) -> wint_t {
         );
 
         if nread != 1 {
-            errno = EILSEQ;
+            errno.set(EILSEQ);
             return WEOF;
         }
 
@@ -75,7 +75,7 @@ pub unsafe extern "C" fn fgetwc(stream: *mut FILE) -> wint_t {
             } else if buf[0] >> 3 == 0x1e {
                 4
             } else {
-                errno = EILSEQ;
+                errno.set(EILSEQ);
                 return WEOF;
             };
         }
@@ -392,7 +392,7 @@ pub unsafe extern "C" fn wcsdup(s: *const wchar_t) -> *mut wchar_t {
     let d = malloc((l + 1) * mem::size_of::<wchar_t>()) as *mut wchar_t;
 
     if d.is_null() {
-        errno = ENOMEM;
+        errno.set(ENOMEM);
         return ptr::null_mut();
     }
 
@@ -586,7 +586,7 @@ pub unsafe extern "C" fn wcsnrtombs(
         let ret = wcrtomb(buf.as_mut_ptr(), **src, ps);
 
         if ret == size_t::MAX {
-            errno = EILSEQ;
+            errno.set(EILSEQ);
             return size_t::MAX;
         }
 
@@ -785,7 +785,7 @@ macro_rules! strtou_impl {
             result = match new {
                 Some(new) => new,
                 None => {
-                    platform::errno = ERANGE;
+                    platform::errno.set(ERANGE);
                     return !0;
                 }
             };
diff --git a/src/header/wchar/utf8.rs b/src/header/wchar/utf8.rs
index d3fa8c65..21f3c346 100644
--- a/src/header/wchar/utf8.rs
+++ b/src/header/wchar/utf8.rs
@@ -44,18 +44,18 @@ fn utf8_char_width(b: u8) -> usize {
 pub unsafe fn mbrtowc(pwc: *mut wchar_t, s: *const c_char, n: usize, ps: *mut mbstate_t) -> usize {
     let size = utf8_char_width(*s as u8);
     if size > n {
-        platform::errno = errno::EILSEQ;
+        platform::errno.set(errno::EILSEQ);
         return -2isize as usize;
     }
     if size == 0 {
-        platform::errno = errno::EILSEQ;
+        platform::errno.set(errno::EILSEQ);
         return -1isize as usize;
     }
 
     let slice = slice::from_raw_parts(s as *const u8, size);
     let decoded = str::from_utf8(slice);
     if decoded.is_err() {
-        platform::errno = errno::EILSEQ;
+        platform::errno.set(errno::EILSEQ);
         return -1isize as usize;
     }
 
@@ -79,7 +79,7 @@ pub unsafe fn wcrtomb(s: *mut c_char, wc: wchar_t, ps: *mut mbstate_t) -> usize
     let dc = char::from_u32(wc as u32);
 
     if dc.is_none() {
-        platform::errno = errno::EILSEQ;
+        platform::errno.set(errno::EILSEQ);
         return -1isize as usize;
     }
 
diff --git a/src/header/wchar/wprintf.rs b/src/header/wchar/wprintf.rs
index bc1ed17f..5576599f 100644
--- a/src/header/wchar/wprintf.rs
+++ b/src/header/wchar/wprintf.rs
@@ -898,7 +898,7 @@ unsafe fn inner_wprintf<W: Write>(
                             let c = match char::from_u32(*ptr as _) {
                                 Some(c) => c,
                                 None => {
-                                    platform::errno = EILSEQ;
+                                    platform::errno.set(EILSEQ);
                                     return Err(io::last_os_error());
                                 }
                             };
@@ -934,7 +934,7 @@ unsafe fn inner_wprintf<W: Write>(
                     let c = match char::from_u32(c as _) {
                         Some(c) => c,
                         None => {
-                            platform::errno = EILSEQ;
+                            platform::errno.set(EILSEQ);
                             return Err(io::last_os_error());
                         }
                     };
diff --git a/src/io/error.rs b/src/io/error.rs
index 92f37deb..00fca803 100644
--- a/src/io/error.rs
+++ b/src/io/error.rs
@@ -170,7 +170,7 @@ impl Error {
     }
 
     pub fn last_os_error() -> Error {
-        let errno = unsafe { crate::platform::errno };
+        let errno = crate::platform::errno.get();
         Error::from_raw_os_error(errno)
     }
 }
diff --git a/src/ld_so/dso.rs b/src/ld_so/dso.rs
index 7dfb5482..d8e16b8d 100644
--- a/src/ld_so/dso.rs
+++ b/src/ld_so/dso.rs
@@ -229,7 +229,8 @@ impl DSO {
                 {
                     return Err(Error::Malformed(format!(
                         "failed to map {}. errno: {}",
-                        path, STR_ERROR[errno as usize]
+                        path,
+                        STR_ERROR[errno.get() as usize]
                     )));
                 }
                 if start as *mut c_void != ptr::null_mut() {
diff --git a/src/macros.rs b/src/macros.rs
index 01f7f346..a82c4a2b 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -145,7 +145,7 @@ macro_rules! strto_impl {
         };
 
         let invalid_input = || {
-            platform::errno = EINVAL;
+            platform::errno.set(EINVAL);
             set_endptr(0);
         };
 
@@ -203,7 +203,7 @@ macro_rules! strto_impl {
         // account for the sign
         let num = num as $rettype;
         let num = if overflow {
-            platform::errno = ERANGE;
+            platform::errno.set(ERANGE);
             if CHECK_SIGN {
                 if positive {
                     MAX_VAL
diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs
index 8792ea8f..b317ea05 100644
--- a/src/platform/linux/mod.rs
+++ b/src/platform/linux/mod.rs
@@ -63,9 +63,7 @@ pub fn e(sys: usize) -> usize {
     match e_raw(sys) {
         Ok(value) => value,
         Err(errcode) => {
-            unsafe {
-                errno = errcode as c_int;
-            }
+            errno.set(errcode as c_int);
             !0
         }
     }
diff --git a/src/platform/mod.rs b/src/platform/mod.rs
index 6554df75..3006997b 100644
--- a/src/platform/mod.rs
+++ b/src/platform/mod.rs
@@ -1,6 +1,6 @@
 use crate::io::{self, Read, Write};
 use alloc::{boxed::Box, vec::Vec};
-use core::{fmt, ptr};
+use core::{cell::Cell, fmt, ptr};
 
 pub use self::allocator::*;
 
@@ -38,7 +38,7 @@ pub mod types;
 #[thread_local]
 #[allow(non_upper_case_globals)]
 #[no_mangle]
-pub static mut errno: c_int = 0;
+pub static errno: Cell<c_int> = Cell::new(0);
 
 #[allow(non_upper_case_globals)]
 pub static mut argv: *mut *mut c_char = ptr::null_mut();
diff --git a/src/platform/redox/epoll.rs b/src/platform/redox/epoll.rs
index aca5ad1a..7ba86f11 100644
--- a/src/platform/redox/epoll.rs
+++ b/src/platform/redox/epoll.rs
@@ -47,7 +47,7 @@ impl PalEpoll for Sys {
                 ) as c_int
             }
             _ => {
-                unsafe { platform::errno = EINVAL };
+                platform::errno.set(EINVAL);
                 return -1;
             }
         }
@@ -64,7 +64,7 @@ impl PalEpoll for Sys {
         assert_eq!(mem::size_of::<epoll_event>(), mem::size_of::<Event>());
 
         if maxevents <= 0 {
-            unsafe { platform::errno = EINVAL };
+            platform::errno.set(EINVAL);
             return -1;
         }
 
diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs
index f3f5b113..5ebb2e80 100644
--- a/src/platform/redox/mod.rs
+++ b/src/platform/redox/mod.rs
@@ -54,9 +54,7 @@ macro_rules! path_from_c_str {
         match $c_str.to_str() {
             Ok(ok) => ok,
             Err(err) => {
-                unsafe {
-                    errno = EINVAL;
-                }
+                errno.set(EINVAL);
                 return -1;
             }
         }
@@ -69,9 +67,7 @@ pub fn e(sys: Result<usize>) -> usize {
     match sys {
         Ok(ok) => ok,
         Err(err) => {
-            unsafe {
-                errno = err.errno as c_int;
-            }
+            errno.set(err.errno as c_int);
             !0
         }
     }
@@ -116,9 +112,7 @@ impl Pal for Sys {
             || (mode & W_OK == W_OK && perms & 0o2 != 0o2)
             || (mode & X_OK == X_OK && perms & 0o1 != 0o1)
         {
-            unsafe {
-                errno = EINVAL;
-            }
+            errno.set(EINVAL);
             return -1;
         }
 
@@ -159,7 +153,7 @@ impl Pal for Sys {
                 addr
             } else {
                 // It was outside of valid range
-                errno = ENOMEM;
+                errno.set(ENOMEM);
                 ptr::null_mut()
             }
         }
@@ -188,7 +182,7 @@ impl Pal for Sys {
     fn clock_getres(clk_id: clockid_t, tp: *mut timespec) -> c_int {
         // TODO
         eprintln!("relibc clock_getres({}, {:p}): not implemented", clk_id, tp);
-        unsafe { errno = ENOSYS }
+        errno.set(ENOSYS);
         -1
     }
 
@@ -204,7 +198,7 @@ impl Pal for Sys {
             "relibc clock_settime({}, {:p}): not implemented",
             clk_id, tp
         );
-        unsafe { errno = ENOSYS };
+        errno.set(ENOSYS);
         -1
     }
 
@@ -252,7 +246,7 @@ impl Pal for Sys {
             match str::from_utf8(&buf[..res]) {
                 Ok(path) => e(path::chdir(path).map(|()| 0)) as c_int,
                 Err(_) => {
-                    unsafe { errno = EINVAL };
+                    errno.set(EINVAL);
                     return -1;
                 }
             }
@@ -343,16 +337,12 @@ impl Pal for Sys {
 
         let buf_slice = unsafe { slice::from_raw_parts_mut(buf as *mut u8, size as usize) };
         if buf_slice.is_empty() {
-            unsafe {
-                errno = EINVAL;
-            }
+            errno.set(EINVAL);
             return ptr::null_mut();
         }
 
         if path::getcwd(buf_slice).is_none() {
-            unsafe {
-                errno = ERANGE;
-            }
+            errno.set(ERANGE);
             return ptr::null_mut();
         }
 
@@ -467,7 +457,7 @@ impl Pal for Sys {
     unsafe fn getgroups(size: c_int, list: *mut gid_t) -> c_int {
         // TODO
         eprintln!("relibc getgroups({}, {:p}): not implemented", size, list);
-        unsafe { errno = ENOSYS };
+        errno.set(ENOSYS);
         -1
     }
 
@@ -490,7 +480,7 @@ impl Pal for Sys {
     fn getpriority(which: c_int, who: id_t) -> c_int {
         // TODO
         eprintln!("getpriority({}, {}): not implemented", which, who);
-        unsafe { errno = ENOSYS };
+        errno.set(ENOSYS);
         -1
     }
 
@@ -540,7 +530,7 @@ impl Pal for Sys {
             "relibc setrlimit({}, {:p}): not implemented",
             resource, rlim
         );
-        unsafe { errno = EPERM };
+        errno.set(EPERM);
         -1
     }
 
@@ -640,7 +630,7 @@ impl Pal for Sys {
         let dir_path = match str::from_utf8(&dir_path_buf[..res as usize]) {
             Ok(path) => path,
             Err(_) => {
-                unsafe { errno = EBADR };
+                errno.set(EBADR);
                 return !0;
             }
         };
@@ -651,7 +641,7 @@ impl Pal for Sys {
                 None => {
                     // Since parent_dir_path is resolved by fpath, it is more likely that
                     // the problem was with path.
-                    unsafe { errno = ENOENT };
+                    errno.set(ENOENT);
                     return !0;
                 }
             };
@@ -884,7 +874,7 @@ impl Pal for Sys {
     unsafe fn setgroups(size: size_t, list: *const gid_t) -> c_int {
         // TODO
         eprintln!("relibc setgroups({}, {:p}): not implemented", size, list);
-        unsafe { errno = ENOSYS };
+        errno.set(ENOSYS);
         -1
     }
 
@@ -898,7 +888,7 @@ impl Pal for Sys {
             "relibc setpriority({}, {}, {}): not implemented",
             which, who, prio
         );
-        unsafe { errno = ENOSYS };
+        errno.set(ENOSYS);
         -1
     }
 
@@ -1027,10 +1017,10 @@ impl Pal for Sys {
 
         match inner(utsname) {
             Ok(()) => 0,
-            Err(err) => unsafe {
-                errno = err;
+            Err(err) => {
+                errno.set(err);
                 -1
-            },
+            }
         }
     }
 
diff --git a/src/platform/redox/ptrace.rs b/src/platform/redox/ptrace.rs
index 7013afd0..0c9ba44d 100644
--- a/src/platform/redox/ptrace.rs
+++ b/src/platform/redox/ptrace.rs
@@ -91,9 +91,7 @@ pub fn get_session(
                     )?,
                 }))
             } else {
-                unsafe {
-                    errno = errnoh::ESRCH;
-                }
+                errno.set(errnoh::ESRCH);
                 Err(io::last_os_error())
             }
         }
diff --git a/src/platform/redox/signal.rs b/src/platform/redox/signal.rs
index da53f619..d60fd9ad 100644
--- a/src/platform/redox/signal.rs
+++ b/src/platform/redox/signal.rs
@@ -19,10 +19,10 @@ impl PalSignal for Sys {
     fn getitimer(which: c_int, out: *mut itimerval) -> c_int {
         let path = match which {
             ITIMER_REAL => "itimer:1",
-            _ => unsafe {
-                errno = EINVAL;
+            _ => {
+                errno.set(EINVAL);
                 return -1;
-            },
+            }
         };
 
         let fd = e(syscall::open(path, syscall::O_RDONLY | syscall::O_CLOEXEC));
@@ -64,10 +64,10 @@ impl PalSignal for Sys {
     fn setitimer(which: c_int, new: *const itimerval, old: *mut itimerval) -> c_int {
         let path = match which {
             ITIMER_REAL => "itimer:1",
-            _ => unsafe {
-                errno = EINVAL;
+            _ => {
+                errno.set(EINVAL);
                 return -1;
-            },
+            }
         };
 
         let fd = e(syscall::open(path, syscall::O_RDWR | syscall::O_CLOEXEC));
@@ -115,9 +115,7 @@ impl PalSignal for Sys {
     }
 
     fn sigpending(set: *mut sigset_t) -> c_int {
-        unsafe {
-            errno = ENOSYS;
-        }
+        errno.set(ENOSYS);
         -1
     }
 
@@ -126,16 +124,12 @@ impl PalSignal for Sys {
     }
 
     fn sigsuspend(set: *const sigset_t) -> c_int {
-        unsafe {
-            errno = ENOSYS;
-        }
+        errno.set(ENOSYS);
         -1
     }
 
     fn sigtimedwait(set: *const sigset_t, sig: *mut siginfo_t, tp: *const timespec) -> c_int {
-        unsafe {
-            errno = ENOSYS;
-        }
+        errno.set(ENOSYS);
         -1
     }
 }
diff --git a/src/platform/redox/socket.rs b/src/platform/redox/socket.rs
index fd5c460c..cb9577b0 100644
--- a/src/platform/redox/socket.rs
+++ b/src/platform/redox/socket.rs
@@ -34,14 +34,14 @@ macro_rules! bind_or_connect {
     }};
     ($mode:ident copy, $socket:expr, $address:expr, $address_len:expr) => {{
         if ($address_len as usize) < mem::size_of::<sa_family_t>() {
-            errno = syscall::EINVAL;
+            errno.set(syscall::EINVAL);
             return -1;
         }
 
         let path = match (*$address).sa_family as c_int {
             AF_INET => {
                 if ($address_len as usize) != mem::size_of::<sockaddr_in>() {
-                    errno = syscall::EINVAL;
+                    errno.set(syscall::EINVAL);
                     return -1;
                 }
                 let data = &*($address as *const sockaddr_in);
@@ -93,7 +93,7 @@ macro_rules! bind_or_connect {
                 path
             },
             _ => {
-                errno = syscall::EAFNOSUPPORT;
+                errno.set(syscall::EAFNOSUPPORT);
                 return -1;
             },
         };
@@ -295,7 +295,7 @@ impl PalSocket for Sys {
         address_len: *mut socklen_t,
     ) -> ssize_t {
         if flags != 0 {
-            errno = syscall::EOPNOTSUPP;
+            errno.set(syscall::EOPNOTSUPP);
             return -1;
         }
         if address == ptr::null_mut() || address_len == ptr::null_mut() {
@@ -319,14 +319,14 @@ impl PalSocket for Sys {
     unsafe fn recvmsg(socket: c_int, msg: *mut msghdr, flags: c_int) -> ssize_t {
         //TODO: implement recvfrom with recvmsg
         eprintln!("recvmsg not implemented on redox");
-        errno = syscall::ENOSYS;
+        errno.set(syscall::ENOSYS);
         return -1;
     }
 
     unsafe fn sendmsg(socket: c_int, msg: *const msghdr, flags: c_int) -> ssize_t {
         //TODO: implement sendto with sendmsg
         eprintln!("sendmsg not implemented on redox");
-        errno = syscall::ENOSYS;
+        errno.set(syscall::ENOSYS);
         return -1;
     }
 
@@ -339,7 +339,7 @@ impl PalSocket for Sys {
         dest_len: socklen_t,
     ) -> ssize_t {
         if flags != 0 {
-            errno = syscall::EOPNOTSUPP;
+            errno.set(syscall::EOPNOTSUPP);
             return -1;
         }
         if dest_addr == ptr::null() || dest_len == 0 {
@@ -414,11 +414,11 @@ impl PalSocket for Sys {
 
     unsafe fn socket(domain: c_int, kind: c_int, protocol: c_int) -> c_int {
         if domain != AF_INET && domain != AF_UNIX {
-            errno = syscall::EAFNOSUPPORT;
+            errno.set(syscall::EAFNOSUPPORT);
             return -1;
         }
         // if protocol != 0 {
-        //     errno = syscall::EPROTONOSUPPORT;
+        //     errno.set(syscall::EPROTONOSUPPORT);
         //     return -1;
         // }
 
@@ -431,7 +431,7 @@ impl PalSocket for Sys {
             (AF_INET, SOCK_DGRAM) => e(syscall::open("udp:", flags)) as c_int,
             (AF_UNIX, SOCK_STREAM) => e(syscall::open("chan:", flags | O_CREAT)) as c_int,
             _ => {
-                errno = syscall::EPROTONOSUPPORT;
+                errno.set(syscall::EPROTONOSUPPORT);
                 -1
             }
         }
@@ -476,7 +476,7 @@ impl PalSocket for Sys {
                     protocol,
                     sv.as_mut_ptr()
                 );
-                errno = syscall::EPROTONOSUPPORT;
+                errno.set(syscall::EPROTONOSUPPORT);
                 -1
             },
         }
-- 
GitLab