From d3c5a4abcd63f83160ce8498925c66483a41a395 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Thu, 28 Mar 2024 10:01:46 +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. and LeakSanitizer points this out: ``` Indirect leak of 10880000 byte(s) in 1 object(s) allocated from: #0 0x558d341d837d in operator new(unsigned long) (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_ipaddr+0x19b37d) (BuildId: 1b7e7e5abfc2b58ce2334712e4c00b2441c25870) #1 0x7fd74c957559 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 0x7fd74c956933 in std::allocator::allocate(unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/allocator.h:185:32 #3 0x7fd74c956933 in boost::circular_buffer >::allocate(unsigned long) /opt/ceph/include/boost/circular_buffer/base.hpp:2396:39 #4 0x7fd74c956690 in boost::circular_buffer >::initialize_buffer(unsigned long) /opt/ceph/include/boost/circular_buffer/base.hpp:2494:18 #5 0x7fd74c950562 in boost::circular_buffer >::circular_buffer(unsigned long, std::allocator const&) /opt/ceph/include/boost/circ ular_buffer/base.hpp:1039:9 #6 0x7fd74c9435b4 in ceph::logging::Log::Log(ceph::logging::SubsystemMap const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/log/Log.cc:53:5 #7 0x7fd74bc1891d 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 0x7fd74bc178eb 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 0x558d341f97e9 in pick_address_filtering_Test::TestBody() /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/test_ipaddr.cc:774:47 #10 0x558d3430c4f6 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 0x558d342c3fc2 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 0x558d342749dc in testing::Test::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2680:5 #13 0x558d34276a12 in testing::TestInfo::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2858:11 #14 0x558d3427804b in testing::TestSuite::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:3012:28 #15 0x558d342954d8 in testing::internal::UnitTestImpl::RunAllTests() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:5723:44 #16 0x558d34314d26 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 0x558d342ca932 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 0x558d34294862 in testing::UnitTest::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:5306:10 #19 0x558d34218d80 in RUN_ALL_TESTS() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/include/gtest/gtest.h:2486:46 #20 0x558d34218d11 in main /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googlemock/src/gmock_main.cc:70:10 #21 0x7fd749331d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16 ``` so, in this change, we do not increase the refcount when creating cct. Signed-off-by: Kefu Chai --- src/test/mon/MonMap.cc | 10 +++++----- src/test/rgw/test_rgw_lc.cc | 8 ++++---- src/test/test_ipaddr.cc | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/test/mon/MonMap.cc b/src/test/mon/MonMap.cc index 81b054a9048f1..5dbf2b65bc422 100644 --- a/src/test/mon/MonMap.cc +++ b/src/test/mon/MonMap.cc @@ -98,7 +98,7 @@ TEST_F(MonMapTest, DISABLED_build_initial_config_from_dns) { - boost::intrusive_ptr cct = new CephContext(CEPH_ENTITY_TYPE_MON); + boost::intrusive_ptr cct(new CephContext(CEPH_ENTITY_TYPE_MON), false); cct->_conf.set_val("mon_dns_srv_name", "cephmon"); MonMap monmap; int r = monmap.build_initial(cct.get(), false, std::cerr); @@ -135,7 +135,7 @@ TEST_F(MonMapTest, DISABLED_build_initial_config_from_dns_fail) { .WillOnce(Return(0)); #endif - boost::intrusive_ptr cct = new CephContext(CEPH_ENTITY_TYPE_MON); + boost::intrusive_ptr cct(new CephContext(CEPH_ENTITY_TYPE_MON), false); // using default value of mon_dns_srv_name option MonMap monmap; int r = monmap.build_initial(cct.get(), false, std::cerr); @@ -196,7 +196,7 @@ TEST_F(MonMapTest, DISABLED_build_initial_config_from_dns_with_domain) { - boost::intrusive_ptr cct = new CephContext(CEPH_ENTITY_TYPE_MON); + boost::intrusive_ptr cct(new CephContext(CEPH_ENTITY_TYPE_MON), false); cct->_conf.set_val("mon_dns_srv_name", "cephmon_ceph.com"); MonMap monmap; int r = monmap.build_initial(cct.get(), false, std::cerr); @@ -221,7 +221,7 @@ TEST_F(MonMapTest, DISABLED_build_initial_config_from_dns_with_domain) { } TEST(MonMapBuildInitial, build_initial_mon_host_from_dns) { - boost::intrusive_ptr cct = new CephContext(CEPH_ENTITY_TYPE_MON); + boost::intrusive_ptr cct(new CephContext(CEPH_ENTITY_TYPE_MON), false); cct->_conf.set_val("mon_host", "ceph.io"); MonMap monmap; int r = monmap.build_initial(cct.get(), false, std::cerr); @@ -233,7 +233,7 @@ TEST(MonMapBuildInitial, build_initial_mon_host_from_dns) { } TEST(MonMapBuildInitial, build_initial_mon_host_from_dns_fail) { - boost::intrusive_ptr cct = new CephContext(CEPH_ENTITY_TYPE_MON); + boost::intrusive_ptr cct(new CephContext(CEPH_ENTITY_TYPE_MON), false); cct->_conf.set_val("mon_host", "ceph.noname"); MonMap monmap; int r = monmap.build_initial(cct.get(), false, std::cerr); diff --git a/src/test/rgw/test_rgw_lc.cc b/src/test/rgw/test_rgw_lc.cc index 7d24db622938b..7c7133f0338e6 100644 --- a/src/test/rgw/test_rgw_lc.cc +++ b/src/test/rgw/test_rgw_lc.cc @@ -186,7 +186,7 @@ TEST(TestLCConfigurationDecoder, XMLDoc5) struct LCWorkTimeTests : ::testing::Test { - CephContext* cct; + boost::intrusive_ptr cct; std::unique_ptr worker; // expects input in the form of "%m/%d/%y %H:%M:%S"; e.g., "01/15/23 23:59:01" @@ -234,15 +234,15 @@ struct LCWorkTimeTests : ::testing::Test protected: void SetUp() override { - cct = (new CephContext(CEPH_ENTITY_TYPE_ANY))->get(); + cct.reset(new CephContext(CEPH_ENTITY_TYPE_ANY), false); cct->_conf->set_value("rgw_lc_max_wp_worker", 0, 0); // no need to create a real workpool - worker = std::make_unique(nullptr, cct, nullptr, 0); + worker = std::make_unique(nullptr, cct.get(), nullptr, 0); } void TearDown() override { worker.reset(); - cct->put(); + cct.reset(); } }; diff --git a/src/test/test_ipaddr.cc b/src/test/test_ipaddr.cc index 8b362df9880d8..49038815318aa 100644 --- a/src/test/test_ipaddr.cc +++ b/src/test/test_ipaddr.cc @@ -771,7 +771,7 @@ TEST(pick_address, filtering) ipv4(&a_two, "10.2.1.123"); ipv6(&a_three, "2001:1234:5678:90ab::cdef"); - boost::intrusive_ptr cct = new CephContext(CEPH_ENTITY_TYPE_MON); + boost::intrusive_ptr cct(new CephContext(CEPH_ENTITY_TYPE_MON), false); cct->_conf._clear_safe_to_start_threads(); // so we can set configs cct->_conf.set_val("public_addr", ""); @@ -943,7 +943,7 @@ TEST(pick_address, ipv4_ipv6_enabled) ipv4(&a_one, "10.1.1.2"); - boost::intrusive_ptr cct = new CephContext(CEPH_ENTITY_TYPE_OSD); + boost::intrusive_ptr cct(new CephContext(CEPH_ENTITY_TYPE_OSD), false); cct->_conf._clear_safe_to_start_threads(); // so we can set configs cct->_conf.set_val("public_addr", ""); @@ -975,7 +975,7 @@ TEST(pick_address, ipv4_ipv6_enabled2) ipv6(&a_one, "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); cct->_conf._clear_safe_to_start_threads(); // so we can set configs cct->_conf.set_val("public_addr", "");