All built-in memory allocations functions are missing the aligned_alloc attribute. As the output of the test case below shows, the absence of the attribute on aligned_alloc and realloc causes __builtin_object_size to fail to determine the size of objects they allocate. $ cat a.c && gcc -O2 -S -Wall -Wextra -Wpedantic -fdump-tree-optimized=/dev/stdout a.c | grep -e ^test -eabort void sink (void*); unsigned size (unsigned n) { return n; } void test_aligned_alloc (unsigned a) { unsigned n = size (7); void *p = __builtin_aligned_alloc (n, a); if (__builtin_object_size (p, 0) != n) __builtin_abort (); sink (p); } void test_alloca (void) { unsigned n = size (13); void *p = __builtin_alloca (n); if (__builtin_object_size (p, 0) != n) __builtin_abort (); sink (p); } void test_calloc (void) { unsigned m = size (19); unsigned n = size (23); void *p = __builtin_calloc (m, n); if (__builtin_object_size (p, 0) != m * n) __builtin_abort (); sink (p); } void test_malloc (void) { unsigned n = size (17); void *p = __builtin_malloc (n); if (__builtin_object_size (p, 0) != n) __builtin_abort (); sink (p); } void test_realloc (void *p) { unsigned n = size (31); p = __builtin_realloc (p, n); if (__builtin_object_size (p, 0) != n) __builtin_abort (); sink (p); } test_aligned_alloc (unsigned int a) __builtin_abort (); test_alloca () test_calloc () test_malloc () test_realloc (void * p) __builtin_abort ();
I have a patch.
Author: msebor Date: Sun Dec 4 17:48:44 2016 New Revision: 243231 URL: https://gcc.gnu.org/viewcvs?rev=243231&root=gcc&view=rev Log: PR c/78668 - aligned_alloc, realloc, et al. missing attribute alloc_size gcc/ChangeLog: PR c/78668 * builtin-attrs.def (ATTR_ALLOC_SIZE, ATTR_RETURNS_NONNULL): New identifier tree nodes. (ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): New attribute list. (ATTR_MALLOC_SIZE_1_NOTHROW_LIST): Same. (ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Same. (ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST): Same. (ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Same. * builtins.def (aligned_alloc, calloc, malloc, realloc): Add attribute alloc_size. (alloca): Add attribute alloc_size and returns_nonnull. gcc/testsuite/ChangeLog: PR c/78668 * gcc.dg/builtin-alloc-size.c: New test. Added: trunk/gcc/testsuite/gcc.dg/builtin-alloc-size.c Modified: trunk/gcc/ChangeLog trunk/gcc/builtin-attrs.def trunk/gcc/builtins.def trunk/gcc/testsuite/ChangeLog
Fixed by r243231.