From f7b5c10b2bc7b22f52b496c04946a7c0afb9e4e9 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 9 Feb 2012 06:24:58 +0000 Subject: [PATCH] add asserts to try to catch crash in peer_connection::received_valid_data --- include/libtorrent/peer_connection.hpp | 1 + include/libtorrent/policy.hpp | 2 +- src/peer_connection.cpp | 7 +++++++ src/policy.cpp | 14 +++++++------- src/torrent.cpp | 7 ++++++- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 3b68cc879..73d786246 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -1261,6 +1261,7 @@ namespace libtorrent bool m_in_constructor:1; bool m_disconnect_started:1; bool m_initialized:1; + int m_in_use; int m_received_in_piece; #endif }; diff --git a/include/libtorrent/policy.hpp b/include/libtorrent/policy.hpp index 7d2141d7f..40d7ee03f 100644 --- a/include/libtorrent/policy.hpp +++ b/include/libtorrent/policy.hpp @@ -311,7 +311,7 @@ namespace libtorrent // we have been connected via uTP at least once bool confirmed_supports_utp:1; bool supports_holepunch:1; -#ifdef TORRENT_DEBUG +#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS bool in_use:1; #endif }; diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index ded43a9d1..d78d40b4d 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -198,6 +198,7 @@ namespace libtorrent , m_in_constructor(true) , m_disconnect_started(false) , m_initialized(false) + , m_in_use(1337) , m_received_in_piece(0) #endif { @@ -349,6 +350,7 @@ namespace libtorrent , m_in_constructor(true) , m_disconnect_started(false) , m_initialized(false) + , m_in_use(1337) , m_received_in_piece(0) #endif { @@ -915,6 +917,10 @@ namespace libtorrent TORRENT_ASSERT(m_disconnecting); TORRENT_ASSERT(m_disconnect_started); +#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS + m_in_use = 0; +#endif + // defensive boost::shared_ptr t = m_torrent.lock(); @@ -5650,6 +5656,7 @@ namespace libtorrent void peer_connection::check_invariant() const { + TORRENT_ASSERT(m_in_use == 1337); TORRENT_ASSERT(m_queued_time_critical <= int(m_request_queue.size())); TORRENT_ASSERT(bool(m_disk_recv_buffer) == (m_disk_recv_buffer_size > 0)); diff --git a/src/policy.cpp b/src/policy.cpp index 3aac7e6e4..8e79712d1 100644 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -423,7 +423,7 @@ namespace libtorrent if (m_round_robin > i - m_peers.begin()) --m_round_robin; if (m_round_robin >= int(m_peers.size())) m_round_robin = 0; -#ifdef TORRENT_DEBUG +#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS TORRENT_ASSERT((*i)->in_use); (*i)->in_use = false; #endif @@ -933,7 +933,7 @@ namespace libtorrent #endif new (p) ipv4_peer(c.remote(), false, 0); -#ifdef TORRENT_DEBUG +#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS p->in_use = true; #endif @@ -1214,13 +1214,13 @@ namespace libtorrent m_torrent->session().m_i2p_peer_pool.set_next_size(500); new (p) i2p_peer(destination, true, src); -#ifdef TORRENT_DEBUG +#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS p->in_use = true; #endif if (!insert_peer(p, iter, flags)) { -#ifdef TORRENT_DEBUG +#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS p->in_use = false; #endif @@ -1339,13 +1339,13 @@ namespace libtorrent #endif new (p) ipv4_peer(remote, true, src); -#ifdef TORRENT_DEBUG +#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS p->in_use = true; #endif if (!insert_peer(p, iter, flags)) { -#ifdef TORRENT_DEBUG +#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS p->in_use = false; #endif #if TORRENT_USE_IPV6 @@ -1677,7 +1677,7 @@ namespace libtorrent , supports_utp(true) // assume peers support utp , confirmed_supports_utp(false) , supports_holepunch(false) -#ifdef TORRENT_DEBUG +#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS , in_use(false) #endif { diff --git a/src/torrent.cpp b/src/torrent.cpp index 6c1ca33da..9e4e05aa8 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -3140,12 +3140,17 @@ namespace libtorrent { policy::peer* p = static_cast(*i); if (p == 0) continue; + TORRENT_ASSERT(p->in_use); p->on_parole = false; int trust_points = p->trust_points; ++trust_points; if (trust_points > 8) trust_points = 8; p->trust_points = trust_points; - if (p->connection) p->connection->received_valid_data(index); + if (p->connection) + { + TORRENT_ASSERT(p->connection->m_in_use == 1337); + p->connection->received_valid_data(index); + } } if (settings().max_sparse_regions > 0