blob: c4229b62cba39723ad0535b25215371b0dd2b226 [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
Steven Moreland7b6a7d92020-04-20 22:00:33 -070040static set<AidlAnnotation> get_strict_annotations(const AidlAnnotatable& node) {
41 // 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 Han90345002020-10-23 15:28:53 +090054 AidlAnnotation::Type::JAVA_DERIVE,
Jeongik Chad0a10272020-08-06 16:33:36 +090055 AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE,
Steven Moreland7b6a7d92020-04-20 22:00:33 -070056 };
57 set<AidlAnnotation> annotations;
58 for (const AidlAnnotation& annotation : node.GetAnnotations()) {
59 if (kIgnoreAnnotations.find(annotation.GetType()) == kIgnoreAnnotations.end()) {
60 annotations.insert(annotation);
61 }
62 }
63 return annotations;
64}
65
Jiyong Park3656c3c2018-08-01 20:02:01 +090066static bool have_compatible_annotations(const AidlAnnotatable& older,
67 const AidlAnnotatable& newer) {
Steven Moreland7b6a7d92020-04-20 22:00:33 -070068 set<AidlAnnotation> olderAnnotations = get_strict_annotations(older);
69 set<AidlAnnotation> newerAnnotations = get_strict_annotations(newer);
70
Jeongik Cha3271ffa2018-12-04 15:19:20 +090071 if (olderAnnotations != newerAnnotations) {
Jiyong Park3656c3c2018-08-01 20:02:01 +090072 const string from = older.ToString().empty() ? "(empty)" : older.ToString();
73 const string to = newer.ToString().empty() ? "(empty)" : newer.ToString();
74 AIDL_ERROR(newer) << "Changed annotations: " << from << " to " << to;
75 return false;
76 }
77 return true;
78}
79
80static bool are_compatible_types(const AidlTypeSpecifier& older, const AidlTypeSpecifier& newer) {
81 bool compatible = true;
82 if (older.ToString() != newer.ToString()) {
83 AIDL_ERROR(newer) << "Type changed: " << older.ToString() << " to " << newer.ToString() << ".";
84 compatible = false;
85 }
86 compatible &= have_compatible_annotations(older, newer);
87 return compatible;
88}
89
90static bool are_compatible_interfaces(const AidlInterface& older, const AidlInterface& newer) {
91 bool compatible = true;
Jiyong Park3656c3c2018-08-01 20:02:01 +090092
93 map<string, AidlMethod*> new_methods;
94 for (const auto& m : newer.AsInterface()->GetMethods()) {
95 new_methods.emplace(m->Signature(), m.get());
96 }
97
98 for (const auto& old_m : older.AsInterface()->GetMethods()) {
99 const auto found = new_methods.find(old_m->Signature());
100 if (found == new_methods.end()) {
Steven Moreland4ee68632018-12-14 15:52:46 -0800101 AIDL_ERROR(old_m) << "Removed or changed method: " << older.GetCanonicalName() << "."
Jiyong Park3656c3c2018-08-01 20:02:01 +0900102 << old_m->Signature();
103 compatible = false;
104 continue;
105 }
106
107 // Compare IDs to detect method reordering. IDs are assigned by their
108 // textual order, so if there is an ID mismatch, that means reordering
109 // has happened.
110 const auto new_m = found->second;
Steven Moreland4ee68632018-12-14 15:52:46 -0800111
112 if (old_m->IsOneway() != new_m->IsOneway()) {
113 AIDL_ERROR(new_m) << "Oneway attribute " << (old_m->IsOneway() ? "removed" : "added") << ": "
114 << older.GetCanonicalName() << "." << old_m->Signature();
115 compatible = false;
116 }
117
Jiyong Park3656c3c2018-08-01 20:02:01 +0900118 if (old_m->GetId() != new_m->GetId()) {
119 AIDL_ERROR(new_m) << "Transaction ID changed: " << older.GetCanonicalName() << "."
120 << old_m->Signature() << " is changed from " << old_m->GetId() << " to "
121 << new_m->GetId() << ".";
122 compatible = false;
123 }
124
125 compatible &= are_compatible_types(old_m->GetType(), new_m->GetType());
126
127 const auto& old_args = old_m->GetArguments();
128 const auto& new_args = new_m->GetArguments();
129 // this is guaranteed because arguments are part of AidlMethod::Signature()
Steven Moreland21780812020-09-11 01:29:45 +0000130 AIDL_FATAL_IF(old_args.size() != new_args.size(), old_m);
Jiyong Park3656c3c2018-08-01 20:02:01 +0900131 for (size_t i = 0; i < old_args.size(); i++) {
132 const AidlArgument& old_a = *(old_args.at(i));
133 const AidlArgument& new_a = *(new_args.at(i));
134 compatible &= are_compatible_types(old_a.GetType(), new_a.GetType());
135
136 if (old_a.GetDirection() != new_a.GetDirection()) {
137 AIDL_ERROR(new_m) << "Direction changed: " << old_a.GetDirectionSpecifier() << " to "
138 << new_a.GetDirectionSpecifier() << ".";
139 compatible = false;
140 }
141 }
142 }
Jiyong Parka428d212018-08-29 22:26:30 +0900143
144 map<string, AidlConstantDeclaration*> new_constdecls;
145 for (const auto& c : newer.AsInterface()->GetConstantDeclarations()) {
146 new_constdecls.emplace(c->GetName(), c.get());
147 }
148
149 for (const auto& old_c : older.AsInterface()->GetConstantDeclarations()) {
150 const auto found = new_constdecls.find(old_c->GetName());
151 if (found == new_constdecls.end()) {
152 AIDL_ERROR(old_c) << "Removed constant declaration: " << older.GetCanonicalName() << "."
153 << old_c->GetName();
154 compatible = false;
155 continue;
156 }
157
158 const auto new_c = found->second;
159 compatible &= are_compatible_types(old_c->GetType(), new_c->GetType());
160
161 const string old_value = old_c->ValueString(AidlConstantValueDecorator);
162 const string new_value = new_c->ValueString(AidlConstantValueDecorator);
163 if (old_value != new_value) {
164 AIDL_ERROR(newer) << "Changed constant value: " << older.GetCanonicalName() << "."
165 << old_c->GetName() << " from " << old_value << " to " << new_value << ".";
166 compatible = false;
167 }
168 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900169 return compatible;
170}
171
Steven Moreland370ed342020-04-28 18:14:39 -0700172// returns whether the given type when defaulted will be accepted by
173// unmarshalling code
174static bool has_usable_nil_type(const AidlTypeSpecifier& specifier) {
175 // TODO(b/155238508): fix for primitives
176
177 // This technically only applies in C++, but even if both the client and the
178 // server of an interface are in Java at a particular point in time, where
179 // null is currently always acceptable, we want to make sure that versions
180 // of this service can work in native and future backends without a problem.
181 // Also, in that case, adding nullable does not hurt.
182 return specifier.IsNullable();
183}
184
Jooyung Han636fd2f2020-10-22 11:33:45 +0900185static bool HasZeroEnumerator(const AidlEnumDeclaration& enum_decl) {
186 return std::any_of(enum_decl.GetEnumerators().begin(), enum_decl.GetEnumerators().end(),
187 [&](const unique_ptr<AidlEnumerator>& enumerator) {
188 return enumerator->GetValue()->ValueString(
189 enum_decl.GetBackingType(), AidlConstantValueDecorator) == "0";
190 });
191}
192
Jooyung Han2946afc2020-10-05 20:29:16 +0900193template <typename ParcelableType>
Jooyung Han636fd2f2020-10-22 11:33:45 +0900194static bool are_compatible_parcelables(const ParcelableType& older, const AidlTypenames&,
195 const ParcelableType& newer,
196 const AidlTypenames& new_types) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900197 const auto& old_fields = older.GetFields();
198 const auto& new_fields = newer.GetFields();
199 if (old_fields.size() > new_fields.size()) {
200 // you can add new fields only at the end
201 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from "
202 << old_fields.size() << " to " << new_fields.size() << ".";
203 return false;
204 }
Devin Moorec7e47a32020-08-07 10:55:25 -0700205 if (newer.IsFixedSize() && old_fields.size() != new_fields.size()) {
206 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is changed from "
207 << old_fields.size() << " to " << new_fields.size()
208 << ". This is an incompatible change for FixedSize types.";
209 return false;
210 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900211
212 bool compatible = true;
213 for (size_t i = 0; i < old_fields.size(); i++) {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900214 const auto& old_field = old_fields.at(i);
215 const auto& new_field = new_fields.at(i);
216 compatible &= are_compatible_types(old_field->GetType(), new_field->GetType());
Jiyong Park3656c3c2018-08-01 20:02:01 +0900217
Jiyong Parka468e2a2018-08-29 21:25:18 +0900218 const string old_value = old_field->ValueString(AidlConstantValueDecorator);
219 const string new_value = new_field->ValueString(AidlConstantValueDecorator);
220 if (old_value != new_value) {
Steven Moreland370ed342020-04-28 18:14:39 -0700221 AIDL_ERROR(new_field) << "Changed default value: " << old_value << " to " << new_value << ".";
222 compatible = false;
223 }
224 }
225
Jiyong Parkb07d9932020-05-15 12:56:54 +0900226 // Reordering of fields is an incompatible change.
227 for (size_t i = 0; i < new_fields.size(); i++) {
228 const auto& new_field = new_fields.at(i);
229 auto found = std::find_if(old_fields.begin(), old_fields.end(), [&new_field](const auto& f) {
230 return new_field->GetName() == f->GetName();
231 });
232 if (found != old_fields.end()) {
233 size_t old_index = std::distance(old_fields.begin(), found);
234 if (old_index != i) {
235 AIDL_ERROR(new_field) << "Reordered " << new_field->GetName() << " from " << old_index
236 << " to " << i << ".";
237 compatible = false;
238 }
239 }
240 }
241
Steven Moreland370ed342020-04-28 18:14:39 -0700242 for (size_t i = old_fields.size(); i < new_fields.size(); i++) {
243 const auto& new_field = new_fields.at(i);
Jooyung Han636fd2f2020-10-22 11:33:45 +0900244 if (new_field->GetDefaultValue()) {
245 continue;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900246 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900247
248 // null is accepted as a valid default value
249 if (has_usable_nil_type(new_field->GetType())) {
250 continue;
251 }
252
253 // enum can't be nullable, but it's okay if it has 0 as a valid enumerator.
254 if (const auto& enum_decl = new_types.GetEnumDeclaration(new_field->GetType());
255 enum_decl != nullptr) {
256 if (HasZeroEnumerator(*enum_decl)) {
257 continue;
258 }
259
260 // TODO(b/142893595): Rephrase the message: "provide a default value or make sure ..."
261 AIDL_ERROR(new_field) << "Field '" << new_field->GetName() << "' of enum '"
262 << enum_decl->GetName()
263 << "' can't be initialized as '0'. Please make sure '"
264 << enum_decl->GetName() << "' has '0' as a valid value.";
265 compatible = false;
266 continue;
267 }
268
269 // Old API versions may suffer from the issue presented here. There is
270 // only a finite number in Android, which we must allow indefinitely.
271 struct HistoricalException {
272 std::string canonical;
273 std::string field;
274 };
275 static std::vector<HistoricalException> exceptions = {
276 {"android.net.DhcpResultsParcelable", "serverHostName"},
277 {"android.net.ResolverParamsParcel", "resolverOptions"},
278 };
279 bool excepted = false;
280 for (const HistoricalException& exception : exceptions) {
281 if (older.GetCanonicalName() == exception.canonical &&
282 new_field->GetName() == exception.field) {
283 excepted = true;
284 break;
285 }
286 }
287 if (excepted) continue;
288
289 AIDL_ERROR(new_field)
290 << "Field '" << new_field->GetName()
291 << "' does not have a useful default in some backends. Please either provide a default "
292 "value for this field or mark the field as @nullable. This value or a null value will "
293 "be used automatically when an old version of this parcelable is sent to a process "
294 "which understands a new version of this parcelable. In order to make sure your code "
295 "continues to be backwards compatible, make sure the default or null value does not "
296 "cause a semantic change to this parcelable.";
297 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900298 }
299 return compatible;
300}
301
Daniel Norman85aed542019-08-21 12:01:14 -0700302static bool are_compatible_enums(const AidlEnumDeclaration& older,
303 const AidlEnumDeclaration& newer) {
304 if (!are_compatible_types(older.GetBackingType(), newer.GetBackingType())) {
305 AIDL_ERROR(newer) << "Changed backing types.";
306 return false;
307 }
308
309 std::map<std::string, const AidlConstantValue*> old_enum_map;
310 for (const auto& enumerator : older.GetEnumerators()) {
311 old_enum_map[enumerator->GetName()] = enumerator->GetValue();
312 }
313 std::map<std::string, const AidlConstantValue*> new_enum_map;
314 for (const auto& enumerator : newer.GetEnumerators()) {
315 new_enum_map[enumerator->GetName()] = enumerator->GetValue();
316 }
317
318 bool compatible = true;
319 for (const auto& [name, value] : old_enum_map) {
320 if (new_enum_map.find(name) == new_enum_map.end()) {
321 AIDL_ERROR(newer) << "Removed enumerator from " << older.GetCanonicalName() << ": " << name;
322 compatible = false;
323 continue;
324 }
325 const string old_value =
Will McVickerd7d18df2019-09-12 13:40:50 -0700326 old_enum_map[name]->ValueString(older.GetBackingType(), AidlConstantValueDecorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700327 const string new_value =
Will McVickerd7d18df2019-09-12 13:40:50 -0700328 new_enum_map[name]->ValueString(newer.GetBackingType(), AidlConstantValueDecorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700329 if (old_value != new_value) {
330 AIDL_ERROR(newer) << "Changed enumerator value: " << older.GetCanonicalName() << "::" << name
331 << " from " << old_value << " to " << new_value << ".";
332 compatible = false;
333 }
334 }
335 return compatible;
336}
337
Jiyong Park0cf03b12020-07-22 19:36:34 +0900338static Result<AidlTypenames> load_from_dir(const Options& options, const IoDelegate& io_delegate,
339 const std::string& dir) {
340 AidlTypenames typenames;
341 for (const auto& file : io_delegate.ListFiles(dir)) {
342 if (!android::base::EndsWith(file, ".aidl")) continue;
343 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames,
344 nullptr /* imported_files */) != AidlError::OK) {
345 AIDL_ERROR(file) << "Failed to read.";
346 return Error();
347 }
348 }
349 return typenames;
350}
351
Jiyong Park3656c3c2018-08-01 20:02:01 +0900352bool check_api(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland21780812020-09-11 01:29:45 +0000353 AIDL_FATAL_IF(!options.IsStructured(), AIDL_LOCATION_HERE);
354 AIDL_FATAL_IF(options.InputFiles().size() != 2, AIDL_LOCATION_HERE)
355 << "--checkapi requires two inputs "
356 << "but got " << options.InputFiles().size();
Jiyong Park0cf03b12020-07-22 19:36:34 +0900357 auto old_tns = load_from_dir(options, io_delegate, options.InputFiles().at(0));
358 if (!old_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900359 return false;
360 }
Jiyong Park0cf03b12020-07-22 19:36:34 +0900361 auto new_tns = load_from_dir(options, io_delegate, options.InputFiles().at(1));
362 if (!new_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900363 return false;
364 }
Jiyong Parkf7534672019-08-12 22:06:22 +0900365
Jiyong Park0cf03b12020-07-22 19:36:34 +0900366 std::vector<AidlDefinedType*> old_types = old_tns->AllDefinedTypes();
367 std::vector<AidlDefinedType*> new_types = new_tns->AllDefinedTypes();
Jiyong Park3656c3c2018-08-01 20:02:01 +0900368
369 map<string, AidlDefinedType*> new_map;
370 for (const auto t : new_types) {
371 new_map.emplace(t->GetCanonicalName(), t);
372 }
373
374 bool compatible = true;
375 for (const auto old_type : old_types) {
376 const auto found = new_map.find(old_type->GetCanonicalName());
377 if (found == new_map.end()) {
378 AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName();
379 compatible = false;
380 continue;
381 }
382 const auto new_type = found->second;
383
Devin Mooredb7ac512020-08-07 11:17:36 -0700384 if (!have_compatible_annotations(*old_type, *new_type)) {
385 compatible = false;
386 }
Daniel Norman85aed542019-08-21 12:01:14 -0700387 if (old_type->AsInterface() != nullptr) {
388 if (new_type->AsInterface() == nullptr) {
389 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
390 << " is changed from " << old_type->GetPreprocessDeclarationName()
391 << " to " << new_type->GetPreprocessDeclarationName();
392 compatible = false;
393 continue;
394 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900395 compatible &=
396 are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface()));
Daniel Norman85aed542019-08-21 12:01:14 -0700397 } else if (old_type->AsStructuredParcelable() != nullptr) {
398 if (new_type->AsStructuredParcelable() == nullptr) {
399 AIDL_ERROR(new_type) << "Parcelable" << new_type->GetCanonicalName()
400 << " is not structured. ";
401 compatible = false;
402 continue;
403 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900404 compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()), *old_tns,
405 *(new_type->AsStructuredParcelable()), *new_tns);
Jooyung Han2946afc2020-10-05 20:29:16 +0900406 } else if (old_type->AsUnionDeclaration() != nullptr) {
407 if (new_type->AsUnionDeclaration() == nullptr) {
408 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
409 << " is changed from " << old_type->GetPreprocessDeclarationName()
410 << " to " << new_type->GetPreprocessDeclarationName();
411 compatible = false;
412 continue;
413 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900414 compatible &= are_compatible_parcelables(*(old_type->AsUnionDeclaration()), *old_tns,
415 *(new_type->AsUnionDeclaration()), *new_tns);
Daniel Norman85aed542019-08-21 12:01:14 -0700416 } else if (old_type->AsEnumDeclaration() != nullptr) {
417 if (new_type->AsEnumDeclaration() == nullptr) {
418 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
419 << " is changed from " << old_type->GetPreprocessDeclarationName()
420 << " to " << new_type->GetPreprocessDeclarationName();
421 compatible = false;
422 continue;
423 }
424 compatible &=
425 are_compatible_enums(*(old_type->AsEnumDeclaration()), *(new_type->AsEnumDeclaration()));
426 } else {
427 AIDL_ERROR(old_type) << "Unsupported type " << old_type->GetPreprocessDeclarationName()
428 << " for " << old_type->GetCanonicalName();
429 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900430 }
431 }
432
433 return compatible;
434}
435
436} // namespace aidl
437} // namespace android