| 6 | #define BASE_ANDROID_REACHED_ADDRESSES_BITSET_H_ |
| 7 |
| 8 | #include <atomic> |
| 9 | #include <vector> |
| 10 |
| 11 | #include "base/base_export.h" |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | template <typename T> |
| 16 | class NoDestructor; |
| 17 |
| 18 | namespace android { |
| 19 |
| 20 | // ReachedAddressesBitset is a set that stores addresses for the |
| 21 | // ReachedCodeProfiler in compact form. Its main features are lock-free |
| 22 | // thread-safety and fast adding of elements. |
| 23 | // |
| 24 | // The addresses are kept with |kBytesGranularity| to save the storage space. |
| 25 | // |
| 26 | // Once insterted, elements cannot be erased from the set. |
| 27 | // |
| 28 | // All methods can be called from any thread. |
| 29 | class BASE_EXPORT ReachedAddressesBitset { |
| 30 | public: |
| 31 | // Returns an instance of ReachedAddressesBitset having enough storage space |
| 32 | // to keep all addresses from the .text section. |
| 33 | // Returns nullptr if SUPPORTS_CODE_ORDERING isn't defined. |
| 34 | // This instance is stored in the .bss section of the binary, meaning that it |
| 35 | // doesn't incur the binary size overhead and it doesn't increase the resident |
| 36 | // memory footprint when not used. |
| 37 | static ReachedAddressesBitset* GetTextBitset(); |
| 38 |
| 39 | // Inserts |address| into the bitset iff |address| lies in the range between |
| 40 | // |start_address_| and |end_address_|. |
| 41 | void RecordAddress(uintptr_t address); |
| 42 |
| 43 | // Returns a list of recorded addresses in the form of offsets from |
| 44 | // |start_address_|. |
| 45 | std::vector<uint32_t> GetReachedOffsets() const; |
| 46 |
| 47 | private: |
| 48 | friend class ReachedAddressesBitsetTest; |
| 49 | friend class NoDestructor<ReachedAddressesBitset>; |
| 50 |
| 51 | // Represents the number of bytes that are mapped into the same bit in the |
| 52 | // bitset. |
| 53 | static constexpr size_t kBytesGranularity = 4; |
| 54 |
| 55 | // Constructs a ReachedAddressesBitset on top of an external storage of |
| 56 | // |storage_size| pointed by |storage_ptr|. This external storage must outlive |
| 57 | // the constructed bitset instance. The size of storage must be large enough |
| 58 | // to fit all addresses in the range between |start_address| and |
| 59 | // |end_address|. |
| 60 | ReachedAddressesBitset(uintptr_t start_address, |
| 61 | uintptr_t end_address, |
| 62 | std::atomic<uint32_t>* storage_ptr, |
| 63 | size_t storage_size); |
| 64 |
| 65 | size_t NumberOfReachableElements() const; |
| 66 |
| 67 | uintptr_t start_address_; |
| 68 | uintptr_t end_address_; |
| 69 | std::atomic<uint32_t>* reached_; |
| 70 | }; |
| 71 |
| 72 | } // namespace android |
| 73 | } // namespace base |
| 74 |
| 75 | #endif // BASE_ANDROID_REACHED_ADDRESSES_BITSET_H_ |
| 76 |