Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix block object leak in the case of default allocation. #456

Merged
merged 8 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading