www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.ldc - free(): double free detected in tcache 2

reply workman <workman gmail.com> writes:
link with -flto=full from linux get this error:

```sh
free(): double free detected in tcache 2

Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig entry=6) at 
../sysdeps/unix/sysv/linux/raise.c:50
50	../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt

../sysdeps/unix/sysv/linux/raise.c:50


(action=action entry=do_abort, fmt=fmt entry=0x7ffff7e0ce2d 
"%s\n") at ../sysdeps/posix/libc_fatal.c:155

(str=str entry=0x7ffff7e0f170 "free(): double free detected in 
tcache 2") at malloc.c:5347

<main_arena>, p=0x54a360, have_lock=1) at malloc.c:4201

(av=av entry=0x7ffff7e3eb80 <main_arena>, 
oldp=oldp entry=0x54a360, oldsize=oldsize entry=32, nb=88736032) 
at malloc.c:4618

bytes=88736016) at malloc.c:3226

core.internal.container.common.xrealloc(void*, ulong) ()

core.internal.container.array.Array!(void[]).Array.length(ulong) 
()

core.internal.container.array.Array!(void[]).Array.insertBack!(
.insertBack(void[]) ()




listp=0x7ffff7e3e718 <__exit_funcs>, 
run_list_atexit=run_list_atexit entry=true, 
run_dtors=run_dtors entry=true) at exit.c:108

exit.c:139

<main>, argc=2, argv=0x7fffffffdec8, init=<optimized out>, 
fini=<optimized out>, rtld_fini=<optimized out>, 
stack_end=0x7fffffffdeb8) at ../csu/libc-start.c:342

```
Aug 12 2021
parent reply Johan <j j.nl> writes:
On Friday, 13 August 2021 at 05:58:34 UTC, workman wrote:
 link with -flto=full from linux get this error:

 ```sh
 free(): double free detected in tcache 2

 Program received signal SIGABRT, Aborted.
 __GI_raise (sig=sig entry=6) at 
 ../sysdeps/unix/sysv/linux/raise.c:50
 50	../sysdeps/unix/sysv/linux/raise.c: No such file or 
 directory.
 (gdb) bt

 ../sysdeps/unix/sysv/linux/raise.c:50


 (action=action entry=do_abort, fmt=fmt entry=0x7ffff7e0ce2d 
 "%s\n") at ../sysdeps/posix/libc_fatal.c:155

 (str=str entry=0x7ffff7e0f170 "free(): double free detected in 
 tcache 2") at malloc.c:5347

 <main_arena>, p=0x54a360, have_lock=1) at malloc.c:4201

 (av=av entry=0x7ffff7e3eb80 <main_arena>, 
 oldp=oldp entry=0x54a360, oldsize=oldsize entry=32, 
 nb=88736032) at malloc.c:4618

 bytes=88736016) at malloc.c:3226

 core.internal.container.common.xrealloc(void*, ulong) ()

 core.internal.container.array.Array!(void[]).Array.length(ulong) ()

 core.internal.container.array.Array!(void[]).Array.insertBack!(
.insertBack(void[]) ()




 listp=0x7ffff7e3e718 <__exit_funcs>, 
 run_list_atexit=run_list_atexit entry=true, 
 run_dtors=run_dtors entry=true) at exit.c:108

 exit.c:139

 <main>, argc=2, argv=0x7fffffffdec8, init=<optimized out>, 
 fini=<optimized out>, rtld_fini=<optimized out>, 
 stack_end=0x7fffffffdeb8) at ../csu/libc-start.c:342

 ```
Can you check how often _d_dso_registry is called after __run_exit_handlers ?
Aug 13 2021
parent reply workman <workman gmail.com> writes:
On Friday, 13 August 2021 at 11:01:00 UTC, Johan wrote:
 On Friday, 13 August 2021 at 05:58:34 UTC, workman wrote:
 [...]
Can you check how often _d_dso_registry is called after __run_exit_handlers ?
not sure how to do this.
Aug 23 2021
parent reply Johan <j j.nl> writes:
On Monday, 23 August 2021 at 15:59:04 UTC, workman wrote:
 On Friday, 13 August 2021 at 11:01:00 UTC, Johan wrote:
 On Friday, 13 August 2021 at 05:58:34 UTC, workman wrote:
 [...]
