I have a C++ application from which I need to call Go functions. I noticed that the application crashes on exit from the `main` function (with only a "Segmentation fault" message, no core dump and neither did gdb show anything useful) about once in three runs. Then I added the `-fsanitize=address` flag to the build and started to see messages such as these:
================================================================= ==8006==ERROR: LeakSanitizer: detected memory leaks Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x7f9ef4571602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602) #1 0x40421d in x_cgo_thread_start /usr/local/go/src/runtime/cgo/gcc_util.c:15 #2 0x42bcff in runtime.startm /usr/local/go/src/runtime/proc.go:1698 #3 0x42c059 in runtime.wakep /usr/local/go/src/runtime/proc.go:1779 #4 0x42f179 in runtime.newproc1 /usr/local/go/src/runtime/proc.go:2958 #5 0x4490d2 in runtime.newproc.func1 /usr/local/go/src/runtime/proc.go:2845 #6 0x4498d1 in runtime.systemstack /usr/local/go/src/runtime/asm_amd64.s:327 Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x7f9ef4571602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602) #1 0x40421d in x_cgo_thread_start /usr/local/go/src/runtime/cgo/gcc_util.c:15 #2 0x4488a7 in runtime.main.func1 /usr/local/go/src/runtime/proc.go:126 #3 0x4498d1 in runtime.systemstack /usr/local/go/src/runtime/asm_amd64.s:327 Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x7f9ef4571602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602) #1 0x40421d in x_cgo_thread_start /usr/local/go/src/runtime/cgo/gcc_util.c:15 #2 0x42bcff in runtime.startm /usr/local/go/src/runtime/proc.go:1698 #3 0x42bdd6 in runtime.handoffp /usr/local/go/src/runtime/proc.go:1725 #4 0x42c1f4 in runtime.stoplockedm /usr/local/go/src/runtime/proc.go:1793 #5 0x42d5da in runtime.schedule /usr/local/go/src/runtime/proc.go:2180 #6 0x42d730 in runtime.park_m /usr/local/go/src/runtime/proc.go:2285 #7 0x449842 in runtime.mcall /usr/local/go/src/runtime/asm_amd64.s:269 SUMMARY: AddressSanitizer: 72 byte(s) leaked in 3 allocation(s). or ================================================================= ==28395==ERROR: LeakSanitizer: detected memory leaks Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x7f562b171602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602) #1 0x403a0d in x_cgo_thread_start /usr/local/go/src/runtime/cgo/gcc_util.c:15 #2 0x42b4ef in runtime.startm /usr/local/go/src/runtime/proc.go:1698 #3 0x42b5c6 in runtime.handoffp /usr/local/go/src/runtime/proc.go:1725 #4 0x42b9e4 in runtime.stoplockedm /usr/local/go/src/runtime/proc.go:1793 #5 0x42cdca in runtime.schedule /usr/local/go/src/runtime/proc.go:2180 #6 0x42cf20 in runtime.park_m /usr/local/go/src/runtime/proc.go:2285 #7 0x449032 in runtime.mcall /usr/local/go/src/runtime/asm_amd64.s:269 Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x7f562b171602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602) #1 0x403a0d in x_cgo_thread_start /usr/local/go/src/runtime/cgo/gcc_util.c:15 #2 0x42b4ef in runtime.startm /usr/local/go/src/runtime/proc.go:1698 #3 0x42b849 in runtime.wakep /usr/local/go/src/runtime/proc.go:1779 #4 0x42c8b4 in runtime.resetspinning /usr/local/go/src/runtime/proc.go:2141 #5 0x42cc09 in runtime.schedule /usr/local/go/src/runtime/proc.go:2229 #6 0x42a2db in runtime.mstart1 /usr/local/go/src/runtime/proc.go:1189 #7 0x42a1f5 in runtime.mstart /usr/local/go/src/runtime/proc.go:1149 #8 0x403a62 (/home/peter/work/tmp/minimal-cgo/a.out+0x403a62) SUMMARY: AddressSanitizer: 48 byte(s) leaked in 2 allocation(s). but most often just this one: ================================================================= ==17138==ERROR: LeakSanitizer: detected memory leaks Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x7f15bfe0e602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602) #1 0x403eed in x_cgo_thread_start /usr/local/go/src/runtime/cgo/gcc_util.c:15 #2 0x448577 in runtime.main.func1 /usr/local/go/src/runtime/proc.go:126 #3 0x4495a1 in runtime.systemstack /usr/local/go/src/runtime/asm_amd64.s:327 SUMMARY: AddressSanitizer: 24 byte(s) leaked in 1 allocation(s). I reduced the code which can reproduce these to the three attached files. With this reduced code I can see the failures a lot less often though. Sometimes I can see the error quite quickly, but sometimes I need to let the `run.sh` script run for more than a minute. I have tested with go1.8, go1.8.3 and go1.9rc2 (linux amd64) with the same results. ~ > g++ --version g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609 Do I need to call some cgo function to properly de-initialize go when exiting the main function? Regards, Peter -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
go_code.go
Description: Binary data
#include "go_code.h" #include <vector> #include <assert.h> struct Options { Options() : ff(10000) { ff.push_back(32); } std::vector<int> ff; }; int main(int argc, const char** argv) { Options options; assert(argc > 0); if (argc > 0) return 0; // This is never called. go_start((char*) ""); return 0; }
#!/bin/sh set -e if [ "$1" = "clean" ]; then rm a.out go_code.h go_code.a 2>/dev/null || true exit fi export ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1 go build --buildmode=c-archive ./go_code.go g++ ./main.cpp -pthread -I./ ./go_code.a -fsanitize=address echo start while true; do ./a.out; done