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