Jan Synacek 12a7e9
Backport the following upstream patches:
Jan Synacek 12a7e9
Jan Synacek 12a7e9
http://git.savannah.gnu.org/cgit/emacs.git/patch/?id=9afea93ed536fb9110ac62b413604cf4c4302199
Jan Synacek 12a7e9
http://git.savannah.gnu.org/cgit/emacs.git/patch/?id=71ca4f6a43bad06192cbc4bb8c7a2d69c179b7b0
Jan Synacek 12a7e9
http://git.savannah.gnu.org/cgit/emacs.git/patch/?id=1047496722a58ef5b736dae64d32adeb58c5055c
Jan Synacek 12a7e9
http://git.savannah.gnu.org/cgit/emacs.git/patch/?id=96ac0c3ebce825e60595794f99e703ec8302e240
Jan Synacek 12a7e9
http://git.savannah.gnu.org/cgit/emacs.git/patch/?id=43986d16fb6ad78a627250e14570ea70bdb1f23a
Jan Synacek 12a7e9
Jan Synacek 12a7e9
Resolves: #1398718
Jan Synacek 12a7e9
Jan Synacek 12a7e9
commit 9afea93ed536fb9110ac62b413604cf4c4302199
Jan Synacek 12a7e9
Author: Eli Zaretskii <eliz@gnu.org>
Jan Synacek 12a7e9
Date:   Sun Oct 23 16:54:00 2016 +0300
Jan Synacek 12a7e9
Jan Synacek 12a7e9
    Attempt to catch reads from a buffer that is relocated
Jan Synacek 12a7e9
    
Jan Synacek 12a7e9
    * src/xml.c (parse_region): Add assertion to ensure buffer text is
Jan Synacek 12a7e9
    not relocated while libxml2 is reading it.  (Bug#24764)
Jan Synacek 12a7e9
Jan Synacek 12a7e9
diff --git a/src/xml.c b/src/xml.c
Jan Synacek 12a7e9
index b1175d1..1ef84bd 100644
Jan Synacek 12a7e9
--- a/src/xml.c
Jan Synacek 12a7e9
+++ b/src/xml.c
Jan Synacek 12a7e9
@@ -181,6 +181,7 @@ parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url,
Jan Synacek 12a7e9
   Lisp_Object result = Qnil;
Jan Synacek 12a7e9
   const char *burl = "";
Jan Synacek 12a7e9
   ptrdiff_t istart, iend, istart_byte, iend_byte;
Jan Synacek 12a7e9
+  unsigned char *buftext;
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
   xmlCheckVersion (LIBXML_VERSION);
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
@@ -200,18 +201,24 @@ parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url,
Jan Synacek 12a7e9
       burl = SSDATA (base_url);
Jan Synacek 12a7e9
     }
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
+  buftext = BYTE_POS_ADDR (istart_byte);
Jan Synacek 12a7e9
   if (htmlp)
