26#include <shared_mutex>
31#include <linux/incrementalfs.h>
42#define LIBINCFS_MAP_PTR_DEBUG false
43#if LIBINCFS_MAP_PTR_DEBUG
44#define LIBINCFS_MAP_PTR_DEBUG_CODE(x) x
46#define LIBINCFS_MAP_PTR_DEBUG_CODE(x)
49template <
typename T,
bool Verified = false>
80 template <typename T =
void>
93 bool Verify(
const uint8_t*
const& data_start,
const uint8_t*
const& data_end,
94 const uint8_t** prev_verified_block)
const;
108 std::unique_ptr<android::FileMap>
map_;
114 template <
typename,
bool>
131template <
typename T,
bool Verified>
137 template <
typename,
bool>
140 template <
typename T1>
141 using IsVoid =
typename std::enable_if_t<std::is_void<T1>::value,
int>;
143 template <
typename T1>
144 using NotVoid =
typename std::enable_if_t<!std::is_void<T1>::value,
int>;
155 friend struct map_ptr<T, Verified>;
210 template <
bool V2,
bool V1 = Verified, IsUnverified<V1> = 0, IsVerified<V2> = 0>
217 template <
bool V2,
bool V1 = Verified, IsUnverified<V1> = 0, IsVerified<V2> = 0>
221 template <
typename U,
bool V2,
typename T1 = T,
bool V1 = Verified, IsVo
id<T1> = 0,
222 NotVo
id<U> = 0, IsUnverified<V1> = 0>
239 template <
bool V2,
bool V1 = Verified, IsUnverified<V1> = 0, IsVerified<V2> = 0>
268 template <
typename U>
277 reinterpret_cast<const T*
>(
reinterpret_cast<const uint8_t*
>(
ptr_) +
287 template <
typename T1 = T, IsVo
id<T1> = 0>
288 operator bool()
const {
289 return ptr_ !=
nullptr;
295 template <
typename T1 = T, NotVo
id<T1> = 0,
bool V1 = Verified, IsUnverified<V1> = 0>
296 operator bool()
const {
300 template <
typename T1 = T, NotVo
id<T1> = 0,
bool V1 = Verified, IsVerified<V1> = 0>
301 operator bool()
const {
302 return ptr_ !=
nullptr;
305 template <
typename T1 = T, NotVo
id<T1> = 0>
310 template <
typename T1 = T, NotVo
id<T1> = 0>
317 template <
typename T1 = T, NotVo
id<T1> = 0>
325 template <
typename S,
typename T1 = T, NotVo
id<T1> = 0>
330 template <
typename S,
typename T1 = T, NotVo
id<T1> = 0>
337 template <
typename T1 = T, NotVo
id<T1> = 0>
340 CHECK(verified_) <<
"Did not verify presence before de-referencing safe pointer");
346 template <
typename T1 = T, NotVo
id<T1> = 0>
349 CHECK(verified_) <<
"Did not verify presence before de-referencing safe pointer");
356 template <
typename T1 = T, NotVo
id<T1> = 0,
bool V1 = Verified, IsUnverified<V1> = 0>
358 if (
ptr_ ==
nullptr) {
364 return ptr_ !=
nullptr;
367 const size_t verify_size =
sizeof(T) * n;
370 const auto data_start =
reinterpret_cast<const uint8_t*
>(
ptr_);
371 const auto data_end =
reinterpret_cast<const uint8_t*
>(
ptr_) + verify_size;
394 template <
typename T1 = T, NotVo
id<T1> = 0>
const char * file_name() const
uint8_t bucket_t
Definition: map_ptr.h:99
const void * unsafe_data() const
int fd_
Definition: map_ptr.h:103
size_t start_block_offset_
Definition: map_ptr.h:105
bool verification_enabled_
Definition: map_ptr.h:104
DISALLOW_COPY_AND_ASSIGN(IncFsFileMap)
static constexpr size_t kBucketBits
Definition: map_ptr.h:100
bool Verify(const uint8_t *const &data_start, const uint8_t *const &data_end, const uint8_t **prev_verified_block) const
const uint8_t * start_block_ptr_
Definition: map_ptr.h:106
map_ptr< T > data() const
Definition: map_ptr.h:81
std::unique_ptr< android::FileMap > map_
Definition: map_ptr.h:108
std::vector< std::atomic< bucket_t > > loaded_blocks_
Definition: map_ptr.h:112
bool Create(int fd, off64_t offset, size_t length, const char *file_name)
bool CreateForceVerification(int fd, off64_t offset, size_t length, const char *file_name, bool verify)
Definition: map_ptr.h:153
bool operator!=(const const_iterator &other) const
Definition: map_ptr.h:166
map_ptr< T > safe_ptr_
Definition: map_ptr.h:197
std::ptrdiff_t operator-(const const_iterator &other) const
Definition: map_ptr.h:167
void pointer
Definition: map_ptr.h:159
const map_ptr< T > value_type
Definition: map_ptr.h:157
const_iterator & operator+=(int n)
Definition: map_ptr.h:184
const const_iterator & operator++()
Definition: map_ptr.h:179
const_iterator(const map_ptr< T > &ptr)
Definition: map_ptr.h:196
bool operator==(const const_iterator &other) const
Definition: map_ptr.h:165
std::ptrdiff_t difference_type
Definition: map_ptr.h:158
const const_iterator operator++(int)
Definition: map_ptr.h:189
std::random_access_iterator_tag iterator_category
Definition: map_ptr.h:156
const_iterator operator+(int n) const
Definition: map_ptr.h:171
const_iterator(const const_iterator &it)=default
reference operator*() const
Definition: map_ptr.h:177
#define CHECK(x)
Definition: logging.h:251
#define LIKELY(exp)
Definition: macros.h:83
Definition: map_ptr.h:132
map_ptr & operator=(const map_ptr &other)=default
bool operator==(const map_ptr< T, V2 > &other) const
Definition: map_ptr.h:249
typename std::enable_if_t< V, int > IsVerified
Definition: map_ptr.h:147
typename std::enable_if_t< std::is_void< T1 >::value, int > IsVoid
Definition: map_ptr.h:141
const map_ptr< T1 > & operator++()
Definition: map_ptr.h:311
bool verify(size_t n=1) const
Definition: map_ptr.h:357
map_ptr(const map_ptr< T, V2 > &other)
Definition: map_ptr.h:211
map_ptr< U > convert() const
Definition: map_ptr.h:269
const IncFsFileMap * map_
Definition: map_ptr.h:406
bool operator<(const map_ptr< T, V2 > &other) const
Definition: map_ptr.h:259
map_ptr(const IncFsFileMap *map, const T *ptr)
Definition: map_ptr.h:400
const uint8_t * verified_block_
Definition: map_ptr.h:407
map_ptr & operator=(const T *ptr)
Definition: map_ptr.h:227
const T * unsafe_ptr() const
Definition: map_ptr.h:283
map_ptr & operator=(const map_ptr< T, V2 > &other)
Definition: map_ptr.h:240
map_ptr(const map_ptr &other)=default
const T1 & value() const
Definition: map_ptr.h:338
const T1 * operator->() const
Definition: map_ptr.h:347
const_iterator iterator() const
Definition: map_ptr.h:306
typename std::enable_if_t<!std::is_void< T1 >::value, int > NotVoid
Definition: map_ptr.h:144
map_ptr< T1 > operator+(const S n) const
Definition: map_ptr.h:326
bool operator!=(const map_ptr< T, V2 > &other) const
Definition: map_ptr.h:254
std::ptrdiff_t operator-(const map_ptr< T, V2 > &other) const
Definition: map_ptr.h:264
map_ptr< T > offset(std::ptrdiff_t offset) const
Definition: map_ptr.h:275
verified_map_ptr< T1 > verified() const
Definition: map_ptr.h:395
LIBINCFS_MAP_PTR_DEBUG_CODE(mutable bool verified_=Verified)
map_ptr< T1 > operator-(const S n) const
Definition: map_ptr.h:331
map_ptr(map_ptr &&other) noexcept=default
map_ptr(const T *ptr)
Definition: map_ptr.h:204
map_ptr(map_ptr &&other)
Definition: map_ptr.h:218
const T * ptr_
Definition: map_ptr.h:405
const map_ptr< T1 > operator++(int)
Definition: map_ptr.h:318
typename std::enable_if_t<!V, int > IsUnverified
Definition: map_ptr.h:150
map_ptr(const map_ptr< U, V2 > &other)
Definition: map_ptr.h:223
map_ptr(const IncFsFileMap *map, const T *ptr, const uint8_t *verified_block)
Definition: map_ptr.h:402