Browse Source

More bug fixes

Steven Engler 4 years ago
parent
commit
dac075e07a

+ 13 - 4
src/core/mainloop/connection.c

@@ -899,7 +899,7 @@ void
 connection_close_immediate(connection_t *conn)
 {
   assert_connection_ok(conn,0);
-  if (CONN_IS_CLOSED(conn)) {
+  if (!connection_uses_safe_conn(conn->type) && CONN_IS_CLOSED(conn)) {
     log_err(LD_BUG,"Attempt to close already-closed connection.");
     tor_fragile_assert();
     return;
@@ -4094,9 +4094,18 @@ connection_fetch_from_buf_http(connection_t *conn,
 int
 connection_wants_to_flush(connection_t *conn)
 {
-  tor_assert(conn->safe_conn == NULL);
-  tor_assert(!connection_uses_safe_conn(conn->type));
-  return conn->outbuf_flushlen > 0;
+  //tor_assert(conn->safe_conn == NULL);
+  //tor_assert(!connection_uses_safe_conn(conn->type));
+  if (!connection_uses_safe_conn(conn->type)) {
+    return conn->outbuf_flushlen > 0;
+  } else {
+    // TODO: fix this ugly locking
+    tor_assert(conn->safe_conn != NULL);
+    tor_mutex_acquire(&(conn->safe_conn->lock));
+    int rv = buf_datalen(conn->safe_conn->outbuf);
+    tor_mutex_release(&(conn->safe_conn->lock));
+    return rv;
+  }
 }
 
 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to

+ 11 - 3
src/core/or/channeltls.c

@@ -632,11 +632,19 @@ channel_tls_has_queued_writes_method(channel_t *chan)
              "something called has_queued_writes on a tlschan "
              "(%p with ID %"PRIu64 " but no conn",
              chan, (chan->global_identifier));
+    return 0;
   }
 
-  outbuf_len = (tlschan->conn != NULL) ?
-    connection_get_outbuf_len(TO_CONN(tlschan->conn)) :
-    0;
+  // TODO: fix this ugly locking
+  connection_t *conn = TO_CONN(tlschan->conn);
+  tor_assert(conn->safe_conn != NULL);
+  tor_mutex_acquire(&(conn->safe_conn->lock));
+  outbuf_len = buf_datalen(conn->safe_conn->outbuf);
+  tor_mutex_release(&(conn->safe_conn->lock));
+
+  //outbuf_len = (tlschan->conn != NULL) ?
+  //  connection_get_outbuf_len(TO_CONN(tlschan->conn)) :
+  //  0;
 
   return (outbuf_len > 0);
 }

+ 19 - 5
src/core/or/connection_or.c

@@ -721,7 +721,14 @@ connection_or_flushed_some(or_connection_t *conn)
 
   /* If we're under the low water mark, add cells until we're just over the
    * high water mark. */
-  datalen = connection_get_outbuf_len(TO_CONN(conn));
+
+  // TODO: fix this ugly locking
+  tor_assert(TO_CONN(conn)->safe_conn != NULL);
+  tor_mutex_acquire(&(TO_CONN(conn)->safe_conn->lock));
+  datalen = buf_datalen(TO_CONN(conn)->safe_conn->outbuf);
+  tor_mutex_release(&(TO_CONN(conn)->safe_conn->lock));
+  //datalen = connection_get_outbuf_len(TO_CONN(conn));
+
   if (datalen < OR_CONN_LOWWATER) {
     /* Let the scheduler know */
     scheduler_channel_wants_writes(TLS_CHAN_TO_BASE(conn->chan));
@@ -745,7 +752,14 @@ connection_or_num_cells_writeable(or_connection_t *conn)
    * writeable; note this is different from the calculation above
    * used to trigger when to start writing after we've stopped.
    */
-  datalen = connection_get_outbuf_len(TO_CONN(conn));
+
+  // TODO: fix this ugly locking
+  tor_assert(TO_CONN(conn)->safe_conn != NULL);
+  tor_mutex_acquire(&(TO_CONN(conn)->safe_conn->lock));
+  datalen = buf_datalen(TO_CONN(conn)->safe_conn->outbuf);
+  tor_mutex_release(&(TO_CONN(conn)->safe_conn->lock));
+  //datalen = connection_get_outbuf_len(TO_CONN(conn));
+
   if (datalen < OR_CONN_HIGHWATER) {
     cell_network_size = get_cell_network_size(conn->wide_circ_ids);
     n = CEIL_DIV(OR_CONN_HIGHWATER - datalen, cell_network_size);
@@ -2348,7 +2362,7 @@ connection_or_set_state_open(or_connection_t *conn)
 
   or_handshake_state_free(conn->handshake_state);
   conn->handshake_state = NULL;
-  connection_start_reading(TO_CONN(conn));
+  //connection_start_reading(TO_CONN(conn));
 
   return 0;
 }
@@ -2888,7 +2902,7 @@ connection_or_send_certs_cell(or_connection_t *conn)
   connection_or_write_var_cell_to_buf(cell, conn);
   var_cell_free(cell);
   certs_cell_free(certs_cell);
-  tor_x509_cert_free(own_link_cert);
+  //tor_x509_cert_free(own_link_cert);
 
   return 0;
 }
@@ -3117,7 +3131,7 @@ connection_or_compute_authenticate_cell_body(or_connection_t *conn,
     memcpy(auth->scert,
            tor_x509_cert_get_cert_digests(cert)->d[DIGEST_SHA256], 32);
 
-    tor_x509_cert_free(cert);
+    //tor_x509_cert_free(cert);
   }
 
   safe_connection_t *safe_conn = TO_CONN(conn)->safe_conn;

+ 17 - 14
src/core/or/safe_connection.c

@@ -520,9 +520,12 @@ safe_or_connection_get_tls_desc(safe_or_connection_t *safe_or_conn,
   tor_assert(safe_or_conn != NULL);
   tor_assert(buf != NULL);
   tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
-  tor_assert(safe_or_conn->tls != NULL);
 
-  tor_tls_get_state_description(safe_or_conn->tls, buf, buf_size);
+  if (safe_or_conn->tls != NULL) {
+    tor_tls_get_state_description(safe_or_conn->tls, buf, buf_size);
+  } else {
+    tor_snprintf(buf, buf_size, "<no tls object>");
+  }
 
   tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
 }
@@ -951,7 +954,7 @@ safe_or_connection_read_plaintext(safe_or_connection_t *safe_or_conn)
              bytes_read);
   }
 
