From 759c961187ca4f108d0f64298ba4ff8e1da908f1 Mon Sep 17 00:00:00 2001 From: Rongqi Sun Date: Thu, 18 Apr 2024 09:09:18 +0000 Subject: [PATCH] rgw/rgw_lua_utils: free std::string When sanitizer is ON, unittest_rgw_lua shows ``` ================================================================= ==3738104==ERROR: LeakSanitizer: detected memory leaks Direct leak of 31 byte(s) in 1 object(s) allocated from: #0 0xaaaac100e848 in operator new(unsigned long) (/root/ceph/build/bin/unittest_rgw_lua+0x25fe848) (BuildId: 524cddb1d44130431ac70e09896af3ab7cecef82) #1 0xffff9356dec0 in __gnu_cxx::new_allocator::allocate(unsigned long, void const*) /usr/bin/../lib/gcc/aarch64-linux-gnu/11/../../../../include/c++/11/ext/new_allocator.h:127:27 #2 0xffff9356de3c in std::allocator::allocate(unsigned long) /usr/bin/../lib/gcc/aarch64-linux-gnu/11/../../../../include/c++/11/bits/allocator.h:185:32 #3 0xffff9356de3c in std::allocator_traits >::allocate(std::allocator&, unsigned long) /usr/bin/../lib/gcc/aarch64-linux-gnu/11/../../../../include/c++/11/bits/alloc_traits.h:464:20 #4 0xffff9356db3c in std::__cxx11::basic_string, std::allocator >::_M_create(unsigned long&, unsigned long) /usr/bin/../lib/gcc/aarch64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.tcc:153:14 #5 0xffff93570bb0 in void std::__cxx11::basic_string, std::allocator >::_M_construct(char const*, char const*, std::forward_iterator_tag) /usr/bin/../lib/gcc/aarch64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.tcc:219:14 #6 0xffff935e1bbc in void std::__cxx11::basic_string, std::allocator >::_M_construct_aux(char const*, char const*, std::__false_type) /usr/bin/../lib/gcc/aarch64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.h:255:11 #7 0xffff935e197c in void std::__cxx11::basic_string, std::allocator >::_M_construct(char const*, char const*) /usr/bin/../lib/gcc/aarch64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.h:274:4 #8 0xffff935da484 in std::__cxx11::basic_string, std::allocator >::basic_string(char const*, unsigned long, std::allocator const&) /usr/bin/../lib/gcc/aarch64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.h:521:9 #9 0xffff95b3d0ac in std::__cxx11::basic_string, std::allocator > fmt::v9::to_string(fmt::v9::basic_memory_buffer > const&) /root/ceph/src/fmt/include/fmt/format.h:4050:10 #10 0xffff95b39874 in fmt::v9::vformat[abi:cxx11](fmt::v9::basic_string_view, fmt::v9::basic_format_args >) /root/ceph/src/fmt/include/fmt/format-inl.h:1473:10 #11 0xaaaac1264ab4 in std::__cxx11::basic_string, std::allocator > fmt::v9::format > const&>(fmt::v9::basic_format_string > const&>::type>, std::basic_string_view > const&) /root/ceph/src/fmt/include/fmt/core.h:3206:10 #12 0xaaaac1264ab4 in rgw::lua::get_iterator_name[abi:cxx11](std::basic_string_view >) /root/ceph/src/rgw/rgw_lua_utils.h:276:10 #13 0xaaaac1286864 in boost::container::flat_map, std::allocator >, std::__cxx11::basic_string, std::allocator >, std::less, std::allocator > >, void>::iterator* rgw::lua::create_iterator_metadata, std::allocator >, std::__cxx11::basic_string, std::allocator >, std::less, std::allocator > >, void> >(lua_State*, std::basic_string_view >, boost::container::flat_map, std::allocator >, std::__cxx11::basic_string, std::allocator >, std::less, std::allocator > >, void>::iterator const&, boost::container::flat_map, std::allocator >, std::__cxx11::basic_string, std::allocator >, std::less, std::allocator > >, void>::iterator const&) /root/ceph/src/rgw/rgw_lua_utils.h:295:38 #14 0xaaaac128603c in int rgw::lua::next, std::allocator >, std::__cxx11::basic_string, std::allocator >, std::less, std::allocator > >, void>, void>(lua_State*) /root/ceph/src/rgw/rgw_lua_utils.h:432:15 #15 0xffff917d1e94 (/lib/aarch64-linux-gnu/liblua5.3.so.0+0x11e94) (BuildId: 3debb95525f7191c93f5ba6001de5c986b4cedfb) #16 0xffff917d20ec (/lib/aarch64-linux-gnu/liblua5.3.so.0+0x120ec) (BuildId: 3debb95525f7191c93f5ba6001de5c986b4cedfb) #17 0xffff917dc32c (/lib/aarch64-linux-gnu/liblua5.3.so.0+0x1c32c) (BuildId: 3debb95525f7191c93f5ba6001de5c986b4cedfb) #18 0xffff917d23b8 (/lib/aarch64-linux-gnu/liblua5.3.so.0+0x123b8) (BuildId: 3debb95525f7191c93f5ba6001de5c986b4cedfb) #19 0xffff917ca528 (/lib/aarch64-linux-gnu/liblua5.3.so.0+0xa528) (BuildId: 3debb95525f7191c93f5ba6001de5c986b4cedfb) #20 0xffff917ccf38 (/lib/aarch64-linux-gnu/liblua5.3.so.0+0xcf38) (BuildId: 3debb95525f7191c93f5ba6001de5c986b4cedfb) #21 0xffff917d226c in lua_pcallk (/lib/aarch64-linux-gnu/liblua5.3.so.0+0x1226c) (BuildId: 3debb95525f7191c93f5ba6001de5c986b4cedfb) #22 0xaaaac1232a8c in rgw::lua::request::execute(rgw::sal::Driver*, RGWREST*, OpsLogSink*, req_state*, RGWOp*, std::__cxx11::basic_string, std::allocator > const&) /root/ceph/src/rgw/rgw_lua_request.cc:823:9 #23 0xaaaac1021934 in TestRGWLua_MetadataIterator_Test::TestBody() /root/ceph/src/test/rgw/test_rgw_lua.cc:628:8 #24 0xaaaac121a40c in void testing::internal::HandleSehExceptionsInMethodIfSupported(testing::Test*, void (testing::Test::*)(), char const*) /root/ceph/src/googletest/googletest/src/gtest.cc:2605:10 #25 0xaaaac11cee0c in void testing::internal::HandleExceptionsInMethodIfSupported(testing::Test*, void (testing::Test::*)(), char const*) /root/ceph/src/googletest/googletest/src/gtest.cc:2641:14 #26 0xaaaac1182268 in testing::Test::Run() /root/ceph/src/googletest/googletest/src/gtest.cc:2680:5 #27 0xaaaac11841ac in testing::TestInfo::Run() /root/ceph/src/googletest/googletest/src/gtest.cc:2858:11 #28 0xaaaac11857ac in testing::TestSuite::Run() /root/ceph/src/googletest/googletest/src/gtest.cc:3012:28 #29 0xaaaac11a1570 in testing::internal::UnitTestImpl::RunAllTests() /root/ceph/src/googletest/googletest/src/gtest.cc:5723:44 #30 0xaaaac1224280 in bool testing::internal::HandleSehExceptionsInMethodIfSupported(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /root/ceph/src/googletest/googletest/src/gtest.cc:2605:10 #31 0xaaaac11d593c in bool testing::internal::HandleExceptionsInMethodIfSupported(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /root/ceph/src/googletest/googletest/src/gtest.cc:2641:14 SUMMARY: AddressSanitizer: 31 byte(s) leaked in 1 allocation(s). ``` Should avoid std::string does not be freed. https://github.com/ceph/ceph/blob/08d35a8d8529783882dd092c73c0b27be41c4d86/src/rgw/rgw_lua_utils.h#L364, this way should be OK. Reported issue: https://github.com/llvm/llvm-project/issues/60709 Fix: https://github.com/llvm/llvm-project/commit/c6b12b7c3bc81266e8becbcccf0eaf4622db0cd0 (clang >= 17, but CI use clang 14) Signed-off-by: Rongqi Sun --- src/rgw/rgw_lua_utils.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/rgw/rgw_lua_utils.h b/src/rgw/rgw_lua_utils.h index 79ebb09fac07a..67b1659558847 100644 --- a/src/rgw/rgw_lua_utils.h +++ b/src/rgw/rgw_lua_utils.h @@ -292,10 +292,9 @@ template typename MapType::iterator* create_iterator_metadata(lua_State* L, const std::string_view name, const typename MapType::iterator& start_it, const typename MapType::iterator& end_it) { using Iterator = typename MapType::iterator; - const std::string qualified_name = get_iterator_name(name); // create metatable for userdata // metatable is created before the userdata to save on allocation if the metatable already exists - const auto metatable_is_new = luaL_newmetatable(L, qualified_name.c_str()); + const auto metatable_is_new = luaL_newmetatable(L, get_iterator_name(name).c_str()); const auto metatable_pos = lua_gettop(L); int userdata_pos; Iterator* new_it = nullptr;