| 6 |
| 7 | #include <limits> |
| 8 | #include <type_traits> |
| 9 |
| 10 | #include "base/threading/thread_collision_warner.h" |
| 11 |
| 12 | namespace base { |
| 13 | namespace { |
| 14 |
| 15 | #if DCHECK_IS_ON() |
| 16 | std::atomic_int g_cross_thread_ref_count_access_allow_count(0); |
| 17 | #endif |
| 18 |
| 19 | } // namespace |
| 20 |
| 21 | namespace subtle { |
| 22 |
| 23 | bool RefCountedThreadSafeBase::HasOneRef() const { |
| 24 | return ref_count_.IsOne(); |
| 25 | } |
| 26 |
| 27 | bool RefCountedThreadSafeBase::HasAtLeastOneRef() const { |
| 28 | return !ref_count_.IsZero(); |
| 29 | } |
| 30 |
| 31 | #if DCHECK_IS_ON() |
| 32 | RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { |
| 33 | DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " |
| 34 | "calling Release()"; |
| 35 | } |
| 36 | #endif |
| 37 |
| 38 | // For security and correctness, we check the arithmetic on ref counts. |
| 39 | // |
| 40 | // In an attempt to avoid binary bloat (from inlining the `CHECK`), we define |
| 41 | // these functions out-of-line. However, compilers are wily. Further testing may |
| 42 | // show that `NOINLINE` helps or hurts. |
| 43 | // |
| 44 | #if defined(ARCH_CPU_64_BITS) |
| 45 | void RefCountedBase::AddRefImpl() const { |
| 46 | // An attacker could induce use-after-free bugs, and potentially exploit them, |
| 47 | // by creating so many references to a ref-counted object that the reference |
| 48 | // count overflows. On 32-bit architectures, there is not enough address space |
| 49 | // to succeed. But on 64-bit architectures, it might indeed be possible. |
| 50 | // Therefore, we can elide the check for arithmetic overflow on 32-bit, but we |
| 51 | // must check on 64-bit. |
| 52 | // |
| 53 | // Make sure the addition didn't wrap back around to 0. This form of check |
| 54 | // works because we assert that `ref_count_` is an unsigned integer type. |
| 55 | CHECK(++ref_count_ != 0); |
| 56 | } |
| 57 |
| 58 | void RefCountedBase::ReleaseImpl() const { |
| 59 | // Make sure the subtraction didn't wrap back around from 0 to the max value. |
| 60 | // That could cause memory leaks, and may induce application-semantic |
| 61 | // correctness or safety bugs. (E.g. what if we really needed that object to |
| 62 | // be destroyed at the right time?) |
| 63 | // |
| 64 | // Note that unlike with overflow, underflow could also happen on 32-bit |
| 65 | // architectures. Arguably, we should do this check on32-bit machines too. |
| 66 | CHECK(--ref_count_ != std::numeric_limits<decltype(ref_count_)>::max()); |
| 67 | } |
| 68 | #endif |
| 69 |
| 70 | #if !defined(ARCH_CPU_X86_FAMILY) |
| 71 | bool RefCountedThreadSafeBase::Release() const { |
| 72 | return ReleaseImpl(); |
| 73 | } |
| 74 | void RefCountedThreadSafeBase::AddRef() const { |
| 75 | AddRefImpl(); |
| 76 | } |
| 77 | void RefCountedThreadSafeBase::AddRefWithCheck() const { |
| 78 | AddRefWithCheckImpl(); |
| 79 | } |
| 80 | #endif |
| 81 |
| 82 | #if DCHECK_IS_ON() |
| 83 | bool RefCountedBase::CalledOnValidSequence() const { |
| 84 | return sequence_checker_.CalledOnValidSequence() || |
| 85 | g_cross_thread_ref_count_access_allow_count.load() != 0; |
| 86 | } |
| 87 | #endif |
| 88 |
| 89 | } // namespace subtle |
| 90 |
| 91 | #if DCHECK_IS_ON() |
| 92 | ScopedAllowCrossThreadRefCountAccess::ScopedAllowCrossThreadRefCountAccess() { |
| 93 | ++g_cross_thread_ref_count_access_allow_count; |
| 94 | } |
| 95 |
| 96 | ScopedAllowCrossThreadRefCountAccess::~ScopedAllowCrossThreadRefCountAccess() { |
| 97 | --g_cross_thread_ref_count_access_allow_count; |
| 98 | } |
| 99 | #endif |
| 100 |
| 101 | } // namespace base |
| 102 |