-  log_debug(LD_NET, "OR plaintext read of %ld", (long)bytes_read);
+  log_debug(LD_NET, "OR plaintext read of %zu", bytes_read);
 
   safe_or_connection_decrement_buckets(safe_or_conn, bytes_read, 0);
   return E_SUCCESS;
@@ -971,7 +974,7 @@ safe_or_connection_read_encrypted(safe_or_connection_t *safe_or_conn,
     safe_or_connection_max_bytes_can_read(safe_or_conn, use_conn_buckets);
   // we may read slightly more than this due to pending TLS bytes
 
-  log_warn(LD_NET, "suggested_bytes_to_read: %ld", suggested_bytes_to_read);
+  log_warn(LD_NET, "suggested_bytes_to_read: %zu", suggested_bytes_to_read);
   // TODO: remove this ^^
 
   if (suggested_bytes_to_read == 0) {
@@ -1013,7 +1016,7 @@ safe_or_connection_read_encrypted(safe_or_connection_t *safe_or_conn,
   if (PREDICT_LIKELY(bytes_read < SIZE_MAX)) {
     size_t buf_len_diff = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf)-buf_initial_size;
     if (bytes_read != buf_len_diff) {
-      log_warn(LD_OR, "Doesn't match! bytes_read: %ld, buf_len_diff: %ld",
+      log_warn(LD_OR, "Doesn't match! bytes_read: %zu, buf_len_diff: %zu",
                bytes_read, buf_len_diff);
       tor_assert_nonfatal_unreached_once();
     }
@@ -1034,8 +1037,8 @@ safe_or_connection_read_encrypted(safe_or_connection_t *safe_or_conn,
   size_t tls_bytes_written = 0;
   tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
                           &tls_bytes_written);
-  log_warn(LD_NET, "After TLS read of %ld: %ld read, %ld written",
-            (long)bytes_read, (long)tls_bytes_read, (long)tls_bytes_written);
+  log_warn(LD_NET, "After TLS read of %zu: %zu read, %zu written",
+            bytes_read, tls_bytes_read, tls_bytes_written);
   // TODO: change this back to debug ^^
 
   safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
@@ -1058,7 +1061,7 @@ safe_or_connection_write_encrypted(safe_or_connection_t *safe_or_conn,
   size_t max_bytes_to_write = \
     safe_or_connection_max_bytes_can_write(safe_or_conn, use_conn_buckets);
 
-  log_warn(LD_NET, "max_bytes_to_write: %ld", max_bytes_to_write);
+  log_warn(LD_NET, "max_bytes_to_write: %zu", max_bytes_to_write);
   // TODO: remove this ^^
 
   if (max_bytes_to_write == 0) {
@@ -1105,7 +1108,7 @@ safe_or_connection_write_encrypted(safe_or_connection_t *safe_or_conn,
   if (PREDICT_LIKELY(bytes_written < SIZE_MAX)) {
     size_t buf_len_diff = buf_initial_size-buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf);
     if (bytes_written != buf_len_diff) {
-      log_warn(LD_OR, "Doesn't match! bytes_written: %ld, buf_len_diff: %ld",
+      log_warn(LD_OR, "Doesn't match! bytes_written: %zu, buf_len_diff: %zu",
                bytes_written, buf_len_diff);
       tor_assert_nonfatal_unreached_once();
     }
@@ -1130,8 +1133,8 @@ safe_or_connection_write_encrypted(safe_or_connection_t *safe_or_conn,
   size_t tls_bytes_written = 0;
   tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
                           &tls_bytes_written);
-  log_warn(LD_NET, "After TLS write of %ld: %ld read, %ld written",
-            (long)bytes_written, (long)tls_bytes_read, (long)tls_bytes_written);
+  log_warn(LD_NET, "After TLS write of %zu: %zu read, %zu written",
+            bytes_written, tls_bytes_read, tls_bytes_written);
   // TODO: change this back to debug ^^
 
   safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
@@ -1346,7 +1349,7 @@ safe_or_connection_write_cb(safe_connection_t *safe_conn)
     tor_error_t rv = safe_or_connection_check_tcp_connection(safe_or_conn);
     if (rv != E_SUCCESS) {
       tor_assert(safe_or_connection_update_state(safe_or_conn,
-        SAFE_OR_CONN_STATE_CLOSED));
+        SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
     }
     break;
   }
@@ -1362,7 +1365,7 @@ safe_or_connection_write_cb(safe_connection_t *safe_conn)
     tor_error_t rv = safe_or_connection_tls_handshake(safe_or_conn);
     if (rv != E_SUCCESS) {
       tor_assert(safe_or_connection_update_state(safe_or_conn,
-        SAFE_OR_CONN_STATE_CLOSED));
+        SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
     }
     break;
   }
@@ -1387,7 +1390,7 @@ safe_or_connection_write_cb(safe_connection_t *safe_conn)
                                                         use_conn_buckets);
     if (rv != E_SUCCESS) {
       tor_assert(safe_or_connection_update_state(safe_or_conn,
-        SAFE_OR_CONN_STATE_CLOSED));
+        SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
     }
     break;
   }

+ 27 - 6
src/core/or/scheduler_kist.c

@@ -123,7 +123,19 @@ channel_outbuf_length(channel_t *chan)
   if (SCHED_BUG(BASE_CHAN_TO_TLS(chan)->conn == NULL, chan)) {
     return 0;
   }
-  return buf_datalen(TO_CONN(BASE_CHAN_TO_TLS(chan)->conn)->outbuf);
+  //return buf_datalen(TO_CONN(BASE_CHAN_TO_TLS(chan)->conn)->outbuf);
+  //return connection_get_outbuf_len(TO_CONN(BASE_CHAN_TO_TLS(chan)->conn));
+  
+  size_t len = 0;
+
+  // TODO: fix this ugly locking
+  connection_t *conn = TO_CONN(BASE_CHAN_TO_TLS(chan)->conn);
+  tor_assert(conn->safe_conn != NULL);
+  tor_mutex_acquire(&(conn->safe_conn->lock));
+  len = buf_datalen(conn->safe_conn->outbuf);
+  tor_mutex_release(&(conn->safe_conn->lock));
+
+  return len;
 }
 
 /* Little helper function for HT_FOREACH_FN. */
@@ -197,8 +209,17 @@ update_socket_info_impl, (socket_table_ent_t *ent))
   int64_t tcp_space, extra_space;
   tor_assert(ent);
   tor_assert(ent->chan);
-  const tor_socket_t sock =
-    TO_CONN(BASE_CHAN_TO_TLS((channel_t *) ent->chan)->conn)->s;
+
+  //const tor_socket_t sock =
+  //  TO_CONN(BASE_CHAN_TO_TLS((channel_t *) ent->chan)->conn)->s;
+
+  // TODO: fix this ugly locking
+  connection_t *conn = TO_CONN(BASE_CHAN_TO_TLS((channel_t *) ent->chan)->conn);
+  tor_assert(conn->safe_conn != NULL);
+  tor_mutex_acquire(&(conn->safe_conn->lock));
+  const tor_socket_t sock = conn->safe_conn->socket;
+  tor_mutex_release(&(conn->safe_conn->lock));
+
   struct tcp_info tcp;
   socklen_t tcp_info_len = sizeof(tcp);
 
@@ -608,7 +629,7 @@ kist_scheduler_run(void)
     }
     if (prev_chan != chan) {
       if (channel_should_write_to_kernel(&outbuf_table, prev_chan)) {
-        channel_write_to_kernel(prev_chan);
+        //channel_write_to_kernel(prev_chan);
         outbuf_table_remove(&outbuf_table, prev_chan);
       }
       prev_chan = chan;
@@ -709,8 +730,8 @@ kist_scheduler_run(void)
   } /* End of main scheduling loop */
 
   /* Write the outbuf of any channels that still have data */
-  HT_FOREACH_FN(outbuf_table_s, &outbuf_table, each_channel_write_to_kernel,
-                NULL);
+  //HT_FOREACH_FN(outbuf_table_s, &outbuf_table, each_channel_write_to_kernel,
+  //              NULL);
   /* We are done with it. */
   HT_FOREACH_FN(outbuf_table_s, &outbuf_table, free_outbuf_info_by_ent, NULL);
   HT_CLEAR(outbuf_table_s, &outbuf_table);