oneapi-src / oneTBB

oneAPI Threading Building Blocks (oneTBB)
https://oneapi-src.github.io/oneTBB/
Apache License 2.0
5.6k stars 1.01k forks source link

Possible memory overflows in libtbb (stringop-overflow gcc warnings) #1466

Closed phprus closed 1 month ago

phprus commented 1 month ago

Version

Commit: 306c75f5e906fc221e12f45dae45900f0d8db6ba

Environment

OS: Debian 12 gcc version 12.2.0 (Debian 12.2.0-14)

Steps To Reproduce

CMake command: cmake -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_CXX_STANDARD=20 -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON ../..

Build warnings:

[ 10%] Linking CXX shared library ../../gnu_12.2_cxx20_64_relwithdebinfo/libtbb.so
cd /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd/src/tbb && /usr/bin/cmake -E cmake_link_script CMakeFiles/tbb.dir/link.txt --verbose=1
/usr/bin/c++ -fPIC -O2 -g -DNDEBUG -flto=auto -fno-fat-lto-objects -Wl,--version-script="/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/src/tbb/def/lin64-tbb.def" -Wl,-z,relro,-z,now,-z,noexecstack -shared -Wl,-soname,libtbb.so.12 -o ../../gnu_12.2_cxx20_64_relwithdebinfo/libtbb.so.12.13 CMakeFiles/tbb.dir/address_waiter.cpp.o CMakeFiles/tbb.dir/allocator.cpp.o CMakeFiles/tbb.dir/arena.cpp.o CMakeFiles/tbb.dir/arena_slot.cpp.o CMakeFiles/tbb.dir/concurrent_bounded_queue.cpp.o CMakeFiles/tbb.dir/dynamic_link.cpp.o CMakeFiles/tbb.dir/exception.cpp.o CMakeFiles/tbb.dir/governor.cpp.o CMakeFiles/tbb.dir/global_control.cpp.o CMakeFiles/tbb.dir/itt_notify.cpp.o CMakeFiles/tbb.dir/main.cpp.o CMakeFiles/tbb.dir/market.cpp.o CMakeFiles/tbb.dir/tcm_adaptor.cpp.o CMakeFiles/tbb.dir/misc.cpp.o CMakeFiles/tbb.dir/misc_ex.cpp.o CMakeFiles/tbb.dir/observer_proxy.cpp.o CMakeFiles/tbb.dir/parallel_pipeline.cpp.o CMakeFiles/tbb.dir/private_server.cpp.o CMakeFiles/tbb.dir/profiling.cpp.o CMakeFiles/tbb.dir/rml_tbb.cpp.o CMakeFiles/tbb.dir/rtm_mutex.cpp.o CMakeFiles/tbb.dir/rtm_rw_mutex.cpp.o CMakeFiles/tbb.dir/semaphore.cpp.o CMakeFiles/tbb.dir/small_object_pool.cpp.o CMakeFiles/tbb.dir/task.cpp.o CMakeFiles/tbb.dir/task_dispatcher.cpp.o CMakeFiles/tbb.dir/task_group_context.cpp.o CMakeFiles/tbb.dir/thread_dispatcher.cpp.o CMakeFiles/tbb.dir/thread_request_serializer.cpp.o CMakeFiles/tbb.dir/threading_control.cpp.o CMakeFiles/tbb.dir/version.cpp.o CMakeFiles/tbb.dir/queuing_rw_mutex.cpp.o  -ldl
In member function ‘store’,
    inlined from ‘store’ at /usr/include/c++/12/atomic:104:20,
    inlined from ‘notify_one_relaxed’ at /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/src/tbb/concurrent_monitor.h:300:53,
    inlined from ‘notify_one’ at /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/src/tbb/concurrent_monitor.h:276:27,
    inlined from ‘__dt_base ’ at /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/src/tbb/arena.cpp:701:53:
