1/*
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
17#include <dirent.h>
18#include <err.h>
19#include <limits.h>
20#include <stdio.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <unistd.h>
24
25#if defined(__linux__)
26#include <sched.h>
27#endif
28
29#include <atomic>
30#include <chrono>
31#include <functional>
32#include <iostream>
33#include <map>
34#include <memory>
35#include <set>
36#include <sstream>
37#include <string>
38#include <thread>
39#include <unordered_map>
40#include <vector>
41
42#include <llvm/ADT/StringRef.h>
43
44#include <android-base/macros.h>
45#include <android-base/parseint.h>
46
47#include "Arch.h"
48#include "DeclarationDatabase.h"
49#include "Driver.h"
50#include "Preprocessor.h"
51#include "SymbolDatabase.h"
52#include "Utils.h"
53#include "VFS.h"
54
55#include "versioner.h"
56
57using namespace std::chrono_literals;
58using namespace std::string_literals;
59
60bool strict;
61bool verbose;
62bool add_include;
63
64static int getCpuCount();
65static int max_thread_count = getCpuCount();
66
67static int getCpuCount() {
68#if defined(__linux__)
69  cpu_set_t cpu_set;
70  int rc = sched_getaffinity(getpid(), sizeof(cpu_set), &cpu_set);
71  if (rc != 0) {
72    err(1, "sched_getaffinity failed");
73  }
74  return CPU_COUNT(&cpu_set);
75#else
76  return 1;
77#endif
78}
79
80static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
81                                                   const std::string& dependency_dir) {
82  std::vector<std::string> headers = collectHeaders(header_dir);
83  std::vector<std::string> dependencies = { header_dir };
84  if (!dependency_dir.empty()) {
85    auto collect_children = [&dependencies, &dependency_dir](const std::string& dir_path) {
86      DIR* dir = opendir(dir_path.c_str());
87      if (!dir) {
88        err(1, "failed to open dependency directory '%s'", dir_path.c_str());
89      }
90
91      struct dirent* dent;
92      while ((dent = readdir(dir))) {
93        if (dent->d_name[0] == '.') {
94          continue;
95        }
96
97        // TODO: Resolve symlinks.
98        std::string dependency = dir_path + "/" + dent->d_name;
99
100        struct stat st;
101        if (stat(dependency.c_str(), &st) != 0) {
102          err(1, "failed to stat dependency '%s'", dependency.c_str());
103        }
104
105        if (!S_ISDIR(st.st_mode)) {
106          errx(1, "'%s' is not a directory", dependency.c_str());
107        }
108
109        dependencies.push_back(dependency);
110      }
111
112      closedir(dir);
113    };
114
115    collect_children(dependency_dir + "/common");
116    collect_children(dependency_dir + "/" + to_string(arch));
117  }
118
119  auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
120    for (const auto& it : header_blacklist) {
121      if (it.second.find(arch) == it.second.end()) {
122        continue;
123      }
124
125      if (header.endswith("/" + it.first)) {
126        return true;
127      }
128    }
129    return false;
130  });
131
132  headers.erase(new_end, headers.end());
133
134  CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
135  return result;
136}
137
138static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
139                                                          const std::set<int>& selected_levels) {
140  std::set<CompilationType> result;
141  for (const auto& arch : selected_architectures) {
142    int min_api = arch_min_api[arch];
143    for (int api_level : selected_levels) {
144      if (api_level < min_api) {
145        continue;
146      }
147
148      for (int file_offset_bits : { 32, 64 }) {
149        CompilationType type = {
150          .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
151        };
152        result.insert(type);
153      }
154    }
155  }
156  return result;
157}
158
159static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
160                                                      const std::string& header_dir,
161                                                      const std::string& dependency_dir) {
162  if (types.empty()) {
163    errx(1, "compileHeaders received no CompilationTypes");
164  }
165
166  auto vfs = createCommonVFS(header_dir, dependency_dir, add_include);
167
168  size_t thread_count = max_thread_count;
169  std::vector<std::thread> threads;
170
171  std::map<CompilationType, HeaderDatabase> header_databases;
172  std::unordered_map<Arch, CompilationRequirements> requirements;
173
174  auto result = std::make_unique<HeaderDatabase>();
175  for (const auto& type : types) {
176    if (requirements.count(type.arch) == 0) {
177      requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
178    }
179  }
180
181  initializeTargetCC1FlagCache(vfs, types, requirements);
182
183  std::vector<std::pair<CompilationType, const std::string&>> jobs;
184  std::atomic<size_t> job_index(0);
185  for (CompilationType type : types) {
186    CompilationRequirements& req = requirements[type.arch];
187    for (const std::string& header : req.headers) {
188      jobs.emplace_back(type, header);
189    }
190  }
191
192  thread_count = std::min(thread_count, jobs.size());
193
194  if (thread_count == 1) {
195    for (const auto& job : jobs) {
196      compileHeader(vfs, result.get(), job.first, job.second);
197    }
198  } else {
199    // Spawn threads.
200    size_t cpu_count = getCpuCount();
201    for (size_t i = 0; i < thread_count; ++i) {
202      threads.emplace_back([&jobs, &job_index, &result, &header_dir, vfs, cpu_count, i]() {
203        while (true) {
204          size_t idx = job_index++;
205          if (idx >= jobs.size()) {
206            return;
207          }
208
209          const auto& job = jobs[idx];
210          compileHeader(vfs, result.get(), job.first, job.second);
211        }
212      });
213    }
214
215    // Reap them.
216    for (auto& thread : threads) {
217      thread.join();
218    }
219    threads.clear();
220  }
221
222  return result;
223}
224
225static std::set<CompilationType> getCompilationTypes(const Declaration* decl) {
226  std::set<CompilationType> result;
227  for (const auto& it : decl->availability) {
228    result.insert(it.first);
229  }
230  return result;
231}
232
233template<typename T>
234static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) {
235  std::vector<T> intersection;
236  std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersection));
237  return intersection;
238}
239
240// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
241//   1. At most one inline definition of the function exists.
242//   2. All of the availability declarations for a symbol are compatible.
243//      If a function is declared as an inline before a certain version, the inline definition
244//      should have no version tag.
245//   3. Each availability type must only be present globally or on a per-arch basis.
246//      (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
247//      but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
248static bool checkSymbol(const Symbol& symbol) {
249  std::string cwd = getWorkingDir() + "/";
250
251  std::unordered_map<const Declaration*, std::set<CompilationType>> inline_definitions;
252  for (const auto& decl_it : symbol.declarations) {
253    const Declaration* decl = &decl_it.second;
254    if (decl->is_definition) {
255      std::set<CompilationType> compilation_types = getCompilationTypes(decl);
256      for (const auto& inline_def_it : inline_definitions) {
257        auto intersection = Intersection(compilation_types, inline_def_it.second);
258        if (!intersection.empty()) {
259          fprintf(stderr, "versioner: conflicting inline definitions:\n");
260          fprintf(stderr, "  declarations visible in: %s\n", Join(intersection, ", ").c_str());
261          decl->dump(cwd, stderr, 4);
262          inline_def_it.first->dump(cwd, stderr, 4);
263          return false;
264        }
265      }
266
267      inline_definitions[decl] = std::move(compilation_types);
268    }
269
270    DeclarationAvailability availability;
271    if (!decl->calculateAvailability(&availability)) {
272      fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
273      decl->dump(cwd, stderr, 2);
274      return false;
275    }
276
277    if (decl->is_definition && !availability.empty()) {
278      fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
279      decl->dump(cwd, stderr, 2);
280      return false;
281    }
282  }
283
284  DeclarationAvailability availability;
285  if (!symbol.calculateAvailability(&availability)) {
286    fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
287    symbol.dump(cwd);
288    return false;
289  }
290
291  // TODO: Check invariant #3.
292  return true;
293}
294
295static bool sanityCheck(const HeaderDatabase* database) {
296  bool error = false;
297  std::string cwd = getWorkingDir() + "/";
298
299  for (const auto& symbol_it : database->symbols) {
300    if (!checkSymbol(symbol_it.second)) {
301      error = true;
302    }
303  }
304  return !error;
305}
306
307// Check that our symbol availability declarations match the actual NDK
308// platform symbol availability.
309static bool checkVersions(const std::set<CompilationType>& types,
310                          const HeaderDatabase* header_database,
311                          const NdkSymbolDatabase& symbol_database) {
312  std::string cwd = getWorkingDir() + "/";
313  bool failed = false;
314
315  std::map<Arch, std::set<CompilationType>> arch_types;
316  for (const CompilationType& type : types) {
317    arch_types[type.arch].insert(type);
318  }
319
320  std::set<std::string> completely_unavailable;
321  std::map<std::string, std::set<CompilationType>> missing_availability;
322  std::map<std::string, std::set<CompilationType>> extra_availability;
323
324  for (const auto& symbol_it : header_database->symbols) {
325    const auto& symbol_name = symbol_it.first;
326    DeclarationAvailability symbol_availability;
327
328    if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
329      errx(1, "failed to calculate symbol availability");
330    }
331
332    const auto platform_availability_it = symbol_database.find(symbol_name);
333    if (platform_availability_it == symbol_database.end()) {
334      completely_unavailable.insert(symbol_name);
335      continue;
336    }
337
338    const auto& platform_availability = platform_availability_it->second;
339
340    for (const CompilationType& type : types) {
341      bool should_be_available = true;
342      const auto& global_availability = symbol_availability.global_availability;
343      const auto& arch_availability = symbol_availability.arch_availability[type.arch];
344      if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
345        should_be_available = false;
346      }
347
348      if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
349        should_be_available = false;
350      }
351
352      if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
353        should_be_available = false;
354      }
355
356      if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
357        should_be_available = false;
358      }
359
360      if (arch_availability.future) {
361        continue;
362      }
363
364      // The function declaration might be (validly) missing for the given CompilationType.
365      if (!symbol_it.second.hasDeclaration(type)) {
366        should_be_available = false;
367      }
368
369      bool is_available = platform_availability.count(type);
370
371      if (should_be_available != is_available) {
372        if (is_available) {
373          extra_availability[symbol_name].insert(type);
374        } else {
375          missing_availability[symbol_name].insert(type);
376        }
377      }
378    }
379  }
380
381  for (const auto& it : symbol_database) {
382    const std::string& symbol_name = it.first;
383
384    bool symbol_error = false;
385    if (auto missing_it = missing_availability.find(symbol_name);
386        missing_it != missing_availability.end()) {
387      printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
388             Join(missing_it->second, ", ").c_str());
389      symbol_error = true;
390      failed = true;
391    }
392
393    if (strict) {
394      if (auto extra_it = extra_availability.find(symbol_name);
395          extra_it != extra_availability.end()) {
396        printf("%s: declaration marked unavailable but symbol available in [%s]\n",
397               symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
398        symbol_error = true;
399        failed = true;
400      }
401    }
402
403    if (symbol_error) {
404      if (auto symbol_it = header_database->symbols.find(symbol_name);
405          symbol_it != header_database->symbols.end()) {
406        symbol_it->second.dump(cwd);
407      } else {
408        errx(1, "failed to find symbol in header database");
409      }
410    }
411  }
412
413  // TODO: Verify that function/variable declarations are actually function/variable symbols.
414  return !failed;
415}
416
417static void usage(bool help = false) {
418  fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
419  if (!help) {
420    printf("Try 'versioner -h' for more information.\n");
421    exit(1);
422  } else {
423    fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
424    fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
425    fprintf(stderr, "\n");
426    fprintf(stderr, "Target specification (defaults to all):\n");
427    fprintf(stderr, "  -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
428    fprintf(stderr, "    \t\tvalid levels are %s\n", Join(supported_levels).c_str());
429    fprintf(stderr, "  -r ARCH\tbuild with specified architecture (can be repeated)\n");
430    fprintf(stderr, "    \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
431    fprintf(stderr, "\n");
432    fprintf(stderr, "Validation:\n");
433    fprintf(stderr, "  -p PATH\tcompare against NDK platform at PATH\n");
434    fprintf(stderr, "  -s\t\tenable strict warnings\n");
435    fprintf(stderr, "\n");
436    fprintf(stderr, "Preprocessing:\n");
437    fprintf(stderr, "  -o PATH\tpreprocess header files and emit them at PATH\n");
438    fprintf(stderr, "  -f\tpreprocess header files even if validation fails\n");
439    fprintf(stderr, "\n");
440    fprintf(stderr, "Miscellaneous:\n");
441    fprintf(stderr, "  -d\t\tdump function availability\n");
442    fprintf(stderr, "  -j THREADS\tmaximum number of threads to use\n");
443    fprintf(stderr, "  -v\t\tenable verbose logging\n");
444    fprintf(stderr, "  -h\t\tdisplay this message\n");
445    exit(0);
446  }
447}
448
449int main(int argc, char** argv) {
450  std::string cwd = getWorkingDir() + "/";
451  bool default_args = true;
452  std::string platform_dir;
453  std::set<Arch> selected_architectures;
454  std::set<int> selected_levels;
455  std::string preprocessor_output_path;
456  bool force = false;
457  bool dump = false;
458
459  int c;
460  while ((c = getopt(argc, argv, "a:r:p:so:fdj:vhi")) != -1) {
461    default_args = false;
462    switch (c) {
463      case 'a': {
464        char* end;
465        int api_level = strtol(optarg, &end, 10);
466        if (end == optarg || strlen(end) > 0) {
467          usage();
468        }
469
470        if (supported_levels.count(api_level) == 0) {
471          errx(1, "unsupported API level %d", api_level);
472        }
473
474        selected_levels.insert(api_level);
475        break;
476      }
477
478      case 'r': {
479        Arch arch = arch_from_string(optarg);
480        selected_architectures.insert(arch);
481        break;
482      }
483
484      case 'p': {
485        if (!platform_dir.empty()) {
486          usage();
487        }
488
489        platform_dir = optarg;
490
491        if (platform_dir.empty()) {
492          usage();
493        }
494
495        struct stat st;
496        if (stat(platform_dir.c_str(), &st) != 0) {
497          err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
498        }
499        if (!S_ISDIR(st.st_mode)) {
500          errx(1, "'%s' is not a directory", optarg);
501        }
502        break;
503      }
504
505      case 's':
506        strict = true;
507        break;
508
509      case 'o':
510        if (!preprocessor_output_path.empty()) {
511          usage();
512        }
513        preprocessor_output_path = optarg;
514        if (preprocessor_output_path.empty()) {
515          usage();
516        }
517        break;
518
519      case 'f':
520        force = true;
521        break;
522
523      case 'd':
524        dump = true;
525        break;
526
527      case 'j':
528        if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) {
529          usage();
530        }
531        break;
532
533      case 'v':
534        verbose = true;
535        break;
536
537      case 'h':
538        usage(true);
539        break;
540
541      case 'i':
542        // Secret option for tests to -include <android/versioning.h>.
543        add_include = true;
544        break;
545
546      default:
547        usage();
548        break;
549    }
550  }
551
552  if (argc - optind > 2 || optind > argc) {
553    usage();
554  }
555
556  std::string header_dir;
557  std::string dependency_dir;
558
559  const char* top = getenv("ANDROID_BUILD_TOP");
560  if (!top && (optind == argc || add_include)) {
561    fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
562    usage();
563  }
564
565  if (optind == argc) {
566    // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
567    std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
568    header_dir = versioner_dir + "/current";
569    dependency_dir = versioner_dir + "/dependencies";
570    if (platform_dir.empty()) {
571      platform_dir = versioner_dir + "/platforms";
572    }
573  } else {
574    // Intentional leak.
575    header_dir = realpath(argv[optind], nullptr);
576
577    if (argc - optind == 2) {
578      dependency_dir = argv[optind + 1];
579    }
580  }
581
582  if (selected_levels.empty()) {
583    selected_levels = supported_levels;
584  }
585
586  if (selected_architectures.empty()) {
587    selected_architectures = supported_archs;
588  }
589
590
591  struct stat st;
592  if (stat(header_dir.c_str(), &st) != 0) {
593    err(1, "failed to stat '%s'", header_dir.c_str());
594  } else if (!S_ISDIR(st.st_mode)) {
595    errx(1, "'%s' is not a directory", header_dir.c_str());
596  }
597
598  std::set<CompilationType> compilation_types;
599  NdkSymbolDatabase symbol_database;
600
601  compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
602
603  // Do this before compiling so that we can early exit if the platforms don't match what we
604  // expect.
605  if (!platform_dir.empty()) {
606    symbol_database = parsePlatforms(compilation_types, platform_dir);
607  }
608
609  auto start = std::chrono::high_resolution_clock::now();
610  std::unique_ptr<HeaderDatabase> declaration_database =
611      compileHeaders(compilation_types, header_dir, dependency_dir);
612  auto end = std::chrono::high_resolution_clock::now();
613
614  if (verbose) {
615    auto diff = (end - start) / 1.0ms;
616    printf("Compiled headers for %zu targets in %0.2LFms\n", compilation_types.size(), diff);
617  }
618
619  bool failed = false;
620  if (dump) {
621    declaration_database->dump(header_dir + "/");
622  } else {
623    if (!sanityCheck(declaration_database.get())) {
624      printf("versioner: sanity check failed\n");
625      failed = true;
626    }
627
628    if (!platform_dir.empty()) {
629      if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
630        printf("versioner: version check failed\n");
631        failed = true;
632      }
633    }
634  }
635
636  if (!preprocessor_output_path.empty() && (force || !failed)) {
637    failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
638  }
639  return failed;
640}
641