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

            Bug ID: 120417
           Summary: gcc -m32 -O1 codegen error, leading to SIGSEGV,
                    workaround -fno-tree-coalesce-vars
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: simonsobisch at gnu dot org
  Target Milestone: ---

Created attachment 61501
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=61501&action=edit
generated sources, cleaned up, showing this issue

This was validated to also happened with gcc-9 to gcc-12 and validated to
haopen on multiple cpus and operating systems. 

If compiled with -O1 -fno-tree-coalesce-vars the program runs as expected and
returns zero.

If ran with only -O1 the following result is seen (this is linked against
libcob from GnuCOBOL 3.2+, but the problematic code is all in the attached
code)

caller.cob:4: attempt to reference invalid memory address (signal SIGSEGV)


The reason is that the second command reorders the executions and executes line
152 (which accesses cob_glob_ptr), then does some init, then 163 (where the
cob_glob_ptr is unset -> SIGSEGV).

It seems that's an issue with multiple optimizations as
-O0 -ftree-coalesce-vars
does not show the error.

The biggest problem here is that -FORTIFY_SOURCE=2 needs -O1 on many setups and
therefore "fortification breaks codegen" :-/


valgrind with -fno-tree-coalesece-vars runs through; if not used

==127523== Memcheck, a memory error detector
==127523== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==127523== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==127523== Command: ./a.out
==127523== 
==127523== Invalid write of size 4
==127523==    at 0x1093C9: caller_ (caller.c:163)
==127523==    by 0x109581: caller (caller.c:60)
==127523==    by 0x1095B1: main (caller.c:42)
==127523==  Address 0x40 is not stack'd, malloc'd or (recently) free'd
==127523== 

caller.cob:5: attempt to reference invalid memory address (signal SIGSEGV)


Test setuo:
* get the attached files
* get GnuCOBOL 3.3 dev (either from VCS checkout or a pre-built tarball [1])
* configure/make, then either
  * install  and gcc -m32 -O1 caller.c callee.c -g -lcob && ./a.out 
  * or:   gcc -m32 -O1 caller.c callee.c -g libcob/.libs/libcob.so && ./a.out 

as noted, the error vanishes if either -O1 is not used or
-fno-tree-coalesce-vars is added

Side note: if you see a better way of code-generation: feel free to send me a
direct mail.

[1]:
https://ci.appveyor.com/api/projects/GitMensch/gnucobol-3-x/artifacts/gnucobol-3.3-dev.tar.gz?job=Image:%20Ubuntu2204

Reply via email to