• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "diff/abi_diff.h"
16 
17 #include "utils/config_file.h"
18 #include "utils/string_utils.h"
19 
20 #include <llvm/ADT/SmallString.h>
21 #include <llvm/Support/CommandLine.h>
22 #include <llvm/Support/FileSystem.h>
23 #include <llvm/Support/Path.h>
24 #include <llvm/Support/raw_ostream.h>
25 
26 #include <fstream>
27 
28 
29 using header_checker::diff::HeaderAbiDiff;
30 using header_checker::repr::CompatibilityStatusIR;
31 using header_checker::repr::DiffPolicyOptions;
32 using header_checker::repr::TextFormatIR;
33 using header_checker::utils::ConfigFile;
34 using header_checker::utils::ConfigParser;
35 using header_checker::utils::ParseBool;
36 
37 
38 static llvm::cl::OptionCategory header_checker_category(
39     "header-abi-diff options");
40 
41 static llvm::cl::opt<std::string> compatibility_report(
42     "o", llvm::cl::desc("<compatibility report>"), llvm::cl::Required,
43     llvm::cl::cat(header_checker_category));
44 
45 static llvm::cl::opt<std::string> lib_name(
46     "lib", llvm::cl::desc("<lib name>"), llvm::cl::Required,
47     llvm::cl::cat(header_checker_category));
48 
49 static llvm::cl::opt<std::string> arch(
50     "arch", llvm::cl::desc("<arch>"), llvm::cl::Required,
51     llvm::cl::cat(header_checker_category));
52 
53 static llvm::cl::opt<std::string> new_dump(
54     "new", llvm::cl::desc("<new dump>"), llvm::cl::Required,
55     llvm::cl::cat(header_checker_category));
56 
57 static llvm::cl::opt<std::string> old_dump(
58     "old", llvm::cl::desc("<old dump>"), llvm::cl::Required,
59     llvm::cl::cat(header_checker_category));
60 
61 static llvm::cl::opt<std::string> ignore_symbol_list(
62     "ignore-symbols", llvm::cl::desc("ignore symbols"), llvm::cl::Optional,
63     llvm::cl::cat(header_checker_category));
64 
65 static llvm::cl::opt<bool> advice_only(
66     "advice-only", llvm::cl::desc("Advisory mode only"), llvm::cl::Optional,
67     llvm::cl::cat(header_checker_category));
68 
69 static llvm::cl::opt<bool> elf_unreferenced_symbol_errors(
70     "elf-unreferenced-symbol-errors",
71     llvm::cl::desc("Display erors on removal of elf symbols, unreferenced by"
72                    "metadata in exported headers."),
73     llvm::cl::Optional, llvm::cl::cat(header_checker_category));
74 
75 static llvm::cl::opt<bool> check_all_apis(
76     "check-all-apis",
77     llvm::cl::desc("All apis, whether referenced or not, by exported symbols in"
78                    " the dynsym table of a shared library are checked"),
79     llvm::cl::Optional, llvm::cl::cat(header_checker_category));
80 
81 static llvm::cl::opt<bool> allow_extensions(
82     "allow-extensions",
83     llvm::cl::desc("Do not return a non zero status on extensions"),
84     llvm::cl::Optional, llvm::cl::cat(header_checker_category));
85 
86 static llvm::cl::opt<bool> allow_unreferenced_elf_symbol_changes(
87     "allow-unreferenced-elf-symbol-changes",
88     llvm::cl::desc("Do not return a non zero status on changes to elf symbols"
89                    "not referenced by metadata in exported headers"),
90     llvm::cl::Optional, llvm::cl::cat(header_checker_category));
91 
92 static llvm::cl::opt<bool> allow_unreferenced_changes(
93     "allow-unreferenced-changes",
94     llvm::cl::desc("Do not return a non zero status on changes to data"
95                    " structures which are not directly referenced by exported"
96                    " APIs."),
97     llvm::cl::Optional, llvm::cl::cat(header_checker_category));
98 
99 static llvm::cl::opt<bool> consider_opaque_types_different(
100     "consider-opaque-types-different",
101     llvm::cl::desc("Consider opaque types with different names as different. "
102                    "This should not be used while comparing C++ library ABIs"),
103     llvm::cl::Optional, llvm::cl::cat(header_checker_category));
104 
105 static llvm::cl::opt<TextFormatIR> text_format_old(
106     "input-format-old", llvm::cl::desc("Specify input format of old abi dump"),
107     llvm::cl::values(clEnumValN(TextFormatIR::ProtobufTextFormat,
108                                 "ProtobufTextFormat", "ProtobufTextFormat"),
109                      clEnumValN(TextFormatIR::Json, "Json", "JSON")),
110     llvm::cl::init(TextFormatIR::Json),
111     llvm::cl::cat(header_checker_category));
112 
113 static llvm::cl::opt<TextFormatIR> text_format_new(
114     "input-format-new", llvm::cl::desc("Specify input format of new abi dump"),
115     llvm::cl::values(clEnumValN(TextFormatIR::ProtobufTextFormat,
116                                 "ProtobufTextFormat", "ProtobufTextFormat"),
117                      clEnumValN(TextFormatIR::Json, "Json", "JSON")),
118     llvm::cl::init(TextFormatIR::Json),
119     llvm::cl::cat(header_checker_category));
120 
121 static llvm::cl::opt<TextFormatIR> text_format_diff(
122     "text-format-diff", llvm::cl::desc("Specify text format of abi-diff"),
123     llvm::cl::values(clEnumValN(TextFormatIR::ProtobufTextFormat,
124                                 "ProtobufTextFormat", "ProtobufTextFormat")),
125     llvm::cl::init(TextFormatIR::ProtobufTextFormat),
126     llvm::cl::cat(header_checker_category));
127 
128 static llvm::cl::opt<bool> allow_adding_removing_weak_symbols(
129     "allow-adding-removing-weak-symbols",
130     llvm::cl::desc("Do not treat addition or removal of weak symbols as "
131                    "incompatible changes."),
132     llvm::cl::init(false), llvm::cl::Optional,
133     llvm::cl::cat(header_checker_category));
134 
LoadIgnoredSymbols(std::string & symbol_list_path)135 static std::set<std::string> LoadIgnoredSymbols(std::string &symbol_list_path) {
136   std::ifstream symbol_ifstream(symbol_list_path);
137   std::set<std::string> ignored_symbols;
138   if (!symbol_ifstream) {
139     llvm::errs() << "Failed to open file containing symbols to ignore\n";
140     ::exit(1);
141   }
142   std::string line = "";
143   while (std::getline(symbol_ifstream, line)) {
144     ignored_symbols.insert(line);
145   }
146   return ignored_symbols;
147 }
148 
GetConfigFilePath(const std::string & dump_file_path)149 static std::string GetConfigFilePath(const std::string &dump_file_path) {
150   llvm::SmallString<128> config_file_path(dump_file_path);
151   llvm::sys::path::remove_filename(config_file_path);
152   llvm::sys::path::append(config_file_path, "config.ini");
153   return config_file_path.str();
154 }
155 
ReadConfigFile(const std::string & config_file_path)156 static void ReadConfigFile(const std::string &config_file_path) {
157   ConfigFile cfg = ConfigParser::ParseFile(config_file_path);
158   if (cfg.HasSection("global")) {
159     for (auto &&[key, value] : cfg.GetSection("global")) {
160       bool value_bool = ParseBool(value);
161       if (key == "allow_adding_removing_weak_symbols") {
162         allow_adding_removing_weak_symbols = value_bool;
163       } else if (key == "advice_only") {
164         advice_only = value_bool;
165       } else if (key == "elf_unreferenced_symbol_errors") {
166         elf_unreferenced_symbol_errors = value_bool;
167       } else if (key == "check_all_apis") {
168         check_all_apis = value_bool;
169       } else if (key == "allow_extensions") {
170         allow_extensions = value_bool;
171       } else if (key == "allow_unreferenced_elf_symbol_changes") {
172         allow_unreferenced_elf_symbol_changes = value_bool;
173       } else if (key == "allow_unreferenced_changes") {
174         allow_unreferenced_changes = value_bool;
175       } else if (key == "consider_opaque_types_different") {
176         consider_opaque_types_different = value_bool;
177       }
178     }
179   }
180 }
181 
182 static const char kWarn[] = "\033[36;1mwarning: \033[0m";
183 static const char kError[] = "\033[31;1merror: \033[0m";
184 
ShouldEmitWarningMessage(CompatibilityStatusIR status)185 bool ShouldEmitWarningMessage(CompatibilityStatusIR status) {
186   return ((!allow_extensions &&
187            (status & CompatibilityStatusIR::Extension)) ||
188           (!allow_unreferenced_changes &&
189            (status & CompatibilityStatusIR::UnreferencedChanges)) ||
190           (!allow_unreferenced_elf_symbol_changes &&
191            (status & CompatibilityStatusIR::ElfIncompatible)) ||
192           (status & CompatibilityStatusIR::Incompatible));
193 }
194 
main(int argc,const char ** argv)195 int main(int argc, const char **argv) {
196   llvm::cl::ParseCommandLineOptions(argc, argv, "header-checker");
197 
198   ReadConfigFile(GetConfigFilePath(old_dump));
199 
200   std::set<std::string> ignored_symbols;
201   if (llvm::sys::fs::exists(ignore_symbol_list)) {
202     ignored_symbols = LoadIgnoredSymbols(ignore_symbol_list);
203   }
204 
205   DiffPolicyOptions diff_policy_options(consider_opaque_types_different);
206 
207   HeaderAbiDiff judge(lib_name, arch, old_dump, new_dump, compatibility_report,
208                       ignored_symbols, allow_adding_removing_weak_symbols,
209                       diff_policy_options, check_all_apis, text_format_old,
210                       text_format_new, text_format_diff);
211 
212   CompatibilityStatusIR status = judge.GenerateCompatibilityReport();
213 
214   std::string status_str = "";
215   std::string unreferenced_change_str = "";
216   std::string error_or_warning_str = kWarn;
217 
218   switch (status) {
219     case CompatibilityStatusIR::Incompatible:
220       error_or_warning_str = kError;
221       status_str = "INCOMPATIBLE CHANGES";
222       break;
223     case CompatibilityStatusIR::ElfIncompatible:
224       if (elf_unreferenced_symbol_errors) {
225         error_or_warning_str = kError;
226       }
227       status_str = "ELF Symbols not referenced by exported headers removed";
228       break;
229     default:
230       break;
231   }
232   if (status & CompatibilityStatusIR::Extension) {
233     if (!allow_extensions) {
234       error_or_warning_str = kError;
235     }
236     status_str = "EXTENDING CHANGES";
237   }
238   if (status & CompatibilityStatusIR::UnreferencedChanges) {
239     unreferenced_change_str = ", changes in exported headers, which are";
240     unreferenced_change_str += " not directly referenced by exported symbols.";
241     unreferenced_change_str += " This MIGHT be an ABI breaking change due to";
242     unreferenced_change_str += " internal typecasts.";
243   }
244 
245   bool should_emit_warning_message = ShouldEmitWarningMessage(status);
246 
247   if (should_emit_warning_message) {
248     llvm::errs() << "******************************************************\n"
249                  << error_or_warning_str
250                  << "VNDK library: "
251                  << lib_name
252                  << "'s ABI has "
253                  << status_str
254                  << unreferenced_change_str
255                  << " Please check compatibility report at: "
256                  << compatibility_report << "\n"
257                  << "******************************************************\n";
258   }
259 
260   if (!advice_only && should_emit_warning_message) {
261     return status;
262   }
263 
264   return CompatibilityStatusIR::Compatible;
265 }
266