Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1 | /* |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 2 | * Copyright (C) 2018, The Android Open Source Project |
| 3 | * |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 4 | * 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" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 18 | |
| 19 | #include <map> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 23 | #include <android-base/result.h> |
Jiyong Park | f753467 | 2019-08-12 22:06:22 +0900 | [diff] [blame] | 24 | #include <android-base/strings.h> |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
Jiyong Park | f753467 | 2019-08-12 22:06:22 +0900 | [diff] [blame] | 26 | |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 27 | #include "aidl_dumpapi.h" |
| 28 | #include "aidl_language.h" |
| 29 | #include "import_resolver.h" |
| 30 | #include "logging.h" |
| 31 | #include "options.h" |
| 32 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace aidl { |
| 35 | |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 36 | using android::base::Error; |
| 37 | using android::base::Result; |
Jooyung Han | 0007327 | 2020-11-27 14:20:20 +0900 | [diff] [blame] | 38 | using android::base::StartsWith; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 39 | using std::map; |
| 40 | using std::set; |
| 41 | using std::string; |
| 42 | using std::vector; |
| 43 | |
Jooyung Han | 02a11be | 2021-02-11 13:18:06 +0900 | [diff] [blame] | 44 | struct DumpForEqualityVisitor : DumpVisitor { |
| 45 | DumpForEqualityVisitor(CodeWriter& out) : DumpVisitor(out) {} |
| 46 | |
| 47 | void DumpConstantValue(const AidlTypeSpecifier&, const AidlConstantValue& c) { |
| 48 | out << c.Literal(); |
| 49 | } |
| 50 | }; |
| 51 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 52 | static std::string Dump(const AidlDefinedType& type) { |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 53 | string code; |
| 54 | CodeWriterPtr out = CodeWriter::ForString(&code); |
Jooyung Han | 02a11be | 2021-02-11 13:18:06 +0900 | [diff] [blame] | 55 | DumpForEqualityVisitor visitor(*out); |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 56 | type.DispatchVisit(visitor); |
| 57 | out->Close(); |
| 58 | return code; |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Uses each type's Dump() and GTest utility(EqHelper). |
| 62 | static bool CheckEquality(const AidlDefinedType& older, const AidlDefinedType& newer) { |
| 63 | using testing::internal::EqHelper; |
| 64 | auto older_file = older.GetLocation().GetFile(); |
| 65 | auto newer_file = newer.GetLocation().GetFile(); |
| 66 | auto result = EqHelper::Compare(older_file.data(), newer_file.data(), Dump(older), Dump(newer)); |
| 67 | if (!result) { |
| 68 | AIDL_ERROR(newer) << result.failure_message(); |
| 69 | } |
| 70 | return result; |
| 71 | } |
| 72 | |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 73 | static vector<string> get_strict_annotations(const AidlAnnotatable& node) { |
Steven Moreland | 7b6a7d9 | 2020-04-20 22:00:33 -0700 | [diff] [blame] | 74 | // This must be symmetrical (if you can add something, you must be able to |
| 75 | // remove it). The reason is that we have no way of knowing which interface a |
| 76 | // server serves and which interface a client serves (e.g. a callback |
| 77 | // interface). Note that this is being overly lenient. It makes sense for |
| 78 | // newer code to start accepting nullable things. However, here, we don't know |
| 79 | // if the client of an interface or the server of an interface is newer. |
| 80 | // |
| 81 | // Here are two examples to demonstrate this: |
| 82 | // - a new implementation might change so that it no longer returns null |
| 83 | // values (remove @nullable) |
| 84 | // - a new implementation might start accepting null values (add @nullable) |
| 85 | static const set<AidlAnnotation::Type> kIgnoreAnnotations{ |
| 86 | AidlAnnotation::Type::NULLABLE, |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 87 | // @JavaDerive doesn't affect read/write |
Jooyung Han | 9034500 | 2020-10-23 15:28:53 +0900 | [diff] [blame] | 88 | AidlAnnotation::Type::JAVA_DERIVE, |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 89 | AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE, |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 90 | // @Backing for a enum type is checked by the enum checker |
| 91 | AidlAnnotation::Type::BACKING, |
| 92 | // @RustDerive doesn't affect read/write |
| 93 | AidlAnnotation::Type::RUST_DERIVE, |
Jooyung Han | f8dbbcc | 2020-12-26 03:05:55 +0900 | [diff] [blame] | 94 | AidlAnnotation::Type::SUPPRESS_WARNINGS, |
Steven Moreland | 7b6a7d9 | 2020-04-20 22:00:33 -0700 | [diff] [blame] | 95 | }; |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 96 | vector<string> annotations; |
Steven Moreland | 7b6a7d9 | 2020-04-20 22:00:33 -0700 | [diff] [blame] | 97 | for (const AidlAnnotation& annotation : node.GetAnnotations()) { |
Jooyung Han | 0007327 | 2020-11-27 14:20:20 +0900 | [diff] [blame] | 98 | if (kIgnoreAnnotations.find(annotation.GetType()) != kIgnoreAnnotations.end()) { |
| 99 | continue; |
Steven Moreland | 7b6a7d9 | 2020-04-20 22:00:33 -0700 | [diff] [blame] | 100 | } |
Jooyung Han | 0007327 | 2020-11-27 14:20:20 +0900 | [diff] [blame] | 101 | auto annotation_string = annotation.ToString(); |
| 102 | // adding @Deprecated (with optional args) is okay |
| 103 | if (StartsWith(annotation_string, "@JavaPassthrough(annotation=\"@Deprecated")) { |
| 104 | continue; |
| 105 | } |
| 106 | annotations.push_back(annotation_string); |
Steven Moreland | 7b6a7d9 | 2020-04-20 22:00:33 -0700 | [diff] [blame] | 107 | } |
| 108 | return annotations; |
| 109 | } |
| 110 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 111 | static bool have_compatible_annotations(const AidlAnnotatable& older, |
| 112 | const AidlAnnotatable& newer) { |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 113 | vector<string> olderAnnotations = get_strict_annotations(older); |
| 114 | vector<string> newerAnnotations = get_strict_annotations(newer); |
| 115 | sort(olderAnnotations.begin(), olderAnnotations.end()); |
| 116 | sort(newerAnnotations.begin(), newerAnnotations.end()); |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 117 | if (olderAnnotations != newerAnnotations) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 118 | const string from = older.ToString().empty() ? "(empty)" : older.ToString(); |
| 119 | const string to = newer.ToString().empty() ? "(empty)" : newer.ToString(); |
| 120 | AIDL_ERROR(newer) << "Changed annotations: " << from << " to " << to; |
| 121 | return false; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | static bool are_compatible_types(const AidlTypeSpecifier& older, const AidlTypeSpecifier& newer) { |
| 127 | bool compatible = true; |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 128 | if (older.Signature() != newer.Signature()) { |
| 129 | AIDL_ERROR(newer) << "Type changed: " << older.Signature() << " to " << newer.Signature() |
| 130 | << "."; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 131 | compatible = false; |
| 132 | } |
| 133 | compatible &= have_compatible_annotations(older, newer); |
| 134 | return compatible; |
| 135 | } |
| 136 | |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 137 | static bool are_compatible_constants(const AidlDefinedType& older, const AidlDefinedType& newer) { |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 138 | bool compatible = true; |
| 139 | |
| 140 | map<string, AidlConstantDeclaration*> new_constdecls; |
| 141 | for (const auto& c : newer.GetConstantDeclarations()) { |
| 142 | new_constdecls[c->GetName()] = &*c; |
| 143 | } |
| 144 | |
| 145 | for (const auto& old_c : older.GetConstantDeclarations()) { |
| 146 | const auto found = new_constdecls.find(old_c->GetName()); |
| 147 | if (found == new_constdecls.end()) { |
| 148 | AIDL_ERROR(old_c) << "Removed constant declaration: " << older.GetCanonicalName() << "." |
| 149 | << old_c->GetName(); |
| 150 | compatible = false; |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | const auto new_c = found->second; |
| 155 | compatible &= are_compatible_types(old_c->GetType(), new_c->GetType()); |
| 156 | |
Jooyung Han | fdaae1d | 2020-12-14 13:16:15 +0900 | [diff] [blame] | 157 | const string old_value = old_c->GetValue().Literal(); |
| 158 | const string new_value = new_c->GetValue().Literal(); |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 159 | if (old_value != new_value) { |
| 160 | AIDL_ERROR(newer) << "Changed constant value: " << older.GetCanonicalName() << "." |
| 161 | << old_c->GetName() << " from " << old_value << " to " << new_value << "."; |
| 162 | compatible = false; |
| 163 | } |
| 164 | } |
| 165 | return compatible; |
| 166 | } |
| 167 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 168 | static bool are_compatible_interfaces(const AidlInterface& older, const AidlInterface& newer) { |
| 169 | bool compatible = true; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 170 | |
| 171 | map<string, AidlMethod*> new_methods; |
| 172 | for (const auto& m : newer.AsInterface()->GetMethods()) { |
| 173 | new_methods.emplace(m->Signature(), m.get()); |
| 174 | } |
| 175 | |
| 176 | for (const auto& old_m : older.AsInterface()->GetMethods()) { |
| 177 | const auto found = new_methods.find(old_m->Signature()); |
| 178 | if (found == new_methods.end()) { |
Steven Moreland | 4ee6863 | 2018-12-14 15:52:46 -0800 | [diff] [blame] | 179 | AIDL_ERROR(old_m) << "Removed or changed method: " << older.GetCanonicalName() << "." |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 180 | << old_m->Signature(); |
| 181 | compatible = false; |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | // Compare IDs to detect method reordering. IDs are assigned by their |
| 186 | // textual order, so if there is an ID mismatch, that means reordering |
| 187 | // has happened. |
| 188 | const auto new_m = found->second; |
Steven Moreland | 4ee6863 | 2018-12-14 15:52:46 -0800 | [diff] [blame] | 189 | |
| 190 | if (old_m->IsOneway() != new_m->IsOneway()) { |
| 191 | AIDL_ERROR(new_m) << "Oneway attribute " << (old_m->IsOneway() ? "removed" : "added") << ": " |
| 192 | << older.GetCanonicalName() << "." << old_m->Signature(); |
| 193 | compatible = false; |
| 194 | } |
| 195 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 196 | if (old_m->GetId() != new_m->GetId()) { |
| 197 | AIDL_ERROR(new_m) << "Transaction ID changed: " << older.GetCanonicalName() << "." |
| 198 | << old_m->Signature() << " is changed from " << old_m->GetId() << " to " |
| 199 | << new_m->GetId() << "."; |
| 200 | compatible = false; |
| 201 | } |
| 202 | |
| 203 | compatible &= are_compatible_types(old_m->GetType(), new_m->GetType()); |
| 204 | |
| 205 | const auto& old_args = old_m->GetArguments(); |
| 206 | const auto& new_args = new_m->GetArguments(); |
| 207 | // this is guaranteed because arguments are part of AidlMethod::Signature() |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 208 | AIDL_FATAL_IF(old_args.size() != new_args.size(), old_m); |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 209 | for (size_t i = 0; i < old_args.size(); i++) { |
| 210 | const AidlArgument& old_a = *(old_args.at(i)); |
| 211 | const AidlArgument& new_a = *(new_args.at(i)); |
| 212 | compatible &= are_compatible_types(old_a.GetType(), new_a.GetType()); |
| 213 | |
| 214 | if (old_a.GetDirection() != new_a.GetDirection()) { |
| 215 | AIDL_ERROR(new_m) << "Direction changed: " << old_a.GetDirectionSpecifier() << " to " |
| 216 | << new_a.GetDirectionSpecifier() << "."; |
| 217 | compatible = false; |
| 218 | } |
| 219 | } |
| 220 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 221 | |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 222 | compatible = are_compatible_constants(older, newer) && compatible; |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 223 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 224 | return compatible; |
| 225 | } |
| 226 | |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 227 | static bool HasZeroEnumerator(const AidlEnumDeclaration& enum_decl) { |
| 228 | return std::any_of(enum_decl.GetEnumerators().begin(), enum_decl.GetEnumerators().end(), |
| 229 | [&](const unique_ptr<AidlEnumerator>& enumerator) { |
Jooyung Han | fdaae1d | 2020-12-14 13:16:15 +0900 | [diff] [blame] | 230 | return enumerator->GetValue()->Literal() == "0"; |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 231 | }); |
| 232 | } |
| 233 | |
Jooyung Han | 308bbe0 | 2021-04-27 12:09:59 +0900 | [diff] [blame] | 234 | static bool EvaluatesToZero(const AidlEnumDeclaration& enum_decl, const std::string& value) { |
| 235 | if (value == "") return true; |
| 236 | // Because --check_api runs with "valid" AIDL definitions, we can safely assume that |
| 237 | // the value is formatted as <scope>.<enumerator>. |
| 238 | auto enumerator_name = value.substr(value.find_last_of('.') + 1); |
| 239 | for (const auto& enumerator : enum_decl.GetEnumerators()) { |
| 240 | if (enumerator->GetName() == enumerator_name) { |
| 241 | return enumerator->GetValue()->Literal() == "0"; |
| 242 | } |
| 243 | } |
| 244 | AIDL_FATAL(enum_decl) << "Can't find " << enumerator_name << " in " << enum_decl.GetName(); |
| 245 | } |
| 246 | |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 247 | static bool are_compatible_parcelables(const AidlDefinedType& older, const AidlTypenames&, |
| 248 | const AidlDefinedType& newer, |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 249 | const AidlTypenames& new_types) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 250 | const auto& old_fields = older.GetFields(); |
| 251 | const auto& new_fields = newer.GetFields(); |
| 252 | if (old_fields.size() > new_fields.size()) { |
| 253 | // you can add new fields only at the end |
| 254 | AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from " |
| 255 | << old_fields.size() << " to " << new_fields.size() << "."; |
| 256 | return false; |
| 257 | } |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 258 | if (newer.IsFixedSize() && old_fields.size() != new_fields.size()) { |
| 259 | AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is changed from " |
| 260 | << old_fields.size() << " to " << new_fields.size() |
| 261 | << ". This is an incompatible change for FixedSize types."; |
| 262 | return false; |
| 263 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 264 | |
Jeongik Cha | 68d18e7 | 2021-04-29 20:07:01 +0900 | [diff] [blame] | 265 | // android.net.UidRangeParcel should be frozen to prevent breakage in legacy (b/186720556) |
| 266 | if (older.GetCanonicalName() == "android.net.UidRangeParcel" && |
| 267 | old_fields.size() != new_fields.size()) { |
| 268 | AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is changed from " |
| 269 | << old_fields.size() << " to " << new_fields.size() |
| 270 | << ". But it is forbidden because of legacy support."; |
| 271 | return false; |
| 272 | } |
| 273 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 274 | bool compatible = true; |
| 275 | for (size_t i = 0; i < old_fields.size(); i++) { |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 276 | const auto& old_field = old_fields.at(i); |
| 277 | const auto& new_field = new_fields.at(i); |
| 278 | compatible &= are_compatible_types(old_field->GetType(), new_field->GetType()); |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 279 | |
Jooyung Han | fdaae1d | 2020-12-14 13:16:15 +0900 | [diff] [blame] | 280 | string old_value = old_field->GetDefaultValue() ? old_field->GetDefaultValue()->Literal() : ""; |
| 281 | string new_value = new_field->GetDefaultValue() ? new_field->GetDefaultValue()->Literal() : ""; |
Jooyung Han | 308bbe0 | 2021-04-27 12:09:59 +0900 | [diff] [blame] | 282 | |
| 283 | if (old_value == new_value) { |
| 284 | continue; |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 285 | } |
Jooyung Han | 308bbe0 | 2021-04-27 12:09:59 +0900 | [diff] [blame] | 286 | // For enum type fields, we accept setting explicit default value which is "zero" |
| 287 | auto enum_decl = new_types.GetEnumDeclaration(new_field->GetType()); |
| 288 | if (old_value == "" && enum_decl && EvaluatesToZero(*enum_decl, new_value)) { |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | AIDL_ERROR(new_field) << "Changed default value: " << old_value << " to " << new_value << "."; |
| 293 | compatible = false; |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Jiyong Park | b07d993 | 2020-05-15 12:56:54 +0900 | [diff] [blame] | 296 | // Reordering of fields is an incompatible change. |
| 297 | for (size_t i = 0; i < new_fields.size(); i++) { |
| 298 | const auto& new_field = new_fields.at(i); |
| 299 | auto found = std::find_if(old_fields.begin(), old_fields.end(), [&new_field](const auto& f) { |
| 300 | return new_field->GetName() == f->GetName(); |
| 301 | }); |
| 302 | if (found != old_fields.end()) { |
| 303 | size_t old_index = std::distance(old_fields.begin(), found); |
| 304 | if (old_index != i) { |
| 305 | AIDL_ERROR(new_field) << "Reordered " << new_field->GetName() << " from " << old_index |
| 306 | << " to " << i << "."; |
| 307 | compatible = false; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 312 | for (size_t i = old_fields.size(); i < new_fields.size(); i++) { |
| 313 | const auto& new_field = new_fields.at(i); |
Jooyung Han | 53fb424 | 2020-12-17 16:03:49 +0900 | [diff] [blame] | 314 | if (new_field->HasUsefulDefaultValue()) { |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 315 | continue; |
| 316 | } |
| 317 | |
| 318 | // enum can't be nullable, but it's okay if it has 0 as a valid enumerator. |
| 319 | if (const auto& enum_decl = new_types.GetEnumDeclaration(new_field->GetType()); |
| 320 | enum_decl != nullptr) { |
| 321 | if (HasZeroEnumerator(*enum_decl)) { |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | // TODO(b/142893595): Rephrase the message: "provide a default value or make sure ..." |
| 326 | AIDL_ERROR(new_field) << "Field '" << new_field->GetName() << "' of enum '" |
| 327 | << enum_decl->GetName() |
| 328 | << "' can't be initialized as '0'. Please make sure '" |
| 329 | << enum_decl->GetName() << "' has '0' as a valid value."; |
| 330 | compatible = false; |
| 331 | continue; |
| 332 | } |
| 333 | |
| 334 | // Old API versions may suffer from the issue presented here. There is |
| 335 | // only a finite number in Android, which we must allow indefinitely. |
| 336 | struct HistoricalException { |
| 337 | std::string canonical; |
| 338 | std::string field; |
| 339 | }; |
| 340 | static std::vector<HistoricalException> exceptions = { |
| 341 | {"android.net.DhcpResultsParcelable", "serverHostName"}, |
| 342 | {"android.net.ResolverParamsParcel", "resolverOptions"}, |
| 343 | }; |
| 344 | bool excepted = false; |
| 345 | for (const HistoricalException& exception : exceptions) { |
| 346 | if (older.GetCanonicalName() == exception.canonical && |
| 347 | new_field->GetName() == exception.field) { |
| 348 | excepted = true; |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | if (excepted) continue; |
| 353 | |
| 354 | AIDL_ERROR(new_field) |
| 355 | << "Field '" << new_field->GetName() |
| 356 | << "' does not have a useful default in some backends. Please either provide a default " |
| 357 | "value for this field or mark the field as @nullable. This value or a null value will " |
| 358 | "be used automatically when an old version of this parcelable is sent to a process " |
| 359 | "which understands a new version of this parcelable. In order to make sure your code " |
| 360 | "continues to be backwards compatible, make sure the default or null value does not " |
| 361 | "cause a semantic change to this parcelable."; |
| 362 | compatible = false; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 363 | } |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 364 | |
| 365 | compatible = are_compatible_constants(older, newer) && compatible; |
| 366 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 367 | return compatible; |
| 368 | } |
| 369 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 370 | static bool are_compatible_enums(const AidlEnumDeclaration& older, |
| 371 | const AidlEnumDeclaration& newer) { |
| 372 | if (!are_compatible_types(older.GetBackingType(), newer.GetBackingType())) { |
| 373 | AIDL_ERROR(newer) << "Changed backing types."; |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | std::map<std::string, const AidlConstantValue*> old_enum_map; |
| 378 | for (const auto& enumerator : older.GetEnumerators()) { |
| 379 | old_enum_map[enumerator->GetName()] = enumerator->GetValue(); |
| 380 | } |
| 381 | std::map<std::string, const AidlConstantValue*> new_enum_map; |
| 382 | for (const auto& enumerator : newer.GetEnumerators()) { |
| 383 | new_enum_map[enumerator->GetName()] = enumerator->GetValue(); |
| 384 | } |
| 385 | |
| 386 | bool compatible = true; |
| 387 | for (const auto& [name, value] : old_enum_map) { |
| 388 | if (new_enum_map.find(name) == new_enum_map.end()) { |
| 389 | AIDL_ERROR(newer) << "Removed enumerator from " << older.GetCanonicalName() << ": " << name; |
| 390 | compatible = false; |
| 391 | continue; |
| 392 | } |
Jooyung Han | fdaae1d | 2020-12-14 13:16:15 +0900 | [diff] [blame] | 393 | const string old_value = old_enum_map[name]->Literal(); |
| 394 | const string new_value = new_enum_map[name]->Literal(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 395 | if (old_value != new_value) { |
| 396 | AIDL_ERROR(newer) << "Changed enumerator value: " << older.GetCanonicalName() << "::" << name |
| 397 | << " from " << old_value << " to " << new_value << "."; |
| 398 | compatible = false; |
| 399 | } |
| 400 | } |
| 401 | return compatible; |
| 402 | } |
| 403 | |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 404 | static Result<AidlTypenames> load_from_dir(const Options& options, const IoDelegate& io_delegate, |
| 405 | const std::string& dir) { |
Steven Moreland | 6a945f3 | 2021-02-18 00:25:36 +0000 | [diff] [blame] | 406 | Result<std::vector<std::string>> dir_files = io_delegate.ListFiles(dir); |
| 407 | if (!dir_files.ok()) { |
| 408 | AIDL_ERROR(dir) << dir_files.error(); |
| 409 | return Error(); |
| 410 | } |
| 411 | |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 412 | AidlTypenames typenames; |
Steven Moreland | 6a945f3 | 2021-02-18 00:25:36 +0000 | [diff] [blame] | 413 | for (const auto& file : *dir_files) { |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 414 | if (!android::base::EndsWith(file, ".aidl")) continue; |
| 415 | if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, |
| 416 | nullptr /* imported_files */) != AidlError::OK) { |
| 417 | AIDL_ERROR(file) << "Failed to read."; |
| 418 | return Error(); |
| 419 | } |
| 420 | } |
Steven Moreland | 6a945f3 | 2021-02-18 00:25:36 +0000 | [diff] [blame] | 421 | |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 422 | return typenames; |
| 423 | } |
| 424 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 425 | bool check_api(const Options& options, const IoDelegate& io_delegate) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 426 | AIDL_FATAL_IF(!options.IsStructured(), AIDL_LOCATION_HERE); |
| 427 | AIDL_FATAL_IF(options.InputFiles().size() != 2, AIDL_LOCATION_HERE) |
| 428 | << "--checkapi requires two inputs " |
| 429 | << "but got " << options.InputFiles().size(); |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 430 | auto old_tns = load_from_dir(options, io_delegate, options.InputFiles().at(0)); |
| 431 | if (!old_tns.ok()) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 432 | return false; |
| 433 | } |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 434 | auto new_tns = load_from_dir(options, io_delegate, options.InputFiles().at(1)); |
| 435 | if (!new_tns.ok()) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 436 | return false; |
| 437 | } |
Jiyong Park | f753467 | 2019-08-12 22:06:22 +0900 | [diff] [blame] | 438 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 439 | const Options::CheckApiLevel level = options.GetCheckApiLevel(); |
| 440 | |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 441 | std::vector<AidlDefinedType*> old_types = old_tns->AllDefinedTypes(); |
| 442 | std::vector<AidlDefinedType*> new_types = new_tns->AllDefinedTypes(); |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 443 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 444 | bool compatible = true; |
| 445 | |
| 446 | if (level == Options::CheckApiLevel::EQUAL) { |
| 447 | std::set<string> old_type_names; |
| 448 | for (const auto t : old_types) { |
| 449 | old_type_names.insert(t->GetCanonicalName()); |
| 450 | } |
| 451 | for (const auto new_type : new_types) { |
| 452 | const auto found = old_type_names.find(new_type->GetCanonicalName()); |
| 453 | if (found == old_type_names.end()) { |
| 454 | AIDL_ERROR(new_type) << "Added type: " << new_type->GetCanonicalName(); |
| 455 | compatible = false; |
| 456 | continue; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 461 | map<string, AidlDefinedType*> new_map; |
| 462 | for (const auto t : new_types) { |
| 463 | new_map.emplace(t->GetCanonicalName(), t); |
| 464 | } |
| 465 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 466 | for (const auto old_type : old_types) { |
| 467 | const auto found = new_map.find(old_type->GetCanonicalName()); |
| 468 | if (found == new_map.end()) { |
| 469 | AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName(); |
| 470 | compatible = false; |
| 471 | continue; |
| 472 | } |
| 473 | const auto new_type = found->second; |
| 474 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 475 | if (level == Options::CheckApiLevel::EQUAL) { |
| 476 | if (!CheckEquality(*old_type, *new_type)) { |
| 477 | compatible = false; |
| 478 | } |
| 479 | continue; |
| 480 | } |
| 481 | |
Devin Moore | db7ac51 | 2020-08-07 11:17:36 -0700 | [diff] [blame] | 482 | if (!have_compatible_annotations(*old_type, *new_type)) { |
| 483 | compatible = false; |
| 484 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 485 | if (old_type->AsInterface() != nullptr) { |
| 486 | if (new_type->AsInterface() == nullptr) { |
| 487 | AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName() |
| 488 | << " is changed from " << old_type->GetPreprocessDeclarationName() |
| 489 | << " to " << new_type->GetPreprocessDeclarationName(); |
| 490 | compatible = false; |
| 491 | continue; |
| 492 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 493 | compatible &= |
| 494 | are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface())); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 495 | } else if (old_type->AsStructuredParcelable() != nullptr) { |
| 496 | if (new_type->AsStructuredParcelable() == nullptr) { |
| 497 | AIDL_ERROR(new_type) << "Parcelable" << new_type->GetCanonicalName() |
| 498 | << " is not structured. "; |
| 499 | compatible = false; |
| 500 | continue; |
| 501 | } |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 502 | compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()), *old_tns, |
| 503 | *(new_type->AsStructuredParcelable()), *new_tns); |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 504 | } else if (old_type->AsUnionDeclaration() != nullptr) { |
| 505 | if (new_type->AsUnionDeclaration() == nullptr) { |
| 506 | AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName() |
| 507 | << " is changed from " << old_type->GetPreprocessDeclarationName() |
| 508 | << " to " << new_type->GetPreprocessDeclarationName(); |
| 509 | compatible = false; |
| 510 | continue; |
| 511 | } |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 512 | compatible &= are_compatible_parcelables(*(old_type->AsUnionDeclaration()), *old_tns, |
| 513 | *(new_type->AsUnionDeclaration()), *new_tns); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 514 | } else if (old_type->AsEnumDeclaration() != nullptr) { |
| 515 | if (new_type->AsEnumDeclaration() == nullptr) { |
| 516 | AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName() |
| 517 | << " is changed from " << old_type->GetPreprocessDeclarationName() |
| 518 | << " to " << new_type->GetPreprocessDeclarationName(); |
| 519 | compatible = false; |
| 520 | continue; |
| 521 | } |
| 522 | compatible &= |
| 523 | are_compatible_enums(*(old_type->AsEnumDeclaration()), *(new_type->AsEnumDeclaration())); |
| 524 | } else { |
| 525 | AIDL_ERROR(old_type) << "Unsupported type " << old_type->GetPreprocessDeclarationName() |
| 526 | << " for " << old_type->GetCanonicalName(); |
| 527 | compatible = false; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | return compatible; |
| 532 | } |
| 533 | |
| 534 | } // namespace aidl |
| 535 | } // namespace android |