Skip to content

cmake: enable building with Sanitizers - #56241

Merged
tchaikov merged 8 commits into
ceph:mainfrom
tchaikov:wip-cmake-enable-sanitizers
Mar 27, 2024
Merged

tchaikov merged 8 commits into
ceph:mainfrom
tchaikov:wip-cmake-enable-sanitizers

Conversation

@tchaikov

@tchaikov tchaikov commented Mar 17, 2024 •

Copy link
Copy Markdown
Contributor

when performing tests, we should enable sanitizers for detecting potential issues. so, in this change, we address various issues in CMake when enabling ASsan, TSan and UBSan.

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands
  • jenkins retest this please
  • jenkins test classic perf
  • jenkins test crimson perf
  • jenkins test signed
  • jenkins test make check
  • jenkins test make check arm64
  • jenkins test submodules
  • jenkins test dashboard
  • jenkins test dashboard cephadm
  • jenkins test api
  • jenkins test docs
  • jenkins render docs
  • jenkins test ceph-volume all
  • jenkins test ceph-volume tox
  • jenkins test windows
  • jenkins test rook e2e

@tchaikov tchaikov mentioned this pull request Mar 17, 2024
4 of 14 tasks
@tchaikov
tchaikov force-pushed the wip-cmake-enable-sanitizers branch from 0d47a1b to 416bae3 Compare March 17, 2024 10:54
@huww98

huww98 commented Mar 17, 2024

Copy link
Copy Markdown
Contributor

We also need UBSAN_OPTIONS=halt_on_error=1 env to fail the tests on errors of UBSan. Or we can set log_path and then check the logs after the test.

@tchaikov
tchaikov force-pushed the wip-cmake-enable-sanitizers branch from 8b97964 to b23c0f2 Compare March 17, 2024 11:26
@tchaikov

Copy link
Copy Markdown
Contributor Author

We also need UBSAN_OPTIONS=halt_on_error=1 env to fail the tests on errors of UBSan. Or we can set log_path and then check the logs after the test.

@huww98 thanks! IMHO, halt_on_error=1 is better. added.

@tchaikov
tchaikov force-pushed the wip-cmake-enable-sanitizers branch 4 times, most recently from 79fd0fd to 04b5d91 Compare March 17, 2024 12:09
@tchaikov

tchaikov commented Mar 17, 2024 •

Copy link
Copy Markdown
Contributor Author

bummer, we cannot enable neorados.cc with UBSan enabled because of llvm/llvm-project#49689. if we could use Clang-15, we would be able to enable UBSan as well.

@tchaikov tchaikov changed the title script/run-make: enable Sanitizers script/run-make: enable ASan Mar 17, 2024
@tchaikov
tchaikov marked this pull request as draft March 17, 2024 13:29
@tchaikov
tchaikov marked this pull request as ready for review March 17, 2024 13:42
@tchaikov

Copy link
Copy Markdown
Contributor Author
CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find rt (missing: rt_LIBRARIES)
Call Stack (most recent call first):

Comment thread src/CMakeLists.txt Resolved
@tchaikov
tchaikov force-pushed the wip-cmake-enable-sanitizers branch from 6e282f4 to 2cf79b8 Compare March 17, 2024 15:06
@tchaikov tchaikov added the DNM label Mar 17, 2024
@tchaikov

Copy link
Copy Markdown
Contributor Author

v2:

  • add a space before ${CMAKE_EXE_EXPORTS_C_FLAG} when appending it to CMAKE_EXE_LINKER_FLAGS.

@tchaikov
tchaikov force-pushed the wip-cmake-enable-sanitizers branch from 81fe6cb to 5ec3efe Compare March 17, 2024 16:37
@tchaikov

