opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.46k stars 5.77k forks source link

Segfault in dnn module #383

Closed inferrna closed 4 years ago

inferrna commented 9 years ago

Just import cv2 from python console and crash happened.

~$ gdb python3
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from python3...Reading symbols from /usr/lib/debug//usr/bin/python3.4m...done.
done.
(gdb) run
Starting program: /usr/bin/python3 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Python 3.4.3 (default, Jul 28 2015, 18:20:59) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
[New Thread 0x7fffd0991700 (LWP 4811)]

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff21b1ddb in cv::dnn::LayerFactory::registerLayer(cv::String const&, cv::Ptr<cv::dnn::Layer> (*)(cv::dnn::LayerParams&)) ()
   from /usr/local/lib/libopencv_dnn.so.3.0
(gdb) bt
#0  0x00007ffff21b1ddb in cv::dnn::LayerFactory::registerLayer(cv::String const&, cv::Ptr<cv::dnn::Layer> (*)(cv::dnn::LayerParams&)) ()
   from /usr/local/lib/libopencv_dnn.so.3.0
#1  0x00007ffff21aa350 in cv::dnn::initModule() () from /usr/local/lib/libopencv_dnn.so.3.0
#2  0x00007ffff7dea13a in call_init (l=<optimized out>, argc=argc@entry=1, argv=argv@entry=0x7fffffffd928, env=env@entry=0x7fffffffd938) at dl-init.c:78
#3  0x00007ffff7dea223 in call_init (env=<optimized out>, argv=<optimized out>, argc=<optimized out>, l=<optimized out>) at dl-init.c:36
#4  _dl_init (main_map=main_map@entry=0xaa48c0, argc=1, argv=0x7fffffffd928, env=0x7fffffffd938) at dl-init.c:126
#5  0x00007ffff7deec70 in dl_open_worker (a=a@entry=0x7fffffffbf18) at dl-open.c:577
inferrna commented 9 years ago

Additional info. OS: Ubuntu 14.04

gcc -v

Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.1.0-0ubuntu11~14.04.1' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=c++98 --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.1.0 (Ubuntu 5.1.0-0ubuntu11~14.04.1) 

Cmake report

