Skip to content

Commit 72009f0

Browse files
committed
[#957] Enable sanitizers for rust
Signed-off-by: Ziad Mostafa <[email protected]>
1 parent 6a7ec2a commit 72009f0

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

.github/workflows/build-test.yml

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,16 @@ jobs:
683683
strategy:
684684
matrix:
685685
os: [ubuntu-latest, macos-latest]
686-
toolchain: [stable]
686+
toolchain: [nightly] # Rust sanitizers require nightly
687687
sanitizer: ["address", "ub", "address;ub", "thread"]
688+
exclude:
689+
# Thread sanitizer is not well supported on macOS
690+
- os: macos-latest
691+
sanitizer: "thread"
688692
timeout-minutes: 60
689693
runs-on: ${{ matrix.os }}
694+
env:
695+
RUST_BACKTRACE: 1
690696
steps:
691697
- name: Checkout sources
692698
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # ratchet:actions/checkout@v4
@@ -704,6 +710,7 @@ jobs:
704710
with:
705711
toolchain: ${{ matrix.toolchain }}
706712
components: rustfmt, clippy
713+
targets: x86_64-unknown-linux-gnu, x86_64-apple-darwin
707714

708715
- name: Download artifact cargo-nextest
709716
uses: ./.github/actions/download-cached-rust-tool
@@ -717,11 +724,34 @@ jobs:
717724
internal/scripts/ci_prepare_ubuntu.sh
718725
uname -a
719726
727+
- name: Set Rust sanitizer flags
728+
run: |
729+
# Set default target based on OS
730+
if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
731+
RUST_TARGET="x86_64-unknown-linux-gnu"
732+
elif [[ "${{ matrix.os }}" == "macos-latest" ]]; then
733+
RUST_TARGET="x86_64-apple-darwin"
734+
fi
735+
736+
# Set sanitizer-specific flags
737+
if [[ "${{ matrix.sanitizer }}" == "address" ]]; then
738+
echo "RUSTFLAGS=-Zsanitizer=address -Copt-level=1" >> $GITHUB_ENV
739+
elif [[ "${{ matrix.sanitizer }}" == "ub" ]]; then
740+
# Rust doesn't have a specific undefined behavior sanitizer, but we can enable debug assertions
741+
echo "RUSTFLAGS=-Cdebug-assertions=on -Coverflow-checks=on -Copt-level=1" >> $GITHUB_ENV
742+
elif [[ "${{ matrix.sanitizer }}" == "address;ub" ]]; then
743+
echo "RUSTFLAGS=-Zsanitizer=address -Cdebug-assertions=on -Coverflow-checks=on -Copt-level=1" >> $GITHUB_ENV
744+
elif [[ "${{ matrix.sanitizer }}" == "thread" ]]; then
745+
echo "RUSTFLAGS=-Zsanitizer=thread -Copt-level=1" >> $GITHUB_ENV
746+
fi
747+
748+
echo "RUST_TARGET=$RUST_TARGET" >> $GITHUB_ENV
749+
720750
- name: Run cargo build
721-
run: cargo build --workspace --all-targets
751+
run: cargo build --workspace --all-targets --target ${{ env.RUST_TARGET }}
722752

723753
- name: Run cargo nextest
724-
run: cargo nextest run --workspace --all-targets --no-fail-fast
754+
run: cargo nextest run --workspace --all-targets --no-fail-fast --target ${{ env.RUST_TARGET }}
725755

726756
- name: Build iceoryx_hoofs
727757
run: internal/scripts/ci_build_and_install_iceoryx_hoofs.sh
@@ -735,7 +765,7 @@ jobs:
735765
-DSANITIZERS="${{ matrix.sanitizer }}" \
736766
-DCMAKE_INSTALL_PREFIX=target/ff/cc/install \
737767
-DCMAKE_PREFIX_PATH="${{ github.workspace }}/target/ff/iceoryx/install" \
738-
-DRUST_BUILD_ARTIFACT_PATH="${{ github.workspace }}/target/debug"
768+
-DRUST_BUILD_ARTIFACT_PATH="${{ github.workspace }}/target/${{ env.RUST_TARGET }}/debug"
739769
cmake --build target/ff/cc/build
740770
cmake --install target/ff/cc/build
741771
@@ -755,31 +785,43 @@ jobs:
755785
TSAN_OPTIONS: "halt_on_error=1:history_size=7"
756786
run: target/ff/cc/build/tests/iceoryx2-cxx-tests
757787

758-
- name: Run C examples with sanitizers
759-
env:
760-
ASAN_OPTIONS: "detect_leaks=1:detect_stack_use_after_return=1:detect_stack_use_after_scope=1:check_initialization_order=true:strict_init_order=true:new_delete_type_mismatch=0:intercept_tls_get_addr=0"
761-
LSAN_OPTIONS: "verbosity=1:log_threads=1"
762-
UBSAN_OPTIONS: "print_stacktrace=1:halt_on_error=1"
763-
TSAN_OPTIONS: "halt_on_error=1:history_size=7"
764-
run: |
765-
cd target/ff/cc/build/examples/c
766-
find . -maxdepth 1 -type f -executable | while read example; do
767-
echo "Running C example: $example"
768-
timeout 10s $example || true
769-
done
770-
771-
- name: Run C++ examples with sanitizers
788+
# - name: Run C examples with sanitizers
789+
# env:
790+
# ASAN_OPTIONS: "detect_leaks=1:detect_stack_use_after_return=1:detect_stack_use_after_scope=1:check_initialization_order=true:strict_init_order=true:new_delete_type_mismatch=0:intercept_tls_get_addr=0"
791+
# LSAN_OPTIONS: "verbosity=1:log_threads=1"
792+
# UBSAN_OPTIONS: "print_stacktrace=1:halt_on_error=1"
793+
# TSAN_OPTIONS: "halt_on_error=1:history_size=7"
794+
# run: |
795+
# cd target/ff/cc/build/examples/c
796+
# find . -maxdepth 1 -type f -executable | while read example; do
797+
# echo "Running C example: $example"
798+
# timeout 10s $example || true
799+
# done
800+
801+
# - name: Run C++ examples with sanitizers
802+
# env:
803+
# ASAN_OPTIONS: "detect_leaks=1:detect_stack_use_after_return=1:detect_stack_use_after_scope=1:check_initialization_order=true:strict_init_order=true:new_delete_type_mismatch=0:intercept_tls_get_addr=0"
804+
# LSAN_OPTIONS: "verbosity=1:log_threads=1"
805+
# UBSAN_OPTIONS: "print_stacktrace=1:halt_on_error=1"
806+
# TSAN_OPTIONS: "halt_on_error=1:history_size=7"
807+
# run: |
808+
# cd target/ff/cc/build/examples/cxx
809+
# find . -maxdepth 1 -type f -executable | while read example; do
810+
# echo "Running C++ example: $example"
811+
# timeout 10s $example || true
812+
# done
813+
814+
- name: Run end-to-end tests with sanitizers
815+
# Skip end-to-end tests with thread sanitizer due to potential issues with dynamic linking
816+
if: ${{ matrix.sanitizer != 'thread' }}
772817
env:
773818
ASAN_OPTIONS: "detect_leaks=1:detect_stack_use_after_return=1:detect_stack_use_after_scope=1:check_initialization_order=true:strict_init_order=true:new_delete_type_mismatch=0:intercept_tls_get_addr=0"
774819
LSAN_OPTIONS: "verbosity=1:log_threads=1"
775820
UBSAN_OPTIONS: "print_stacktrace=1:halt_on_error=1"
776821
TSAN_OPTIONS: "halt_on_error=1:history_size=7"
777822
run: |
778-
cd target/ff/cc/build/examples/cxx
779-
find . -maxdepth 1 -type f -executable | while read example; do
780-
echo "Running C++ example: $example"
781-
timeout 10s $example || true
782-
done
823+
cd "${GITHUB_WORKSPACE}"
824+
internal/scripts/ci_build_and_run_end_to_end_tests.sh no-build
783825
784826
cargo-publish-dry-run:
785827
needs: [preflight-check, static-code-analysis]

0 commit comments

Comments
 (0)