Bug 78668 - aligned_alloc, realloc, et al. missing attribute alloc_size
Summary: aligned_alloc, realloc, et al. missing attribute alloc_size
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: Martin Sebor
URL:
Keywords: missed-optimization
Depends on:
Blocks: 78284
  Show dependency treegraph
 
Reported: 2016-12-03 23:56 UTC by Martin Sebor
Modified: 2016-12-04 17:52 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-12-03 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2016-12-03 23:56:32 UTC
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 ();
Comment 1 Martin Sebor 2016-12-03 23:57:18 UTC
I have a patch.
Comment 2 Martin Sebor 2016-12-04 17:49:16 UTC
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
Comment 3 Martin Sebor 2016-12-04 17:52:04 UTC
Fixed by r243231.