• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018, The Android Open Source Project *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "aidl.h"
17 #include "aidl_language.h"
18 #include "import_resolver.h"
19 #include "options.h"
20 #include "type_java.h"
21 
22 #include <map>
23 #include <string>
24 #include <vector>
25 
26 namespace android {
27 namespace aidl {
28 
29 using std::map;
30 using std::set;
31 using std::string;
32 using std::vector;
33 
have_compatible_annotations(const AidlAnnotatable & older,const AidlAnnotatable & newer)34 static bool have_compatible_annotations(const AidlAnnotatable& older,
35                                         const AidlAnnotatable& newer) {
36   set<AidlAnnotation> olderAnnotations(older.GetAnnotations().begin(),
37                                        older.GetAnnotations().end());
38   set<AidlAnnotation> newerAnnotations(newer.GetAnnotations().begin(),
39                                        newer.GetAnnotations().end());
40   if (olderAnnotations != newerAnnotations) {
41     const string from = older.ToString().empty() ? "(empty)" : older.ToString();
42     const string to = newer.ToString().empty() ? "(empty)" : newer.ToString();
43     AIDL_ERROR(newer) << "Changed annotations: " << from << " to " << to;
44     return false;
45   }
46   return true;
47 }
48 
are_compatible_types(const AidlTypeSpecifier & older,const AidlTypeSpecifier & newer)49 static bool are_compatible_types(const AidlTypeSpecifier& older, const AidlTypeSpecifier& newer) {
50   bool compatible = true;
51   if (older.ToString() != newer.ToString()) {
52     AIDL_ERROR(newer) << "Type changed: " << older.ToString() << " to " << newer.ToString() << ".";
53     compatible = false;
54   }
55   compatible &= have_compatible_annotations(older, newer);
56   return compatible;
57 }
58 
are_compatible_interfaces(const AidlInterface & older,const AidlInterface & newer)59 static bool are_compatible_interfaces(const AidlInterface& older, const AidlInterface& newer) {
60   bool compatible = true;
61   compatible &= have_compatible_annotations(older, newer);
62 
63   map<string, AidlMethod*> new_methods;
64   for (const auto& m : newer.AsInterface()->GetMethods()) {
65     new_methods.emplace(m->Signature(), m.get());
66   }
67 
68   for (const auto& old_m : older.AsInterface()->GetMethods()) {
69     const auto found = new_methods.find(old_m->Signature());
70     if (found == new_methods.end()) {
71       AIDL_ERROR(old_m) << "Removed or changed method: " << older.GetCanonicalName() << "."
72                         << old_m->Signature();
73       compatible = false;
74       continue;
75     }
76 
77     // Compare IDs to detect method reordering. IDs are assigned by their
78     // textual order, so if there is an ID mismatch, that means reordering
79     // has happened.
80     const auto new_m = found->second;
81 
82     if (old_m->IsOneway() != new_m->IsOneway()) {
83       AIDL_ERROR(new_m) << "Oneway attribute " << (old_m->IsOneway() ? "removed" : "added") << ": "
84                         << older.GetCanonicalName() << "." << old_m->Signature();
85       compatible = false;
86     }
87 
88     if (old_m->GetId() != new_m->GetId()) {
89       AIDL_ERROR(new_m) << "Transaction ID changed: " << older.GetCanonicalName() << "."
90                         << old_m->Signature() << " is changed from " << old_m->GetId() << " to "
91                         << new_m->GetId() << ".";
92       compatible = false;
93     }
94 
95     compatible &= are_compatible_types(old_m->GetType(), new_m->GetType());
96 
97     const auto& old_args = old_m->GetArguments();
98     const auto& new_args = new_m->GetArguments();
99     // this is guaranteed because arguments are part of AidlMethod::Signature()
100     CHECK(old_args.size() == new_args.size());
101     for (size_t i = 0; i < old_args.size(); i++) {
102       const AidlArgument& old_a = *(old_args.at(i));
103       const AidlArgument& new_a = *(new_args.at(i));
104       compatible &= are_compatible_types(old_a.GetType(), new_a.GetType());
105 
106       if (old_a.GetDirection() != new_a.GetDirection()) {
107         AIDL_ERROR(new_m) << "Direction changed: " << old_a.GetDirectionSpecifier() << " to "
108                           << new_a.GetDirectionSpecifier() << ".";
109         compatible = false;
110       }
111     }
112   }
113 
114   map<string, AidlConstantDeclaration*> new_constdecls;
115   for (const auto& c : newer.AsInterface()->GetConstantDeclarations()) {
116     new_constdecls.emplace(c->GetName(), c.get());
117   }
118 
119   for (const auto& old_c : older.AsInterface()->GetConstantDeclarations()) {
120     const auto found = new_constdecls.find(old_c->GetName());
121     if (found == new_constdecls.end()) {
122       AIDL_ERROR(old_c) << "Removed constant declaration: " << older.GetCanonicalName() << "."
123                         << old_c->GetName();
124       compatible = false;
125       continue;
126     }
127 
128     const auto new_c = found->second;
129     compatible &= are_compatible_types(old_c->GetType(), new_c->GetType());
130 
131     const string old_value = old_c->ValueString(AidlConstantValueDecorator);
132     const string new_value = new_c->ValueString(AidlConstantValueDecorator);
133     if (old_value != new_value) {
134       AIDL_ERROR(newer) << "Changed constant value: " << older.GetCanonicalName() << "."
135                         << old_c->GetName() << " from " << old_value << " to " << new_value << ".";
136       compatible = false;
137     }
138   }
139   return compatible;
140 }
141 
are_compatible_parcelables(const AidlStructuredParcelable & older,const AidlStructuredParcelable & newer)142 static bool are_compatible_parcelables(const AidlStructuredParcelable& older,
143                                        const AidlStructuredParcelable& newer) {
144   const auto& old_fields = older.GetFields();
145   const auto& new_fields = newer.GetFields();
146   if (old_fields.size() > new_fields.size()) {
147     // you can add new fields only at the end
148     AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from "
149                       << old_fields.size() << " to " << new_fields.size() << ".";
150     return false;
151   }
152 
153   bool compatible = true;
154   for (size_t i = 0; i < old_fields.size(); i++) {
155     const auto& old_field = old_fields.at(i);
156     const auto& new_field = new_fields.at(i);
157     compatible &= are_compatible_types(old_field->GetType(), new_field->GetType());
158 
159     // Note: unlike method argument names, field name change is an incompatible
160     // change, otherwise, we can't detect
161     // parcelable Point {int x; int y;} -> parcelable Point {int y; int x;}
162     if (old_field->GetName() != new_field->GetName()) {
163       AIDL_ERROR(newer) << "Renamed field: " << old_field->GetName() << " to "
164                         << new_field->GetName() << ".";
165       compatible = false;
166     }
167 
168     const string old_value = old_field->ValueString(AidlConstantValueDecorator);
169     const string new_value = new_field->ValueString(AidlConstantValueDecorator);
170     if (old_value != new_value) {
171       AIDL_ERROR(newer) << "Changed default value: " << old_value << " to " << new_value << ".";
172       compatible = false;
173     }
174   }
175   return compatible;
176 }
177 
check_api(const Options & options,const IoDelegate & io_delegate)178 bool check_api(const Options& options, const IoDelegate& io_delegate) {
179   CHECK(options.IsStructured());
180   CHECK(options.InputFiles().size() == 2) << "--checkapi requires two inputs "
181                                           << "but got " << options.InputFiles().size();
182 
183   java::JavaTypeNamespace old_ns;
184   old_ns.Init();
185   const string old_dir = options.InputFiles().at(0);
186   vector<AidlDefinedType*> old_types;
187   vector<string> old_files = io_delegate.ListFiles(old_dir);
188   if (old_files.size() == 0) {
189     AIDL_ERROR(old_dir) << "No API file exist";
190     return false;
191   }
192   for (const auto& file : old_files) {
193     vector<AidlDefinedType*> types;
194     if (internals::load_and_validate_aidl(file, options, io_delegate, &old_ns, &types,
195                                           nullptr /* imported_files */) != AidlError::OK) {
196       AIDL_ERROR(file) << "Failed to read.";
197       return false;
198     }
199     old_types.insert(old_types.end(), types.begin(), types.end());
200   }
201 
202   java::JavaTypeNamespace new_ns;
203   new_ns.Init();
204   const string new_dir = options.InputFiles().at(1);
205   vector<AidlDefinedType*> new_types;
206   vector<string> new_files = io_delegate.ListFiles(new_dir);
207   if (new_files.size() == 0) {
208     AIDL_ERROR(new_dir) << "No API file exist";
209     return false;
210   }
211   for (const auto& file : new_files) {
212     vector<AidlDefinedType*> types;
213     if (internals::load_and_validate_aidl(file, options, io_delegate, &new_ns, &types,
214                                           nullptr /* imported_files */) != AidlError::OK) {
215       AIDL_ERROR(file) << "Failed to read.";
216       return false;
217     }
218     new_types.insert(new_types.end(), types.begin(), types.end());
219   }
220 
221   map<string, AidlDefinedType*> new_map;
222   for (const auto t : new_types) {
223     new_map.emplace(t->GetCanonicalName(), t);
224   }
225 
226   bool compatible = true;
227   for (const auto old_type : old_types) {
228     const auto found = new_map.find(old_type->GetCanonicalName());
229     if (found == new_map.end()) {
230       AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName();
231       compatible = false;
232       continue;
233     }
234     const auto new_type = found->second;
235 
236     const bool old_is_iface = old_type->AsInterface() != nullptr;
237     const bool new_is_iface = new_type->AsInterface() != nullptr;
238     if (old_is_iface != new_is_iface) {
239       AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
240                            << " is changed from " << old_type->GetPreprocessDeclarationName()
241                            << " to " << new_type->GetPreprocessDeclarationName();
242       compatible = false;
243       continue;
244     }
245 
246     if (old_is_iface) {
247       compatible &=
248           are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface()));
249     } else {
250       CHECK(old_type->AsStructuredParcelable() != nullptr)
251           << "Parcelable" << old_type->GetCanonicalName() << " is not structured. ";
252       CHECK(new_type->AsStructuredParcelable() != nullptr)
253           << "Parcelable" << new_type->GetCanonicalName() << " is not structured. ";
254       compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()),
255                                                *(new_type->AsStructuredParcelable()));
256     }
257   }
258 
259   return compatible;
260 }
261 
262 }  // namespace aidl
263 }  // namespace android
264