fix and future-proof against stack overflow in aio io threads
[musl] / src / aio / aio.c
index aafd8e8..38943bc 100644 (file)
@@ -5,9 +5,9 @@
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <sys/auxv.h>
 #include "syscall.h"
 #include "atomic.h"
-#include "libc.h"
 #include "pthread_impl.h"
 
 /* The following is a threads-based implementation of AIO with minimal
@@ -257,6 +257,15 @@ static void *io_thread_func(void *ctx)
        return 0;
 }
 
+static size_t io_thread_stack_size = MINSIGSTKSZ+2048;
+static pthread_once_t init_stack_size_once;
+
+static void init_stack_size()
+{
+       unsigned long val = __getauxval(AT_MINSIGSTKSZ);
+       if (val > MINSIGSTKSZ) io_thread_stack_size = val + 512;
+}
+
 static int submit(struct aiocb *cb, int op)
 {
        int ret = 0;
@@ -272,8 +281,9 @@ static int submit(struct aiocb *cb, int op)
                else
                        pthread_attr_init(&a);
        } else {
+               pthread_once(&init_stack_size_once, init_stack_size);
                pthread_attr_init(&a);
-               pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN);
+               pthread_attr_setstacksize(&a, io_thread_stack_size);
                pthread_attr_setguardsize(&a, 0);
        }
        pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
@@ -371,9 +381,9 @@ int __aio_close(int fd)
        return fd;
 }
 
-LFS64(aio_cancel);
-LFS64(aio_error);
-LFS64(aio_fsync);
-LFS64(aio_read);
-LFS64(aio_write);
-LFS64(aio_return);
+weak_alias(aio_cancel, aio_cancel64);
+weak_alias(aio_error, aio_error64);
+weak_alias(aio_fsync, aio_fsync64);
+weak_alias(aio_read, aio_read64);
+weak_alias(aio_write, aio_write64);
+weak_alias(aio_return, aio_return64);