From ed5347278f396110e0e1c4131a07dfd8d02c5a98 Mon Sep 17 00:00:00 2001 From: Will Dietz Date: Mon, 24 Sep 2018 23:27:03 -0500 Subject: [PATCH 1/5] musl: whitespace, NFC --- pkgs/os-specific/linux/musl/default.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkgs/os-specific/linux/musl/default.nix b/pkgs/os-specific/linux/musl/default.nix index 376d824a043..b0758437da9 100644 --- a/pkgs/os-specific/linux/musl/default.nix +++ b/pkgs/os-specific/linux/musl/default.nix @@ -64,9 +64,9 @@ stdenv.mkDerivation rec { # Use execveat to impl fexecve when avail (useful for containers) ./fexecve-execveat.patch # improve behavior in few cases - ./0001-in-pthread_mutex_trylock-EBUSY-out-more-directly-whe.patch - ./0002-in-pthread_mutex_timedlock-avoid-repeatedly-reading-.patch - ./0003-fix-namespace-violation-for-c11-mutex-functions.patch + ./0001-in-pthread_mutex_trylock-EBUSY-out-more-directly-whe.patch + ./0002-in-pthread_mutex_timedlock-avoid-repeatedly-reading-.patch + ./0003-fix-namespace-violation-for-c11-mutex-functions.patch ]; preConfigure = '' configureFlagsArray+=("--syslibdir=$out/lib") From 5c217591fda32180f688207f78caf73daf65b4df Mon Sep 17 00:00:00 2001 From: Will Dietz Date: Mon, 24 Sep 2018 23:35:01 -0500 Subject: [PATCH 2/5] musl: pick upstream patches for name_to_handle_at support --- pkgs/os-specific/linux/musl/default.nix | 3 + .../max-handle-sz-for-name-to-handle-at.patch | 26 +++++++ .../linux/musl/name-to-handle-at.patch | 71 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 pkgs/os-specific/linux/musl/max-handle-sz-for-name-to-handle-at.patch create mode 100644 pkgs/os-specific/linux/musl/name-to-handle-at.patch diff --git a/pkgs/os-specific/linux/musl/default.nix b/pkgs/os-specific/linux/musl/default.nix index b0758437da9..d2dbfea63ab 100644 --- a/pkgs/os-specific/linux/musl/default.nix +++ b/pkgs/os-specific/linux/musl/default.nix @@ -67,6 +67,9 @@ stdenv.mkDerivation rec { ./0001-in-pthread_mutex_trylock-EBUSY-out-more-directly-whe.patch ./0002-in-pthread_mutex_timedlock-avoid-repeatedly-reading-.patch ./0003-fix-namespace-violation-for-c11-mutex-functions.patch + # name_to_handle_at + ./name-to-handle-at.patch + ./max-handle-sz-for-name-to-handle-at.patch ]; preConfigure = '' configureFlagsArray+=("--syslibdir=$out/lib") diff --git a/pkgs/os-specific/linux/musl/max-handle-sz-for-name-to-handle-at.patch b/pkgs/os-specific/linux/musl/max-handle-sz-for-name-to-handle-at.patch new file mode 100644 index 00000000000..aa00b4619f8 --- /dev/null +++ b/pkgs/os-specific/linux/musl/max-handle-sz-for-name-to-handle-at.patch @@ -0,0 +1,26 @@ +From 7d7f44253f2d8cfd0a7adf9f918d88aa24d4e012 Mon Sep 17 00:00:00 2001 +From: Khem Raj +Date: Thu, 13 Sep 2018 07:00:05 -0700 +Subject: [PATCH] define MAX_HANDLE_SZ for use with name_to_handle_at + +MAX_HANDLE_SZ is described in name_to_handle_at() to contain maximum +expected size for a file handle +--- + include/fcntl.h | 1 + + 1 file changed, 1 insertion(+) + +diff --git a/include/fcntl.h b/include/fcntl.h +index 99b21759..4d91338b 100644 +--- a/include/fcntl.h ++++ b/include/fcntl.h +@@ -166,6 +166,7 @@ struct f_owner_ex { + }; + #define FALLOC_FL_KEEP_SIZE 1 + #define FALLOC_FL_PUNCH_HOLE 2 ++#define MAX_HANDLE_SZ 128 + #define SYNC_FILE_RANGE_WAIT_BEFORE 1 + #define SYNC_FILE_RANGE_WRITE 2 + #define SYNC_FILE_RANGE_WAIT_AFTER 4 +-- +2.19.0 + diff --git a/pkgs/os-specific/linux/musl/name-to-handle-at.patch b/pkgs/os-specific/linux/musl/name-to-handle-at.patch new file mode 100644 index 00000000000..10cd8a9947c --- /dev/null +++ b/pkgs/os-specific/linux/musl/name-to-handle-at.patch @@ -0,0 +1,71 @@ +From 3e14bbcd1979376b188bfabb816ff828608fb5d7 Mon Sep 17 00:00:00 2001 +From: Khem Raj +Date: Wed, 12 Sep 2018 18:02:11 -0700 +Subject: [PATCH] wireup linux/name_to_handle_at and name_to_handle_at syscalls + +--- + include/fcntl.h | 7 +++++++ + src/linux/name_to_handle_at.c | 10 ++++++++++ + src/linux/open_by_handle_at.c | 8 ++++++++ + 3 files changed, 25 insertions(+) + create mode 100644 src/linux/name_to_handle_at.c + create mode 100644 src/linux/open_by_handle_at.c + +diff --git a/include/fcntl.h b/include/fcntl.h +index 6d8edcd1..99b21759 100644 +--- a/include/fcntl.h ++++ b/include/fcntl.h +@@ -155,6 +155,11 @@ int lockf(int, int, off_t); + #define F_OWNER_PID 1 + #define F_OWNER_PGRP 2 + #define F_OWNER_GID 2 ++struct file_handle { ++ unsigned handle_bytes; ++ int handle_type; ++ unsigned char f_handle[]; ++}; + struct f_owner_ex { + int type; + pid_t pid; +@@ -170,6 +175,8 @@ struct f_owner_ex { + #define SPLICE_F_GIFT 8 + int fallocate(int, int, off_t, off_t); + #define fallocate64 fallocate ++int name_to_handle_at(int, const char *, struct file_handle *, int *, int); ++int open_by_handle_at(int, struct file_handle *, int); + ssize_t readahead(int, off_t, size_t); + int sync_file_range(int, off_t, off_t, unsigned); + ssize_t vmsplice(int, const struct iovec *, size_t, unsigned); +diff --git a/src/linux/name_to_handle_at.c b/src/linux/name_to_handle_at.c +new file mode 100644 +index 00000000..cd4075bd +--- /dev/null ++++ b/src/linux/name_to_handle_at.c +@@ -0,0 +1,10 @@ ++#define _GNU_SOURCE ++#include ++#include "syscall.h" ++ ++int name_to_handle_at(int dirfd, const char *pathname, ++ struct file_handle *handle, int *mount_id, int flags) ++{ ++ return syscall(SYS_name_to_handle_at, dirfd, ++ pathname, handle, mount_id, flags); ++} +diff --git a/src/linux/open_by_handle_at.c b/src/linux/open_by_handle_at.c +new file mode 100644 +index 00000000..1c9b6a2b +--- /dev/null ++++ b/src/linux/open_by_handle_at.c +@@ -0,0 +1,8 @@ ++#define _GNU_SOURCE ++#include ++#include "syscall.h" ++ ++int open_by_handle_at(int mount_fd, struct file_handle *handle, int flags) ++{ ++ return syscall(SYS_open_by_handle_at, mount_fd, handle, flags); ++} +-- +2.19.0 + From dcd5e4558fb38c202c69f588e3a5fa99c73ea09b Mon Sep 17 00:00:00 2001 From: Will Dietz Date: Mon, 24 Sep 2018 23:37:44 -0500 Subject: [PATCH 3/5] musl: pick getaddrinfo fix (containers) --- pkgs/os-specific/linux/musl/default.nix | 2 + ...drinfo-regression-with-AI_ADDRCONFIG.patch | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 pkgs/os-specific/linux/musl/fix-getaddrinfo-regression-with-AI_ADDRCONFIG.patch diff --git a/pkgs/os-specific/linux/musl/default.nix b/pkgs/os-specific/linux/musl/default.nix index d2dbfea63ab..e28b6316486 100644 --- a/pkgs/os-specific/linux/musl/default.nix +++ b/pkgs/os-specific/linux/musl/default.nix @@ -67,6 +67,8 @@ stdenv.mkDerivation rec { ./0001-in-pthread_mutex_trylock-EBUSY-out-more-directly-whe.patch ./0002-in-pthread_mutex_timedlock-avoid-repeatedly-reading-.patch ./0003-fix-namespace-violation-for-c11-mutex-functions.patch + # Fix getaddrinfo usage encountered sometimes in containers + ./fix-getaddrinfo-regression-with-AI_ADDRCONFIG.patch # name_to_handle_at ./name-to-handle-at.patch ./max-handle-sz-for-name-to-handle-at.patch diff --git a/pkgs/os-specific/linux/musl/fix-getaddrinfo-regression-with-AI_ADDRCONFIG.patch b/pkgs/os-specific/linux/musl/fix-getaddrinfo-regression-with-AI_ADDRCONFIG.patch new file mode 100644 index 00000000000..d603c16f806 --- /dev/null +++ b/pkgs/os-specific/linux/musl/fix-getaddrinfo-regression-with-AI_ADDRCONFIG.patch @@ -0,0 +1,52 @@ +From f381c118b2d4f7d914481d3cdc830ce41369b002 Mon Sep 17 00:00:00 2001 +From: Rich Felker +Date: Wed, 19 Sep 2018 18:03:22 -0400 +Subject: [PATCH] fix getaddrinfo regression with AI_ADDRCONFIG on some + configurations + +despite not being documented to do so in the standard or Linux +documentation, attempts to udp connect to 127.0.0.1 or ::1 generate +EADDRNOTAVAIL when the loopback device is not configured and there is +no default route for IPv6. this caused getaddrinfo with AI_ADDRCONFIG +to fail with EAI_SYSTEM and EADDRNOTAVAIL on some no-IPv6 +configurations, rather than the intended behavior of detecting IPv6 as +unsuppported and producing IPv4-only results. + +previously, only EAFNOSUPPORT was treated as unavailability of the +address family being probed. instead, treat all errors related to +inability to get an address or route as conclusive that the family +being probed is unsupported, and only fail with EAI_SYSTEM on other +errors. + +further improvements may be desirable, such as reporting EAI_AGAIN +instead of EAI_SYSTEM for errors which are expected to be transient, +but this patch should suffice to fix the serious regression. +--- + src/network/getaddrinfo.c | 11 ++++++++++- + 1 file changed, 10 insertions(+), 1 deletion(-) + +diff --git a/src/network/getaddrinfo.c b/src/network/getaddrinfo.c +index ba26847a..e33bfa28 100644 +--- a/src/network/getaddrinfo.c ++++ b/src/network/getaddrinfo.c +@@ -76,7 +76,16 @@ int getaddrinfo(const char *restrict host, const char *restrict serv, const stru + close(s); + if (!r) continue; + } +- if (errno != EAFNOSUPPORT) return EAI_SYSTEM; ++ switch (errno) { ++ case EADDRNOTAVAIL: ++ case EAFNOSUPPORT: ++ case EHOSTUNREACH: ++ case ENETDOWN: ++ case ENETUNREACH: ++ break; ++ default: ++ return EAI_SYSTEM; ++ } + if (family == tf[i]) return EAI_NONAME; + family = tf[1-i]; + } +-- +2.19.0 + From f38218a7568296eb19eed65b745d310a33f43297 Mon Sep 17 00:00:00 2001 From: Will Dietz Date: Mon, 24 Sep 2018 23:40:08 -0500 Subject: [PATCH 4/5] musl: pick stacksize-related improvements, increase default size Also supports setting default thread stack size via linker, making it possible to fix programs without modifying source. --- pkgs/os-specific/linux/musl/default.nix | 5 ++ .../linux/musl/stacksize-0001.patch | 56 ++++++++++++ .../linux/musl/stacksize-0002.patch | 86 +++++++++++++++++++ .../linux/musl/stacksize-0003.patch | 36 ++++++++ .../linux/musl/stacksize-0004.patch | 81 +++++++++++++++++ 5 files changed, 264 insertions(+) create mode 100644 pkgs/os-specific/linux/musl/stacksize-0001.patch create mode 100644 pkgs/os-specific/linux/musl/stacksize-0002.patch create mode 100644 pkgs/os-specific/linux/musl/stacksize-0003.patch create mode 100644 pkgs/os-specific/linux/musl/stacksize-0004.patch diff --git a/pkgs/os-specific/linux/musl/default.nix b/pkgs/os-specific/linux/musl/default.nix index e28b6316486..ba634c5d161 100644 --- a/pkgs/os-specific/linux/musl/default.nix +++ b/pkgs/os-specific/linux/musl/default.nix @@ -72,6 +72,11 @@ stdenv.mkDerivation rec { # name_to_handle_at ./name-to-handle-at.patch ./max-handle-sz-for-name-to-handle-at.patch + # Upstream improvements regarding stack size, incl size increase + ./stacksize-0001.patch + ./stacksize-0002.patch + ./stacksize-0003.patch + ./stacksize-0004.patch ]; preConfigure = '' configureFlagsArray+=("--syslibdir=$out/lib") diff --git a/pkgs/os-specific/linux/musl/stacksize-0001.patch b/pkgs/os-specific/linux/musl/stacksize-0001.patch new file mode 100644 index 00000000000..5c4a6b3e9bd --- /dev/null +++ b/pkgs/os-specific/linux/musl/stacksize-0001.patch @@ -0,0 +1,56 @@ +From c7ed3e909a69d34a7821f7db644c2fa590a1a690 Mon Sep 17 00:00:00 2001 +From: Rich Felker +Date: Tue, 18 Sep 2018 19:43:52 -0400 +Subject: [PATCH 1/4] remove redundant declarations of __default_stacksize, + __default_guardsize + +these are now declared in pthread_impl.h. +--- + src/thread/pthread_attr_init.c | 3 --- + src/thread/pthread_create.c | 2 -- + src/thread/pthread_setattr_default_np.c | 3 --- + 3 files changed, 8 deletions(-) + +diff --git a/src/thread/pthread_attr_init.c b/src/thread/pthread_attr_init.c +index 398990d1..463a8d20 100644 +--- a/src/thread/pthread_attr_init.c ++++ b/src/thread/pthread_attr_init.c +@@ -1,8 +1,5 @@ + #include "pthread_impl.h" + +-extern size_t __default_stacksize; +-extern size_t __default_guardsize; +- + int pthread_attr_init(pthread_attr_t *a) + { + *a = (pthread_attr_t){0}; +diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c +index 3293dcd5..3da7db14 100644 +--- a/src/thread/pthread_create.c ++++ b/src/thread/pthread_create.c +@@ -162,8 +162,6 @@ static void *dummy_tsd[1] = { 0 }; + weak_alias(dummy_tsd, __pthread_tsd_main); + + volatile int __block_new_threads = 0; +-extern size_t __default_stacksize; +-extern size_t __default_guardsize; + + static FILE *volatile dummy_file = 0; + weak_alias(dummy_file, __stdin_used); +diff --git a/src/thread/pthread_setattr_default_np.c b/src/thread/pthread_setattr_default_np.c +index 88503e34..256f0685 100644 +--- a/src/thread/pthread_setattr_default_np.c ++++ b/src/thread/pthread_setattr_default_np.c +@@ -2,9 +2,6 @@ + #include "pthread_impl.h" + #include + +-extern size_t __default_stacksize; +-extern size_t __default_guardsize; +- + int pthread_setattr_default_np(const pthread_attr_t *attrp) + { + /* Reject anything in the attr object other than stack/guard size. */ +-- +2.19.0 + diff --git a/pkgs/os-specific/linux/musl/stacksize-0002.patch b/pkgs/os-specific/linux/musl/stacksize-0002.patch new file mode 100644 index 00000000000..1191452d669 --- /dev/null +++ b/pkgs/os-specific/linux/musl/stacksize-0002.patch @@ -0,0 +1,86 @@ +From 792f32772e64a32527cd455ebfa087ef434a6f4f Mon Sep 17 00:00:00 2001 +From: Rich Felker +Date: Tue, 18 Sep 2018 23:06:50 -0400 +Subject: [PATCH 2/4] limit the configurable default stack/guard size for + threads + +limit to 8MB/1MB, repectively. since the defaults cannot be reduced +once increased, excessively large settings would lead to an +unrecoverably broken state. this change is in preparation to allow +defaults to be increased via program headers at the linker level. + +creation of threads that really need larger sizes needs to be done +with an explicit attribute. +--- + src/internal/pthread_impl.h | 7 +++++-- + src/thread/default_attr.c | 4 ++-- + src/thread/pthread_setattr_default_np.c | 12 ++++++++---- + 3 files changed, 15 insertions(+), 8 deletions(-) + +diff --git a/src/internal/pthread_impl.h b/src/internal/pthread_impl.h +index 26e6e1df..e73a251f 100644 +--- a/src/internal/pthread_impl.h ++++ b/src/internal/pthread_impl.h +@@ -182,12 +182,15 @@ hidden void __acquire_ptc(void); + hidden void __release_ptc(void); + hidden void __inhibit_ptc(void); + +-extern hidden size_t __default_stacksize; +-extern hidden size_t __default_guardsize; ++extern hidden unsigned __default_stacksize; ++extern hidden unsigned __default_guardsize; + + #define DEFAULT_STACK_SIZE 81920 + #define DEFAULT_GUARD_SIZE 4096 + ++#define DEFAULT_STACK_MAX (8<<20) ++#define DEFAULT_GUARD_MAX (1<<20) ++ + #define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1) + + #endif +diff --git a/src/thread/default_attr.c b/src/thread/default_attr.c +index 46fe98ee..dce96409 100644 +--- a/src/thread/default_attr.c ++++ b/src/thread/default_attr.c +@@ -1,4 +1,4 @@ + #include "pthread_impl.h" + +-size_t __default_stacksize = DEFAULT_STACK_SIZE; +-size_t __default_guardsize = DEFAULT_GUARD_SIZE; ++unsigned __default_stacksize = DEFAULT_STACK_SIZE; ++unsigned __default_guardsize = DEFAULT_GUARD_SIZE; +diff --git a/src/thread/pthread_setattr_default_np.c b/src/thread/pthread_setattr_default_np.c +index 256f0685..58486220 100644 +--- a/src/thread/pthread_setattr_default_np.c ++++ b/src/thread/pthread_setattr_default_np.c +@@ -2,6 +2,9 @@ + #include "pthread_impl.h" + #include + ++#define MIN(a,b) ((a)<(b) ? (a) : (b)) ++#define MAX(a,b) ((a)>(b) ? (a) : (b)) ++ + int pthread_setattr_default_np(const pthread_attr_t *attrp) + { + /* Reject anything in the attr object other than stack/guard size. */ +@@ -11,11 +14,12 @@ int pthread_setattr_default_np(const pthread_attr_t *attrp) + if (memcmp(&tmp, &zero, sizeof tmp)) + return EINVAL; + ++ unsigned stack = MIN(attrp->_a_stacksize, DEFAULT_STACK_MAX); ++ unsigned guard = MIN(attrp->_a_guardsize, DEFAULT_GUARD_MAX); ++ + __inhibit_ptc(); +- if (attrp->_a_stacksize >= __default_stacksize) +- __default_stacksize = attrp->_a_stacksize; +- if (attrp->_a_guardsize >= __default_guardsize) +- __default_guardsize = attrp->_a_guardsize; ++ __default_stacksize = MAX(__default_stacksize, stack); ++ __default_guardsize = MAX(__default_guardsize, guard); + __release_ptc(); + + return 0; +-- +2.19.0 + diff --git a/pkgs/os-specific/linux/musl/stacksize-0003.patch b/pkgs/os-specific/linux/musl/stacksize-0003.patch new file mode 100644 index 00000000000..fb5373005cb --- /dev/null +++ b/pkgs/os-specific/linux/musl/stacksize-0003.patch @@ -0,0 +1,36 @@ +From c0058ab465e950c2c3302d2b62e21cc0b494224b Mon Sep 17 00:00:00 2001 +From: Rich Felker +Date: Tue, 18 Sep 2018 23:11:49 -0400 +Subject: [PATCH 3/4] increase default thread stack/guard size + +stack size default is increased from 80k to 128k. this coincides with +Linux's hard-coded default stack for the main thread (128k is +initially committed; growth beyond that up to ulimit is contingent on +additional allocation succeeding) and GNU ld's default PT_GNU_STACK +size for FDPIC, at least on sh. + +guard size default is increased from 4k to 8k to reduce the risk of +guard page jumping on overflow, since use of just over 4k of stack is +common (PATH_MAX buffers, etc.). +--- + src/internal/pthread_impl.h | 4 ++-- + 1 file changed, 2 insertions(+), 2 deletions(-) + +diff --git a/src/internal/pthread_impl.h b/src/internal/pthread_impl.h +index e73a251f..d491f975 100644 +--- a/src/internal/pthread_impl.h ++++ b/src/internal/pthread_impl.h +@@ -185,8 +185,8 @@ hidden void __inhibit_ptc(void); + extern hidden unsigned __default_stacksize; + extern hidden unsigned __default_guardsize; + +-#define DEFAULT_STACK_SIZE 81920 +-#define DEFAULT_GUARD_SIZE 4096 ++#define DEFAULT_STACK_SIZE 131072 ++#define DEFAULT_GUARD_SIZE 8192 + + #define DEFAULT_STACK_MAX (8<<20) + #define DEFAULT_GUARD_MAX (1<<20) +-- +2.19.0 + diff --git a/pkgs/os-specific/linux/musl/stacksize-0004.patch b/pkgs/os-specific/linux/musl/stacksize-0004.patch new file mode 100644 index 00000000000..7a6f9792de5 --- /dev/null +++ b/pkgs/os-specific/linux/musl/stacksize-0004.patch @@ -0,0 +1,81 @@ +From 7b3348a98c139b4b4238384e52d4b0eb237e4833 Mon Sep 17 00:00:00 2001 +From: Rich Felker +Date: Tue, 18 Sep 2018 23:54:18 -0400 +Subject: [PATCH 4/4] support setting of default thread stack size via + PT_GNU_STACK header + +this facilitates building software that assumes a large default stack +size without any patching to call pthread_setattr_default_np or +pthread_attr_setstacksize at each thread creation site, using just +LDFLAGS. + +normally the PT_GNU_STACK header is used only to reflect whether +executable stack is desired, but with GNU ld at least, passing +-Wl,-z,stack-size=N will set a size on the program header. with this +patch, that size will be incorporated into the default stack size +(subject to increase-only rule and DEFAULT_STACK_MAX limit). + +both static and dynamic linking honor the program header. for dynamic +linking, all libraries loaded at program start, including preloaded +ones, are considered. dlopened libraries are not considered, for +several reasons. extra logic would be needed to defer processing until +the load of the new library is commited, synchronization woud be +needed since other threads may be running concurrently, and the +effectiveness woud be limited since the larger size would not apply to +threads that already existed at the time of dlopen. programs that will +dlopen code expecting a large stack need to declare the requirement +themselves, or pthread_setattr_default_np can be used. +--- + ldso/dynlink.c | 12 ++++++++++++ + src/env/__init_tls.c | 5 +++++ + 2 files changed, 17 insertions(+) + +diff --git a/ldso/dynlink.c b/ldso/dynlink.c +index e4829c3a..3ecbddfa 100644 +--- a/ldso/dynlink.c ++++ b/ldso/dynlink.c +@@ -609,6 +609,12 @@ static void *map_library(int fd, struct dso *dso) + } else if (ph->p_type == PT_GNU_RELRO) { + dso->relro_start = ph->p_vaddr & -PAGE_SIZE; + dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; ++ } else if (ph->p_type == PT_GNU_STACK) { ++ if (!runtime && ph->p_memsz > __default_stacksize) { ++ __default_stacksize = ++ ph->p_memsz < DEFAULT_STACK_MAX ? ++ ph->p_memsz : DEFAULT_STACK_MAX; ++ } + } + if (ph->p_type != PT_LOAD) continue; + nsegs++; +@@ -1238,6 +1244,12 @@ static void kernel_mapped_dso(struct dso *p) + } else if (ph->p_type == PT_GNU_RELRO) { + p->relro_start = ph->p_vaddr & -PAGE_SIZE; + p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; ++ } else if (ph->p_type == PT_GNU_STACK) { ++ if (!runtime && ph->p_memsz > __default_stacksize) { ++ __default_stacksize = ++ ph->p_memsz < DEFAULT_STACK_MAX ? ++ ph->p_memsz : DEFAULT_STACK_MAX; ++ } + } + if (ph->p_type != PT_LOAD) continue; + if (ph->p_vaddr < min_addr) +diff --git a/src/env/__init_tls.c b/src/env/__init_tls.c +index e0224243..96d0e284 100644 +--- a/src/env/__init_tls.c ++++ b/src/env/__init_tls.c +@@ -90,6 +90,11 @@ static void static_init_tls(size_t *aux) + base = (size_t)_DYNAMIC - phdr->p_vaddr; + if (phdr->p_type == PT_TLS) + tls_phdr = phdr; ++ if (phdr->p_type == PT_GNU_STACK && ++ phdr->p_memsz > __default_stacksize) ++ __default_stacksize = ++ phdr->p_memsz < DEFAULT_STACK_MAX ? ++ phdr->p_memsz : DEFAULT_STACK_MAX; + } + + if (tls_phdr) { +-- +2.19.0 + From d3a0aa22da1f3583c80338e21a7b42a78032e7ab Mon Sep 17 00:00:00 2001 From: Will Dietz Date: Mon, 24 Sep 2018 23:54:59 -0500 Subject: [PATCH 5/5] musl: reduce stacksize patch series to just the bump The others rely on some of the recent restructuring+cleanup in musl, which hopefully reaches us soon :). For now just bump the default sizes. --- pkgs/os-specific/linux/musl/default.nix | 7 +- .../linux/musl/stacksize-0001.patch | 56 ------------ .../linux/musl/stacksize-0002.patch | 86 ------------------- .../linux/musl/stacksize-0004.patch | 81 ----------------- ...cksize-0003.patch => stacksize-bump.patch} | 0 5 files changed, 2 insertions(+), 228 deletions(-) delete mode 100644 pkgs/os-specific/linux/musl/stacksize-0001.patch delete mode 100644 pkgs/os-specific/linux/musl/stacksize-0002.patch delete mode 100644 pkgs/os-specific/linux/musl/stacksize-0004.patch rename pkgs/os-specific/linux/musl/{stacksize-0003.patch => stacksize-bump.patch} (100%) diff --git a/pkgs/os-specific/linux/musl/default.nix b/pkgs/os-specific/linux/musl/default.nix index ba634c5d161..6137a5942c2 100644 --- a/pkgs/os-specific/linux/musl/default.nix +++ b/pkgs/os-specific/linux/musl/default.nix @@ -72,11 +72,8 @@ stdenv.mkDerivation rec { # name_to_handle_at ./name-to-handle-at.patch ./max-handle-sz-for-name-to-handle-at.patch - # Upstream improvements regarding stack size, incl size increase - ./stacksize-0001.patch - ./stacksize-0002.patch - ./stacksize-0003.patch - ./stacksize-0004.patch + # stacksize bump (upstream) + ./stacksize-bump.patch ]; preConfigure = '' configureFlagsArray+=("--syslibdir=$out/lib") diff --git a/pkgs/os-specific/linux/musl/stacksize-0001.patch b/pkgs/os-specific/linux/musl/stacksize-0001.patch deleted file mode 100644 index 5c4a6b3e9bd..00000000000 --- a/pkgs/os-specific/linux/musl/stacksize-0001.patch +++ /dev/null @@ -1,56 +0,0 @@ -From c7ed3e909a69d34a7821f7db644c2fa590a1a690 Mon Sep 17 00:00:00 2001 -From: Rich Felker -Date: Tue, 18 Sep 2018 19:43:52 -0400 -Subject: [PATCH 1/4] remove redundant declarations of __default_stacksize, - __default_guardsize - -these are now declared in pthread_impl.h. ---- - src/thread/pthread_attr_init.c | 3 --- - src/thread/pthread_create.c | 2 -- - src/thread/pthread_setattr_default_np.c | 3 --- - 3 files changed, 8 deletions(-) - -diff --git a/src/thread/pthread_attr_init.c b/src/thread/pthread_attr_init.c -index 398990d1..463a8d20 100644 ---- a/src/thread/pthread_attr_init.c -+++ b/src/thread/pthread_attr_init.c -@@ -1,8 +1,5 @@ - #include "pthread_impl.h" - --extern size_t __default_stacksize; --extern size_t __default_guardsize; -- - int pthread_attr_init(pthread_attr_t *a) - { - *a = (pthread_attr_t){0}; -diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c -index 3293dcd5..3da7db14 100644 ---- a/src/thread/pthread_create.c -+++ b/src/thread/pthread_create.c -@@ -162,8 +162,6 @@ static void *dummy_tsd[1] = { 0 }; - weak_alias(dummy_tsd, __pthread_tsd_main); - - volatile int __block_new_threads = 0; --extern size_t __default_stacksize; --extern size_t __default_guardsize; - - static FILE *volatile dummy_file = 0; - weak_alias(dummy_file, __stdin_used); -diff --git a/src/thread/pthread_setattr_default_np.c b/src/thread/pthread_setattr_default_np.c -index 88503e34..256f0685 100644 ---- a/src/thread/pthread_setattr_default_np.c -+++ b/src/thread/pthread_setattr_default_np.c -@@ -2,9 +2,6 @@ - #include "pthread_impl.h" - #include - --extern size_t __default_stacksize; --extern size_t __default_guardsize; -- - int pthread_setattr_default_np(const pthread_attr_t *attrp) - { - /* Reject anything in the attr object other than stack/guard size. */ --- -2.19.0 - diff --git a/pkgs/os-specific/linux/musl/stacksize-0002.patch b/pkgs/os-specific/linux/musl/stacksize-0002.patch deleted file mode 100644 index 1191452d669..00000000000 --- a/pkgs/os-specific/linux/musl/stacksize-0002.patch +++ /dev/null @@ -1,86 +0,0 @@ -From 792f32772e64a32527cd455ebfa087ef434a6f4f Mon Sep 17 00:00:00 2001 -From: Rich Felker -Date: Tue, 18 Sep 2018 23:06:50 -0400 -Subject: [PATCH 2/4] limit the configurable default stack/guard size for - threads - -limit to 8MB/1MB, repectively. since the defaults cannot be reduced -once increased, excessively large settings would lead to an -unrecoverably broken state. this change is in preparation to allow -defaults to be increased via program headers at the linker level. - -creation of threads that really need larger sizes needs to be done -with an explicit attribute. ---- - src/internal/pthread_impl.h | 7 +++++-- - src/thread/default_attr.c | 4 ++-- - src/thread/pthread_setattr_default_np.c | 12 ++++++++---- - 3 files changed, 15 insertions(+), 8 deletions(-) - -diff --git a/src/internal/pthread_impl.h b/src/internal/pthread_impl.h -index 26e6e1df..e73a251f 100644 ---- a/src/internal/pthread_impl.h -+++ b/src/internal/pthread_impl.h -@@ -182,12 +182,15 @@ hidden void __acquire_ptc(void); - hidden void __release_ptc(void); - hidden void __inhibit_ptc(void); - --extern hidden size_t __default_stacksize; --extern hidden size_t __default_guardsize; -+extern hidden unsigned __default_stacksize; -+extern hidden unsigned __default_guardsize; - - #define DEFAULT_STACK_SIZE 81920 - #define DEFAULT_GUARD_SIZE 4096 - -+#define DEFAULT_STACK_MAX (8<<20) -+#define DEFAULT_GUARD_MAX (1<<20) -+ - #define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1) - - #endif -diff --git a/src/thread/default_attr.c b/src/thread/default_attr.c -index 46fe98ee..dce96409 100644 ---- a/src/thread/default_attr.c -+++ b/src/thread/default_attr.c -@@ -1,4 +1,4 @@ - #include "pthread_impl.h" - --size_t __default_stacksize = DEFAULT_STACK_SIZE; --size_t __default_guardsize = DEFAULT_GUARD_SIZE; -+unsigned __default_stacksize = DEFAULT_STACK_SIZE; -+unsigned __default_guardsize = DEFAULT_GUARD_SIZE; -diff --git a/src/thread/pthread_setattr_default_np.c b/src/thread/pthread_setattr_default_np.c -index 256f0685..58486220 100644 ---- a/src/thread/pthread_setattr_default_np.c -+++ b/src/thread/pthread_setattr_default_np.c -@@ -2,6 +2,9 @@ - #include "pthread_impl.h" - #include - -+#define MIN(a,b) ((a)<(b) ? (a) : (b)) -+#define MAX(a,b) ((a)>(b) ? (a) : (b)) -+ - int pthread_setattr_default_np(const pthread_attr_t *attrp) - { - /* Reject anything in the attr object other than stack/guard size. */ -@@ -11,11 +14,12 @@ int pthread_setattr_default_np(const pthread_attr_t *attrp) - if (memcmp(&tmp, &zero, sizeof tmp)) - return EINVAL; - -+ unsigned stack = MIN(attrp->_a_stacksize, DEFAULT_STACK_MAX); -+ unsigned guard = MIN(attrp->_a_guardsize, DEFAULT_GUARD_MAX); -+ - __inhibit_ptc(); -- if (attrp->_a_stacksize >= __default_stacksize) -- __default_stacksize = attrp->_a_stacksize; -- if (attrp->_a_guardsize >= __default_guardsize) -- __default_guardsize = attrp->_a_guardsize; -+ __default_stacksize = MAX(__default_stacksize, stack); -+ __default_guardsize = MAX(__default_guardsize, guard); - __release_ptc(); - - return 0; --- -2.19.0 - diff --git a/pkgs/os-specific/linux/musl/stacksize-0004.patch b/pkgs/os-specific/linux/musl/stacksize-0004.patch deleted file mode 100644 index 7a6f9792de5..00000000000 --- a/pkgs/os-specific/linux/musl/stacksize-0004.patch +++ /dev/null @@ -1,81 +0,0 @@ -From 7b3348a98c139b4b4238384e52d4b0eb237e4833 Mon Sep 17 00:00:00 2001 -From: Rich Felker -Date: Tue, 18 Sep 2018 23:54:18 -0400 -Subject: [PATCH 4/4] support setting of default thread stack size via - PT_GNU_STACK header - -this facilitates building software that assumes a large default stack -size without any patching to call pthread_setattr_default_np or -pthread_attr_setstacksize at each thread creation site, using just -LDFLAGS. - -normally the PT_GNU_STACK header is used only to reflect whether -executable stack is desired, but with GNU ld at least, passing --Wl,-z,stack-size=N will set a size on the program header. with this -patch, that size will be incorporated into the default stack size -(subject to increase-only rule and DEFAULT_STACK_MAX limit). - -both static and dynamic linking honor the program header. for dynamic -linking, all libraries loaded at program start, including preloaded -ones, are considered. dlopened libraries are not considered, for -several reasons. extra logic would be needed to defer processing until -the load of the new library is commited, synchronization woud be -needed since other threads may be running concurrently, and the -effectiveness woud be limited since the larger size would not apply to -threads that already existed at the time of dlopen. programs that will -dlopen code expecting a large stack need to declare the requirement -themselves, or pthread_setattr_default_np can be used. ---- - ldso/dynlink.c | 12 ++++++++++++ - src/env/__init_tls.c | 5 +++++ - 2 files changed, 17 insertions(+) - -diff --git a/ldso/dynlink.c b/ldso/dynlink.c -index e4829c3a..3ecbddfa 100644 ---- a/ldso/dynlink.c -+++ b/ldso/dynlink.c -@@ -609,6 +609,12 @@ static void *map_library(int fd, struct dso *dso) - } else if (ph->p_type == PT_GNU_RELRO) { - dso->relro_start = ph->p_vaddr & -PAGE_SIZE; - dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; -+ } else if (ph->p_type == PT_GNU_STACK) { -+ if (!runtime && ph->p_memsz > __default_stacksize) { -+ __default_stacksize = -+ ph->p_memsz < DEFAULT_STACK_MAX ? -+ ph->p_memsz : DEFAULT_STACK_MAX; -+ } - } - if (ph->p_type != PT_LOAD) continue; - nsegs++; -@@ -1238,6 +1244,12 @@ static void kernel_mapped_dso(struct dso *p) - } else if (ph->p_type == PT_GNU_RELRO) { - p->relro_start = ph->p_vaddr & -PAGE_SIZE; - p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; -+ } else if (ph->p_type == PT_GNU_STACK) { -+ if (!runtime && ph->p_memsz > __default_stacksize) { -+ __default_stacksize = -+ ph->p_memsz < DEFAULT_STACK_MAX ? -+ ph->p_memsz : DEFAULT_STACK_MAX; -+ } - } - if (ph->p_type != PT_LOAD) continue; - if (ph->p_vaddr < min_addr) -diff --git a/src/env/__init_tls.c b/src/env/__init_tls.c -index e0224243..96d0e284 100644 ---- a/src/env/__init_tls.c -+++ b/src/env/__init_tls.c -@@ -90,6 +90,11 @@ static void static_init_tls(size_t *aux) - base = (size_t)_DYNAMIC - phdr->p_vaddr; - if (phdr->p_type == PT_TLS) - tls_phdr = phdr; -+ if (phdr->p_type == PT_GNU_STACK && -+ phdr->p_memsz > __default_stacksize) -+ __default_stacksize = -+ phdr->p_memsz < DEFAULT_STACK_MAX ? -+ phdr->p_memsz : DEFAULT_STACK_MAX; - } - - if (tls_phdr) { --- -2.19.0 - diff --git a/pkgs/os-specific/linux/musl/stacksize-0003.patch b/pkgs/os-specific/linux/musl/stacksize-bump.patch similarity index 100% rename from pkgs/os-specific/linux/musl/stacksize-0003.patch rename to pkgs/os-specific/linux/musl/stacksize-bump.patch