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

            Bug ID: 129070
           Summary: [16/17 Regression] Bogus -Wstringop-overflow: thread2
                    isolates impossible null path, memset (0B, 0, SIZE_MAX
                    * 8)
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: will at 256k1 dot dev
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

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

GCC 16.1 and 16.2 emit a false-positive -Wstringop-overflow at -O2 (16.2.0
also at -O3). GCC 14.4.0 and 15.3.0 do not warn. No headers are needed.

Target: x86_64-linux-gnu. Reproduced with 16.1 and 16.2 on Compiler Explorer,
and with a NixOS build of 16.2.0. Trunk not tested.

=== Testcase (t.cpp) ===

typedef __SIZE_TYPE__ size_t;
struct node { node *prior; };

// Largest entry is 2^64 - 59, so sizes[i] + 1 is never 0.
extern const size_t sizes[4] = {53, 97, 193, 18446744073709551557ul};

struct space {
  size_t n;
  node *data;
  explicit space(size_t k)
      : n(k), data(n ? static_cast<node *>(::operator new(n * sizeof(node))) :
nullptr) {}
  ~space() { if (n) ::operator delete(data); }
};

struct buckets {
  size_t index;
  space spc;
  explicit buckets(size_t i) : index(i), spc(sizes[index] + 1) {
    for (node *x = spc.data, *y = spc.data + sizes[index]; x != y; ++x)
      x->prior = nullptr;
  }
};

struct table {
  buckets b;
  size_t max_load;
  explicit table(size_t i) : b(i) {
    float fml = static_cast<float>(sizes[b.index]);
    max_load = ~size_t(0);
    if (max_load > fml) max_load = static_cast<size_t>(fml);
  }
};

size_t pick();  // returns 0..2 in practice
table *f() { return new table(pick()); }

=== Output ===

$ g++ -std=c++20 -O2 -Wall -c t.cpp
In constructor 'buckets::buckets(size_t)',
    inlined from 'table::table(size_t)' at t.cpp:27:30,
    inlined from 'table* f()' at t.cpp:35:37:
t.cpp:20:16: warning: 'void* __builtin_memset(void*, int, long unsigned int)'
specified bound 18446744073709551608 exceeds maximum object size
9223372036854775807 [-Wstringop-overflow=]
   20 |       x->prior = nullptr;
      |       ~~~~~~~~~^~~~~~~~~

Expected: no warning.

=== Analysis ===

The bound is SIZE_MAX * 8 modulo 2^64. It belongs only to the path where
sizes[index] + 1 == 0. No entry of the constant table has that value.

Dumps from 16.2.0 at -O2:

  175t.ldist:    __builtin_memset (iftmp.0_8, 0, _11);
  211t.thread2:  __builtin_memset (0B, 0, _39);
                 __builtin_memset (iftmp.0_8, 0, _11);
  212t.dom3:     __builtin_memset (0B, 0, 18446744073709551608);
                 __builtin_memset (_26, 0, _11);

ldist turns the loop into memset, thread2 duplicates it onto the null path, and
dom3 folds the bound there. With 15.3.0 the thread2 dump has only one memset.

Results with 16.2.0 at -O2 unless noted:

  -O2, -O3                                  warns
  -O1                                       no warning
  -fno-thread-jumps                         no warning
  -fno-tree-loop-distribute-patterns        no warning
  -fno-tree-dominator-opts                  warns
  -fno-tree-vrp                             warns
  -fno-exceptions                           warns
  "x < y" instead of "x != y"               no warning
  constructing buckets without table        no warning

With pick() returning 2, the program runs clean under
-fsanitize=address,undefined.

=== Origin ===

Reduced by hand from Boost.MultiIndex (bucket_array / auto_space), where it
breaks a Bitcoin Core -Werror build.

Possibly related: PR 119946 (folding memset of a null pointer), which might
remove the bad call seen in thread2; and PR 109995 and PR 100477 (bogus memset
bounds on wraparound paths), both reported against older releases. This
testcase is clean in 14.4 and 15.3.

Reply via email to