]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
test/mon/test_config_map: free CephContext created with new
authorKefu Chai <tchaikov@gmail.com>
Tue, 26 Mar 2024 02:50:16 +0000 (10:50 +0800)
committerKefu Chai <tchaikov@gmail.com>
Tue, 26 Mar 2024 03:06:54 +0000 (11:06 +0800)
before this change, we create a new cct instance with `new`, but
we never free this instance after done with it. and LeakSanitizer
points this out:

```
Indirect leak of 10880000 byte(s) in 1 object(s) allocated from:
    #0 0x561afe148fed in operator new(unsigned long) (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_config_map+0x1c2fed) (BuildId: 3ce9eeed38cee335628fa74fdd08cd215b15019e)
    #1 0x7f37dc9ac189 in __gnu_cxx::new_allocator<ceph::logging::ConcreteEntry>::allocate(unsigned long, void const*) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ext/new_allocator.h:127:27
    #2 0x7f37dc9ab563 in std::allocator<ceph::logging::ConcreteEntry>::allocate(unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/allocator.h:185:32
    #3 0x7f37dc9ab563 in boost::circular_buffer<ceph::logging::ConcreteEntry, std::allocator<ceph::logging::ConcreteEntry> >::allocate(unsigned long) /opt/ceph/include/boost/circular_buffer/base.hpp:2396:39
    #4 0x7f37dc9ab2c0 in boost::circular_buffer<ceph::logging::ConcreteEntry, std::allocator<ceph::logging::ConcreteEntry> >::initialize_buffer(unsigned long) /opt/ceph/include/boost/circular_buffer/base.hpp:2494:18
    #5 0x7f37dc9a5192 in boost::circular_buffer<ceph::logging::ConcreteEntry, std::allocator<ceph::logging::ConcreteEntry> >::circular_buffer(unsigned long, std::allocator<ceph::logging::ConcreteEntry> const&) /opt/ceph/include/boost/circular_buffer/base.hpp:1039:9
    #6 0x7f37dc9981e4 in ceph::logging::Log::Log(ceph::logging::SubsystemMap const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/log/Log.cc:53:5
    #7 0x7f37dbc6e96d 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 0x7f37dbc6d93b 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 0x561afe14e983 in ConfigMap_add_option_Test::TestBody() /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/mon/test_config_map.cc:58:18
    #10 0x561afe2689b6 in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2605:10
    #11 0x561afe221262 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2641:14
    #12 0x561afe1d1f7c in testing::Test::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2680:5
    #13 0x561afe1d3fb2 in testing::TestInfo::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2858:11
    #14 0x561afe1d55eb in testing::TestSuite::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:3012:28
    #15 0x561afe1f2a78 in testing::internal::UnitTestImpl::RunAllTests() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:5723:44
    #16 0x561afe2711e6 in bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(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 0x561afe227bd2 in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(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 0x561afe1f1e02 in testing::UnitTest::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:5306:10
    #19 0x561afe176ec0 in RUN_ALL_TESTS() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/include/gtest/gtest.h:2486:46
    #20 0x561afe176e51 in main /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googlemock/src/gmock_main.cc:70:10
    #21 0x7f37d9397d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
```

so in this change, we manage the `CephContext` pointer with a smart
pointer. because the size of CephContext could be large, we don't create
it on stack.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/test/mon/test_config_map.cc

index ab148a10bca6864cc3fd17ea03dc7d648dbd63b6..75fbe1ce526e2dca614b52450bc07ef923f57ad7 100644 (file)
@@ -55,11 +55,11 @@ TEST(ConfigMap, parse_key)
 TEST(ConfigMap, add_option)
 {
   ConfigMap cm;
-  auto cct = new CephContext(CEPH_ENTITY_TYPE_MON);
+  boost::intrusive_ptr<CephContext> cct{new CephContext(CEPH_ENTITY_TYPE_CLIENT), false};
   int r;
 
   r = cm.add_option(
-    cct, "foo", "global", "fooval",
+    cct.get(), "foo", "global", "fooval",
     [&](const std::string& name) {
       return nullptr;
     });
@@ -67,7 +67,7 @@ TEST(ConfigMap, add_option)
   ASSERT_EQ(1, cm.global.options.size());
 
   r = cm.add_option(
-    cct, "foo", "mon", "fooval",
+    cct.get(), "foo", "mon", "fooval",
     [&](const std::string& name) {
       return nullptr;
     });
@@ -76,7 +76,7 @@ TEST(ConfigMap, add_option)
   ASSERT_EQ(1, cm.by_type["mon"].options.size());
   
   r = cm.add_option(
-    cct, "foo", "mon.a", "fooval",
+    cct.get(), "foo", "mon.a", "fooval",
     [&](const std::string& name) {
       return nullptr;
     });
@@ -89,14 +89,14 @@ TEST(ConfigMap, add_option)
 TEST(ConfigMap, result_sections)
 {
   ConfigMap cm;
-  auto cct = new CephContext(CEPH_ENTITY_TYPE_MON);
+  boost::intrusive_ptr<CephContext> cct{new CephContext(CEPH_ENTITY_TYPE_CLIENT), false};
   auto crush = new CrushWrapper;
   crush->finalize();
 
   int r;
 
   r = cm.add_option(
-    cct, "foo", "global", "g",
+    cct.get(), "foo", "global", "g",
     [&](const std::string& name) {
       return nullptr;
     });
@@ -104,7 +104,7 @@ TEST(ConfigMap, result_sections)
   ASSERT_EQ(1, cm.global.options.size());
 
   r = cm.add_option(
-    cct, "foo", "mon", "m",
+    cct.get(), "foo", "mon", "m",
     [&](const std::string& name) {
       return nullptr;
     });
@@ -113,7 +113,7 @@ TEST(ConfigMap, result_sections)
   ASSERT_EQ(1, cm.by_type["mon"].options.size());
 
   r = cm.add_option(
-    cct, "foo", "mon.a", "a",
+    cct.get(), "foo", "mon.a", "a",
     [&](const std::string& name) {
       return nullptr;
     });