diff options
author | Philipp Stephani <phst@google.com> | 2019-04-25 21:28:46 +0200 |
---|---|---|
committer | Philipp Stephani <phst@google.com> | 2019-04-25 21:30:12 +0200 |
commit | fe6c507f5ce0fd744b5bd1d0db6ea175e1188a7f (patch) | |
tree | f55da6d44458911f708c720ca88df4fec55ad005 | |
parent | e08e0880f9892fba747abdb95b1f3382ebd17e32 (diff) | |
download | emacs-fe6c507f5ce0fd744b5bd1d0db6ea175e1188a7f.tar.gz emacs-fe6c507f5ce0fd744b5bd1d0db6ea175e1188a7f.tar.bz2 emacs-fe6c507f5ce0fd744b5bd1d0db6ea175e1188a7f.zip |
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.
-rw-r--r-- | src/alloc.c | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/alloc.c b/src/alloc.c index 402fada1ad2..3b5e3bb9b01 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; |