Can you check how often _d_dso_registry is called after __run_exit_handlers ?
not sure how to do this.
(not really sure if this will really help figure out the problem, but...) You can break on it (probably smth like "break _d_dso_registry"), then continue when execution is stopped at the start of the function (probably "c"), and count how often the breakpoint is triggered (keep "c"), before the program exits. -Johan
Aug 23 2021
parent reply max haughton <maxhaton gmail.com> writes:
On Monday, 23 August 2021 at 22:34:58 UTC, Johan wrote:
 On Monday, 23 August 2021 at 15:59:04 UTC, workman wrote:
 On Friday, 13 August 2021 at 11:01:00 UTC, Johan wrote:
 On Friday, 13 August 2021 at 05:58:34 UTC, workman wrote:
 [...]
Can you check how often _d_dso_registry is called after __run_exit_handlers ?
not sure how to do this.
(not really sure if this will really help figure out the problem, but...) You can break on it (probably smth like "break _d_dso_registry"), then continue when execution is stopped at the start of the function (probably "c"), and count how often the breakpoint is triggered (keep "c"), before the program exits. -Johan
You need to break, then set the ignore count, then `info breakpoints` (something like you that) will still give you the hit count for that breakpoint
Aug 23 2021
parent reply workman <workman gmail.com> writes:
On Monday, 23 August 2021 at 23:25:19 UTC, max haughton wrote:
 You need to break, then set the ignore count, then `info 
 breakpoints` (something like you that) will still give you the 
 hit count for that breakpoint
Thanks for the tips, ```sh Num Type Disp Enb Address What 1 breakpoint keep y 0x00000000004b0f90 <_d_dso_registry> breakpoint already hit 3 times ``` without lto ```sh Num Type Disp Enb Address What 1 breakpoint keep y 0x000000000052ece0 <_d_dso_registry> breakpoint already hit 2 times ```
Aug 23 2021
parent reply Johan <j j.nl> writes:
On Tuesday, 24 August 2021 at 06:04:12 UTC, workman wrote:
 On Monday, 23 August 2021 at 23:25:19 UTC, max haughton wrote:
 You need to break, then set the ignore count, then `info 
 breakpoints` (something like you that) will still give you the 
 hit count for that breakpoint
Thanks for the tips, ```sh Num Type Disp Enb Address What 1 breakpoint keep y 0x00000000004b0f90 <_d_dso_registry> breakpoint already hit 3 times ``` without lto ```sh Num Type Disp Enb Address What 1 breakpoint keep y 0x000000000052ece0 <_d_dso_registry> breakpoint already hit 2 times ```
OK, so indeed it is what I thought: with LTO _d_dso_registry is called too often. The function does initialization or deinit (it is a 'toggle' operation). 1st call: initialization 2nd call: deinitialization 3rd call: initialization, but here the struct passed probably contains something that triggers a second free call on a pointer that was not nulled maybe? Regardless, this third call should not happen. Which linker is used in the LTO case? ld.bfd, ld.gold, ld.lld ? (`ldc2 -v ...` one of the last lines is the C compiler invocation for linking; check for "-fuse-ld="; if it is not there then just `ld` is used, check `ld --version`) Probably the same as this bug: https://github.com/ldc-developers/ldc/issues/3786 -Johan
Aug 24 2021
parent reply workman <workman gmail.com> writes:
On Tuesday, 24 August 2021 at 11:10:47 UTC, Johan wrote:
 OK, so indeed it is what I thought: with LTO _d_dso_registry is 
 called too often.
 The function does initialization or deinit (it is a 'toggle' 
 operation).

 1st call:  initialization
 2nd call:  deinitialization
 3rd call:  initialization, but here the struct passed probably 
 contains something that triggers a second free call on a 
 pointer that was not nulled maybe?  Regardless, this third call 
 should not happen.

 Which linker is used in the LTO case? ld.bfd, ld.gold, ld.lld ?
 (`ldc2 -v ...` one of the last lines is the C compiler 
 invocation for linking; check for "-fuse-ld="; if it is not 
 there then just `ld` is used, check `ld --version`)

 Probably the same as this bug: 
 https://github.com/ldc-developers/ldc/issues/3786

 -Johan
