← Back to team overview

kicad-developers team mailing list archive

PATCH: fix race condition related to 3D cache and resolver

 

The attached patch fixes a segfault due to a race condition.
If a user starts eeschema with an empty sheet and clicks
the 'cvpcb' button, memory is corrupted and the program
segfaults. The issue appears to arise from multiple threads
accessing the 3D cache and resolver. This patch makes
relevant code thread-safe by using a wxCriticalSection.

- Cirilo
=== modified file '3d-viewer/3d_cache/3d_cache.cpp'
--- 3d-viewer/3d_cache/3d_cache.cpp	2016-04-07 02:41:24 +0000
+++ 3d-viewer/3d_cache/3d_cache.cpp	2016-04-09 22:51:48 +0000
@@ -32,6 +32,7 @@
 #include <wx/datetime.h>
 #include <wx/filename.h>
 #include <wx/log.h>
+#include <wx/thread.h>
 #include <wx/utils.h>
 #include <wx/stdpaths.h>
 
@@ -51,6 +52,8 @@
 #define CACHE_CONFIG_NAME wxT( "cache.cfg" )
 #define MASK_3D_CACHE "3D_CACHE"
 
+static wxCriticalSection lock3D_cache;
+
 static bool isSHA1Same( const unsigned char* shaA, const unsigned char* shaB )
 {
     for( int i = 0; i < 20; ++i )
@@ -215,6 +218,7 @@
     }
 
     // check cache if file is already loaded
+    wxCriticalSectionLocker lock( lock3D_cache );
     std::map< wxString, S3D_CACHE_ENTRY*, S3D::rsort_wxString >::iterator mi;
     mi = m_CacheMap.find( full3Dpath );
 

=== modified file '3d-viewer/3d_cache/3d_cache_wrapper.cpp'
--- 3d-viewer/3d_cache/3d_cache_wrapper.cpp	2015-12-08 07:31:57 +0000
+++ 3d-viewer/3d_cache/3d_cache_wrapper.cpp	2016-04-09 22:45:13 +0000
@@ -23,8 +23,10 @@
 
 
 #include <common.h>
+#include <wx/thread.h>
 #include "3d_cache_wrapper.h"
 
+static wxCriticalSection lock3D_wrapper;
 
 CACHE_WRAPPER::CACHE_WRAPPER()
 {
@@ -40,6 +42,7 @@
 
 S3D_CACHE* PROJECT::Get3DCacheManager( bool updateProjDir )
 {
+    wxCriticalSectionLocker lock( lock3D_wrapper );
     CACHE_WRAPPER* cw = (CACHE_WRAPPER*) GetElem( ELEM_3DCACHE );
     S3D_CACHE* cache = dynamic_cast<S3D_CACHE*>( cw );
 

=== modified file '3d-viewer/3d_cache/3d_filename_resolver.cpp'
--- 3d-viewer/3d_cache/3d_filename_resolver.cpp	2016-04-05 10:32:22 +0000
+++ 3d-viewer/3d_cache/3d_filename_resolver.cpp	2016-04-09 22:55:08 +0000
@@ -29,6 +29,7 @@
 #include <sstream>
 #include <wx/filename.h>
 #include <wx/log.h>
+#include <wx/thread.h>
 #include <wx/utils.h>
 #include <wx/msgdlg.h>
 
@@ -45,6 +46,7 @@
 
 #define MASK_3D_RESOLVER "3D_RESOLVER"
 
+static wxCriticalSection lock3D_resolver;
 
 static bool getHollerith( const std::string& aString, size_t& aIndex, wxString& aResult );
 
@@ -203,6 +205,7 @@
 
 wxString S3D_FILENAME_RESOLVER::ResolvePath( const wxString& aFileName )
 {
+    wxCriticalSectionLocker lock( lock3D_resolver );
     if( aFileName.empty() )
         return wxEmptyString;
 
@@ -372,6 +375,8 @@
     if( aPath.m_alias.empty() || aPath.m_pathvar.empty() )
         return false;
 
+    wxCriticalSectionLocker lock( lock3D_resolver );
+
     S3D_ALIAS tpath = aPath;
     tpath.m_duplicate = false;
 
@@ -736,6 +741,7 @@
     if( m_Paths.empty() )
         createPathList();
 
+    wxCriticalSectionLocker lock( lock3D_resolver );
     std::list< S3D_ALIAS >::const_iterator sL = m_Paths.begin();
     std::list< S3D_ALIAS >::const_iterator eL = m_Paths.end();
     size_t idx;


Follow ups