]> git.ipfire.org Git - thirdparty/openldap.git/commitdiff
Introduce mutex checks
authorOndřej Kuzník <okuznik@symas.com>
Wed, 5 Dec 2018 13:54:37 +0000 (13:54 +0000)
committerOndřej Kuzník <okuznik@symas.com>
Tue, 17 Nov 2020 17:58:15 +0000 (17:58 +0000)
Switched off unless thread debugging is on, but still useful for static
analysis.

servers/lloadd/backend.c
servers/lloadd/bind.c
servers/lloadd/client.c
servers/lloadd/connection.c
servers/lloadd/lload.h
servers/lloadd/operation.c
servers/lloadd/upstream.c

index 7b975687c83a4669175912e002e424382a2c8f79..22dcc562cb524c3fb0ae59be0f51364ee65ac6c6 100644 (file)
@@ -284,6 +284,8 @@ backend_select( LloadOperation *op, int *res )
 
                 checked_unlock( &b->b_mutex );
                 *res = LDAP_SUCCESS;
+                CONNECTION_ASSERT_LOCKED(c);
+                assert_locked( &c->c_io_mutex );
                 return c;
             }
             CONNECTION_UNLOCK(c);
@@ -311,6 +313,7 @@ backend_retry( LloadBackend *b )
                 "shutting down\n" );
         return;
     }
+    assert_locked( &b->b_mutex );
 
     requested = b->b_numconns;
 #ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
@@ -323,6 +326,7 @@ backend_retry( LloadBackend *b )
     if ( b->b_active + b->b_bindavail + b->b_opening >= requested ) {
         Debug( LDAP_DEBUG_CONNS, "backend_retry: "
                 "no more connections needed for this backend\n" );
+        assert_locked( &b->b_mutex );
         return;
     }
 
@@ -330,6 +334,7 @@ backend_retry( LloadBackend *b )
         Debug( LDAP_DEBUG_CONNS, "backend_retry: "
                 "retry in progress already\n" );
         assert( b->b_opening == 1 );
+        assert_locked( &b->b_mutex );
         return;
     }
 
@@ -343,6 +348,7 @@ backend_retry( LloadBackend *b )
                 "scheduling a retry in %d ms\n",
                 b->b_retry_timeout );
         event_add( b->b_retry_event, &b->b_retry_tv );
+        assert_locked( &b->b_mutex );
         return;
     }
 
@@ -359,6 +365,7 @@ backend_retry( LloadBackend *b )
         b->b_failed++;
         event_add( b->b_retry_event, &b->b_retry_tv );
     }
+    assert_locked( &b->b_mutex );
 }
 
 void
