From: Kefu Chai Date: Sun, 28 Apr 2024 01:05:07 +0000 (+0800) Subject: test//erasure-code/TestErasureCodeLrc: free allocated CrushWrapper X-Git-Tag: v20.0.0~2074^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F57113%2Fhead;p=ceph.git test//erasure-code/TestErasureCodeLrc: free allocated CrushWrapper in TestErasureCodeLrc.cc, we create a `CrushWrapper` without destroying and freeing it. and ASan points this out: ``` Indirect leak of 72000 byte(s) in 1000 object(s) allocated from: #0 0x559e00acc2bd in operator new(unsigned long) (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_erasure_code_lrc+0x1ed2bd) (BuildId: 1cd93d0f231006242239eb0d81a8d677e36aeba2) #1 0x559e00b2bb76 in __gnu_cxx::new_allocator, std::allocator > const, int> > >::allocate(unsigned long, void const*) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ext/new_allocator.h:127:27 #2 0x559e00b2bb00 in std::allocator, std::allocator > const, int> > >::allocate(unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/allocator.h:185:32 #3 0x559e00b2bb00 in std::allocator_traits, std::allocator > const, int> > > >::allocate(std::allocator, std::allocator > const, int> > >&, unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/alloc_traits.h:464:20 #4 0x559e00b2b9d1 in std::_Rb_tree, std::allocator >, std::pair, std::allocator > const, int>, std::_Select1st, std::allocator > const, int> >, std::less, std::allocator > >, std::allocator, std::allocator > const, int> > >::_M_get_node() /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_tree.h:561:16 #5 0x559e00b2aae4 in std::_Rb_tree_node, std::allocator > const, int> >* std::_Rb_tree, std::allocator >, std::pair, std::allocator > const, int>, std::_Select1st, std::allocator > const, int> >, std::less, std::allocator > >, std::allocator, std::allocator > const, int> > >::_M_create_node, std::allocator > const&>, std::tuple<> >(std::piecewise_construct_t const&, std::tuple, std::allocator > const&>&&, std::tuple<>&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_tree.h:611:23 #6 0x559e00b29520 in std::_Rb_tree_iterator, std::allocator > const, int> > std::_Rb_tree, std::allocator >, std::pair, std::allocator > const, int>, std::_Select1st, std::allocator > const, int> >, std::less, std::allocator > >, std::allocator, std::allocator > const, int> > >::_M_emplace_hint_unique, std::allocator > const&>, std::tuple<> >(std::_Rb_tree_const_iterator, std::allocator > const, int> >, std::piecewise_construct_t const&, std::tuple, std::allocator > const&>&&, std::tuple<>&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_tree.h:2431:19 #7 0x559e00b255c2 in std::map, std::allocator >, int, std::less, std::allocator > >, std::allocator, std::allocator > const, int> > >::operator[](std::__cxx11::basic_string, std::allocator > const&) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_map.h:501:15 #8 0x559e00b188da in CrushWrapper::set_item_name(int, std::__cxx11::basic_string, std::allocator > const&) /home/jenkins-build/build/workspace/ceph-pull-requests/src/crush/CrushWrapper.h:466:7 #9 0x7feb54a4dcc2 in CrushWrapper::insert_item(ceph::common::CephContext*, int, float, std::__cxx11::basic_string, std::allocator >, std::map, std::allocator >, std::__cxx11::basic_string, std::allocator >, std::less, std::allocator > >, std::allocator, std::allocator > const, std::__cxx11::basic_string, std::allocator > > > > const&, bool) /home/jenkins-build/build/workspace/ceph-pull-requests/src/crush/CrushWrapper.cc:1119:5 #10 0x559e00ada922 in ErasureCodeTest_create_rule_Test::TestBody() /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/erasure-code/TestErasureCodeLrc.cc:127:5 #11 0x559e00c0e0a6 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 ``` so, in this change, let's manage the lifecycle of the `CrushWrapper` instance with a smart pointer, so that it is destroyed and free'd properly, and this should silence the ASan warning. Signed-off-by: Kefu Chai --- diff --git a/src/test/erasure-code/TestErasureCodeLrc.cc b/src/test/erasure-code/TestErasureCodeLrc.cc index aca6ccae91f6..22caef3396dc 100644 --- a/src/test/erasure-code/TestErasureCodeLrc.cc +++ b/src/test/erasure-code/TestErasureCodeLrc.cc @@ -16,6 +16,7 @@ */ #include +#include #include #include "crush/CrushWrapper.h" @@ -91,7 +92,7 @@ TEST(ErasureCodeLrc, parse_rule) TEST(ErasureCodeTest, create_rule) { - CrushWrapper *c = new CrushWrapper; + auto c = std::make_unique(); c->create(); int root_type = 3; c->set_type_name(root_type, "root");