blob: 6a5645c4d07f40089f28e30edbe054835a14a1af [file] [log] [blame]
Yifan Hong676447a2016-11-15 12:57:23 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
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 "CompatibilityMatrix.h"
18
Yifan Hongb6c7f492018-02-27 14:07:57 -080019#include <iostream>
Yifan Hongdbe9db32017-12-11 19:06:11 -080020#include <utility>
21
Yifan Hongbbfff302017-06-06 17:10:13 -070022#include "parse_string.h"
Yifan Hongddae77e2017-12-18 16:57:07 -080023#include "parse_xml.h"
Yifan Hongb6c7f492018-02-27 14:07:57 -080024#include "utils.h"
Yifan Hongddae77e2017-12-18 16:57:07 -080025
Yifan Hong676447a2016-11-15 12:57:23 -080026namespace android {
27namespace vintf {
28
Yifan Hong676447a2016-11-15 12:57:23 -080029bool CompatibilityMatrix::add(MatrixHal &&hal) {
Yifan Hong0fd7aef2017-05-24 14:37:19 -070030 return HalGroup<MatrixHal>::add(std::move(hal));
Yifan Hong676447a2016-11-15 12:57:23 -080031}
32
33bool CompatibilityMatrix::add(MatrixKernel &&kernel) {
Yifan Hong7c7d7062017-04-04 16:26:51 -070034 if (mType != SchemaType::FRAMEWORK) {
35 return false;
36 }
37 framework.mKernels.push_back(std::move(kernel));
Yifan Hong676447a2016-11-15 12:57:23 -080038 return true;
39}
40
Yifan Hong398f4c72017-04-13 20:18:01 -070041SchemaType CompatibilityMatrix::type() const {
42 return mType;
43}
44
Yifan Hong2027a492017-12-11 15:21:19 -080045Level CompatibilityMatrix::level() const {
46 return mLevel;
47}
48
Yifan Hongdb127cb2017-09-19 13:36:21 -070049Version CompatibilityMatrix::getMinimumMetaVersion() const {
50 // TODO(b/62801658): this needs to depend on whether there are 1.1 requirements
51 // (e.g. required <xmlfile> entry)
52 return {1, 0};
53}
Yifan Hong1e5a0542017-04-28 14:37:56 -070054
Yifan Hong9f78c182018-07-12 14:45:52 -070055status_t CompatibilityMatrix::fetchAllInformation(const FileSystem* fileSystem,
56 const std::string& path, std::string* error) {
57 return details::fetchAllInformation(fileSystem, path, gCompatibilityMatrixConverter, this,
58 error);
Yifan Hong1e5a0542017-04-28 14:37:56 -070059}
60
Yifan Hongbbfff302017-06-06 17:10:13 -070061std::string CompatibilityMatrix::getXmlSchemaPath(const std::string& xmlFileName,
62 const Version& version) const {
63 using std::literals::string_literals::operator""s;
64 auto range = getXmlFiles(xmlFileName);
65 for (auto it = range.first; it != range.second; ++it) {
66 const MatrixXmlFile& matrixXmlFile = it->second;
67 if (matrixXmlFile.versionRange().contains(version)) {
68 if (!matrixXmlFile.overriddenPath().empty()) {
69 return matrixXmlFile.overriddenPath();
70 }
71 return "/"s + (type() == SchemaType::DEVICE ? "vendor" : "system") + "/etc/" +
72 xmlFileName + "_V" + std::to_string(matrixXmlFile.versionRange().majorVer) +
73 "_" + std::to_string(matrixXmlFile.versionRange().maxMinor) + "." +
74 to_string(matrixXmlFile.format());
75 }
76 }
77 return "";
78}
79
Yifan Honge7e45532018-03-16 18:11:49 -070080// Split existingHal into a HAL that contains only interface/instance and a HAL
81// that does not contain it. Return the HAL that contains only interface/instance.
82// - Return nullptr if existingHal does not contain interface/instance
83// - Return existingHal if existingHal contains only interface/instance
84// - Remove interface/instance from existingHal, and return a new MatrixHal (that is added
85// to "this") that contains only interface/instance.
86MatrixHal* CompatibilityMatrix::splitInstance(MatrixHal* existingHal, const std::string& interface,
Yifan Hong643a9ef2018-03-21 14:13:55 -070087 const std::string& instanceOrPattern, bool isRegex) {
88 bool found = false;
89 bool foundOthers = false;
90 existingHal->forEachInstance([&](const auto& matrixInstance) {
91 bool interfaceMatch = matrixInstance.interface() == interface;
92 bool instanceMatch = false;
93 if (matrixInstance.isRegex() && isRegex) {
94 instanceMatch = (matrixInstance.regexPattern() == instanceOrPattern);
95 } else if (!matrixInstance.isRegex() && !isRegex) {
96 instanceMatch = (matrixInstance.exactInstance() == instanceOrPattern);
97 }
98
99 bool match = interfaceMatch && instanceMatch;
100
101 found |= match;
102 foundOthers |= (!match);
103
104 return !found || !foundOthers;
105 });
106
107 if (!found) {
Yifan Honge7e45532018-03-16 18:11:49 -0700108 return nullptr;
109 }
110
Yifan Hong643a9ef2018-03-21 14:13:55 -0700111 if (!foundOthers) {
Yifan Honge7e45532018-03-16 18:11:49 -0700112 return existingHal;
113 }
114
Yifan Hong643a9ef2018-03-21 14:13:55 -0700115 existingHal->removeInstance(interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700116 MatrixHal copy = *existingHal;
117 copy.clearInstances();
Yifan Hong643a9ef2018-03-21 14:13:55 -0700118 copy.insertInstance(interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700119
120 return addInternal(std::move(copy));
121}
122
Yifan Hong7967d7b2018-03-15 17:08:58 -0700123// Add all package@other_version::interface/instance as an optional instance.
124// If package@this_version::interface/instance is in this (that is, some instance
125// with the same package and interface and instance exists), then other_version is
126// considered a possible replacement to this_version.
127// See LibVintfTest.AddOptionalHal* tests for details.
Yifan Hongdbe9db32017-12-11 19:06:11 -0800128bool CompatibilityMatrix::addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error) {
129 if (other == nullptr || other->level() <= level()) {
130 return true;
131 }
132
133 for (auto& pair : other->mHals) {
134 const std::string& name = pair.first;
135 MatrixHal& halToAdd = pair.second;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800136
Yifan Honge7e45532018-03-16 18:11:49 -0700137 std::set<std::pair<std::string, std::string>> insertedInstances;
Yifan Hong643a9ef2018-03-21 14:13:55 -0700138 std::set<std::pair<std::string, std::string>> insertedRegex;
Yifan Honge7e45532018-03-16 18:11:49 -0700139 auto existingHals = getHals(name);
140
141 halToAdd.forEachInstance([&](const std::vector<VersionRange>& versionRanges,
Yifan Hong643a9ef2018-03-21 14:13:55 -0700142 const std::string& interface,
143 const std::string& instanceOrPattern, bool isRegex) {
Yifan Honge7e45532018-03-16 18:11:49 -0700144 for (auto* existingHal : existingHals) {
Yifan Hong643a9ef2018-03-21 14:13:55 -0700145 MatrixHal* splitInstance =
146 this->splitInstance(existingHal, interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700147 if (splitInstance != nullptr) {
148 splitInstance->insertVersionRanges(versionRanges);
Yifan Hong643a9ef2018-03-21 14:13:55 -0700149 if (isRegex) {
150 insertedRegex.insert(std::make_pair(interface, instanceOrPattern));
151 } else {
152 insertedInstances.insert(std::make_pair(interface, instanceOrPattern));
153 }
Yifan Honge7e45532018-03-16 18:11:49 -0700154 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800155 }
Yifan Honge7e45532018-03-16 18:11:49 -0700156 return true;
157 });
158
159 // Add the remaining instances.
160 for (const auto& pair : insertedInstances) {
Yifan Hong643a9ef2018-03-21 14:13:55 -0700161 halToAdd.removeInstance(pair.first, pair.second, false /* isRegex */);
162 }
163 for (const auto& pair : insertedRegex) {
164 halToAdd.removeInstance(pair.first, pair.second, true /* isRegex */);
Yifan Hong7967d7b2018-03-15 17:08:58 -0700165 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800166
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700167 if (halToAdd.instancesCount() > 0) {
Yifan Hong7967d7b2018-03-15 17:08:58 -0700168 halToAdd.setOptional(true);
169 if (!add(std::move(halToAdd))) {
170 if (error) {
171 *error = "Cannot add HAL " + name + " for unknown reason.";
Yifan Hongdbe9db32017-12-11 19:06:11 -0800172 }
173 return false;
174 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800175 }
176 }
177 return true;
178}
179
Yifan Hongd4b92fe2017-12-20 15:29:03 -0800180bool CompatibilityMatrix::addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error) {
181 if (other == nullptr || other->level() <= level()) {
182 return true;
183 }
184 for (auto& pair : other->mXmlFiles) {
185 const std::string& name = pair.first;
186 MatrixXmlFile& xmlFileToAdd = pair.second;
187
188 xmlFileToAdd.mOptional = true;
189 if (!addXmlFile(std::move(xmlFileToAdd))) {
190 if (error) {
191 *error = "Cannot add XML File " + name + " for unknown reason.";
192 }
193 return false;
194 }
195 }
196 return true;
197}
198
Yifan Hongd6de7f62018-04-26 18:40:02 -0700199bool CompatibilityMatrix::addAllKernelsAsOptional(CompatibilityMatrix* other, std::string* error) {
200 if (other == nullptr || other->level() <= level()) {
201 return true;
202 }
203
204 for (MatrixKernel& kernelToAdd : other->framework.mKernels) {
205 bool exists =
206 std::any_of(this->framework.mKernels.begin(), this->framework.mKernels.end(),
207 [&kernelToAdd](const MatrixKernel& existing) {
208 return kernelToAdd.minLts().version == existing.minLts().version &&
209 kernelToAdd.minLts().majorRev == existing.minLts().majorRev;
210 });
211
212 if (exists) {
213 // Shouldn't retroactively add requirements to minLts(), so ignore this.
214 // This happens even when kernelToAdd.conditions() != existing.conditions().
215 continue;
216 }
217
218 KernelVersion minLts = kernelToAdd.minLts();
219 if (!add(std::move(kernelToAdd))) {
220 if (error) {
221 *error = "Cannot add " + to_string(minLts) + " for unknown reason.";
222 }
223 return false;
224 }
225 }
226 return true;
227}
228
Yifan Hongfb7469c2017-04-05 19:15:21 -0700229bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) {
Yifan Hong2027a492017-12-11 15:21:19 -0800230 return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals &&
231 lft.mXmlFiles == rgt.mXmlFiles &&
Yifan Hongfeb454e2018-01-09 16:16:40 -0800232 (lft.mType != SchemaType::DEVICE ||
233 (
Yifan Hong0f529fa2018-01-10 14:51:59 -0800234#pragma clang diagnostic push
235#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Yifan Hongfeb454e2018-01-09 16:16:40 -0800236 lft.device.mVndk == rgt.device.mVndk &&
Yifan Hong0f529fa2018-01-10 14:51:59 -0800237#pragma clang diagnostic pop
Yifan Honga28729e2018-01-17 13:40:35 -0800238 lft.device.mVendorNdk == rgt.device.mVendorNdk &&
239 lft.device.mSystemSdk == rgt.device.mSystemSdk)) &&
Yifan Hongd4857902017-06-13 14:13:56 -0700240 (lft.mType != SchemaType::FRAMEWORK ||
241 (lft.framework.mKernels == rgt.framework.mKernels &&
242 lft.framework.mSepolicy == rgt.framework.mSepolicy &&
243 lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion));
Yifan Hongfb7469c2017-04-05 19:15:21 -0700244}
245
Yifan Hongddae77e2017-12-18 16:57:07 -0800246// Find compatibility_matrix.empty.xml (which has unspecified level) and use it
247// as a base matrix.
248CompatibilityMatrix* CompatibilityMatrix::findOrInsertBaseMatrix(
Yifan Hongffcaf992018-01-09 17:08:51 -0800249 std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700250 std::vector<CompatibilityMatrix*> matricesUnspecified;
251 std::vector<CompatibilityMatrix*> matricesEmpty;
Yifan Hongddae77e2017-12-18 16:57:07 -0800252 for (auto& e : *matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800253 if (e.object.level() == Level::UNSPECIFIED) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700254 matricesUnspecified.push_back(&e.object);
255
Yifan Hongffcaf992018-01-09 17:08:51 -0800256 if (!e.object.mHals.empty()) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700257 continue;
Yifan Hongddae77e2017-12-18 16:57:07 -0800258 }
259
Yifan Hongffcaf992018-01-09 17:08:51 -0800260 if (!e.object.mXmlFiles.empty()) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700261 continue;
Yifan Hongddae77e2017-12-18 16:57:07 -0800262 }
263
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700264 matricesEmpty.push_back(&e.object);
Yifan Hongddae77e2017-12-18 16:57:07 -0800265 }
266 }
267
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700268 if (matricesEmpty.size() > 1) {
Yifan Hongddae77e2017-12-18 16:57:07 -0800269 if (error) {
270 *error =
271 "Error: multiple framework compatibility matrix files have "
272 "unspecified level; there should only be one such file.\n";
273 for (auto& e : *matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800274 if (e.object.level() == Level::UNSPECIFIED) {
275 *error += " " + e.name + "\n";
Yifan Hongddae77e2017-12-18 16:57:07 -0800276 }
277 }
278 }
279 return nullptr;
280 }
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700281 if (matricesEmpty.size() == 1) {
282 return matricesEmpty.front();
Yifan Hongddae77e2017-12-18 16:57:07 -0800283 }
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700284 if (!matricesUnspecified.empty()) {
285 return matricesUnspecified.front();
286 }
287 auto matrix = &matrices->emplace(matrices->end())->object;
288 matrix->mType = SchemaType::FRAMEWORK;
289 matrix->mLevel = Level::UNSPECIFIED;
Yifan Hongddae77e2017-12-18 16:57:07 -0800290 return matrix;
291}
292
Yifan Hongd6de7f62018-04-26 18:40:02 -0700293// Check if there are two files declaring level="1", for example, because
294// combine() use this assumption to simplify a lot of logic.
295static bool checkDuplicateLevels(const std::vector<Named<CompatibilityMatrix>>& matrices,
296 std::string* error) {
297 std::map<Level, const std::string*> existingLevels;
298 for (const auto& e : matrices) {
Yifan Hong72201b82018-07-02 10:15:24 -0700299 if (e.object.level() != Level::UNSPECIFIED &&
Yifan Hongd6de7f62018-04-26 18:40:02 -0700300 existingLevels.find(e.object.level()) != existingLevels.end()) {
301 if (error) {
302 *error = "Conflict of levels: file \"" +
303 *existingLevels.find(e.object.level())->second + "\" and \"" + e.name +
304 " both have level " + to_string(e.object.level());
305 }
306 return false;
307 }
308 existingLevels.emplace(e.object.level(), &e.name);
309 }
310 return true;
311}
312
Yifan Hongffcaf992018-01-09 17:08:51 -0800313CompatibilityMatrix* CompatibilityMatrix::combine(Level deviceLevel,
314 std::vector<Named<CompatibilityMatrix>>* matrices,
315 std::string* error) {
Yifan Hongd6de7f62018-04-26 18:40:02 -0700316 if (!checkDuplicateLevels(*matrices, error)) {
317 return nullptr;
318 }
Yifan Hongddae77e2017-12-18 16:57:07 -0800319
320 CompatibilityMatrix* matrix = findOrInsertBaseMatrix(matrices, error);
321 if (matrix == nullptr) {
322 return nullptr;
323 }
324
325 matrix->mLevel = deviceLevel;
326
327 for (auto& e : *matrices) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700328 if (&e.object != matrix &&
329 (e.object.level() == deviceLevel || e.object.level() == Level::UNSPECIFIED)) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800330 if (!matrix->addAllHals(&e.object, error)) {
Yifan Hongddae77e2017-12-18 16:57:07 -0800331 if (error) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800332 *error = "File \"" + e.name + "\" cannot be added: HAL " + *error +
Yifan Hongddae77e2017-12-18 16:57:07 -0800333 " has a conflict.";
334 }
335 return nullptr;
336 }
337
Yifan Hongffcaf992018-01-09 17:08:51 -0800338 if (!matrix->addAllXmlFiles(&e.object, error)) {
Yifan Hongddae77e2017-12-18 16:57:07 -0800339 if (error) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800340 *error = "File \"" + e.name + "\" cannot be added: XML File entry " + *error +
Yifan Hongddae77e2017-12-18 16:57:07 -0800341 " has a conflict.";
342 }
343 return nullptr;
344 }
345 }
346 }
347
348 for (auto& e : *matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800349 if (&e.object != matrix && e.object.level() != Level::UNSPECIFIED &&
350 e.object.level() > deviceLevel) {
351 if (!matrix->addAllHalsAsOptional(&e.object, error)) {
Yifan Hongddae77e2017-12-18 16:57:07 -0800352 if (error) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800353 *error = "File \"" + e.name + "\" cannot be added: " + *error +
Yifan Hongddae77e2017-12-18 16:57:07 -0800354 ". See <hal> with the same name " +
355 "in previously parsed files or previously declared in this file.";
356 }
357 return nullptr;
358 }
359
Yifan Hongffcaf992018-01-09 17:08:51 -0800360 if (!matrix->addAllXmlFilesAsOptional(&e.object, error)) {
Yifan Hongddae77e2017-12-18 16:57:07 -0800361 if (error) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800362 *error = "File \"" + e.name + "\" cannot be added: XML File entry " + *error +
Yifan Hongddae77e2017-12-18 16:57:07 -0800363 " has a conflict.";
364 }
365 return nullptr;
366 }
367 }
368 }
369
Yifan Hongd6de7f62018-04-26 18:40:02 -0700370 // Add <kernel> from exact "level", then optionally add <kernel> from high levels to low levels.
371 // For example, (each letter is a kernel version x.y.z)
372 // 1.xml: A1, B1
373 // 2.xml: B2, C2, D2
374 // 3.xml: D3, E3
375 // Then the combined 1.xml should have
376 // A1, B1 (from 1.xml, required), C2, D2, E3 (optional, use earliest possible).
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800377 for (auto& e : *matrices) {
378 if (&e.object != matrix && e.object.level() == deviceLevel &&
379 e.object.type() == SchemaType::FRAMEWORK) {
380 for (MatrixKernel& kernel : e.object.framework.mKernels) {
381 KernelVersion ver = kernel.minLts();
382 if (!matrix->add(std::move(kernel))) {
383 if (error) {
384 *error = "Cannot add kernel version " + to_string(ver) +
385 " from FCM version " + to_string(deviceLevel);
386 }
387 return nullptr;
388 }
389 }
390 }
391 }
392
Yifan Hongd6de7f62018-04-26 18:40:02 -0700393 // There is only one file per level, hence a map is used instead of a multimap. Also, there is
394 // no good ordering (i.e. priority) for multiple files with the same level.
395 std::map<Level, Named<CompatibilityMatrix>*> matricesMap;
396 for (auto& e : *matrices) {
397 if (&e.object != matrix && e.object.level() != Level::UNSPECIFIED &&
398 e.object.level() > deviceLevel && e.object.type() == SchemaType::FRAMEWORK) {
399 matricesMap.emplace(e.object.level(), &e);
400 }
401 }
402
403 for (auto&& pair : matricesMap) {
404 if (!matrix->addAllKernelsAsOptional(&pair.second->object, error)) {
405 if (error) {
406 *error = "Cannot add new kernel versions from FCM version " +
407 to_string(pair.first) + " (" + pair.second->name + ")" +
408 " to FCM version " + to_string(deviceLevel) + ": " + *error;
409 }
410 return nullptr;
411 }
412 }
413
Yifan Hongddae77e2017-12-18 16:57:07 -0800414 return matrix;
415}
416
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800417bool CompatibilityMatrix::forEachInstanceOfVersion(
418 const std::string& package, const Version& expectVersion,
419 const std::function<bool(const MatrixInstance&)>& func) const {
420 for (const MatrixHal* hal : getHals(package)) {
421 bool cont = hal->forEachInstance([&](const MatrixInstance& matrixInstance) {
422 if (matrixInstance.versionRange().contains(expectVersion)) {
423 return func(matrixInstance);
Yifan Honge3a92342018-01-25 17:00:16 -0800424 }
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800425 return true;
426 });
427 if (!cont) return false;
Yifan Honge3a92342018-01-25 17:00:16 -0800428 }
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800429 return true;
Yifan Honge3a92342018-01-25 17:00:16 -0800430}
431
Yifan Hongdef7e7f2018-03-20 13:27:36 -0700432bool CompatibilityMatrix::matchInstance(const std::string& halName, const Version& version,
433 const std::string& interfaceName,
434 const std::string& instance) const {
435 bool found = false;
436 (void)forEachInstanceOfInterface(halName, version, interfaceName,
437 [&found, &instance](const auto& e) {
438 found |= (e.matchInstance(instance));
439 return !found; // if not found, continue
440 });
441 return found;
442}
443
Yifan Hong52b7fae2018-05-22 16:21:31 -0700444std::string CompatibilityMatrix::getVendorNdkVersion() const {
445 return type() == SchemaType::DEVICE ? device.mVendorNdk.version() : "";
446}
447
Yifan Hong676447a2016-11-15 12:57:23 -0800448} // namespace vintf
449} // namespace android