From nobody Thu Apr 30 15:30:55 2026 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=1745915571; cv=none; d=zohomail.com; s=zohoarc; b=BA5WXO6ANsRvdT+9hfYMM8q+wRIHT/bIqB3JXrwmJGlv9FecqpjXE5bGa8nmxBQM5pgLK9kT6zvISH1+O2afrF9E7g4G4gj18vks6XEAJgZRoqSgM4Uf3pz3Mqh0zRG9fy0v+zCg0CEJ5e/M0S3YBCVhPwA1dwaB7FNL68AKefQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1745915571; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=UpEjHk/S78N7y3etNP7B4Vz1B9edomiTrzSgrjwnnkQ=; b=MKD50D5/AhWxzuLxAuzv/pXUkJjeWJeKT73KUsHQOVr9aTEPEk/Z1O6h5h0LHDr3LdIo0mqFSxucW5cK2NeYSSb3W+w3dhZqnsyk9dkM3y9Qcyhi6itAfZIpkT+drTLoi1Iu+LXHnZEsBeVgVNo5uDC4FOn9ZZp2kpTqzGxCSwI= 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 1745915571650109.87793510644417; Tue, 29 Apr 2025 01:32:51 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id E5B1B124F; Tue, 29 Apr 2025 04:32:50 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 52DF71274; Tue, 29 Apr 2025 04:31:58 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 2B8ABADC; Tue, 29 Apr 2025 04:31:52 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 70C2EB38 for ; Tue, 29 Apr 2025 04:31:51 -0400 (EDT) Received: from mx-prod-mc-06.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-423-s73xXWbFP6OC6tZ8YCIA4Q-1; Tue, 29 Apr 2025 04:31:49 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A305B1800873 for ; Tue, 29 Apr 2025 08:31:48 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.3.236]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0BBDB18002AD for ; Tue, 29 Apr 2025 08:31:47 +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=1745915511; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4W7azJRBOpJMBXdppGsPjCVk5TDd8XiDglyTBN9/sdE=; b=Itr5+c/BBsQADRiYyLQxRcLHj/oT4O6ajnul0AsSU/7G74hij+qmS+ZY3WxedANGjiI/4D oEAzohqckobpRf6h4BgmX2KT1KZiVaf6W8r1ViGoMeykPG9QcfeytGPpCwBZQ9PeJu7ZXc LCNnjc+MfX5L7adphDhECiXj3yj3chM= X-MC-Unique: s73xXWbFP6OC6tZ8YCIA4Q-1 X-Mimecast-MFC-AGG-ID: s73xXWbFP6OC6tZ8YCIA4Q_1745915508 To: devel@lists.libvirt.org Subject: [PATCH v2 1/3] internal: Introduce ATTRIBUTE_MOCKABLE Date: Tue, 29 Apr 2025 10:31:41 +0200 Message-ID: <3c6c1c73332f4353144ca3ecc953be59172ab11d.1745914265.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 6fN4OLmgcWO9jVXl3qcaXG-iPOJc4qLrzAE27lM3aRY_1745915508 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: TCP6W5MP5R3ZICNHXW4HRCU2PHZRU7TW X-Message-ID-Hash: TCP6W5MP5R3ZICNHXW4HRCU2PHZRU7TW X-MailFrom: mprivozn@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: Michal Privoznik via Devel Reply-To: Michal Privoznik X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1745915573807019000 Content-Type: text/plain; charset="utf-8" From: Michal Privoznik Currently, if we want to mock a function the noinline attribute is appended after the function (via G_NO_INLINE macro). This used to work for non pure functions. But there are some trivial functions (for instance virQEMUCapsProbeHVF()) that are pure, i.e. have no side effect, and while their call from other parts of the code is not optimized out, their call from within the same compilation unit (qemu_capabilities.c) is optimized out. This is because inlining and semantic interposition are two different things. Even GCC's documentation for noinline attribute [1] states that clearly: This function attribute prevents a function from being considered for inlining. It also disables some other interprocedural optimizations; it=E2=80=99s preferable to use the more comprehensive noipa attribute instead if that is your goal. Even if a function is declared with the noinline attribute, there are optimizations other than inlining that can cause calls to be optimized away if it does not have side effects, although the function call is live. Unfortunately, despite attempts [2] Clang still does not support the attribute and thus we have to rely on noinline + -fsemantic-interposition combo. 1: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index= -noinline-function-attribute 2: https://reviews.llvm.org/D101011 Signed-off-by: Michal Privoznik Reviewed-by: Daniel P. Berrang=C3=A9 --- docs/coding-style.rst | 2 +- scripts/cocci-macro-file.h | 1 + src/internal.h | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/coding-style.rst b/docs/coding-style.rst index fe5fe9a906..a4934ef333 100644 --- a/docs/coding-style.rst +++ b/docs/coding-style.rst @@ -633,7 +633,7 @@ analysis tools understand the code better: ``G_GNUC_FALLTHROUGH`` allow code reuse by multiple switch cases =20 -``G_NO_INLINE`` +``ATTRIBUTE_MOCKABLE`` the function is mocked in the test suite =20 ``G_GNUC_NORETURN`` diff --git a/scripts/cocci-macro-file.h b/scripts/cocci-macro-file.h index c3112663d1..4f70c6b4cc 100644 --- a/scripts/cocci-macro-file.h +++ b/scripts/cocci-macro-file.h @@ -21,6 +21,7 @@ =20 #pragma once =20 +#define ATTRIBUTE_MOCKABLE #define ATTRIBUTE_NONNULL(x) #define ATTRIBUTE_PACKED =20 diff --git a/src/internal.h b/src/internal.h index 20aa9b1d41..092b02120a 100644 --- a/src/internal.h +++ b/src/internal.h @@ -177,6 +177,28 @@ # endif #endif =20 +/** + * + * ATTRIBUTE_MOCKABLE + * + * Force compiler to disable interprocedural optimizations between the + * function with this attribute and its callers. On compilers that + * support it (gcc), this expands to noipa attribute which implies + * noinline attribute and some others and allows us to mock functions + * even if they are pure. + * + * On compilers which don't support the noipa attribute (clang) this + * expands to noinline attribute which in combination with + * -fsemantic-interposition option does roughly the same. + */ +#ifndef ATTRIBUTE_MOCKABLE +# if defined(__has_attribute) && __has_attribute(noipa) +# define ATTRIBUTE_MOCKABLE __attribute__((noipa)) +# else +# define ATTRIBUTE_MOCKABLE G_NO_INLINE +# endif +#endif + #define VIR_WARNINGS_NO_CAST_ALIGN \ _Pragma ("GCC diagnostic push") \ _Pragma ("GCC diagnostic ignored \"-Wcast-align\"") --=20 2.49.0