00db10
diff -pruN glibc-2.17-c758a686/csu/libc-start.c glibc-2.17-c758a686/csu/libc-start.c
00db10
--- glibc-2.17-c758a686/csu/libc-start.c	2012-12-25 08:32:13.000000000 +0530
00db10
+++ glibc-2.17-c758a686/csu/libc-start.c	2013-07-30 11:46:44.571901690 +0530
00db10
@@ -32,7 +32,7 @@ extern int __libc_multiple_libcs;
00db10
 #include <tls.h>
00db10
 #ifndef SHARED
00db10
 # include <dl-osinfo.h>
00db10
-extern void __pthread_initialize_minimal (void);
00db10
+extern void __pthread_initialize_minimal (int, char **, char **);
00db10
 # ifndef THREAD_SET_STACK_GUARD
00db10
 /* Only exported for architectures that don't store the stack guard canary
00db10
    in thread local area.  */
00db10
@@ -175,7 +175,7 @@ LIBC_START_MAIN (int (*main) (int, char
00db10
   /* Initialize the thread library at least a bit since the libgcc
00db10
      functions are using thread functions if these are available and
00db10
      we need to setup errno.  */
00db10
-  __pthread_initialize_minimal ();
00db10
+  __pthread_initialize_minimal (argc, argv, __environ);
00db10
 
00db10
   /* Set up the stack checker's canary.  */
00db10
   uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
00db10
diff -pruN glibc-2.17-c758a686/csu/libc-tls.c glibc-2.17-c758a686/csu/libc-tls.c
00db10
--- glibc-2.17-c758a686/csu/libc-tls.c	2012-12-25 08:32:13.000000000 +0530
00db10
+++ glibc-2.17-c758a686/csu/libc-tls.c	2013-07-30 11:46:44.572901690 +0530
00db10
@@ -243,7 +243,7 @@ _dl_tls_setup (void)
00db10
    not used.  */
00db10
 void
00db10
 __attribute__ ((weak))
00db10
-__pthread_initialize_minimal (void)
00db10
+__pthread_initialize_minimal (int argc, char **argv, char **envp)
00db10
 {
00db10
   __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
00db10
 }
00db10
diff -pruN glibc-2.17-c758a686/nptl/Makefile glibc-2.17-c758a686/nptl/Makefile
00db10
--- glibc-2.17-c758a686/nptl/Makefile	2013-07-30 11:46:34.909902026 +0530
00db10
+++ glibc-2.17-c758a686/nptl/Makefile	2013-07-30 11:46:44.573901690 +0530
00db10
@@ -201,7 +201,7 @@ CFLAGS-pt-system.c = -fexceptions
00db10
 
00db10
 
00db10
 tests = tst-typesizes \
00db10
-	tst-attr1 tst-attr2 tst-attr3 \
00db10
+	tst-attr1 tst-attr2 tst-attr3 tst-default-attr \
00db10
 	tst-mutex1 tst-mutex2 tst-mutex3 tst-mutex4 tst-mutex5 tst-mutex6 \
00db10
 	tst-mutex7 tst-mutex8 tst-mutex9 tst-mutex5a tst-mutex7a \
00db10
 	tst-mutexpi1 tst-mutexpi2 tst-mutexpi3 tst-mutexpi4 tst-mutexpi5 \
00db10
@@ -276,6 +276,13 @@ gen-as-const-headers = pthread-errnos.sy
00db10
 
00db10
 LDFLAGS-pthread.so = -Wl,--enable-new-dtags,-z,nodelete,-z,initfirst
00db10
 
00db10
+# The size is 1MB + 4KB.  The extra 4KB has been added to prevent allocatestack
00db10
+# from resizing the input size to avoid the 64K aliasing conflict on Intel
00db10
+# processors.
00db10
+DEFAULT_STACKSIZE=1052672
00db10
+CFLAGS-tst-default-attr.c = -DDEFAULT_STACKSIZE=$(DEFAULT_STACKSIZE)
00db10
+tst-default-attr-ENV = GLIBC_PTHREAD_STACKSIZE=$(DEFAULT_STACKSIZE)
00db10
+
00db10
 include ../Makeconfig
00db10
 
00db10
 ifeq ($(have-forced-unwind),yes)
00db10
diff -pruN glibc-2.17-c758a686/nptl/nptl-init.c glibc-2.17-c758a686/nptl/nptl-init.c
00db10
--- glibc-2.17-c758a686/nptl/nptl-init.c	2013-07-30 11:46:35.112902019 +0530
00db10
+++ glibc-2.17-c758a686/nptl/nptl-init.c	2013-07-30 11:46:44.601901689 +0530
00db10
@@ -35,6 +35,7 @@
00db10
 #include <smp.h>
00db10
 #include <lowlevellock.h>
00db10
 #include <kernel-features.h>
00db10
+#include <libc-internal.h>
00db10
 
00db10
 
00db10
 /* Size and alignment of static TLS block.  */
00db10
@@ -276,8 +277,28 @@ extern void **__libc_dl_error_tsd (void)
00db10
 /* This can be set by the debugger before initialization is complete.  */
00db10
 static bool __nptl_initial_report_events __attribute_used__;
00db10
 
00db10
+/* Validate and set the default stacksize.  */
00db10
+static void
00db10
+set_default_stacksize (size_t stacksize)
00db10
+{
00db10
+  if (stacksize < PTHREAD_STACK_MIN)
00db10
+    stacksize = PTHREAD_STACK_MIN;
00db10
+
00db10
+  /* Make sure it meets the minimum size that allocate_stack
00db10
+     (allocatestack.c) will demand, which depends on the page size.  */
00db10
+  const uintptr_t pagesz = GLRO(dl_pagesize);
00db10
+  const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
00db10
+
00db10
+  if (stacksize < minstack)
00db10
+    stacksize = minstack;
00db10
+
00db10
+  /* Round the resource limit up to page size.  */
00db10
+  stacksize = ALIGN_UP (stacksize, pagesz);
00db10
+  __default_pthread_attr.stacksize = stacksize;
00db10
+}
00db10
+
00db10
 void
00db10
-__pthread_initialize_minimal_internal (void)
00db10
+__pthread_initialize_minimal_internal (int argc, char **argv, char **envp)
00db10
 {
00db10
 #ifndef SHARED
00db10
   /* Unlike in the dynamically linked case the dynamic linker has not
00db10
@@ -401,29 +422,44 @@ __pthread_initialize_minimal_internal (v
00db10
 
00db10
   __static_tls_size = roundup (__static_tls_size, static_tls_align);
00db10
 
00db10
-  /* Determine the default allowed stack size.  This is the size used
00db10
-     in case the user does not specify one.  */
00db10
-  struct rlimit limit;
00db10
-  if (__getrlimit (RLIMIT_STACK, &limit) != 0
00db10
-      || limit.rlim_cur == RLIM_INFINITY)
00db10
-    /* The system limit is not usable.  Use an architecture-specific
00db10
-       default.  */
00db10
-    limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
00db10
-  else if (limit.rlim_cur < PTHREAD_STACK_MIN)
00db10
-    /* The system limit is unusably small.
00db10
-       Use the minimal size acceptable.  */
00db10
-    limit.rlim_cur = PTHREAD_STACK_MIN;
00db10
+  /* Initialize the environment.  libc.so gets initialized after us due to a
00db10
+     circular dependency and hence __environ is not available otherwise.  */
00db10
+  __environ = envp; 
00db10
 
00db10
-  /* Make sure it meets the minimum size that allocate_stack
00db10
-     (allocatestack.c) will demand, which depends on the page size.  */
00db10
-  const uintptr_t pagesz = GLRO(dl_pagesize);
00db10
-  const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
00db10
-  if (limit.rlim_cur < minstack)
00db10
-    limit.rlim_cur = minstack;
00db10
+#ifndef SHARED
00db10
+  __libc_init_secure ();
00db10
+#endif
00db10
 
00db10
-  /* Round the resource limit up to page size.  */
00db10
-  limit.rlim_cur = (limit.rlim_cur + pagesz - 1) & -pagesz;
00db10
-  __default_pthread_attr.stacksize = limit.rlim_cur;
00db10
+  /* Get the default stack size from the environment variable if it is set and
00db10
+     is valid.  */
00db10
+  size_t stacksize = 0;
00db10
+  char *envval = __libc_secure_getenv ("GLIBC_PTHREAD_STACKSIZE");
00db10
+
00db10
+  if (__builtin_expect (envval != NULL && envval[0] != '\0', 0))
00db10
+    {
00db10
+      char *env_conv = envval;
00db10
+      size_t ret = strtoul (envval, &env_conv, 0);
00db10
+
00db10
+      if (*env_conv == '\0' && env_conv != envval)
00db10
+	stacksize = ret;
00db10
+    }
00db10
+
00db10
+  if (stacksize == 0)
00db10
+    {
00db10
+      /* Determine the default allowed stack size.  */
00db10
+      struct rlimit limit;
00db10
+      if (__getrlimit (RLIMIT_STACK, &limit) != 0
00db10
+	  || limit.rlim_cur == RLIM_INFINITY)
00db10
+	/* The system limit is not usable.  Use an architecture-specific
00db10
+	   default.  */
00db10
+	stacksize = ARCH_STACK_DEFAULT_SIZE;
00db10
+      else
00db10
+	stacksize = limit.rlim_cur;
00db10
+    }
00db10
+
00db10
+  /* Finally, set the default stack size.  This size is used when the user does
00db10
+     not specify a stack size during thread creation.  */
00db10
+  set_default_stacksize (stacksize);
00db10
   __default_pthread_attr.guardsize = GLRO (dl_pagesize);
00db10
 
00db10
 #ifdef SHARED
00db10
diff -pruN glibc-2.17-c758a686/nptl/tst-default-attr.c glibc-2.17-c758a686/nptl/tst-default-attr.c
00db10
--- glibc-2.17-c758a686/nptl/tst-default-attr.c	1970-01-01 05:30:00.000000000 +0530
00db10
+++ glibc-2.17-c758a686/nptl/tst-default-attr.c	2013-07-30 11:46:44.601901689 +0530
00db10
@@ -0,0 +1,109 @@
00db10
+/* Verify that default stack size gets set correctly from the environment
00db10
+   variable.
00db10
+
00db10
+   Copyright (C) 2013 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <pthread.h>
00db10
+#include <stdio.h>
00db10
+#include <stdint.h>
00db10
+#include <string.h>
00db10
+#include <unistd.h>
00db10
+#include <errno.h>
00db10
+
00db10
+#define RETURN_IF_FAIL(f, ...) \
00db10
+  ({									      \
00db10
+    int ret = f (__VA_ARGS__);						      \
00db10
+    if (ret != 0)							      \
00db10
+      {									      \
00db10
+	printf ("%s:%d: %s returned %d (errno = %d)\n", __FILE__, __LINE__,   \
00db10
+		#f, ret, errno);					      \
00db10
+	return ret;							      \
00db10
+      }									      \
00db10
+  })
00db10
+
00db10
+/* DEFAULT_STACKSIZE macro is defined in the Makefile.  */
00db10
+static size_t stacksize = DEFAULT_STACKSIZE;
00db10
+
00db10
+static int
00db10
+verify_stacksize_result (pthread_attr_t *attr)
00db10
+{
00db10
+  size_t stack;
00db10
+
00db10
+  RETURN_IF_FAIL (pthread_attr_getstacksize, attr, &stack);
00db10
+
00db10
+  if (stacksize != stack)
00db10
+    {
00db10
+      printf ("failed to set default stacksize (%zu, %zu)\n", stacksize, stack);
00db10
+      return 1;
00db10
+    }
00db10
+
00db10
+  return 0;
00db10
+}
00db10
+
00db10
+static void *
00db10
+thr (void *unused __attribute__ ((unused)))
00db10
+{
00db10
+  pthread_attr_t attr;
00db10
+  int ret;
00db10
+
00db10
+  memset (&attr, 0xab, sizeof attr);
00db10
+  /* To verify that the attributes actually got applied.  */
00db10
+  if ((ret = pthread_getattr_np (pthread_self (), &attr)) != 0)
00db10
+    {
00db10
+      printf ("pthread_getattr_np failed: %s\n", strerror (ret));
00db10
+      goto out;
00db10
+    }
00db10
+
00db10
+  ret = verify_stacksize_result (&attr);
00db10
+
00db10
+out:
00db10
+  return (void *) (uintptr_t) ret;
00db10
+}
00db10
+
00db10
+static int
00db10
+run_threads (void)
00db10
+{
00db10
+  pthread_t t;
00db10
+  void *tret = NULL;
00db10
+
00db10
+  /* Run twice to ensure that the attributes do not get overwritten in the
00db10
+     first run somehow.  */
00db10
+  for (int i = 0; i < 2; i++)
00db10
+    {
00db10
+      RETURN_IF_FAIL (pthread_create, &t, NULL, thr, NULL);
00db10
+      RETURN_IF_FAIL (pthread_join, t, &tret);
00db10
+
00db10
+      if (tret != NULL)
00db10
+	{
00db10
+	  puts ("Thread failed");
00db10
+	  return 1;
00db10
+	}
00db10
+    }
00db10
+
00db10
+  return 0;
00db10
+}
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  RETURN_IF_FAIL (run_threads);
00db10
+  return 0;
00db10
+}
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"