Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v generated shared .so crash on dlclose #15657

Open
seven1240 opened this issue Sep 4, 2022 · 0 comments
Open

v generated shared .so crash on dlclose #15657

seven1240 opened this issue Sep 4, 2022 · 0 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@seven1240
Copy link
Contributor

V version: V 0.3.1 dd78620
OS: Linux adf6cd4e2dff 5.10.47-linuxkit #1 SMP PREEMPT Sat Jul 3 21:50:16 UTC 2021 aarch64 GNU/Linux

What did you do?

code: https://github.com/seven1240/vcrash

a little hack to track _vinit and vcleanup

diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v
index 5aaaa4da8..7ec24fdc7 100644
--- a/vlib/v/gen/c/cgen.v
+++ b/vlib/v/gen/c/cgen.v
@@ -4967,14 +4967,19 @@ fn (mut g Gen) write_init_function() {
 		// Note: os.args in this case will be [].
 		g.writeln('__attribute__ ((constructor))')
 		g.writeln('void _vinit_caller() {')
+		g.writeln('printf("vinit_caller\\n");')
 		g.writeln('\tstatic bool once = false; if (once) {return;} once = true;')
+		g.writeln('printf("vinit\\n");')
 		g.writeln('\t_vinit(0,0);')
+		g.writeln('printf("vinit done\\n");')
 		g.writeln('}')
 
 		g.writeln('__attribute__ ((destructor))')
 		g.writeln('void _vcleanup_caller() {')
 		g.writeln('\tstatic bool once = false; if (once) {return;} once = true;')
+		g.writeln('printf("vcleanup\\n");')
 		g.writeln('\t_vcleanup();')
+		g.writeln('printf("vcleanup done\\n");')
 		g.writeln('}')
 	}
 }

build

# make
v -cc gcc -g -shared -enable-globals -o mod .
gcc -o main main.c -ldl -lpthread

What did you expect to see?

some times it's ok as follows

# ./main
Hello
Closing
vinit_caller
vinit
vinit done
start
stop
vcleanup
vcleanup done
thread done
Closed
Tick ... 2
Tick ... 1
Tick ... 0
Done

What did you see instead?

sometime it crashes

# ./main
Hello
Closing
vinit_caller
vinit
vinit done
start
stop
vcleanup
vcleanup done
Segmentation fault (core dumped)

# gdb -core core ./main
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./main...
(No debugging symbols found in ./main)
[New LWP 1898]
[New LWP 1895]
[New LWP 1894]
[New LWP 1899]
[New LWP 1900]
[New LWP 1896]
[New LWP 1897]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
--Type <RET> for more, q to quit, c to continue without paging--
Core was generated by `./main'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000ffffb3c229b0 in __aarch64_ldadd4_rel ()
   from /lib/aarch64-linux-gnu/libpthread.so.0
[Current thread is 1 (Thread 0xffffb219b1e0 (LWP 1898))]
(gdb) bt
#0  0x0000ffffb3c229b0 in __aarch64_ldadd4_rel ()
   from /lib/aarch64-linux-gnu/libpthread.so.0
#1  0x0000ffffb3c1af24 in __condvar_dec_grefs (
    cond=cond@entry=0xffffb3254ea8 <mark_cv>, g=g@entry=0,
    private=private@entry=0) at pthread_cond_wait.c:153
#2  0x0000ffffb3c1b428 in __pthread_cond_wait_common (abstime=0x0, clockid=0,
    mutex=0xffffb3254e78 <mark_mutex>, cond=0xffffb3254ea8 <mark_cv>)
    at pthread_cond_wait.c:541
#3  __pthread_cond_wait (cond=0xffffb3254ea8 <mark_cv>,
    mutex=0xffffb3254e78 <mark_mutex>) at pthread_cond_wait.c:638
#4  0x0000ffffb321db84 in GC_wait_marker () from ./mod.so
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

It works fine on macOS Darwin 192.168.7.8 21.1.0 Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:01 PDT 2021; root:xnu-8019.41.5~1/RELEASE_ARM64_T6000 arm64, since dlclose doesn't trigger _vcleanup. until the main process exit.

Hello
Closing
vinit_caller
vinit
done
start
stop
thread done
Closed
Tick ... 2
Tick ... 1
Tick ... 0
Done
vcleanup
vcleanup done

Thanks.

@seven1240 seven1240 added the Bug This tag is applied to issues which reports bugs. label Sep 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

1 participant