blob: a0107e41c8e7ed9c40220cd85382a7f3294ed07c [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,
Jiyong Park43113fb2020-07-20 16:26:19 +090054 AidlAnnotation::Type::JAVA_DEBUG,
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()
130 CHECK(old_args.size() == new_args.size());
131 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
Jiyong Park3656c3c2018-08-01 20:02:01 +0900185static bool are_compatible_parcelables(const AidlStructuredParcelable& older,
186 const AidlStructuredParcelable& newer) {
187 const auto& old_fields = older.GetFields();
188 const auto& new_fields = newer.GetFields();
189 if (old_fields.size() > new_fields.size()) {
190 // you can add new fields only at the end
191 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from "
192 << old_fields.size() << " to " << new_fields.size() << ".";
193 return false;
194 }
Devin Moorec7e47a32020-08-07 10:55:25 -0700195 if (newer.IsFixedSize() && old_fields.size() != new_fields.size()) {
196 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is changed from "
197 << old_fields.size() << " to " << new_fields.size()
198 << ". This is an incompatible change for FixedSize types.";
199 return false;
200 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900201
202 bool compatible = true;
203 for (size_t i = 0; i < old_fields.size(); i++) {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900204 const auto& old_field = old_fields.at(i);
205 const auto& new_field = new_fields.at(i);
206 compatible &= are_compatible_types(old_field->GetType(), new_field->GetType());
Jiyong Park3656c3c2018-08-01 20:02:01 +0900207
Jiyong Parka468e2a2018-08-29 21:25:18 +0900208 const string old_value = old_field->ValueString(AidlConstantValueDecorator);
209 const string new_value = new_field->ValueString(AidlConstantValueDecorator);
210 if (old_value != new_value) {
Steven Moreland370ed342020-04-28 18:14:39 -0700211 AIDL_ERROR(new_field) << "Changed default value: " << old_value << " to " << new_value << ".";
212 compatible = false;
213 }
214 }
215
Jiyong Parkb07d9932020-05-15 12:56:54 +0900216 // Reordering of fields is an incompatible change.
217 for (size_t i = 0; i < new_fields.size(); i++) {
218 const auto& new_field = new_fields.at(i);
219 auto found = std::find_if(old_fields.begin(), old_fields.end(), [&new_field](const auto& f) {
220 return new_field->GetName() == f->GetName();
221 });
222 if (found != old_fields.end()) {
223 size_t old_index = std::distance(old_fields.begin(), found);
224 if (old_index != i) {
225 AIDL_ERROR(new_field) << "Reordered " << new_field->GetName() << " from " << old_index
226 << " to " << i << ".";
227 compatible = false;
228 }
229 }
230 }
231
Steven Moreland370ed342020-04-28 18:14:39 -0700232 for (size_t i = old_fields.size(); i < new_fields.size(); i++) {
233 const auto& new_field = new_fields.at(i);
234 if (!new_field->GetDefaultValue() && !has_usable_nil_type(new_field->GetType())) {
235 // Old API versions may suffer from the issue presented here. There is
236 // only a finite number in Android, which we must allow indefinitely.
237 struct HistoricalException {
238 std::string canonical;
239 std::string field;
240 };
241 static std::vector<HistoricalException> exceptions = {
Steven Moreland370ed342020-04-28 18:14:39 -0700242 {"android.net.DhcpResultsParcelable", "serverHostName"},
Lorenzo Colittib183a8b2020-05-01 11:31:33 +0900243 {"android.net.ResolverParamsParcel", "resolverOptions"},
Steven Moreland370ed342020-04-28 18:14:39 -0700244 };
245 bool excepted = false;
246 for (const HistoricalException& exception : exceptions) {
247 if (older.GetCanonicalName() == exception.canonical &&
248 new_field->GetName() == exception.field) {
249 excepted = true;
250 break;
251 }
252 }
253 if (excepted) continue;
254
255 AIDL_ERROR(new_field)
256 << "Field '" << new_field->GetName()
257 << "' does not have a useful default in some backends. Please either provide a default "
258 "value for this field or mark the field as @nullable. This value or a null value will "
259 "be used automatically when an old version of this parcelable is sent to a process "
260 "which understands a new version of this parcelable. In order to make sure your code "
261 "continues to be backwards compatible, make sure the default or null value does not "
262 "cause a semantic change to this parcelable.";
Jiyong Park3656c3c2018-08-01 20:02:01 +0900263 compatible = false;
264 }
265 }
266 return compatible;
267}
268
Daniel Norman85aed542019-08-21 12:01:14 -0700269static bool are_compatible_enums(const AidlEnumDeclaration& older,
270 const AidlEnumDeclaration& newer) {
271 if (!are_compatible_types(older.GetBackingType(), newer.GetBackingType())) {
272 AIDL_ERROR(newer) << "Changed backing types.";
273 return false;
274 }
275
276 std::map<std::string, const AidlConstantValue*> old_enum_map;
277 for (const auto& enumerator : older.GetEnumerators()) {
278 old_enum_map[enumerator->GetName()] = enumerator->GetValue();
279 }
280 std::map<std::string, const AidlConstantValue*> new_enum_map;
281 for (const auto& enumerator : newer.GetEnumerators()) {
282 new_enum_map[enumerator->GetName()] = enumerator->GetValue();
283 }
284
285 bool compatible = true;
286 for (const auto& [name, value] : old_enum_map) {
287 if (new_enum_map.find(name) == new_enum_map.end()) {
288 AIDL_ERROR(newer) << "Removed enumerator from " << older.GetCanonicalName() << ": " << name;
289 compatible = false;
290 continue;
291 }
292 const string old_value =
Will McVickerd7d18df2019-09-12 13:40:50 -0700293 old_enum_map[name]->ValueString(older.GetBackingType(), AidlConstantValueDecorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700294 const string new_value =
Will McVickerd7d18df2019-09-12 13:40:50 -0700295 new_enum_map[name]->ValueString(newer.GetBackingType(), AidlConstantValueDecorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700296 if (old_value != new_value) {
297 AIDL_ERROR(newer) << "Changed enumerator value: " << older.GetCanonicalName() << "::" << name
298 << " from " << old_value << " to " << new_value << ".";
299 compatible = false;
300 }
301 }
302 return compatible;
303}
304
Jiyong Park0cf03b12020-07-22 19:36:34 +0900305static Result<AidlTypenames> load_from_dir(const Options& options, const IoDelegate& io_delegate,
306 const std::string& dir) {
307 AidlTypenames typenames;
308 for (const auto& file : io_delegate.ListFiles(dir)) {
309 if (!android::base::EndsWith(file, ".aidl")) continue;
310 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames,
311 nullptr /* imported_files */) != AidlError::OK) {
312 AIDL_ERROR(file) << "Failed to read.";
313 return Error();
314 }
315 }
316 return typenames;
317}
318
Jiyong Park3656c3c2018-08-01 20:02:01 +0900319bool check_api(const Options& options, const IoDelegate& io_delegate) {
320 CHECK(options.IsStructured());
321 CHECK(options.InputFiles().size() == 2) << "--checkapi requires two inputs "
322 << "but got " << options.InputFiles().size();
Jiyong Park0cf03b12020-07-22 19:36:34 +0900323 auto old_tns = load_from_dir(options, io_delegate, options.InputFiles().at(0));
324 if (!old_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900325 return false;
326 }
Jiyong Park0cf03b12020-07-22 19:36:34 +0900327 auto new_tns = load_from_dir(options, io_delegate, options.InputFiles().at(1));
328 if (!new_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900329 return false;
330 }
Jiyong Parkf7534672019-08-12 22:06:22 +0900331
Jiyong Park0cf03b12020-07-22 19:36:34 +0900332 std::vector<AidlDefinedType*> old_types = old_tns->AllDefinedTypes();
333 std::vector<AidlDefinedType*> new_types = new_tns->AllDefinedTypes();
Jiyong Park3656c3c2018-08-01 20:02:01 +0900334
335 map<string, AidlDefinedType*> new_map;
336 for (const auto t : new_types) {
337 new_map.emplace(t->GetCanonicalName(), t);
338 }
339
340 bool compatible = true;
341 for (const auto old_type : old_types) {
342 const auto found = new_map.find(old_type->GetCanonicalName());
343 if (found == new_map.end()) {
344 AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName();
345 compatible = false;
346 continue;
347 }
348 const auto new_type = found->second;
349
Devin Mooredb7ac512020-08-07 11:17:36 -0700350 if (!have_compatible_annotations(*old_type, *new_type)) {
351 compatible = false;
352 }
Daniel Norman85aed542019-08-21 12:01:14 -0700353 if (old_type->AsInterface() != nullptr) {
354 if (new_type->AsInterface() == nullptr) {
355 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
356 << " is changed from " << old_type->GetPreprocessDeclarationName()
357 << " to " << new_type->GetPreprocessDeclarationName();
358 compatible = false;
359 continue;
360 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900361 compatible &=
362 are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface()));
Daniel Norman85aed542019-08-21 12:01:14 -0700363 } else if (old_type->AsStructuredParcelable() != nullptr) {
364 if (new_type->AsStructuredParcelable() == nullptr) {
365 AIDL_ERROR(new_type) << "Parcelable" << new_type->GetCanonicalName()
366 << " is not structured. ";
367 compatible = false;
368 continue;
369 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900370 compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()),
371 *(new_type->AsStructuredParcelable()));
Daniel Norman85aed542019-08-21 12:01:14 -0700372 } else if (old_type->AsEnumDeclaration() != nullptr) {
373 if (new_type->AsEnumDeclaration() == nullptr) {
374 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
375 << " is changed from " << old_type->GetPreprocessDeclarationName()
376 << " to " << new_type->GetPreprocessDeclarationName();
377 compatible = false;
378 continue;
379 }
380 compatible &=
381 are_compatible_enums(*(old_type->AsEnumDeclaration()), *(new_type->AsEnumDeclaration()));
382 } else {
383 AIDL_ERROR(old_type) << "Unsupported type " << old_type->GetPreprocessDeclarationName()
384 << " for " << old_type->GetCanonicalName();
385 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900386 }
387 }
388
389 return compatible;
390}
391
392} // namespace aidl
393} // namespace android