Issue #18062 has been updated by jaruga (Jun Aruga).

File jaruga_make_check.log added
File jaruga_make_cflags_with_O2_lto_flags.log added

I identified which compiler flags make this error, segmentation fault. I tested it on the latest master commit:28d03ee776af4d630556fcaad7305340cbcf9cde . I think this info helps for developers to reproduce this error on their machine.

My testing environment is Fedora 34 x86_64 and the used gcc version is as follows.

```
$ gcc --version
gcc (GCC) 11.2.1 20210728 (Red Hat 11.2.1-1)
Copyright (C) 2021 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.
```

As a note, on Fedora, the following cflags is set as a default for both gcc and clang.
https://src.fedoraproject.org/rpms/redhat-rpm-config/blob/rawhide/f/macros#_341

```
%_gcc_lto_cflags -flto=auto -ffat-lto-objects
%_clang_lto_cflags -flto
...
%_general_options -O2 %{?_lto_cflags} -fexceptions -g -grecord-gcc-switches -pipe
```

### `CFLAGS='-flto=auto -ffat-lto-objects'` => `make`: ok

First, I confirmed that with `CFLAGS='-flto=auto -ffat-lto-objects'`, the `make` command works and finishes without error against my expectation.

```
$ git clean -fdx
$ autoconf
$ CFLAGS='-flto=auto -ffat-lto-objects' ./configure --enable-shared --prefix $(pwd)/dest
$ make 2>&1 | tee make.log
```
### `CFLAGS='-O2 -flto=auto -ffat-lto-objects'` => `make`: error

After spending some time, I found the `CFLAGS='-O2 -flto=auto -ffat-lto-objects'` (-O2 + lto flags) causes the `make` error. I would attach the log as `jaruga_make_cflags_with_O2_lto_flags.log`.

```
$ git clean -fdx
$ autoconf
$ CFLAGS='-O2 -flto=auto -ffat-lto-objects' \
  ./configure --enable-shared --prefix $(pwd)/dest
$ make 2>&1 | tee make.log
...
+ cp enc/jis/props.h.blt enc/jis/props.h
generating encdb.h
<internal:gc>:240: [BUG] Segmentation fault at 0x0000000000000004
ruby 3.1.0dev (2021-08-10T10:08:38Z master 28d03ee776) [x86_64-linux]
...
  => error
```

### `CFLAGS='-O2 -flto=auto -ffat-lto-objects'` + a commit on the PR https://github.com/ruby/ruby/pull/4716 => `make`: ok, `make check`: error

On the latest master commit:28d03ee776af4d630556fcaad7305340cbcf9cde + applying a commit on the PR https://github.com/ruby/ruby/pull/4716 , the `make` is ok, but `make check` still has error. I would attach the log as `jaruga_make_check.log.

```
$ git clean -fdx
$ autoconf
$ CFLAGS='-O2 -flto=auto -ffat-lto-objects' \
  ./configure --enable-shared --prefix $(pwd)/dest
$ make 2>&1 | tee make.log
$ make check 2>&1 | tee make_check.log
...
Run options:-
  --seed=91697
  "--ruby=./miniruby -I./lib -I. -I.ext/common  ./tool/runruby.rb --extout=.ext  -- --disable-gems"
  --excludes-dir=./test/excludes
  --name=!/memory_leak/

# Running tests:

Leaked file descriptor: DRbTests::TestDRbTCP#test_immediate_close: 12 : #<TCPSocket:fd 12, AF_INET6, ::1, 38855>
Leaked file descriptor: DRbTests::TestDRbTCP#test_immediate_close: 13 : #<IO:fd 13>
Leaked file descriptor: PPTestModule::PPFileStatTest#test_nothing_raised: 7 #<File::Stat dev=0xfd02, ino=13370094, mode=0100664, nlink=1, uid=0, gid=0, rdev=0x0, size=9253600, blksize=4096, blocks=18080, atime=2021-08-10 19:54:31.33281203 +0200, mtime=2021-08-10 19:54:31.331812023 +0200, ctime=2021-08-10 19:54:31.331812023 +0200>
COMMAND     PID   USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME 
ruby    1158696 jaruga    7r   REG  253,2  9253600 13370094 /var/lib/sss/mc/passwd
/home/jaruga/var/git/ruby/ruby/test/ruby/test_hash.rb:2080: [BUG] Segmentation fault at 0x0000000000000003
ruby 3.1.0dev (2021-08-10T17:41:00Z master 5340724165) [x86_64-linux]
...
  => error
```



