(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
builtin-alloc-size.c
       1  /* PR c/78668 - aligned_alloc, realloc, et al. missing attribute alloc_size
       2     Test to verify that memory allocation built-ins are decorated with
       3     attribute alloc_size that __builtin_object_size can make use of (or
       4     are treated as if they were for that purpose)..
       5     { dg-do compile }
       6     { dg-require-effective-target alloca }
       7     { dg-additional-options "-O2 -fdump-tree-optimized" } */
       8  
       9  void sink (void*);
      10  
      11  static unsigned size (unsigned n)
      12  {
      13    return n;
      14  }
      15  
      16  void test_aligned_alloc (unsigned a)
      17  {
      18    unsigned n = size (7);
      19  
      20    void *p = __builtin_aligned_alloc (a, n);
      21    if (__builtin_object_size (p, 0) != n)
      22      __builtin_abort ();
      23    sink (p);
      24  }
      25  
      26  void test_alloca (void)
      27  {
      28    unsigned n = size (13);
      29  
      30    void *p = __builtin_alloca (n);
      31  
      32    /* Also verify that alloca is declared with attribute returns_nonnull
      33       (or treated as it were as the case may be).  */
      34    if (!p)
      35      __builtin_abort ();
      36  
      37    if (__builtin_object_size (p, 0) != n)
      38      __builtin_abort ();
      39    sink (p);
      40  }
      41  
      42  void test_calloc (void)
      43  {
      44    unsigned m = size (19);
      45    unsigned n = size (23);
      46  
      47    void *p = __builtin_calloc (m, n);
      48    if (__builtin_object_size (p, 0) != m * n)
      49      __builtin_abort ();
      50    sink (p);
      51  }
      52  
      53  void test_malloc (void)
      54  {
      55    unsigned n = size (17);
      56  
      57    void *p = __builtin_malloc (n);
      58    if (__builtin_object_size (p, 0) != n)
      59      __builtin_abort ();
      60    sink (p);
      61  }
      62  
      63  void test_realloc (void *p)
      64  {
      65    unsigned n = size (31);
      66  
      67    p = __builtin_realloc (p, n);
      68    if (__builtin_object_size (p, 0) != n)
      69      __builtin_abort ();
      70    sink (p);
      71  }
      72  
      73  /* { dg-final { scan-tree-dump-not "abort" "optimized" } } */