Jan Synacek 12a7e9
-    doc = htmlReadMemory ((char *) BYTE_POS_ADDR (istart_byte),
Jan Synacek 12a7e9
+    doc = htmlReadMemory ((char *)buftext,
Jan Synacek 12a7e9
 			  iend_byte - istart_byte, burl, "utf-8",
Jan Synacek 12a7e9
 			  HTML_PARSE_RECOVER|HTML_PARSE_NONET|
Jan Synacek 12a7e9
 			  HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR|
Jan Synacek 12a7e9
 			  HTML_PARSE_NOBLANKS);
Jan Synacek 12a7e9
   else
Jan Synacek 12a7e9
-    doc = xmlReadMemory ((char *) BYTE_POS_ADDR (istart_byte),
Jan Synacek 12a7e9
+    doc = xmlReadMemory ((char *)buftext,
Jan Synacek 12a7e9
 			 iend_byte - istart_byte, burl, "utf-8",
Jan Synacek 12a7e9
 			 XML_PARSE_NONET|XML_PARSE_NOWARNING|
Jan Synacek 12a7e9
 			 XML_PARSE_NOBLANKS |XML_PARSE_NOERROR);
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
+  /* If the assertion below fails, malloc was called inside the above
Jan Synacek 12a7e9
+     libxml2 functions, and ralloc.c caused relocation of buffer text,
Jan Synacek 12a7e9
+     so we could have read from unrelated memory.  */
Jan Synacek 12a7e9
+  eassert (buftext == BYTE_POS_ADDR (istart_byte));
Jan Synacek 12a7e9
+
Jan Synacek 12a7e9
   if (doc != NULL)
Jan Synacek 12a7e9
     {
Jan Synacek 12a7e9
       Lisp_Object r = Qnil;
Jan Synacek 12a7e9
Jan Synacek 12a7e9
commit 71ca4f6a43bad06192cbc4bb8c7a2d69c179b7b0
Jan Synacek 12a7e9
Author: Eli Zaretskii <eliz@gnu.org>
Jan Synacek 12a7e9
Date:   Sun Oct 23 19:52:56 2016 +0300
Jan Synacek 12a7e9
Jan Synacek 12a7e9
    Avoid relocating buffers while libxml2 reads its text
Jan Synacek 12a7e9
    
Jan Synacek 12a7e9
    * src/xml.c (parse_region) [REL_ALLOC]: Freeze the ralloc arena
Jan Synacek 12a7e9
    while libxml2 reads the current buffer's text.  (Bug#24764)
Jan Synacek 12a7e9
Jan Synacek 12a7e9
diff --git a/src/xml.c b/src/xml.c
Jan Synacek 12a7e9
index 1ef84bd..612b16c 100644
Jan Synacek 12a7e9
--- a/src/xml.c
Jan Synacek 12a7e9
+++ b/src/xml.c
Jan Synacek 12a7e9
@@ -202,6 +202,11 @@ parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url,
Jan Synacek 12a7e9
     }
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
   buftext = BYTE_POS_ADDR (istart_byte);
Jan Synacek 12a7e9
+#ifdef REL_ALLOC
Jan Synacek 12a7e9
+  /* Prevent ralloc.c from relocating the current buffer while libxml2
Jan Synacek 12a7e9
+     functions below read its text.  */
Jan Synacek 12a7e9
+  r_alloc_inhibit_buffer_relocation (1);
Jan Synacek 12a7e9
+#endif
Jan Synacek 12a7e9
   if (htmlp)
Jan Synacek 12a7e9
     doc = htmlReadMemory ((char *)buftext,
Jan Synacek 12a7e9
 			  iend_byte - istart_byte, burl, "utf-8",
Jan Synacek 12a7e9
@@ -214,6 +219,9 @@ parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url,
Jan Synacek 12a7e9
 			 XML_PARSE_NONET|XML_PARSE_NOWARNING|
Jan Synacek 12a7e9
 			 XML_PARSE_NOBLANKS |XML_PARSE_NOERROR);
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
+#ifdef REL_ALLOC
Jan Synacek 12a7e9
+  r_alloc_inhibit_buffer_relocation (0);
Jan Synacek 12a7e9
+#endif
Jan Synacek 12a7e9
   /* If the assertion below fails, malloc was called inside the above
Jan Synacek 12a7e9
      libxml2 functions, and ralloc.c caused relocation of buffer text,
Jan Synacek 12a7e9
      so we could have read from unrelated memory.  */
Jan Synacek 12a7e9
Jan Synacek 12a7e9
commit 1047496722a58ef5b736dae64d32adeb58c5055c
Jan Synacek 12a7e9
Author: Eli Zaretskii <eliz@gnu.org>
Jan Synacek 12a7e9
Date:   Mon Oct 24 16:59:34 2016 +0300
Jan Synacek 12a7e9
Jan Synacek 12a7e9
    Another fix for using pointer to buffer text
Jan Synacek 12a7e9
    
Jan Synacek 12a7e9
    * src/search.c (Freplace_match): Move the call to BYTE_POS_ADDR
Jan Synacek 12a7e9
    after the call to xpalloc, to avoid the danger of buffer text
Jan Synacek 12a7e9
    relocation after its address was taken.  (Bug#24358)
Jan Synacek 12a7e9
Jan Synacek 12a7e9
diff --git a/src/search.c b/src/search.c
Jan Synacek 12a7e9
index 5c04916..f8acd40 100644
Jan Synacek 12a7e9
--- a/src/search.c
Jan Synacek 12a7e9
+++ b/src/search.c
Jan Synacek 12a7e9
@@ -2640,6 +2640,7 @@ since only regular expressions have distinguished subexpressions.  */)
Jan Synacek 12a7e9
 	  const unsigned char *add_stuff = NULL;
Jan Synacek 12a7e9
 	  ptrdiff_t add_len = 0;
Jan Synacek 12a7e9
 	  ptrdiff_t idx = -1;
Jan Synacek 12a7e9
+	  ptrdiff_t begbyte;
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
 	  if (str_multibyte)
Jan Synacek 12a7e9
 	    {
Jan Synacek 12a7e9
@@ -2702,11 +2703,10 @@ since only regular expressions have distinguished subexpressions.  */)
Jan Synacek 12a7e9
 	     set up ADD_STUFF and ADD_LEN to point to it.  */
Jan Synacek 12a7e9
 	  if (idx >= 0)
Jan Synacek 12a7e9
 	    {
Jan Synacek 12a7e9
-	      ptrdiff_t begbyte = CHAR_TO_BYTE (search_regs.start[idx]);
Jan Synacek 12a7e9
+	      begbyte = CHAR_TO_BYTE (search_regs.start[idx]);
Jan Synacek 12a7e9
 	      add_len = CHAR_TO_BYTE (search_regs.end[idx]) - begbyte;
Jan Synacek 12a7e9
 	      if (search_regs.start[idx] < GPT && GPT < search_regs.end[idx])
Jan Synacek 12a7e9
 		move_gap_both (search_regs.start[idx], begbyte);
Jan Synacek 12a7e9
-	      add_stuff = BYTE_POS_ADDR (begbyte);
Jan Synacek 12a7e9
 	    }
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
 	  /* Now the stuff we want to add to SUBSTED
Jan Synacek 12a7e9
@@ -2719,6 +2719,11 @@ since only regular expressions have distinguished subexpressions.  */)
Jan Synacek 12a7e9
 		       add_len - (substed_alloc_size - substed_len),
Jan Synacek 12a7e9
 		       STRING_BYTES_BOUND, 1);
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
+	  /* We compute this after the call to xpalloc, because that
Jan Synacek 12a7e9
+	     could cause buffer text be relocated when ralloc.c is used.  */
Jan Synacek 12a7e9
+	  if (idx >= 0)
Jan Synacek 12a7e9
+	    add_stuff = BYTE_POS_ADDR (begbyte);
Jan Synacek 12a7e9
+
Jan Synacek 12a7e9
 	  /* Now add to the end of SUBSTED.  */
