Skip to content

Commit

Permalink
Add TSAN check support and skip some test on Linux when TSAN is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Dec 19, 2023
1 parent f297112 commit 881b858
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Sources/COpenCombineHelpers/COpenCombineHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,27 @@ void opencombine_stop_in_debugger(void) {
raise(SIGTRAP);
#endif
}

bool opencombine_sanitize_address_enabled(void) {
#if ASAN_ENABLED
return true;
#else
return false;
#endif
}

bool opencombine_sanitize_thread_enabled(void) {
#if TSAN_ENABLED
return true;
#else
return false;
#endif
}

bool opencombine_sanitize_coverage_enabled(void) {
#if COVERAGE_ENABLED
return true;
#else
return false;
#endif
}
10 changes: 10 additions & 0 deletions Sources/COpenCombineHelpers/include/COpenCombineHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#define COPENCOMBINEHELPERS_H

#include <stdint.h>
#include <stdbool.h>
#include "Compiler.h"

#if __has_attribute(swift_name)
# define OPENCOMBINE_SWIFT_NAME(_name) __attribute__((swift_name(#_name)))
Expand Down Expand Up @@ -72,6 +74,14 @@ void opencombine_unfair_recursive_lock_dealloc(OpenCombineUnfairRecursiveLock lo

void opencombine_stop_in_debugger(void) OPENCOMBINE_SWIFT_NAME(__stopInDebugger());

#pragma mark - COMPILER_SUPPORTS

bool opencombine_sanitize_address_enabled(void) OPENCOMBINE_SWIFT_NAME(__sanitizeAddressEnabled());

bool opencombine_sanitize_thread_enabled(void) OPENCOMBINE_SWIFT_NAME(__sanitizeThreadEnabled());

bool opencombine_sanitize_coverage_enabled(void) OPENCOMBINE_SWIFT_NAME(__sanitizeCoverageEnabled());

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
74 changes: 74 additions & 0 deletions Sources/COpenCombineHelpers/include/Compiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2011-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

/* COMPILER_HAS_CLANG_FEATURE() - whether the compiler supports a particular language or library feature. */
/* http://clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension */
#ifdef __has_feature
#define COMPILER_HAS_CLANG_FEATURE(x) __has_feature(x)
#else
#define COMPILER_HAS_CLANG_FEATURE(x) 0
#endif

/* ==== COMPILER_SUPPORTS - additional compiler feature detection, in alphabetical order ==== */

/* ASAN_ENABLED and SUPPRESS_ASAN */

#ifdef __SANITIZE_ADDRESS__
#define ASAN_ENABLED 1
#else
#define ASAN_ENABLED COMPILER_HAS_CLANG_FEATURE(address_sanitizer)
#endif

#if ASAN_ENABLED
#define SUPPRESS_ASAN __attribute__((no_sanitize_address))
#else
#define SUPPRESS_ASAN
#endif

/* TSAN_ENABLED and SUPPRESS_TSAN */

#ifdef __SANITIZE_THREAD__
#define TSAN_ENABLED 1
#else
#define TSAN_ENABLED COMPILER_HAS_CLANG_FEATURE(thread_sanitizer)
#endif

#if TSAN_ENABLED
#define SUPPRESS_TSAN __attribute__((no_sanitize_thread))
#else
#define SUPPRESS_TSAN
#endif

/* COVERAGE_ENABLED and SUPPRESS_COVERAGE */

#define COVERAGE_ENABLED COMPILER_HAS_CLANG_FEATURE(coverage_sanitizer)

#if COVERAGE_ENABLED
#define SUPPRESS_COVERAGE __attribute__((no_sanitize("coverage")))
#else
#define SUPPRESS_COVERAGE
#endif
14 changes: 14 additions & 0 deletions Tests/OpenCombineTests/PublisherTests/CatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import XCTest
import COpenCombineHelpers

#if OPENCOMBINE_COMPATIBILITY_TEST
import Combine
Expand All @@ -16,6 +17,19 @@ import OpenCombine
@available(macOS 10.15, iOS 13.0, *)
final class CatchTests: XCTestCase {

// FIXME: CatchTests will have 8 failure on Linux platform when TSAN is enabled
func skipIfNeeded() throws {
#if os(Linux)
if __sanitizeThreadEnabled() {
throw XCTSkip("Skip the test on TSAN is enabled on Linux")
}
#endif
}

override func setUp() async throws {
try skipIfNeeded()
}

// MARK: - Catch

func testSimpleCatch() {
Expand Down

0 comments on commit 881b858

Please sign in to comment.