xref: /aosp_15_r20/system/incremental_delivery/incfs/util/map_ptr.cpp (revision 9190c2a8bd3622b7aa9bd7bfe4b3aec77820f478)
1*9190c2a8SAndroid Build Coastguard Worker /*
2*9190c2a8SAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*9190c2a8SAndroid Build Coastguard Worker  *
4*9190c2a8SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*9190c2a8SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*9190c2a8SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*9190c2a8SAndroid Build Coastguard Worker  *
8*9190c2a8SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*9190c2a8SAndroid Build Coastguard Worker  *
10*9190c2a8SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*9190c2a8SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*9190c2a8SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*9190c2a8SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*9190c2a8SAndroid Build Coastguard Worker  * limitations under the License.
15*9190c2a8SAndroid Build Coastguard Worker  */
16*9190c2a8SAndroid Build Coastguard Worker 
17*9190c2a8SAndroid Build Coastguard Worker #include <android-base/file.h>
18*9190c2a8SAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
19*9190c2a8SAndroid Build Coastguard Worker #include <utils/FileMap.h>
20*9190c2a8SAndroid Build Coastguard Worker 
21*9190c2a8SAndroid Build Coastguard Worker #ifdef __ANDROID__
22*9190c2a8SAndroid Build Coastguard Worker #include "incfs_inline.h"
23*9190c2a8SAndroid Build Coastguard Worker #endif
24*9190c2a8SAndroid Build Coastguard Worker 
25*9190c2a8SAndroid Build Coastguard Worker #include "util/map_ptr.h"
26*9190c2a8SAndroid Build Coastguard Worker 
27*9190c2a8SAndroid Build Coastguard Worker namespace android::incfs {
28*9190c2a8SAndroid Build Coastguard Worker IncFsFileMap::IncFsFileMap() noexcept = default;
29*9190c2a8SAndroid Build Coastguard Worker IncFsFileMap::IncFsFileMap(IncFsFileMap&&) noexcept = default;
30*9190c2a8SAndroid Build Coastguard Worker IncFsFileMap& IncFsFileMap::operator =(IncFsFileMap&&) noexcept = default;
31*9190c2a8SAndroid Build Coastguard Worker IncFsFileMap::~IncFsFileMap() noexcept = default;
32*9190c2a8SAndroid Build Coastguard Worker 
unsafe_data() const33*9190c2a8SAndroid Build Coastguard Worker const void* IncFsFileMap::unsafe_data() const {
34*9190c2a8SAndroid Build Coastguard Worker     return map_->getDataPtr();
35*9190c2a8SAndroid Build Coastguard Worker }
36*9190c2a8SAndroid Build Coastguard Worker 
length() const37*9190c2a8SAndroid Build Coastguard Worker size_t IncFsFileMap::length() const {
38*9190c2a8SAndroid Build Coastguard Worker     return map_->getDataLength();
39*9190c2a8SAndroid Build Coastguard Worker }
40*9190c2a8SAndroid Build Coastguard Worker 
offset() const41*9190c2a8SAndroid Build Coastguard Worker off64_t IncFsFileMap::offset() const {
42*9190c2a8SAndroid Build Coastguard Worker     return map_->getDataOffset();
43*9190c2a8SAndroid Build Coastguard Worker }
44*9190c2a8SAndroid Build Coastguard Worker 
file_name() const45*9190c2a8SAndroid Build Coastguard Worker const char* IncFsFileMap::file_name() const {
46*9190c2a8SAndroid Build Coastguard Worker     return map_->getFileName();
47*9190c2a8SAndroid Build Coastguard Worker }
48*9190c2a8SAndroid Build Coastguard Worker 
Create(int fd,off64_t offset,size_t length,const char * file_name)49*9190c2a8SAndroid Build Coastguard Worker bool IncFsFileMap::Create(int fd, off64_t offset, size_t length, const char* file_name) {
50*9190c2a8SAndroid Build Coastguard Worker     return Create(fd, offset, length, file_name, true /* verify */);
51*9190c2a8SAndroid Build Coastguard Worker }
52*9190c2a8SAndroid Build Coastguard Worker 
53*9190c2a8SAndroid Build Coastguard Worker #ifdef __ANDROID__
IsVerificationEnabled(int fd)54*9190c2a8SAndroid Build Coastguard Worker static bool IsVerificationEnabled(int fd) {
55*9190c2a8SAndroid Build Coastguard Worker     return isIncFsFd(fd) && isFullyLoaded(fd) != LoadingState::Full;
56*9190c2a8SAndroid Build Coastguard Worker }
57*9190c2a8SAndroid Build Coastguard Worker 
58*9190c2a8SAndroid Build Coastguard Worker using data_block_index_t = uint32_t;
59*9190c2a8SAndroid Build Coastguard Worker 
get_block_index(const uint8_t * ptr,const uint8_t * start_block_ptr)60*9190c2a8SAndroid Build Coastguard Worker static data_block_index_t get_block_index(const uint8_t* ptr, const uint8_t* start_block_ptr) {
61*9190c2a8SAndroid Build Coastguard Worker     return (ptr - start_block_ptr) / INCFS_DATA_FILE_BLOCK_SIZE;
62*9190c2a8SAndroid Build Coastguard Worker }
63*9190c2a8SAndroid Build Coastguard Worker 
Create(int fd,off64_t offset,size_t length,const char * file_name,bool verify)64*9190c2a8SAndroid Build Coastguard Worker bool IncFsFileMap::Create(int fd, off64_t offset, size_t length, const char* file_name,
65*9190c2a8SAndroid Build Coastguard Worker                           bool verify) {
66*9190c2a8SAndroid Build Coastguard Worker     return CreateForceVerification(fd, offset, length, file_name,
67*9190c2a8SAndroid Build Coastguard Worker                                    verify && IsVerificationEnabled(fd));
68*9190c2a8SAndroid Build Coastguard Worker }
69*9190c2a8SAndroid Build Coastguard Worker 
CreateForceVerification(int fd,off64_t offset,size_t length,const char * file_name,bool verify)70*9190c2a8SAndroid Build Coastguard Worker bool IncFsFileMap::CreateForceVerification(int fd, off64_t offset, size_t length,
71*9190c2a8SAndroid Build Coastguard Worker                                            const char* file_name, bool verify) {
72*9190c2a8SAndroid Build Coastguard Worker     map_ = std::make_unique<android::FileMap>();
73*9190c2a8SAndroid Build Coastguard Worker     if (!map_->create(file_name, fd, offset, length, true /* readOnly */)) {
74*9190c2a8SAndroid Build Coastguard Worker         return false;
75*9190c2a8SAndroid Build Coastguard Worker     }
76*9190c2a8SAndroid Build Coastguard Worker 
77*9190c2a8SAndroid Build Coastguard Worker     fd_ = fd;
78*9190c2a8SAndroid Build Coastguard Worker     verification_enabled_ = verify;
79*9190c2a8SAndroid Build Coastguard Worker     if (verification_enabled_) {
80*9190c2a8SAndroid Build Coastguard Worker         // Initialize the block cache with enough buckets to hold all of the blocks within the
81*9190c2a8SAndroid Build Coastguard Worker         // memory-mapped region.
82*9190c2a8SAndroid Build Coastguard Worker         size_t offset_diff = offset % INCFS_DATA_FILE_BLOCK_SIZE;
83*9190c2a8SAndroid Build Coastguard Worker         size_t base_length_ = length + offset_diff;
84*9190c2a8SAndroid Build Coastguard Worker         start_block_offset_ = offset - offset_diff;
85*9190c2a8SAndroid Build Coastguard Worker         start_block_ptr_ = reinterpret_cast<const uint8_t*>(map_->getDataPtr()) - offset_diff;
86*9190c2a8SAndroid Build Coastguard Worker 
87*9190c2a8SAndroid Build Coastguard Worker         const size_t bucket_count = (base_length_ / INCFS_DATA_FILE_BLOCK_SIZE) / kBucketBits;
88*9190c2a8SAndroid Build Coastguard Worker         loaded_blocks_ = std::vector<std::atomic<bucket_t> >(bucket_count + 1U);
89*9190c2a8SAndroid Build Coastguard Worker     }
90*9190c2a8SAndroid Build Coastguard Worker     return true;
91*9190c2a8SAndroid Build Coastguard Worker }
92*9190c2a8SAndroid Build Coastguard Worker 
Verify(const uint8_t * const & data_start,const uint8_t * const & data_end,const uint8_t ** prev_verified_block) const93*9190c2a8SAndroid Build Coastguard Worker bool IncFsFileMap::Verify(const uint8_t* const& data_start, const uint8_t* const& data_end,
94*9190c2a8SAndroid Build Coastguard Worker                           const uint8_t** prev_verified_block) const {
95*9190c2a8SAndroid Build Coastguard Worker     const data_block_index_t start_index = get_block_index(data_start, start_block_ptr_);
96*9190c2a8SAndroid Build Coastguard Worker     const data_block_index_t end_index = get_block_index(data_end - 1U, start_block_ptr_);
97*9190c2a8SAndroid Build Coastguard Worker 
98*9190c2a8SAndroid Build Coastguard Worker     bool success = true;
99*9190c2a8SAndroid Build Coastguard Worker     // Retrieve the set of the required blocks that must be present in order to read the data
100*9190c2a8SAndroid Build Coastguard Worker     // safely.
101*9190c2a8SAndroid Build Coastguard Worker     for (data_block_index_t curr_index = start_index; curr_index <= end_index; ++curr_index) {
102*9190c2a8SAndroid Build Coastguard Worker         const size_t i = curr_index / kBucketBits;
103*9190c2a8SAndroid Build Coastguard Worker         const bucket_t present_bit = 1U << (curr_index % kBucketBits);
104*9190c2a8SAndroid Build Coastguard Worker         std::atomic<bucket_t>& bucket = loaded_blocks_[i];
105*9190c2a8SAndroid Build Coastguard Worker         if (LIKELY(bucket.load(std::memory_order_relaxed) & present_bit)) {
106*9190c2a8SAndroid Build Coastguard Worker             continue;
107*9190c2a8SAndroid Build Coastguard Worker         }
108*9190c2a8SAndroid Build Coastguard Worker 
109*9190c2a8SAndroid Build Coastguard Worker         // Touch all of the blocks with pread to ensure that the region of data is fully present.
110*9190c2a8SAndroid Build Coastguard Worker         uint8_t value;
111*9190c2a8SAndroid Build Coastguard Worker         const off64_t read_offset = (curr_index * INCFS_DATA_FILE_BLOCK_SIZE) + start_block_offset_;
112*9190c2a8SAndroid Build Coastguard Worker         if (UNLIKELY(TEMP_FAILURE_RETRY(pread64(fd_, &value, 1U, read_offset)) <= 0)) {
113*9190c2a8SAndroid Build Coastguard Worker             success = false;
114*9190c2a8SAndroid Build Coastguard Worker             break;
115*9190c2a8SAndroid Build Coastguard Worker         }
116*9190c2a8SAndroid Build Coastguard Worker 
117*9190c2a8SAndroid Build Coastguard Worker         bucket.fetch_or(present_bit, std::memory_order_relaxed);
118*9190c2a8SAndroid Build Coastguard Worker     }
119*9190c2a8SAndroid Build Coastguard Worker 
120*9190c2a8SAndroid Build Coastguard Worker     if (UNLIKELY(!success)) {
121*9190c2a8SAndroid Build Coastguard Worker         // Log the region of the file that could not be fully loaded.
122*9190c2a8SAndroid Build Coastguard Worker         size_t start_offset = (data_start - start_block_ptr_) + map_->getDataOffset();
123*9190c2a8SAndroid Build Coastguard Worker         size_t end_offset = (data_end - start_block_ptr_) + map_->getDataOffset();
124*9190c2a8SAndroid Build Coastguard Worker         std::string location = file_name() ? base::StringPrintf("path %s", file_name())
125*9190c2a8SAndroid Build Coastguard Worker                                            : base::StringPrintf("fd %d", fd_);
126*9190c2a8SAndroid Build Coastguard Worker         const std::string message =
127*9190c2a8SAndroid Build Coastguard Worker                 base::StringPrintf("region 0x%016zx - 0x%016zx of %s not fully loaded",
128*9190c2a8SAndroid Build Coastguard Worker                                    start_offset, end_offset, location.c_str());
129*9190c2a8SAndroid Build Coastguard Worker         LOG(WARNING) << message;
130*9190c2a8SAndroid Build Coastguard Worker         return false;
131*9190c2a8SAndroid Build Coastguard Worker     }
132*9190c2a8SAndroid Build Coastguard Worker 
133*9190c2a8SAndroid Build Coastguard Worker     // Update the previous verified block pointer to optimize repeated verifies on the same block.
134*9190c2a8SAndroid Build Coastguard Worker     *prev_verified_block = start_block_ptr_ + (end_index * INCFS_DATA_FILE_BLOCK_SIZE);
135*9190c2a8SAndroid Build Coastguard Worker     return true;
136*9190c2a8SAndroid Build Coastguard Worker }
137*9190c2a8SAndroid Build Coastguard Worker 
138*9190c2a8SAndroid Build Coastguard Worker #else
Create(int fd,off64_t offset,size_t length,const char * file_name,bool verify)139*9190c2a8SAndroid Build Coastguard Worker bool IncFsFileMap::Create(int fd, off64_t offset, size_t length, const char* file_name,
140*9190c2a8SAndroid Build Coastguard Worker                           bool verify) {
141*9190c2a8SAndroid Build Coastguard Worker     return CreateForceVerification(fd, offset, length, file_name, verify);
142*9190c2a8SAndroid Build Coastguard Worker }
143*9190c2a8SAndroid Build Coastguard Worker 
CreateForceVerification(int fd,off64_t offset,size_t length,const char * file_name,bool)144*9190c2a8SAndroid Build Coastguard Worker bool IncFsFileMap::CreateForceVerification(int fd, off64_t offset, size_t length,
145*9190c2a8SAndroid Build Coastguard Worker                                            const char* file_name, bool /* verify */) {
146*9190c2a8SAndroid Build Coastguard Worker     map_ = std::make_unique<android::FileMap>();
147*9190c2a8SAndroid Build Coastguard Worker     return map_->create(file_name, fd, offset, length, true /* readOnly */);
148*9190c2a8SAndroid Build Coastguard Worker }
149*9190c2a8SAndroid Build Coastguard Worker 
Verify(const uint8_t * const &,const uint8_t * const &,const uint8_t **) const150*9190c2a8SAndroid Build Coastguard Worker bool IncFsFileMap::Verify(const uint8_t* const& /* data_start */,
151*9190c2a8SAndroid Build Coastguard Worker                           const uint8_t* const& /* data_end */,
152*9190c2a8SAndroid Build Coastguard Worker                           const uint8_t** /* prev_verified_block */) const {
153*9190c2a8SAndroid Build Coastguard Worker     return true;
154*9190c2a8SAndroid Build Coastguard Worker }
155*9190c2a8SAndroid Build Coastguard Worker #endif
156*9190c2a8SAndroid Build Coastguard Worker 
157*9190c2a8SAndroid Build Coastguard Worker } // namespace android::incfs