Subject: bug#16069: emacs bug #16069 - cause & reproduction

From: Dmitry Antipov <[email-masked]>
Date: Fri, 28 Feb 2014 10:39:40 +0400

On 02/27/2014 04:27 PM, nil wrote:

> I ran into the first crash reported on bug #16069, on trunk r116550. It appears to trigger when an xftfont is garbage collected - but only after its Display has been freed and then reused. The guard in xftfont_close is satisfied, then XftFontClose causes xft's reference counting to go out of sync. SIGSEGV occurs when the display is closed (_XftCloseDisplay, XftFontManageMemory, XftFontFindNthUnref returns an unexpected NULL)
>
> To reproduce:
>
> gdb --args emacs -Q -nw
>
> set $dpy = 0
> break xterm.c:9810
>    condition $bpnum $dpy == dpy
> break xterm.c:9814
>    commands
>      silent
>      set $dpy = dpy
>      cont
>    end
> run
>
> M-x server-start
>
> Then repeatedly:
>
> emacsclient -c -n .
> C-x C-c
>
> until the breakpoint fires on Display address reuse. after continuing, closing the terminal should SIGSEGV in xft/src/xftfreetype.c. (This is on an x86_64 linux with recent glibc/xorg/etc, if it makes any difference.)
>
> I'm not nearly familiar enough with the insides of emacs to suggest a fix, I'm afraid. Happy to provide any other information you need, though!

Thanks. Next time please do CC: to appropriate bug (i.e. [bug-number]@debbugs.gnu.org).

Could you please try this patch?

Dmitry

=== modified file 'src/xfont.c'
--- src/xfont.c	2014-01-01 07:43:34 +0000
+++ src/xfont.c	2014-02-28 06:20:04 +0000
@@ -42,6 +42,7 @@
   struct font font;
   Display *display;
   XFontStruct *xfont;
+  unsigned x_display_id;
 };
 
 /* Prototypes of support functions.  */
@@ -808,6 +809,7 @@
   font = XFONT_OBJECT (font_object);
   ((struct xfont_info *) font)->xfont = xfont;
   ((struct xfont_info *) font)->display = FRAME_X_DISPLAY (f);
+  ((struct xfont_info *) font)->x_display_id = FRAME_DISPLAY_INFO (f)->x_id;
   font->pixel_size = pixel_size;
   font->driver = &xfont_driver;
   font->encoding_charset = encoding->id;
@@ -892,12 +894,15 @@
 static void
 xfont_close (struct font *font)
 {
+  struct x_display_info *xdi;
   struct xfont_info *xfi = (struct xfont_info *) font;
 
   /* This function may be called from GC when X connection is gone
      (Bug#16093), and an attempt to free font resources on invalid
      display may lead to X protocol errors or segfaults.  */
-  if (xfi->xfont && x_display_info_for_display (xfi->display))
+  if (xfi->xfont
+      && ((xdi = x_display_info_for_display (xfi->display))
+	  && xfi->x_display_id == xdi->x_id))
     {
       block_input ();
       XFreeFont (xfi->display, xfi->xfont);

=== modified file 'src/xftfont.c'
--- src/xftfont.c	2014-01-06 06:25:30 +0000
+++ src/xftfont.c	2014-02-28 06:23:32 +0000
@@ -59,6 +59,7 @@
   FT_Matrix matrix;
   Display *display;
   XftFont *xftfont;
+  unsigned x_display_id;
 };
 
 /* Structure pointed by (struct face *)->extra  */
@@ -372,6 +373,7 @@
   xftfont_info = (struct xftfont_info *) font;
   xftfont_info->display = display;
   xftfont_info->xftfont = xftfont;
+  xftfont_info->x_display_id = FRAME_DISPLAY_INFO (f)->x_id;
   /* This means that there's no need of transformation.  */
   xftfont_info->matrix.xx = 0;
   if (FcPatternGetMatrix (xftfont->pattern, FC_MATRIX, 0, &matrix)
@@ -481,6 +483,7 @@
 static void
 xftfont_close (struct font *font)
 {
+  struct x_display_info *xdi;
   struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
 
 #ifdef HAVE_LIBOTF
@@ -493,7 +496,8 @@
 
   /* See comment in xfont_close.  */
   if (xftfont_info->xftfont
-      && x_display_info_for_display (xftfont_info->display))
+      && ((xdi = x_display_info_for_display (xftfont_info->display))
+	  && xftfont_info->x_display_id == xdi->x_id))
     {
       block_input ();
       XftUnlockFace (xftfont_info->xftfont);

=== modified file 'src/xterm.c'
--- src/xterm.c	2014-02-04 07:36:58 +0000
+++ src/xterm.c	2014-02-28 06:28:46 +0000
@@ -9679,6 +9679,8 @@
 }
 #endif
 
+static unsigned x_display_id;
+
 /* Open a connection to X display DISPLAY_NAME, and return
    the structure that describes the open display.
    If we cannot contact the display, return null.  */
@@ -9896,6 +9898,7 @@
   lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
   if (lim - SBYTES (Vinvocation_name) < SBYTES (Vsystem_name))
     memory_full (SIZE_MAX);
+  dpyinfo->x_id = ++x_display_id;
   dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
 				+ SBYTES (Vsystem_name) + 2);
   strcat (strcat (strcpy (dpyinfo->x_id_name, SSDATA (Vinvocation_name)), "@"),

=== modified file 'src/xterm.h'
--- src/xterm.h	2014-01-11 09:31:09 +0000
+++ src/xterm.h	2014-02-28 06:11:05 +0000
@@ -198,6 +198,7 @@
      mouse-face.  */
   Mouse_HLInfo mouse_highlight;
 
+  unsigned x_id;
   char *x_id_name;
 
   /* The number of fonts opened for this display.  */