----------------------------------------
Bug #18062: Ruby with enabled LTO segfaults during build
https://bugs.ruby-lang.org/issues/18062#change-93222

* Author: vo.x (Vit Ondruch)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: REQUIRED
----------------------------------------
After a while, I am trying to enable LTO for Ruby in Fedora. Previously I hit #17052, but now I observe issues much earlier during compliation already:

~~~
./miniruby -I./lib -I. -I.ext/common  ./tool/generic_erb.rb -c -o encdb.h ./template/encdb.h.tmpl ./enc enc
<internal:gc>:240: [BUG] Segmentation fault at 0x0000000000000004
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0003 p:0008 s:0011 e:000008 CLASS  <internal:gc>:240
c:0002 p:0015 s:0006 e:000005 TOP    <internal:gc>:235 [FINISH]
c:0001 p:0000 s:0003 E:000230 (none) [FINISH]
-- Ruby level backtrace information ----------------------------------------
<internal:gc>:235:in `<internal:gc>'
<internal:gc>:240:in `<module:ObjectSpace>'
-- Machine register context ------------------------------------------------
 RIP: 0x00005585da1c832a RBP: 0x000022f100140003 RSP: 0x00007ffd6460a6a0
 RAX: 0x0000000000000001 RBX: 0x00000000000022f1 RCX: 0x00005585dade5d90
 RDX: 0x000022f100140003 RDI: 0x00005585dae0c2d8 RSI: 0x0000000000000000
  R8: 0x0000000000000000  R9: 0x00007ffd6460a6e0 R10: 0x00005585daebbe50
 R11: 0x00007f00a785baa0 R12: 0x0000000000000000 R13: 0x00005585dade5d90
 R14: 0x00005585dae0c2d8 R15: 0x00005585dade5d90 EFL: 0x0000000000010202
-- C level backtrace information -------------------------------------------
./miniruby(rb_print_backtrace+0x19) [0x5585da1cdba9]
./miniruby(rb_vm_bugreport+0x215) [0x5585da1d1a05]
./miniruby(rb_bug_for_fatal_signal+0xf4) [0x5585da004fc4]
./miniruby(sigsegv+0x4d) [0x5585da13709d]
[0x7f00a76a4760]
./miniruby(vm_ccs_push.part.0.lto_priv.0+0x2a) [0x5585da1c832a]
./miniruby(vm_search_cc+0xfe) [0x5585da1a7b7e]
./miniruby(vm_search_method_slowpath0.lto_priv.0+0x40) [0x5585da1a7d30]
./miniruby(vm_sendish.lto_priv.0+0x6e4) [0x5585da1aeff4]
./miniruby(vm_exec_core.lto_priv.0+0xa8) [0x5585da1b25b8]
./miniruby(rb_vm_exec+0x110) [0x5585da1c9ea0]
./miniruby(ruby_opt_init+0x6f) [0x5585da1355af]
./miniruby(load_file_internal+0x4b2) [0x5585da1376e2]
./miniruby(rb_ensure+0x106) [0x5585da00d5a6]
./miniruby(process_options+0x19e5) [0x5585da139a65]
./miniruby(ruby_process_options+0x151) [0x5585da13a551]
./miniruby(ruby_options+0x119) [0x5585da012de9]
./miniruby(main+0x57) [0x5585d9f8a1e7]
-- Other runtime information -----------------------------------------------
* Loaded script: ./miniruby
* Loaded features:
    0 enumerator.so
    1 thread.rb
    2 rational.so
    3 complex.so
    4 ruby2_keywords.rb
