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 &&p : cfg.GetSection("global")) {
160 auto &&key = p.first;
161 bool value_bool = ParseBool(p.second);
162 if (key == "allow_adding_removing_weak_symbols") {
163 allow_adding_removing_weak_symbols = value_bool;
164 } else if (key == "advice_only") {
165 advice_only = value_bool;
166 } else if (key == "elf_unreferenced_symbol_errors") {
167 elf_unreferenced_symbol_errors = value_bool;
168 } else if (key == "check_all_apis") {
169 check_all_apis = value_bool;
170 } else if (key == "allow_extensions") {
171 allow_extensions = value_bool;
172 } else if (key == "allow_unreferenced_elf_symbol_changes") {
173 allow_unreferenced_elf_symbol_changes = value_bool;
174 } else if (key == "allow_unreferenced_changes") {
175 allow_unreferenced_changes = value_bool;
176 } else if (key == "consider_opaque_types_different") {
177 consider_opaque_types_different = value_bool;
178 }
179 }
180 }
181 }
182
183 static const char kWarn[] = "\033[36;1mwarning: \033[0m";
184 static const char kError[] = "\033[31;1merror: \033[0m";
185
ShouldEmitWarningMessage(CompatibilityStatusIR status)186 bool ShouldEmitWarningMessage(CompatibilityStatusIR status) {
187 return ((!allow_extensions &&
188 (status & CompatibilityStatusIR::Extension)) ||
189 (!allow_unreferenced_changes &&
190 (status & CompatibilityStatusIR::UnreferencedChanges)) ||
191 (!allow_unreferenced_elf_symbol_changes &&
192 (status & CompatibilityStatusIR::ElfIncompatible)) ||
193 (status & CompatibilityStatusIR::Incompatible));
194 }
195
main(int argc,const char ** argv)196 int main(int argc, const char **argv) {
197 llvm::cl::ParseCommandLineOptions(argc, argv, "header-checker");
198
199 ReadConfigFile(GetConfigFilePath(old_dump));
200
201 std::set<std::string> ignored_symbols;
202 if (llvm::sys::fs::exists(ignore_symbol_list)) {
203 ignored_symbols = LoadIgnoredSymbols(ignore_symbol_list);
204 }
205
206 DiffPolicyOptions diff_policy_options(consider_opaque_types_different);
207
208 HeaderAbiDiff judge(lib_name, arch, old_dump, new_dump, compatibility_report,
209 ignored_symbols, allow_adding_removing_weak_symbols,
210 diff_policy_options, check_all_apis, text_format_old,
211 text_format_new, text_format_diff);
212
213 CompatibilityStatusIR status = judge.GenerateCompatibilityReport();
214
215 std::string status_str = "";
216 std::string unreferenced_change_str = "";
217 std::string error_or_warning_str = kWarn;
218
219 switch (status) {
220 case CompatibilityStatusIR::Incompatible:
221 error_or_warning_str = kError;
222 status_str = "INCOMPATIBLE CHANGES";
223 break;
224 case CompatibilityStatusIR::ElfIncompatible:
225 if (elf_unreferenced_symbol_errors) {
226 error_or_warning_str = kError;
227 }
228 status_str = "ELF Symbols not referenced by exported headers removed";
229 break;
230 default:
231 break;
232 }
233 if (status & CompatibilityStatusIR::Extension) {
234 if (!allow_extensions) {
235 error_or_warning_str = kError;
236 }
237 status_str = "EXTENDING CHANGES";
238 }
239 if (status & CompatibilityStatusIR::UnreferencedChanges) {
240 unreferenced_change_str = ", changes in exported headers, which are";
241 unreferenced_change_str += " not directly referenced by exported symbols.";
242 unreferenced_change_str += " This MIGHT be an ABI breaking change due to";
243 unreferenced_change_str += " internal typecasts.";
244 }
245
246 bool should_emit_warning_message = ShouldEmitWarningMessage(status);
247
248 if (should_emit_warning_message) {
249 llvm::errs() << "******************************************************\n"
250 << error_or_warning_str
251 << "VNDK library: "
252 << lib_name
253 << "'s ABI has "
254 << status_str
255 << unreferenced_change_str
256 << " Please check compatibility report at: "
257 << compatibility_report << "\n"
258 << "******************************************************\n";
259 }
260
261 if (!advice_only && should_emit_warning_message) {
262 return status;
263 }
264
265 return CompatibilityStatusIR::Compatible;
266 }
267