Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit 6912093

Browse files
authored
Merge pull request #76 from ali-ince/1.7-valgrind-fixes
Fixes for concurrency issues
2 parents ec6a637 + 0510ab0 commit 6912093

File tree

7 files changed

+176
-58
lines changed

7 files changed

+176
-58
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ project(seabolt
77
IF (DEFINED ENV{SEABOLT_VERSION})
88
set(_SEABOLT_VERSION $ENV{SEABOLT_VERSION})
99
ELSE ()
10-
set(_SEABOLT_VERSION "1.7.3-dev")
10+
set(_SEABOLT_VERSION "1.7.4-dev")
1111
ENDIF ()
1212

1313
set(SEABOLT_VERSION ${_SEABOLT_VERSION} CACHE STRING "The version of seabolt being built")

run_tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ try
186186
CheckBoltKit
187187
Compile
188188

189-
$Neo4jVersion = "-e 3.4"
189+
$Neo4jVersion = "-e 3.5"
190190
If (Test-Path "env:NEOCTRLARGS") {
191191
$Neo4jVersion = $env:NEOCTRLARGS
192192
}

run_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ if [[ -z "${PYTHON}" ]]; then
2020
fi
2121

2222
if [[ -z "${NEOCTRLARGS}" ]]; then
23-
NEO4J_VERSION="-e 3.4"
23+
NEO4J_VERSION="-e 3.5"
2424
else
2525
NEO4J_VERSION="${NEOCTRLARGS}"
2626
fi

src/seabolt/src/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,13 @@ foreach (target ${SEABOLT_SHARED} ${SEABOLT_STATIC})
180180
if (ON_WINDOWS)
181181
target_link_libraries(${target}
182182
PUBLIC
183-
crypt32
184-
legacy_stdio_definitions)
183+
crypt32)
184+
185+
if (MSVC)
186+
target_link_libraries(${target}
187+
PUBLIC
188+
legacy_stdio_definitions)
189+
endif ()
185190
endif ()
186191
endif ()
187192

src/seabolt/src/bolt/communication-plain-posix.c

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "communication-plain.h"
2020
#include "status-private.h"
2121

22+
#include <pthread.h>
2223
#include <errno.h>
2324

