[Bug tree-optimization/119070] gcc15 incorrectly reporting negative array-bounds errors

2025-02-28 Thread taylor.hutt at broadcom dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119070

--- Comment #4 from Taylor Hutt  ---
(In reply to Andrew Pinski from comment #3)
> I don't see anything wrong with the warning since it is undefined in C to
> what this testcase is trying to do.
> 
> Do you have a full source rather than a reduced one? Because the reduced one
> is definitely undefined code.

Can you point to the section in the language ref for this?  Pointer arithmetic?


Sorry, I cannot provide the whole file; it's part of a commercial
virtualization product.

It's interfacing with (possibly proprietary virtual) hardware via a
specification (which I have never seen).  The subtraction of 0x2000 from the
pointer is, apparently, to access specific hardware.

If the operations are undefined, how should one interface directly with
hardware without having code broken by UB?

[Bug c/119070] gcc15 incorrectly reporting negative array-bounds errors

2025-02-28 Thread taylor.hutt at broadcom dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119070

--- Comment #1 from Taylor Hutt  ---
Created attachment 60616
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=60616&action=edit
C file that demonstrates errors.

[Bug tree-optimization/119070] gcc15 incorrectly reporting negative array-bounds errors

2025-03-03 Thread taylor.hutt at broadcom dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119070

--- Comment #7 from Taylor Hutt  ---
(In reply to Andrew Pinski from comment #6)
> You could do:
> 
>struct_1  *v1 = &global_0.f_2_0;
>asm("":"+r"(v1));
>unsigned char *v2 = (unsigned char *)v1;
> 
> to hide from GCC that the address of v2 is related to a global variable.
> And that should get rid of the warning too.
> 
> But otherwise this is undefined code.

Why not cast the pointer to uintptr_t at the point of the undefined behavior
pointer arithmetic?

[Bug c/119070] gcc15 incorrectly reporting negative array-bounds errors

2025-02-28 Thread taylor.hutt at broadcom dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119070

--- Comment #2 from Taylor Hutt  ---
Created attachment 60617
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=60617&action=edit
Shell script to compile C file.

[Bug c/119070] New: gcc15 incorrectly reporting negative array-bounds errors

2025-02-28 Thread taylor.hutt at broadcom dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119070

Bug ID: 119070
   Summary: gcc15 incorrectly reporting negative array-bounds
errors
   Product: gcc
   Version: 15.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
  Assignee: unassigned at gcc dot gnu.org
  Reporter: taylor.hutt at broadcom dot com
  Target Milestone: ---

The code in 'cit.c' has been extracted from a production audio driver
present in widely used virtualization products.

The code has not been meaningfully changed in many years.

The gcc version banner is:

  x86_64-vmw-linux-gnu-gcc (crosstool-NG 1.26.0.127_1c395b2) 15.0.1 20250227
(experimental)
  Copyright (C) 2025 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The code was checked out using crosstool-ng at the top of the 'master'
branch on the day of the build.


The script 'citi' (with modifications to produce a path to gcc15) will
reproduce the errors.  The errors are as follows:


  /tmp/gcc15/cit.c: In function 'function_1':
  /tmp/gcc15/cit.c:27:19: error: array subscript -227 is outside array bounds
of 'struct_2[1]' [-Werror=array-bounds=]
 27 |unsigned char *v3 = v2 - 0x2000;
|   ^~
  /tmp/gcc15/cit.c:16:17: note: at offset -8192 into object 'global_0' of size
36
 16 | extern struct_2 global_0;
| ^~~~
  /tmp/gcc15/cit.c:27:19: error: array subscript -227 is outside array bounds
of 'struct_2[1]' [-Werror=array-bounds=]
 27 |unsigned char *v3 = v2 - 0x2000;
|   ^~
  /tmp/gcc15/cit.c:16:17: note: at offset -8192 into object 'global_0' of size
36
 16 | extern struct_2 global_0;
| ^~~~
  /tmp/gcc15/cit.c:27:19: error: array subscript -227 is outside array bounds
of 'struct_2[1]' [-Werror=array-bounds=]
 27 |unsigned char *v3 = v2 - 0x2000;
|   ^~
  /tmp/gcc15/cit.c:16:17: note: at offset -8192 into object 'global_0' of size
36
 16 | extern struct_2 global_0;
| ^~~~
  cc1: all warnings being treated as errors


I find, using https://godbolt.org/, that the regression was introduced
in gcc10.  The code compiles without issue with gcc9.