Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Refactor hll_sketch and fix stack use after return bug #50600

Merged
merged 3 commits into from
Sep 3, 2024

Conversation

LiShuMing
Copy link
Contributor

@LiShuMing LiShuMing commented Sep 3, 2024

Why I'm doing:

Possible stack-use-after-return :

=1696562==ERROR: AddressSanitizer: stack-use-after-return on address 0x7fd2d9e00a70 at pc 0x00001a4dcfcf bp 0x7fd2d9a1cf10 sp 0x7fd2d9a1cf00
READ of size 8 at 0x7fd2d9e00a70 thread T136
    #0 0x1a4dcfce in starrocks::STLCountingAllocator<datasketches::CouponList<starrocks::STLCountingAllocator<unsigned char> > >::allocate(unsigned long) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x1a4dcfce)
    #1 0x1a6d6f7c in datasketches::CouponList<starrocks::STLCountingAllocator<unsigned char> >::copyAs(datasketches::target_hll_type) const (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x1a6d6f7c)
    #2 0x206f93dc in datasketches::hll_sketch_alloc<starrocks::STLCountingAllocator<unsigned char> >::hll_sketch_alloc(datasketches::hll_sketch_alloc<starrocks::STLCountingAllocator<unsigned char> > const&, datasketches::target_hll_type) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x206f93dc)
    #3 0x206f5a4e in datasketches::hll_union_alloc<starrocks::STLCountingAllocator<unsigned char> >::get_result(datasketches::target_hll_type) const (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x206f5a4e)
    #4 0x206f54d0 in starrocks::DataSketchesHll::get_hll_sketch() const (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x206f54d0)
    #5 0x206f1ebd in starrocks::DataSketchesHll::serialize_size() const (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x206f1ebd)
    #6 0x1a716416 in starrocks::HllSketchAggregateFunction<(starrocks::LogicalType)24, unsigned char>::serialize_to_column(starrocks::FunctionContext*, unsigned char const*, starrocks::Column*) const (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x1a716416)
    #7 0x1a6ccd36 in starrocks::NullableAggregateFunctionBase<std::shared_ptr<starrocks::AggregateFunction>, starrocks::NullableAggregateFunctionState<starrocks::HLLSketchState, false>, false, true, starrocks::AggNonNullPred<starrocks::HLLSketchState> >::serialize_to_column(starrocks::FunctionContext*, unsigned char const*, starrocks::Column*) const (/h
ome/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x1a6ccd36)
    #8 0x18090c20 in starrocks::AggStateUnion::finalize_to_column(starrocks::FunctionContext*, unsigned char const*, starrocks::Column*) const (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x18090c20)
    #9 0x18093fcd in starrocks::AggFuncBasedValueAggregator::append_data(starrocks::Column*) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x18093fcd)
    #10 0x1809461b in starrocks::AggFuncBasedValueAggregator::finalize() (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x1809461b)
    #11 0x18081d56 in starrocks::ChunkAggregator::aggregate_result() (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x18081d56)
    #12 0x1b86fa43 in starrocks::MemTable::finalize() (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x1b86fa43)
    #13 0x1b8e1759 in starrocks::DeltaWriter::flush_memtable_async(bool) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x1b8e1759)
    #14 0x1b8df4bf in starrocks::DeltaWriter::close() (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x1b8df4bf)
    #15 0x20274544 in starrocks::AsyncDeltaWriter::_execute(void*, bthread::TaskIterator<starrocks::AsyncDeltaWriter::Task>&) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x20274544)
    #16 0x2027ddc5 in bthread::ExecutionQueue<starrocks::AsyncDeltaWriter::Task>::execute_task(void*, void*, bthread::TaskIteratorBase&) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x2027ddc5)
    #17 0x258c2dde in bthread::ExecutionQueueBase::_execute(bthread::TaskNode*, bool, int*) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x258c2dde)
    #18 0x258c3f0a in bthread::ExecutionQueueBase::_execute_tasks(void*) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x258c3f0a)
    #19 0x207a79e9 in starrocks::bthreads::ThreadPoolExecutor::submit(void* (*)(void*), void*)::{lambda()#1}::operator()() const (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x207a79e9)
    #20 0x207a8acb in void std::__invoke_impl<void, starrocks::bthreads::ThreadPoolExecutor::submit(void* (*)(void*), void*)::{lambda()#1}&>(std::__invoke_other, starrocks::bthreads::ThreadPoolExecutor::submit(void* (*)(void*), void*)::{lambda()#1}&) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x207a8acb)
    #21 0x207a895d in std::enable_if<is_invocable_r_v<void, starrocks::bthreads::ThreadPoolExecutor::submit(void* (*)(void*), void*)::{lambda()#1}&>, void>::type std::__invoke_r<void, starrocks::bthreads::ThreadPoolExecutor::submit(void* (*)(void*), void*)::{lambda()#1}&>(starrocks::bthreads::ThreadPoolExecutor::submit(void* (*)(void*), void*)::{lambda(
)#1}&) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x207a895d)
    #22 0x207a87be in std::_Function_handler<void (), starrocks::bthreads::ThreadPoolExecutor::submit(void* (*)(void*), void*)::{lambda()#1}>::_M_invoke(std::_Any_data const&) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x207a87be)
    #23 0x13a7d2c9 in std::function<void ()>::operator()() const (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x13a7d2c9)
    #24 0x20a7edab in starrocks::FunctionRunnable::run() (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x20a7edab)
    #25 0x20a7accb in starrocks::ThreadPool::dispatch_thread() (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x20a7accb)
    #26 0x20a9a36d in void std::__invoke_impl<void, void (starrocks::ThreadPool::*&)(), starrocks::ThreadPool*&>(std::__invoke_memfun_deref, void (starrocks::ThreadPool::*&)(), starrocks::ThreadPool*&) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x20a9a36d)
    #27 0x20a99e0e in std::__invoke_result<void (starrocks::ThreadPool::*&)(), starrocks::ThreadPool*&>::type std::__invoke<void (starrocks::ThreadPool::*&)(), starrocks::ThreadPool*&>(void (starrocks::ThreadPool::*&)(), starrocks::ThreadPool*&) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x20a99e0e)
    #28 0x20a98ccb in void std::_Bind<void (starrocks::ThreadPool::*(starrocks::ThreadPool*))()>::__call<void, , 0ul>(std::tuple<>&&, std::_Index_tuple<0ul>) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x20a98ccb)
    #29 0x20a97a1a in void std::_Bind<void (starrocks::ThreadPool::*(starrocks::ThreadPool*))()>::operator()<, void>() (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x20a97a1a)
    #30 0x20a951eb in void std::__invoke_impl<void, std::_Bind<void (starrocks::ThreadPool::*(starrocks::ThreadPool*))()>&>(std::__invoke_other, std::_Bind<void (starrocks::ThreadPool::*(starrocks::ThreadPool*))()>&) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x20a951eb)
    #31 0x20a91381 in std::enable_if<is_invocable_r_v<void, std::_Bind<void (starrocks::ThreadPool::*(starrocks::ThreadPool*))()>&>, void>::type std::__invoke_r<void, std::_Bind<void (starrocks::ThreadPool::*(starrocks::ThreadPool*))()>&>(std::_Bind<void (starrocks::ThreadPool::*(starrocks::ThreadPool*))()>&) (/home/disk1/lishuming/work/dev2/output/be/l
