[05/16] abipkgdiff: Initialize libxml2 to use it in a multi-thread context

Message ID 87v8cmf6g0.fsf@redhat.com
State New
Series Fixing various issues found while working on PR30309 |

Commit Message

Dodji Seketeli Sept. 7, 2023, 1:39 p.m. UTC

While running some test, I stumbled upon a transient deadlock
happening when using libxml2's text reader to create a reader from a
buffer.  After reading the documentation at
https://gitlab.gnome.org/GNOME/libxml2/-/wikis/Thread-safety, I
realized that users of the library need to initialize libxml2 before
using it in a multi-thread setting.

So this patch is providing the abigail::tools_utils::initialize()
function to be called prior to using the library.  This is going to be
the place where to perform this kind of one-time initialization.

	* include/abg-tools-utils.h (initialize): Declare ...
	* src/abg-tools-utils.cc (initialize): ... new function.
	* tools/abipkgdiff.cc (main): Invoke the new
	abigail::tools_utils::initialize() here.

Signed-off-by: Dodji Seketeli <dodji@redhat.com>
Applied to master.
 include/abg-tools-utils.h |  1 +
 src/abg-tools-utils.cc    | 17 ++++++++++++++++-
 tools/abipkgdiff.cc       |  2 ++
 3 files changed, 19 insertions(+), 1 deletion(-)


diff --git a/include/abg-tools-utils.h b/include/abg-tools-utils.h
index 7f931c5d..ef748871 100644
--- a/include/abg-tools-utils.h
+++ b/include/abg-tools-utils.h
@@ -30,6 +30,7 @@  using std::string;
 using std::set;
 using std::shared_ptr;
+void initialize();
 const char* get_system_libdir();
 const char* get_anonymous_struct_internal_name_prefix();
 const char* get_anonymous_union_internal_name_prefix();
diff --git a/src/abg-tools-utils.cc b/src/abg-tools-utils.cc
index 2ea6f8b4..62a0ebb1 100644
--- a/src/abg-tools-utils.cc
+++ b/src/abg-tools-utils.cc
@@ -33,7 +33,8 @@ 
 #include <ctype.h>
 #include <errno.h>
 #include <libgen.h>
+#include <libxml/parser.h>
+#include <libxml/xmlversion.h>
 #include <algorithm>
 #include <cstdlib>
 #include <cstring>
@@ -76,6 +77,20 @@  using namespace abigail::ini;
 namespace tools_utils
+/// This function needs to be called before any libabigail function.
+/// Users of libabigail must call it prior to using any of the
+/// functions of the library.
+/// It intends to initialize the underlying libraries that might need
+/// initialization, especially, libxml2, in multi-threaded environments.
+  xmlInitParser();
 /// Get the value of $libdir variable of the autotools build
 /// system.  This is where shared libraries are usually installed.
diff --git a/tools/abipkgdiff.cc b/tools/abipkgdiff.cc
index 69661c5b..5ab5c22c 100644
--- a/tools/abipkgdiff.cc
+++ b/tools/abipkgdiff.cc
@@ -3887,6 +3887,8 @@  main(int argc, char* argv[])
 	      | abigail::tools_utils::ABIDIFF_ERROR);
+  abigail::tools_utils::initialize();
   if (opts.self_check)
     return compare_to_self(first_package, opts);