blob: 29f9a08f9a1032780963c733db723a091fc20259 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinski1ab598f2015-08-14 14:26:04 -070017#include "compile/IdAssigner.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <map>
Ryan Mitchell9634efb2021-03-19 14:53:17 -070020#include <unordered_map>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021
Ryan Mitchell9634efb2021-03-19 14:53:17 -070022#include "android-base/expected.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "android-base/logging.h"
24
Adam Lesinskicacb28f2016-10-19 12:18:14 -070025#include "ResourceTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "process/IResourceTableConsumer.h"
27#include "util/Util.h"
28
Ryan Mitchell9634efb2021-03-19 14:53:17 -070029using android::base::expected;
30using android::base::unexpected;
31
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032namespace aapt {
33
Ryan Mitchell9634efb2021-03-19 14:53:17 -070034namespace {
35template <typename T>
36using Result = expected<T, std::string>;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070037
Ryan Mitchell9634efb2021-03-19 14:53:17 -070038template <typename Id, typename Key>
39struct 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
62struct 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 Mitchell2e9bec12021-03-22 09:31:00 -070080struct 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 Mitchell9634efb2021-03-19 14:53:17 -0700101struct IdAssignerContext {
102 IdAssignerContext(std::string package_name, uint8_t package_id)
103 : package_name_(std::move(package_name)), package_id_(package_id) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700105
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700106 // 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 Mitchell2e9bec12021-03-22 09:31:00 -0700109 bool ReserveId(const ResourceName& name, ResourceId id, const Visibility& visibility,
110 IDiagnostics* diag);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700111
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 Mitchell2e9bec12021-03-22 09:31:00 -0700118 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 Mitchell9634efb2021-03-19 14:53:17 -0700122};
123
124} // namespace
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126bool IdAssigner::Consume(IAaptContext* context, ResourceTable* table) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700127 IdAssignerContext assigned_ids(context->GetCompilationPackage(), context->GetPackageId());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 for (auto& package : table->packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 for (auto& type : package->types) {
130 for (auto& entry : type->entries) {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000131 const ResourceName name(package->name, type->named_type, entry->name);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700132 if (entry->id && !assigned_ids.ReserveId(name, entry->id.value(), entry->visibility,
133 context->GetDiagnostics())) {
134 return false;
135 }
136
137 auto v = entry->visibility;
138 v.staged_api = true;
139 if (entry->staged_id && !assigned_ids.ReserveId(name, entry->staged_id.value().id, v,
140 context->GetDiagnostics())) {
141 return false;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700142 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 if (assigned_id_map_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 // Assign the pre-assigned stable ID meant for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 const auto iter = assigned_id_map_->find(name);
147 if (iter != assigned_id_map_->end()) {
148 const ResourceId assigned_id = iter->second;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700149 if (!assigned_ids.ReserveId(name, assigned_id, entry->visibility,
150 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700153 entry->id = assigned_id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700157 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 if (assigned_id_map_) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700161 // Reserve all the IDs mentioned in the stable ID map. That way we won't assig IDs that were
162 // listed in the map if they don't exist in the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 for (const auto& stable_id_entry : *assigned_id_map_) {
164 const ResourceName& pre_assigned_name = stable_id_entry.first;
165 const ResourceId& pre_assigned_id = stable_id_entry.second;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700166 if (!assigned_ids.ReserveId(pre_assigned_name, pre_assigned_id, {},
167 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 return false;
169 }
170 }
171 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700172
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700173 // Assign any resources without IDs the next available ID. Gaps will be filled if possible,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 // unless those IDs have been reserved.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 for (auto& package : table->packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 for (auto& type : package->types) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 for (auto& entry : type->entries) {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000178 const ResourceName name(package->name, type->named_type, entry->name);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700179 if (entry->id) {
180 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700182 auto id = assigned_ids.NextId(name, context->GetDiagnostics());
183 if (!id.has_value()) {
184 return false;
185 }
186 entry->id = id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700188 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 }
190 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700191}
192
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700193namespace {
194template <typename Id, typename Key>
195Result<Id> NextIdFinder<Id, Key>::ReserveId(Key key, Id id) {
196 CHECK(!next_id_called_) << "ReserveId cannot be called after NextId";
197 auto assign_result = pre_assigned_ids_.emplace(id, key);
198 if (!assign_result.second && assign_result.first->second != key) {
199 std::stringstream error;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700200 error << "ID is already assigned to " << assign_result.first->second;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700201 return unexpected(error.str());
202 }
203 return id;
204}
205
206template <typename Id, typename Key>
207std::optional<Id> NextIdFinder<Id, Key>::NextId() {
208 if (!next_id_called_) {
209 next_id_called_ = true;
210 next_preassigned_id_ = pre_assigned_ids_.begin();
211 }
212 return SkipToNextAvailableId();
213}
214
215template <typename Id, typename Key>
216std::optional<Id> NextIdFinder<Id, Key>::SkipToNextAvailableId() {
217 if (exhausted_) {
218 return {};
219 }
220 while (next_preassigned_id_ != pre_assigned_ids_.end()) {
221 if (next_preassigned_id_->first == next_id_) {
222 if (next_id_ == std::numeric_limits<Id>::max()) {
223 // The last identifier was reserved so there are no more available identifiers.
224 exhausted_ = true;
225 return {};
226 }
227 ++next_id_;
228 ++next_preassigned_id_;
229 continue;
230 }
231 CHECK(next_preassigned_id_->first > next_id_) << "Preassigned IDs are not in sorted order";
232 break;
233 }
234 if (next_id_ == std::numeric_limits<Id>::max()) {
235 // There are no more identifiers after this one, but this one is still available so return it.
236 exhausted_ = true;
237 }
238 return next_id_++;
239}
240
241Result<std::monostate> TypeGroup::ReserveId(const ResourceName& name, ResourceId id) {
242 if (type_id_ != id.type_id()) {
243 // Currently there cannot be multiple type ids for a single type.
244 std::stringstream error;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700245 error << "type '" << name.type << "' already has ID " << std::hex << (int)type_id_;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700246 return unexpected(error.str());
247 }
248
249 auto assign_result = next_entry_id_.ReserveId(name, id.entry_id());
250 if (!assign_result.has_value()) {
251 std::stringstream error;
252 error << "entry " << assign_result.error();
253 return unexpected(error.str());
254 }
255 return {};
256}
257
258Result<ResourceId> TypeGroup::NextId() {
259 auto entry_id = next_entry_id_.NextId();
260 if (!entry_id.has_value()) {
261 std::stringstream error;
262 error << "resource type ID has exceeded the maximum number of resource entries ("
263 << (std::numeric_limits<uint16_t>::max() + 1u) << ")";
264 return unexpected(error.str());
265 }
266 return ResourceId(package_id_, type_id_, entry_id.value());
267}
268
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700269bool IdAssignerContext::ReserveId(const ResourceName& name, ResourceId id,
270 const Visibility& visibility, IDiagnostics* diag) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700271 if (package_id_ != id.package_id()) {
272 diag->Error(DiagMessage() << "can't assign ID " << id << " to resource " << name
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700273 << " because package already has ID " << std::hex
274 << (int)id.package_id());
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700275 return false;
276 }
277
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000278 auto key = ResourceTypeKey{name.type.type, id.type_id()};
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700279 auto type = types_.find(key);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700280 if (type == types_.end()) {
281 // The type has not been assigned an id yet. Ensure that the specified id is not being used by
282 // another type.
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700283 auto assign_result = type_id_finder_.ReserveId(key, id.type_id());
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700284 if (!assign_result.has_value()) {
285 diag->Error(DiagMessage() << "can't assign ID " << id << " to resource " << name
286 << " because type " << assign_result.error());
287 return false;
288 }
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700289 type = types_.emplace(key, TypeGroup(package_id_, id.type_id())).first;
290 }
291
292 if (!visibility.staged_api) {
293 // Ensure that non-staged resources can only exist in one type ID.
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000294 auto non_staged_type = non_staged_type_ids_.emplace(name.type.type, id.type_id());
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700295 if (!non_staged_type.second && non_staged_type.first->second != id.type_id()) {
296 diag->Error(DiagMessage() << "can't assign ID " << id << " to resource " << name
297 << " because type already has ID " << std::hex
298 << (int)id.type_id());
299 return false;
300 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700301 }
302
303 auto assign_result = type->second.ReserveId(name, id);
304 if (!assign_result.has_value()) {
305 diag->Error(DiagMessage() << "can't assign ID " << id << " to resource " << name << " because "
306 << assign_result.error());
307 return false;
308 }
309
310 return true;
311}
312
313std::optional<ResourceId> IdAssignerContext::NextId(const ResourceName& name, IDiagnostics* diag) {
314 // The package name is not known during the compile stage.
315 // Resources without a package name are considered a part of the app being linked.
316 CHECK(name.package.empty() || name.package == package_name_);
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700317
318 // Find the type id for non-staged resources of this type.
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000319 auto non_staged_type = non_staged_type_ids_.find(name.type.type);
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700320 if (non_staged_type == non_staged_type_ids_.end()) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700321 auto next_type_id = type_id_finder_.NextId();
322 CHECK(next_type_id.has_value()) << "resource type IDs allocated have exceeded maximum (256)";
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000323 non_staged_type = non_staged_type_ids_.emplace(name.type.type, *next_type_id).first;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700324 }
325
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000326 ResourceTypeKey key{name.type.type, non_staged_type->second};
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700327 auto type = types_.find(key);
328 if (type == types_.end()) {
329 type = types_.emplace(key, TypeGroup(package_id_, key.id)).first;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700330 }
331
332 auto assign_result = type->second.NextId();
333 if (!assign_result.has_value()) {
334 diag->Error(DiagMessage() << "can't assign resource ID to resource " << name << " because "
335 << assign_result.error());
336 return {};
337 }
338 return assign_result.value();
339}
340} // namespace
341
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342} // namespace aapt