From 9b3bcd0870f2d62e8636a662dc7e9eaa20b7a958 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 9 May 2023 21:32:13 +0200 Subject: [PATCH] Fix stack-use-after-scope in gtest_resource_manager_static Reordering of the static variables should be enough to fix this: [ RUN ] IOResourceStaticResourceManager.Prioritization ================================================================= ==13==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7f7d8d621970 at pc 0x5636b80dcbb2 bp 0x7f7c48e47dd0 sp 0x7f7c48e47dc8 READ of size 8 at 0x7f7d8d621970 thread T3975 (ThreadPool) #0 0x5636b80dcbb1 in IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_1::operator()(long) const build_docker/./src/IO/Resource/tests/gtest_resource_manager_static.cpp:78:13 #1 0x5636b80dcbb1 in IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_0::operator()() const build_docker/./src/IO/Resource/tests/gtest_resource_manager_static.cpp:95:21 #2 0x5636b80dcbb1 in decltype(std::declval()()) std::__1::__invoke[abi:v15000](IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_0&) build_docker/./contrib/llvm-project/libcxx/include/__functional/invoke.h:394:23 #3 0x5636b80dcbb1 in decltype(auto) std::__1::__apply_tuple_impl[abi:v15000]&>(IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_0&, std::__1::tuple<>&, std::__1::__tuple_indices<>) build_docker/./contrib/llvm-project/libcxx/include/tuple:1789:1 #4 0x5636b80dcbb1 in decltype(auto) std::__1::apply[abi:v15000]&>(IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_0&, std::__1::tuple<>&) build_docker/./contrib/llvm-project/libcxx/include/tuple:1798:1 #5 0x5636b80dcbb1 in ThreadFromGlobalPoolImpl::ThreadFromGlobalPoolImpl(IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_0&&)::'lambda'()::operator()() build_docker/./src/Common/ThreadPool.h:228:13 #6 0x5636b80dcbb1 in decltype(std::declval()()) std::__1::__invoke[abi:v15000]::ThreadFromGlobalPoolImpl(IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_0&&)::'lambda'()&>(IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_0&&) build_docker/./contrib/llvm-project/libcxx/include/__functional/invoke.h:394:23 #7 0x5636b80dcbb1 in void std::__1::__invoke_void_return_wrapper::__call::ThreadFromGlobalPoolImpl(IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_0&&)::'lambda'()&>(ThreadFromGlobalPoolImpl::ThreadFromGlobalPoolImpl(IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_0&&)::'lambda'()&) build_docker/./contrib/llvm-project/libcxx/include/__functional/invoke.h:479:9 #8 0x5636b80dcbb1 in std::__1::__function::__default_alloc_func::ThreadFromGlobalPoolImpl(IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_0&&)::'lambda'(), void ()>::operator()[abi:v15000]() build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:235:12 #9 0x5636b80dcbb1 in void std::__1::__function::__policy_invoker::__call_impl::ThreadFromGlobalPoolImpl(IOResourceStaticResourceManager_Prioritization_Test::TestBody()::$_0&&)::'lambda'(), void ()>>(std::__1::__function::__policy_storage const*) build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:716:16 #10 0x5636ea219310 in std::__1::__function::__policy_func::operator()[abi:v15000]() const build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:848:16 #11 0x5636ea219310 in std::__1::function::operator()() const build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:1187:12 #12 0x5636ea219310 in ThreadPoolImpl::worker(std::__1::__list_iterator) build_docker/./src/Common/ThreadPool.cpp:416:13 #13 0x5636ea2258ac in void ThreadPoolImpl::scheduleImpl(std::__1::function, long, std::__1::optional, bool)::'lambda0'()::operator()() const build_docker/./src/Common/ThreadPool.cpp:180:73 #14 0x5636ea2258ac in decltype(std::declval()()) std::__1::__invoke[abi:v15000]::scheduleImpl(std::__1::function, long, std::__1::optional, bool)::'lambda0'()>(void&&) build_docker/./contrib/llvm-project/libcxx/include/__functional/invoke.h:394:23 #15 0x5636ea2258ac in void std::__1::__thread_execute[abi:v15000]>, void ThreadPoolImpl::scheduleImpl(std::__1::function, long, std::__1::optional, bool)::'lambda0'()>(std::__1::tuple::scheduleImpl(std::__1::function, long, std::__1::optional, bool)::'lambda0'()>&, std::__1::__tuple_indices<>) build_docker/./contrib/llvm-project/libcxx/include/thread:284:5 #16 0x5636ea2258ac in void* std::__1::__thread_proxy[abi:v15000]>, void ThreadPoolImpl::scheduleImpl(std::__1::function, long, std::__1::optional, bool)::'lambda0'()>>(void*) build_docker/./contrib/llvm-project/libcxx/include/thread:295:5 #17 0x7f7d8fcf1608 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x8608) (BuildId: 7b4536f41cdaa5888408e82d0836e33dcf436466) #18 0x7f7d8fc16132 in __clone (/lib/x86_64-linux-gnu/libc.so.6+0x11f132) (BuildId: 1878e6b475720c7c51969e69ab2d276fae6d1dee) Address 0x7f7d8d621970 is located in stack of thread T0 at offset 368 in frame #0 0x5636b80d9bef in IOResourceStaticResourceManager_Prioritization_Test::TestBody() build_docker/./src/IO/Resource/tests/gtest_resource_manager_static.cpp:46 This frame has 10 object(s): [32, 36) 'requests_per_thead' (line 48) [48, 208) 't' (line 49) [272, 296) 'ref.tmp' (line 51) [336, 352) 'last_priority' (line 74) [368, 376) 'check' (line 75) <== Memory access at offset 368 is inside this variable [400, 424) 'name' (line 83) [464, 512) 'ref.tmp13' (line 87) [544, 560) 'c' (line 101) [576, 600) 'ref.tmp32' (line 101) [640, 664) 'ref.tmp41' (line 102) Signed-off-by: Azat Khuzhin --- .../tests/gtest_resource_manager_static.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/IO/Resource/tests/gtest_resource_manager_static.cpp b/src/IO/Resource/tests/gtest_resource_manager_static.cpp index cd426b1a6bc6..35bb78bbbe34 100644 --- a/src/IO/Resource/tests/gtest_resource_manager_static.cpp +++ b/src/IO/Resource/tests/gtest_resource_manager_static.cpp @@ -44,6 +44,15 @@ TEST(IOResourceStaticResourceManager, Smoke) TEST(IOResourceStaticResourceManager, Prioritization) { + std::optional last_priority; + auto check = [&] (Int64 priority) + { + // Lock is not required here because this is called during request execution and we have max_requests = 1 + if (last_priority) + EXPECT_TRUE(priority <= *last_priority); // Should be true if every queue arrived at the same time at busy period start + last_priority = priority; + }; + constexpr size_t threads_per_queue = 2; int requests_per_thead = 100; ResourceTest t(4 * threads_per_queue + 1); @@ -71,15 +80,6 @@ TEST(IOResourceStaticResourceManager, Prioritization) )CONFIG"); - std::optional last_priority; - auto check = [&] (Int64 priority) - { - // Lock is not required here because this is called during request execution and we have max_requests = 1 - if (last_priority) - EXPECT_TRUE(priority <= *last_priority); // Should be true if every queue arrived at the same time at busy period start - last_priority = priority; - }; - for (String name : {"A", "B", "C", "D"}) { for (int thr = 0; thr < threads_per_queue; thr++)