ib/starrocks_be+0x20a91381)
    #32 0x20a8d226 in std::_Function_handler<void (), std::_Bind<void (starrocks::ThreadPool::*(starrocks::ThreadPool*))()> >::_M_invoke(std::_Any_data const&) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x20a8d226)
    #33 0x13a7d2c9 in std::function<void ()>::operator()() const (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x13a7d2c9)
    #34 0x20a5e17d in starrocks::Thread::supervise_thread(void*) (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x20a5e17d)
    #35 0x7fd34a70dac2 in start_thread nptl/pthread_create.c:442
    #36 0x7fd34a79f84f  (/lib/x86_64-linux-gnu/libc.so.6+0x12684f)

Address 0x7fd2d9e00a70 is located in stack of thread T136 at offset 112 in frame
    #0 0x1a715a21 in starrocks::HllSketchAggregateFunction<(starrocks::LogicalType)24, unsigned char>::merge(starrocks::FunctionContext*, starrocks::Column const*, unsigned char*, unsigned long) const (/home/disk1/lishuming/work/dev2/output/be/lib/starrocks_be+0x1a715a21)

What I'm doing:

  • Split hll_sketch from hll.h since it will cost too much time once hll.h has changed.
  • Rename hll_skecth.h aggregate function to ds_hll_count_distinct.h
  • Fix stack after return bug by pinning memory_usage pointer.

Fixes #49000

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

@LiShuMing LiShuMing requested review from a team as code owners September 3, 2024 02:11
Signed-off-by: shuming.li <[email protected]>
@LiShuMing LiShuMing changed the title [Enhancement] Refactor hll_sktch and fix stack use after free bug [Enhancement] Refactor hll_sktch and fix stack use after return bug Sep 3, 2024
Signed-off-by: shuming.li <[email protected]>
Copy link

github-actions bot commented Sep 3, 2024

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

github-actions bot commented Sep 3, 2024

[FE Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

github-actions bot commented Sep 3, 2024

[BE Incremental Coverage Report]

fail : 160 / 250 (64.00%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 be/src/exprs/agg/avg.h 1 2 50.00% [218]
🔵 be/src/exprs/agg/ds_hll_count_distinct.h 66 127 51.97% [43, 44, 45, 46, 48, 74, 78, 79, 81, 82, 83, 84, 86, 87, 91, 92, 93, 94, 96, 97, 101, 116, 118, 119, 120, 121, 122, 124, 125, 127, 143, 145, 146, 148, 149, 150, 152, 153, 157, 158, 159, 160, 161, 162, 163, 164, 165, 167, 168, 170, 171, 173, 174, 177, 178, 179, 181, 182, 184, 192, 198]
🔵 be/src/types/hll_sketch.cpp 40 58 68.97% [28, 33, 43, 54, 61, 62, 65, 66, 67, 102, 103, 104, 116, 117, 118, 120, 121, 122]
🔵 be/src/types/hll_sketch.h 18 24 75.00% [76, 113, 114, 115, 116, 118]
🔵 be/src/exec/aggregator.cpp 28 32 87.50% [495, 506, 508, 509]
🔵 be/src/storage/column_aggregate_func.cpp 1 1 100.00% []
🔵 be/src/exprs/agg/aggregate_state_allocator.h 3 3 100.00% []
🔵 be/src/storage/chunk_aggregator.cpp 3 3 100.00% []

@kangkaisen kangkaisen merged commit 8c4dc97 into StarRocks:main Sep 3, 2024
48 of 49 checks passed
va-os-commits pushed a commit to va-os-commits/starrocks that referenced this pull request Sep 10, 2024
@chaoyli chaoyli changed the title [Enhancement] Refactor hll_sktch and fix stack use after return bug [Enhancement] Refactor hll_sketch and fix stack use after return bug Sep 26, 2024
renzhimin7 pushed a commit to renzhimin7/starrocks that referenced this pull request Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Generic Aggregate Function State
5 participants