| 6 |
| 7 | #include "base/android/library_loader/anchor_functions.h" |
| 8 | #include "base/android/library_loader/anchor_functions_buildflags.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/no_destructor.h" |
| 11 |
| 12 | namespace base { |
| 13 | namespace android { |
| 14 |
| 15 | namespace { |
| 16 | constexpr size_t kBitsPerElement = sizeof(uint32_t) * 8; |
| 17 |
| 18 | #if BUILDFLAG(SUPPORTS_CODE_ORDERING) |
| 19 | // Enough for 1 << 29 bytes of code, 512MB. |
| 20 | constexpr size_t kTextBitfieldSize = 1 << 20; |
| 21 | std::atomic<uint32_t> g_text_bitfield[kTextBitfieldSize]; |
| 22 | #endif |
| 23 | } // namespace |
| 24 |
| 25 | // static |
| 26 | ReachedAddressesBitset* ReachedAddressesBitset::GetTextBitset() { |
| 27 | #if BUILDFLAG(SUPPORTS_CODE_ORDERING) |
| 28 | static base::NoDestructor<ReachedAddressesBitset> text_bitset( |
| 29 | kStartOfText, kEndOfText, g_text_bitfield, kTextBitfieldSize); |
| 30 | return text_bitset.get(); |
| 31 | #else |
| 32 | return nullptr; |
| 33 | #endif |
| 34 | } |
| 35 |
| 36 | void ReachedAddressesBitset::RecordAddress(uintptr_t address) { |
| 37 | // |address| is outside of the range. |
| 38 | if (address < start_address_ || address >= end_address_) |
| 39 | return; |
| 40 |
| 41 | uint32_t offset = address - start_address_; |
| 42 | uint32_t offset_index = offset / kBytesGranularity; |
| 43 |
| 44 | // Atomically set the corresponding bit in the array. |
| 45 | std::atomic<uint32_t>* element = reached_ + (offset_index / kBitsPerElement); |
| 46 |
| 47 | // First, a racy check. This saves a CAS if the bit is already set, and |
| 48 | // allows the cache line to remain shared across CPUs in this case. |
| 49 | uint32_t value = element->load(std::memory_order_relaxed); |
| 50 | uint32_t mask = 1 << (offset_index % kBitsPerElement); |
| 51 | if (value & mask) |
| 52 | return; |
| 53 | element->fetch_or(mask, std::memory_order_relaxed); |
| 54 | } |
| 55 |
| 56 | std::vector<uint32_t> ReachedAddressesBitset::GetReachedOffsets() const { |
| 57 | std::vector<uint32_t> offsets; |
| 58 | const size_t elements = NumberOfReachableElements(); |
| 59 |
| 60 | for (size_t i = 0; i < elements; ++i) { |
| 61 | uint32_t element = reached_[i].load(std::memory_order_relaxed); |
| 62 | // No reached addresses at this element. |
| 63 | if (element == 0) |
| 64 | continue; |
| 65 |
| 66 | for (size_t j = 0; j < 32; ++j) { |
| 67 | if (!((element >> j) & 1)) |
| 68 | continue; |
| 69 |
| 70 | uint32_t offset_index = i * 32 + j; |
| 71 | uint32_t offset = offset_index * kBytesGranularity; |
| 72 | offsets.push_back(offset); |
| 73 | } |
| 74 | } |
| 75 |
| 76 | return offsets; |
| 77 | } |
| 78 |
| 79 | ReachedAddressesBitset::ReachedAddressesBitset( |
| 80 | uintptr_t start_address, |
| 81 | uintptr_t end_address, |
| 82 | std::atomic<uint32_t>* storage_ptr, |
| 83 | size_t storage_size) |
| 84 | : start_address_(start_address), |
| 85 | end_address_(end_address), |
| 86 | reached_(storage_ptr) { |
| 87 | DCHECK_LE(start_address_, end_address_); |
| 88 | DCHECK_LE(NumberOfReachableElements(), storage_size * kBitsPerElement); |
| 89 | } |
| 90 |
| 91 | size_t ReachedAddressesBitset::NumberOfReachableElements() const { |
| 92 | size_t reachable_bits = |
| 93 | (end_address_ + kBytesGranularity - 1) / kBytesGranularity - |
| 94 | start_address_ / kBytesGranularity; |
| 95 | return (reachable_bits + kBitsPerElement - 1) / kBitsPerElement; |
| 96 | } |
| 97 |
| 98 | } // namespace android |
| 99 | } // namespace base |
| 100 |