Copy link
Copy Markdown
Contributor Author
CFLAGS="" CPPFLAGS="-iquote/home/jenkins-build/build/workspace/ceph-pull-requests/src/include -w -D'void0=dead_function(void)' -D'__Pyx_check_single_interpreter(ARG)=ARG##0'" CXX="/usr/bin/clang++-14" LDSHARED="/usr/bin/clang-14 -shared" OPT="-DNDEBUG -g -fwrapv -O2 -w" LDFLAGS="\ -fsanitize=address\ -fno-omit-frame-pointer -L/home/jenkins-build/build/workspace/ceph-pull-requests/build/lib" CYTHON_BUILD_DIR=/home/jenkins-build/build/workspace/ceph-pull-requests/build/src/pybind/rados CEPH_LIBDIR=/home/jenkins-build/build/workspace/ceph-pull-requests/build/lib /usr/bin/python3.10 /home/jenkins-build/build/workspace/ceph-pull-requests/src/pybind/rados/setup.py build --build-base /home/jenkins-build/build/workspace/ceph-pull-requests/build/lib/cython_modules --build-platlib /home/jenkins-build/build/workspace/ceph-pull-requests/build/lib/cython_modules/lib.3
/usr/bin/ld: /home/jenkins-build/build/workspace/ceph-pull-requests/build/lib/libceph-common.so.2: undefined reference to `__asan_stack_free_10'

it seems when cythonizing rados python binding, the linker fails to pull in libasan.

@tchaikov tchaikov mentioned this pull request Mar 24, 2024
4 of 14 tasks
@tchaikov
tchaikov force-pushed the wip-cmake-enable-sanitizers branch from 5ec3efe to 72dbd27 Compare March 24, 2024 12:32
@tchaikov
tchaikov requested a review from a team as a code owner March 24, 2024 12:32
@github-actions github-actions Bot added cephfs Ceph File System pybind rbd labels Mar 24, 2024
@tchaikov
tchaikov force-pushed the wip-cmake-enable-sanitizers branch 3 times, most recently from 26041a6 to f2dffdc Compare March 24, 2024 13:16
@tchaikov
tchaikov force-pushed the wip-cmake-enable-sanitizers branch from e132210 to 974c8d2 Compare March 26, 2024 02:11
@tchaikov

Copy link
Copy Markdown
Contributor Author

some of the leakages should be addressed by #56461

@tchaikov

tchaikov commented Mar 26, 2024 •

Copy link
Copy Markdown
Contributor Author

@cbodley hi Casey, thank you for your reviews of the fixes of the leaks identified by LeakSanitizer. i am wondering if it is acceptable to get part of the changeset merged before we address all of the leaks? for two reasons:

  1. i don't have to rebase the changeset because of the change in main HEAD
  2. developers interested in running tests with ASan, TSan and UBSan can enable them with the help of the changes in this changeset.

if you are for this proposal, i will extract the last commit which enables ASan by default into another PR, and try to get the rest of the changes into main. otherwise, i will continue the efforts to get a clean run. it might take a while though.

what do you think?

@cbodley

cbodley commented Mar 26, 2024

Copy link
Copy Markdown
Contributor

what do you think?

works for me

@tchaikov
tchaikov force-pushed the wip-cmake-enable-sanitizers branch from 974c8d2 to 241891a Compare March 26, 2024 13:01
@tchaikov

Copy link
Copy Markdown
Contributor Author

v6:

  • drop the last commit which adds cmake_opts+=" -DWITH_ASAN=ON" to src/script/run-make.sh.

@tchaikov tchaikov removed the DNM label Mar 26, 2024
@tchaikov tchaikov changed the title script/run-make: enable ASan cmake: enable building with Sanitizers Mar 26, 2024
otherwised we'd have

```
CMake Error at cmake/modules/FindSanitizers.cmake:17 (if):
  if given arguments:

    "address" "IN_LIST" "address" "thread" "undefined_behavior" "OR" "leak" "IN_LIST" "address" "thread" "undefined_behavior"

  Unknown arguments specified
```

when enabling TSan with WITH_TSAN=ON.

Signed-off-by: Kefu Chai <[email protected]>
so we can be alerted if UBSan identify something wrong.

Signed-off-by: Kefu Chai <[email protected]>
instead of overriding CMAKE_EXE_LINKER_FLAGS, let's append to it.
so that the existing `CMAKE_EXE_LINKER_FLAGS` is not overriden.

this should enable us to build with Clang and with sanitizer(s) enabled.

Signed-off-by: Kefu Chai <[email protected]>
…on extensions

if sanitizers are enabled, we have to populate the required link flags
to python extensions's building workflow. otherwise ld would fail to
link like:
```
/usr/bin/ld: /home/jenkins-build/build/workspace/ceph-pull-requests/build/lib/libceph-common.so.2: undefined reference to `__asan_stack_free_10'
```