Jan Synacek 12a7e9
 	  if (add_stuff)
Jan Synacek 12a7e9
 	    {
Jan Synacek 12a7e9
Jan Synacek 12a7e9
commit 96ac0c3ebce825e60595794f99e703ec8302e240
Jan Synacek 12a7e9
Author: Eli Zaretskii <eliz@gnu.org>
Jan Synacek 12a7e9
Date:   Mon Oct 24 21:37:20 2016 +0300
Jan Synacek 12a7e9
Jan Synacek 12a7e9
    Yet another fix for using pointers into buffer text
Jan Synacek 12a7e9
    
Jan Synacek 12a7e9
    * src/search.c (boyer_moore): Update pointers to buffer text
Jan Synacek 12a7e9
    after call to set_search_regs.  (Bug#24358)
Jan Synacek 12a7e9
Jan Synacek 12a7e9
diff --git a/src/search.c b/src/search.c
Jan Synacek 12a7e9
index f8acd40..b50e7f0 100644
Jan Synacek 12a7e9
--- a/src/search.c
Jan Synacek 12a7e9
+++ b/src/search.c
Jan Synacek 12a7e9
@@ -2014,13 +2014,20 @@ boyer_moore (EMACS_INT n, unsigned char *base_pat,
Jan Synacek 12a7e9
 	      cursor += dirlen - i - direction;	/* fix cursor */
Jan Synacek 12a7e9
 	      if (i + direction == 0)
Jan Synacek 12a7e9
 		{
Jan Synacek 12a7e9
-		  ptrdiff_t position, start, end;
Jan Synacek 12a7e9
+		  ptrdiff_t position, start, end, cursor_off;
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
 		  cursor -= direction;
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
 		  position = pos_byte + cursor - p2 + ((direction > 0)
Jan Synacek 12a7e9
 						       ? 1 - len_byte : 0);
Jan Synacek 12a7e9
+		  /* set_search_regs might call malloc, which could
Jan Synacek 12a7e9
+		     cause ralloc.c relocate buffer text.  We need to
Jan Synacek 12a7e9
+		     update pointers into buffer text due to that.  */
Jan Synacek 12a7e9
+		  cursor_off = cursor - p2;
Jan Synacek 12a7e9
 		  set_search_regs (position, len_byte);
Jan Synacek 12a7e9
+		  p_limit = BYTE_POS_ADDR (limit);
Jan Synacek 12a7e9
+		  p2 = BYTE_POS_ADDR (pos_byte);
Jan Synacek 12a7e9
+		  cursor = p2 + cursor_off;
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
 		  if (NILP (Vinhibit_changing_match_data))
Jan Synacek 12a7e9
 		    {
Jan Synacek 12a7e9
Jan Synacek 12a7e9
commit 43986d16fb6ad78a627250e14570ea70bdb1f23a
Jan Synacek 12a7e9
Author: Noam Postavsky <npostavs@gmail.com>
Jan Synacek 12a7e9
Date:   Mon Oct 24 21:22:07 2016 -0400
Jan Synacek 12a7e9
Jan Synacek 12a7e9
    Inhibit buffer relocation during regex searches
Jan Synacek 12a7e9
    
Jan Synacek 12a7e9
    * src/search.c (looking_at_1, fast_looking_at, search_buffer): Prevent
Jan Synacek 12a7e9
    relocation of buffer contents during calls to re_search_2.  This ensures
Jan Synacek 12a7e9
    the pointers into buffer text won't be invalidated by
Jan Synacek 12a7e9
    r_alloc_sbrk (called from malloc with configurations where
Jan Synacek 12a7e9
    REL_ALLOC=yes).
Jan Synacek 12a7e9
Jan Synacek 12a7e9
diff --git a/src/search.c b/src/search.c
Jan Synacek 12a7e9
index fa5ac44..15504be 100644
Jan Synacek 12a7e9
--- a/src/search.c
Jan Synacek 12a7e9
+++ b/src/search.c
Jan Synacek 12a7e9
@@ -308,12 +308,20 @@ looking_at_1 (Lisp_Object string, bool posix)
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
   re_match_object = Qnil;
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
+#ifdef REL_ALLOC
Jan Synacek 12a7e9
+  /* Prevent ralloc.c from relocating the current buffer while
Jan Synacek 12a7e9
+     searching it.  */
Jan Synacek 12a7e9
+  r_alloc_inhibit_buffer_relocation (1);
Jan Synacek 12a7e9
+#endif
Jan Synacek 12a7e9
   i = re_match_2 (bufp, (char *) p1, s1, (char *) p2, s2,
Jan Synacek 12a7e9
 		  PT_BYTE - BEGV_BYTE,
Jan Synacek 12a7e9
 		  (NILP (Vinhibit_changing_match_data)
Jan Synacek 12a7e9
 		   ? &search_regs : NULL),
Jan Synacek 12a7e9
 		  ZV_BYTE - BEGV_BYTE);
Jan Synacek 12a7e9
   immediate_quit = 0;
Jan Synacek 12a7e9
+#ifdef REL_ALLOC
Jan Synacek 12a7e9
+  r_alloc_inhibit_buffer_relocation (0);
Jan Synacek 12a7e9
+#endif
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
   if (i == -2)
Jan Synacek 12a7e9
     matcher_overflow ();
Jan Synacek 12a7e9
@@ -561,8 +569,16 @@ fast_looking_at (Lisp_Object regexp, ptrdiff_t pos, ptrdiff_t pos_byte,
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
   buf = compile_pattern (regexp, 0, Qnil, 0, multibyte);
Jan Synacek 12a7e9
   immediate_quit = 1;
Jan Synacek 12a7e9
+#ifdef REL_ALLOC
Jan Synacek 12a7e9
+  /* Prevent ralloc.c from relocating the current buffer while
Jan Synacek 12a7e9
+     searching it.  */
Jan Synacek 12a7e9
+  r_alloc_inhibit_buffer_relocation (1);
Jan Synacek 12a7e9
+#endif
Jan Synacek 12a7e9
   len = re_match_2 (buf, (char *) p1, s1, (char *) p2, s2,
Jan Synacek 12a7e9
 		    pos_byte, NULL, limit_byte);
Jan Synacek 12a7e9
+#ifdef REL_ALLOC
Jan Synacek 12a7e9
+  r_alloc_inhibit_buffer_relocation (0);
Jan Synacek 12a7e9
+#endif
Jan Synacek 12a7e9
   immediate_quit = 0;
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
   return len;
Jan Synacek 12a7e9
@@ -1213,6 +1229,12 @@ search_buffer (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
Jan Synacek 12a7e9
 	}
Jan Synacek 12a7e9
       re_match_object = Qnil;
Jan Synacek 12a7e9
 
Jan Synacek 12a7e9
+#ifdef REL_ALLOC
Jan Synacek 12a7e9
+  /* Prevent ralloc.c from relocating the current buffer while
Jan Synacek 12a7e9
+     searching it.  */
Jan Synacek 12a7e9
+  r_alloc_inhibit_buffer_relocation (1);
Jan Synacek 12a7e9
+#endif
Jan Synacek 12a7e9
+
Jan Synacek 12a7e9
       while (n < 0)
Jan Synacek 12a7e9
 	{
Jan Synacek 12a7e9
 	  ptrdiff_t val;
Jan Synacek 12a7e9
@@ -1254,6 +1276,9 @@ search_buffer (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
Jan Synacek 12a7e9
 	  else
Jan Synacek 12a7e9
 	    {
Jan Synacek 12a7e9
 	      immediate_quit = 0;
Jan Synacek 12a7e9
+#ifdef REL_ALLOC
Jan Synacek 12a7e9
+              r_alloc_inhibit_buffer_relocation (0);
Jan Synacek 12a7e9
+#endif
Jan Synacek 12a7e9
 	      return (n);
Jan Synacek 12a7e9
 	    }
Jan Synacek 12a7e9
 	  n++;
Jan Synacek 12a7e9
@@ -1296,11 +1321,17 @@ search_buffer (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
Jan Synacek 12a7e9
 	  else
Jan Synacek 12a7e9
 	    {
Jan Synacek 12a7e9
 	      immediate_quit = 0;
Jan Synacek 12a7e9
+#ifdef REL_ALLOC
Jan Synacek 12a7e9
+              r_alloc_inhibit_buffer_relocation (0);
Jan Synacek 12a7e9
+#endif
Jan Synacek 12a7e9
 	      return (0 - n);
Jan Synacek 12a7e9
 	    }
Jan Synacek 12a7e9
 	  n--;
Jan Synacek 12a7e9
 	}
Jan Synacek 12a7e9
       immediate_quit = 0;
Jan Synacek 12a7e9
+#ifdef REL_ALLOC
Jan Synacek 12a7e9
+      r_alloc_inhibit_buffer_relocation (0);
Jan Synacek 12a7e9
+#endif
Jan Synacek 12a7e9
       return (pos);
Jan Synacek 12a7e9
     }
Jan Synacek 12a7e9
   else				/* non-RE case */