@@ -517,6 +524,7 @@ backend_connect_task( void *ctx, void *arg )
 void
 backend_reset( LloadBackend *b, int gentle )
 {
+    assert_locked( &b->b_mutex );
     if ( b->b_cookie ) {
         if ( ldap_pvt_thread_pool_retract( b->b_cookie ) ) {
             b->b_cookie = NULL;
@@ -582,6 +590,7 @@ backend_reset( LloadBackend *b, int gentle )
     connections_walk_last( &b->b_mutex, &b->b_conns, b->b_last_conn,
             lload_connection_close, &gentle );
     assert( gentle || b->b_active == 0 );
+    assert_locked( &b->b_mutex );
 }
 
 void
index 7636e5191060c0b342ecbdbfcc56c52859813cfd..e3ee3b88a25149b59a1f66685454393ad56afe58 100644 (file)
@@ -37,6 +37,7 @@ bind_mech_external(
     char *ptr, *message = "";
     int result = LDAP_SUCCESS;
 
+    CONNECTION_ASSERT_LOCKED(client);
     client->c_state = LLOAD_C_READY;
     client->c_type = LLOAD_C_OPEN;
 
@@ -370,6 +371,7 @@ request_bind( LloadConnection *client, LloadOperation *op )
         assert( client->c_pin_id == 0 );
         goto done;
     }
+    assert_locked( &upstream->c_io_mutex );
     /*
      * At this point, either:
      * - upstream is READY and pin == 0
@@ -524,6 +526,7 @@ finish_sasl_bind(
     ber_int_t msgid;
     int rc;
 
+    CONNECTION_ASSERT_LOCKED(upstream);
     removed = tavl_delete( &upstream->c_ops, op, operation_upstream_cmp );
     if ( !removed ) {
         assert( upstream->c_state != LLOAD_C_BINDING );
index 8dc6b9a9866260de48ca636833df00041a4b0a79..f3e2870b666f955cabf656bf1bceb7f104636035 100644 (file)
@@ -102,6 +102,8 @@ request_process( LloadConnection *client, LloadOperation *op )
         operation_send_reject( op, res, "no connections available", 1 );
         goto fail;
     }
+    CONNECTION_ASSERT_LOCKED(upstream);
+    assert_locked( &upstream->c_io_mutex );
     op->o_upstream = upstream;
     op->o_upstream_connid = upstream->c_connid;
     op->o_res = LLOAD_OP_FAILED;
@@ -481,6 +483,7 @@ client_reset( LloadConnection *c )
     TAvlnode *root;
     long freed = 0, executing;
 
+    CONNECTION_ASSERT_LOCKED(c);
     root = c->c_ops;
     c->c_ops = NULL;
     executing = c->c_n_ops_executing;
@@ -505,6 +508,7 @@ client_reset( LloadConnection *c )
     assert( freed == executing );
 
     CONNECTION_LOCK(c);
+    CONNECTION_ASSERT_LOCKED(c);
 }
 
 void
@@ -517,6 +521,7 @@ client_unlink( LloadConnection *c )
             "removing client connid=%lu\n",
             c->c_connid );
 
+    CONNECTION_ASSERT_LOCKED(c);
     assert( c->c_state != LLOAD_C_INVALID );
     assert( c->c_state != LLOAD_C_DYING );
 
@@ -543,6 +548,7 @@ client_unlink( LloadConnection *c )
 
     CONNECTION_LOCK(c);
     client_reset( c );
+    CONNECTION_ASSERT_LOCKED(c);
 }
 
 void
index e884965ff835e5a5a3b40e684ebfec6e535cabc2..0bcbc73fcbe429fe9f001e5794e60ea4e96820f9 100644 (file)
@@ -332,6 +332,7 @@ connection_destroy( LloadConnection *c )
             "destroying connection connid=%lu\n",
             c->c_connid );
 
+    CONNECTION_ASSERT_LOCKED(c);
     assert( c->c_live == 0 );
     assert( c->c_refcnt == 0 );
     assert( c->c_state == LLOAD_C_INVALID );
@@ -394,12 +395,15 @@ connections_walk_last(
     if ( LDAP_CIRCLEQ_EMPTY( cq ) ) {
         return;
     }
+    assert_locked( cq_mutex );
+
     last_connid = c->c_connid;
     c = LDAP_CIRCLEQ_LOOP_NEXT( cq, c, c_next );
 
     while ( !acquire_ref( &c->c_refcnt ) ) {
         c = LDAP_CIRCLEQ_LOOP_NEXT( cq, c, c_next );
         if ( c->c_connid >= last_connid ) {
+            assert_locked( cq_mutex );
             return;
         }
     }
@@ -432,10 +436,12 @@ connections_walk_last(
             LloadConnection *old = c;
             c = LDAP_CIRCLEQ_LOOP_NEXT( cq, c, c_next );
             if ( c->c_connid <= old->c_connid || c->c_connid > last_connid ) {
+                assert_locked( cq_mutex );
                 return;
             }
         } while ( !acquire_ref( &c->c_refcnt ) );
     } while ( c->c_connid <= last_connid );
+    assert_locked( cq_mutex );
 }
 
 void
index b8726cf5aefc61dc58cb5581a20091d2e6e7492d..0de410bebe52d4aa5095fa9f5a762333030e1962 100644 (file)
@@ -91,6 +91,13 @@ LDAP_BEGIN_DECL
 #define checked_unlock( mutex ) \
     if ( ldap_pvt_thread_mutex_unlock( mutex ) != 0 ) assert(0)
 
+#ifdef LDAP_THREAD_DEBUG
+#define assert_locked( mutex ) \
+    if ( ldap_pvt_thread_mutex_trylock( mutex ) == 0 ) assert(0)
+#else
+#define assert_locked( mutex ) ( (void)0 )
+#endif
+
 typedef struct LloadBackend LloadBackend;
 typedef struct LloadPendingConnection LloadPendingConnection;
 typedef struct LloadConnection LloadConnection;
@@ -302,6 +309,7 @@ struct LloadConnection {
     CONNECTION_DESTROY_CB c_unlink;
     CONNECTION_DESTROY_CB c_destroy;
     CONNECTION_PDU_CB c_pdu_cb;
+#define CONNECTION_ASSERT_LOCKED(c) assert_locked( &(c)->c_mutex )
 #define CONNECTION_LOCK(c) \
     do { \
         checked_lock( &(c)->c_mutex ); \
index 17b8526ae23a9911cdbed524f2860d3a45e6ade9..f959701c44ecbaa68d59d3d87974a61f4c638a6c 100644 (file)
@@ -146,6 +146,7 @@ operation_init( LloadConnection *c, BerElement *ber )
         goto fail;
     }
 
+    CONNECTION_ASSERT_LOCKED(c);
     rc = tavl_insert( &c->c_ops, op, operation_client_cmp, avl_dup_error );
     if ( rc ) {
         Debug( LDAP_DEBUG_PACKETS, "operation_init: "
index 81ceb8a5242dbe93649d68da1002c102c9abf890..dd153923b5cc50d3e4b9b2ccce725329da02d389 100644 (file)
@@ -117,6 +117,7 @@ forward_final_response(
 static int
 handle_unsolicited( LloadConnection *c, BerElement *ber )
 {
+    CONNECTION_ASSERT_LOCKED(c);
     if ( c->c_state != LLOAD_C_PREPARING ) {
         c->c_state = LLOAD_C_CLOSING;
     }
@@ -605,6 +606,8 @@ upstream_finish( LloadConnection *c )
     LloadBackend *b = c->c_private;
     int is_bindconn = 0;
 
+    assert_locked( &b->b_mutex );
+    CONNECTION_ASSERT_LOCKED(c);
     assert( c->c_live );
     c->c_pdu_cb = handle_one_response;
 
@@ -975,6 +978,7 @@ upstream_unlink( LloadConnection *c )
     Debug( LDAP_DEBUG_CONNS, "upstream_unlink: "
             "removing upstream connid=%lu\n",
             c->c_connid );
+    CONNECTION_ASSERT_LOCKED(c);
 
     assert( c->c_state != LLOAD_C_INVALID );
     assert( c->c_state != LLOAD_C_DYING );
@@ -1042,6 +1046,7 @@ upstream_unlink( LloadConnection *c )
     checked_unlock( &b->b_mutex );
 
     CONNECTION_LOCK(c);
+    CONNECTION_ASSERT_LOCKED(c);
 }
 
 void