blob: 9d9387ccce76090da4c0893efee71603ad124d93 [file] [log] [blame]
Jiyong Park3656c3c2018-08-01 20:02:01 +09001/*
Will McVickerd7d18df2019-09-12 13:40:50 -07002 * Copyright (C) 2018, The Android Open Source Project
3 *
Jiyong Park3656c3c2018-08-01 20:02:01 +09004 * 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
17#include "aidl.h"
18#include "aidl_language.h"
19#include "import_resolver.h"
Jeongik Cha047c5ee2019-08-07 23:16:49 +090020#include "logging.h"
Jiyong Park3656c3c2018-08-01 20:02:01 +090021#include "options.h"
Jiyong Park3656c3c2018-08-01 20:02:01 +090022
23#include <map>
24#include <string>
25#include <vector>
26
Jiyong Park0cf03b12020-07-22 19:36:34 +090027#include <android-base/result.h>
Jiyong Parkf7534672019-08-12 22:06:22 +090028#include <android-base/strings.h>
29
Jiyong Park3656c3c2018-08-01 20:02:01 +090030namespace android {
31namespace aidl {
32
Jiyong Park0cf03b12020-07-22 19:36:34 +090033using android::base::Error;
34using android::base::Result;
Jiyong Park3656c3c2018-08-01 20:02:01 +090035using std::map;
36using std::set;
37using std::string;
38using std::vector;
39
Jooyung Han69ea4ba2020-10-29 15:33:37 +090040static vector<string> get_strict_annotations(const AidlAnnotatable& node) {
Steven Moreland7b6a7d92020-04-20 22:00:33 -070041 // This must be symmetrical (if you can add something, you must be able to
42 // remove it). The reason is that we have no way of knowing which interface a
43 // server serves and which interface a client serves (e.g. a callback
44 // interface). Note that this is being overly lenient. It makes sense for
45 // newer code to start accepting nullable things. However, here, we don't know
46 // if the client of an interface or the server of an interface is newer.
47 //
48 // Here are two examples to demonstrate this:
49 // - a new implementation might change so that it no longer returns null
50 // values (remove @nullable)
51 // - a new implementation might start accepting null values (add @nullable)
52 static const set<AidlAnnotation::Type> kIgnoreAnnotations{
53 AidlAnnotation::Type::NULLABLE,
Jooyung Han69ea4ba2020-10-29 15:33:37 +090054 // @JavaDerive doesn't affect read/write
Jooyung Han90345002020-10-23 15:28:53 +090055 AidlAnnotation::Type::JAVA_DERIVE,
Jeongik Chad0a10272020-08-06 16:33:36 +090056 AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE,
Jooyung Han69ea4ba2020-10-29 15:33:37 +090057 // @Backing for a enum type is checked by the enum checker
58 AidlAnnotation::Type::BACKING,
59 // @RustDerive doesn't affect read/write
60 AidlAnnotation::Type::RUST_DERIVE,
Steven Moreland7b6a7d92020-04-20 22:00:33 -070061 };
Jooyung Han69ea4ba2020-10-29 15:33:37 +090062 vector<string> annotations;
Steven Moreland7b6a7d92020-04-20 22:00:33 -070063 for (const AidlAnnotation& annotation : node.GetAnnotations()) {
64 if (kIgnoreAnnotations.find(annotation.GetType()) == kIgnoreAnnotations.end()) {
Jooyung Han965e31d2020-11-27 12:30:16 +090065 annotations.push_back(annotation.ToString());
Steven Moreland7b6a7d92020-04-20 22:00:33 -070066 }
67 }
68 return annotations;
69}
70
Jiyong Park3656c3c2018-08-01 20:02:01 +090071static bool have_compatible_annotations(const AidlAnnotatable& older,
72 const AidlAnnotatable& newer) {
Jooyung Han69ea4ba2020-10-29 15:33:37 +090073 vector<string> olderAnnotations = get_strict_annotations(older);
74 vector<string> newerAnnotations = get_strict_annotations(newer);
75 sort(olderAnnotations.begin(), olderAnnotations.end());
76 sort(newerAnnotations.begin(), newerAnnotations.end());
Jeongik Cha3271ffa2018-12-04 15:19:20 +090077 if (olderAnnotations != newerAnnotations) {
Jiyong Park3656c3c2018-08-01 20:02:01 +090078 const string from = older.ToString().empty() ? "(empty)" : older.ToString();
79 const string to = newer.ToString().empty() ? "(empty)" : newer.ToString();
80 AIDL_ERROR(newer) << "Changed annotations: " << from << " to " << to;
81 return false;
82 }
83 return true;
84}
85
86static bool are_compatible_types(const AidlTypeSpecifier& older, const AidlTypeSpecifier& newer) {
87 bool compatible = true;
Jooyung Han965e31d2020-11-27 12:30:16 +090088 if (older.Signature() != newer.Signature()) {
89 AIDL_ERROR(newer) << "Type changed: " << older.Signature() << " to " << newer.Signature()
90 << ".";
Jiyong Park3656c3c2018-08-01 20:02:01 +090091 compatible = false;
92 }
93 compatible &= have_compatible_annotations(older, newer);
94 return compatible;
95}
96
97static bool are_compatible_interfaces(const AidlInterface& older, const AidlInterface& newer) {
98 bool compatible = true;
Jiyong Park3656c3c2018-08-01 20:02:01 +090099
100 map<string, AidlMethod*> new_methods;
101 for (const auto& m : newer.AsInterface()->GetMethods()) {
102 new_methods.emplace(m->Signature(), m.get());
103 }
104
105 for (const auto& old_m : older.AsInterface()->GetMethods()) {
106 const auto found = new_methods.find(old_m->Signature());
107 if (found == new_methods.end()) {
Steven Moreland4ee68632018-12-14 15:52:46 -0800108 AIDL_ERROR(old_m) << "Removed or changed method: " << older.GetCanonicalName() << "."
Jiyong Park3656c3c2018-08-01 20:02:01 +0900109 << old_m->Signature();
110 compatible = false;
111 continue;
112 }
113
114 // Compare IDs to detect method reordering. IDs are assigned by their
115 // textual order, so if there is an ID mismatch, that means reordering
116 // has happened.
117 const auto new_m = found->second;
Steven Moreland4ee68632018-12-14 15:52:46 -0800118
119 if (old_m->IsOneway() != new_m->IsOneway()) {
120 AIDL_ERROR(new_m) << "Oneway attribute " << (old_m->IsOneway() ? "removed" : "added") << ": "
121 << older.GetCanonicalName() << "." << old_m->Signature();
122 compatible = false;
123 }
124
Jiyong Park3656c3c2018-08-01 20:02:01 +0900125 if (old_m->GetId() != new_m->GetId()) {
126 AIDL_ERROR(new_m) << "Transaction ID changed: " << older.GetCanonicalName() << "."
127 << old_m->Signature() << " is changed from " << old_m->GetId() << " to "
128 << new_m->GetId() << ".";
129 compatible = false;
130 }
131
132 compatible &= are_compatible_types(old_m->GetType(), new_m->GetType());
133
134 const auto& old_args = old_m->GetArguments();
135 const auto& new_args = new_m->GetArguments();
136 // this is guaranteed because arguments are part of AidlMethod::Signature()
Steven Moreland21780812020-09-11 01:29:45 +0000137 AIDL_FATAL_IF(old_args.size() != new_args.size(), old_m);
Jiyong Park3656c3c2018-08-01 20:02:01 +0900138 for (size_t i = 0; i < old_args.size(); i++) {
139 const AidlArgument& old_a = *(old_args.at(i));
140 const AidlArgument& new_a = *(new_args.at(i));
141 compatible &= are_compatible_types(old_a.GetType(), new_a.GetType());
142
143 if (old_a.GetDirection() != new_a.GetDirection()) {
144 AIDL_ERROR(new_m) << "Direction changed: " << old_a.GetDirectionSpecifier() << " to "
145 << new_a.GetDirectionSpecifier() << ".";
146 compatible = false;
147 }
148 }
149 }
Jiyong Parka428d212018-08-29 22:26:30 +0900150
151 map<string, AidlConstantDeclaration*> new_constdecls;
152 for (const auto& c : newer.AsInterface()->GetConstantDeclarations()) {
153 new_constdecls.emplace(c->GetName(), c.get());
154 }
155
156 for (const auto& old_c : older.AsInterface()->GetConstantDeclarations()) {
157 const auto found = new_constdecls.find(old_c->GetName());
158 if (found == new_constdecls.end()) {
159 AIDL_ERROR(old_c) << "Removed constant declaration: " << older.GetCanonicalName() << "."
160 << old_c->GetName();
161 compatible = false;
162 continue;
163 }
164
165 const auto new_c = found->second;
166 compatible &= are_compatible_types(old_c->GetType(), new_c->GetType());
167
168 const string old_value = old_c->ValueString(AidlConstantValueDecorator);
169 const string new_value = new_c->ValueString(AidlConstantValueDecorator);
170 if (old_value != new_value) {
171 AIDL_ERROR(newer) << "Changed constant value: " << older.GetCanonicalName() << "."
172 << old_c->GetName() << " from " << old_value << " to " << new_value << ".";
173 compatible = false;
174 }
175 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900176 return compatible;
177}
178
Steven Moreland370ed342020-04-28 18:14:39 -0700179// returns whether the given type when defaulted will be accepted by
180// unmarshalling code
181static bool has_usable_nil_type(const AidlTypeSpecifier& specifier) {
182 // TODO(b/155238508): fix for primitives
183
184 // This technically only applies in C++, but even if both the client and the
185 // server of an interface are in Java at a particular point in time, where
186 // null is currently always acceptable, we want to make sure that versions
187 // of this service can work in native and future backends without a problem.
188 // Also, in that case, adding nullable does not hurt.
189 return specifier.IsNullable();
190}
191
Jooyung Han636fd2f2020-10-22 11:33:45 +0900192static bool HasZeroEnumerator(const AidlEnumDeclaration& enum_decl) {
193 return std::any_of(enum_decl.GetEnumerators().begin(), enum_decl.GetEnumerators().end(),
194 [&](const unique_ptr<AidlEnumerator>& enumerator) {
195 return enumerator->GetValue()->ValueString(
196 enum_decl.GetBackingType(), AidlConstantValueDecorator) == "0";
197 });
198}
199
Jooyung Han2946afc2020-10-05 20:29:16 +0900200template <typename ParcelableType>
Jooyung Han636fd2f2020-10-22 11:33:45 +0900201static bool are_compatible_parcelables(const ParcelableType& older, const AidlTypenames&,
202 const ParcelableType& newer,
203 const AidlTypenames& new_types) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900204 const auto& old_fields = older.GetFields();
205 const auto& new_fields = newer.GetFields();
206 if (old_fields.size() > new_fields.size()) {
207 // you can add new fields only at the end
208 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from "
209 << old_fields.size() << " to " << new_fields.size() << ".";
210 return false;
211 }
Devin Moorec7e47a32020-08-07 10:55:25 -0700212 if (newer.IsFixedSize() && old_fields.size() != new_fields.size()) {
213 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is changed from "
214 << old_fields.size() << " to " << new_fields.size()
215 << ". This is an incompatible change for FixedSize types.";
216 return false;
217 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900218
219 bool compatible = true;
220 for (size_t i = 0; i < old_fields.size(); i++) {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900221 const auto& old_field = old_fields.at(i);
222 const auto& new_field = new_fields.at(i);
223 compatible &= are_compatible_types(old_field->GetType(), new_field->GetType());
Jiyong Park3656c3c2018-08-01 20:02:01 +0900224
Jiyong Parka468e2a2018-08-29 21:25:18 +0900225 const string old_value = old_field->ValueString(AidlConstantValueDecorator);
226 const string new_value = new_field->ValueString(AidlConstantValueDecorator);
227 if (old_value != new_value) {
Steven Moreland370ed342020-04-28 18:14:39 -0700228 AIDL_ERROR(new_field) << "Changed default value: " << old_value << " to " << new_value << ".";
229 compatible = false;
230 }
231 }
232
Jiyong Parkb07d9932020-05-15 12:56:54 +0900233 // Reordering of fields is an incompatible change.
234 for (size_t i = 0; i < new_fields.size(); i++) {
235 const auto& new_field = new_fields.at(i);
236 auto found = std::find_if(old_fields.begin(), old_fields.end(), [&new_field](const auto& f) {
237 return new_field->GetName() == f->GetName();
238 });
239 if (found != old_fields.end()) {
240 size_t old_index = std::distance(old_fields.begin(), found);
241 if (old_index != i) {
242 AIDL_ERROR(new_field) << "Reordered " << new_field->GetName() << " from " << old_index
243 << " to " << i << ".";
244 compatible = false;
245 }
246 }
247 }
248
Steven Moreland370ed342020-04-28 18:14:39 -0700249 for (size_t i = old_fields.size(); i < new_fields.size(); i++) {
250 const auto& new_field = new_fields.at(i);
Jooyung Han636fd2f2020-10-22 11:33:45 +0900251 if (new_field->GetDefaultValue()) {
252 continue;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900253 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900254
255 // null is accepted as a valid default value
256 if (has_usable_nil_type(new_field->GetType())) {
257 continue;
258 }
259
260 // enum can't be nullable, but it's okay if it has 0 as a valid enumerator.
261 if (const auto& enum_decl = new_types.GetEnumDeclaration(new_field->GetType());
262 enum_decl != nullptr) {
263 if (HasZeroEnumerator(*enum_decl)) {
264 continue;
265 }
266
267 // TODO(b/142893595): Rephrase the message: "provide a default value or make sure ..."
268 AIDL_ERROR(new_field) << "Field '" << new_field->GetName() << "' of enum '"
269 << enum_decl->GetName()
270 << "' can't be initialized as '0'. Please make sure '"
271 << enum_decl->GetName() << "' has '0' as a valid value.";
272 compatible = false;
273 continue;
274 }
275
276 // Old API versions may suffer from the issue presented here. There is
277 // only a finite number in Android, which we must allow indefinitely.
278 struct HistoricalException {
279 std::string canonical;
280 std::string field;
281 };
282 static std::vector<HistoricalException> exceptions = {
283 {"android.net.DhcpResultsParcelable", "serverHostName"},
284 {"android.net.ResolverParamsParcel", "resolverOptions"},
285 };
286 bool excepted = false;
287 for (const HistoricalException& exception : exceptions) {
288 if (older.GetCanonicalName() == exception.canonical &&
289 new_field->GetName() == exception.field) {
290 excepted = true;
291 break;
292 }
293 }
294 if (excepted) continue;
295
296 AIDL_ERROR(new_field)
297 << "Field '" << new_field->GetName()
298 << "' does not have a useful default in some backends. Please either provide a default "
299 "value for this field or mark the field as @nullable. This value or a null value will "
300 "be used automatically when an old version of this parcelable is sent to a process "
301 "which understands a new version of this parcelable. In order to make sure your code "
302 "continues to be backwards compatible, make sure the default or null value does not "
303 "cause a semantic change to this parcelable.";
304 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900305 }
306 return compatible;
307}
308
Daniel Norman85aed542019-08-21 12:01:14 -0700309static bool are_compatible_enums(const AidlEnumDeclaration& older,
310 const AidlEnumDeclaration& newer) {
311 if (!are_compatible_types(older.GetBackingType(), newer.GetBackingType())) {
312 AIDL_ERROR(newer) << "Changed backing types.";
313 return false;
314 }
315
316 std::map<std::string, const AidlConstantValue*> old_enum_map;
317 for (const auto& enumerator : older.GetEnumerators()) {
318 old_enum_map[enumerator->GetName()] = enumerator->GetValue();
319 }
320 std::map<std::string, const AidlConstantValue*> new_enum_map;
321 for (const auto& enumerator : newer.GetEnumerators()) {
322 new_enum_map[enumerator->GetName()] = enumerator->GetValue();
323 }
324
325 bool compatible = true;
326 for (const auto& [name, value] : old_enum_map) {
327 if (new_enum_map.find(name) == new_enum_map.end()) {
328 AIDL_ERROR(newer) << "Removed enumerator from " << older.GetCanonicalName() << ": " << name;
329 compatible = false;
330 continue;
331 }
332 const string old_value =
Will McVickerd7d18df2019-09-12 13:40:50 -0700333 old_enum_map[name]->ValueString(older.GetBackingType(), AidlConstantValueDecorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700334 const string new_value =
Will McVickerd7d18df2019-09-12 13:40:50 -0700335 new_enum_map[name]->ValueString(newer.GetBackingType(), AidlConstantValueDecorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700336 if (old_value != new_value) {
337 AIDL_ERROR(newer) << "Changed enumerator value: " << older.GetCanonicalName() << "::" << name
338 << " from " << old_value << " to " << new_value << ".";
339 compatible = false;
340 }
341 }
342 return compatible;
343}
344
Jiyong Park0cf03b12020-07-22 19:36:34 +0900345static Result<AidlTypenames> load_from_dir(const Options& options, const IoDelegate& io_delegate,
346 const std::string& dir) {
347 AidlTypenames typenames;
348 for (const auto& file : io_delegate.ListFiles(dir)) {
349 if (!android::base::EndsWith(file, ".aidl")) continue;
350 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames,
351 nullptr /* imported_files */) != AidlError::OK) {
352 AIDL_ERROR(file) << "Failed to read.";
353 return Error();
354 }
355 }
356 return typenames;
357}
358
Jiyong Park3656c3c2018-08-01 20:02:01 +0900359bool check_api(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland21780812020-09-11 01:29:45 +0000360 AIDL_FATAL_IF(!options.IsStructured(), AIDL_LOCATION_HERE);
361 AIDL_FATAL_IF(options.InputFiles().size() != 2, AIDL_LOCATION_HERE)
362 << "--checkapi requires two inputs "
363 << "but got " << options.InputFiles().size();
Jiyong Park0cf03b12020-07-22 19:36:34 +0900364 auto old_tns = load_from_dir(options, io_delegate, options.InputFiles().at(0));
365 if (!old_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900366 return false;
367 }
Jiyong Park0cf03b12020-07-22 19:36:34 +0900368 auto new_tns = load_from_dir(options, io_delegate, options.InputFiles().at(1));
369 if (!new_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900370 return false;
371 }
Jiyong Parkf7534672019-08-12 22:06:22 +0900372
Jiyong Park0cf03b12020-07-22 19:36:34 +0900373 std::vector<AidlDefinedType*> old_types = old_tns->AllDefinedTypes();
374 std::vector<AidlDefinedType*> new_types = new_tns->AllDefinedTypes();
Jiyong Park3656c3c2018-08-01 20:02:01 +0900375
376 map<string, AidlDefinedType*> new_map;
377 for (const auto t : new_types) {
378 new_map.emplace(t->GetCanonicalName(), t);
379 }
380
381 bool compatible = true;
382 for (const auto old_type : old_types) {
383 const auto found = new_map.find(old_type->GetCanonicalName());
384 if (found == new_map.end()) {
385 AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName();
386 compatible = false;
387 continue;
388 }
389 const auto new_type = found->second;
390
Devin Mooredb7ac512020-08-07 11:17:36 -0700391 if (!have_compatible_annotations(*old_type, *new_type)) {
392 compatible = false;
393 }
Daniel Norman85aed542019-08-21 12:01:14 -0700394 if (old_type->AsInterface() != nullptr) {
395 if (new_type->AsInterface() == nullptr) {
396 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
397 << " is changed from " << old_type->GetPreprocessDeclarationName()
398 << " to " << new_type->GetPreprocessDeclarationName();
399 compatible = false;
400 continue;
401 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900402 compatible &=
403 are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface()));
Daniel Norman85aed542019-08-21 12:01:14 -0700404 } else if (old_type->AsStructuredParcelable() != nullptr) {
405 if (new_type->AsStructuredParcelable() == nullptr) {
406 AIDL_ERROR(new_type) << "Parcelable" << new_type->GetCanonicalName()
407 << " is not structured. ";
408 compatible = false;
409 continue;
410 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900411 compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()), *old_tns,
412 *(new_type->AsStructuredParcelable()), *new_tns);
Jooyung Han2946afc2020-10-05 20:29:16 +0900413 } else if (old_type->AsUnionDeclaration() != nullptr) {
414 if (new_type->AsUnionDeclaration() == nullptr) {
415 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
416 << " is changed from " << old_type->GetPreprocessDeclarationName()
417 << " to " << new_type->GetPreprocessDeclarationName();
418 compatible = false;
419 continue;
420 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900421 compatible &= are_compatible_parcelables(*(old_type->AsUnionDeclaration()), *old_tns,
422 *(new_type->AsUnionDeclaration()), *new_tns);
Daniel Norman85aed542019-08-21 12:01:14 -0700423 } else if (old_type->AsEnumDeclaration() != nullptr) {
424 if (new_type->AsEnumDeclaration() == nullptr) {
425 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
426 << " is changed from " << old_type->GetPreprocessDeclarationName()
427 << " to " << new_type->GetPreprocessDeclarationName();
428 compatible = false;
429 continue;
430 }
431 compatible &=
432 are_compatible_enums(*(old_type->AsEnumDeclaration()), *(new_type->AsEnumDeclaration()));
433 } else {
434 AIDL_ERROR(old_type) << "Unsupported type " << old_type->GetPreprocessDeclarationName()
435 << " for " << old_type->GetCanonicalName();
436 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900437 }
438 }
439
440 return compatible;
441}
442
443} // namespace aidl
444} // namespace android