Skip to content

Commit

Permalink
[RSDK-5863] Add Address Sanitizer (ASan) option for intel realsense m…
Browse files Browse the repository at this point in the history
…odule (#38)

* Make init changes to cmakelists

* Add rest of changes needed

* Update README and var name

* Add extra sanitizer deps in script conditionally

* Update cmakelists to include -fsanitize-address-use-after-scope and -fno-omit-frame-pointer

* Update README with extra details and supported platforms note

* Remove extra dir in .gitignore

* Remove artifacts from git

* Remove changes to x86 appimage recipe since we're not supporting it
  • Loading branch information
hexbabe authored Apr 30, 2024
1 parent 9269e3e commit 619d91c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
37 changes: 37 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ project(viam-camera-realsense
)
set(CMAKE_CXX_STANDARD 17)

# Define an option for enabling build with AddressSanitizer
option(ENABLE_SANITIZER "Enable AddressSanitizer" OFF)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(realsense2 REQUIRED)
Expand All @@ -32,6 +35,40 @@ endif()
add_executable(viam-camera-realsense main.cpp)
target_link_libraries(viam-camera-realsense PRIVATE viamrealsense)

if(ENABLE_SANITIZER)
add_compile_options(
-fsanitize=address
-fsanitize-address-use-after-scope
-fno-omit-frame-pointer
)
add_link_options(
-fsanitize=address
-fno-omit-frame-pointer
)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libasan")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libasan -fno-omit-frame-pointer")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libasan")

target_compile_options(viamrealsense PRIVATE
-fsanitize=address
-fsanitize-address-use-after-scope
-fno-omit-frame-pointer
)
target_link_options(viamrealsense PRIVATE
-fsanitize=address
-fno-omit-frame-pointer
)
target_compile_options(viam-camera-realsense PRIVATE
-fsanitize=address
-fsanitize-address-use-after-scope
-fno-omit-frame-pointer
)
target_link_options(viam-camera-realsense PRIVATE
-fsanitize=address
-fno-omit-frame-pointer
)
endif()

# Add shared flags
target_compile_features(viamrealsense PUBLIC
cxx_std_17
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
format: src/*.cpp src/*.hpp test/*.cpp
ls src/*.cpp src/*.hpp test/*.cpp | xargs clang-format -i --style="{BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 100}"

SANITIZE ?= OFF
viam-camera-realsense: src/*
rm -rf build/ && \
mkdir build && \
cd build && \
cmake -G Ninja .. && \
cmake -G Ninja -DENABLE_SANITIZER=$(SANITIZE) .. && \
ninja all -j 4 && \
cp viam-camera-realsense ../

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Use the commands
docker pull ghcr.io/viamrobotics/viam-camera-realsense:arm64
git clone https://github.com/viamrobotics/viam-camera-realsense/
cd viam-camera-realsense/
canon -arch arm64 make appimage
canon -arch arm64 make appimage-arm64
```

This will use the Docker container to compile a binary for the `aarch64` architecture. If you want to compile for `x86_64`/`amd64` architecture, change `arm64` to `amd64` in the above commands. The AppImage will be put in the `packaging/appimages/deploy` directory.
Expand All @@ -164,3 +164,6 @@ If you would like to try to gather all of the dependencies yourself and not use

then do `make viam-camera-realsense` to compile the binary, and `make appimage` to create the AppImage.

## Building with Address Sanitizer

When developing, you also have the option to build the module with ASAN/LSAN enabled to test for memory leaks. You can do so by running a build command such as `canon -arch arm64 make clean appimage-arm64 SANITIZE=ON` with the `SANITIZE` flag `=ON`. ASAN/LSAN logs will then be included as error logs in your robot logs on the Viam App. Additionally, running the integration test binary against the debug ASAN/LSAN build will fail if a leak is detected. Currently the debug ASAN/LSAN build is only supported on linux/arm64.
8 changes: 8 additions & 0 deletions integration/tests/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func TestCameraServer(t *testing.T) {
_, _, err = cam.Images(context.Background())
test.That(t, err, test.ShouldBeNil)
})
t.Run("get image method", func(t *testing.T) {
cam, err := camera.FromRobot(myRobot, "TheRealSense")
test.That(t, err, test.ShouldBeNil)
stream, err := cam.Stream(context.Background())
test.That(t, err, test.ShouldBeNil)
_, _, err = stream.Next(context.Background())
test.That(t, err, test.ShouldBeNil)
})
t.Run("shutdown the robot", func(t *testing.T) {
test.That(t, myRobot.Close(context.Background()), test.ShouldBeNil)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ AppDir:
- sourceline: deb [trusted=yes] http://deb.debian.org/debian bookworm-updates main
include:
- libturbojpeg0:arm64
- libjpeg62-turbo:arm64
exclude:
- usr/share/man
- usr/share/doc
Expand Down
8 changes: 8 additions & 0 deletions packaging/appimages/viam-camera-realsense-aarch64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ script:
- go install github.com/Otterverse/aix@latest
- cp `go env GOPATH`/bin/aix $TARGET_APPDIR/usr/bin/
- chmod 755 $TARGET_APPDIR/usr/bin/*
- |
if [ "${SANITIZE}" = "ON" ]; then
mkdir -p $TARGET_APPDIR/usr/lib
cp /lib/aarch64-linux-gnu/libabsl_* $TARGET_APPDIR/usr/lib/
cp /lib/aarch64-linux-gnu/libboost_* $TARGET_APPDIR/usr/lib/
fi
AppDir:
path: ./AppDir
Expand Down Expand Up @@ -42,6 +48,8 @@ AppDir:
runtime:
env:
AIX_TARGET: usr/bin/viam-camera-realsense
ASAN_OPTIONS: detect_leaks=1 detect_stack_use_after_return=true
LSAN_OPTIONS: verbosity=1:log_threads=1
AppImage:
arch: aarch64
comp: gzip
Expand Down

0 comments on commit 619d91c

Please sign in to comment.