Skip to content

Commit

Permalink
[cmake] Add a definition to enable clang sanitizers.
Browse files Browse the repository at this point in the history
This can be useful to detect diverse memory mishandling. Case at hand here is
the detection of a memory leak introduced in the past that went undetected for a while.

Currently, it may not be possible to enable it as part of the bcc CI as there is
a few existing leaks, but long term it may be beneficial to enable this as part
of the CI si bus get caught early on.
In the meantime, it is still valuable to be able to enable this in an ad-hoc manner.

Testing:

Ran the building step with:
```
docker run --privileged \
                   --pid=host \
                   -v $(pwd):/bcc \
                   -v /sys/kernel/debug:/sys/kernel/debug:rw \
                   -v /lib/modules:/lib/modules:ro \
                   -v /usr/src:/usr/src:ro \
                   -v /usr/include/linux:/usr/include/linux:ro \
                   bcc-docker \
                   /bin/bash -c \
                   'mkdir -p /bcc/build && cd /bcc/build && \
                    cmake -DCMAKE_BUILD_TYPE=Debug -DENABLE_LLVM_NATIVECODEGEN=OFF -DCMAKE_SANITIZE_TYPE=leak .. && make -j9'
```

Followed by a test run:
```
docker run -ti \
                    --privileged \
                    --network=host \
                    --pid=host \
                    -v $(pwd):/bcc \
                    -v /sys/kernel/debug:/sys/kernel/debug:rw \
                    -v /lib/modules:/lib/modules:ro \
                    -v /usr/src:/usr/src:ro \
                    -e CTEST_OUTPUT_ON_FAILURE=1 \
                    bcc-docker \
                    /bin/bash -c \
                    '/bcc/build/tests/wrapper.sh \
                        c_test_all sudo /bcc/build/tests/cc/test_libbcc'
```

Test run shows leaks in https://gist.github.com/chantra/ef964a4805d94ea47199e9d62e2231ca
  • Loading branch information
chantra authored and yonghong-song committed Jul 5, 2022
1 parent c54336e commit 741ba58
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

if(CMAKE_SANITIZE_TYPE)
add_compile_options(-fsanitize=${CMAKE_SANITIZE_TYPE})
add_link_options(-fsanitize=${CMAKE_SANITIZE_TYPE})
endif()


if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "path to install" FORCE)
endif()
Expand Down

0 comments on commit 741ba58

Please sign in to comment.