• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
19 #include <iostream>
20 #include <utility>
21 
22 #include <android-base/logging.h>
23 #include <android-base/strings.h>
24 
25 #include "parse_string.h"
26 #include "parse_xml.h"
27 #include "utils.h"
28 
29 namespace android {
30 namespace vintf {
31 
32 using details::mergeField;
33 
add(MatrixHal && halToAdd,std::string *)34 bool CompatibilityMatrix::add(MatrixHal&& halToAdd, std::string*) {
35     CHECK(addInternal(std::move(halToAdd)) != nullptr);
36     return true;
37 }
38 
addAllHals(CompatibilityMatrix * other,std::string *)39 bool CompatibilityMatrix::addAllHals(CompatibilityMatrix* other, std::string*) {
40     std::string internalError;
41     for (auto& entry : other->mHals) {
42         CHECK(add(std::move(entry.second), &internalError)) << internalError;
43     }
44     other->mHals.clear();
45     return true;
46 }
47 
addKernel(MatrixKernel && kernel,std::string * error)48 bool CompatibilityMatrix::addKernel(MatrixKernel&& kernel, std::string* error) {
49     if (mType != SchemaType::FRAMEWORK) {
50         if (error) {
51             *error = "Cannot add <kernel> to a " + to_string(mType) + " compatibility matrix.";
52         }
53         return false;
54     }
55 
56     if (kernel.getSourceMatrixLevel() == Level::UNSPECIFIED) {
57         kernel.setSourceMatrixLevel(level());
58     }
59 
60     auto it = framework.mKernels.begin();
61     for (; it != framework.mKernels.end(); ++it) {
62         if (it->getSourceMatrixLevel() != kernel.getSourceMatrixLevel()) {
63             continue;
64         }
65         if (it->minLts() == kernel.minLts()) {
66             break;
67         }
68         if (it->minLts().dropMinor() == kernel.minLts().dropMinor()) {
69             if (error) {
70                 *error = "Kernel version mismatch; for level " +
71                          to_string(kernel.getSourceMatrixLevel()) + ", cannot add " +
72                          to_string(kernel.minLts()) + " because " + to_string(it->minLts()) +
73                          " was added.";
74             }
75             return false;
76         }
77     }
78 
79     bool seenVersion = it != framework.mKernels.end();
80 
81     if (seenVersion) {
82         // If no conditions, must be the first among the same minLts
83         // because O libvintf only checks the first <kernel> tag that version matches.
84         if (kernel.conditions().empty()) {
85             // Found first <kernel> with the same minLts.
86             // Append config if it does not have <condition>s, else error.
87             if (it->conditions().empty()) {
88                 const auto& configs = kernel.configs();
89                 it->mConfigs.insert(it->mConfigs.end(), configs.begin(), configs.end());
90             } else {
91                 if (error) {
92                     *error =
93                         "Base compatibility matrix has <condition> for the first <kernel> "
94                         "with minlts " +
95                         to_string(kernel.minLts()) + " for unknown reason.";
96                 }
97                 return false;
98             }
99             return true;
100         }
101     } else {
102         // First <kernel> of a minLts must not have <condition>'s for backwards compatibility
103         // with O libvintf.
104         if (!kernel.conditions().empty()) {
105             framework.mKernels.push_back(MatrixKernel(KernelVersion{kernel.minLts()}, {}));
106         }
107     }
108 
109     framework.mKernels.push_back(std::move(kernel));
110     return true;
111 }
112 
type() const113 SchemaType CompatibilityMatrix::type() const {
114     return mType;
115 }
116 
level() const117 Level CompatibilityMatrix::level() const {
118     return mLevel;
119 }
120 
fetchAllInformation(const FileSystem * fileSystem,const std::string & path,std::string * error)121 status_t CompatibilityMatrix::fetchAllInformation(const FileSystem* fileSystem,
122                                                   const std::string& path, std::string* error) {
123     return details::fetchAllInformation(fileSystem, path, this, error);
124 }
125 
getXmlSchemaPath(const std::string & xmlFileName,const Version & version) const126 std::string CompatibilityMatrix::getXmlSchemaPath(const std::string& xmlFileName,
127                                                   const Version& version) const {
128     using std::literals::string_literals::operator""s;
129     auto range = getXmlFiles(xmlFileName);
130     for (auto it = range.first; it != range.second; ++it) {
131         const MatrixXmlFile& matrixXmlFile = it->second;
132         if (matrixXmlFile.versionRange().contains(version)) {
133             if (!matrixXmlFile.overriddenPath().empty()) {
134                 return matrixXmlFile.overriddenPath();
135             }
136             return "/"s + (type() == SchemaType::DEVICE ? "vendor" : "system") + "/etc/" +
137                    xmlFileName + "_V" + std::to_string(matrixXmlFile.versionRange().majorVer) +
138                    "_" + std::to_string(matrixXmlFile.versionRange().maxMinor) + "." +
139                    to_string(matrixXmlFile.format());
140         }
141     }
142     return "";
143 }
144 
145 // Split existingHal into a HAL that contains only interface/instance and a HAL
146 // that does not contain it. Return the HAL that contains only interface/instance.
147 // - Return nullptr if existingHal does not contain interface/instance
148 // - Return existingHal if existingHal contains only interface/instance
149 // - Remove interface/instance from existingHal, and return a new MatrixHal (that is added
150 //   to "this") that contains only interface/instance.
splitInstance(MatrixHal * existingHal,const std::string & interface,const std::string & instanceOrPattern,bool isRegex)151 MatrixHal* CompatibilityMatrix::splitInstance(MatrixHal* existingHal, const std::string& interface,
152                                               const std::string& instanceOrPattern, bool isRegex) {
153     bool found = false;
154     bool foundOthers = false;
155     existingHal->forEachInstance([&](const auto& matrixInstance) {
156         bool interfaceMatch = matrixInstance.interface() == interface;
157         bool instanceMatch = false;
158         if (matrixInstance.isRegex() && isRegex) {
159             instanceMatch = (matrixInstance.regexPattern() == instanceOrPattern);
160         } else if (!matrixInstance.isRegex() && !isRegex) {
161             instanceMatch = (matrixInstance.exactInstance() == instanceOrPattern);
162         }
163 
164         bool match = interfaceMatch && instanceMatch;
165 
166         found |= match;
167         foundOthers |= (!match);
168 
169         return !found || !foundOthers;
170     });
171 
172     if (!found) {
173         return nullptr;
174     }
175 
176     if (!foundOthers) {
177         return existingHal;
178     }
179 
180     existingHal->removeInstance(interface, instanceOrPattern, isRegex);
181     MatrixHal copy = *existingHal;
182     copy.clearInstances();
183     copy.insertInstance(interface, instanceOrPattern, isRegex);
184 
185     return addInternal(std::move(copy));
186 }
187 
188 // Add all package@other_version::interface/instance as an optional instance.
189 // If package@this_version::interface/instance is in this (that is, some instance
190 // with the same package and interface and instance exists), then other_version is
191 // considered a possible replacement to this_version.
192 // See LibVintfTest.AddOptionalHal* tests for details.
addAllHalsAsOptional(CompatibilityMatrix * other,std::string * error)193 bool CompatibilityMatrix::addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error) {
194     if (other == nullptr || other->level() <= level()) {
195         return true;
196     }
197 
198     for (auto& halEntry : other->mHals) {
199         const std::string& name = halEntry.first;
200         MatrixHal& halToAdd = halEntry.second;
201 
202         std::set<std::pair<std::string, std::string>> insertedInstances;
203         std::set<std::pair<std::string, std::string>> insertedRegex;
204         auto existingHals = getHals(name);
205 
206         halToAdd.forEachInstance([&](const std::vector<VersionRange>& versionRanges,
207                                      const std::string& interface,
208                                      const std::string& instanceOrPattern, bool isRegex) {
209             for (auto* existingHal : existingHals) {
210                 // Ignore HALs with different format.
211                 if (halToAdd.format != existingHal->format) {
212                     continue;
213                 }
214 
215                 MatrixHal* splitInstance =
216                     this->splitInstance(existingHal, interface, instanceOrPattern, isRegex);
217                 if (splitInstance != nullptr) {
218                     splitInstance->updatableViaApex |= halToAdd.updatableViaApex;
219                     splitInstance->insertVersionRanges(versionRanges);
220                     if (isRegex) {
221                         insertedRegex.insert(std::make_pair(interface, instanceOrPattern));
222                     } else {
223                         insertedInstances.insert(std::make_pair(interface, instanceOrPattern));
224                     }
225                 }
226             }
227             return true;
228         });
229 
230         // Add the remaining instances.
231         for (const auto& entry : insertedInstances) {
232             halToAdd.removeInstance(entry.first, entry.second, false /* isRegex */);
233         }
234         for (const auto& entry : insertedRegex) {
235             halToAdd.removeInstance(entry.first, entry.second, true /* isRegex */);
236         }
237 
238         if (halToAdd.instancesCount() > 0) {
239             if (!add(std::move(halToAdd))) {
240                 if (error) {
241                     *error = "Cannot add HAL " + name + " for unknown reason.";
242                 }
243                 return false;
244             }
245         }
246     }
247     return true;
248 }
249 
addAllXmlFilesAsOptional(CompatibilityMatrix * other,std::string * error)250 bool CompatibilityMatrix::addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error) {
251     if (other == nullptr || other->level() <= level()) {
252         return true;
253     }
254     for (auto& pair : other->mXmlFiles) {
255         const std::string& name = pair.first;
256         MatrixXmlFile& xmlFileToAdd = pair.second;
257 
258         if (!addXmlFile(std::move(xmlFileToAdd))) {
259             if (error) {
260                 *error = "Cannot add XML File " + name + " for unknown reason.";
261             }
262             return false;
263         }
264     }
265     return true;
266 }
267 
268 // Merge Kernel. See KernelInfo::getMatchedKernelRequirements for details on compatibility checks.
addAllKernels(CompatibilityMatrix * other,std::string * error)269 bool CompatibilityMatrix::addAllKernels(CompatibilityMatrix* other, std::string* error) {
270     for (MatrixKernel& kernel : other->framework.mKernels) {
271         if (kernel.getSourceMatrixLevel() == Level::UNSPECIFIED) {
272             kernel.setSourceMatrixLevel(other->level());
273         }
274         KernelVersion ver = kernel.minLts();
275         if (!addKernel(std::move(kernel), error)) {
276             if (error) {
277                 *error = "Cannot add kernel version " + to_string(ver) + ": " + *error;
278             }
279             return false;
280         }
281     }
282     return true;
283 }
284 
addSepolicy(CompatibilityMatrix * other,std::string * error)285 bool CompatibilityMatrix::addSepolicy(CompatibilityMatrix* other, std::string* error) {
286     bool success = mergeField(&this->framework.mSepolicy, &other->framework.mSepolicy);
287     if (!success && error) *error = "<sepolicy> is already defined";
288     return success;
289 }
290 
addAvbMetaVersion(CompatibilityMatrix * other,std::string * error)291 bool CompatibilityMatrix::addAvbMetaVersion(CompatibilityMatrix* other, std::string* error) {
292     bool success = mergeField(&this->framework.mAvbMetaVersion, &other->framework.mAvbMetaVersion);
293     if (!success && error) *error = "<avb><vbmeta-version> is already defined";
294     return success;
295 }
296 
addVndk(CompatibilityMatrix * other,std::string * error)297 bool CompatibilityMatrix::addVndk(CompatibilityMatrix* other, std::string* error) {
298 #pragma clang diagnostic push
299 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
300     bool success = mergeField(&this->device.mVndk, &other->device.mVndk);
301 #pragma clang diagnostic pop
302     if (!success && error) *error = "<vndk> is already defined";
303     return success;
304 }
305 
addVendorNdk(CompatibilityMatrix * other,std::string * error)306 bool CompatibilityMatrix::addVendorNdk(CompatibilityMatrix* other, std::string* error) {
307     bool success = mergeField(&this->device.mVendorNdk, &other->device.mVendorNdk);
308     if (!success && error) *error = "<vendor-ndk> is already defined";
309     return success;
310 }
311 
addSystemSdk(CompatibilityMatrix * other,std::string *)312 bool CompatibilityMatrix::addSystemSdk(CompatibilityMatrix* other, std::string* /* error */) {
313     this->device.mSystemSdk.addAll(&other->device.mSystemSdk);
314     return true;
315 }
316 
operator ==(const CompatibilityMatrix & lft,const CompatibilityMatrix & rgt)317 bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) {
318     // ignore fileName().
319     return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals &&
320            lft.mXmlFiles == rgt.mXmlFiles &&
321            (lft.mType != SchemaType::DEVICE ||
322             (
323 #pragma clang diagnostic push
324 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
325                 lft.device.mVndk == rgt.device.mVndk &&
326 #pragma clang diagnostic pop
327                 lft.device.mVendorNdk == rgt.device.mVendorNdk &&
328                 lft.device.mSystemSdk == rgt.device.mSystemSdk)) &&
329            (lft.mType != SchemaType::FRAMEWORK ||
330             (lft.framework.mKernels == rgt.framework.mKernels &&
331              lft.framework.mSepolicy == rgt.framework.mSepolicy &&
332              lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion));
333 }
334 
combine(Level deviceLevel,Level kernelLevel,std::vector<CompatibilityMatrix> * matrices,std::string * error)335 std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combine(
336     Level deviceLevel, Level kernelLevel, std::vector<CompatibilityMatrix>* matrices,
337     std::string* error) {
338     // Check type.
339     for (const auto& e : *matrices) {
340         if (e.type() != SchemaType::FRAMEWORK) {
341             if (error) {
342                 *error = "File \"" + e.fileName() + "\" is not a framework compatibility matrix.";
343                 return nullptr;
344             }
345         }
346     }
347 
348     // Matrices with unspecified (empty) level are auto-filled with deviceLevel.
349     for (auto& e : *matrices) {
350         if (e.level() == Level::UNSPECIFIED) {
351             e.mLevel = deviceLevel;
352         }
353     }
354 
355     // Add from low to high FCM version so that optional <kernel> requirements are added correctly.
356     // See comment in addAllAsOptional.
357     std::sort(matrices->begin(), matrices->end(),
358               [](const auto& x, const auto& y) { return x.level() < y.level(); });
359 
360     auto baseMatrix = std::make_unique<CompatibilityMatrix>();
361     baseMatrix->mLevel = deviceLevel;
362     baseMatrix->mType = SchemaType::FRAMEWORK;
363 
364     std::vector<std::string> parsedFiles;
365     for (auto& e : *matrices) {
366         bool success = false;
367         if (e.level() < deviceLevel) {
368             if (kernelLevel == Level::UNSPECIFIED) continue;
369             if (e.level() < kernelLevel) continue;
370             success = baseMatrix->addAllKernels(&e, error);
371         } else if (e.level() == deviceLevel) {
372             success = baseMatrix->addAll(&e, error);
373         } else {
374             success = baseMatrix->addAllAsOptional(&e, error);
375         }
376         if (!success) {
377             if (error) {
378                 *error = "Conflict when merging \"" + e.fileName() + "\": " + *error + "\n" +
379                          "Previous files:\n" + base::Join(parsedFiles, "\n");
380             }
381             return nullptr;
382         }
383         parsedFiles.push_back(e.fileName());
384     }
385 
386     return baseMatrix;
387 }
388 
combineDeviceMatrices(std::vector<CompatibilityMatrix> * matrices,std::string * error)389 std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combineDeviceMatrices(
390     std::vector<CompatibilityMatrix>* matrices, std::string* error) {
391     auto baseMatrix = std::make_unique<CompatibilityMatrix>();
392     baseMatrix->mType = SchemaType::DEVICE;
393 
394     std::vector<std::string> parsedFiles;
395     for (auto& e : *matrices) {
396         bool success = baseMatrix->addAll(&e, error);
397         if (!success) {
398             if (error) {
399                 *error = "Conflict when merging \"" + e.fileName() + "\": " + *error + "\n" +
400                          "Previous files:\n" + base::Join(parsedFiles, "\n");
401             }
402             return nullptr;
403         }
404         parsedFiles.push_back(e.fileName());
405     }
406     return baseMatrix;
407 }
408 
addAll(CompatibilityMatrix * inputMatrix,std::string * error)409 bool CompatibilityMatrix::addAll(CompatibilityMatrix* inputMatrix, std::string* error) {
410     if (!addAllHals(inputMatrix, error) || !addAllXmlFiles(inputMatrix, error) ||
411         !addAllKernels(inputMatrix, error) || !addSepolicy(inputMatrix, error) ||
412         !addAvbMetaVersion(inputMatrix, error) || !addVndk(inputMatrix, error) ||
413         !addVendorNdk(inputMatrix, error) || !addSystemSdk(inputMatrix, error)) {
414         if (error) {
415             *error = "File \"" + inputMatrix->fileName() + "\" cannot be added: " + *error + ".";
416         }
417         return false;
418     }
419     return true;
420 }
421 
addAllAsOptional(CompatibilityMatrix * inputMatrix,std::string * error)422 bool CompatibilityMatrix::addAllAsOptional(CompatibilityMatrix* inputMatrix, std::string* error) {
423     if (!addAllHalsAsOptional(inputMatrix, error) ||
424         !addAllXmlFilesAsOptional(inputMatrix, error) || !addAllKernels(inputMatrix, error)) {
425         if (error) {
426             *error = "File \"" + inputMatrix->fileName() + "\" cannot be added: " + *error;
427         }
428         return false;
429     }
430     // ignore <sepolicy> requirement from higher level
431     // ignore <avb> requirement from higher level
432     return true;
433 }
434 
forEachInstanceOfVersion(HalFormat format,ExclusiveTo exclusiveTo,const std::string & package,const Version & expectVersion,const std::function<bool (const MatrixInstance &)> & func) const435 bool CompatibilityMatrix::forEachInstanceOfVersion(
436     HalFormat format, ExclusiveTo exclusiveTo, const std::string& package,
437     const Version& expectVersion, const std::function<bool(const MatrixInstance&)>& func) const {
438     for (const MatrixHal* hal : getHals(package)) {
439         bool cont = hal->forEachInstance([&](const MatrixInstance& matrixInstance) {
440             if (matrixInstance.format() == format &&
441                 matrixInstance.versionRange().contains(expectVersion) &&
442                 matrixInstance.exclusiveTo() == exclusiveTo) {
443                 return func(matrixInstance);
444             }
445             return true;
446         });
447         if (!cont) return false;
448     }
449     return true;
450 }
451 
matchInstance(HalFormat format,ExclusiveTo exclusiveTo,const std::string & halName,const Version & version,const std::string & interfaceName,const std::string & instance) const452 bool CompatibilityMatrix::matchInstance(HalFormat format, ExclusiveTo exclusiveTo,
453                                         const std::string& halName, const Version& version,
454                                         const std::string& interfaceName,
455                                         const std::string& instance) const {
456     bool found = false;
457     (void)forEachInstanceOfInterface(format, exclusiveTo, halName, version, interfaceName,
458                                      [&found, &instance](const auto& e) {
459                                          found |= (e.matchInstance(instance));
460                                          return !found;  // if not found, continue
461                                      });
462     return found;
463 }
464 
getSepolicyVersions() const465 std::vector<SepolicyVersionRange> CompatibilityMatrix::getSepolicyVersions() const {
466     if (type() == SchemaType::FRAMEWORK) return framework.mSepolicy.sepolicyVersions();
467     return {};
468 }
469 
getVendorNdkVersion() const470 std::string CompatibilityMatrix::getVendorNdkVersion() const {
471     return type() == SchemaType::DEVICE ? device.mVendorNdk.version() : "";
472 }
473 
getLatestKernelMinLts() const474 KernelVersion CompatibilityMatrix::getLatestKernelMinLts() const {
475     if (type() != SchemaType::FRAMEWORK) {
476         return {};
477     }
478     auto maxIt = std::max_element(
479         framework.mKernels.begin(), framework.mKernels.end(),
480         [](const MatrixKernel& a, const MatrixKernel& b) { return a.minLts() < b.minLts(); });
481     if (maxIt == framework.mKernels.end()) {
482         return {};
483     }
484     return maxIt->minLts();
485 }
486 
487 } // namespace vintf
488 } // namespace android
489