about summary refs log tree commit diff
path: root/src/libstd/sys/cloudabi
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-04-03 20:56:05 +0000
committerbors <bors@rust-lang.org>2020-04-03 20:56:05 +0000
commit74bd074eefcf4915c73d1ab91bc90859664729e6 (patch)
tree4eb869837e228d81b08b2fb4db3ad1af00942039 /src/libstd/sys/cloudabi
parentf6fe99c798cb65280a9a56f442b371adcb7b8aa2 (diff)
parent4c41ea36cda77748b532cf6d989a8d5d2fcc872e (diff)
downloadrust-74bd074eefcf4915c73d1ab91bc90859664729e6.tar.gz
rust-74bd074eefcf4915c73d1ab91bc90859664729e6.zip
Auto merge of #70747 - Centril:rollup-2vx9bve, r=Centril
Rollup of 9 pull requests

Successful merges:

 - #69860 (Use associated numeric consts in documentation)
 - #70576 (Update the description of the ticket to point at RFC 1721)
 - #70597 (Fix double-free and undefined behaviour in libstd::syn::unix::Thread::new)
 - #70640 (Hide `task_context` when lowering body)
 - #70641 (Remove duplicated code in trait selection)
 - #70707 (Remove unused graphviz emitter)
 - #70720 (Place TLS initializers with relocations in .tdata)
 - #70735 (Clean up E0502 explanation)
 - #70741 (Add test for #59023)

Failed merges:

r? @ghost
Diffstat (limited to 'src/libstd/sys/cloudabi')
-rw-r--r--src/libstd/sys/cloudabi/stack_overflow.rs8
-rw-r--r--src/libstd/sys/cloudabi/thread.rs15
2 files changed, 10 insertions, 13 deletions
diff --git a/src/libstd/sys/cloudabi/stack_overflow.rs b/src/libstd/sys/cloudabi/stack_overflow.rs
index e97831b2c28..9339b143731 100644
--- a/src/libstd/sys/cloudabi/stack_overflow.rs
+++ b/src/libstd/sys/cloudabi/stack_overflow.rs
@@ -1,13 +1,5 @@
 #![cfg_attr(test, allow(dead_code))]
 
-pub struct Handler;
-
-impl Handler {
-    pub unsafe fn new() -> Handler {
-        Handler
-    }
-}
-
 pub unsafe fn init() {}
 
 pub unsafe fn cleanup() {}
diff --git a/src/libstd/sys/cloudabi/thread.rs b/src/libstd/sys/cloudabi/thread.rs
index 3afcae7ae75..a15dc8653e8 100644
--- a/src/libstd/sys/cloudabi/thread.rs
+++ b/src/libstd/sys/cloudabi/thread.rs
@@ -5,7 +5,6 @@ use crate::mem;
 use crate::ptr;
 use crate::sys::cloudabi::abi;
 use crate::sys::time::checked_dur2intervals;
-use crate::sys_common::thread::*;
 use crate::time::Duration;
 
 pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
@@ -22,7 +21,7 @@ unsafe impl Sync for Thread {}
 impl Thread {
     // unsafe: see thread::Builder::spawn_unchecked for safety requirements
     pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
-        let p = box p;
+        let p = Box::into_raw(box p);
         let mut native: libc::pthread_t = mem::zeroed();
         let mut attr: libc::pthread_attr_t = mem::zeroed();
         assert_eq!(libc::pthread_attr_init(&mut attr), 0);
@@ -30,19 +29,25 @@ impl Thread {
         let stack_size = cmp::max(stack, min_stack_size(&attr));
         assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0);
 
-        let ret = libc::pthread_create(&mut native, &attr, thread_start, &*p as *const _ as *mut _);
+        let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
+        // Note: if the thread creation fails and this assert fails, then p will
+        // be leaked. However, an alternative design could cause double-free
+        // which is clearly worse.
         assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
 
         return if ret != 0 {
+            // The thread failed to start and as a result p was not consumed. Therefore, it is
+            // safe to reconstruct the box so that it gets deallocated.
+            drop(Box::from_raw(p));
             Err(io::Error::from_raw_os_error(ret))
         } else {
-            mem::forget(p); // ownership passed to pthread_create
             Ok(Thread { id: native })
         };
 
         extern "C" fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
             unsafe {
-                start_thread(main as *mut u8);
+                // Let's run some code.
+                Box::from_raw(main as *mut Box<dyn FnOnce()>)();
             }
             ptr::null_mut()
         }