From 3c33437ddf49e813c5dd0a6652e80486deaefcc5 Mon Sep 17 00:00:00 2001 From: Dodji Seketeli Date: Thu, 8 Aug 2024 11:47:06 +0200 Subject: [PATCH] ctf-reader: Enumerate dicts in the archive rather than using their name ctf::reader::process_ctf_archive calls ctf_dict_open with the name of the (parent) dictionary to open. If that parent dictionary is set to an unexpected name, then the call to ctf_dict_open fails. This can happen for instance when at link time, the name of the parent dictionary is set to an arbitrary name using the "ctf_link_set_memb_name_changer" function. This patch enumerates the dictionaries of the archive to avoid having to know the name of the parent dictionary. The enumeration is done using the ctf_archive_next function of libctf. There is currently no binary with an unexpected dictionary name in the test suite so this patch cannot be tested for that particular case. I'd be glad to have such binaries added to the test suite. In the mean time this patch has been tested successfully using "make fullcheck" on the existing test suite. * src/abg-ctf-reader.cc (reader::process_ctf_archive): Do not use ctf_dict_open to open the dictionary by name. Rather, enumerate the dictionaries of the current archive by using ctf_archive_next just like what lookup_symbol_in_ctf_archive does. Set the argument of the skip_parent parameter to "false" to ensure we get the parent dictionary. (lookup_symbol_in_ctf_archive): Clean this up to properly initialize the parameters and to document the arguments to the parameters of ctf_archive_next. Use nullptr instead of NULL. * tests/data/test-diff-pkg-ctf/test-rpm-report-1.txt: Adjust. * tests/data/test-diff-pkg-ctf/test-rpm-report-2.txt: Adjust. Signed-off-by: Dodji Seketeli --- src/abg-ctf-reader.cc | 52 +++++++++---------- .../test-diff-pkg-ctf/test-rpm-report-1.txt | 2 +- .../test-diff-pkg-ctf/test-rpm-report-2.txt | 2 +- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/abg-ctf-reader.cc b/src/abg-ctf-reader.cc index 9f551eab..8c7e6e9e 100644 --- a/src/abg-ctf-reader.cc +++ b/src/abg-ctf-reader.cc @@ -445,35 +445,26 @@ public: corp->add(ir_translation_unit); cur_transl_unit(ir_translation_unit); - int ctf_err; - ctf_dict_t *ctf_dict, *dict_tmp; + ctf_dict_t *ctf_dict = nullptr, *initial_ctf_dict = nullptr; const auto symt = symtab(); symtab_reader::symtab_filter filter = symt->make_filter(); filter.set_public_symbols(); - std::string dict_name; - if ((corp->get_origin() & corpus::LINUX_KERNEL_BINARY_ORIGIN) - && corpus_group()) + ctf_next_t *it = nullptr; + // Iterate through the dictionnaries of the archive and get the + // first one, which should be the parent dictionnary. + initial_ctf_dict = ctf_archive_next(ctfa, /*iterator=*/&it, + /*dict_name=*/nullptr, + /*skip_parent=*/false, + /*ctf_error=*/nullptr); + if (!initial_ctf_dict) { - tools_utils::base_name(corpus_path(), dict_name); - // remove .* suffix - std::size_t pos = dict_name.find("."); - if (pos != string::npos) - dict_name.erase(pos); - - std::replace(dict_name.begin(), dict_name.end(), '-', '_'); + std::cerr << "Could not find any dictionnary in the CTF archive\n"; + ctf_next_destroy(it); + return; } - if ((ctf_dict = ctf_dict_open(ctfa, - dict_name.empty() ? NULL : dict_name.c_str(), - &ctf_err)) == NULL) - { - fprintf(stderr, "ERROR dictionary not found\n"); - abort(); - } - - dict_tmp = ctf_dict; - + ctf_dict = initial_ctf_dict; for (const auto& symbol : symtab_reader::filtered_symtab(*symt, filter)) { std::string sym_name = symbol->get_name(); @@ -524,11 +515,14 @@ public: func_declaration->set_is_in_public_symbol_table(true); add_fn_to_exported_or_undefined_decls(func_declaration.get()); } - - ctf_dict = dict_tmp; + if (ctf_dict != initial_ctf_dict) + { + ctf_dict_close(initial_ctf_dict); + initial_ctf_dict = ctf_dict; + } } - ctf_dict_close(ctf_dict); + ctf_next_destroy(it); } /// Add a new type declaration to the given libabigail IR corpus CORP. @@ -1667,10 +1661,12 @@ lookup_symbol_in_ctf_archive(ctf_archive_t *ctfa, ctf_dict_t **ctf_dict, if (ctf_type == CTF_ERR) { ctf_dict_t *fp; - ctf_next_t *i = NULL; - const char *arcname; + ctf_next_t *i = nullptr; + const char *arcname = nullptr; - while ((fp = ctf_archive_next(ctfa, &i, &arcname, 1, &ctf_err)) != NULL) + while ((fp = ctf_archive_next(ctfa, &i, &arcname, + /*skip_parent=*/true, + &ctf_err)) != nullptr) { if ((ctf_type = ctf_lookup_by_symbol_name (fp, sym_name)) == CTF_ERR) ctf_type = ctf_lookup_variable(fp, sym_name); diff --git a/tests/data/test-diff-pkg-ctf/test-rpm-report-1.txt b/tests/data/test-diff-pkg-ctf/test-rpm-report-1.txt index 84d22968..af0afa73 100644 --- a/tests/data/test-diff-pkg-ctf/test-rpm-report-1.txt +++ b/tests/data/test-diff-pkg-ctf/test-rpm-report-1.txt @@ -1,5 +1,5 @@ ================ changes of 'libdwarf.so.1.20180129.0'=============== - Functions changes summary: 0 Removed, 0 Changed, 0 Added function + Functions changes summary: 0 Removed, 0 Changed (1 filtered out), 0 Added function Variables changes summary: 0 Removed, 0 Changed, 0 Added variable Function symbols changes summary: 0 Removed, 1 Added function symbol not referenced by debug info Variable symbols changes summary: 0 Removed, 0 Added variable symbol not referenced by debug info diff --git a/tests/data/test-diff-pkg-ctf/test-rpm-report-2.txt b/tests/data/test-diff-pkg-ctf/test-rpm-report-2.txt index e22399de..1347b012 100644 --- a/tests/data/test-diff-pkg-ctf/test-rpm-report-2.txt +++ b/tests/data/test-diff-pkg-ctf/test-rpm-report-2.txt @@ -1,5 +1,5 @@ ================ changes of 'libdwarf.so.1.20180129.0'=============== - Functions changes summary: 0 Removed, 0 Changed, 0 Added function + Functions changes summary: 0 Removed, 0 Changed (1 filtered out), 0 Added function Variables changes summary: 0 Removed, 0 Changed, 0 Added variable Function symbols changes summary: 1 Removed, 0 Added function symbol not referenced by debug info Variable symbols changes summary: 0 Removed, 0 Added variable symbol not referenced by debug info