-- General configuration for OpenCV 3.0.0-dev =====================================
--   Version control:               3.0.0-437-g9533982-dirty
-- 
--   Platform:
--     Host:                        Linux 3.16.0-50-generic x86_64
--     CMake:                       2.8.12.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                /usr/lib/ccache/c++  (ver 5.1.0)
--     C++ flags (Release):         -O2 -march=native --param l1-cache-size=16 --param l1-cache-line-size=64 --param l2-cache-size=2048 -mtune=native -pipe    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -O2 -march=native --param l1-cache-size=16 --param l1-cache-line-size=64 --param l2-cache-size=2048 -mtune=native -pipe    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/lib/ccache/cc
--     C flags (Release):           -std=gnu99    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -std=gnu99    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      
--     Linker flags (Debug):        
--     Precompiled headers:         NO
--     Extra dependencies:          /usr/lib/x86_64-linux-gnu/libwebp.so /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/x86_64-linux-gnu/libtiff.so /usr/lib/x86_64-linux-gnu/libjasper.so /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so gtk-3 gdk-3 atk-1.0 gio-2.0 pangocairo-1.0 gdk_pixbuf-2.0 cairo-gobject pango-1.0 cairo gobject-2.0 gthread-2.0 glib-2.0 dc1394 avcodec avformat avutil swscale gphoto2 gphoto2_port exif /usr/lib/x86_64-linux-gnu/libbz2.so $<$<NOT:$<CONFIG:DEBUG>>:/usr/lib/x86_64-linux-gnu/libprotobuf.so> $<$<CONFIG:DEBUG>:/usr/lib/x86_64-linux-gnu/libprotobuf.so> vtkCharts vtkCommon vtkFiltering vtkGenericFiltering vtkGeovis vtkGraphics vtkHybrid vtkIO vtkImaging vtkInfovis vtkParallel vtkRendering vtkViews vtkVolumeRendering vtkWidgets dl m pthread rt tbb
--     3rdparty dependencies:       ippicv
-- 
--   OpenCV modules:
--     To be built:                 hal core flann imgproc ml photo reg surface_matching video viz dnn imgcodecs shape videoio highgui objdetect optflow superres tracking ts xobjdetect xphoto adas bgsegm bioinspired dpm face features2d line_descriptor saliency text calib3d ccalib datasets java rgbd stereo structured_light videostab xfeatures2d ximgproc aruco stitching python2 python3
--     Disabled:                    world contrib_world
--     Disabled by dependency:      -
--     Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv matlab
-- 
--   GUI: 
--     QT:                          NO
--     GTK+ 3.x:                    YES (ver 3.10.8)
--     GThread :                    YES (ver 2.40.2)
--     GtkGlExt:                    NO
--     OpenGL support:              NO
--     VTK support:                 YES (ver 5.8.0)
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.8)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
--     WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x0202)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.2.50)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 - 4.0.3)
--     JPEG 2000:                   /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
--     OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 1.6.1)
--     GDAL:                        NO
-- 
--   Video I/O:
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  YES (ver 2.2.1)
--     FFMPEG:                      YES
--       codec:                     YES (ver 54.35.0)
--       format:                    YES (ver 54.20.4)
--       util:                      YES (ver 52.3.0)
--       swscale:                   YES (ver 2.1.1)
--       resample:                  NO
--       gentoo-style:              YES
--     GStreamer:                   NO
--     OpenNI:                      NO
--     OpenNI PrimeSensor Modules:  NO
--     OpenNI2:                     NO
--     PvAPI:                       NO
--     GigEVisionSDK:               NO
--     UniCap:                      NO
--     UniCap ucil:                 NO
--     V4L/V4L2:                    NO/YES
--     XIMEA:                       NO
--     Xine:                        NO
--     gPhoto2:                     YES
-- 
--   Parallel framework:            TBB (ver 4.2 interface 7000)
-- 
--   Other third-party libraries:
--     Use IPP:                     8.2.1 [8.2.1]
--          at:                     /home/inferno/.dev/opencv/3rdparty/ippicv/unpack/ippicv_lnx
--     Use IPP Async:               NO
--     Use VA:                      NO
--     Use Intel VA-API/OpenCL:     NO
--     Use Eigen:                   YES (ver 3.2.0)
--     Use Cuda:                    NO
--     Use OpenCL:                  YES
-- 
--   OpenCL:
--     Version:                     dynamic
--     Include path:                /home/inferno/.dev/opencv/3rdparty/include/opencl/1.2 /usr/include
--     Use AMDFFT:                  NO
--     Use AMDBLAS:                 YES
-- 
--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.6)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.6)
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2)
--     packages path:               lib/python2.7/dist-packages
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3.4 (ver 3.4.3)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)
--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.8.2)
--     packages path:               lib/python3.4/dist-packages
-- 
--   Python (for build):            /usr/bin/python2.7
-- 
--   Java:
--     ant:                         /usr/bin/ant (ver 1.9.3)
--     JNI:                         /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include
--     Java wrappers:               YES
--     Java tests:                  YES
-- 
--   Matlab:
--     mex:                         NO
-- 
--   Documentation:
--     Doxygen:                     /usr/bin/doxygen (ver 1.8.6)
--     PlantUML:                    NO
-- 
--   Tests and samples:
--     Tests:                       YES
--     Performance tests:           YES
--     C/C++ Examples:              NO
-- 
--   Install path:                  /usr/local
-- 
--   cvconfig.h is in:              /home/inferno/.dev/opencv/opencv
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    BUILD_NEW_PYTHON_SUPPORT
rokm commented 9 years ago

I hit this on one of my machines today, when trying to use either python2/3 bindings or mexopencv bindings with Matlab. But the issue is reproducible even with the following minimal code, as long as the resulting executable is linked against libopencv_dnn:

