diff options
| author | Philipp Stephani | 2019-04-25 21:28:46 +0200 |
|---|---|---|
| committer | Philipp Stephani | 2019-04-25 21:30:12 +0200 |
| commit | fe6c507f5ce0fd744b5bd1d0db6ea175e1188a7f (patch) | |
| tree | f55da6d44458911f708c720ca88df4fec55ad005 /src | |
| parent | e08e0880f9892fba747abdb95b1f3382ebd17e32 (diff) | |
| download | emacs-fe6c507f5ce0fd744b5bd1d0db6ea175e1188a7f.tar.gz 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.
Diffstat (limited to 'src')
| -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/>. */ | |||
| 21 | #include <config.h> | 21 | #include <config.h> |
| 22 | 22 | ||
| 23 | #include <errno.h> | 23 | #include <errno.h> |
| 24 | #include <stdalign.h> | ||
| 25 | #include <stdint.h> | ||
| 24 | #include <stdio.h> | 26 | #include <stdio.h> |
| 25 | #include <stdlib.h> | 27 | #include <stdlib.h> |
| 26 | #include <limits.h> /* For CHAR_BIT. */ | 28 | #include <limits.h> /* For CHAR_BIT. */ |
| @@ -1578,9 +1580,9 @@ static struct Lisp_String *string_free_list; | |||
| 1578 | "cookie" after each allocated string data block, and check for the | 1580 | "cookie" after each allocated string data block, and check for the |
| 1579 | presence of this cookie during GC. */ | 1581 | presence of this cookie during GC. */ |
| 1580 | 1582 | ||
| 1581 | #define GC_STRING_OVERRUN_COOKIE_SIZE 4 | 1583 | #define GC_STRING_OVERRUN_COOKIE_SIZE 8 |
| 1582 | static char const string_overrun_cookie[GC_STRING_OVERRUN_COOKIE_SIZE] = | 1584 | static char const string_overrun_cookie[GC_STRING_OVERRUN_COOKIE_SIZE] = |
| 1583 | { '\xde', '\xad', '\xbe', '\xef' }; | 1585 | { '\xde', '\xad', '\xbe', '\xef', '\xde', '\xad', '\xbe', '\xef' }; |
| 1584 | 1586 | ||
| 1585 | #else | 1587 | #else |
| 1586 | #define GC_STRING_OVERRUN_COOKIE_SIZE 0 | 1588 | #define GC_STRING_OVERRUN_COOKIE_SIZE 0 |
| @@ -1616,6 +1618,11 @@ static char const string_overrun_cookie[GC_STRING_OVERRUN_COOKIE_SIZE] = | |||
| 1616 | 1618 | ||
| 1617 | #define GC_STRING_EXTRA (GC_STRING_OVERRUN_COOKIE_SIZE) | 1619 | #define GC_STRING_EXTRA (GC_STRING_OVERRUN_COOKIE_SIZE) |
| 1618 | 1620 | ||
| 1621 | /* Make sure that allocating the extra bytes doesn't misalign | ||
| 1622 | `sdata'. */ | ||
| 1623 | |||
| 1624 | verify (GC_STRING_EXTRA % alignof (sdata) == 0); | ||
| 1625 | |||
| 1619 | /* Exact bound on the number of bytes in a string, not counting the | 1626 | /* Exact bound on the number of bytes in a string, not counting the |
| 1620 | terminating NUL. A string cannot contain more bytes than | 1627 | terminating NUL. A string cannot contain more bytes than |
| 1621 | STRING_BYTES_BOUND, nor can it be so long that the size_t | 1628 | STRING_BYTES_BOUND, nor can it be so long that the size_t |
| @@ -1875,6 +1882,7 @@ allocate_string_data (struct Lisp_String *s, | |||
| 1875 | 1882 | ||
| 1876 | data->string = s; | 1883 | data->string = s; |
| 1877 | b->next_free = (sdata *) ((char *) data + needed + GC_STRING_EXTRA); | 1884 | b->next_free = (sdata *) ((char *) data + needed + GC_STRING_EXTRA); |
| 1885 | eassert ((uintptr_t) (char *) b->next_free % alignof (sdata) == 0); | ||
| 1878 | 1886 | ||
| 1879 | MALLOC_UNBLOCK_INPUT; | 1887 | MALLOC_UNBLOCK_INPUT; |
| 1880 | 1888 | ||