Skip to content

Commit

Permalink
Merge pull request #456 from evoskuil/master
Browse files Browse the repository at this point in the history
Fix block object leak in the case of default allocation.
  • Loading branch information
evoskuil authored Jan 31, 2025
2 parents 6a3fda0 + 52b0b8a commit 704358c
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
3 changes: 1 addition & 2 deletions builds/msvc/vs2022/libbitcoin-system.import.props
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
<!-- WIN32_LEAN_AND_MEAN avoids inclusion of certain headers, winsock.h conflicts with boost and protocol use of winsock2.h. -->
<PreprocessorDefinitions>WITH_ICU;WIN32_LEAN_AND_MEAN;NOMINMAX;_WIN32_WINNT=0x0600;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Linkage-libbitcoin-system)' == 'static' Or '$(Linkage-libbitcoin-system)' == 'ltcg'">BC_STATIC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<ClCompile>
<PreprocessorDefinitions Condition="$(Configuration.IndexOf('Debug')) != -1">_CRTDBG_MAP_ALLOC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<!-- Enable use of CPU intrinsics. -->
<PreprocessorDefinitions Condition="'$(Option-avx512)' == 'true'">WITH_AVX512;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Option-avx2)' == 'true'">WITH_AVX2;%(PreprocessorDefinitions)</PreprocessorDefinitions>
Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/network/net/distributor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ class BCT_API distributor
memory& memory_;
};

// block message uses specialized deserializer for memory management.
// Block message uses specialized deserializer for memory management.
// Other message types use default (unspecified) memory allocation.
template <>
code distributor::do_notify<messages::block>(
distributor::block_subscriber& subscriber, uint32_t version,
Expand Down
12 changes: 6 additions & 6 deletions src/messages/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ const uint32_t block::version_maximum = level::maximum_protocol;
typename block::cptr block::deserialize(uint32_t version,
const data_chunk& data, bool witness) NOEXCEPT
{
static default_memory memory{};
return deserialize(*memory.get_arena(), version, data, witness);
// default_arena::get() returns pointer to static instance of
// system::default_arena, which is not detachable and calls into
// std::malloc() and std::free() for each individual allocation.
return deserialize(*default_arena::get(), version, data, witness);
}

// static
Expand All @@ -58,8 +60,6 @@ typename block::cptr block::deserialize(arena& arena, uint32_t version,

// Set starting address of block allocation (nullptr if not detachable).
const auto memory = pointer_cast<uint8_t>(arena.start(data.size()));
if (is_null(memory))
return nullptr;

istream source{ data };
byte_reader reader{ source, &arena };
Expand All @@ -85,10 +85,10 @@ typename block::cptr block::deserialize(arena& arena, uint32_t version,

// All block and contained object destructors should be optimized out.
return to_shared<messages::block>(std::shared_ptr<chain::block>(block,
[&arena, memory](auto) NOEXCEPT
[&arena, memory](auto ptr) NOEXCEPT
{
// Destruct and deallocate objects (nop deallocate if detachable).
byte_allocator::deleter<chain::block>(&arena);
byte_allocator::deleter<chain::block>(&arena)(ptr);

// Deallocate detached memory (nop if not detachable).
arena.release(memory);
Expand Down
1 change: 0 additions & 1 deletion src/net/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <bitcoin/network/net/proxy.hpp>

#include <algorithm>
#include <functional>
#include <utility>
#include <bitcoin/system.hpp>
#include <bitcoin/network/async/async.hpp>
Expand Down
1 change: 0 additions & 1 deletion src/sessions/session_outbound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <bitcoin/network/sessions/session_outbound.hpp>

#include <algorithm>
#include <functional>
#include <utility>
#include <bitcoin/system.hpp>
#include <bitcoin/network/async/async.hpp>
Expand Down

0 comments on commit 704358c

Please sign in to comment.