https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129069

            Bug ID: 129069
           Summary: CCP folds __builtin_alloca_with_align executed
                    repeatedly in one block into a single array
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xhxxh at mail dot nwpu.edu.cn
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu
             Build: x86_64-pc-linux-gnu

Created attachment 66299
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66299&action=edit
testcase

fold_builtin_alloca_with_align in tree-ssa-ccp.cc replaces a call with a
constant size by the address of a single fixed-size local array.  This is
only correct if, at any time, at most one object allocated by that call is
live.  When the same call is executed several times without leaving its
enclosing block (here via a backward goto in the function body), each
execution allocates a new object.  The documentation says these objects
stay live until the end of the enclosing block:

  "The lifetime of the allocated object ends at the end of the block in
  which the function was called. The allocated storage is released no
  later than just before the calling function returns to its caller, but
  may be released at the end of the block in which the function was
  called."

After folding, all executions return the same address, so live objects
overlap and later stores clobber earlier ones.

Testcase (no headers, so it is its own preprocessed source):

/* Each execution of the call allocates a distinct object whose lifetime
   extends to the end of the enclosing block (here: the function body),
   so p[0] and p[1] must not overlap.  */

__attribute__((noipa)) int
f (void)
{
  int *p[2];
  int i = 0;
again:
  p[i] = __builtin_alloca_with_align (sizeof (int), 32);
  *p[i] = i;
  if (++i < 2)
    goto again;
  return *p[0];
}

int
main (void)
{
  if (f () != 0)
    __builtin_abort ();
  return 0;
}

$ gcc -O1 pr.c && ./a.out
Aborted

gcc -v:

Using built-in specs.
COLLECT_GCC=/opt/gcc17/bin/gcc
COLLECT_LTO_WRAPPER=/opt/gcc17/libexec/gcc/x86_64-pc-linux-gnu/17.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc/configure --prefix=/opt/gcc17 --enable-languages=c
--disable-bootstrap --disable-multilib --enable-checking=release
--disable-libsanitizer --disable-libquadmath --disable-libgomp --without-isl
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 17.0.0 20261108 (experimental) (GCC)

Results (x86_64-pc-linux-gnu):

gcc 17.0.0 20261108 (experimental)
  -O0                     ok
  -O1 / -O2 / -O3 / -Os   abort
  -O1 -fno-tree-ccp       ok
  -O2 -fno-tree-ccp       ok

gcc 12.2.0
  -O0                     ok
  -O1 / -O2               abort
  -O1 -fno-tree-ccp       ok

No warnings with -Wall -Wextra at -O0/-O1/-O2.  With gcc 17, still aborts
with -O1/-O2 -fno-strict-aliasing -fwrapv.

-fdump-tree-ccp1 shows that the call has been replaced by the address of a
single array, which is stored on every iteration:

  unsigned char D.2988[4];
  ...
again:
  _1 = &D.2988;
  p[i_4] = _1;
  _2 = p[i_4];
  *_2 = i_4;
  i_11 = i_4 + 1;
  if (i_11 <= 1) goto <bb 4>; else goto <bb 5>;
  ...
  _3 = p[0];
  _12 = *_3;
  return _12;

The same happens with __builtin_alloca_with_align_and_max.  It also happens
with other shapes, as long as the call is executed more than once before its
block ends: different sizes and alignments, the pointers kept in a static
array, or the result compared with == (the comparison folds from false to
true).

Note that the analogous code with a loop body block, e.g.

  for (i = 0; i < 2; i++)
    p[i] = __builtin_alloca_with_align (sizeof (int), 32);

followed by uses of p[0] after the loop, is invalid (the loop body is a
block, so the objects are dead after each iteration).  The bug is specific
to repeated execution within the *same* block, which C permits via goto.
Ordinary VLAs are not affected, because jumping back before a VLA
declaration leaves its scope and ends the lifetime of the previous instance.

Reply via email to