From dc53b767e3f48b66b7b2b794d4c53cdc9475e57e Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Mon, 25 Mar 2024 07:37:58 +0800 Subject: [PATCH] test: do not increase ref when creating intrusive_ptr before this change, we increment the refcount when constructing `cct` instrusive_ptr, but nobody owns this smart pointer. also, `CephContext` 's constructor set its refcount to 1. so, when the test finishes, the refcount is 1, and this leads to a leakage of the `CephContext` instance, this not only annoys ASan, and defeats the purpose of 14d878c8. ``` Indirect leak of 10880000 byte(s) in 1 object(s) allocated from: #0 0x5564d173537d in operator new(unsigned long) (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_ipaddr+0x19b37d) (BuildId: 45c0c7f28b253c04fcb7bb1a43aed52a5526d734) #1 0x7fe7f2ccd189 in __gnu_cxx::new_allocator::allocate(unsigned long, void const*) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ext/new_allocator.h:127:27 #2 0x7fe7f2ccc563 in std::allocator::allocate(unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/allocator.h:185:32 #3 0x7fe7f2ccc563 in boost::circular_buffer >::allocate(unsigned long) /opt/ceph/include/boost/circular_buffer/base.hpp:2396:39 #4 0x7fe7f2ccc2c0 in boost::circular_buffer >::initialize_buffer(unsigned long) /opt/ceph/include/boost/circular_buffer/base.hpp:2494:18 #5 0x7fe7f2cc6192 in boost::circular_buffer >::circular_buffer(unsigned long, std::allocator const&) /opt/ceph/include/boost/circular_buffer/base.hpp:1039:9 #6 0x7fe7f2cb91e4 in ceph::logging::Log::Log(ceph::logging::SubsystemMap const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/log/Log.cc:53:5 #7 0x7fe7f1f8f96d in ceph::common::CephContext::CephContext(unsigned int, ceph::common::CephContext::create_options const&) /home/jenkins-build/build/workspace/ceph-pull-requests/src/common/ceph_context.cc:729:16 #8 0x7fe7f1f8e93b in ceph::common::CephContext::CephContext(unsigned int, code_environment_t, int) /home/jenkins-build/build/workspace/ceph-pull-requests/src/common/ceph_context.cc:697:5 #9 0x5564d1752eb9 in pick_address_find_ip_in_subnet_list_Test::TestBody() /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/test_ipaddr.cc:706:47 #10 0x5564d18694d6 in void testing::internal::HandleSehExceptionsInMethodIfSupported(testing::Test*, void (testing::Test::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2605:10 #11 0x5564d1820fc2 in void testing::internal::HandleExceptionsInMethodIfSupported(testing::Test*, void (testing::Test::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2641:14 #12 0x5564d17d19dc in testing::Test::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2680:5 #13 0x5564d17d3a12 in testing::TestInfo::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2858:11 #14 0x5564d17d504b in testing::TestSuite::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:3012:28 #15 0x5564d17f24d8 in testing::internal::UnitTestImpl::RunAllTests() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:5723:44 #16 0x5564d1871d06 in bool testing::internal::HandleSehExceptionsInMethodIfSupported(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2605:10 #17 0x5564d1827932 in bool testing::internal::HandleExceptionsInMethodIfSupported(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2641:14 #18 0x5564d17f1862 in testing::UnitTest::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:5306:10 #19 0x5564d1775d80 in RUN_ALL_TESTS() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/include/gtest/gtest.h:2486:46 #20 0x5564d1775d11 in main /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googlemock/src/gmock_main.cc:70:10 ``` so, in this change, we do not increase the refcount when creating cct. the same applies to `test/common/test_fault_injector.cc`. Signed-off-by: Kefu Chai --- src/test/common/test_fault_injector.cc | 5 +++-- src/test/test_ipaddr.cc | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/common/test_fault_injector.cc b/src/test/common/test_fault_injector.cc index dfa147478ea58..9b5eb1595bc67 100644 --- a/src/test/common/test_fault_injector.cc +++ b/src/test/common/test_fault_injector.cc @@ -40,8 +40,9 @@ class Fixture : public testing::Test { protected: void SetUp() override { CephInitParameters params(CEPH_ENTITY_TYPE_CLIENT); - cct = common_preinit(params, CODE_ENVIRONMENT_UTILITY, - CINIT_FLAG_NO_DEFAULT_CONFIG_FILE); + cct.reset(common_preinit(params, CODE_ENVIRONMENT_UTILITY, + CINIT_FLAG_NO_DEFAULT_CONFIG_FILE), + false); prefix.emplace(cct.get(), ceph_subsys_context); } void TearDown() override { diff --git a/src/test/test_ipaddr.cc b/src/test/test_ipaddr.cc index bc8dbef70d7e1..8b362df9880d8 100644 --- a/src/test/test_ipaddr.cc +++ b/src/test/test_ipaddr.cc @@ -703,7 +703,7 @@ TEST(pick_address, find_ip_in_subnet_list) ipv4(&a_two, "10.2.1.123"); ipv6(&a_three, "2001:1234:5678:90ab::cdef"); - boost::intrusive_ptr cct = new CephContext(CEPH_ENTITY_TYPE_OSD); + boost::intrusive_ptr cct{new CephContext(CEPH_ENTITY_TYPE_OSD), false}; // match by network result = find_ip_in_subnet_list(