/usr/include/c++/12/bits/atomic_base.h:464:25: warning: ‘__atomic_store_1’ writing 1 byte into a region of size 0 overflows the destination [-Wstringop-overflow=]
  464 |         __atomic_store_n(&_M_i, __i, int(__m));
      |                         ^
In member function ‘store’,
    inlined from ‘store’ at /usr/include/c++/12/atomic:104:20,
    inlined from ‘notify_one_relaxed’ at /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/src/tbb/concurrent_monitor.h:300:53,
    inlined from ‘notify_one’ at /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/src/tbb/concurrent_monitor.h:276:27,
    inlined from ‘execute’ at /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/src/tbb/arena.cpp:803:47:
/usr/include/c++/12/bits/atomic_base.h:464:25: warning: ‘__atomic_store_1’ writing 1 byte into a region of size 0 overflows the destination [-Wstringop-overflow=]
  464 |         __atomic_store_n(&_M_i, __i, int(__m));
      |                         ^
pavelkumbrasev commented 1 month ago

Is there some problem with macro? https://github.com/oneapi-src/oneTBB/blob/306c75f5e906fc221e12f45dae45900f0d8db6ba/src/tbb/concurrent_monitor.h#L294

phprus commented 1 month ago

@pavelkumbrasev

Yes. If LTO is enabled (it is by default), then pragma GCC diagnostic ignored cannot suppress the warning. We need to add -Wno-stringop-overflow when linking.

I can create a PR if this warning suppression approach is possible in oneTBB.

pavelkumbrasev commented 1 month ago

We need to add -Wno-stringop-overflow when linking.

Will the link flag help during the compilation stage or did I miss something?

phprus commented 1 month ago

Will the link flag help during the compilation stage or did I miss something?

If LTO is enabled, the -Wno-stringop-overflow flag must be added to linker, since when LTO is enabled, GCC performs analysis at linking.

For example into TBB_COMMON_LINK_FLAGS or TBB_LIB_LINK_FLAGS.

Linking without warnings:

[ 10%] Linking CXX shared library ../../gnu_12.2_cxx20_64_relwithdebinfo/libtbb.so
cd /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/src/tbb && /usr/bin/cmake -E cmake_link_script CMakeFiles/tbb.dir/link.txt --verbose=1
/usr/bin/c++ -fPIC -O2 -g -DNDEBUG -flto=auto -fno-fat-lto-objects -Wl,--version-script="/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/src/tbb/def/lin64-tbb.def" -Wl,-z,relro,-z,now,-z,noexecstack -shared -Wl,-soname,libtbb.so.12 -o ../../gnu_12.2_cxx20_64_relwithdebinfo/libtbb.so.12.13 CMakeFiles/tbb.dir/address_waiter.cpp.o CMakeFiles/tbb.dir/allocator.cpp.o CMakeFiles/tbb.dir/arena.cpp.o CMakeFiles/tbb.dir/arena_slot.cpp.o CMakeFiles/tbb.dir/concurrent_bounded_queue.cpp.o CMakeFiles/tbb.dir/dynamic_link.cpp.o CMakeFiles/tbb.dir/exception.cpp.o CMakeFiles/tbb.dir/governor.cpp.o CMakeFiles/tbb.dir/global_control.cpp.o CMakeFiles/tbb.dir/itt_notify.cpp.o CMakeFiles/tbb.dir/main.cpp.o CMakeFiles/tbb.dir/market.cpp.o CMakeFiles/tbb.dir/tcm_adaptor.cpp.o CMakeFiles/tbb.dir/misc.cpp.o CMakeFiles/tbb.dir/misc_ex.cpp.o CMakeFiles/tbb.dir/observer_proxy.cpp.o CMakeFiles/tbb.dir/parallel_pipeline.cpp.o CMakeFiles/tbb.dir/private_server.cpp.o CMakeFiles/tbb.dir/profiling.cpp.o CMakeFiles/tbb.dir/rml_tbb.cpp.o CMakeFiles/tbb.dir/rtm_mutex.cpp.o CMakeFiles/tbb.dir/rtm_rw_mutex.cpp.o CMakeFiles/tbb.dir/semaphore.cpp.o CMakeFiles/tbb.dir/small_object_pool.cpp.o CMakeFiles/tbb.dir/task.cpp.o CMakeFiles/tbb.dir/task_dispatcher.cpp.o CMakeFiles/tbb.dir/task_group_context.cpp.o CMakeFiles/tbb.dir/thread_dispatcher.cpp.o CMakeFiles/tbb.dir/thread_request_serializer.cpp.o CMakeFiles/tbb.dir/threading_control.cpp.o CMakeFiles/tbb.dir/version.cpp.o CMakeFiles/tbb.dir/queuing_rw_mutex.cpp.o  -ldl -Wno-stringop-overflow
cd /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/src/tbb && /usr/bin/cmake -E cmake_symlink_library ../../gnu_12.2_cxx20_64_relwithdebinfo/libtbb.so.12.13 ../../gnu_12.2_cxx20_64_relwithdebinfo/libtbb.so.12 ../../gnu_12.2_cxx20_64_relwithdebinfo/libtbb.so
cd /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/src/tbb && /usr/bin/cmake -DBINARY_DIR=/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto -DSOURCE_DIR=/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba -DBIN_PATH=/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/gnu_12.2_cxx20_64_relwithdebinfo -DVARS_TEMPLATE=linux/env/vars.sh.in -DVARS_NAME=vars.sh -DTBB_INSTALL_VARS=OFF -DTBB_CMAKE_INSTALL_LIBDIR=lib -P /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/integration/cmake/generate_vars.cmake
gmake[2]: Leaving directory '/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto'
[ 10%] Built target tbb
/usr/bin/gmake  -f src/tbbmalloc/CMakeFiles/tbbmalloc.dir/build.make src/tbbmalloc/CMakeFiles/tbbmalloc.dir/depend
gmake[2]: Entering directory '/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto'
cd /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/src/tbbmalloc /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/src/tbbmalloc /home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/src/tbbmalloc/CMakeFiles/tbbmalloc.dir/DependInfo.cmake --color=
Dependencies file "src/tbbmalloc/CMakeFiles/tbbmalloc.dir/__/tbb/itt_notify.cpp.o.d" is newer than depends file "/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/src/tbbmalloc/CMakeFiles/tbbmalloc.dir/compiler_depend.internal".
Dependencies file "src/tbbmalloc/CMakeFiles/tbbmalloc.dir/backend.cpp.o.d" is newer than depends file "/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/src/tbbmalloc/CMakeFiles/tbbmalloc.dir/compiler_depend.internal".
Dependencies file "src/tbbmalloc/CMakeFiles/tbbmalloc.dir/backref.cpp.o.d" is newer than depends file "/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/src/tbbmalloc/CMakeFiles/tbbmalloc.dir/compiler_depend.internal".
Dependencies file "src/tbbmalloc/CMakeFiles/tbbmalloc.dir/frontend.cpp.o.d" is newer than depends file "/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/src/tbbmalloc/CMakeFiles/tbbmalloc.dir/compiler_depend.internal".
Dependencies file "src/tbbmalloc/CMakeFiles/tbbmalloc.dir/large_objects.cpp.o.d" is newer than depends file "/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/src/tbbmalloc/CMakeFiles/tbbmalloc.dir/compiler_depend.internal".
Dependencies file "src/tbbmalloc/CMakeFiles/tbbmalloc.dir/tbbmalloc.cpp.o.d" is newer than depends file "/home/phprus/test/tbb/oneTBB-306c75f5e906fc221e12f45dae45900f0d8db6ba/build/cpp20rwd-lto/src/tbbmalloc/CMakeFiles/tbbmalloc.dir/compiler_depend.internal".
phprus commented 1 month ago

Possible solution: PR #1470