blob: 9473d46cecec5d17f750d66e40f42ec6a94866ec [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 }
195
196 bool compatible = true;
197 for (size_t i = 0; i < old_fields.size(); i++) {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900198 const auto& old_field = old_fields.at(i);
199 const auto& new_field = new_fields.at(i);
200 compatible &= are_compatible_types(old_field->GetType(), new_field->GetType());
Jiyong Park3656c3c2018-08-01 20:02:01 +0900201
Jiyong Parka468e2a2018-08-29 21:25:18 +0900202 const string old_value = old_field->ValueString(AidlConstantValueDecorator);
203 const string new_value = new_field->ValueString(AidlConstantValueDecorator);
204 if (old_value != new_value) {
Steven Moreland370ed342020-04-28 18:14:39 -0700205 AIDL_ERROR(new_field) << "Changed default value: " << old_value << " to " << new_value << ".";
206 compatible = false;
207 }
208 }
209
Jiyong Parkb07d9932020-05-15 12:56:54 +0900210 // Reordering of fields is an incompatible change.
211 for (size_t i = 0; i < new_fields.size(); i++) {
212 const auto& new_field = new_fields.at(i);
213 auto found = std::find_if(old_fields.begin(), old_fields.end(), [&new_field](const auto& f) {
214 return new_field->GetName() == f->GetName();
215 });
216 if (found != old_fields.end()) {
217 size_t old_index = std::distance(old_fields.begin(), found);
218 if (old_index != i) {
219 AIDL_ERROR(new_field) << "Reordered " << new_field->GetName() << " from " << old_index
220 << " to " << i << ".";
221 compatible = false;
222 }
223 }
224 }
225
Steven Moreland370ed342020-04-28 18:14:39 -0700226 for (size_t i = old_fields.size(); i < new_fields.size(); i++) {
227 const auto& new_field = new_fields.at(i);
228 if (!new_field->GetDefaultValue() && !has_usable_nil_type(new_field->GetType())) {
229 // Old API versions may suffer from the issue presented here. There is
230 // only a finite number in Android, which we must allow indefinitely.
231 struct HistoricalException {
232 std::string canonical;
233 std::string field;
234 };
235 static std::vector<HistoricalException> exceptions = {
Steven Moreland370ed342020-04-28 18:14:39 -0700236 {"android.net.DhcpResultsParcelable", "serverHostName"},
Lorenzo Colittib183a8b2020-05-01 11:31:33 +0900237 {"android.net.ResolverParamsParcel", "resolverOptions"},
Steven Moreland370ed342020-04-28 18:14:39 -0700238 };
239 bool excepted = false;
240 for (const HistoricalException& exception : exceptions) {
241 if (older.GetCanonicalName() == exception.canonical &&
242 new_field->GetName() == exception.field) {
243 excepted = true;
244 break;
245 }
246 }
247 if (excepted) continue;
248
249 AIDL_ERROR(new_field)
250 << "Field '" << new_field->GetName()
251 << "' does not have a useful default in some backends. Please either provide a default "
252 "value for this field or mark the field as @nullable. This value or a null value will "
253 "be used automatically when an old version of this parcelable is sent to a process "
254 "which understands a new version of this parcelable. In order to make sure your code "
255 "continues to be backwards compatible, make sure the default or null value does not "
256 "cause a semantic change to this parcelable.";
Jiyong Park3656c3c2018-08-01 20:02:01 +0900257 compatible = false;
258 }
259 }
260 return compatible;
261}
262
Daniel Norman85aed542019-08-21 12:01:14 -0700263static bool are_compatible_enums(const AidlEnumDeclaration& older,
264 const AidlEnumDeclaration& newer) {
265 if (!are_compatible_types(older.GetBackingType(), newer.GetBackingType())) {
266 AIDL_ERROR(newer) << "Changed backing types.";
267 return false;
268 }
269
270 std::map<std::string, const AidlConstantValue*> old_enum_map;
271 for (const auto& enumerator : older.GetEnumerators()) {
272 old_enum_map[enumerator->GetName()] = enumerator->GetValue();
273 }
274 std::map<std::string, const AidlConstantValue*> new_enum_map;
275 for (const auto& enumerator : newer.GetEnumerators()) {
276 new_enum_map[enumerator->GetName()] = enumerator->GetValue();
277 }
278
279 bool compatible = true;
280 for (const auto& [name, value] : old_enum_map) {
281 if (new_enum_map.find(name) == new_enum_map.end()) {
282 AIDL_ERROR(newer) << "Removed enumerator from " << older.GetCanonicalName() << ": " << name;
283 compatible = false;
284 continue;
285 }
286 const string old_value =
Will McVickerd7d18df2019-09-12 13:40:50 -0700287 old_enum_map[name]->ValueString(older.GetBackingType(), AidlConstantValueDecorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700288 const string new_value =
Will McVickerd7d18df2019-09-12 13:40:50 -0700289 new_enum_map[name]->ValueString(newer.GetBackingType(), AidlConstantValueDecorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700290 if (old_value != new_value) {
291 AIDL_ERROR(newer) << "Changed enumerator value: " << older.GetCanonicalName() << "::" << name
292 << " from " << old_value << " to " << new_value << ".";
293 compatible = false;
294 }
295 }
296 return compatible;
297}
298
Jiyong Park0cf03b12020-07-22 19:36:34 +0900299static Result<AidlTypenames> load_from_dir(const Options& options, const IoDelegate& io_delegate,
300 const std::string& dir) {
301 AidlTypenames typenames;
302 for (const auto& file : io_delegate.ListFiles(dir)) {
303 if (!android::base::EndsWith(file, ".aidl")) continue;
304 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames,
305 nullptr /* imported_files */) != AidlError::OK) {
306 AIDL_ERROR(file) << "Failed to read.";
307 return Error();
308 }
309 }
310 return typenames;
311}
312
Jiyong Park3656c3c2018-08-01 20:02:01 +0900313bool check_api(const Options& options, const IoDelegate& io_delegate) {
314 CHECK(options.IsStructured());
315 CHECK(options.InputFiles().size() == 2) << "--checkapi requires two inputs "
316 << "but got " << options.InputFiles().size();
Jiyong Park0cf03b12020-07-22 19:36:34 +0900317 auto old_tns = load_from_dir(options, io_delegate, options.InputFiles().at(0));
318 if (!old_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900319 return false;
320 }
Jiyong Park0cf03b12020-07-22 19:36:34 +0900321 auto new_tns = load_from_dir(options, io_delegate, options.InputFiles().at(1));
322 if (!new_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900323 return false;
324 }
Jiyong Parkf7534672019-08-12 22:06:22 +0900325
Jiyong Park0cf03b12020-07-22 19:36:34 +0900326 std::vector<AidlDefinedType*> old_types = old_tns->AllDefinedTypes();
327 std::vector<AidlDefinedType*> new_types = new_tns->AllDefinedTypes();
Jiyong Park3656c3c2018-08-01 20:02:01 +0900328
329 map<string, AidlDefinedType*> new_map;
330 for (const auto t : new_types) {
331 new_map.emplace(t->GetCanonicalName(), t);
332 }
333
334 bool compatible = true;
335 for (const auto old_type : old_types) {
336 const auto found = new_map.find(old_type->GetCanonicalName());
337 if (found == new_map.end()) {
338 AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName();
339 compatible = false;
340 continue;
341 }
342 const auto new_type = found->second;
343
Devin Mooredb7ac512020-08-07 11:17:36 -0700344 if (!have_compatible_annotations(*old_type, *new_type)) {
345 compatible = false;
346 }
Daniel Norman85aed542019-08-21 12:01:14 -0700347 if (old_type->AsInterface() != nullptr) {
348 if (new_type->AsInterface() == nullptr) {
349 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
350 << " is changed from " << old_type->GetPreprocessDeclarationName()
351 << " to " << new_type->GetPreprocessDeclarationName();
352 compatible = false;
353 continue;
354 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900355 compatible &=
356 are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface()));
Daniel Norman85aed542019-08-21 12:01:14 -0700357 } else if (old_type->AsStructuredParcelable() != nullptr) {
358 if (new_type->AsStructuredParcelable() == nullptr) {
359 AIDL_ERROR(new_type) << "Parcelable" << new_type->GetCanonicalName()
360 << " is not structured. ";
361 compatible = false;
362 continue;
363 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900364 compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()),
365 *(new_type->AsStructuredParcelable()));
Daniel Norman85aed542019-08-21 12:01:14 -0700366 } else if (old_type->AsEnumDeclaration() != nullptr) {
367 if (new_type->AsEnumDeclaration() == nullptr) {
368 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
369 << " is changed from " << old_type->GetPreprocessDeclarationName()
370 << " to " << new_type->GetPreprocessDeclarationName();
371 compatible = false;
372 continue;
373 }
374 compatible &=
375 are_compatible_enums(*(old_type->AsEnumDeclaration()), *(new_type->AsEnumDeclaration()));
376 } else {
377 AIDL_ERROR(old_type) << "Unsupported type " << old_type->GetPreprocessDeclarationName()
378 << " for " << old_type->GetCanonicalName();
379 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900380 }
381 }
382
383 return compatible;
384}
385
386} // namespace aidl
387} // namespace android