Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 17 | #include "compile/IdAssigner.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <map> |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 20 | #include <unordered_map> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 22 | #include "android-base/expected.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | #include "android-base/logging.h" |
| 24 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 25 | #include "ResourceTable.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "process/IResourceTableConsumer.h" |
| 27 | #include "util/Util.h" |
| 28 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 29 | using android::base::expected; |
| 30 | using android::base::unexpected; |
| 31 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 34 | namespace { |
| 35 | template <typename T> |
| 36 | using Result = expected<T, std::string>; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 37 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 38 | template <typename Id, typename Key> |
| 39 | struct NextIdFinder { |
| 40 | explicit NextIdFinder(Id start_id = 0u) : next_id_(start_id){}; |
| 41 | |
| 42 | // Attempts to reserve an identifier for the specified key. |
| 43 | // If the identifier is already reserved by a different key, an error message is returned. |
| 44 | // Reserving identifiers must be completed before `NextId` is called for the first time. |
| 45 | Result<Id> ReserveId(Key key, Id id); |
| 46 | |
| 47 | // Retrieves the next available identifier that has not been reserved. |
| 48 | std::optional<Id> NextId(); |
| 49 | |
| 50 | private: |
| 51 | // Attempts to set `next_id_` to the next available identifier that has not been reserved. |
| 52 | // Returns whether there were any available identifiers. |
| 53 | std::optional<Id> SkipToNextAvailableId(); |
| 54 | |
| 55 | Id next_id_; |
| 56 | bool next_id_called_ = false; |
| 57 | bool exhausted_ = false; |
| 58 | std::map<Id, Key> pre_assigned_ids_; |
| 59 | typename std::map<Id, Key>::iterator next_preassigned_id_; |
| 60 | }; |
| 61 | |
| 62 | struct TypeGroup { |
| 63 | explicit TypeGroup(uint8_t package_id, uint8_t type_id) |
| 64 | : package_id_(package_id), type_id_(type_id){}; |
| 65 | |
| 66 | // Attempts to reserve the resource id for the specified resource name. |
| 67 | // If the id is already reserved by a different name, an error message is returned. |
| 68 | // Reserving identifiers must be completed before `NextId` is called for the first time. |
| 69 | Result<std::monostate> ReserveId(const ResourceName& name, ResourceId id); |
| 70 | |
| 71 | // Retrieves the next available resource id that has not been reserved. |
| 72 | Result<ResourceId> NextId(); |
| 73 | |
| 74 | private: |
| 75 | uint8_t package_id_; |
| 76 | uint8_t type_id_; |
| 77 | NextIdFinder<uint16_t, ResourceName> next_entry_id_; |
| 78 | }; |
| 79 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 80 | struct ResourceTypeKey { |
| 81 | ResourceType type; |
| 82 | uint8_t id; |
| 83 | |
| 84 | bool operator<(const ResourceTypeKey& other) const { |
| 85 | return (type != other.type) ? type < other.type : id < other.id; |
| 86 | } |
| 87 | |
| 88 | bool operator==(const ResourceTypeKey& other) const { |
| 89 | return type == other.type && id == other.id; |
| 90 | } |
| 91 | |
| 92 | bool operator!=(const ResourceTypeKey& other) const { |
| 93 | return !(*this == other); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | ::std::ostream& operator<<(::std::ostream& out, const ResourceTypeKey& type) { |
| 98 | return out << type.type; |
| 99 | } |
| 100 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 101 | struct IdAssignerContext { |
| 102 | IdAssignerContext(std::string package_name, uint8_t package_id) |
| 103 | : package_name_(std::move(package_name)), package_id_(package_id) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 105 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 106 | // Attempts to reserve the resource id for the specified resource name. |
| 107 | // Returns whether the id was reserved successfully. |
| 108 | // Reserving identifiers must be completed before `NextId` is called for the first time. |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 109 | bool ReserveId(const ResourceName& name, ResourceId id, const Visibility& visibility, |
| 110 | IDiagnostics* diag); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 111 | |
| 112 | // Retrieves the next available resource id that has not been reserved. |
| 113 | std::optional<ResourceId> NextId(const ResourceName& name, IDiagnostics* diag); |
| 114 | |
| 115 | private: |
| 116 | std::string package_name_; |
| 117 | uint8_t package_id_; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 118 | std::map<ResourceTypeKey, TypeGroup> types_; |
| 119 | std::map<ResourceType, uint8_t> non_staged_type_ids_; |
| 120 | NextIdFinder<uint8_t, ResourceTypeKey> type_id_finder_ = |
| 121 | NextIdFinder<uint8_t, ResourceTypeKey>(1); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 125 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 126 | bool IdAssigner::Consume(IAaptContext* context, ResourceTable* table) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 127 | IdAssignerContext assigned_ids(context->GetCompilationPackage(), context->GetPackageId()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 128 | for (auto& package : table->packages) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | for (auto& type : package->types) { |
| 130 | for (auto& entry : type->entries) { |
| 131 | const ResourceName name(package->name, type->type, entry->name); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 132 | if (entry->id) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 133 | if (!assigned_ids.ReserveId(name, entry->id.value(), entry->visibility, |
| 134 | context->GetDiagnostics())) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 138 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 139 | if (assigned_id_map_) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | // Assign the pre-assigned stable ID meant for this resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | const auto iter = assigned_id_map_->find(name); |
| 142 | if (iter != assigned_id_map_->end()) { |
| 143 | const ResourceId assigned_id = iter->second; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 144 | if (!assigned_ids.ReserveId(name, assigned_id, entry->visibility, |
| 145 | context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 146 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 147 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 148 | entry->id = assigned_id; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 150 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 151 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 152 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 154 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | if (assigned_id_map_) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 156 | // Reserve all the IDs mentioned in the stable ID map. That way we won't assig IDs that were |
| 157 | // listed in the map if they don't exist in the table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | for (const auto& stable_id_entry : *assigned_id_map_) { |
| 159 | const ResourceName& pre_assigned_name = stable_id_entry.first; |
| 160 | const ResourceId& pre_assigned_id = stable_id_entry.second; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 161 | if (!assigned_ids.ReserveId(pre_assigned_name, pre_assigned_id, {}, |
| 162 | context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | } |
| 166 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 167 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 168 | // Assign any resources without IDs the next available ID. Gaps will be filled if possible, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 169 | // unless those IDs have been reserved. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 170 | for (auto& package : table->packages) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | for (auto& type : package->types) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 172 | for (auto& entry : type->entries) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 173 | const ResourceName name(package->name, type->type, entry->name); |
| 174 | if (entry->id) { |
| 175 | continue; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 177 | auto id = assigned_ids.NextId(name, context->GetDiagnostics()); |
| 178 | if (!id.has_value()) { |
| 179 | return false; |
| 180 | } |
| 181 | entry->id = id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 183 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | } |
| 185 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 188 | namespace { |
| 189 | template <typename Id, typename Key> |
| 190 | Result<Id> NextIdFinder<Id, Key>::ReserveId(Key key, Id id) { |
| 191 | CHECK(!next_id_called_) << "ReserveId cannot be called after NextId"; |
| 192 | auto assign_result = pre_assigned_ids_.emplace(id, key); |
| 193 | if (!assign_result.second && assign_result.first->second != key) { |
| 194 | std::stringstream error; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 195 | error << "ID is already assigned to " << assign_result.first->second; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 196 | return unexpected(error.str()); |
| 197 | } |
| 198 | return id; |
| 199 | } |
| 200 | |
| 201 | template <typename Id, typename Key> |
| 202 | std::optional<Id> NextIdFinder<Id, Key>::NextId() { |
| 203 | if (!next_id_called_) { |
| 204 | next_id_called_ = true; |
| 205 | next_preassigned_id_ = pre_assigned_ids_.begin(); |
| 206 | } |
| 207 | return SkipToNextAvailableId(); |
| 208 | } |
| 209 | |
| 210 | template <typename Id, typename Key> |
| 211 | std::optional<Id> NextIdFinder<Id, Key>::SkipToNextAvailableId() { |
| 212 | if (exhausted_) { |
| 213 | return {}; |
| 214 | } |
| 215 | while (next_preassigned_id_ != pre_assigned_ids_.end()) { |
| 216 | if (next_preassigned_id_->first == next_id_) { |
| 217 | if (next_id_ == std::numeric_limits<Id>::max()) { |
| 218 | // The last identifier was reserved so there are no more available identifiers. |
| 219 | exhausted_ = true; |
| 220 | return {}; |
| 221 | } |
| 222 | ++next_id_; |
| 223 | ++next_preassigned_id_; |
| 224 | continue; |
| 225 | } |
| 226 | CHECK(next_preassigned_id_->first > next_id_) << "Preassigned IDs are not in sorted order"; |
| 227 | break; |
| 228 | } |
| 229 | if (next_id_ == std::numeric_limits<Id>::max()) { |
| 230 | // There are no more identifiers after this one, but this one is still available so return it. |
| 231 | exhausted_ = true; |
| 232 | } |
| 233 | return next_id_++; |
| 234 | } |
| 235 | |
| 236 | Result<std::monostate> TypeGroup::ReserveId(const ResourceName& name, ResourceId id) { |
| 237 | if (type_id_ != id.type_id()) { |
| 238 | // Currently there cannot be multiple type ids for a single type. |
| 239 | std::stringstream error; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 240 | error << "type '" << name.type << "' already has ID " << std::hex << (int)id.type_id(); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 241 | return unexpected(error.str()); |
| 242 | } |
| 243 | |
| 244 | auto assign_result = next_entry_id_.ReserveId(name, id.entry_id()); |
| 245 | if (!assign_result.has_value()) { |
| 246 | std::stringstream error; |
| 247 | error << "entry " << assign_result.error(); |
| 248 | return unexpected(error.str()); |
| 249 | } |
| 250 | return {}; |
| 251 | } |
| 252 | |
| 253 | Result<ResourceId> TypeGroup::NextId() { |
| 254 | auto entry_id = next_entry_id_.NextId(); |
| 255 | if (!entry_id.has_value()) { |
| 256 | std::stringstream error; |
| 257 | error << "resource type ID has exceeded the maximum number of resource entries (" |
| 258 | << (std::numeric_limits<uint16_t>::max() + 1u) << ")"; |
| 259 | return unexpected(error.str()); |
| 260 | } |
| 261 | return ResourceId(package_id_, type_id_, entry_id.value()); |
| 262 | } |
| 263 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 264 | bool IdAssignerContext::ReserveId(const ResourceName& name, ResourceId id, |
| 265 | const Visibility& visibility, IDiagnostics* diag) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 266 | if (package_id_ != id.package_id()) { |
| 267 | diag->Error(DiagMessage() << "can't assign ID " << id << " to resource " << name |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 268 | << " because package already has ID " << std::hex |
| 269 | << (int)id.package_id()); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 270 | return false; |
| 271 | } |
| 272 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 273 | auto key = ResourceTypeKey{name.type, id.type_id()}; |
| 274 | auto type = types_.find(key); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 275 | if (type == types_.end()) { |
| 276 | // The type has not been assigned an id yet. Ensure that the specified id is not being used by |
| 277 | // another type. |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 278 | auto assign_result = type_id_finder_.ReserveId(key, id.type_id()); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 279 | if (!assign_result.has_value()) { |
| 280 | diag->Error(DiagMessage() << "can't assign ID " << id << " to resource " << name |
| 281 | << " because type " << assign_result.error()); |
| 282 | return false; |
| 283 | } |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 284 | type = types_.emplace(key, TypeGroup(package_id_, id.type_id())).first; |
| 285 | } |
| 286 | |
| 287 | if (!visibility.staged_api) { |
| 288 | // Ensure that non-staged resources can only exist in one type ID. |
| 289 | auto non_staged_type = non_staged_type_ids_.emplace(name.type, id.type_id()); |
| 290 | if (!non_staged_type.second && non_staged_type.first->second != id.type_id()) { |
| 291 | diag->Error(DiagMessage() << "can't assign ID " << id << " to resource " << name |
| 292 | << " because type already has ID " << std::hex |
| 293 | << (int)id.type_id()); |
| 294 | return false; |
| 295 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | auto assign_result = type->second.ReserveId(name, id); |
| 299 | if (!assign_result.has_value()) { |
| 300 | diag->Error(DiagMessage() << "can't assign ID " << id << " to resource " << name << " because " |
| 301 | << assign_result.error()); |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | std::optional<ResourceId> IdAssignerContext::NextId(const ResourceName& name, IDiagnostics* diag) { |
| 309 | // The package name is not known during the compile stage. |
| 310 | // Resources without a package name are considered a part of the app being linked. |
| 311 | CHECK(name.package.empty() || name.package == package_name_); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 312 | |
| 313 | // Find the type id for non-staged resources of this type. |
| 314 | auto non_staged_type = non_staged_type_ids_.find(name.type); |
| 315 | if (non_staged_type == non_staged_type_ids_.end()) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 316 | auto next_type_id = type_id_finder_.NextId(); |
| 317 | CHECK(next_type_id.has_value()) << "resource type IDs allocated have exceeded maximum (256)"; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 318 | non_staged_type = non_staged_type_ids_.emplace(name.type, *next_type_id).first; |
| 319 | } |
| 320 | |
| 321 | ResourceTypeKey key{name.type, non_staged_type->second}; |
| 322 | auto type = types_.find(key); |
| 323 | if (type == types_.end()) { |
| 324 | type = types_.emplace(key, TypeGroup(package_id_, key.id)).first; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | auto assign_result = type->second.NextId(); |
| 328 | if (!assign_result.has_value()) { |
| 329 | diag->Error(DiagMessage() << "can't assign resource ID to resource " << name << " because " |
| 330 | << assign_result.error()); |
| 331 | return {}; |
| 332 | } |
| 333 | return assign_result.value(); |
| 334 | } |
| 335 | } // namespace |
| 336 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 337 | } // namespace aapt |