blob: 2e4638690266bcee12e96a4e4035ed66dae13007 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 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
Andreas Huber5345ec22016-07-29 13:33:27 -070017#include "Coordinator.h"
18
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070019#include <dirent.h>
Andreas Huberd2943e12016-08-05 11:59:31 -070020#include <sys/stat.h>
Andreas Huber5345ec22016-07-29 13:33:27 -070021
Yifan Hong78b38d12017-02-13 18:14:46 +000022#include <algorithm>
23#include <iterator>
24
25#include <android-base/logging.h>
26#include <hidl-util/StringHelper.h>
27
28#include "AST.h"
29#include "Interface.h"
30
Andreas Huber0d0f9a22016-08-17 10:26:11 -070031extern android::status_t parseFile(android::AST *ast);
Andreas Huber5345ec22016-07-29 13:33:27 -070032
Yifan Hong78b38d12017-02-13 18:14:46 +000033static bool existdir(const char *name) {
34 DIR *dir = opendir(name);
35 if (dir == NULL) {
36 return false;
37 }
38 closedir(dir);
39 return true;
40}
41
Andreas Huber5345ec22016-07-29 13:33:27 -070042namespace android {
43
Andreas Huberdca261f2016-08-04 13:47:51 -070044Coordinator::Coordinator(
45 const std::vector<std::string> &packageRootPaths,
46 const std::vector<std::string> &packageRoots)
47 : mPackageRootPaths(packageRootPaths),
48 mPackageRoots(packageRoots) {
49 // empty
Andreas Huberdc981332016-07-29 15:46:54 -070050}
51
Andreas Huberdca261f2016-08-04 13:47:51 -070052Coordinator::~Coordinator() {
53 // empty
54}
Andreas Huber5345ec22016-07-29 13:33:27 -070055
Andreas Huber39fa7182016-08-19 14:27:33 -070056AST *Coordinator::parse(const FQName &fqName, std::set<AST *> *parsedASTs) {
Andreas Huber68f24592016-07-29 14:53:48 -070057 CHECK(fqName.isFullyQualified());
58
Steven Morelandd537ab02016-09-12 10:32:01 -070059 auto it = mCache.find(fqName);
60 if (it != mCache.end()) {
61 AST *ast = (*it).second;
Andreas Huber5345ec22016-07-29 13:33:27 -070062
Andreas Huber39fa7182016-08-19 14:27:33 -070063 if (ast != nullptr && parsedASTs != nullptr) {
64 parsedASTs->insert(ast);
65 }
66
Andreas Huber5345ec22016-07-29 13:33:27 -070067 return ast;
68 }
69
Andreas Huber68f24592016-07-29 14:53:48 -070070 // Add this to the cache immediately, so we can discover circular imports.
Steven Morelandd537ab02016-09-12 10:32:01 -070071 mCache[fqName] = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -070072
73 AST *typesAST = nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -070074
Andreas Huber68f24592016-07-29 14:53:48 -070075 if (fqName.name() != "types") {
76 // Any interface file implicitly imports its package's types.hal.
Yifan Hongfece6ec2017-01-12 17:04:04 -080077 FQName typesName = fqName.getTypesForPackage();
Andreas Huber39fa7182016-08-19 14:27:33 -070078 typesAST = parse(typesName, parsedASTs);
Andreas Huber68f24592016-07-29 14:53:48 -070079
80 // fall through.
81 }
82
Andreas Huberdca261f2016-08-04 13:47:51 -070083 std::string path = getPackagePath(fqName);
84
Andreas Huber68f24592016-07-29 14:53:48 -070085 path.append(fqName.name());
86 path.append(".hal");
Andreas Huber5345ec22016-07-29 13:33:27 -070087
Andreas Huber0d0f9a22016-08-17 10:26:11 -070088 AST *ast = new AST(this, path);
Andreas Huber39fa7182016-08-19 14:27:33 -070089
90 if (typesAST != NULL) {
91 // If types.hal for this AST's package existed, make it's defined
92 // types available to the (about to be parsed) AST right away.
93 ast->addImportedAST(typesAST);
94 }
95
Andreas Huber0d0f9a22016-08-17 10:26:11 -070096 status_t err = parseFile(ast);
Andreas Huber5345ec22016-07-29 13:33:27 -070097
Andreas Huber68f24592016-07-29 14:53:48 -070098 if (err != OK) {
Andreas Huber6e584b72016-07-29 16:14:07 -070099 // LOG(ERROR) << "parsing '" << path << "' FAILED.";
100
Andreas Huber68f24592016-07-29 14:53:48 -0700101 delete ast;
Andreas Huber39fa7182016-08-19 14:27:33 -0700102 ast = nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -0700103
Andreas Huber39fa7182016-08-19 14:27:33 -0700104 return nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -0700105 }
106
Andreas Hubera2723d22016-07-29 15:36:07 -0700107 if (ast->package().package() != fqName.package()
108 || ast->package().version() != fqName.version()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700109 fprintf(stderr,
110 "ERROR: File at '%s' does not match expected package and/or "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700111 "version.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700112 path.c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700113
114 err = UNKNOWN_ERROR;
115 } else {
116 std::string ifaceName;
117 if (ast->isInterface(&ifaceName)) {
118 if (fqName.name() == "types") {
Andreas Huber70a59e12016-08-16 12:57:01 -0700119 fprintf(stderr,
120 "ERROR: File at '%s' declares an interface '%s' "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700121 "instead of the expected types common to the package.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700122 path.c_str(),
123 ifaceName.c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700124
125 err = UNKNOWN_ERROR;
126 } else if (ifaceName != fqName.name()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700127 fprintf(stderr,
128 "ERROR: File at '%s' does not declare interface type "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700129 "'%s'.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700130 path.c_str(),
131 fqName.name().c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700132
133 err = UNKNOWN_ERROR;
134 }
135 } else if (fqName.name() != "types") {
Andreas Huber70a59e12016-08-16 12:57:01 -0700136 fprintf(stderr,
137 "ERROR: File at '%s' declares types rather than the "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700138 "expected interface type '%s'.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700139 path.c_str(),
140 fqName.name().c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700141
142 err = UNKNOWN_ERROR;
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700143 } else if (ast->containsInterfaces()) {
144 fprintf(stderr,
145 "ERROR: types.hal file at '%s' declares at least one "
146 "interface type.\n",
147 path.c_str());
148
149 err = UNKNOWN_ERROR;
Andreas Hubera2723d22016-07-29 15:36:07 -0700150 }
151 }
152
153 if (err != OK) {
154 delete ast;
Andreas Huber39fa7182016-08-19 14:27:33 -0700155 ast = nullptr;
Andreas Hubera2723d22016-07-29 15:36:07 -0700156
Andreas Huber39fa7182016-08-19 14:27:33 -0700157 return nullptr;
Andreas Hubera2723d22016-07-29 15:36:07 -0700158 }
159
Andreas Huber39fa7182016-08-19 14:27:33 -0700160 if (parsedASTs != nullptr) { parsedASTs->insert(ast); }
161
Steven Morelandd537ab02016-09-12 10:32:01 -0700162 mCache[fqName] = ast;
Andreas Huber5345ec22016-07-29 13:33:27 -0700163
Yifan Hong78b38d12017-02-13 18:14:46 +0000164 // For each .hal file that hidl-gen parses, the whole package will be checked.
165 err = enforceRestrictionsOnPackage(fqName);
166 if (err != OK) {
167 delete ast;
168 ast = nullptr;
169 return nullptr;
170 }
171
Andreas Huber5345ec22016-07-29 13:33:27 -0700172 return ast;
173}
174
Andreas Huberdca261f2016-08-04 13:47:51 -0700175std::vector<std::string>::const_iterator
176Coordinator::findPackageRoot(const FQName &fqName) const {
Andreas Huber5345ec22016-07-29 13:33:27 -0700177 CHECK(!fqName.package().empty());
178 CHECK(!fqName.version().empty());
Andreas Huber5345ec22016-07-29 13:33:27 -0700179
Andreas Huberdca261f2016-08-04 13:47:51 -0700180 // Find the right package prefix and path for this FQName. For
181 // example, if FQName is "android.hardware.nfc@1.0::INfc", and the
182 // prefix:root is set to [ "android.hardware:hardware/interfaces",
183 // "vendor.qcom.hardware:vendor/qcom"], then we will identify the
184 // prefix "android.hardware" and the package root
185 // "hardware/interfaces".
186
Andreas Huberdca261f2016-08-04 13:47:51 -0700187 auto it = mPackageRoots.begin();
Steven Moreland62709d72017-01-18 10:14:51 -0800188 auto ret = mPackageRoots.end();
Andreas Huberdca261f2016-08-04 13:47:51 -0700189 for (; it != mPackageRoots.end(); it++) {
Steven Moreland62709d72017-01-18 10:14:51 -0800190 if (!fqName.inPackage(*it)) {
191 continue;
Andreas Huberdca261f2016-08-04 13:47:51 -0700192 }
Steven Moreland62709d72017-01-18 10:14:51 -0800193
194 CHECK(ret == mPackageRoots.end())
195 << "Multiple package roots found for " << fqName.string()
196 << " (" << *it << " and " << *ret << ")";
197
198 ret = it;
Andreas Huberdca261f2016-08-04 13:47:51 -0700199 }
Steven Moreland62709d72017-01-18 10:14:51 -0800200 CHECK(ret != mPackageRoots.end())
Andreas Huber401cd162016-08-26 10:40:30 -0700201 << "Unable to find package root for " << fqName.string();
Andreas Huberdca261f2016-08-04 13:47:51 -0700202
Steven Moreland62709d72017-01-18 10:14:51 -0800203 return ret;
Andreas Huberdca261f2016-08-04 13:47:51 -0700204}
205
206std::string Coordinator::getPackageRoot(const FQName &fqName) const {
207 auto it = findPackageRoot(fqName);
208 auto prefix = *it;
209 return prefix;
210}
211
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700212std::string Coordinator::getPackageRootPath(const FQName &fqName) const {
213 auto it = findPackageRoot(fqName);
214 auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)];
215 return root;
216}
217
Yifan Hongc8934042016-11-17 17:10:52 -0800218std::string Coordinator::getPackageRootOption(const FQName &fqName) const {
219 return getPackageRoot(fqName) + ":" + getPackageRootPath(fqName);
220}
221
Andreas Huberdca261f2016-08-04 13:47:51 -0700222std::string Coordinator::getPackagePath(
Yifan Hong97288ac2016-12-12 16:03:51 -0800223 const FQName &fqName, bool relative, bool sanitized) const {
Andreas Huberdca261f2016-08-04 13:47:51 -0700224
225 auto it = findPackageRoot(fqName);
226 auto prefix = *it;
227 auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)];
228
229 // Make sure the prefix ends on a '.' and the root path on a '/'
230 if ((*--prefix.end()) != '.') {
231 prefix += '.';
232 }
233
234 if ((*--root.end()) != '/') {
235 root += '/';
236 }
237
238 // Given FQName of "android.hardware.nfc@1.0::IFoo" and a prefix
239 // "android.hardware.", the suffix is "nfc@1.0::IFoo".
240 const std::string packageSuffix = fqName.package().substr(prefix.length());
Andreas Huber5345ec22016-07-29 13:33:27 -0700241
Andreas Huber881227d2016-08-02 14:20:21 -0700242 std::string packagePath;
243 if (!relative) {
Andreas Huberdca261f2016-08-04 13:47:51 -0700244 packagePath = root;
Andreas Huber881227d2016-08-02 14:20:21 -0700245 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700246
247 size_t startPos = 0;
248 size_t dotPos;
249 while ((dotPos = packageSuffix.find('.', startPos)) != std::string::npos) {
250 packagePath.append(packageSuffix.substr(startPos, dotPos - startPos));
251 packagePath.append("/");
252
253 startPos = dotPos + 1;
254 }
255 CHECK_LT(startPos + 1, packageSuffix.length());
256 packagePath.append(packageSuffix.substr(startPos));
257 packagePath.append("/");
258
Yifan Hong97288ac2016-12-12 16:03:51 -0800259 packagePath.append(sanitized ? fqName.sanitizedVersion() : fqName.version());
Andreas Huber5345ec22016-07-29 13:33:27 -0700260 packagePath.append("/");
261
262 return packagePath;
263}
264
Andreas Huberd2943e12016-08-05 11:59:31 -0700265status_t Coordinator::getPackageInterfaceFiles(
266 const FQName &package,
267 std::vector<std::string> *fileNames) const {
268 fileNames->clear();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700269
Andreas Huberd2943e12016-08-05 11:59:31 -0700270 const std::string packagePath = getPackagePath(package);
271
272 DIR *dir = opendir(packagePath.c_str());
273
274 if (dir == NULL) {
Steven Moreland261370a2016-08-29 15:36:59 -0700275 LOG(ERROR) << "Could not open package path: " << packagePath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700276 return -errno;
277 }
278
279 struct dirent *ent;
280 while ((ent = readdir(dir)) != NULL) {
281 if (ent->d_type != DT_REG) {
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700282 continue;
283 }
284
Andreas Huberd2943e12016-08-05 11:59:31 -0700285 const auto suffix = ".hal";
286 const auto suffix_len = std::strlen(suffix);
287 const auto d_namelen = strlen(ent->d_name);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700288
Andreas Huberd2943e12016-08-05 11:59:31 -0700289 if (d_namelen < suffix_len
290 || strcmp(ent->d_name + d_namelen - suffix_len, suffix)) {
291 continue;
Andreas Hubere61e3f72016-08-03 10:22:03 -0700292 }
Andreas Huberd2943e12016-08-05 11:59:31 -0700293
294 fileNames->push_back(std::string(ent->d_name, d_namelen - suffix_len));
295 }
296
297 closedir(dir);
298 dir = NULL;
299
Iliyan Malchev5e8bcfa2016-09-09 16:19:57 -0700300 std::sort(fileNames->begin(), fileNames->end(),
301 [](const std::string& lhs, const std::string& rhs) -> bool {
302 if (lhs == "types") {
303 return true;
304 }
305 if (rhs == "types") {
306 return false;
307 }
308 return lhs < rhs;
309 });
310
Andreas Huberd2943e12016-08-05 11:59:31 -0700311 return OK;
312}
313
Steven Morelandaa186832016-09-26 13:51:43 -0700314status_t Coordinator::appendPackageInterfacesToVector(
Andreas Huberd2943e12016-08-05 11:59:31 -0700315 const FQName &package,
316 std::vector<FQName> *packageInterfaces) const {
317 packageInterfaces->clear();
318
319 std::vector<std::string> fileNames;
320 status_t err = getPackageInterfaceFiles(package, &fileNames);
321
322 if (err != OK) {
323 return err;
324 }
325
326 for (const auto &fileName : fileNames) {
327 FQName subFQName(
Yifan Hong90ea87f2016-11-01 14:25:47 -0700328 package.package() + package.atVersion() + "::" + fileName);
Andreas Huberd2943e12016-08-05 11:59:31 -0700329
330 if (!subFQName.isValid()) {
331 LOG(WARNING)
332 << "Whole-package import encountered invalid filename '"
333 << fileName
334 << "' in package "
335 << package.package()
Yifan Hong90ea87f2016-11-01 14:25:47 -0700336 << package.atVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700337
338 continue;
339 }
340
341 packageInterfaces->push_back(subFQName);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700342 }
343
344 return OK;
345}
346
Andreas Huberd2943e12016-08-05 11:59:31 -0700347std::string Coordinator::convertPackageRootToPath(const FQName &fqName) const {
348 std::string packageRoot = getPackageRoot(fqName);
349
350 if (*(packageRoot.end()--) != '.') {
351 packageRoot += '.';
352 }
353
354 std::replace(packageRoot.begin(), packageRoot.end(), '.', '/');
355
356 return packageRoot; // now converted to a path
357}
358
Yifan Hong78b38d12017-02-13 18:14:46 +0000359
360status_t Coordinator::enforceRestrictionsOnPackage(const FQName &fqName) {
361 // need fqName to be something like android.hardware.foo@1.0.
362 // name and valueName is ignored.
363 if (fqName.package().empty() || fqName.version().empty()) {
364 LOG(ERROR) << "Cannot enforce restrictions on package " << fqName.string()
365 << ": package or version is missing.";
366 return BAD_VALUE;
367 }
368 FQName package = fqName.getPackageAndVersion();
369 // look up cache.
370 if (mPackagesEnforced.find(package) != mPackagesEnforced.end()) {
371 return OK;
372 }
373
374 // enforce all rules.
375 status_t err = enforceMinorVersionUprevs(package);
376 if (err != OK) {
377 return err;
378 }
379
380 // cache it so that it won't need to be enforced again.
381 mPackagesEnforced.insert(package);
382 return OK;
383}
384
385status_t Coordinator::enforceMinorVersionUprevs(const FQName &currentPackage) {
386 if(!currentPackage.hasVersion()) {
387 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
388 << ": missing version.";
389 return UNKNOWN_ERROR;
390 }
391
392 if (currentPackage.getPackageMinorVersion() == 0) {
393 return OK; // ignore for @x.0
394 }
395
396 bool hasPrevPackage = false;
397 FQName prevPacakge = currentPackage;
398 while (prevPacakge.getPackageMinorVersion() > 0) {
399 prevPacakge = prevPacakge.downRev();
400 if (existdir(getPackagePath(prevPacakge).c_str())) {
401 hasPrevPackage = true;
402 break;
403 }
404 }
405 if (!hasPrevPackage) {
406 // no @x.z, where z < y, exist.
407 return OK;
408 }
409
410 if (prevPacakge != currentPackage.downRev()) {
411 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
412 << ": Found package " << prevPacakge.string() << " but missing "
413 << currentPackage.downRev().string() << "; you cannot skip a minor version.";
414 return UNKNOWN_ERROR;
415 }
416
417 status_t err;
418 std::vector<FQName> packageInterfaces;
419 err = appendPackageInterfacesToVector(currentPackage, &packageInterfaces);
420 if (err != OK) {
421 return err;
422 }
423
424 for (const FQName &currentFQName : packageInterfaces) {
425 if (currentFQName.name() == "types") {
426 continue; // ignore types.hal
427 }
428 // Assume that currentFQName == android.hardware.foo@2.2::IFoo.
429 // Then prevFQName == android.hardware.foo@2.1::IFoo.
430 const Interface *iface = nullptr;
431 AST *currentAST = parse(currentFQName);
432 if (currentAST != nullptr) {
433 iface = currentAST->getInterface();
434 }
435 if (iface == nullptr) {
436 LOG(WARNING) << "Warning: Skipping " << currentFQName.string()
437 << " because it could not be found or parsed. "
438 << currentAST;
439 continue;
440 }
441
442 // android.hardware.foo@2.2::IFoo exists. Now make sure
443 // @2.2::IFoo extends @2.1::IFoo. If any interface IFoo in @2.2
444 // ensures this, @2.2 passes the enforcement.
445 FQName prevFQName(prevPacakge.package(), prevPacakge.version(),
446 currentFQName.name());
447 if (iface->superType() == nullptr) {
448 // @2.2::IFoo doesn't extend anything. (This is probably IBase.)
449 continue;
450 }
451 if (iface->superType()->fqName() != prevFQName) {
452 // @2.2::IFoo doesn't extend @2.1::IFoo.
453 if (iface->superType()->fqName().package() == currentPackage.package()) {
454 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
455 << ": " << iface->fqName().string() << " extends "
456 << iface->superType()->fqName().string() << ", which is not allowed.";
457 return UNKNOWN_ERROR;
458 }
459 // @2.2::IFoo extends something from a package with a different package name.
460 // Check the next interface.
461 continue;
462 }
463
464 LOG(VERBOSE) << "enforceMinorVersionUprevs: " << currentFQName.string() << " passes.";
465 return OK;
466 }
467
468 // No interface extends the interface with the same name in @x.(y-1).
469 LOG(ERROR) << currentPackage.string() << " doesn't pass minor version uprev requirement.";
470 return UNKNOWN_ERROR;
471}
472
Andreas Huberd2943e12016-08-05 11:59:31 -0700473// static
474bool Coordinator::MakeParentHierarchy(const std::string &path) {
475 static const mode_t kMode = 0755;
476
477 size_t start = 1; // Ignore leading '/'
478 size_t slashPos;
479 while ((slashPos = path.find("/", start)) != std::string::npos) {
480 std::string partial = path.substr(0, slashPos);
481
482 struct stat st;
483 if (stat(partial.c_str(), &st) < 0) {
484 if (errno != ENOENT) {
485 return false;
486 }
487
488 int res = mkdir(partial.c_str(), kMode);
489 if (res < 0) {
490 return false;
491 }
492 } else if (!S_ISDIR(st.st_mode)) {
493 return false;
494 }
495
496 start = slashPos + 1;
497 }
498
499 return true;
500}
501
Andreas Huber5345ec22016-07-29 13:33:27 -0700502} // namespace android
503