|
| 1 | +//===--- ThreadSanitizer.h - Thread Sanitizer support --------- -*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Helper functions for code that needs to integrate with the thread |
| 14 | +// sanitizer. In particular, TSan can't see inside the runtime libraries, |
| 15 | +// so we occasionally need to give it a hint that we're doing synchronization |
| 16 | +// in order to avoid false positives. |
| 17 | +// |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | + |
| 20 | +#ifndef SWIFT_THREADING_THREAD_SANITIZER_H |
| 21 | +#define SWIFT_THREADING_THREAD_SANITIZER_H |
| 22 | + |
| 23 | +#include "swift/shims/Visibility.h" |
| 24 | + |
| 25 | +namespace swift { |
| 26 | + |
| 27 | +#if defined(_WIN32) || defined(__wasi__) || !__has_include(<dlfcn.h>) |
| 28 | + |
| 29 | +#define SWIFT_THREADING_TSAN_SUPPORT 0 |
| 30 | + |
| 31 | +namespace tsan { |
| 32 | + |
| 33 | +inline bool enabled() { return false; } |
| 34 | +template <typename T> T *acquire(T *ptr) { return ptr; } |
| 35 | +template <typename T> T *release(T *ptr) { return ptr; } |
| 36 | + |
| 37 | +} // namespace tsan |
| 38 | +#else |
| 39 | + |
| 40 | +#define SWIFT_THREADING_TSAN_SUPPORT 1 |
| 41 | + |
| 42 | +// If we're static linking to libswiftThreading.a, these symbols can come |
| 43 | +// from there. If, on the other hand, we're dynamically linked, we want |
| 44 | +// to get them from libswiftCore.dylib instead. |
| 45 | +#if SWIFT_THREADING_STATIC |
| 46 | +#define SWIFT_THREADING_EXPORT extern "C" |
| 47 | +#else |
| 48 | +#define SWIFT_THREADING_EXPORT SWIFT_RUNTIME_EXPORT |
| 49 | +#endif |
| 50 | + |
| 51 | +namespace threading_impl { |
| 52 | + |
| 53 | +SWIFT_THREADING_EXPORT bool _swift_tsan_enabled; |
| 54 | +SWIFT_THREADING_EXPORT void (*_swift_tsan_acquire)(const void *ptr); |
| 55 | +SWIFT_THREADING_EXPORT void (*_swift_tsan_release)(const void *ptr); |
| 56 | + |
| 57 | +} // namespace threading_impl |
| 58 | + |
| 59 | +namespace tsan { |
| 60 | + |
| 61 | +/// Returns true if TSan is enabled |
| 62 | +inline bool enabled() { |
| 63 | + return threading_impl::_swift_tsan_enabled; |
| 64 | +} |
| 65 | + |
| 66 | +/// Inform TSan about a synchronization operation. |
| 67 | +/// |
| 68 | +/// This is used when TSan cannot see the synchronization operation, for |
| 69 | +/// example, if it is using a custom primitive for which TSan doesn't have |
| 70 | +/// a built-in interceptor. This does not necessarily mean a lock or a C(++) |
| 71 | +/// atomic operation - it could be any kind of synchronization mechanism. |
| 72 | +/// |
| 73 | +/// An acquire-release pair using the same address establishes an ordering |
| 74 | +/// constraint in TSan's happens-before graph, which TSan uses to determine |
| 75 | +/// whether two memory accesses from different threads have a well-defined |
| 76 | +/// order. |
| 77 | +/// |
| 78 | +/// For instance, in |
| 79 | +/// |
| 80 | +/// Thread 1 Thread 2 |
| 81 | +/// |
| 82 | +/// access to y |
| 83 | +/// tsan::release(x) |
| 84 | +/// lock given away |
| 85 | +/// |
| 86 | +/// --> sync point --> |
| 87 | +/// |
| 88 | +/// lock taken |
| 89 | +/// tsan::acquire(x) |
| 90 | +/// access to y |
| 91 | +/// |
| 92 | +/// the access to y from Thread 2 is safe relative to the preceding access to |
| 93 | +/// y on Thread 1 because it is preceded by an acquire of x that was itself |
| 94 | +/// preceded by a release of x. |
| 95 | +template <typename T> |
| 96 | +T *acquire(T *ptr) { |
| 97 | + if (threading_impl::_swift_tsan_acquire) { |
| 98 | + threading_impl::_swift_tsan_acquire(ptr); |
| 99 | + } |
| 100 | + return ptr; |
| 101 | +} |
| 102 | + |
| 103 | +/// Inform TSan about a synchronization operation. |
| 104 | +/// |
| 105 | +/// This is the counterpart to tsan::acquire. |
| 106 | +template <typename T> |
| 107 | +T *release(T *ptr) { |
| 108 | + if (threading_impl::_swift_tsan_release) { |
| 109 | + threading_impl::_swift_tsan_release(ptr); |
| 110 | + } |
| 111 | + return ptr; |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace tsan |
| 115 | + |
| 116 | +#endif |
| 117 | + |
| 118 | +} // namespace swift |
| 119 | + |
| 120 | +#endif |
0 commit comments