| 1 |
//===- InlineAnalysis.cpp - Find out which functions got inlined -*- C++ -*-===// |
| 2 |
// |
| 3 |
// This project is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
/// \file |
| 10 |
/// Use the information from debuginfo, ipa-clones and symvers to decide if |
| 11 |
/// symbol is inlined, private or public in the target livepatch binary. |
| 12 |
// |
| 13 |
//===----------------------------------------------------------------------===// |
| 14 |
|
| 15 |
/* Author: Giuliano Belinassi */ |
| 16 |
|
| 17 |
#include "InlineAnalysis.hh" |
| 18 |
#include "NonLLVMMisc.hh" |
| 19 |
#include <iostream> |
| 20 |
#include <cxxabi.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <cxxabi.h> |
| 23 |
#include <stdexcept> |
| 24 |
|
| 25 |
InlineAnalysis::InlineAnalysis(const std::vector<std::string> &elfs_path, |
| 26 |
const char *ipaclones_path, |
| 27 |
const char *symvers_path, bool is_kernel) |
| 28 |
: ElfCache(nullptr), |
| 29 |
Ipa(nullptr), |
| 30 |
Symv(nullptr), |
| 31 |
Kernel(is_kernel) |
| 32 |
{ |
| 33 |
try { |
| 34 |
/* Debuginfo information is not needed for inline analysis. But is desired |
| 35 |
for better precision. That is why whe declare those objects dynamically. */ |
| 36 |
if (elfs_path.size() > 0) { |
| 37 |
/* Only create an ElfSymbolCache if we received elfs from command line. */ |
| 38 |
ElfCache = new ElfSymbolCache(); |
| 39 |
} |
| 40 |
|
| 41 |
/* Initialize the SymbolCache. */ |
| 42 |
for (auto it = elfs_path.begin(); it != elfs_path.end(); ++it) { |
| 43 |
const std::string &path = *it; |
| 44 |
ElfObject elf(path); |
| 45 |
ElfCache->Analyze_ELF(elf); |
| 46 |
} |
| 47 |
|
| 48 |
if (ipaclones_path) { |
| 49 |
Ipa = new IpaClones(ipaclones_path); |
| 50 |
} |
| 51 |
|
| 52 |
if (symvers_path) { |
| 53 |
Symv = new Symvers(symvers_path); |
| 54 |
} |
| 55 |
} catch (std::runtime_error &e) { |
| 56 |
/* So what happens if this constructor throws an exception (like file not |
| 57 |
found) is that the destructor is never called and therefore it generates |
| 58 |
memory leaks. But we *can* call the destructor here because it was |
| 59 |
designed to be safely callable if the object was not properly constructed |
| 60 |
and hence we call it here. */ |
| 61 |
this->~InlineAnalysis(); |
| 62 |
|
| 63 |
/* Throw the error up for someone else to catch. */ |
| 64 |
throw e; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
InlineAnalysis::~InlineAnalysis(void) |
| 69 |
{ |
| 70 |
if (ElfCache) |
| 71 |
delete ElfCache; |
| 72 |
if (Ipa) |
| 73 |
delete Ipa; |
| 74 |
if (Symv) |
| 75 |
delete Symv; |
| 76 |
} |
| 77 |
|
| 78 |
static int Action_Add_Node2(void *s, IpaCloneNode *n1, IpaCloneNode *n2) |
| 79 |
{ |
| 80 |
(void) n1; |
| 81 |
|
| 82 |
std::set<std::string> *set = static_cast<std::set<std::string> *>(s); |
| 83 |
set->insert(n2->Name); |
| 84 |
|
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
std::set<std::string> InlineAnalysis::Get_Inline_Closure_Of_Symbol(const std::string &asm_name) |
| 89 |
{ |
| 90 |
std::set<std::string> set; |
| 91 |
|
| 92 |
/* If we don't have the IPA information there is nothing we can do. */ |
| 93 |
if (Ipa == nullptr) { |
| 94 |
return set; |
| 95 |
} |
| 96 |
|
| 97 |
IpaClosure(*Ipa, (void*) &set, Action_Add_Node2).Find_Inlined_Symbols(asm_name); |
| 98 |
return set; |
| 99 |
} |
| 100 |
|
| 101 |
std::set<std::string> InlineAnalysis::Get_Inline_Closure_Of_Symbols(const std::vector<std::string> &symbols) |
| 102 |
{ |
| 103 |
std::set<std::string> set; |
| 104 |
/* If we don't have the IPA information there is nothing we can do. */ |
| 105 |
if (Ipa == nullptr) { |
| 106 |
return set; |
| 107 |
} |
| 108 |
|
| 109 |
IpaClosure closure(*Ipa, (void*) &set, Action_Add_Node2); |
| 110 |
for (const std::string &sym : symbols) { |
| 111 |
closure.Find_Inlined_Symbols(sym); |
| 112 |
} |
| 113 |
|
| 114 |
return set; |
| 115 |
} |
| 116 |
|
| 117 |
std::set<std::string> InlineAnalysis::Get_Where_Symbol_Is_Inlined(const std::string &asm_name) |
| 118 |
{ |
| 119 |
std::set<std::string> set; |
| 120 |
/* If we don't have the IPA information there is nothing we can do. */ |
| 121 |
if (Ipa == nullptr) { |
| 122 |
return set; |
| 123 |
} |
| 124 |
|
| 125 |
IpaClosure(*Ipa, (void *) &set, Action_Add_Node2) |
| 126 |
.Find_Where_Symbol_Is_Inlined(asm_name); |
| 127 |
|
| 128 |
return set; |
| 129 |
} |
| 130 |
|
| 131 |
std::set<std::string> InlineAnalysis::Get_Where_Symbols_Is_Inlined(const std::vector<std::string> &symbols) |
| 132 |
{ |
| 133 |
std::set<std::string> set; |
| 134 |
|
| 135 |
/* If we don't have the IPA information there is nothing we can do. */ |
| 136 |
if (Ipa == nullptr) { |
| 137 |
return set; |
| 138 |
} |
| 139 |
|
| 140 |
IpaClosure closure(*Ipa, (void *) &set, Action_Add_Node2); |
| 141 |
for (const std::string &sym : symbols) { |
| 142 |
closure.Find_Where_Symbol_Is_Inlined(sym); |
| 143 |
} |
| 144 |
|
| 145 |
return set; |
| 146 |
} |
| 147 |
|
| 148 |
static int Action_Graphviz(void *s, IpaCloneNode *n1, IpaCloneNode *n2) |
| 149 |
{ |
| 150 |
FILE *file = static_cast<FILE*>(s); |
| 151 |
|
| 152 |
const char *name1 = InlineAnalysis::Demangle_Symbol(n1->Name.c_str()); |
| 153 |
const char *name2 = InlineAnalysis::Demangle_Symbol(n2->Name.c_str()); |
| 154 |
fprintf(file, "\n\"%s\" -> \"%s\"", name1, name2); |
| 155 |
|
| 156 |
free(name1); |
| 157 |
free(name2); |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
void InlineAnalysis::Print_Node_Colors(const std::set<IpaCloneNode *> &set, FILE *fp) |
| 163 |
{ |
| 164 |
if (!Have_Debuginfo()) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
for (IpaCloneNode *node : set) { |
| 169 |
const char *demangled = InlineAnalysis::Demangle_Symbol(node->Name.c_str()); |
| 170 |
std::pair<unsigned char, ElfSymtabType> infos = Get_Symbol_Info(node->Name); |
| 171 |
unsigned char syminfo = infos.first; |
| 172 |
ElfSymtabType symtab = infos.second; |
| 173 |
if (syminfo == 0) { |
| 174 |
fprintf(fp, "\n\"%s\" [style=dotted]", demangled); |
| 175 |
} else { |
| 176 |
//unsigned char type = ElfSymbol::Type_Of(syminfo); |
| 177 |
unsigned char bind = ElfSymbol::Bind_Of(syminfo); |
| 178 |
if (bind == STB_LOCAL) { |
| 179 |
fprintf(fp, "\n\"%s\" [color=red]", demangled); |
| 180 |
} else if (bind == STB_GLOBAL && symtab == SHT_DYNSYM) { |
| 181 |
fprintf(fp, "\n\"%s\" [color=black]", demangled); |
| 182 |
} else if (bind == STB_GLOBAL && symtab == SHT_SYMTAB) { |
| 183 |
fprintf(fp, "\n\"%s\" [color=red]", demangled); |
| 184 |
} else if (bind == STB_WEAK) { |
| 185 |
fprintf(fp, "\n\"%s\" [color=green]", demangled); |
| 186 |
} else { |
| 187 |
fprintf(fp, "\n\"%s\" [color=yellow]", demangled); |
| 188 |
} |
| 189 |
} |
| 190 |
free(demangled); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
void InlineAnalysis::Get_Graphviz_Of_Inlines(const std::vector<std::string> &symbols, const char *output_path) |
| 195 |
{ |
| 196 |
FILE *file = fopen(output_path, "w"); |
| 197 |
if (file == nullptr) { |
| 198 |
throw std::runtime_error("ERROR: Unable to open file " + |
| 199 |
std::string(output_path) + |
| 200 |
" to write"); |
| 201 |
} |
| 202 |
|
| 203 |
fprintf(file, "strict digraph {"); |
| 204 |
IpaClosure closure(*Ipa, (void *) file, Action_Graphviz); |
| 205 |
for (const std::string &sym : symbols) { |
| 206 |
closure.Find_Where_Symbol_Is_Inlined(sym); |
| 207 |
} |
| 208 |
|
| 209 |
Print_Node_Colors(closure.Set, file); |
| 210 |
|
| 211 |
fprintf(file, "\n}"); |
| 212 |
fclose(file); |
| 213 |
} |
| 214 |
|
| 215 |
static int Action_Graphviz_Reverse(void *s, IpaCloneNode *n1, IpaCloneNode *n2) |
| 216 |
{ |
| 217 |
FILE *file = static_cast<FILE*>(s); |
| 218 |
const char *name1 = InlineAnalysis::Demangle_Symbol(n2->Name.c_str()); |
| 219 |
const char *name2 = InlineAnalysis::Demangle_Symbol(n1->Name.c_str()); |
| 220 |
|
| 221 |
fprintf(file, "\n\"%s\" -> \"%s\"", name1, name2); |
| 222 |
|
| 223 |
free(name1); |
| 224 |
free(name2); |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
void InlineAnalysis::Get_Graphviz_Of_Inline_Closure(const std::vector<std::string> &symbols, const char *output_path) |
| 229 |
{ |
| 230 |
FILE *file = fopen(output_path, "w"); |
| 231 |
if (file == nullptr) { |
| 232 |
throw std::runtime_error("Unable to open file " + |
| 233 |
std::string(output_path) + |
| 234 |
" to write"); |
| 235 |
} |
| 236 |
|
| 237 |
fprintf(file, "strict digraph {"); |
| 238 |
IpaClosure closure(*Ipa, (void *) file, Action_Graphviz_Reverse); |
| 239 |
for (const std::string &sym : symbols) { |
| 240 |
closure.Find_Inlined_Symbols(sym); |
| 241 |
} |
| 242 |
|
| 243 |
Print_Node_Colors(closure.Set, file); |
| 244 |
|
| 245 |
fprintf(file, "\n}"); |
| 246 |
fclose(file); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
void InlineAnalysis::Dump(void) |
| 251 |
{ |
| 252 |
if (Ipa) { |
| 253 |
Ipa->Dump(); |
| 254 |
} else { |
| 255 |
std::cout << "IPA is NULL\n"; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
std::pair<unsigned char, ElfSymtabType> |
| 260 |
InlineAnalysis::Get_Symbol_Info(const std::string &sym) |
| 261 |
{ |
| 262 |
/* Try the dynsym first, which means this symbol is most likely publically |
| 263 |
visible. */ |
| 264 |
if (ElfCache == nullptr) { |
| 265 |
/* If we don't have the ElfCache there is nothing we can do. */ |
| 266 |
return std::make_pair(0, ElfSymtabType::TAB_NONE); |
| 267 |
} |
| 268 |
|
| 269 |
/* Forward this to ElfCXX. */ |
| 270 |
return ElfCache->Get_Symbol_Info(sym); |
| 271 |
} |
| 272 |
|
| 273 |
static const char *Bind(unsigned link, ElfSymtabType symtab) |
| 274 |
{ |
| 275 |
switch (link) { |
| 276 |
case STB_LOCAL: |
| 277 |
return "Private symbol"; |
| 278 |
break; |
| 279 |
|
| 280 |
case STB_GLOBAL: |
| 281 |
if (symtab == SHT_SYMTAB) { |
| 282 |
return "Private symbol"; |
| 283 |
} else if (symtab == SHT_DYNSYM) { |
| 284 |
return "Public symbol"; |
| 285 |
} else { |
| 286 |
assert(0 && "Unreachable"); |
| 287 |
} |
| 288 |
break; |
| 289 |
|
| 290 |
case STB_WEAK: |
| 291 |
return "Extern symbol"; |
| 292 |
break; |
| 293 |
|
| 294 |
default: |
| 295 |
/* If the symbol is not available on the debuginfo then we consider it as |
| 296 |
inlined. */ |
| 297 |
return "Inlined"; |
| 298 |
break; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
ExternalizationType InlineAnalysis::Needs_Externalization(const std::string &sym) |
| 303 |
{ |
| 304 |
if (Symv) { |
| 305 |
const std::string &sym_mod = Symv->Get_Symbol_Module(sym); |
| 306 |
/* If the symbol comes from vmlinux, then we should use Weak |
| 307 |
* externalization, since the symbol is always present when loading the |
| 308 |
* livepatch. */ |
| 309 |
if (sym_mod == "vmlinux") |
| 310 |
return ExternalizationType::WEAK; |
| 311 |
|
| 312 |
/* |
| 313 |
* If the symbol exists on Symvers we can decide whether the symbol must be |
| 314 |
* externalized or not, and not rely on ELF. |
| 315 |
*/ |
| 316 |
if (!sym_mod.empty()) |
| 317 |
return (Symv->Needs_Externalization(sym_mod)) ? ExternalizationType::STRONG |
| 318 |
: ExternalizationType::NONE; |
| 319 |
} |
| 320 |
|
| 321 |
auto infos = Get_Symbol_Info(sym); |
| 322 |
unsigned char info = infos.first; |
| 323 |
ElfSymtabType symtab = infos.second; |
| 324 |
if (info > 0) { |
| 325 |
unsigned bind = ElfSymbol::Bind_Of(info); |
| 326 |
switch (bind) { |
| 327 |
case STB_GLOBAL: |
| 328 |
/* |
| 329 |
* When creating a livepatch for Linux kernel, we need to externalize |
| 330 |
* all existing symbols that don't reside on vmlinux. In this case, the |
| 331 |
* specific symbol was found in the ELF object, so we can safely externalize |
| 332 |
* it. |
| 333 |
*/ |
| 334 |
if (Kernel) { |
| 335 |
return ExternalizationType::STRONG; |
| 336 |
} |
| 337 |
|
| 338 |
/* We need to be careful with the symbol table this came from. If this |
| 339 |
is from SYMTAB we can't weakly externalize it. */ |
| 340 |
if (symtab == SHT_SYMTAB) { |
| 341 |
return ExternalizationType::STRONG; |
| 342 |
} else if (symtab == SHT_DYNSYM) { |
| 343 |
return ExternalizationType::WEAK; |
| 344 |
} else { |
| 345 |
assert(0 && "Unreachable."); |
| 346 |
} |
| 347 |
|
| 348 |
break; |
| 349 |
case STB_LOCAL: |
| 350 |
return ExternalizationType::STRONG; |
| 351 |
break; |
| 352 |
|
| 353 |
case STB_WEAK: |
| 354 |
default: |
| 355 |
return ExternalizationType::NONE; |
| 356 |
break; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
/* No debuginfo provided, there is nothing we can do. */ |
| 361 |
return ExternalizationType::NONE; |
| 362 |
} |
| 363 |
|
| 364 |
bool InlineAnalysis::Is_Externally_Visible(const std::string &sym) |
| 365 |
{ |
| 366 |
return Needs_Externalization(sym) != ExternalizationType::STRONG; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
std::set<std::string> InlineAnalysis::Get_All_Symbols(void) |
| 371 |
{ |
| 372 |
/* We have to use the information that the user provide us. This means that |
| 373 |
if the debuginfo or the ipaclones are not available, we have to do the |
| 374 |
best we can. */ |
| 375 |
|
| 376 |
std::set<std::string> set; |
| 377 |
|
| 378 |
if (Have_Debuginfo()) { |
| 379 |
for (auto sym : ElfCache->Get_All_Symbols()) |
| 380 |
set.insert(sym); |
| 381 |
} |
| 382 |
|
| 383 |
if (Ipa) { |
| 384 |
for (auto node_map : *Ipa) { |
| 385 |
const std::string &sym = node_map.first; |
| 386 |
set.insert(sym); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
if (Symv) { |
| 391 |
for (auto &sym : Symv->Get_All_Symbols()) |
| 392 |
set.insert(sym); |
| 393 |
} |
| 394 |
|
| 395 |
return set; |
| 396 |
} |
| 397 |
|
| 398 |
void InlineAnalysis::Print_Symbol_Set(const std::set<std::string> &symbol_set, |
| 399 |
bool csv, FILE *out) |
| 400 |
{ |
| 401 |
bool have_debuginfo = Have_Debuginfo(); |
| 402 |
size_t max_symbol_chars = 15; |
| 403 |
size_t max_symbol_chars_mangled = 15; |
| 404 |
size_t num_symbols = symbol_set.size(); |
| 405 |
|
| 406 |
if (num_symbols == 0) { |
| 407 |
/* No symbols to print. */ |
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
const char *demangleds[num_symbols]; |
| 412 |
size_t i = 0; |
| 413 |
|
| 414 |
for (std::string s : symbol_set) { |
| 415 |
const char *demangled = InlineAnalysis::Demangle_Symbol(s.c_str()); |
| 416 |
max_symbol_chars = std::max(max_symbol_chars, strlen(demangled)); |
| 417 |
max_symbol_chars_mangled = std::max(max_symbol_chars_mangled, s.length()); |
| 418 |
|
| 419 |
demangleds[i++] = demangled; |
| 420 |
} |
| 421 |
|
| 422 |
// Ignore warnings related to printf formating. |
| 423 |
#pragma GCC diagnostic push |
| 424 |
#pragma GCC diagnostic ignored "-Wformat-security" |
| 425 |
/* Print table header. */ |
| 426 |
fprintf(out,"Mangled name"); |
| 427 |
if (csv) { |
| 428 |
fprintf(out,";"); |
| 429 |
} else { |
| 430 |
size_t j = max_symbol_chars_mangled + 1 - strlen("Mangled name"); |
| 431 |
fprintf(out, std::string(j, ' ').c_str()); |
| 432 |
} |
| 433 |
|
| 434 |
fprintf(out,"Demangled name"); |
| 435 |
if (csv) { |
| 436 |
fprintf(out,";"); |
| 437 |
} else { |
| 438 |
size_t s = max_symbol_chars + 1 - strlen("Demangled name"); |
| 439 |
fprintf(out, std::string(s, ' ').c_str()); |
| 440 |
} |
| 441 |
|
| 442 |
if (have_debuginfo) { |
| 443 |
if (csv) { |
| 444 |
fprintf(out,"Type;Available?"); |
| 445 |
} else { |
| 446 |
fprintf(out,"Type\tAvailable?"); |
| 447 |
} |
| 448 |
} else if (Symv) { |
| 449 |
fprintf(out, "Module"); |
| 450 |
} |
| 451 |
fprintf(out, "\n"); |
| 452 |
|
| 453 |
i = 0; |
| 454 |
for (std::string s : symbol_set) { |
| 455 |
/* Print mangled name. */ |
| 456 |
fprintf(out,"%s", s.c_str()); |
| 457 |
if (csv) { |
| 458 |
fprintf(out,";"); |
| 459 |
} else { |
| 460 |
size_t j = max_symbol_chars_mangled + 1 - s.length(); |
| 461 |
fprintf(out, std::string(j, ' ').c_str()); |
| 462 |
} |
| 463 |
|
| 464 |
/* Print demangled name. */ |
| 465 |
const char *demangled = demangleds[i++]; |
| 466 |
size_t len = strlen(demangled); |
| 467 |
fprintf(out,"%s", demangled); |
| 468 |
if (csv) { |
| 469 |
fprintf(out,";"); |
| 470 |
} else { |
| 471 |
size_t j = max_symbol_chars + 1 - len; |
| 472 |
fprintf(out, std::string(j, ' ').c_str()); |
| 473 |
} |
| 474 |
|
| 475 |
/* In case we have debuginfo, also print if symbols was completely inlined. */ |
| 476 |
if (have_debuginfo) { |
| 477 |
auto syminfos = Get_Symbol_Info(s); |
| 478 |
unsigned char syminfo = syminfos.first; |
| 479 |
ElfSymtabType symtab = syminfos.second; |
| 480 |
|
| 481 |
unsigned type = ElfSymbol::Type_Of(syminfo); |
| 482 |
unsigned bind = ElfSymbol::Bind_Of(syminfo); |
| 483 |
|
| 484 |
const char *type_str = ElfSymbol::Type_As_String(type); |
| 485 |
const char *bind_str = (syminfo > 0) ? Bind(bind, symtab) : "Inlined"; |
| 486 |
|
| 487 |
if (csv) { |
| 488 |
fprintf(out,"%s;%s\n", type_str, bind_str); |
| 489 |
} else { |
| 490 |
fprintf(out,"%s\t%s\n", type_str, bind_str); |
| 491 |
} |
| 492 |
} else if (Symv) { |
| 493 |
fprintf(out, "%s\n", Symv->Get_Symbol_Module(s).c_str()); |
| 494 |
} else { |
| 495 |
fprintf(out,"\n"); |
| 496 |
} |
| 497 |
|
| 498 |
free(demangled); |
| 499 |
} |
| 500 |
#pragma GCC diagnostic pop |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
const char *InlineAnalysis::Demangle_Symbol(const char *symbol) |
| 505 |
{ |
| 506 |
int status; |
| 507 |
const char *demangled = abi::__cxa_demangle(symbol, nullptr, nullptr, &status); |
| 508 |
if (status == -2) { |
| 509 |
return strdup(symbol); |
| 510 |
} else { |
| 511 |
return demangled; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
/* |
| 516 |
* Check if Kernel module was found on Symvers or ELF object. Returns empty is |
| 517 |
* the module was not found, or if the LP is not from a kernel source. |
| 518 |
*/ |
| 519 |
std::string InlineAnalysis::Get_Symbol_Module(std::string sym) |
| 520 |
{ |
| 521 |
std::string mod; |
| 522 |
if (Symv) { |
| 523 |
mod = Symv->Get_Symbol_Module(sym); |
| 524 |
if (!mod.empty()) |
| 525 |
return mod; |
| 526 |
} |
| 527 |
|
| 528 |
if (Have_Debuginfo()) |
| 529 |
return ElfCache->Get_Symbol_Module(sym); |
| 530 |
|
| 531 |
return {}; |
| 532 |
} |