emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master fe6c507: Make sure that ‘sdata’ objects in ‘sblock


From: Philipp Stephani
Subject: [Emacs-diffs] master fe6c507: Make sure that ‘sdata’ objects in ‘sblock’ objects are aligned.
Date: Thu, 25 Apr 2019 15:30:07 -0400 (EDT)

branch: master
commit fe6c507f5ce0fd744b5bd1d0db6ea175e1188a7f
Author: Philipp Stephani <address@hidden>
Commit: Philipp Stephani <address@hidden>

    Make sure that ‘sdata’ objects in ‘sblock’ objects are aligned.
    
    Issue found by Clang’s UBSan.
    
    * src/alloc.c (GC_STRING_OVERRUN_COOKIE_SIZE): Increase to 8.
    (string_overrun_cookie): Extend accordingly.
    (GC_STRING_EXTRA): Ensure that it’s properly aligned for ‘sdata’.
    (allocate_string_data): Verify that ‘sdata’ blocks remain aligned.
---
 src/alloc.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/alloc.c b/src/alloc.c
index 402fada..3b5e3bb 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -21,6 +21,8 @@ along with GNU Emacs.  If not, see 
<https://www.gnu.org/licenses/>.  */
 #include <config.h>
 
 #include <errno.h>
+#include <stdalign.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <limits.h>            /* For CHAR_BIT.  */
@@ -1578,9 +1580,9 @@ static struct Lisp_String *string_free_list;
    "cookie" after each allocated string data block, and check for the
    presence of this cookie during GC.  */
 
-#define GC_STRING_OVERRUN_COOKIE_SIZE  4
+#define GC_STRING_OVERRUN_COOKIE_SIZE  8
 static char const string_overrun_cookie[GC_STRING_OVERRUN_COOKIE_SIZE] =
-  { '\xde', '\xad', '\xbe', '\xef' };
+  { '\xde', '\xad', '\xbe', '\xef', '\xde', '\xad', '\xbe', '\xef' };
 
 #else
 #define GC_STRING_OVERRUN_COOKIE_SIZE 0
@@ -1616,6 +1618,11 @@ static char const 
string_overrun_cookie[GC_STRING_OVERRUN_COOKIE_SIZE] =
 
 #define GC_STRING_EXTRA (GC_STRING_OVERRUN_COOKIE_SIZE)
 
+/* Make sure that allocating the extra bytes doesn't misalign
+   `sdata'.  */
+
+verify (GC_STRING_EXTRA % alignof (sdata) == 0);
+
 /* Exact bound on the number of bytes in a string, not counting the
    terminating NUL.  A string cannot contain more bytes than
    STRING_BYTES_BOUND, nor can it be so long that the size_t
@@ -1875,6 +1882,7 @@ allocate_string_data (struct Lisp_String *s,
 
   data->string = s;
   b->next_free = (sdata *) ((char *) data + needed + GC_STRING_EXTRA);
+  eassert ((uintptr_t) (char *) b->next_free % alignof (sdata) == 0);
 
   MALLOC_UNBLOCK_INPUT;
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]