diff --git a/.github/workflows/x86_64-linux.yml b/.github/workflows/x86_64-linux.yml new file mode 100644 index 0000000..a6ab3da --- /dev/null +++ b/.github/workflows/x86_64-linux.yml @@ -0,0 +1,45 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* +name: Bazel Build (Linux) +on: + pull_request: + types: [opened, reopened, synchronize] + push: + branches: + - main + merge_group: + types: [checks_requested] + workflow_call: +jobs: + host_config_1: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./examples + steps: + - name: Checkout Repository + uses: actions/checkout@v6 + - name: Setup Bazel + uses: bazel-contrib/setup-bazel@0.18.0 + with: + bazelisk-cache: true + disk-cache: ${{ github.job }} + repository-cache: true + cache-save: ${{ github.event_name == 'push' }} + - name: Bazel Build (basic) + run: | + bazel build --config host_config_1 -- //... + - name: Bazel Test + run: | + bazel test --config host_config_1 -- //... + diff --git a/examples/.bazelversion b/examples/.bazelversion index c9277c5..56b6be4 100644 --- a/examples/.bazelversion +++ b/examples/.bazelversion @@ -1 +1 @@ -9.0.0 \ No newline at end of file +8.3.1 diff --git a/examples/BUILD b/examples/BUILD index 92232d3..f675bbd 100644 --- a/examples/BUILD +++ b/examples/BUILD @@ -40,7 +40,7 @@ cc_test( cc_shared_library( name = "math_lib_shared", - shared_lib_name = "libmath_lib.so", + shared_lib_name = "libmath_lib_shared.so", deps = [":math_lib"], ) @@ -61,3 +61,47 @@ cc_test( "@googletest//:gtest_main", ], ) + +# for some reason this test is very flaky. +# cc_test( +# name = "tsan_test", +# srcs = ["tsan_test.cpp"], +# features = ["tsan"], +# env = { +# "TSAN_OPTIONS": "halt_on_error=1", +# }, +# deps = [ +# "@googletest//:gtest", +# "@googletest//:gtest_main", +# ], +# ) + +cc_test( + name = "ubsan_test", + srcs = ["ubsan_test.cpp"], + features = ["ubsan"], + env = { + "UBSAN_OPTIONS": "halt_on_error=1", + }, + deps = [ + "@googletest//:gtest", + "@googletest//:gtest_main", + ], +) + +cc_test( + name = "lsan_test", + srcs = ["lsan_test.cpp"], + features = [ + "lsan", + "asan", + ], + env = { + "ASAN_OPTIONS": "detect_leaks=1", + "LSAN_OPTIONS": "exitcode=23", + }, + deps = [ + "@googletest//:gtest", + "@googletest//:gtest_main", + ], +) diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index c9a958d..ae38379 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel @@ -25,7 +25,7 @@ bazel_dep(name = "bazel_skylib", version = "1.8.2") # Constraint values for specifying platforms and toolchains # ******************************************************************************* bazel_dep(name = "platforms", version = "1.0.0") -bazel_dep(name = "score_bazel_platforms", version = "0.1.0") +bazel_dep(name = "score_bazel_platforms", version = "0.1.1") # ******************************************************************************* # C++ Rules for Bazel diff --git a/examples/lsan_test.cpp b/examples/lsan_test.cpp new file mode 100644 index 0000000..66eb34a --- /dev/null +++ b/examples/lsan_test.cpp @@ -0,0 +1,32 @@ +/******************************************************************************** +* Copyright (c) 2025 Contributors to the Eclipse Foundation +* +* See the NOTICE file(s) distributed with this work for additional +* information regarding copyright ownership. +* +* This program and the accompanying materials are made available under the +* terms of the Apache License Version 2.0 which is available at +* https://www.apache.org/licenses/LICENSE-2.0 +* +* SPDX-License-Identifier: Apache-2.0 +********************************************************************************/ + +#include +#include + +static void lsan_buggy() { + int* leaked = new int[100]; + leaked[0] = 42; + + // Drop the last visible reference so LSan can't find it on the stack. + leaked = nullptr; + + std::exit(0); +} + +TEST(LsanBugReproTest, MemoryLeak_ShouldBeReportedByLeakSanitizer) { + EXPECT_EXIT( + { lsan_buggy(); }, + ::testing::ExitedWithCode(23), + ".*"); +} \ No newline at end of file diff --git a/examples/tsan_test.cpp b/examples/tsan_test.cpp new file mode 100644 index 0000000..21dc032 --- /dev/null +++ b/examples/tsan_test.cpp @@ -0,0 +1,41 @@ +/******************************************************************************** +* Copyright (c) 2025 Contributors to the Eclipse Foundation +* +* See the NOTICE file(s) distributed with this work for additional +* information regarding copyright ownership. +* +* This program and the accompanying materials are made available under the +* terms of the Apache License Version 2.0 which is available at +* https://www.apache.org/licenses/LICENSE-2.0 +* +* SPDX-License-Identifier: Apache-2.0 +********************************************************************************/ + +#include +#include +#include + +static int shared_value = 0; + +static void tsan_buggy() { + std::thread writer([]() { + for (int i = 0; i < 100000; ++i) { + shared_value = i; // unsynchronized write + } + }); + + std::thread reader([]() { + int tmp = 0; + for (int i = 0; i < 100000; ++i) { + tmp = shared_value; // unsynchronized read + } + (void)tmp; + }); + + writer.join(); + reader.join(); +} + +TEST(TsanBugReproTest, ReadWriteRace_ShouldCrashUnderThreadSanitizer) { + ASSERT_DEATH({ tsan_buggy(); }, ".*"); +} \ No newline at end of file diff --git a/examples/ubsan_test.cpp b/examples/ubsan_test.cpp new file mode 100644 index 0000000..18ef3d8 --- /dev/null +++ b/examples/ubsan_test.cpp @@ -0,0 +1,28 @@ +/******************************************************************************** +* Copyright (c) 2025 Contributors to the Eclipse Foundation +* +* See the NOTICE file(s) distributed with this work for additional +* information regarding copyright ownership. +* +* This program and the accompanying materials are made available under the +* terms of the Apache License Version 2.0 which is available at +* https://www.apache.org/licenses/LICENSE-2.0 +* +* SPDX-License-Identifier: Apache-2.0 +********************************************************************************/ + +#include +#include + +static int ubsan_buggy() { + volatile int x = INT_MAX; + volatile int y = 1; + + // Signed overflow is UB. + return x + y; +} + +TEST(UbsanBugReproTest, SignedOverflow_ShouldCrashUnderUndefinedBehaviorSanitizer) { + // Requires -fno-sanitize-recover=undefined or UBSAN_OPTIONS=halt_on_error=1 + ASSERT_DEATH({ (void)ubsan_buggy(); }, ".*"); +} \ No newline at end of file