/* compile with: g++ -o test test.cpp -L/opt/opencv-3.0.0/libs -lopencv_dnn */
int main (int argc, char **argv)
{
    return 0;
}
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a99b9e in std::_Rb_tree<cv::String, std::pair<cv::String const, cv::Ptr<cv::dnn::Layer> (*)(cv::dnn::LayerParams&)>, std::_Select1st<std::pair<cv::String const, cv::Ptr<cv::dnn::Layer> (*)(cv::dnn::LayerParams&)> >, std::less<cv::String>, std::allocator<std::pair<cv::String const, cv::Ptr<cv::dnn::Layer> (*)(cv::dnn::LayerParams&)> > >::find (this=0x0, __k=...)
    at /usr/include/c++/5.1.1/bits/stl_tree.h:2292
2292        _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
(gdb) bt
#0  0x00007ffff7a99b9e in std::_Rb_tree<cv::String, std::pair<cv::String const, cv::Ptr<cv::dnn::Layer> (*)(cv::dnn::LayerParams&)>, std::_Select1st<std::pair<cv::String const, cv::Ptr<cv::dnn::Layer> (*)(cv::dnn::LayerParams&)> >, std::less<cv::String>, std::allocator<std::pair<cv::String const, cv::Ptr<cv::dnn::Layer> (*)(cv::dnn::LayerParams&)> > >::find (this=0x0, __k=...)
    at /usr/include/c++/5.1.1/bits/stl_tree.h:2292
#1  0x00007ffff7a9a1c6 in find (__x=..., this=<optimized out>) at /usr/include/c++/5.1.1/bits/stl_map.h:843
#2  cv::dnn::LayerFactory::registerLayer (_type=..., constructor=constructor@entry=0x7ffff7a8c290 <cv::dnn::_layerDynamicRegisterer<cv::dnn::SliceLayer>(cv::dnn::LayerParams&)>)
    at /home/rok/Projects/opencv_contrib/modules/dnn/src/dnn.cpp:570
#3  0x00007ffff7a90320 in cv::dnn::initModule () at /home/rok/Projects/opencv_contrib/modules/dnn/src/init.cpp:79
#4  0x00007ffff7deb63a in call_init (l=<optimized out>, argc=argc@entry=1, argv=argv@entry=0x7fffffffdf28, env=env@entry=0x7fffffffdf38) at dl-init.c:72
#5  0x00007ffff7deb74b in call_init (env=0x7fffffffdf38, argv=0x7fffffffdf28, argc=1, l=<optimized out>) at dl-init.c:30
#6  _dl_init (main_map=0x7ffff7ffe148, argc=1, argv=0x7fffffffdf28, env=0x7fffffffdf38) at dl-init.c:120
#7  0x00007ffff7ddccba in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#8  0x0000000000000001 in ?? ()
#9  0x00007fffffffe266 in ?? ()
#10 0x0000000000000000 in ?? ()
(gdb) up 2
#2  cv::dnn::LayerFactory::registerLayer (_type=..., constructor=constructor@entry=0x7ffff7a8c290 <cv::dnn::_layerDynamicRegisterer<cv::dnn::SliceLayer>(cv::dnn::LayerParams&)>)
    at /home/rok/Projects/opencv_contrib/modules/dnn/src/dnn.cpp:570
570     Impl::iterator it = impl->find(type);
(gdb) display impl
1: impl = {owner = 0x0, stored = 0x0}

As indicated by the above backtrace, at the time when cv::dnn::initModule() is called, and calls the cv::dnn::LayerFactory::registerLayer() in turn, the static cv::dnn::LayerFactory::impl member is still uninitialized, leading to segfault.

So the issue appears to be the so-called "static initialization order fiasco"; because the initialization code depends on a specific order of initialization for static variables (the initModule() function is also called via initialization of a static variable), we have a 50% chance of crashing...