Signed-off-by: Kefu Chai <[email protected]>
when building python bindings extensions using Cython, we first perform
sanity checks by building an executable and linking it against the
shared library of the C language binding. if the executable builds,
we consider the sanity test passes.

before this change, we don't add `LDFLAGS` specified in environmental
variable to the ldflags when building the executable. but we need to
link against libasan, so that the symbols used by, for instance,
librados.so, can be found by the executable.

in this change, we always check `LDFLAGS`, and add it to the ldflags
when building the executable for testing, if it's set.

Signed-off-by: Kefu Chai <[email protected]>
turns out we have multiple copies of following symbol defined by rbd executable:
```
AddressSanitizer: odr-violation: global 'ceph::buffer::list::always_empty_bptr' at /home/jenkins-build/build/workspace/ceph-pull-requests/src/common/buffer.cc:1267:34
```
before addressing it. let's disable this warning.

Refs https://tracker.ceph.com/issues/65098

Signed-off-by: Kefu Chai <[email protected]>
there are known leaks, which are tracked by qa/lsan.suppr, in Ceph.
so let's reuse it so we don't see them when running unit test with
ASan enabled.

see also https://clang.llvm.org/docs/AddressSanitizer.html#issue-suppression

Signed-off-by: Kefu Chai <[email protected]>
LeakSanitizer reports
```
==688591==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 45 byte(s) in 1 object(s) allocated from:
    #0 0x55f8dd9969dd in operator new(unsigned long) (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_fastbmap_allocator+0x1f89dd) (BuildId: cac39eac8ef1e8774f9dd48e6e3f677fdd864776)
    #1 0x55f8dd99c730 in __gnu_cxx::new_allocator<char>::allocate(unsigned long, void const*) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ext/new_allocator.h:127:27
    #2 0x55f8dd99c690 in std::allocator<char>::allocate(unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/allocator.h:185:32
    #3 0x55f8dd99c690 in std::allocator_traits<std::allocator<char> >::allocate(std::allocator<char>&, unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/alloc_traits.h:464:20
    #4 0x55f8dd99c393 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_create(unsigned long&, unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.tcc:153:14
    #5 0x55f8dda96a6c in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.tcc:307:21
    #6 0x55f8dda96852 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_append(char const*, unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.tcc:395:8
    #7 0x7f4a751ab6f0 in
    MallocExtension::Initialize() (/lib/x86_64-linux-gnu/libtcmalloc.so.4+0x2a6f0) (BuildId:
    eeef3d1257388a806e122398dbce3157ee568ef4)
```

this is a global object allocated by the allocator, so we can suppress this report.

Signed-off-by: Kefu Chai <[email protected]>
@tchaikov
tchaikov force-pushed the wip-cmake-enable-sanitizers branch from 241891a to 1a80165 Compare March 26, 2024 23:35
@tchaikov

tchaikov commented Mar 26, 2024 •

Copy link
Copy Markdown
Contributor Author

v7:

  • add quotes around $CMAKE_SHARED_LINKER_FLAGS in string(REPLACE " " ";" PY_LDFLAGS "${CMAKE_SHARED_LINKER_FLAGS}"), as without any sanitizers enabled, this variable is empty. and CMake errors out with
    CMake Error at cmake/modules/Distutils.cmake:76 (string):
    string sub-command REPLACE requires at least four arguments.
    Call Stack (most recent call first):
       src/pybind/rados/CMakeLists.txt:1 (distutils_add_cython_module)
    

@tchaikov

Copy link
Copy Markdown
Contributor Author
Test failures:

	 32 - run-rbd-unit-tests-61.sh (Failed)

@tchaikov

Copy link
Copy Markdown
Contributor Author

jenkins test make check

1 similar comment
@tchaikov

Copy link
Copy Markdown
Contributor Author

jenkins test make check

@tchaikov
tchaikov merged commit 87a931c into ceph:main Mar 27, 2024
@tchaikov
tchaikov deleted the wip-cmake-enable-sanitizers branch March 27, 2024 23:16
@tchaikov

