blob: 2b5664a65b0a95bad6ae569299b9129408e2249b [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;
Jooyung Han00073272020-11-27 14:20:20 +090035using android::base::StartsWith;
Jiyong Park3656c3c2018-08-01 20:02:01 +090036using std::map;
37using std::set;
38using std::string;
39using std::vector;
40
Jooyung Han69ea4ba2020-10-29 15:33:37 +090041static vector<string> get_strict_annotations(const AidlAnnotatable& node) {
Steven Moreland7b6a7d92020-04-20 22:00:33 -070042 // This must be symmetrical (if you can add something, you must be able to
43 // remove it). The reason is that we have no way of knowing which interface a
44 // server serves and which interface a client serves (e.g. a callback
45 // interface). Note that this is being overly lenient. It makes sense for
46 // newer code to start accepting nullable things. However, here, we don't know
47 // if the client of an interface or the server of an interface is newer.
48 //
49 // Here are two examples to demonstrate this:
50 // - a new implementation might change so that it no longer returns null
51 // values (remove @nullable)
52 // - a new implementation might start accepting null values (add @nullable)
53 static const set<AidlAnnotation::Type> kIgnoreAnnotations{
54 AidlAnnotation::Type::NULLABLE,
Jooyung Han69ea4ba2020-10-29 15:33:37 +090055 // @JavaDerive doesn't affect read/write
Jooyung Han90345002020-10-23 15:28:53 +090056 AidlAnnotation::Type::JAVA_DERIVE,
Jeongik Chad0a10272020-08-06 16:33:36 +090057 AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE,
Jooyung Han69ea4ba2020-10-29 15:33:37 +090058 // @Backing for a enum type is checked by the enum checker
59 AidlAnnotation::Type::BACKING,
60 // @RustDerive doesn't affect read/write
61 AidlAnnotation::Type::RUST_DERIVE,
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +090062 AidlAnnotation::Type::SUPPRESS_WARNINGS,
Steven Moreland7b6a7d92020-04-20 22:00:33 -070063 };
Jooyung Han69ea4ba2020-10-29 15:33:37 +090064 vector<string> annotations;
Steven Moreland7b6a7d92020-04-20 22:00:33 -070065 for (const AidlAnnotation& annotation : node.GetAnnotations()) {
Jooyung Han00073272020-11-27 14:20:20 +090066 if (kIgnoreAnnotations.find(annotation.GetType()) != kIgnoreAnnotations.end()) {
67 continue;
Steven Moreland7b6a7d92020-04-20 22:00:33 -070068 }
Jooyung Han00073272020-11-27 14:20:20 +090069 auto annotation_string = annotation.ToString();
70 // adding @Deprecated (with optional args) is okay
71 if (StartsWith(annotation_string, "@JavaPassthrough(annotation=\"@Deprecated")) {
72 continue;
73 }
74 annotations.push_back(annotation_string);
Steven Moreland7b6a7d92020-04-20 22:00:33 -070075 }
76 return annotations;
77}
78
Jiyong Park3656c3c2018-08-01 20:02:01 +090079static bool have_compatible_annotations(const AidlAnnotatable& older,
80 const AidlAnnotatable& newer) {
Jooyung Han69ea4ba2020-10-29 15:33:37 +090081 vector<string> olderAnnotations = get_strict_annotations(older);
82 vector<string> newerAnnotations = get_strict_annotations(newer);
83 sort(olderAnnotations.begin(), olderAnnotations.end());
84 sort(newerAnnotations.begin(), newerAnnotations.end());
Jeongik Cha3271ffa2018-12-04 15:19:20 +090085 if (olderAnnotations != newerAnnotations) {
Jiyong Park3656c3c2018-08-01 20:02:01 +090086 const string from = older.ToString().empty() ? "(empty)" : older.ToString();
87 const string to = newer.ToString().empty() ? "(empty)" : newer.ToString();
88 AIDL_ERROR(newer) << "Changed annotations: " << from << " to " << to;
89 return false;
90 }
91 return true;
92}
93
94static bool are_compatible_types(const AidlTypeSpecifier& older, const AidlTypeSpecifier& newer) {
95 bool compatible = true;
Jooyung Han965e31d2020-11-27 12:30:16 +090096 if (older.Signature() != newer.Signature()) {
97 AIDL_ERROR(newer) << "Type changed: " << older.Signature() << " to " << newer.Signature()
98 << ".";
Jiyong Park3656c3c2018-08-01 20:02:01 +090099 compatible = false;
100 }
101 compatible &= have_compatible_annotations(older, newer);
102 return compatible;
103}
104
Jooyung Han829ec7c2020-12-02 12:07:36 +0900105static bool are_compatible_constants(const AidlDefinedType& older, const AidlDefinedType& newer) {
Jooyung Han3f347ca2020-12-01 12:41:50 +0900106 bool compatible = true;
107
108 map<string, AidlConstantDeclaration*> new_constdecls;
109 for (const auto& c : newer.GetConstantDeclarations()) {
110 new_constdecls[c->GetName()] = &*c;
111 }
112
113 for (const auto& old_c : older.GetConstantDeclarations()) {
114 const auto found = new_constdecls.find(old_c->GetName());
115 if (found == new_constdecls.end()) {
116 AIDL_ERROR(old_c) << "Removed constant declaration: " << older.GetCanonicalName() << "."
117 << old_c->GetName();
118 compatible = false;
119 continue;
120 }
121
122 const auto new_c = found->second;
123 compatible &= are_compatible_types(old_c->GetType(), new_c->GetType());
124
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900125 const string old_value = old_c->GetValue().Literal();
126 const string new_value = new_c->GetValue().Literal();
Jooyung Han3f347ca2020-12-01 12:41:50 +0900127 if (old_value != new_value) {
128 AIDL_ERROR(newer) << "Changed constant value: " << older.GetCanonicalName() << "."
129 << old_c->GetName() << " from " << old_value << " to " << new_value << ".";
130 compatible = false;
131 }
132 }
133 return compatible;
134}
135
Jiyong Park3656c3c2018-08-01 20:02:01 +0900136static bool are_compatible_interfaces(const AidlInterface& older, const AidlInterface& newer) {
137 bool compatible = true;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900138
139 map<string, AidlMethod*> new_methods;
140 for (const auto& m : newer.AsInterface()->GetMethods()) {
141 new_methods.emplace(m->Signature(), m.get());
142 }
143
144 for (const auto& old_m : older.AsInterface()->GetMethods()) {
145 const auto found = new_methods.find(old_m->Signature());
146 if (found == new_methods.end()) {
Steven Moreland4ee68632018-12-14 15:52:46 -0800147 AIDL_ERROR(old_m) << "Removed or changed method: " << older.GetCanonicalName() << "."
Jiyong Park3656c3c2018-08-01 20:02:01 +0900148 << old_m->Signature();
149 compatible = false;
150 continue;
151 }
152
153 // Compare IDs to detect method reordering. IDs are assigned by their
154 // textual order, so if there is an ID mismatch, that means reordering
155 // has happened.
156 const auto new_m = found->second;
Steven Moreland4ee68632018-12-14 15:52:46 -0800157
158 if (old_m->IsOneway() != new_m->IsOneway()) {
159 AIDL_ERROR(new_m) << "Oneway attribute " << (old_m->IsOneway() ? "removed" : "added") << ": "
160 << older.GetCanonicalName() << "." << old_m->Signature();
161 compatible = false;
162 }
163
Jiyong Park3656c3c2018-08-01 20:02:01 +0900164 if (old_m->GetId() != new_m->GetId()) {
165 AIDL_ERROR(new_m) << "Transaction ID changed: " << older.GetCanonicalName() << "."
166 << old_m->Signature() << " is changed from " << old_m->GetId() << " to "
167 << new_m->GetId() << ".";
168 compatible = false;
169 }
170
171 compatible &= are_compatible_types(old_m->GetType(), new_m->GetType());
172
173 const auto& old_args = old_m->GetArguments();
174 const auto& new_args = new_m->GetArguments();
175 // this is guaranteed because arguments are part of AidlMethod::Signature()
Steven Moreland21780812020-09-11 01:29:45 +0000176 AIDL_FATAL_IF(old_args.size() != new_args.size(), old_m);
Jiyong Park3656c3c2018-08-01 20:02:01 +0900177 for (size_t i = 0; i < old_args.size(); i++) {
178 const AidlArgument& old_a = *(old_args.at(i));
179 const AidlArgument& new_a = *(new_args.at(i));
180 compatible &= are_compatible_types(old_a.GetType(), new_a.GetType());
181
182 if (old_a.GetDirection() != new_a.GetDirection()) {
183 AIDL_ERROR(new_m) << "Direction changed: " << old_a.GetDirectionSpecifier() << " to "
184 << new_a.GetDirectionSpecifier() << ".";
185 compatible = false;
186 }
187 }
188 }
Jiyong Parka428d212018-08-29 22:26:30 +0900189
Jooyung Han3f347ca2020-12-01 12:41:50 +0900190 compatible = are_compatible_constants(older, newer) && compatible;
Jiyong Parka428d212018-08-29 22:26:30 +0900191
Jiyong Park3656c3c2018-08-01 20:02:01 +0900192 return compatible;
193}
194
Jooyung Han636fd2f2020-10-22 11:33:45 +0900195static bool HasZeroEnumerator(const AidlEnumDeclaration& enum_decl) {
196 return std::any_of(enum_decl.GetEnumerators().begin(), enum_decl.GetEnumerators().end(),
197 [&](const unique_ptr<AidlEnumerator>& enumerator) {
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900198 return enumerator->GetValue()->Literal() == "0";
Jooyung Han636fd2f2020-10-22 11:33:45 +0900199 });
200}
201
Jooyung Han829ec7c2020-12-02 12:07:36 +0900202static bool are_compatible_parcelables(const AidlDefinedType& older, const AidlTypenames&,
203 const AidlDefinedType& newer,
Jooyung Han636fd2f2020-10-22 11:33:45 +0900204 const AidlTypenames& new_types) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900205 const auto& old_fields = older.GetFields();
206 const auto& new_fields = newer.GetFields();
207 if (old_fields.size() > new_fields.size()) {
208 // you can add new fields only at the end
209 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from "
210 << old_fields.size() << " to " << new_fields.size() << ".";
211 return false;
212 }
Devin Moorec7e47a32020-08-07 10:55:25 -0700213 if (newer.IsFixedSize() && old_fields.size() != new_fields.size()) {
214 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is changed from "
215 << old_fields.size() << " to " << new_fields.size()
216 << ". This is an incompatible change for FixedSize types.";
217 return false;
218 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900219
220 bool compatible = true;
221 for (size_t i = 0; i < old_fields.size(); i++) {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900222 const auto& old_field = old_fields.at(i);
223 const auto& new_field = new_fields.at(i);
224 compatible &= are_compatible_types(old_field->GetType(), new_field->GetType());
Jiyong Park3656c3c2018-08-01 20:02:01 +0900225
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900226 string old_value = old_field->GetDefaultValue() ? old_field->GetDefaultValue()->Literal() : "";
227 string new_value = new_field->GetDefaultValue() ? new_field->GetDefaultValue()->Literal() : "";
Jiyong Parka468e2a2018-08-29 21:25:18 +0900228 if (old_value != new_value) {
Steven Moreland370ed342020-04-28 18:14:39 -0700229 AIDL_ERROR(new_field) << "Changed default value: " << old_value << " to " << new_value << ".";
230 compatible = false;
231 }
232 }
233
Jiyong Parkb07d9932020-05-15 12:56:54 +0900234 // Reordering of fields is an incompatible change.
235 for (size_t i = 0; i < new_fields.size(); i++) {
236 const auto& new_field = new_fields.at(i);
237 auto found = std::find_if(old_fields.begin(), old_fields.end(), [&new_field](const auto& f) {
238 return new_field->GetName() == f->GetName();
239 });
240 if (found != old_fields.end()) {
241 size_t old_index = std::distance(old_fields.begin(), found);
242 if (old_index != i) {
243 AIDL_ERROR(new_field) << "Reordered " << new_field->GetName() << " from " << old_index
244 << " to " << i << ".";
245 compatible = false;
246 }
247 }
248 }
249
Steven Moreland370ed342020-04-28 18:14:39 -0700250 for (size_t i = old_fields.size(); i < new_fields.size(); i++) {
251 const auto& new_field = new_fields.at(i);
Jooyung Han53fb4242020-12-17 16:03:49 +0900252 if (new_field->HasUsefulDefaultValue()) {
Jooyung Han636fd2f2020-10-22 11:33:45 +0900253 continue;
254 }
255
256 // enum can't be nullable, but it's okay if it has 0 as a valid enumerator.
257 if (const auto& enum_decl = new_types.GetEnumDeclaration(new_field->GetType());
258 enum_decl != nullptr) {
259 if (HasZeroEnumerator(*enum_decl)) {
260 continue;
261 }
262
263 // TODO(b/142893595): Rephrase the message: "provide a default value or make sure ..."
264 AIDL_ERROR(new_field) << "Field '" << new_field->GetName() << "' of enum '"
265 << enum_decl->GetName()
266 << "' can't be initialized as '0'. Please make sure '"
267 << enum_decl->GetName() << "' has '0' as a valid value.";
268 compatible = false;
269 continue;
270 }
271
272 // Old API versions may suffer from the issue presented here. There is
273 // only a finite number in Android, which we must allow indefinitely.
274 struct HistoricalException {
275 std::string canonical;
276 std::string field;
277 };
278 static std::vector<HistoricalException> exceptions = {
279 {"android.net.DhcpResultsParcelable", "serverHostName"},
280 {"android.net.ResolverParamsParcel", "resolverOptions"},
281 };
282 bool excepted = false;
283 for (const HistoricalException& exception : exceptions) {
284 if (older.GetCanonicalName() == exception.canonical &&
285 new_field->GetName() == exception.field) {
286 excepted = true;
287 break;
288 }
289 }
290 if (excepted) continue;
291
292 AIDL_ERROR(new_field)
293 << "Field '" << new_field->GetName()
294 << "' does not have a useful default in some backends. Please either provide a default "
295 "value for this field or mark the field as @nullable. This value or a null value will "
296 "be used automatically when an old version of this parcelable is sent to a process "
297 "which understands a new version of this parcelable. In order to make sure your code "
298 "continues to be backwards compatible, make sure the default or null value does not "
299 "cause a semantic change to this parcelable.";
300 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900301 }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900302
303 compatible = are_compatible_constants(older, newer) && compatible;
304
Jiyong Park3656c3c2018-08-01 20:02:01 +0900305 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 }
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900331 const string old_value = old_enum_map[name]->Literal();
332 const string new_value = new_enum_map[name]->Literal();
Daniel Norman85aed542019-08-21 12:01:14 -0700333 if (old_value != new_value) {
334 AIDL_ERROR(newer) << "Changed enumerator value: " << older.GetCanonicalName() << "::" << name
335 << " from " << old_value << " to " << new_value << ".";
336 compatible = false;
337 }
338 }
339 return compatible;
340}
341
Jiyong Park0cf03b12020-07-22 19:36:34 +0900342static Result<AidlTypenames> load_from_dir(const Options& options, const IoDelegate& io_delegate,
343 const std::string& dir) {
344 AidlTypenames typenames;
345 for (const auto& file : io_delegate.ListFiles(dir)) {
346 if (!android::base::EndsWith(file, ".aidl")) continue;
347 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames,
348 nullptr /* imported_files */) != AidlError::OK) {
349 AIDL_ERROR(file) << "Failed to read.";
350 return Error();
351 }
352 }
353 return typenames;
354}
355
Jiyong Park3656c3c2018-08-01 20:02:01 +0900356bool check_api(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland21780812020-09-11 01:29:45 +0000357 AIDL_FATAL_IF(!options.IsStructured(), AIDL_LOCATION_HERE);
358 AIDL_FATAL_IF(options.InputFiles().size() != 2, AIDL_LOCATION_HERE)
359 << "--checkapi requires two inputs "
360 << "but got " << options.InputFiles().size();
Jiyong Park0cf03b12020-07-22 19:36:34 +0900361 auto old_tns = load_from_dir(options, io_delegate, options.InputFiles().at(0));
362 if (!old_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900363 return false;
364 }
Jiyong Park0cf03b12020-07-22 19:36:34 +0900365 auto new_tns = load_from_dir(options, io_delegate, options.InputFiles().at(1));
366 if (!new_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900367 return false;
368 }
Jiyong Parkf7534672019-08-12 22:06:22 +0900369
Jiyong Park0cf03b12020-07-22 19:36:34 +0900370 std::vector<AidlDefinedType*> old_types = old_tns->AllDefinedTypes();
371 std::vector<AidlDefinedType*> new_types = new_tns->AllDefinedTypes();
Jiyong Park3656c3c2018-08-01 20:02:01 +0900372
373 map<string, AidlDefinedType*> new_map;
374 for (const auto t : new_types) {
375 new_map.emplace(t->GetCanonicalName(), t);
376 }
377
378 bool compatible = true;
379 for (const auto old_type : old_types) {
380 const auto found = new_map.find(old_type->GetCanonicalName());
381 if (found == new_map.end()) {
382 AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName();
383 compatible = false;
384 continue;
385 }
386 const auto new_type = found->second;
387
Devin Mooredb7ac512020-08-07 11:17:36 -0700388 if (!have_compatible_annotations(*old_type, *new_type)) {
389 compatible = false;
390 }
Daniel Norman85aed542019-08-21 12:01:14 -0700391 if (old_type->AsInterface() != nullptr) {
392 if (new_type->AsInterface() == nullptr) {
393 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
394 << " is changed from " << old_type->GetPreprocessDeclarationName()
395 << " to " << new_type->GetPreprocessDeclarationName();
396 compatible = false;
397 continue;
398 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900399 compatible &=
400 are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface()));
Daniel Norman85aed542019-08-21 12:01:14 -0700401 } else if (old_type->AsStructuredParcelable() != nullptr) {
402 if (new_type->AsStructuredParcelable() == nullptr) {
403 AIDL_ERROR(new_type) << "Parcelable" << new_type->GetCanonicalName()
404 << " is not structured. ";
405 compatible = false;
406 continue;
407 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900408 compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()), *old_tns,
409 *(new_type->AsStructuredParcelable()), *new_tns);
Jooyung Han2946afc2020-10-05 20:29:16 +0900410 } else if (old_type->AsUnionDeclaration() != nullptr) {
411 if (new_type->AsUnionDeclaration() == nullptr) {
412 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
413 << " is changed from " << old_type->GetPreprocessDeclarationName()
414 << " to " << new_type->GetPreprocessDeclarationName();
415 compatible = false;
416 continue;
417 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900418 compatible &= are_compatible_parcelables(*(old_type->AsUnionDeclaration()), *old_tns,
419 *(new_type->AsUnionDeclaration()), *new_tns);
Daniel Norman85aed542019-08-21 12:01:14 -0700420 } else if (old_type->AsEnumDeclaration() != nullptr) {
421 if (new_type->AsEnumDeclaration() == nullptr) {
422 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
423 << " is changed from " << old_type->GetPreprocessDeclarationName()
424 << " to " << new_type->GetPreprocessDeclarationName();
425 compatible = false;
426 continue;
427 }
428 compatible &=
429 are_compatible_enums(*(old_type->AsEnumDeclaration()), *(new_type->AsEnumDeclaration()));
430 } else {
431 AIDL_ERROR(old_type) << "Unsupported type " << old_type->GetPreprocessDeclarationName()
432 << " for " << old_type->GetCanonicalName();
433 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900434 }
435 }
436
437 return compatible;
438}
439
440} // namespace aidl
441} // namespace android