2425
int socket_last_error(BoltCommunication* comm)
@@ -66,16 +67,31 @@ int socket_transform_error(BoltCommunication* comm, int error_code)
6667
int socket_ignore_sigpipe(void** replaced_action)
6768
{
6869
#if !defined(SO_NOSIGPIPE)
69-
if (*replaced_action==NULL) {
70-
*replaced_action = malloc(sizeof(struct sigaction));
70+
sigset_t sig_block, sig_restore, sig_pending;
71+
72+
sigemptyset(&sig_block);
73+
sigaddset(&sig_block, SIGPIPE);
74+
75+
int result = pthread_sigmask(SIG_BLOCK, &sig_block, &sig_restore);
76+
if (result!=0) {
77+
return result;
78+
}
79+
80+
int sigpipe_pending = -1;
81+
if (sigpending(&sig_pending)!=-1) {
82+
sigpipe_pending = sigismember(&sig_pending, SIGPIPE);
83+
}
84+
85+
if (sigpipe_pending==-1) {
86+
pthread_sigmask(SIG_SETMASK, &sig_restore, NULL);
87+
return -1;
7188
}
7289

73-
struct sigaction ignore_act;
74-
memset(&ignore_act, '\0', sizeof(ignore_act));
75-
memset(*replaced_action, '\0', sizeof(struct sigaction));
76-
ignore_act.sa_handler = SIG_IGN;
77-
ignore_act.sa_flags = 0;
78-
return sigaction(SIGPIPE, &ignore_act, *replaced_action);
90+
if (*replaced_action==NULL) {
91+
*replaced_action = malloc(sizeof(sigset_t));
92+
}
93+
memcpy(*replaced_action, &sig_restore, sizeof(sigset_t));
94+
return 0;
7995
#endif
8096
UNUSED(replaced_action);
8197
return BOLT_SUCCESS;
@@ -85,10 +101,26 @@ int socket_restore_sigpipe(void** action_to_restore)
85101
{
86102
#if !defined(SO_NOSIGPIPE)
87103
if (action_to_restore!=NULL) {
88-
int result = sigaction(SIGPIPE, *action_to_restore, NULL);
104+
sigset_t sig_block;
105+
106+
sigemptyset(&sig_block);
107+
sigaddset(&sig_block, SIGPIPE);
108+
109+
struct timespec ts;
110+
ts.tv_sec = 0;
111+
ts.tv_nsec = 0;
112+
113+
while (sigtimedwait(&sig_block, 0, &ts)==-1) {
114+
if (errno!=EINTR) {
115+
break;
116+
}
117+
}
118+
119+
pthread_sigmask(SIG_SETMASK, (sigset_t*) *action_to_restore, NULL);
120+
89121
free(*action_to_restore);
90122
*action_to_restore = NULL;
91-
return result;
123+
return 0;
92124
}
93125
#endif
94126
UNUSED(action_to_restore);

src/seabolt/src/bolt/communication-secure-openssl.c

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,19 @@ void BoltSecurityContext_destroy(BoltSecurityContext* context)
214214

215215
static mutex_t* locks;
216216

217-
unsigned long secure_openssl_id_callback(void);
217+
struct CRYPTO_dynlock_value {
218+
mutex_t mutex;
219+
};
218220

219-
void secure_openssl_locking_callback(int mode, int type, const char* file, int line);
221+
static unsigned long secure_openssl_id_callback(void);
222+
223+
static void secure_openssl_locking_callback(int mode, int type, const char* file, int line);
224+
225+
static struct CRYPTO_dynlock_value* secure_openssl_create_mutex(const char* file, int line);
226+
227+
static void secure_openssl_lock_mutext(int mode, struct CRYPTO_dynlock_value* l, const char* file, int line);
228+
229+
static void secure_openssl_destroy_mutex(struct CRYPTO_dynlock_value* l, const char* file, int line);
220230

221231
void CRYPTO_thread_setup(void)
222232
{
@@ -230,21 +240,31 @@ void CRYPTO_thread_setup(void)
230240
BoltSync_mutex_create(&locks[i]);
231241
}
232242

233-
CRYPTO_set_id_callback(&secure_openssl_id_callback);
234-
CRYPTO_set_locking_callback(&secure_openssl_locking_callback);
243+
CRYPTO_set_id_callback(secure_openssl_id_callback);
244+
CRYPTO_set_locking_callback(secure_openssl_locking_callback);
245+
246+
CRYPTO_set_dynlock_create_callback(secure_openssl_create_mutex);
247+
CRYPTO_set_dynlock_lock_callback(secure_openssl_lock_mutext);
248+
CRYPTO_set_dynlock_destroy_callback(secure_openssl_destroy_mutex);
235249
}
236250

237-
static void CRYPTO_thread_cleanup(void)
251+
void CRYPTO_thread_cleanup(void)
238252
{
239-
int i;
253+
CRYPTO_set_id_callback(NULL);
254+
CRYPTO_set_locking_callback(NULL);
255+
256+
CRYPTO_set_dynlock_create_callback(NULL);
257+
CRYPTO_set_dynlock_lock_callback(NULL);
258+
CRYPTO_set_dynlock_destroy_callback(NULL);
240259

241260
CRYPTO_set_locking_callback(NULL);
242-
for (i = 0; i<CRYPTO_num_locks(); i++)
261+
for (int i = 0; i<CRYPTO_num_locks(); i++) {
243262
BoltSync_mutex_destroy(&locks[i]);
263+
}
244264
OPENSSL_free(locks);
245265
}
246266

247-
void secure_openssl_locking_callback(int mode, int type, const char* file, int line)
267+
static void secure_openssl_locking_callback(int mode, int type, const char* file, int line)
248268
{
249269
UNUSED(file);
250270
UNUSED(line);
@@ -257,18 +277,52 @@ void secure_openssl_locking_callback(int mode, int type, const char* file, int l
257277
}
258278
}
259279

260-
unsigned long secure_openssl_id_callback(void)
280+
static unsigned long secure_openssl_id_callback(void)
261281
{
262282
return BoltThread_id();
263283
}
264284

285+
static struct CRYPTO_dynlock_value* secure_openssl_create_mutex(const char* file, int line)
286+
{
287+
UNUSED(file);
288+
UNUSED(line);
289+
290+
struct CRYPTO_dynlock_value* value;
291+
value = (struct CRYPTO_dynlock_value*) malloc(sizeof(struct CRYPTO_dynlock_value));
292+
if (!value) return NULL;
293+
BoltSync_mutex_create(&value->mutex);
294+
return value;
295+
}
296+
297+
static void secure_openssl_lock_mutext(int mode, struct CRYPTO_dynlock_value* l, const char* file, int line)
298+
{
299+
UNUSED(file);
300+
UNUSED(line);
301+
302+
if (mode & CRYPTO_LOCK) {
303+
BoltSync_mutex_lock(&l->mutex);
304+
}
305+
else {
306+
BoltSync_mutex_unlock(&l->mutex);
307+
}
308+
}
309+
310+
static void secure_openssl_destroy_mutex(struct CRYPTO_dynlock_value* l, const char* file, int line)
311+
{
312+
UNUSED(file);
313+
UNUSED(line);
314+
315+
BoltSync_mutex_destroy(&l->mutex);
316+
free(l);
317+
}
318+
265319
#endif
266320

267321
int BoltSecurityContext_startup()
268322
{
269323
#if OPENSSL_VERSION_NUMBER<0x10100000L
270-
SSL_library_init();
271324
CRYPTO_thread_setup();
325+
SSL_library_init();
272326
#else
273327
OPENSSL_init_ssl(0, NULL);
274328
#endif

0 commit comments

Comments
 (0)