CompatibilityMatrix.cpp 18 KB

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