* Process memory map:
5585d9f5d000-5585d9f85000 r--p 00000000 fc:05 137001366                  /builddir/build/BUILD/ruby-3.0.2/miniruby
5585d9f85000-5585da1f2000 r-xp 00028000 fc:05 137001366                  /builddir/build/BUILD/ruby-3.0.2/miniruby
5585da1f2000-5585da2f6000 r--p 00295000 fc:05 137001366                  /builddir/build/BUILD/ruby-3.0.2/miniruby
5585da2f6000-5585da2fd000 r--p 00398000 fc:05 137001366                  /builddir/build/BUILD/ruby-3.0.2/miniruby
5585da2fd000-5585da2fe000 rw-p 0039f000 fc:05 137001366                  /builddir/build/BUILD/ruby-3.0.2/miniruby
5585da2fe000-5585da30e000 rw-p 00000000 00:00 0 
5585dad8d000-5585daed3000 rw-p 00000000 00:00 0                          [heap]
7f00a28ae000-7f00a409b000 r--s 00000000 fc:05 137001366                  /builddir/build/BUILD/ruby-3.0.2/miniruby
7f00a409b000-7f00a409e000 r--p 00000000 fc:05 270435464                  /usr/lib64/libgcc_s-11-20210728.so.1
7f00a409e000-7f00a40b0000 r-xp 00003000 fc:05 270435464                  /usr/lib64/libgcc_s-11-20210728.so.1
7f00a40b0000-7f00a40b3000 r--p 00015000 fc:05 270435464                  /usr/lib64/libgcc_s-11-20210728.so.1
7f00a40b3000-7f00a40b4000 r--p 00017000 fc:05 270435464                  /usr/lib64/libgcc_s-11-20210728.so.1
7f00a40b4000-7f00a40b5000 rw-p 00018000 fc:05 270435464                  /usr/lib64/libgcc_s-11-20210728.so.1
7f00a40b5000-7f00a40b6000 ---p 00000000 00:00 0 
7f00a40b6000-7f00a4157000 rw-p 00000000 00:00 0 
7f00a4157000-7f00a4158000 ---p 00000000 00:00 0 
7f00a4158000-7f00a41f9000 rw-p 00000000 00:00 0 
7f00a41f9000-7f00a41fa000 ---p 00000000 00:00 0 
7f00a41fa000-7f00a429b000 rw-p 00000000 00:00 0 
7f00a429b000-7f00a429c000 ---p 00000000 00:00 0 
7f00a429c000-7f00a433d000 rw-p 00000000 00:00 0 
7f00a433d000-7f00a433e000 ---p 00000000 00:00 0 
7f00a433e000-7f00a43df000 rw-p 00000000 00:00 0 
7f00a43df000-7f00a43e0000 ---p 00000000 00:00 0 
7f00a43e0000-7f00a4481000 rw-p 00000000 00:00 0 
7f00a4481000-7f00a4482000 ---p 00000000 00:00 0 
7f00a4482000-7f00a4523000 rw-p 00000000 00:00 0 
7f00a4523000-7f00a4524000 ---p 00000000 00:00 0 
7f00a4524000-7f00a45c5000 rw-p 00000000 00:00 0 
7f00a45c5000-7f00a45c6000 ---p 00000000 00:00 0 
7f00a45c6000-7f00a4667000 rw-p 00000000 00:00 0 
7f00a4667000-7f00a4668000 ---p 00000000 00:00 0 
7f00a4668000-7f00a4709000 rw-p 00000000 00:00 0 
7f00a4709000-7f00a470a000 ---p 00000000 00:00 0 
7f00a470a000-7f00a47ab000 rw-p 00000000 00:00 0 
7f00a47ab000-7f00a47ac000 ---p 00000000 00:00 0 
7f00a47ac000-7f00a484d000 rw-p 00000000 00:00 0 
7f00a484d000-7f00a484e000 ---p 00000000 00:00 0 
7f00a484e000-7f00a48ef000 rw-p 00000000 00:00 0 
7f00a48ef000-7f00a48f0000 ---p 00000000 00:00 0 
7f00a48f0000-7f00a4991000 rw-p 00000000 00:00 0 
7f00a4991000-7f00a4992000 ---p 00000000 00:00 0 
7f00a4992000-7f00a4a33000 rw-p 00000000 00:00 0 
7f00a4a33000-7f00a4a34000 ---p 00000000 00:00 0 
7f00a4a34000-7f00a4ad5000 rw-p 00000000 00:00 0 
7f00a4ad5000-7f00a4ad6000 ---p 00000000 00:00 0 
7f00a4ad6000-7f00a4b77000 rw-p 00000000 00:00 0 
7f00a4b77000-7f00a4b78000 ---p 00000000 00:00 0 
7f00a4b78000-7f00a4c19000 rw-p 00000000 00:00 0 
7f00a4c19000-7f00a4c1a000 ---p 00000000 00:00 0 
7f00a4c1a000-7f00a4cbb000 rw-p 00000000 00:00 0 
7f00a4cbb000-7f00a4cbc000 ---p 00000000 00:00 0 
7f00a4cbc000-7f00a4d5d000 rw-p 00000000 00:00 0 
7f00a4d5d000-7f00a4d5e000 ---p 00000000 00:00 0 
7f00a4d5e000-7f00a4dff000 rw-p 00000000 00:00 0 
7f00a4dff000-7f00a4e00000 ---p 00000000 00:00 0 
7f00a4e00000-7f00a4ea1000 rw-p 00000000 00:00 0 
7f00a4ea1000-7f00a4ea2000 ---p 00000000 00:00 0 
7f00a4ea2000-7f00a4f43000 rw-p 00000000 00:00 0 
7f00a4f43000-7f00a4f44000 ---p 00000000 00:00 0 
7f00a4f44000-7f00a4fe5000 rw-p 00000000 00:00 0 
7f00a4fe5000-7f00a4fe6000 ---p 00000000 00:00 0 
7f00a4fe6000-7f00a5087000 rw-p 00000000 00:00 0 
7f00a5087000-7f00a5088000 ---p 00000000 00:00 0 
7f00a5088000-7f00a5129000 rw-p 00000000 00:00 0 
7f00a5129000-7f00a512a000 ---p 00000000 00:00 0 
7f00a512a000-7f00a51cb000 rw-p 00000000 00:00 0 
7f00a51cb000-7f00a51cc000 ---p 00000000 00:00 0 
7f00a51cc000-7f00a526d000 rw-p 00000000 00:00 0 
7f00a526d000-7f00a526e000 ---p 00000000 00:00 0 
7f00a526e000-7f00a530f000 rw-p 00000000 00:00 0 
7f00a530f000-7f00a5310000 ---p 00000000 00:00 0 
7f00a5310000-7f00a53b1000 rw-p 00000000 00:00 0 
7f00a53b1000-7f00a53b2000 ---p 00000000 00:00 0 
7f00a53b2000-7f00a5453000 rw-p 00000000 00:00 0 
7f00a5453000-7f00a5454000 ---p 00000000 00:00 0 
7f00a5454000-7f00a7662000 rw-p 00000000 00:00 0 
7f00a7662000-7f00a768e000 r--p 00000000 fc:05 270939738                  /usr/lib64/libc.so.6
7f00a768e000-7f00a7803000 r-xp 0002c000 fc:05 270939738                  /usr/lib64/libc.so.6
7f00a7803000-7f00a7857000 r--p 001a1000 fc:05 270939738                  /usr/lib64/libc.so.6
7f00a7857000-7f00a7858000 ---p 001f5000 fc:05 270939738                  /usr/lib64/libc.so.6
7f00a7858000-7f00a785b000 r--p 001f5000 fc:05 270939738                  /usr/lib64/libc.so.6
7f00a785b000-7f00a785e000 rw-p 001f8000 fc:05 270939738                  /usr/lib64/libc.so.6
7f00a785e000-7f00a786b000 rw-p 00000000 00:00 0 
7f00a786b000-7f00a787a000 r--p 00000000 fc:05 270939741                  /usr/lib64/libm.so.6
7f00a787a000-7f00a78eb000 r-xp 0000f000 fc:05 270939741                  /usr/lib64/libm.so.6
7f00a78eb000-7f00a7945000 r--p 00080000 fc:05 270939741                  /usr/lib64/libm.so.6
7f00a7945000-7f00a7946000 r--p 000d9000 fc:05 270939741                  /usr/lib64/libm.so.6
7f00a7946000-7f00a7947000 rw-p 000da000 fc:05 270939741                  /usr/lib64/libm.so.6
7f00a7947000-7f00a7949000 r--p 00000000 fc:05 271023995                  /usr/lib64/libcrypt.so.2.0.0
7f00a7949000-7f00a795d000 r-xp 00002000 fc:05 271023995                  /usr/lib64/libcrypt.so.2.0.0
7f00a795d000-7f00a7976000 r--p 00016000 fc:05 271023995                  /usr/lib64/libcrypt.so.2.0.0
7f00a7976000-7f00a7977000 ---p 0002f000 fc:05 271023995                  /usr/lib64/libcrypt.so.2.0.0
7f00a7977000-7f00a7978000 r--p 0002f000 fc:05 271023995                  /usr/lib64/libcrypt.so.2.0.0
7f00a7978000-7f00a7981000 rw-p 00000000 00:00 0 
7f00a7981000-7f00a7992000 r--p 00000000 fc:05 271068897                  /usr/lib64/libgmp.so.10.4.0
7f00a7992000-7f00a7a0a000 r-xp 00011000 fc:05 271068897                  /usr/lib64/libgmp.so.10.4.0
7f00a7a0a000-7f00a7a21000 r--p 00089000 fc:05 271068897                  /usr/lib64/libgmp.so.10.4.0
7f00a7a21000-7f00a7a23000 r--p 0009f000 fc:05 271068897                  /usr/lib64/libgmp.so.10.4.0
7f00a7a23000-7f00a7a24000 rw-p 000a1000 fc:05 271068897                  /usr/lib64/libgmp.so.10.4.0
7f00a7a24000-7f00a7a27000 r--p 00000000 fc:05 271023970                  /usr/lib64/libz.so.1.2.11
7f00a7a27000-7f00a7a35000 r-xp 00003000 fc:05 271023970                  /usr/lib64/libz.so.1.2.11
7f00a7a35000-7f00a7a3b000 r--p 00011000 fc:05 271023970                  /usr/lib64/libz.so.1.2.11
7f00a7a3b000-7f00a7a3c000 ---p 00017000 fc:05 271023970                  /usr/lib64/libz.so.1.2.11
7f00a7a3c000-7f00a7a3d000 r--p 00017000 fc:05 271023970                  /usr/lib64/libz.so.1.2.11
7f00a7a3d000-7f00a7a3e000 rw-p 00000000 00:00 0 
7f00a7a41000-7f00a7a43000 rw-p 00000000 00:00 0 
7f00a7a43000-7f00a7a44000 r--p 00000000 fc:05 270939734                  /usr/lib64/ld-linux-x86-64.so.2
7f00a7a44000-7f00a7a69000 r-xp 00001000 fc:05 270939734                  /usr/lib64/ld-linux-x86-64.so.2
7f00a7a69000-7f00a7a73000 r--p 00026000 fc:05 270939734                  /usr/lib64/ld-linux-x86-64.so.2
7f00a7a73000-7f00a7a75000 r--p 0002f000 fc:05 270939734                  /usr/lib64/ld-linux-x86-64.so.2
7f00a7a75000-7f00a7a77000 rw-p 00031000 fc:05 270939734                  /usr/lib64/ld-linux-x86-64.so.2
7ffd63e17000-7ffd64616000 rw-p 00000000 00:00 0                          [stack]
7ffd64762000-7ffd64766000 r--p 00000000 00:00 0                          [vvar]
7ffd64766000-7ffd64768000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
make: *** [uncommon.mk:1096: encdb.h] Aborted (core dumped)
~~~



---Files--------------------------------
0001-Get-rid-of-type-punning-pointer-cast.patch (1.41 KB)
4716.patch (7.33 KB)
jaruga_make_cflags_with_O2_lto_flags.log (16.2 KB)
jaruga_make_check.log (263 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>