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 "VintfObject.h"
17
18 #include <dirent.h>
19
20 #include <algorithm>
21 #include <functional>
22 #include <memory>
23 #include <mutex>
24 #include <vector>
25
26 #include <aidl/metadata.h>
27 #include <android-base/file.h>
28 #include <android-base/logging.h>
29 #include <android-base/result.h>
30 #include <android-base/strings.h>
31 #include <hidl/metadata.h>
32
33 #include "Apex.h"
34 #include "CompatibilityMatrix.h"
35 #include "VintfObjectUtils.h"
36 #include "constants-private.h"
37 #include "include/vintf/FqInstance.h"
38 #include "parse_string.h"
39 #include "parse_xml.h"
40 #include "utils.h"
41
42 using std::placeholders::_1;
43 using std::placeholders::_2;
44 using std::string_literals::operator""s;
45
46 namespace android {
47 namespace vintf {
48
49 using namespace details;
50
51 #ifdef LIBVINTF_TARGET
52 static constexpr bool kIsTarget = true;
53 #else
54 static constexpr bool kIsTarget = false;
55 #endif
56
createDefaultFileSystem()57 static std::unique_ptr<FileSystem> createDefaultFileSystem() {
58 std::unique_ptr<FileSystem> fileSystem;
59 if (kIsTarget) {
60 fileSystem = std::make_unique<details::FileSystemImpl>();
61 } else {
62 fileSystem = std::make_unique<details::FileSystemNoOp>();
63 }
64 return fileSystem;
65 }
66
createDefaultPropertyFetcher()67 static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
68 std::unique_ptr<PropertyFetcher> propertyFetcher;
69 if (kIsTarget) {
70 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
71 } else {
72 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
73 }
74 return propertyFetcher;
75 }
76
77 // Check whether the current executable is allowed to use libvintf.
78 // Allowed binaries:
79 // - host binaries
80 // - tests
81 // - {hw,}servicemanager
isAllowedToUseLibvintf()82 static bool isAllowedToUseLibvintf() {
83 if constexpr (!kIsTarget) {
84 return true;
85 }
86
87 auto execPath = android::base::GetExecutablePath();
88 if (android::base::StartsWith(execPath, "/data/")) {
89 return true;
90 }
91
92 std::vector<std::string> allowedBinaries{
93 "/system/bin/servicemanager",
94 "/system/bin/hwservicemanager",
95 "/system_ext/bin/hwservicemanager",
96 // Java: boot time VINTF check
97 "/system/bin/app_process32",
98 "/system/bin/app_process64",
99 // These aren't daemons so the memory impact is less concerning.
100 "/system/bin/lshal",
101 "/system/bin/vintf",
102 };
103
104 return std::find(allowedBinaries.begin(), allowedBinaries.end(), execPath) !=
105 allowedBinaries.end();
106 }
107
GetInstance()108 std::shared_ptr<VintfObject> VintfObject::GetInstance() {
109 static details::LockedSharedPtr<VintfObject> sInstance{};
110 std::unique_lock<std::mutex> lock(sInstance.mutex);
111 if (sInstance.object == nullptr) {
112 if (!isAllowedToUseLibvintf()) {
113 LOG(ERROR) << "libvintf-usage-violation: Executable "
114 << android::base::GetExecutablePath()
115 << " should not use libvintf. It should query VINTF "
116 << "metadata via servicemanager";
117 }
118
119 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
120 }
121 return sInstance.object;
122 }
123
GetDeviceHalManifest()124 std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest() {
125 return GetInstance()->getDeviceHalManifest();
126 }
127
getDeviceHalManifest()128 std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest() {
129 // TODO(b/242070736): only APEX data needs to be updated
130 return Get(__func__, &mDeviceManifest,
131 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2),
132 apex::GetModifiedTime(getFileSystem().get(), getPropertyFetcher().get()));
133 }
134
GetFrameworkHalManifest()135 std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest() {
136 return GetInstance()->getFrameworkHalManifest();
137 }
138
getFrameworkHalManifest()139 std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest() {
140 // TODO(b/242070736): only APEX data needs to be updated
141 return Get(__func__, &mFrameworkManifest,
142 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2),
143 apex::GetModifiedTime(getFileSystem().get(), getPropertyFetcher().get()));
144 }
145
GetDeviceCompatibilityMatrix()146 std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix() {
147 return GetInstance()->getDeviceCompatibilityMatrix();
148 }
149
getDeviceCompatibilityMatrix()150 std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix() {
151 return Get(__func__, &mDeviceMatrix, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
152 }
153
GetFrameworkCompatibilityMatrix()154 std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix() {
155 return GetInstance()->getFrameworkCompatibilityMatrix();
156 }
157
getFrameworkCompatibilityMatrix()158 std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix() {
159 // To avoid deadlock, get device manifest before any locks.
160 auto deviceManifest = getDeviceHalManifest();
161
162 std::string error;
163 auto kernelLevel = getKernelLevel(&error);
164 if (kernelLevel == Level::UNSPECIFIED) {
165 LOG(WARNING) << "getKernelLevel: " << error;
166 }
167
168 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
169
170 auto combined = Get(__func__, &mCombinedFrameworkMatrix,
171 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest,
172 kernelLevel, _1, _2));
173 if (combined != nullptr) {
174 return combined;
175 }
176
177 return Get(__func__, &mFrameworkMatrix,
178 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
179 kSystemLegacyMatrix, _2));
180 }
181
getCombinedFrameworkMatrix(const std::shared_ptr<const HalManifest> & deviceManifest,Level kernelLevel,CompatibilityMatrix * out,std::string * error)182 status_t VintfObject::getCombinedFrameworkMatrix(
183 const std::shared_ptr<const HalManifest>& deviceManifest, Level kernelLevel,
184 CompatibilityMatrix* out, std::string* error) {
185 std::vector<CompatibilityMatrix> matrixFragments;
186 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
187 if (matrixFragmentsStatus != OK) {
188 return matrixFragmentsStatus;
189 }
190 if (matrixFragments.empty()) {
191 if (error && error->empty()) {
192 *error = "Cannot get framework matrix for each FCM version for unknown error.";
193 }
194 return NAME_NOT_FOUND;
195 }
196
197 Level deviceLevel = Level::UNSPECIFIED;
198
199 if (deviceManifest != nullptr) {
200 deviceLevel = deviceManifest->level();
201 }
202
203 if (deviceLevel == Level::UNSPECIFIED) {
204 // Cannot infer FCM version. Combine all matrices by assuming
205 // Shipping FCM Version == min(all supported FCM Versions in the framework)
206 for (auto&& fragment : matrixFragments) {
207 Level fragmentLevel = fragment.level();
208 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
209 deviceLevel = fragmentLevel;
210 }
211 }
212 }
213
214 if (deviceLevel == Level::UNSPECIFIED) {
215 // None of the fragments specify any FCM version. Should never happen except
216 // for inconsistent builds.
217 if (error) {
218 *error = "No framework compatibility matrix files under "s + kSystemVintfDir +
219 " declare FCM version.";
220 }
221 return NAME_NOT_FOUND;
222 }
223
224 auto combined = CompatibilityMatrix::combine(deviceLevel, kernelLevel, &matrixFragments, error);
225 if (combined == nullptr) {
226 return BAD_VALUE;
227 }
228 *out = std::move(*combined);
229 return OK;
230 }
231
232 // Load and combine all of the manifests in a directory
233 // If forceSchemaType, all fragment manifests are coerced into manifest->type().
addDirectoryManifests(const std::string & directory,HalManifest * manifest,bool forceSchemaType,std::string * error)234 status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
235 bool forceSchemaType, std::string* error) {
236 std::vector<std::string> fileNames;
237 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
238 // if the directory isn't there, that's okay
239 if (err == NAME_NOT_FOUND) {
240 if (error) {
241 error->clear();
242 }
243 return OK;
244 }
245 if (err != OK) return err;
246
247 for (const std::string& file : fileNames) {
248 // Only adds HALs because all other things are added by libvintf
249 // itself for now.
250 HalManifest fragmentManifest;
251 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
252 if (err != OK) return err;
253
254 if (forceSchemaType) {
255 fragmentManifest.setType(manifest->type());
256 }
257
258 if (!manifest->addAll(&fragmentManifest, error)) {
259 if (error) {
260 error->insert(0, "Cannot add manifest fragment " + directory + file + ": ");
261 }
262 return UNKNOWN_ERROR;
263 }
264 }
265
266 return OK;
267 }
268
269 // addDirectoryManifests for multiple directories
addDirectoriesManifests(const std::vector<std::string> & directories,HalManifest * manifest,bool forceSchemaType,std::string * error)270 status_t VintfObject::addDirectoriesManifests(const std::vector<std::string>& directories,
271 HalManifest* manifest, bool forceSchemaType,
272 std::string* error) {
273 for (const auto& dir : directories) {
274 auto status = addDirectoryManifests(dir, manifest, forceSchemaType, error);
275 if (status != OK) {
276 return status;
277 }
278 }
279 return OK;
280 }
281
282 // Fetch fragments from apexes originated from /vendor.
283 // For now, we don't have /odm apexes.
fetchDeviceHalManifestApex(HalManifest * out,std::string * error)284 status_t VintfObject::fetchDeviceHalManifestApex(HalManifest* out, std::string* error) {
285 std::vector<std::string> dirs;
286 status_t status =
287 apex::GetDeviceVintfDirs(getFileSystem().get(), getPropertyFetcher().get(), &dirs, error);
288 if (status != OK) {
289 return status;
290 }
291 return addDirectoriesManifests(dirs, out, /*forceSchemaType=*/false, error);
292 }
293
294 // Priority for loading vendor manifest:
295 // 1. Vendor manifest + device fragments (including vapex) + ODM manifest (optional) + odm fragments
296 // 2. Vendor manifest + device fragments (including vapex)
297 // 3. ODM manifest (optional) + odm fragments
298 // 4. /vendor/manifest.xml (legacy, no fragments)
299 // where:
300 // A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
301 // over A.
fetchDeviceHalManifest(HalManifest * out,std::string * error)302 status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
303 HalManifest vendorManifest;
304 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
305 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
306 return vendorStatus;
307 }
308
309 if (vendorStatus == OK) {
310 *out = std::move(vendorManifest);
311 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out,
312 false /* forceSchemaType*/, error);
313 if (fragmentStatus != OK) {
314 return fragmentStatus;
315 }
316
317 status_t apexStatus = fetchDeviceHalManifestApex(out, error);
318 if (apexStatus != OK) {
319 return apexStatus;
320 }
321 }
322
323 HalManifest odmManifest;
324 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
325 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
326 return odmStatus;
327 }
328
329 if (vendorStatus == OK) {
330 if (odmStatus == OK) {
331 if (!out->addAll(&odmManifest, error)) {
332 if (error) {
333 error->insert(0, "Cannot add ODM manifest :");
334 }
335 return UNKNOWN_ERROR;
336 }
337 }
338 return addDirectoryManifests(kOdmManifestFragmentDir, out, false /* forceSchemaType */,
339 error);
340 }
341
342 // vendorStatus != OK, "out" is not changed.
343 if (odmStatus == OK) {
344 *out = std::move(odmManifest);
345 return addDirectoryManifests(kOdmManifestFragmentDir, out, false /* forceSchemaType */,
346 error);
347 }
348
349 // Use legacy /vendor/manifest.xml
350 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
351 }
352
353 // Priority:
354 // 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
355 // 2. /vendor/etc/vintf/manifest.xml
356 // where:
357 // {vendorSku} is the value of ro.boot.product.vendor.sku
fetchVendorHalManifest(HalManifest * out,std::string * error)358 status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
359 status_t status;
360
361 std::string vendorSku;
362 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
363
364 if (!vendorSku.empty()) {
365 status =
366 fetchOneHalManifest(kVendorVintfDir + "manifest_"s + vendorSku + ".xml", out, error);
367 if (status == OK || status != NAME_NOT_FOUND) {
368 return status;
369 }
370 }
371
372 status = fetchOneHalManifest(kVendorManifest, out, error);
373 if (status == OK || status != NAME_NOT_FOUND) {
374 return status;
375 }
376
377 return NAME_NOT_FOUND;
378 }
379
getOdmProductManifestFile(const std::string & dir,const::std::string & sku)380 std::string getOdmProductManifestFile(const std::string& dir, const ::std::string& sku) {
381 return sku.empty() ? "" : dir + "manifest_"s + sku + ".xml";
382 }
383
384 // "out" is written to iff return status is OK.
385 // Priority:
386 // 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
387 // 2. /odm/etc/vintf/manifest.xml
388 // 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
389 // 4. /odm/etc/manifest.xml
390 // where:
391 // {sku} is the value of ro.boot.product.hardware.sku
fetchOdmHalManifest(HalManifest * out,std::string * error)392 status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
393 status_t status;
394 std::string productModel;
395 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
396
397 const std::string productFile = getOdmProductManifestFile(kOdmVintfDir, productModel);
398 if (!productFile.empty()) {
399 status = fetchOneHalManifest(productFile, out, error);
400 if (status == OK || status != NAME_NOT_FOUND) {
401 return status;
402 }
403 }
404
405 status = fetchOneHalManifest(kOdmManifest, out, error);
406 if (status == OK || status != NAME_NOT_FOUND) {
407 return status;
408 }
409
410 const std::string productLegacyFile =
411 getOdmProductManifestFile(kOdmLegacyVintfDir, productModel);
412 if (!productLegacyFile.empty()) {
413 status = fetchOneHalManifest(productLegacyFile, out, error);
414 if (status == OK || status != NAME_NOT_FOUND) {
415 return status;
416 }
417 }
418
419 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
420 if (status == OK || status != NAME_NOT_FOUND) {
421 return status;
422 }
423
424 return NAME_NOT_FOUND;
425 }
426
427 // Fetch one manifest.xml file. "out" is written to iff return status is OK.
428 // Returns NAME_NOT_FOUND if file is missing.
fetchOneHalManifest(const std::string & path,HalManifest * out,std::string * error)429 status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
430 std::string* error) {
431 HalManifest ret;
432 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
433 if (status == OK) {
434 *out = std::move(ret);
435 }
436 return status;
437 }
438
fetchDeviceMatrix(CompatibilityMatrix * out,std::string * error)439 status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
440 CompatibilityMatrix etcMatrix;
441 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
442 *out = std::move(etcMatrix);
443 return OK;
444 }
445 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
446 }
447
448 // Priority:
449 // 1. /system/etc/vintf/manifest.xml
450 // + /system/etc/vintf/manifest/*.xml if they exist
451 // + /product/etc/vintf/manifest.xml if it exists
452 // + /product/etc/vintf/manifest/*.xml if they exist
453 // 2. (deprecated) /system/manifest.xml
fetchUnfilteredFrameworkHalManifest(HalManifest * out,std::string * error)454 status_t VintfObject::fetchUnfilteredFrameworkHalManifest(HalManifest* out, std::string* error) {
455 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
456 if (systemEtcStatus == OK) {
457 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out,
458 false /* forceSchemaType */, error);
459 if (dirStatus != OK) {
460 return dirStatus;
461 }
462
463 std::vector<std::pair<const char*, const char*>> extensions{
464 {kProductManifest, kProductManifestFragmentDir},
465 {kSystemExtManifest, kSystemExtManifestFragmentDir},
466 };
467 for (auto&& [manifestPath, frags] : extensions) {
468 HalManifest halManifest;
469 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
470 if (status != OK && status != NAME_NOT_FOUND) {
471 return status;
472 }
473 if (status == OK) {
474 if (!out->addAll(&halManifest, error)) {
475 if (error) {
476 error->insert(0, "Cannot add "s + manifestPath + ":");
477 }
478 return UNKNOWN_ERROR;
479 }
480 }
481
482 auto fragmentStatus =
483 addDirectoryManifests(frags, out, false /* forceSchemaType */, error);
484 if (fragmentStatus != OK) {
485 return fragmentStatus;
486 }
487 }
488
489 return OK;
490 } else {
491 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
492 << (error ? *error : strerror(-systemEtcStatus));
493 }
494
495 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
496 }
497
fetchFrameworkHalManifest(HalManifest * out,std::string * error)498 status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
499 status_t status = fetchUnfilteredFrameworkHalManifest(out, error);
500 if (status != OK) {
501 return status;
502 }
503 status = fetchFrameworkHalManifestApex(out, error);
504 if (status != OK) {
505 return status;
506 }
507 filterHalsByDeviceManifestLevel(out);
508 return OK;
509 }
510
511 // Fetch fragments from apexes originated from /system.
fetchFrameworkHalManifestApex(HalManifest * out,std::string * error)512 status_t VintfObject::fetchFrameworkHalManifestApex(HalManifest* out, std::string* error) {
513 std::vector<std::string> dirs;
514 status_t status = apex::GetFrameworkVintfDirs(getFileSystem().get(), getPropertyFetcher().get(),
515 &dirs, error);
516 if (status != OK) {
517 return status;
518 }
519 return addDirectoriesManifests(dirs, out, /*forceSchemaType=*/false, error);
520 }
521
522 // If deviceManifestLevel is not in the range [minLevel, maxLevel] of a HAL, remove the HAL,
523 // where:
524 // minLevel = hal.getMinLevel(); if unspecified, -infinity
525 // maxLevel = hal.getMaxLevel(); if unspecified, +infinity
526 // deviceManifestLevel = deviceManifest->level(); if unspecified, -infinity
527 // That is, if device manifest has no level, it is treated as an infinitely old device.
filterHalsByDeviceManifestLevel(HalManifest * out)528 void VintfObject::filterHalsByDeviceManifestLevel(HalManifest* out) {
529 auto deviceManifest = getDeviceHalManifest();
530 Level deviceManifestLevel =
531 deviceManifest != nullptr ? deviceManifest->level() : Level::UNSPECIFIED;
532
533 if (deviceManifest == nullptr) {
534 LOG(WARNING) << "Cannot fetch device manifest to determine target FCM version to "
535 "filter framework manifest HALs properly. Treating as infinitely old "
536 "device.";
537 } else if (deviceManifestLevel == Level::UNSPECIFIED) {
538 LOG(WARNING)
539 << "Cannot filter framework manifest HALs properly because target FCM version is "
540 "unspecified in the device manifest. Treating as infinitely old device.";
541 }
542
543 out->removeHalsIf([deviceManifestLevel](const ManifestHal& hal) {
544 if (hal.getMaxLevel() != Level::UNSPECIFIED) {
545 if (deviceManifestLevel != Level::UNSPECIFIED &&
546 hal.getMaxLevel() < deviceManifestLevel) {
547 return true;
548 }
549 }
550 if (hal.getMinLevel() != Level::UNSPECIFIED) {
551 if (deviceManifestLevel == Level::UNSPECIFIED ||
552 hal.getMinLevel() > deviceManifestLevel) {
553 return true;
554 }
555 }
556 return false;
557 });
558 }
559
appendLine(std::string * error,const std::string & message)560 static void appendLine(std::string* error, const std::string& message) {
561 if (error != nullptr) {
562 if (!error->empty()) *error += "\n";
563 *error += message;
564 }
565 }
566
getOneMatrix(const std::string & path,CompatibilityMatrix * out,std::string * error)567 status_t VintfObject::getOneMatrix(const std::string& path, CompatibilityMatrix* out,
568 std::string* error) {
569 std::string content;
570 status_t status = getFileSystem()->fetch(path, &content, error);
571 if (status != OK) {
572 return status;
573 }
574 if (!fromXml(out, content, error)) {
575 if (error) {
576 error->insert(0, "Cannot parse " + path + ": ");
577 }
578 return BAD_VALUE;
579 }
580 out->setFileName(path);
581 return OK;
582 }
583
getAllFrameworkMatrixLevels(std::vector<CompatibilityMatrix> * results,std::string * error)584 status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<CompatibilityMatrix>* results,
585 std::string* error) {
586 std::vector<std::string> dirs = {
587 kSystemVintfDir,
588 kSystemExtVintfDir,
589 kProductVintfDir,
590 };
591 for (const auto& dir : dirs) {
592 std::vector<std::string> fileNames;
593 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
594 if (listStatus == NAME_NOT_FOUND) {
595 if (error) {
596 error->clear();
597 }
598 continue;
599 }
600 if (listStatus != OK) {
601 return listStatus;
602 }
603 for (const std::string& fileName : fileNames) {
604 std::string path = dir + fileName;
605 CompatibilityMatrix namedMatrix;
606 std::string matrixError;
607 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
608 if (matrixStatus != OK) {
609 // Manifests and matrices share the same dir. Client may not have enough
610 // permissions to read system manifests, or may not be able to parse it.
611 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
612 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
613 continue;
614 }
615 results->emplace_back(std::move(namedMatrix));
616 }
617
618 if (dir == kSystemVintfDir && results->empty()) {
619 if (error) {
620 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
621 }
622 return NAME_NOT_FOUND;
623 }
624 }
625
626 if (results->empty()) {
627 if (error) {
628 *error =
629 "No framework matrices can be fetched or parsed. "
630 "The following directories are searched:\n " +
631 android::base::Join(dirs, "\n ");
632 }
633 return NAME_NOT_FOUND;
634 }
635 return OK;
636 }
637
GetRuntimeInfo(RuntimeInfo::FetchFlags flags)638 std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(RuntimeInfo::FetchFlags flags) {
639 return GetInstance()->getRuntimeInfo(flags);
640 }
getRuntimeInfo(RuntimeInfo::FetchFlags flags)641 std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(RuntimeInfo::FetchFlags flags) {
642 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
643
644 // Skip fetching information that has already been fetched previously.
645 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
646
647 if (mDeviceRuntimeInfo.object == nullptr) {
648 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
649 }
650
651 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
652 if (status != OK) {
653 // If only kernel FCM is needed, ignore errors when fetching RuntimeInfo because RuntimeInfo
654 // is not available on host. On host, the kernel level can still be inferred from device
655 // manifest.
656 // If other information is needed, flag the error by returning nullptr.
657 auto allExceptKernelFcm = RuntimeInfo::FetchFlag::ALL & ~RuntimeInfo::FetchFlag::KERNEL_FCM;
658 bool needDeviceRuntimeInfo = flags & allExceptKernelFcm;
659 if (needDeviceRuntimeInfo) {
660 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
661 return nullptr;
662 }
663 }
664
665 // To support devices without GKI, RuntimeInfo::fetchAllInformation does not report errors
666 // if kernel level cannot be retrieved. If so, fetch kernel FCM version from device HAL
667 // manifest and store it in RuntimeInfo too.
668 if (flags & RuntimeInfo::FetchFlag::KERNEL_FCM) {
669 Level deviceManifestKernelLevel = Level::UNSPECIFIED;
670 auto manifest = getDeviceHalManifest();
671 if (manifest) {
672 deviceManifestKernelLevel = manifest->inferredKernelLevel();
673 }
674 if (deviceManifestKernelLevel != Level::UNSPECIFIED) {
675 Level kernelLevel = mDeviceRuntimeInfo.object->kernelLevel();
676 if (kernelLevel == Level::UNSPECIFIED) {
677 mDeviceRuntimeInfo.object->setKernelLevel(deviceManifestKernelLevel);
678 } else if (kernelLevel != deviceManifestKernelLevel) {
679 LOG(WARNING) << "uname() reports kernel level " << kernelLevel
680 << " but device manifest sets kernel level "
681 << deviceManifestKernelLevel << ". Using kernel level " << kernelLevel;
682 }
683 }
684 }
685
686 mDeviceRuntimeInfo.fetchedFlags |= flags;
687 return mDeviceRuntimeInfo.object;
688 }
689
checkCompatibility(std::string * error,CheckFlags::Type flags)690 int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
691 status_t status = OK;
692 // null checks for files and runtime info
693 if (getFrameworkHalManifest() == nullptr) {
694 appendLine(error, "No framework manifest file from device or from update package");
695 status = NO_INIT;
696 }
697 if (getDeviceHalManifest() == nullptr) {
698 appendLine(error, "No device manifest file from device or from update package");
699 status = NO_INIT;
700 }
701 if (getFrameworkCompatibilityMatrix() == nullptr) {
702 appendLine(error, "No framework matrix file from device or from update package");
703 status = NO_INIT;
704 }
705 if (getDeviceCompatibilityMatrix() == nullptr) {
706 appendLine(error, "No device matrix file from device or from update package");
707 status = NO_INIT;
708 }
709
710 if (flags.isRuntimeInfoEnabled()) {
711 if (getRuntimeInfo() == nullptr) {
712 appendLine(error, "No runtime info from device");
713 status = NO_INIT;
714 }
715 }
716 if (status != OK) return status;
717
718 // compatiblity check.
719 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
720 if (error) {
721 error->insert(0,
722 "Device manifest and framework compatibility matrix are incompatible: ");
723 }
724 return INCOMPATIBLE;
725 }
726 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
727 if (error) {
728 error->insert(0,
729 "Framework manifest and device compatibility matrix are incompatible: ");
730 }
731 return INCOMPATIBLE;
732 }
733
734 if (flags.isRuntimeInfoEnabled()) {
735 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
736 flags)) {
737 if (error) {
738 error->insert(0,
739 "Runtime info and framework compatibility matrix are incompatible: ");
740 }
741 return INCOMPATIBLE;
742 }
743 }
744
745 return COMPATIBLE;
746 }
747
748 namespace details {
749
dumpFileList(const std::string & sku)750 std::vector<std::string> dumpFileList(const std::string& sku) {
751 std::vector<std::string> list = {
752 // clang-format off
753 kSystemVintfDir,
754 kVendorVintfDir,
755 kOdmVintfDir,
756 kProductVintfDir,
757 kSystemExtVintfDir,
758 kOdmLegacyManifest,
759 kVendorLegacyManifest,
760 kVendorLegacyMatrix,
761 kSystemLegacyManifest,
762 kSystemLegacyMatrix,
763 // clang-format on
764 };
765 if (!sku.empty()) {
766 list.push_back(getOdmProductManifestFile(kOdmLegacyVintfDir, sku));
767 }
768 return list;
769 }
770
771 } // namespace details
772
IsHalDeprecated(const MatrixHal & oldMatrixHal,const std::string & oldMatrixHalFileName,const CompatibilityMatrix & targetMatrix,const std::shared_ptr<const HalManifest> & deviceManifest,const ChildrenMap & childrenMap,std::string * appendedError)773 bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
774 const std::string& oldMatrixHalFileName,
775 const CompatibilityMatrix& targetMatrix,
776 const std::shared_ptr<const HalManifest>& deviceManifest,
777 const ChildrenMap& childrenMap, std::string* appendedError) {
778 bool isDeprecated = false;
779 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
780 if (IsInstanceDeprecated(oldMatrixInstance, oldMatrixHalFileName, targetMatrix,
781 deviceManifest, childrenMap, appendedError)) {
782 isDeprecated = true;
783 }
784 return true; // continue to check next instance
785 });
786 return isDeprecated;
787 }
788
789 // Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
790 // If any "@servedVersion::interface/servedInstance" in deviceManifest(package@x.y::interface)
791 // matches instancePattern, return true iff for all child interfaces (from
792 // GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
IsInstanceDeprecated(const MatrixInstance & oldMatrixInstance,const std::string & oldMatrixInstanceFileName,const CompatibilityMatrix & targetMatrix,const std::shared_ptr<const HalManifest> & deviceManifest,const ChildrenMap & childrenMap,std::string * appendedError)793 bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
794 const std::string& oldMatrixInstanceFileName,
795 const CompatibilityMatrix& targetMatrix,
796 const std::shared_ptr<const HalManifest>& deviceManifest,
797 const ChildrenMap& childrenMap, std::string* appendedError) {
798 const std::string& package = oldMatrixInstance.package();
799 const Version& version = oldMatrixInstance.versionRange().minVer();
800 const std::string& interface = oldMatrixInstance.interface();
801
802 std::vector<std::string> accumulatedErrors;
803
804 auto addErrorForInstance = [&](const ManifestInstance& manifestInstance) {
805 const std::string& servedInstance = manifestInstance.instance();
806 Version servedVersion = manifestInstance.version();
807 if (!oldMatrixInstance.matchInstance(servedInstance)) {
808 // ignore unrelated instance
809 return true; // continue
810 }
811
812 auto inheritance =
813 GetListedInstanceInheritance(oldMatrixInstance.format(), package, servedVersion,
814 interface, servedInstance, deviceManifest, childrenMap);
815 if (!inheritance.has_value()) {
816 accumulatedErrors.push_back(inheritance.error().message());
817 return true; // continue
818 }
819
820 std::vector<std::string> errors;
821 for (const auto& fqInstance : *inheritance) {
822 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
823 fqInstance, deviceManifest);
824 if (result.ok()) {
825 errors.clear();
826 break;
827 }
828 std::string error = result.error().message() + "\n ";
829 std::string servedFqInstanceString =
830 toFQNameString(package, servedVersion, interface, servedInstance);
831 if (fqInstance.string() == servedFqInstanceString) {
832 error += "because it matches ";
833 } else {
834 error += "because it inherits from " + fqInstance.string() + " that matches ";
835 }
836 error += oldMatrixInstance.description(oldMatrixInstance.versionRange().minVer()) +
837 " from " + oldMatrixInstanceFileName;
838 errors.push_back(error);
839 // Do not immediately think (package, servedVersion, interface, servedInstance)
840 // is deprecated; check parents too.
841 }
842
843 if (errors.empty()) {
844 return true; // continue
845 }
846 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
847 return true; // continue to next instance
848 };
849 (void)deviceManifest->forEachInstanceOfInterface(oldMatrixInstance.format(), package, version,
850 interface, addErrorForInstance);
851
852 if (accumulatedErrors.empty()) {
853 return false;
854 }
855 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
856 return true;
857 }
858
859 // Check if fqInstance is listed in |deviceManifest|.
IsInstanceListed(const std::shared_ptr<const HalManifest> & deviceManifest,HalFormat format,const FqInstance & fqInstance)860 bool VintfObject::IsInstanceListed(const std::shared_ptr<const HalManifest>& deviceManifest,
861 HalFormat format, const FqInstance& fqInstance) {
862 bool found = false;
863 (void)deviceManifest->forEachInstanceOfInterface(
864 format, fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
865 [&](const ManifestInstance& manifestInstance) {
866 if (manifestInstance.instance() == fqInstance.getInstance()) {
867 found = true;
868 }
869 return !found; // continue to next instance if not found
870 });
871 return found;
872 }
873
874 // Return a list of FqInstance, where each element:
875 // - is listed in |deviceManifest|; AND
876 // - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
GetListedInstanceInheritance(HalFormat format,const std::string & package,const Version & version,const std::string & interface,const std::string & instance,const std::shared_ptr<const HalManifest> & deviceManifest,const ChildrenMap & childrenMap)877 android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
878 HalFormat format, const std::string& package, const Version& version,
879 const std::string& interface, const std::string& instance,
880 const std::shared_ptr<const HalManifest>& deviceManifest, const ChildrenMap& childrenMap) {
881 FqInstance fqInstance;
882 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
883 return android::base::Error() << toFQNameString(package, version, interface, instance)
884 << " is not a valid FqInstance";
885 }
886
887 if (!IsInstanceListed(deviceManifest, format, fqInstance)) {
888 return {};
889 }
890
891 const auto& fqName = fqInstance.getFqNameString();
892
893 std::vector<FqInstance> ret;
894 ret.push_back(fqInstance);
895
896 auto childRange = childrenMap.equal_range(fqName);
897 for (auto it = childRange.first; it != childRange.second; ++it) {
898 const auto& childFqNameString = it->second;
899 FQName childFqName;
900 if (!childFqName.setTo(childFqNameString)) {
901 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
902 }
903 FqInstance childFqInstance;
904 if (!childFqInstance.setTo(childFqName.package(), childFqName.getPackageMajorVersion(),
905 childFqName.getPackageMinorVersion(),
906 childFqName.getInterfaceName(), fqInstance.getInstance())) {
907 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
908 << fqInstance.getInstance() << " as FqInstance";
909 continue;
910 }
911 if (!IsInstanceListed(deviceManifest, format, childFqInstance)) {
912 continue;
913 }
914 ret.push_back(childFqInstance);
915 }
916 return ret;
917 }
918
919 // Check if |fqInstance| is in |targetMatrix|; essentially equal to
920 // targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
921 // 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
922 // 2. package@x.z::interface/servedInstance is in targetMatrix but
923 // servedInstance is not in deviceManifest(package@x.z::interface)
IsFqInstanceDeprecated(const CompatibilityMatrix & targetMatrix,HalFormat format,const FqInstance & fqInstance,const std::shared_ptr<const HalManifest> & deviceManifest)924 android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
925 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
926 const std::shared_ptr<const HalManifest>& deviceManifest) {
927 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
928 bool foundInstance = false;
929 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
930 targetMatrix.forEachInstanceOfPackage(
931 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
932 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
933 targetMatrixInstance.interface() == fqInstance.getInterface() &&
934 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
935 targetMatrixMinVer =
936 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
937 foundInstance = true;
938 }
939 return true;
940 });
941 if (!foundInstance) {
942 return android::base::Error()
943 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
944 << targetMatrix.level() << "; it should not be served.";
945 }
946
947 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
948 bool targetVersionServed = false;
949
950 (void)deviceManifest->forEachInstanceOfInterface(
951 format, fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
952 [&](const ManifestInstance& manifestInstance) {
953 if (manifestInstance.instance() == fqInstance.getInstance()) {
954 targetVersionServed = true;
955 return false; // break
956 }
957 return true; // continue
958 });
959
960 if (!targetVersionServed) {
961 return android::base::Error()
962 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
963 }
964 return {};
965 }
966
checkDeprecation(const std::vector<HidlInterfaceMetadata> & hidlMetadata,std::string * error)967 int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
968 std::string* error) {
969 std::vector<CompatibilityMatrix> matrixFragments;
970 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
971 if (matrixFragmentsStatus != OK) {
972 return matrixFragmentsStatus;
973 }
974 if (matrixFragments.empty()) {
975 if (error && error->empty()) {
976 *error = "Cannot get framework matrix for each FCM version for unknown error.";
977 }
978 return NAME_NOT_FOUND;
979 }
980 auto deviceManifest = getDeviceHalManifest();
981 if (deviceManifest == nullptr) {
982 if (error) *error = "No device manifest.";
983 return NAME_NOT_FOUND;
984 }
985 Level deviceLevel = deviceManifest->level();
986 if (deviceLevel == Level::UNSPECIFIED) {
987 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
988 return BAD_VALUE;
989 }
990 std::string kernelLevelError;
991 Level kernelLevel = getKernelLevel(&kernelLevelError);
992 if (kernelLevel == Level::UNSPECIFIED) {
993 LOG(WARNING) << kernelLevelError;
994 }
995
996 std::vector<CompatibilityMatrix> targetMatrices;
997 // Partition matrixFragments into two groups, where the second group
998 // contains all matrices whose level == deviceLevel.
999 auto targetMatricesPartition = std::partition(
1000 matrixFragments.begin(), matrixFragments.end(),
1001 [&](const CompatibilityMatrix& matrix) { return matrix.level() != deviceLevel; });
1002 // Move these matrices into the targetMatrices vector...
1003 std::move(targetMatricesPartition, matrixFragments.end(), std::back_inserter(targetMatrices));
1004 if (targetMatrices.empty()) {
1005 if (error)
1006 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
1007 return NAME_NOT_FOUND;
1008 }
1009 // so that they can be combined into one matrix for deprecation checking.
1010 auto targetMatrix =
1011 CompatibilityMatrix::combine(deviceLevel, kernelLevel, &targetMatrices, error);
1012 if (targetMatrix == nullptr) {
1013 return BAD_VALUE;
1014 }
1015
1016 std::multimap<std::string, std::string> childrenMap;
1017 for (const auto& child : hidlMetadata) {
1018 for (const auto& parent : child.inherited) {
1019 childrenMap.emplace(parent, child.name);
1020 }
1021 }
1022 // AIDL does not have inheritance.
1023
1024 // Find a list of possibly deprecated HALs by comparing |deviceManifest| with older matrices.
1025 // Matrices with unspecified level are considered "current".
1026 bool isDeprecated = false;
1027 for (auto it = matrixFragments.begin(); it < targetMatricesPartition; ++it) {
1028 const auto& namedMatrix = *it;
1029 if (namedMatrix.level() == Level::UNSPECIFIED) continue;
1030 if (namedMatrix.level() > deviceLevel) continue;
1031 for (const MatrixHal& hal : namedMatrix.getHals()) {
1032 if (IsHalDeprecated(hal, namedMatrix.fileName(), *targetMatrix, deviceManifest,
1033 childrenMap, error)) {
1034 isDeprecated = true;
1035 }
1036 }
1037 }
1038
1039 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
1040 }
1041
getKernelLevel(std::string * error)1042 Level VintfObject::getKernelLevel(std::string* error) {
1043 auto runtimeInfo = getRuntimeInfo(RuntimeInfo::FetchFlag::KERNEL_FCM);
1044 if (!runtimeInfo) {
1045 if (error) *error = "Cannot retrieve runtime info with kernel level.";
1046 return Level::UNSPECIFIED;
1047 }
1048 if (runtimeInfo->kernelLevel() != Level::UNSPECIFIED) {
1049 return runtimeInfo->kernelLevel();
1050 }
1051 if (error) {
1052 *error = "Both device manifest and kernel release do not specify kernel FCM version.";
1053 }
1054 return Level::UNSPECIFIED;
1055 }
1056
getFileSystem()1057 const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
1058 return mFileSystem;
1059 }
1060
getPropertyFetcher()1061 const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
1062 return mPropertyFetcher;
1063 }
1064
getRuntimeInfoFactory()1065 const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
1066 return mRuntimeInfoFactory;
1067 }
1068
hasFrameworkCompatibilityMatrixExtensions()1069 android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
1070 std::vector<CompatibilityMatrix> matrixFragments;
1071 std::string error;
1072 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
1073 if (status != OK) {
1074 return android::base::Error(-status)
1075 << "Cannot get all framework matrix fragments: " << error;
1076 }
1077 for (const auto& namedMatrix : matrixFragments) {
1078 // Returns true if product matrix exists.
1079 if (android::base::StartsWith(namedMatrix.fileName(), kProductVintfDir)) {
1080 return true;
1081 }
1082 // Returns true if system_ext matrix exists.
1083 if (android::base::StartsWith(namedMatrix.fileName(), kSystemExtVintfDir)) {
1084 return true;
1085 }
1086 // Returns true if device system matrix exists.
1087 if (android::base::StartsWith(namedMatrix.fileName(), kSystemVintfDir) &&
1088 namedMatrix.level() == Level::UNSPECIFIED && !namedMatrix.getHals().empty()) {
1089 return true;
1090 }
1091 }
1092 return false;
1093 }
1094
checkUnusedHals(const std::vector<HidlInterfaceMetadata> & hidlMetadata)1095 android::base::Result<void> VintfObject::checkUnusedHals(
1096 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
1097 auto matrix = getFrameworkCompatibilityMatrix();
1098 if (matrix == nullptr) {
1099 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
1100 }
1101 auto manifest = getDeviceHalManifest();
1102 if (manifest == nullptr) {
1103 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
1104 }
1105 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
1106 if (!unused.empty()) {
1107 return android::base::Error()
1108 << "The following instances are in the device manifest but "
1109 << "not specified in framework compatibility matrix: \n"
1110 << " " << android::base::Join(unused, "\n ") << "\n"
1111 << "Suggested fix:\n"
1112 << "1. Update deprecated HALs to the latest version.\n"
1113 << "2. Check for any typos in device manifest or framework compatibility "
1114 << "matrices with FCM version >= " << matrix->level() << ".\n"
1115 << "3. For new platform HALs, add them to any framework compatibility matrix "
1116 << "with FCM version >= " << matrix->level() << " where applicable.\n"
1117 << "4. For device-specific HALs, add to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
1118 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
1119 }
1120 return {};
1121 }
1122
1123 namespace {
1124
1125 // Insert |name| into |ret| if shouldCheck(name).
InsertIf(const std::string & name,const std::function<bool (const std::string &)> & shouldCheck,std::set<std::string> * ret)1126 void InsertIf(const std::string& name, const std::function<bool(const std::string&)>& shouldCheck,
1127 std::set<std::string>* ret) {
1128 if (shouldCheck(name)) ret->insert(name);
1129 }
1130
StripHidlInterface(const std::string & fqNameString)1131 std::string StripHidlInterface(const std::string& fqNameString) {
1132 FQName fqName;
1133 if (!fqName.setTo(fqNameString)) {
1134 return "";
1135 }
1136 return fqName.getPackageAndVersion().string();
1137 }
1138
1139 // StripAidlType(android.hardware.foo.IFoo)
1140 // -> android.hardware.foo
StripAidlType(const std::string & type)1141 std::string StripAidlType(const std::string& type) {
1142 auto items = android::base::Split(type, ".");
1143 if (items.empty()) {
1144 return "";
1145 }
1146 items.erase(items.end() - 1);
1147 return android::base::Join(items, ".");
1148 }
1149
1150 // GetAidlPackageAndVersion(android.hardware.foo.IFoo, 1)
1151 // -> android.hardware.foo@1
GetAidlPackageAndVersion(const std::string & package,size_t version)1152 std::string GetAidlPackageAndVersion(const std::string& package, size_t version) {
1153 return package + "@" + std::to_string(version);
1154 }
1155
1156 // android.hardware.foo@1.0
HidlMetadataToPackagesAndVersions(const std::vector<HidlInterfaceMetadata> & hidlMetadata,const std::function<bool (const std::string &)> & shouldCheck)1157 std::set<std::string> HidlMetadataToPackagesAndVersions(
1158 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1159 const std::function<bool(const std::string&)>& shouldCheck) {
1160 std::set<std::string> ret;
1161 for (const auto& item : hidlMetadata) {
1162 InsertIf(StripHidlInterface(item.name), shouldCheck, &ret);
1163 }
1164 return ret;
1165 }
1166
1167 // android.hardware.foo@1
1168 // All non-vintf stable interfaces are filtered out.
AidlMetadataToVintfPackagesAndVersions(const std::vector<AidlInterfaceMetadata> & aidlMetadata,const std::function<bool (const std::string &)> & shouldCheck)1169 android::base::Result<std::set<std::string>> AidlMetadataToVintfPackagesAndVersions(
1170 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1171 const std::function<bool(const std::string&)>& shouldCheck) {
1172 std::set<std::string> ret;
1173 for (const auto& item : aidlMetadata) {
1174 if (item.stability != "vintf") {
1175 continue;
1176 }
1177 for (const auto& type : item.types) {
1178 auto package = StripAidlType(type);
1179 for (const auto& version : item.versions) {
1180 InsertIf(GetAidlPackageAndVersion(package, version), shouldCheck, &ret);
1181 }
1182 if (item.has_development) {
1183 auto maxVerIt = std::max_element(item.versions.begin(), item.versions.end());
1184 // If no frozen versions, the in-development version is 1.
1185 size_t maxVer = maxVerIt == item.versions.end() ? 0 : *maxVerIt;
1186 auto nextVer = maxVer + 1;
1187 if (nextVer < maxVer) {
1188 return android::base::Error()
1189 << "Bad version " << maxVer << " for AIDL type " << type
1190 << "; integer overflow when inferring in-development version";
1191 }
1192 InsertIf(GetAidlPackageAndVersion(package, nextVer), shouldCheck, &ret);
1193 }
1194 }
1195 }
1196 return ret;
1197 }
1198
1199 // android.hardware.foo@1.0::IFoo.
1200 // Note that UDTs are not filtered out, so there might be non-interface types.
HidlMetadataToNames(const std::vector<HidlInterfaceMetadata> & hidlMetadata)1201 std::set<std::string> HidlMetadataToNames(const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
1202 std::set<std::string> ret;
1203 for (const auto& item : hidlMetadata) {
1204 ret.insert(item.name);
1205 }
1206 return ret;
1207 }
1208
1209 // android.hardware.foo.IFoo
1210 // Note that UDTs are not filtered out, so there might be non-interface types.
1211 // All non-vintf stable interfaces are filtered out.
AidlMetadataToVintfNames(const std::vector<AidlInterfaceMetadata> & aidlMetadata)1212 std::set<std::string> AidlMetadataToVintfNames(
1213 const std::vector<AidlInterfaceMetadata>& aidlMetadata) {
1214 std::set<std::string> ret;
1215 for (const auto& item : aidlMetadata) {
1216 if (item.stability == "vintf") {
1217 for (const auto& type : item.types) {
1218 ret.insert(type);
1219 }
1220 }
1221 }
1222 return ret;
1223 }
1224
1225 } // anonymous namespace
1226
getAllFrameworkMatrixLevels()1227 android::base::Result<std::vector<CompatibilityMatrix>> VintfObject::getAllFrameworkMatrixLevels() {
1228 // Get all framework matrix fragments instead of the combined framework compatibility matrix
1229 // because the latter may omit interfaces from the latest FCM if device target-level is not
1230 // the latest.
1231 std::vector<CompatibilityMatrix> matrixFragments;
1232 std::string error;
1233 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, &error);
1234 if (matrixFragmentsStatus != OK) {
1235 return android::base::Error(-matrixFragmentsStatus)
1236 << "Unable to get all framework matrix fragments: " << error;
1237 }
1238 if (matrixFragments.empty()) {
1239 if (error.empty()) {
1240 error = "Cannot get framework matrix for each FCM version for unknown error.";
1241 }
1242 return android::base::Error(-NAME_NOT_FOUND) << error;
1243 }
1244 return matrixFragments;
1245 }
1246
1247 // Check the compatibility matrix for the latest available AIDL interfaces only
1248 // when AIDL_USE_UNFROZEN is defined
getCheckAidlCompatMatrix()1249 bool VintfObject::getCheckAidlCompatMatrix() {
1250 #ifdef AIDL_USE_UNFROZEN
1251 constexpr bool kAidlUseUnfrozen = true;
1252 #else
1253 constexpr bool kAidlUseUnfrozen = false;
1254 #endif
1255 return mFakeCheckAidlCompatibilityMatrix.value_or(kAidlUseUnfrozen);
1256 }
1257
checkMissingHalsInMatrices(const std::vector<HidlInterfaceMetadata> & hidlMetadata,const std::vector<AidlInterfaceMetadata> & aidlMetadata,std::function<bool (const std::string &)> shouldCheckHidl,std::function<bool (const std::string &)> shouldCheckAidl)1258 android::base::Result<void> VintfObject::checkMissingHalsInMatrices(
1259 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1260 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1261 std::function<bool(const std::string&)> shouldCheckHidl,
1262 std::function<bool(const std::string&)> shouldCheckAidl) {
1263 auto matrixFragments = getAllFrameworkMatrixLevels();
1264 if (!matrixFragments.ok()) return matrixFragments.error();
1265
1266 // Filter aidlMetadata and hidlMetadata with shouldCheck.
1267 auto allAidlPackagesAndVersions =
1268 AidlMetadataToVintfPackagesAndVersions(aidlMetadata, shouldCheckAidl);
1269 if (!allAidlPackagesAndVersions.ok()) return allAidlPackagesAndVersions.error();
1270 auto allHidlPackagesAndVersions =
1271 HidlMetadataToPackagesAndVersions(hidlMetadata, shouldCheckHidl);
1272
1273 // Filter out instances in allAidlVintfPackages and allHidlPackagesAndVersions that are
1274 // in the matrices.
1275 std::vector<std::string> errors;
1276 for (const auto& matrix : matrixFragments.value()) {
1277 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1278 switch (matrixInstance.format()) {
1279 case HalFormat::AIDL: {
1280 for (Version v = matrixInstance.versionRange().minVer();
1281 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
1282 allAidlPackagesAndVersions->erase(
1283 GetAidlPackageAndVersion(matrixInstance.package(), v.minorVer));
1284 }
1285 return true; // continue to next instance
1286 }
1287 case HalFormat::HIDL: {
1288 for (Version v = matrixInstance.versionRange().minVer();
1289 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
1290 allHidlPackagesAndVersions.erase(
1291 toFQNameString(matrixInstance.package(), v));
1292 }
1293 return true; // continue to next instance
1294 }
1295 default: {
1296 for (Version v = matrixInstance.versionRange().minVer();
1297 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
1298 if (shouldCheckHidl(toFQNameString(matrixInstance.package(), v))) {
1299 errors.push_back("HAL package " + matrixInstance.package() +
1300 " is not allowed to have format " +
1301 to_string(matrixInstance.format()) + ".");
1302 }
1303 }
1304 return true; // continue to next instance
1305 }
1306 }
1307 });
1308 }
1309
1310 if (!allHidlPackagesAndVersions.empty()) {
1311 errors.push_back(
1312 "The following HIDL packages are not found in any compatibility matrix fragments:\t\n" +
1313 android::base::Join(allHidlPackagesAndVersions, "\t\n"));
1314 }
1315 if (!allAidlPackagesAndVersions->empty() && getCheckAidlCompatMatrix()) {
1316 errors.push_back(
1317 "The following AIDL packages are not found in any compatibility matrix fragments:\t\n" +
1318 android::base::Join(*allAidlPackagesAndVersions, "\t\n"));
1319 }
1320
1321 if (!errors.empty()) {
1322 return android::base::Error() << android::base::Join(errors, "\n");
1323 }
1324
1325 return {};
1326 }
1327
checkMatrixHalsHasDefinition(const std::vector<HidlInterfaceMetadata> & hidlMetadata,const std::vector<AidlInterfaceMetadata> & aidlMetadata)1328 android::base::Result<void> VintfObject::checkMatrixHalsHasDefinition(
1329 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1330 const std::vector<AidlInterfaceMetadata>& aidlMetadata) {
1331 auto matrixFragments = getAllFrameworkMatrixLevels();
1332 if (!matrixFragments.ok()) return matrixFragments.error();
1333
1334 auto allAidlVintfNames = AidlMetadataToVintfNames(aidlMetadata);
1335 auto allHidlNames = HidlMetadataToNames(hidlMetadata);
1336 std::set<std::string> badAidlInterfaces;
1337 std::set<std::string> badHidlInterfaces;
1338
1339 std::vector<std::string> errors;
1340 for (const auto& matrix : matrixFragments.value()) {
1341 if (matrix.level() == Level::UNSPECIFIED) {
1342 LOG(INFO) << "Skip checkMatrixHalsHasDefinition() on " << matrix.fileName()
1343 << " with no level.";
1344 continue;
1345 }
1346
1347 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1348 switch (matrixInstance.format()) {
1349 case HalFormat::AIDL: {
1350 auto matrixInterface =
1351 toAidlFqnameString(matrixInstance.package(), matrixInstance.interface());
1352 if (allAidlVintfNames.find(matrixInterface) == allAidlVintfNames.end()) {
1353 errors.push_back(
1354 "AIDL interface " + matrixInterface + " is referenced in " +
1355 matrix.fileName() +
1356 ", but there is no corresponding .aidl definition associated with an "
1357 "aidl_interface module in this build. Typo?");
1358 }
1359 return true; // continue to next instance
1360 }
1361 case HalFormat::HIDL: {
1362 for (Version v = matrixInstance.versionRange().minVer();
1363 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
1364 auto matrixInterface = matrixInstance.interfaceDescription(v);
1365 if (allHidlNames.find(matrixInterface) == allHidlNames.end()) {
1366 errors.push_back(
1367 "HIDL interface " + matrixInterface + " is referenced in " +
1368 matrix.fileName() +
1369 ", but there is no corresponding .hal definition associated with "
1370 "a hidl_interface module in this build. Typo?");
1371 }
1372 }
1373 return true; // continue to next instance
1374 }
1375 default: {
1376 // We do not have data for native HALs.
1377 return true; // continue to next instance
1378 }
1379 }
1380 });
1381 }
1382
1383 if (!errors.empty()) {
1384 return android::base::Error() << android::base::Join(errors, "\n");
1385 }
1386
1387 return {};
1388 }
1389
getLatestMinLtsAtFcmVersion(Level fcmVersion)1390 android::base::Result<KernelVersion> VintfObject::getLatestMinLtsAtFcmVersion(Level fcmVersion) {
1391 auto allFcms = getAllFrameworkMatrixLevels();
1392 if (!allFcms.ok()) return allFcms.error();
1393
1394 // Get the max of latestKernelMinLts for all FCM fragments at |fcmVersion|.
1395 // Usually there's only one such fragment.
1396 KernelVersion foundLatestMinLts;
1397 for (const auto& fcm : *allFcms) {
1398 if (fcm.level() != fcmVersion) {
1399 continue;
1400 }
1401 // Note: this says "minLts", but "Latest" indicates that it is a max value.
1402 auto thisLatestMinLts = fcm.getLatestKernelMinLts();
1403 if (foundLatestMinLts < thisLatestMinLts) foundLatestMinLts = thisLatestMinLts;
1404 }
1405 if (foundLatestMinLts != KernelVersion{}) {
1406 return foundLatestMinLts;
1407 }
1408 return android::base::Error(-NAME_NOT_FOUND)
1409 << "Can't find compatibility matrix fragment for level " << fcmVersion;
1410 }
1411
1412 // make_unique does not work because VintfObject constructor is private.
Builder()1413 VintfObject::Builder::Builder()
1414 : VintfObjectBuilder(std::unique_ptr<VintfObject>(new VintfObject())) {}
1415
1416 namespace details {
1417
~VintfObjectBuilder()1418 VintfObjectBuilder::~VintfObjectBuilder() {}
1419
setFileSystem(std::unique_ptr<FileSystem> && e)1420 VintfObjectBuilder& VintfObjectBuilder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
1421 mObject->mFileSystem = std::move(e);
1422 return *this;
1423 }
1424
setRuntimeInfoFactory(std::unique_ptr<ObjectFactory<RuntimeInfo>> && e)1425 VintfObjectBuilder& VintfObjectBuilder::setRuntimeInfoFactory(
1426 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
1427 mObject->mRuntimeInfoFactory = std::move(e);
1428 return *this;
1429 }
1430
setPropertyFetcher(std::unique_ptr<PropertyFetcher> && e)1431 VintfObjectBuilder& VintfObjectBuilder::setPropertyFetcher(std::unique_ptr<PropertyFetcher>&& e) {
1432 mObject->mPropertyFetcher = std::move(e);
1433 return *this;
1434 }
1435
buildInternal()1436 std::unique_ptr<VintfObject> VintfObjectBuilder::buildInternal() {
1437 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
1438 if (!mObject->mRuntimeInfoFactory)
1439 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
1440 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
1441 return std::move(mObject);
1442 }
1443
1444 } // namespace details
1445
1446 } // namespace vintf
1447 } // namespace android
1448