From nobody Sun Dec 14 01:57:44 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1750676582; cv=none; d=zohomail.com; s=zohoarc; b=eXuzN02GIR+/l3IjbEjCTKTanPBR34u0SONG/wBm30PnEdCgSQkAhfcWVR9a9go7o4uNU4U7ATHGp1gqUs/eYgppEpC4a2SePFnGdluWkLBQo/6XGvTJQZcITN/BLIoMVtwtcy52g8BE1uVFQ8a4jS6iMhidqdtFH8t0sd5CAXw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750676582; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Subject:Subject:To:To:Message-Id; bh=trUt7QKJQslwkAp4DxTYK5e03Lw6qxo7Dtom9uzAwpA=; b=eD6QU9VEBK5UAQJuBJZt/P7izGe3rYWU0d91ThoAMlK7f2YFSyBHXe8SiZ7xP9UWvM2NQqyoMFPY7FHfMNw6KLbx4h41T9qnJOVaPOLfItCq5aBYAoETW+2k4xXt+ubMlnNIk310CXbgVrFRRdY03fc72uOUEzNPwVvwSNOI9Lg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1750676582733653.1008755567435; Mon, 23 Jun 2025 04:03:02 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 9E470A4F; Mon, 23 Jun 2025 07:03:01 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 277F5B33; Mon, 23 Jun 2025 07:02:41 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 013E2A1F; Mon, 23 Jun 2025 07:02:38 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 65A2AA4F for ; Mon, 23 Jun 2025 07:02:37 -0400 (EDT) Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-672-Ag5mQgyKO2-q4Fa-bqvnuQ-1; Mon, 23 Jun 2025 07:02:35 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B3EDD1809C84 for ; Mon, 23 Jun 2025 11:02:34 +0000 (UTC) Received: from toolbx.redhat.com (unknown [10.42.28.95]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 77A9D19560AF; Mon, 23 Jun 2025 11:02:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750676557; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=sovrHgS8gSVYzayIzGnmUVef1O6/BoVRFwX25fIX6xM=; b=AMG+1Vri58gy/IDZ62XT/wgp+tjt8j8nfIjoTREpG7JQeVE7/q+3JVGCWXJYoscj83d5g2 HZWJ/sVAq2LnvV4d5ymYd1ovAONPgSjYVkkMdhx0Ox8nssquvZvF8orI9vAJf55mdVZE71 sy+3346/P/5b/LZQTZe+XIWQMFdXu5A= X-MC-Unique: Ag5mQgyKO2-q4Fa-bqvnuQ-1 X-Mimecast-MFC-AGG-ID: Ag5mQgyKO2-q4Fa-bqvnuQ_1750676554 To: devel@lists.libvirt.org Cc: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH] util: workaround libxml2 lack of thread safe initialization Date: Mon, 23 Jun 2025 12:02:31 +0100 Message-ID: <20250623110231.3262608-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: ZSV4wuOuB09tvDzhn-gLlKOWtN8w79uRVWnKGzDI5es_1750676554 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: P52BNZ4R7LCCMQ7ZNGP2226RMUJ4NEAX X-Message-ID-Hash: P52BNZ4R7LCCMQ7ZNGP2226RMUJ4NEAX X-MailFrom: berrange@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9_via_Devel?= Reply-To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1750676586717116600 Content-Type: text/plain; charset="utf-8" From: Daniel P. Berrang=C3=A9 The main XML parser code global initializer historically had a mutex protecting it, and more recently uses a pthread_once. The RelaxNG code, however, relies on three other global initializers that are not thread safe, just relying on setting an integer "initialized" flag. Calling the relevant initializers from libvirt in a protected global initializer will protect libvirt's own concurrent usage, however, it cannot protect against other libraries loaded in process that might be using libxml2's schema code. Fortunately: * The chances of other loaded non-libvirt code using libxml is relatively low * The chances of other loaded non-libvirt code using the schema validation / catalog functionality inside libxml is even lower * The chances of both libvirt and the non-libvirt usage having their *1st* usage of libxml2 be concurrent is tiny IOW, in practice, although our solution doesn't fully fix the thread safety, it is good enough. libxml2 should none the less still be fixed to make its global initializers be thread safe without special actions by its API consumers. Resolves: https://gitlab.com/libvirt/libvirt/-/issues/788 Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Peter Krempa --- src/util/virxml.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/util/virxml.c b/src/util/virxml.c index 9d46e5f32f..c2d5a109dc 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -26,6 +26,8 @@ =20 #include #include +#include +#include =20 #include "virerror.h" #include "virxml.h" @@ -35,6 +37,7 @@ #include "virstring.h" #include "virutil.h" #include "viruuid.h" +#include "virthread.h" #include "configmake.h" =20 #define VIR_FROM_THIS VIR_FROM_XML @@ -50,6 +53,25 @@ struct virParserData { }; =20 =20 +static int virXMLSchemaOnceInit(void) +{ + if (xmlSchemaInitTypes() < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to initialize libxml2 schema types")); + return -1; + } + if (xmlRelaxNGInitTypes() < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to initialize libxml2 RelaxNG data")); + return -1; + } + /* No return value, it just ignores errors :-( */ + xmlInitializeCatalog(); + return 0; +} + +VIR_ONCE_GLOBAL_INIT(virXMLSchema); + static xmlXPathContextPtr virXMLXPathContextNew(xmlDocPtr xml) { @@ -1603,6 +1625,9 @@ virXMLValidatorInit(const char *schemafile) { g_autoptr(virXMLValidator) validator =3D NULL; =20 + if (virXMLSchemaInitialize() < 0) + return NULL; + validator =3D g_new0(virXMLValidator, 1); =20 validator->schemafile =3D g_strdup(schemafile); --=20 2.49.0