Copy link
Copy Markdown
Contributor Author

thank you Casey, i am continuing the saga at #56537 .

@leonid-s-usov

leonid-s-usov commented Mar 28, 2024 •

Copy link
Copy Markdown
Contributor

Hi, I'm seeing this error now when building the latest main on a vossi:

[2/413] Generating ../../../lib/cython_modules/lib.3/rados.cpython-36m-x86_64-linux-gnu.so
FAILED: lib/cython_modules/lib.3/rados.cpython-36m-x86_64-linux-gnu.so
cd /home/leonidus/ceph/ceph/src/pybind/rados && env CC="/opt/rh/gcc-toolset-11/root/usr/bin/gcc" CFLAGS="-fno-var-tracking-assignments" CPPFLAGS="-iquote/home/leonidus/ceph/ceph/src/include -w -D'void0=dead_function(void)' -D'__Pyx_check_single_interpreter(ARG)=ARG##0'" CXX="/opt/rh/gcc-toolset-11/root/usr/bin/g++" LDSHARED="/opt/rh/gcc-toolset-11/root/usr/bin/gcc -shared" OPT="-DNDEBUG -g -fwrapv -O2 -w" LDFLAGS="-L/home/leonidus/ceph/ceph/build/lib" CYTHON_BUILD_DIR=/home/leonidus/ceph/ceph/build/src/pybind/rados CEPH_LIBDIR=/home/leonidus/ceph/ceph/build/lib /usr/bin/python3.6 /home/leonidus/ceph/ceph/src/pybind/rados/setup.py build --build-base /home/leonidus/ceph/ceph/build/lib/cython_modules --build-platlib /home/leonidus/ceph/ceph/build/lib/cython_modules/lib.3
  File "/home/leonidus/ceph/ceph/src/pybind/rados/setup.py", line 115
    if ldflags := os.environ.get('LDFLAGS'):
                ^
SyntaxError: invalid syntax
[3/413] Generating ../../../lib/cython_modules/lib.3/cephfs.cpython-36m-x86_64-linux-gnu.so
FAILED: lib/cython_modules/lib.3/cephfs.cpython-36m-x86_64-linux-gnu.so
cd /home/leonidus/ceph/ceph/src/pybind/cephfs && env CC="/opt/rh/gcc-toolset-11/root/usr/bin/gcc" CFLAGS="" CPPFLAGS="-iquote/home/leonidus/ceph/ceph/src/include -w -D'void0=dead_function(void)' -D'__Pyx_check_single_interpreter(ARG)=ARG##0'" CXX="/opt/rh/gcc-toolset-11/root/usr/bin/g++" LDSHARED="/opt/rh/gcc-toolset-11/root/usr/bin/gcc -shared" OPT="-DNDEBUG -g -fwrapv -O2 -w" LDFLAGS="-L/home/leonidus/ceph/ceph/build/lib" CYTHON_BUILD_DIR=/home/leonidus/ceph/ceph/build/src/pybind/cephfs CEPH_LIBDIR=/home/leonidus/ceph/ceph/build/lib /usr/bin/python3.6 /home/leonidus/ceph/ceph/src/pybind/cephfs/setup.py build --build-base /home/leonidus/ceph/ceph/build/lib/cython_modules --build-platlib /home/leonidus/ceph/ceph/build/lib/cython_modules/lib.3
  File "/home/leonidus/ceph/ceph/src/pybind/cephfs/setup.py", line 120
    if ldflags := os.environ.get('LDFLAGS'):
                ^

Is there any prerequisite I need to install? I've configured with

 ./do_cmake.sh -DCMAKE_BUILD_TYPE=RelWithDebInfo -DWITH_MGR_DASHBOARD_FRONTEND=OFF

@leonid-s-usov

Copy link
Copy Markdown
Contributor

Classic: found the answer after posting. Looks like my python is out of date.

@tchaikov

Copy link
Copy Markdown
Contributor Author

i can create a workaround, if we have to support python 3.6.

@baum

baum commented Mar 28, 2024 •

Copy link
Copy Markdown
Contributor

@leonid-s-usov @tchaikov It seems the shaman CentOS8 builders, which serve as the base for container images, aren't current with the ":=" syntax. Could you take a look at #56544 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants