blob: 3c9654a0548cc58cf2cd730fae88518bbd09f742 [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 Han69ea4ba2020-10-29 15:33:37 +090065 annotations.push_back(annotation.ToString(AidlConstantValueDecorator));
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;
88 if (older.ToString() != newer.ToString()) {
89 AIDL_ERROR(newer) << "Type changed: " << older.ToString() << " to " << newer.ToString() << ".";
90 compatible = false;
91 }
92 compatible &= have_compatible_annotations(older, newer);
93 return compatible;
94}
95
96static bool are_compatible_interfaces(const AidlInterface& older, const AidlInterface& newer) {
97 bool compatible = true;
Jiyong Park3656c3c2018-08-01 20:02:01 +090098
99 map<string, AidlMethod*> new_methods;
100 for (const auto& m : newer.AsInterface()->GetMethods()) {
101 new_methods.emplace(m->Signature(), m.get());
102 }
103
104 for (const auto& old_m : older.AsInterface()->GetMethods()) {
105 const auto found = new_methods.find(old_m->Signature());
106 if (found == new_methods.end()) {
Steven Moreland4ee68632018-12-14 15:52:46 -0800107 AIDL_ERROR(old_m) << "Removed or changed method: " << older.GetCanonicalName() << "."
Jiyong Park3656c3c2018-08-01 20:02:01 +0900108 << old_m->Signature();
109 compatible = false;
110 continue;
111 }
112
113 // Compare IDs to detect method reordering. IDs are assigned by their
114 // textual order, so if there is an ID mismatch, that means reordering
115 // has happened.
116 const auto new_m = found->second;
Steven Moreland4ee68632018-12-14 15:52:46 -0800117
118 if (old_m->IsOneway() != new_m->IsOneway()) {
119 AIDL_ERROR(new_m) << "Oneway attribute " << (old_m->IsOneway() ? "removed" : "added") << ": "
120 << older.GetCanonicalName() << "." << old_m->Signature();
121 compatible = false;
122 }
123
Jiyong Park3656c3c2018-08-01 20:02:01 +0900124 if (old_m->GetId() != new_m->GetId()) {
125 AIDL_ERROR(new_m) << "Transaction ID changed: " << older.GetCanonicalName() << "."
126 << old_m->Signature() << " is changed from " << old_m->GetId() << " to "
127 << new_m->GetId() << ".";
128 compatible = false;
129 }
130
131 compatible &= are_compatible_types(old_m->GetType(), new_m->GetType());
132
133 const auto& old_args = old_m->GetArguments();
134 const auto& new_args = new_m->GetArguments();
135 // this is guaranteed because arguments are part of AidlMethod::Signature()
Steven Moreland21780812020-09-11 01:29:45 +0000136 AIDL_FATAL_IF(old_args.size() != new_args.size(), old_m);
Jiyong Park3656c3c2018-08-01 20:02:01 +0900137 for (size_t i = 0; i < old_args.size(); i++) {
138 const AidlArgument& old_a = *(old_args.at(i));
139 const AidlArgument& new_a = *(new_args.at(i));
140 compatible &= are_compatible_types(old_a.GetType(), new_a.GetType());
141
142 if (old_a.GetDirection() != new_a.GetDirection()) {
143 AIDL_ERROR(new_m) << "Direction changed: " << old_a.GetDirectionSpecifier() << " to "
144 << new_a.GetDirectionSpecifier() << ".";
145 compatible = false;
146 }
147 }
148 }
Jiyong Parka428d212018-08-29 22:26:30 +0900149
150 map<string, AidlConstantDeclaration*> new_constdecls;
151 for (const auto& c : newer.AsInterface()->GetConstantDeclarations()) {
152 new_constdecls.emplace(c->GetName(), c.get());
153 }
154
155 for (const auto& old_c : older.AsInterface()->GetConstantDeclarations()) {
156 const auto found = new_constdecls.find(old_c->GetName());
157 if (found == new_constdecls.end()) {
158 AIDL_ERROR(old_c) << "Removed constant declaration: " << older.GetCanonicalName() << "."
159 << old_c->GetName();
160 compatible = false;
161 continue;
162 }
163
164 const auto new_c = found->second;
165 compatible &= are_compatible_types(old_c->GetType(), new_c->GetType());
166
167 const string old_value = old_c->ValueString(AidlConstantValueDecorator);
168 const string new_value = new_c->ValueString(AidlConstantValueDecorator);
169 if (old_value != new_value) {
170 AIDL_ERROR(newer) << "Changed constant value: " << older.GetCanonicalName() << "."
171 << old_c->GetName() << " from " << old_value << " to " << new_value << ".";
172 compatible = false;
173 }
174 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900175 return compatible;
176}
177
Steven Moreland370ed342020-04-28 18:14:39 -0700178// returns whether the given type when defaulted will be accepted by
179// unmarshalling code
180static bool has_usable_nil_type(const AidlTypeSpecifier& specifier) {
181 // TODO(b/155238508): fix for primitives
182
183 // This technically only applies in C++, but even if both the client and the
184 // server of an interface are in Java at a particular point in time, where
185 // null is currently always acceptable, we want to make sure that versions
186 // of this service can work in native and future backends without a problem.
187 // Also, in that case, adding nullable does not hurt.
188 return specifier.IsNullable();
189}
190
Jooyung Han636fd2f2020-10-22 11:33:45 +0900191static bool HasZeroEnumerator(const AidlEnumDeclaration& enum_decl) {
192 return std::any_of(enum_decl.GetEnumerators().begin(), enum_decl.GetEnumerators().end(),
193 [&](const unique_ptr<AidlEnumerator>& enumerator) {
194 return enumerator->GetValue()->ValueString(
195 enum_decl.GetBackingType(), AidlConstantValueDecorator) == "0";
196 });
197}
198
Jooyung Han2946afc2020-10-05 20:29:16 +0900199template <typename ParcelableType>
Jooyung Han636fd2f2020-10-22 11:33:45 +0900200static bool are_compatible_parcelables(const ParcelableType& older, const AidlTypenames&,
201 const ParcelableType& newer,
202 const AidlTypenames& new_types) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900203 const auto& old_fields = older.GetFields();
204 const auto& new_fields = newer.GetFields();
205 if (old_fields.size() > new_fields.size()) {
206 // you can add new fields only at the end
207 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from "
208 << old_fields.size() << " to " << new_fields.size() << ".";
209 return false;
210 }
Devin Moorec7e47a32020-08-07 10:55:25 -0700211 if (newer.IsFixedSize() && old_fields.size() != new_fields.size()) {
212 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is changed from "
213 << old_fields.size() << " to " << new_fields.size()
214 << ". This is an incompatible change for FixedSize types.";
215 return false;
216 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900217
218 bool compatible = true;
219 for (size_t i = 0; i < old_fields.size(); i++) {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900220 const auto& old_field = old_fields.at(i);
221 const auto& new_field = new_fields.at(i);
222 compatible &= are_compatible_types(old_field->GetType(), new_field->GetType());
Jiyong Park3656c3c2018-08-01 20:02:01 +0900223
Jiyong Parka468e2a2018-08-29 21:25:18 +0900224 const string old_value = old_field->ValueString(AidlConstantValueDecorator);
225 const string new_value = new_field->ValueString(AidlConstantValueDecorator);
226 if (old_value != new_value) {
Steven Moreland370ed342020-04-28 18:14:39 -0700227 AIDL_ERROR(new_field) << "Changed default value: " << old_value << " to " << new_value << ".";
228 compatible = false;
229 }
230 }
231
Jiyong Parkb07d9932020-05-15 12:56:54 +0900232 // Reordering of fields is an incompatible change.
233 for (size_t i = 0; i < new_fields.size(); i++) {
234 const auto& new_field = new_fields.at(i);
235 auto found = std::find_if(old_fields.begin(), old_fields.end(), [&new_field](const auto& f) {
236 return new_field->GetName() == f->GetName();
237 });
238 if (found != old_fields.end()) {
239 size_t old_index = std::distance(old_fields.begin(), found);
240 if (old_index != i) {
241 AIDL_ERROR(new_field) << "Reordered " << new_field->GetName() << " from " << old_index
242 << " to " << i << ".";
243 compatible = false;
244 }
245 }
246 }
247
Steven Moreland370ed342020-04-28 18:14:39 -0700248 for (size_t i = old_fields.size(); i < new_fields.size(); i++) {
249 const auto& new_field = new_fields.at(i);
Jooyung Han636fd2f2020-10-22 11:33:45 +0900250 if (new_field->GetDefaultValue()) {
251 continue;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900252 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900253
254 // null is accepted as a valid default value
255 if (has_usable_nil_type(new_field->GetType())) {
256 continue;
257 }
258
259 // enum can't be nullable, but it's okay if it has 0 as a valid enumerator.
260 if (const auto& enum_decl = new_types.GetEnumDeclaration(new_field->GetType());
261 enum_decl != nullptr) {
262 if (HasZeroEnumerator(*enum_decl)) {
263 continue;
264 }
265
266 // TODO(b/142893595): Rephrase the message: "provide a default value or make sure ..."
267 AIDL_ERROR(new_field) << "Field '" << new_field->GetName() << "' of enum '"
268 << enum_decl->GetName()
269 << "' can't be initialized as '0'. Please make sure '"
270 << enum_decl->GetName() << "' has '0' as a valid value.";
271 compatible = false;
272 continue;
273 }
274
275 // Old API versions may suffer from the issue presented here. There is
276 // only a finite number in Android, which we must allow indefinitely.
277 struct HistoricalException {
278 std::string canonical;
279 std::string field;
280 };
281 static std::vector<HistoricalException> exceptions = {
282 {"android.net.DhcpResultsParcelable", "serverHostName"},
283 {"android.net.ResolverParamsParcel", "resolverOptions"},
284 };
285 bool excepted = false;
286 for (const HistoricalException& exception : exceptions) {
287 if (older.GetCanonicalName() == exception.canonical &&
288 new_field->GetName() == exception.field) {
289 excepted = true;
290 break;
291 }
292 }
293 if (excepted) continue;
294
295 AIDL_ERROR(new_field)
296 << "Field '" << new_field->GetName()
297 << "' does not have a useful default in some backends. Please either provide a default "
298 "value for this field or mark the field as @nullable. This value or a null value will "
299 "be used automatically when an old version of this parcelable is sent to a process "
300 "which understands a new version of this parcelable. In order to make sure your code "
301 "continues to be backwards compatible, make sure the default or null value does not "
302 "cause a semantic change to this parcelable.";
303 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900304 }
305 return compatible;
306}
307
Daniel Norman85aed542019-08-21 12:01:14 -0700308static bool are_compatible_enums(const AidlEnumDeclaration& older,
309 const AidlEnumDeclaration& newer) {
310 if (!are_compatible_types(older.GetBackingType(), newer.GetBackingType())) {
311 AIDL_ERROR(newer) << "Changed backing types.";
312 return false;
313 }
314
315 std::map<std::string, const AidlConstantValue*> old_enum_map;
316 for (const auto& enumerator : older.GetEnumerators()) {
317 old_enum_map[enumerator->GetName()] = enumerator->GetValue();
318 }
319 std::map<std::string, const AidlConstantValue*> new_enum_map;
320 for (const auto& enumerator : newer.GetEnumerators()) {
321 new_enum_map[enumerator->GetName()] = enumerator->GetValue();
322 }
323
324 bool compatible = true;
325 for (const auto& [name, value] : old_enum_map) {
326 if (new_enum_map.find(name) == new_enum_map.end()) {
327 AIDL_ERROR(newer) << "Removed enumerator from " << older.GetCanonicalName() << ": " << name;
328 compatible = false;
329 continue;
330 }
331 const string old_value =
Will McVickerd7d18df2019-09-12 13:40:50 -0700332 old_enum_map[name]->ValueString(older.GetBackingType(), AidlConstantValueDecorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700333 const string new_value =
Will McVickerd7d18df2019-09-12 13:40:50 -0700334 new_enum_map[name]->ValueString(newer.GetBackingType(), AidlConstantValueDecorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700335 if (old_value != new_value) {
336 AIDL_ERROR(newer) << "Changed enumerator value: " << older.GetCanonicalName() << "::" << name
337 << " from " << old_value << " to " << new_value << ".";
338 compatible = false;
339 }
340 }
341 return compatible;
342}
343
Jiyong Park0cf03b12020-07-22 19:36:34 +0900344static Result<AidlTypenames> load_from_dir(const Options& options, const IoDelegate& io_delegate,
345 const std::string& dir) {
346 AidlTypenames typenames;
347 for (const auto& file : io_delegate.ListFiles(dir)) {
348 if (!android::base::EndsWith(file, ".aidl")) continue;
349 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames,
350 nullptr /* imported_files */) != AidlError::OK) {
351 AIDL_ERROR(file) << "Failed to read.";
352 return Error();
353 }
354 }
355 return typenames;
356}
357
Jiyong Park3656c3c2018-08-01 20:02:01 +0900358bool check_api(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland21780812020-09-11 01:29:45 +0000359 AIDL_FATAL_IF(!options.IsStructured(), AIDL_LOCATION_HERE);
360 AIDL_FATAL_IF(options.InputFiles().size() != 2, AIDL_LOCATION_HERE)
361 << "--checkapi requires two inputs "
362 << "but got " << options.InputFiles().size();
Jiyong Park0cf03b12020-07-22 19:36:34 +0900363 auto old_tns = load_from_dir(options, io_delegate, options.InputFiles().at(0));
364 if (!old_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900365 return false;
366 }
Jiyong Park0cf03b12020-07-22 19:36:34 +0900367 auto new_tns = load_from_dir(options, io_delegate, options.InputFiles().at(1));
368 if (!new_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900369 return false;
370 }
Jiyong Parkf7534672019-08-12 22:06:22 +0900371
Jiyong Park0cf03b12020-07-22 19:36:34 +0900372 std::vector<AidlDefinedType*> old_types = old_tns->AllDefinedTypes();
373 std::vector<AidlDefinedType*> new_types = new_tns->AllDefinedTypes();
Jiyong Park3656c3c2018-08-01 20:02:01 +0900374
375 map<string, AidlDefinedType*> new_map;
376 for (const auto t : new_types) {
377 new_map.emplace(t->GetCanonicalName(), t);
378 }
379
380 bool compatible = true;
381 for (const auto old_type : old_types) {
382 const auto found = new_map.find(old_type->GetCanonicalName());
383 if (found == new_map.end()) {
384 AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName();
385 compatible = false;
386 continue;
387 }
388 const auto new_type = found->second;
389
Devin Mooredb7ac512020-08-07 11:17:36 -0700390 if (!have_compatible_annotations(*old_type, *new_type)) {
391 compatible = false;
392 }
Daniel Norman85aed542019-08-21 12:01:14 -0700393 if (old_type->AsInterface() != nullptr) {
394 if (new_type->AsInterface() == nullptr) {
395 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
396 << " is changed from " << old_type->GetPreprocessDeclarationName()
397 << " to " << new_type->GetPreprocessDeclarationName();
398 compatible = false;
399 continue;
400 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900401 compatible &=
402 are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface()));
Daniel Norman85aed542019-08-21 12:01:14 -0700403 } else if (old_type->AsStructuredParcelable() != nullptr) {
404 if (new_type->AsStructuredParcelable() == nullptr) {
405 AIDL_ERROR(new_type) << "Parcelable" << new_type->GetCanonicalName()
406 << " is not structured. ";
407 compatible = false;
408 continue;
409 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900410 compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()), *old_tns,
411 *(new_type->AsStructuredParcelable()), *new_tns);
Jooyung Han2946afc2020-10-05 20:29:16 +0900412 } else if (old_type->AsUnionDeclaration() != nullptr) {
413 if (new_type->AsUnionDeclaration() == nullptr) {
414 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
415 << " is changed from " << old_type->GetPreprocessDeclarationName()
416 << " to " << new_type->GetPreprocessDeclarationName();
417 compatible = false;
418 continue;
419 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900420 compatible &= are_compatible_parcelables(*(old_type->AsUnionDeclaration()), *old_tns,
421 *(new_type->AsUnionDeclaration()), *new_tns);
Daniel Norman85aed542019-08-21 12:01:14 -0700422 } else if (old_type->AsEnumDeclaration() != nullptr) {
423 if (new_type->AsEnumDeclaration() == nullptr) {
424 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
425 << " is changed from " << old_type->GetPreprocessDeclarationName()
426 << " to " << new_type->GetPreprocessDeclarationName();
427 compatible = false;
428 continue;
429 }
430 compatible &=
431 are_compatible_enums(*(old_type->AsEnumDeclaration()), *(new_type->AsEnumDeclaration()));
432 } else {
433 AIDL_ERROR(old_type) << "Unsupported type " << old_type->GetPreprocessDeclarationName()
434 << " for " << old_type->GetCanonicalName();
435 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900436 }
437 }
438
439 return compatible;
440}
441
442} // namespace aidl
443} // namespace android