I use ld.ldd to do the link, try -flto=full and -flto=thin, has same problem: `free(): double free detected in tcache 2`
Aug 24 2021
parent reply Johan <j j.nl> writes:
On Wednesday, 25 August 2021 at 06:55:40 UTC, workman wrote:
 On Tuesday, 24 August 2021 at 11:10:47 UTC, Johan wrote:
 Probably the same as this bug: 
 https://github.com/ldc-developers/ldc/issues/3786

 -Johan
I use ld.ldd to do the link, try -flto=full and -flto=thin, has same problem: `free(): double free detected in tcache 2`
Then it's the same bug as I mentioned. If you have the option to try with other linkers, then please try. It should work with ld.gold. -Johan
Aug 25 2021
parent workman <workman gmail.com> writes:
On Wednesday, 25 August 2021 at 09:18:56 UTC, Johan wrote:
 Then it's the same bug as I mentioned.
 If you have the option to try with other linkers, then please 
 try. It should work with ld.gold.

 -Johan
Thanks for explain, I try use gold before and they are create a lot link error like this: ```sh /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/crt1.o:function _start: error: undefined reference to '__libc_csu_fini' /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/crt1.o:function _start: error: undefined reference to '__libc_csu_init' /usr/bin/../lib/libphobos2-ldc.a(file.o):file.d:function _D3std4file8readImplFNeMAxaMPxamZAv: error: undefined reference to 'fstat64' /usr/bin/../lib/libphobos2-ldc.a(file.o):file.d:function _D3std4file10existsImplFNbNiNePxaZb: error: undefined reference to 'lstat64' /usr/bin/../lib/libphobos2-ldc.a(file.o):file.d:function _D3std4file12mkdirRecurseFNfMAxaZv: error: undefined reference to 'lstat64' /usr/bin/../lib/libphobos2-ldc.a(file.o):file.d:function _D3std4file__T6existsTAxaZQmFNbNiNfQnZb: error: undefined reference to 'lstat64' /usr/bin/../lib/libphobos2-ldc.a(file.o):file.d:function _D3std4file8DirEntry6__ctorMFNcNfAyaZSQBkQBjQBh: error: undefined reference to 'lstat64' /usr/bin/../lib/libphobos2-ldc.a(file.o):file.d:function _D3std4file8DirEntry5isDirMFNdNfZb: error: undefined reference to 'stat64' /usr/bin/../lib/libphobos2-ldc.a(file.o):file.d:function _D3std4file8DirEntry22_ensureStatOrLStatDoneMFNeZv: error: undefined reference to 'stat64' /usr/bin/../lib/libphobos2-ldc.a(file.o):file.d:function _D3std4file8DirEntry6isFileMFNdNfZb: error: undefined reference to 'stat64' /usr/bin/../lib/libphobos2-ldc.a(file.o):file.d:function _D3std4file8DirEntry15_ensureStatDoneMFNeZv: error: undefined reference to 'stat64' /usr/bin/../lib/libphobos2-ldc.a(file.o):file.d:function _D3std4file8copyImplFNeMAxaMQeMPxaMQeEQBk8typecons__T4FlagVAyaa18_70726573657276654174747 696275746573ZQByZv: error: undefined reference to 'fstat64' /usr/bin/../lib/libphobos2-ldc.a(file.o):file.d:function _D3std4file8copyImplFNeMAxaMQeMPxaMQeEQBk8typecons__T4FlagVAyaa18_70726573657276654174747 696275746573ZQByZv: error: undefined reference to 'fstat64' /usr/bin/../lib/libphobos2-ldc.a(curl.o):curl.d:function _D3std3net4curl7CurlAPI7loadAPIFZPv: error: undefined reference to 'atexit' /usr/bin/../lib/libphobos2-ldc.a(process.o):process.d:function _D3std7process17spawnProcessPosixFNeMAxAaSQBo5stdio4FileQpQrMxHAyaAyaSQCqQCp6Con igMAxaZCQDjQDi3Pid: error: undefined reference to 'fstat64' /usr/bin/../lib/libdruntime-ldc.a(gc.o):gc.d:function _D4core8internal2gc4impl12conservativeQw14ConservativeGC6__ctorMFZCQC QClQCfQCfQCdQCoQBt: error: undefined reference to 'pthread_atfork' /usr/bin/../lib/libdruntime-ldc.a(gc.o):gc.d:function _D4core8internal2gc4impl12conservativeQw3Gcx10initializeMFZv: error: undefined reference to 'pthread_atfork' ``` LLD work great if I use LDC betterC and LTO.
Aug 25 2021