tsan_assist.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Contemporary compilers implement lock-free atomic memory access
  11. * primitives that facilitate writing "thread-opportunistic" or even real
  12. * multi-threading low-overhead code. "Thread-opportunistic" is when
  13. * exact result is not required, e.g. some statistics, or execution flow
  14. * doesn't have to be unambiguous. Simplest example is lazy "constant"
  15. * initialization when one can synchronize on variable itself, e.g.
  16. *
  17. * if (var == NOT_YET_INITIALIZED)
  18. * var = function_returning_same_value();
  19. *
  20. * This does work provided that loads and stores are single-instruction
  21. * operations (and integer ones are on *all* supported platforms), but
  22. * it upsets Thread Sanitizer. Suggested solution is
  23. *
  24. * if (tsan_load(&var) == NOT_YET_INITIALIZED)
  25. * tsan_store(&var, function_returning_same_value());
  26. *
  27. * Production machine code would be the same, so one can wonder why
  28. * bother. Having Thread Sanitizer accept "thread-opportunistic" code
  29. * allows to move on trouble-shooting real bugs.
  30. *
  31. * Resolving Thread Sanitizer nits was the initial purpose for this module,
  32. * but it was later extended with more nuanced primitives that are useful
  33. * even in "non-opportunistic" scenarios. Most notably verifying if a shared
  34. * structure is fully initialized and bypassing the initialization lock.
  35. * It's suggested to view macros defined in this module as "annotations" for
  36. * thread-safe lock-free code, "Thread-Safe ANnotations"...
  37. *
  38. * It's assumed that ATOMIC_{LONG|INT}_LOCK_FREE are assigned same value as
  39. * ATOMIC_POINTER_LOCK_FREE. And check for >= 2 ensures that corresponding
  40. * code is inlined. It should be noted that statistics counters become
  41. * accurate in such case.
  42. *
  43. * Special note about TSAN_QUALIFIER. It might be undesired to use it in
  44. * a shared header. Because whether operation on specific variable or member
  45. * is atomic or not might be irrelevant in other modules. In such case one
  46. * can use TSAN_QUALIFIER in cast specifically when it has to count.
  47. */
  48. #ifndef OSSL_INTERNAL_TSAN_ASSIST_H
  49. # define OSSL_INTERNAL_TSAN_ASSIST_H
  50. # pragma once
  51. # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
  52. && !defined(__STDC_NO_ATOMICS__)
  53. # include <stdatomic.h>
  54. # if defined(ATOMIC_POINTER_LOCK_FREE) \
  55. && ATOMIC_POINTER_LOCK_FREE >= 2
  56. # define TSAN_QUALIFIER _Atomic
  57. # define tsan_load(ptr) atomic_load_explicit((ptr), memory_order_relaxed)
  58. # define tsan_store(ptr, val) atomic_store_explicit((ptr), (val), memory_order_relaxed)
  59. # define tsan_add(ptr, n) atomic_fetch_add_explicit((ptr), (n), memory_order_relaxed)
  60. # define tsan_ld_acq(ptr) atomic_load_explicit((ptr), memory_order_acquire)
  61. # define tsan_st_rel(ptr, val) atomic_store_explicit((ptr), (val), memory_order_release)
  62. # endif
  63. # elif defined(__GNUC__) && defined(__ATOMIC_RELAXED)
  64. # if defined(__GCC_ATOMIC_POINTER_LOCK_FREE) \
  65. && __GCC_ATOMIC_POINTER_LOCK_FREE >= 2
  66. # define TSAN_QUALIFIER volatile
  67. # define tsan_load(ptr) __atomic_load_n((ptr), __ATOMIC_RELAXED)
  68. # define tsan_store(ptr, val) __atomic_store_n((ptr), (val), __ATOMIC_RELAXED)
  69. # define tsan_add(ptr, n) __atomic_fetch_add((ptr), (n), __ATOMIC_RELAXED)
  70. # define tsan_ld_acq(ptr) __atomic_load_n((ptr), __ATOMIC_ACQUIRE)
  71. # define tsan_st_rel(ptr, val) __atomic_store_n((ptr), (val), __ATOMIC_RELEASE)
  72. # endif
  73. # elif defined(_MSC_VER) && _MSC_VER>=1200 \
  74. && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
  75. defined(_M_ARM64) || (defined(_M_ARM) && _M_ARM >= 7 && !defined(_WIN32_WCE)))
  76. /*
  77. * There is subtle dependency on /volatile:<iso|ms> command-line option.
  78. * "ms" implies same semantic as memory_order_acquire for loads and
  79. * memory_order_release for stores, while "iso" - memory_order_relaxed for
  80. * either. Real complication is that defaults are different on x86 and ARM.
  81. * There is explanation for that, "ms" is backward compatible with earlier
  82. * compiler versions, while multi-processor ARM can be viewed as brand new
  83. * platform to MSC and its users, and with non-relaxed semantic taking toll
  84. * with additional instructions and penalties, it kind of makes sense to
  85. * default to "iso"...
  86. */
  87. # define TSAN_QUALIFIER volatile
  88. # if defined(_M_ARM) || defined(_M_ARM64)
  89. # define _InterlockedExchangeAdd _InterlockedExchangeAdd_nf
  90. # pragma intrinsic(_InterlockedExchangeAdd_nf)
  91. # pragma intrinsic(__iso_volatile_load32, __iso_volatile_store32)
  92. # ifdef _WIN64
  93. # define _InterlockedExchangeAdd64 _InterlockedExchangeAdd64_nf
  94. # pragma intrinsic(_InterlockedExchangeAdd64_nf)
  95. # pragma intrinsic(__iso_volatile_load64, __iso_volatile_store64)
  96. # define tsan_load(ptr) (sizeof(*(ptr)) == 8 ? __iso_volatile_load64(ptr) \
  97. : __iso_volatile_load32(ptr))
  98. # define tsan_store(ptr, val) (sizeof(*(ptr)) == 8 ? __iso_volatile_store64((ptr), (val)) \
  99. : __iso_volatile_store32((ptr), (val)))
  100. # else
  101. # define tsan_load(ptr) __iso_volatile_load32(ptr)
  102. # define tsan_store(ptr, val) __iso_volatile_store32((ptr), (val))
  103. # endif
  104. # else
  105. # define tsan_load(ptr) (*(ptr))
  106. # define tsan_store(ptr, val) (*(ptr) = (val))
  107. # endif
  108. # pragma intrinsic(_InterlockedExchangeAdd)
  109. # ifdef _WIN64
  110. # pragma intrinsic(_InterlockedExchangeAdd64)
  111. # define tsan_add(ptr, n) (sizeof(*(ptr)) == 8 ? _InterlockedExchangeAdd64((ptr), (n)) \
  112. : _InterlockedExchangeAdd((ptr), (n)))
  113. # else
  114. # define tsan_add(ptr, n) _InterlockedExchangeAdd((ptr), (n))
  115. # endif
  116. # if !defined(_ISO_VOLATILE)
  117. # define tsan_ld_acq(ptr) (*(ptr))
  118. # define tsan_st_rel(ptr, val) (*(ptr) = (val))
  119. # endif
  120. # endif
  121. # ifndef TSAN_QUALIFIER
  122. # ifdef OPENSSL_THREADS
  123. # define TSAN_QUALIFIER volatile
  124. # define TSAN_REQUIRES_LOCKING
  125. # else /* OPENSSL_THREADS */
  126. # define TSAN_QUALIFIER
  127. # endif /* OPENSSL_THREADS */
  128. # define tsan_load(ptr) (*(ptr))
  129. # define tsan_store(ptr, val) (*(ptr) = (val))
  130. # define tsan_add(ptr, n) (*(ptr) += (n))
  131. /*
  132. * Lack of tsan_ld_acq and tsan_ld_rel means that compiler support is not
  133. * sophisticated enough to support them. Code that relies on them should be
  134. * protected with #ifdef tsan_ld_acq with locked fallback.
  135. */
  136. # endif
  137. # define tsan_counter(ptr) tsan_add((ptr), 1)
  138. # define tsan_decr(ptr) tsan_add((ptr), -1)
  139. #endif