blob: 0331313563c89e851e4a87580a79ad9b27e9147b [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 Lesinskice5e56e2016-10-21 17:56:45 -070017#include "link/ReferenceLinker.h"
18
19#include "android-base/logging.h"
20#include "androidfw/ResourceTypes.h"
21
Adam Lesinskicacb28f2016-10-19 12:18:14 -070022#include "Diagnostics.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "ResourceTable.h"
24#include "ResourceUtils.h"
25#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "ValueVisitor.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "link/Linkers.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "process/IResourceTableConsumer.h"
29#include "process/SymbolTable.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080030#include "util/Util.h"
31#include "xml/XmlUtil.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032
Adam Lesinskid5083f62017-01-16 15:07:21 -080033using android::StringPiece;
34
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035namespace aapt {
36
37namespace {
38
39/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 * The ReferenceLinkerVisitor will follow all references and make sure they
41 * point
42 * to resources that actually exist, either in the local resource table, or as
43 * external
44 * symbols. Once the target resource has been found, the ID of the resource will
45 * be assigned
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046 * to the reference object.
47 *
48 * NOTE: All of the entries in the ResourceTable must be assigned IDs.
49 */
Adam Lesinski467f1712015-11-16 17:35:44 -080050class ReferenceLinkerVisitor : public ValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 using ValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 ReferenceLinkerVisitor(IAaptContext* context, SymbolTable* symbols,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 StringPool* string_pool, xml::IPackageDeclStack* decl,
56 CallSite* callsite)
57 : context_(context),
58 symbols_(symbols),
59 package_decls_(decl),
60 string_pool_(string_pool),
61 callsite_(callsite) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 void Visit(Reference* ref) override {
64 if (!ReferenceLinker::LinkReference(ref, context_, symbols_, package_decls_,
65 callsite_)) {
66 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 }
68 }
69
70 /**
71 * We visit the Style specially because during this phase, values of
72 * attributes are
73 * all RawString values. Now that we are expected to resolve all symbols, we
74 * can
75 * lookup the attributes to find out which types are allowed for the
76 * attributes' values.
77 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 void Visit(Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 if (style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 Visit(&style->parent.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081 }
82
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 for (Style::Entry& entry : style->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 std::string err_str;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 // Transform the attribute reference so that it is using the fully
87 // qualified package
88 // name. This will also mark the reference as being able to see private
89 // resources if
90 // there was a '*' in the reference or if the package came from the
91 // private namespace.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 Reference transformed_reference = entry.key;
93 TransformReferenceFromNamespace(package_decls_,
94 context_->GetCompilationPackage(),
95 &transformed_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096
97 // Find the attribute in the symbol table and check if it is visible from
98 // this callsite.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080099 const SymbolTable::Symbol* symbol = ReferenceLinker::ResolveAttributeCheckVisibility(
100 transformed_reference, symbols_, callsite_, &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 if (symbol) {
102 // Assign our style key the correct ID.
103 // The ID may not exist.
104 entry.key.id = symbol->id;
105
106 // Try to convert the value to a more specific, typed value based on the
107 // attribute it is set to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 entry.value = ParseValueWithAttribute(std::move(entry.value),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 symbol->attribute.get());
110
111 // Link/resolve the final value (mostly if it's a reference).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 entry.value->Accept(this);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113
114 // Now verify that the type of this item is compatible with the
115 // attribute it
116 // is defined for. We pass `nullptr` as the DiagMessage so that this
117 // check is
118 // fast and we avoid creating a DiagMessage when the match is
119 // successful.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 if (!symbol->attribute->Matches(entry.value.get(), nullptr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 // The actual type of this item is incompatible with the attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 DiagMessage msg(entry.key.GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123
124 // Call the matches method again, this time with a DiagMessage so we
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800125 // fill in the actual error message.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 symbol->attribute->Matches(entry.value.get(), &msg);
127 context_->GetDiagnostics()->Error(msg);
128 error_ = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129 }
130
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 DiagMessage msg(entry.key.GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 msg << "style attribute '";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 ReferenceLinker::WriteResourceName(&msg, entry.key,
135 transformed_reference);
136 msg << "' " << err_str;
137 context_->GetDiagnostics()->Error(msg);
138 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 }
140 }
141 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 bool HasError() { return error_; }
Adam Lesinski467f1712015-11-16 17:35:44 -0800144
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 DISALLOW_COPY_AND_ASSIGN(ReferenceLinkerVisitor);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 /**
149 * Transform a RawString value into a more specific, appropriate value, based
150 * on the
151 * Attribute. If a non RawString value is passed in, this is an identity
152 * transform.
153 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 std::unique_ptr<Item> ParseValueWithAttribute(std::unique_ptr<Item> value,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 const Attribute* attr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 if (RawString* raw_string = ValueCast<RawString>(value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 std::unique_ptr<Item> transformed =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 ResourceUtils::TryParseItemForAttribute(*raw_string->value, attr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 // If we could not parse as any specific type, try a basic STRING.
161 if (!transformed &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 (attr->type_mask & android::ResTable_map::TYPE_STRING)) {
163 util::StringBuilder string_builder;
164 string_builder.Append(*raw_string->value);
165 if (string_builder) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 transformed = util::make_unique<String>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 string_pool_->MakeRef(string_builder.ToString()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 if (transformed) {
172 return transformed;
173 }
174 };
175 return value;
176 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177
178 IAaptContext* context_;
179 SymbolTable* symbols_;
180 xml::IPackageDeclStack* package_decls_;
181 StringPool* string_pool_;
182 CallSite* callsite_;
183 bool error_ = false;
184};
185
186class EmptyDeclStack : public xml::IPackageDeclStack {
187 public:
188 EmptyDeclStack() = default;
189
190 Maybe<xml::ExtractedPackage> TransformPackageAlias(
191 const StringPiece& alias,
192 const StringPiece& local_package) const override {
193 if (alias.empty()) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800194 return xml::ExtractedPackage{local_package.to_string(), true /* private */};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 }
196 return {};
197 }
198
199 private:
200 DISALLOW_COPY_AND_ASSIGN(EmptyDeclStack);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700201};
202
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203} // namespace
Adam Lesinski467f1712015-11-16 17:35:44 -0800204
205/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 * The symbol is visible if it is public, or if the reference to it is
207 * requesting private access
Adam Lesinski467f1712015-11-16 17:35:44 -0800208 * or if the callsite comes from the same package.
209 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210bool ReferenceLinker::IsSymbolVisible(const SymbolTable::Symbol& symbol,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 const Reference& ref,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 const CallSite& callsite) {
213 if (!symbol.is_public && !ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 if (ref.name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 return callsite.resource.package == ref.name.value().package;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 } else if (ref.id && symbol.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 return ref.id.value().package_id() == symbol.id.value().package_id();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 } else {
219 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800220 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 }
222 return true;
Adam Lesinski467f1712015-11-16 17:35:44 -0800223}
224
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800225const SymbolTable::Symbol* ReferenceLinker::ResolveSymbol(const Reference& reference,
226 SymbolTable* symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 if (reference.name) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800228 return symbols->FindByName(reference.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 } else if (reference.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 return symbols->FindById(reference.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 } else {
232 return nullptr;
233 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800234}
235
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800236const SymbolTable::Symbol* ReferenceLinker::ResolveSymbolCheckVisibility(const Reference& reference,
237 SymbolTable* symbols,
238 CallSite* callsite,
239 std::string* out_error) {
240 const SymbolTable::Symbol* symbol = ResolveSymbol(reference, symbols);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 if (!symbol) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 if (out_error) *out_error = "not found";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 return nullptr;
244 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800245
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 if (!IsSymbolVisible(*symbol, reference, *callsite)) {
247 if (out_error) *out_error = "is private";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 return nullptr;
249 }
250 return symbol;
Adam Lesinski467f1712015-11-16 17:35:44 -0800251}
252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253const SymbolTable::Symbol* ReferenceLinker::ResolveAttributeCheckVisibility(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800254 const Reference& reference, SymbolTable* symbols, CallSite* callsite, std::string* out_error) {
255 const SymbolTable::Symbol* symbol =
256 ResolveSymbolCheckVisibility(reference, symbols, callsite, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 if (!symbol) {
258 return nullptr;
259 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800260
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 if (!symbol->attribute) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 if (out_error) *out_error = "is not an attribute";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 return nullptr;
264 }
265 return symbol;
Adam Lesinski467f1712015-11-16 17:35:44 -0800266}
267
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800268Maybe<xml::AaptAttribute> ReferenceLinker::CompileXmlAttribute(const Reference& reference,
269 SymbolTable* symbols,
270 CallSite* callsite,
271 std::string* out_error) {
272 const SymbolTable::Symbol* symbol = ResolveSymbol(reference, symbols);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 if (!symbol) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 if (out_error) *out_error = "not found";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 return {};
276 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800277
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 if (!symbol->attribute) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 if (out_error) *out_error = "is not an attribute";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 return {};
281 }
282 return xml::AaptAttribute{symbol->id, *symbol->attribute};
Adam Lesinski467f1712015-11-16 17:35:44 -0800283}
284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285void ReferenceLinker::WriteResourceName(DiagMessage* out_msg,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 const Reference& orig,
Adam Lesinski28cacf02015-11-23 14:22:47 -0800287 const Reference& transformed) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 CHECK(out_msg != nullptr);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800289
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 if (orig.name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 *out_msg << orig.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 if (transformed.name.value() != orig.name.value()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 *out_msg << " (aka " << transformed.name.value() << ")";
Adam Lesinski28cacf02015-11-23 14:22:47 -0800294 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 *out_msg << orig.id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 }
Adam Lesinski28cacf02015-11-23 14:22:47 -0800298}
299
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300bool ReferenceLinker::LinkReference(Reference* reference, IAaptContext* context,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 SymbolTable* symbols,
302 xml::IPackageDeclStack* decls,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 CallSite* callsite) {
304 CHECK(reference != nullptr);
305 CHECK(reference->name || reference->id);
Adam Lesinski467f1712015-11-16 17:35:44 -0800306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 Reference transformed_reference = *reference;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800308 TransformReferenceFromNamespace(decls, context->GetCompilationPackage(), &transformed_reference);
Adam Lesinski467f1712015-11-16 17:35:44 -0800309
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 std::string err_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800311 const SymbolTable::Symbol* s =
312 ResolveSymbolCheckVisibility(transformed_reference, symbols, callsite, &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 if (s) {
314 // The ID may not exist. This is fine because of the possibility of building
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 // against libraries without assigned IDs.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 // Ex: Linking against own resources when building a static library.
317 reference->id = s->id;
318 return true;
319 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800320
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 DiagMessage error_msg(reference->GetSource());
322 error_msg << "resource ";
323 WriteResourceName(&error_msg, *reference, transformed_reference);
324 error_msg << " " << err_str;
325 context->GetDiagnostics()->Error(error_msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800327}
328
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329bool ReferenceLinker::Consume(IAaptContext* context, ResourceTable* table) {
330 EmptyDeclStack decl_stack;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331 bool error = false;
332 for (auto& package : table->packages) {
333 for (auto& type : package->types) {
334 for (auto& entry : type->entries) {
335 // Symbol state information may be lost if there is no value for the
336 // resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 if (entry->symbol_status.state != SymbolState::kUndefined &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 entry->values.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 context->GetDiagnostics()->Error(
340 DiagMessage(entry->symbol_status.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 << "no definition for declared symbol '"
342 << ResourceNameRef(package->name, type->type, entry->name)
343 << "'");
344 error = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700345 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700346
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347 CallSite callsite = {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 ResourceNameRef(package->name, type->type, entry->name)};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 ReferenceLinkerVisitor visitor(context, context->GetExternalSymbols(),
350 &table->string_pool, &decl_stack,
351 &callsite);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 for (auto& config_value : entry->values) {
354 config_value->value->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 }
356
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 if (visitor.HasError()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 error = true;
359 }
360 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700361 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 }
363 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700364}
365
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366} // namespace aapt