public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/108448] New: GCC Elides Assignment to Pointer and memcpy
@ 2023-01-18 18:09 gavin at yzena dot com
  2023-01-18 18:15 ` [Bug c/108448] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: gavin at yzena dot com @ 2023-01-18 18:09 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108448
           Summary: GCC Elides Assignment to Pointer and memcpy
           Product: gcc
           Version: 11.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gavin at yzena dot com
  Target Milestone: ---
              Host: x86_64
            Target: x86_64

I have a stack allocator ("stackpool") that can automatically free all data
allocated after entry to a function. To do this, I pass in a pointer to the
function name to be the marker for the function start.

To debug, I also check the function name on exit.

This last item is segfaulting because the function name pointer is NULL. I ran
it through GDB, and the assignment to the allocated pointer ([1]) is skipped
entirely, but only when not compiling in plain Debug mode (CMake). I tried a
memcpy() ([2]), but it is *also* skipped.

I checked for undefined behavior with UBSan. It didn't catch any. I fully
acknowledge that there could be some, though.

To reproduce, run the following:

```
$ git clone https://git.yzena.com/Yzena/Yc.git
$ cd Yc
$ git config --local include.path ./.gitconfig
$ git submodule update --init --recursive
$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_C_COMPILER=<path/to/gcc>
-DYC_ENABLE_STACKTRACES=ON -DYC_ENABLE_LONG_TESTS=ON -DYC_BUILD_DOCS=OFF ..
$ make -j<ncores>
```

If the build fails, you have successfully reproduced the problem because there
is a program that uses that code used as part of the build (to generate a test
file). The program is `tests/strgen` (produced from `tests/strgen.c`), and the
command-line it runs is something like:

```
$ tests/container/../strgen tests/container/kjvbible.txt
tests/container/kjvbible.c kjvbible_array 1
```

To pre-emptively answer some questions: the function name pointer that is
passed in to `y_stackpool_enterFunc()` (the one with the elided assignment) is
not NULL, and neither is the stackpool.

But again, I wouldn't be surprised if there's some UB somewhere that is giving
GCC the "right" to elide this assignment. I just can't find where because I'm
not as expert in C as the GCC authors.

[1]:
https://git.yzena.com/Yzena/Yc/src/commit/6afdc86bd2c17f98b2f9e97e79e37fdf8c6b7708/src/alloc/stackpool.c#L441

[2]:
https://git.yzena.com/Yzena/Yc/src/commit/c9a855a0b6d9c5758e4d605977bdf571830132a2/src/alloc/stackpool.c#L442-L443

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-01-23 22:33 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-18 18:09 [Bug c/108448] New: GCC Elides Assignment to Pointer and memcpy gavin at yzena dot com
2023-01-18 18:15 ` [Bug c/108448] " pinskia at gcc dot gnu.org
2023-01-18 18:16 ` [Bug middle-end/108448] " pinskia at gcc dot gnu.org
2023-01-18 18:22 ` gavin at yzena dot com
2023-01-18 18:29 ` pinskia at gcc dot gnu.org
2023-01-18 18:44 ` gavin at yzena dot com
2023-01-19  5:51 ` gavin at yzena dot com
2023-01-19  6:03 ` pinskia at gcc dot gnu.org
2023-01-19 15:29 ` gavin at yzena dot com
2023-01-19 15:36 ` gavin at yzena dot com
2023-01-23 22:33 ` gavin at yzena dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).