Subject: 25.0.50; Questionable call to getrlimit in handle_sigsegv

From: Ken Brown <[email-masked]>
Date: Tue, 07 Jul 2015 16:11:53 -0400

There is a call to setrlimit in main() that tries to increase the stack
size.  As a result of this, the value of rlim.rlim_cur in
handle_sigsegv() might exceed the actual stack size.  See Corinna
Vinschen's message at

   https://www.cygwin.com/ml/cygwin/2015-07/msg00092.html

for a more detailed explanation.

Corinna suggests using pthread_getattr_np instead of getrlimit to avoid
this problem, as in the following patch:

diff --git a/src/sysdep.c b/src/sysdep.c
index 91036f0..c49e333 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -1625,6 +1625,8 @@ handle_arith_signal (int sig)

#ifdef HAVE_STACK_OVERFLOW_HANDLING

+#include <pthread.h>
+
/* -1 if stack grows down as expected on most OS/ABI variants, 1 
otherwise.  */

static int stack_direction;
@@ -1642,9 +1644,11 @@ handle_sigsegv (int sig, siginfo_t *siginfo, void 
*arg)
too nested calls to mark_object.  No way to survive.  */
if (!gc_in_progress)
{
-      struct rlimit rlim;
+      pthread_attr_t attr;
+      size_t stacksize;

-      if (!getrlimit (RLIMIT_STACK, &rlim))
+      if (!pthread_getattr_np (pthread_self (), &attr)
+         && !pthread_attr_getstacksize (&attr, &stacksize))
{
/* STACK_DANGER_ZONE has to be bigger than 16K on Cygwin, for
reasons explained in
@@ -1657,7 +1661,7 @@ handle_sigsegv (int sig, siginfo_t *siginfo, void 
*arg)
char *beg, *end, *addr;

beg = stack_bottom;
-         end = stack_bottom + stack_direction * rlim.rlim_cur;
+         end = stack_bottom + stack_direction * stacksize;
if (beg > end)
addr = beg, beg = end, end = addr;
addr = (char *) siginfo->si_addr;

Of course, the definition of HAVE_STACK_OVERFLOW_HANDLING would have to
be changed to ensure that pthread_getattr_np exists.

In GNU Emacs 25.0.50.17 (x86_64-unknown-cygwin, GTK+ Version 3.14.13)
  of 2015-07-07 on moufang
Repository revision: 0bfc94047da4960af55196242728a7a55120867f
Windowing system distributor `The Cygwin/X Project', version 11.0.11701000
Configured using:
  `configure 'CFLAGS=-g3 -O0''

Configured features:
XPM JPEG TIFF GIF PNG RSVG IMAGEMAGICK SOUND DBUS GSETTINGS NOTIFY ACL
GNUTLS LIBXML2 FREETYPE M17N_FLT LIBOTF XFT ZLIB TOOLKIT_SCROLL_BARS
GTK3 X11

Important settings:
   value of $LANG: en_US.UTF-8
   locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
   tooltip-mode: t
   global-eldoc-mode: t
   electric-indent-mode: t
   mouse-wheel-mode: t
   tool-bar-mode: t
   menu-bar-mode: t
   file-name-shadow-mode: t
   global-font-lock-mode: t
   font-lock-mode: t
   blink-cursor-mode: t
   auto-composition-mode: t
   auto-encryption-mode: t
   auto-compression-mode: t
   line-number-mode: t
   transient-mark-mode: t

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
Making completion list...

Load-path shadows:
None found.

Features:
(shadow sort gnus-util mail-extr emacsbug message dired format-spec
rfc822 mml mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums
mm-util help-fns help-mode easymenu cl-loaddefs pcase cl-lib mail-prsvr
mail-utils time-date mule-util tooltip eldoc electric uniquify
ediff-hook vc-hooks lisp-float-type mwheel x-win term/common-win x-dnd
tool-bar dnd fontset image regexp-opt fringe tabulated-list newcomment
elisp-mode lisp-mode prog-mode register page menu-bar rfn-eshadow timer
select scroll-bar mouse jit-lock font-lock syntax facemenu font-core
frame cl-generic cham georgian utf-8-lang misc-lang vietnamese tibetan
thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek romanian
slovak czech european ethiopic indian cyrillic chinese charscript
case-table epa-hook jka-cmpr-hook help simple abbrev minibuffer
cl-preloaded nadvice loaddefs button faces cus-face macroexp files
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote dbusbind gfilenotify
dynamic-setting system-font-setting font-render-setting move-toolbar gtk
x-toolkit x multi-tty make-network-process emacs)