From nobody Mon Nov 25 09:51:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1716529074; cv=none; d=zohomail.com; s=zohoarc; b=n8qYjtGj8CA5liGwmqL5cJod9xnZ9Rm75ofbV6YC8gmnJAFI5JubNwqHDlY1x/Jxs0ZP3iAggvf1zhTQCGUi1gyd/Wpc7JPRM08pGFCY7dR0aHHyfgOC/GvHZwXNSmCUxN8flOKk8/n2TX7R3153iS0Ju6pX5org09xf4w+wNH0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716529074; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=AfvWh15VwJ7bsi1zE22Wn8t3fv0MNqUE1xgb1xRqaAQ=; b=jbHSqDJ2C3uLb2pr9Y9DMfMbsTrY0sTxL+0BHa+kgw6oUXKOgKMjOSPPs9HClvklett9kUHahYbxe8KC4+61XZLksEYXXCP11QmVIrUSfApBDgOpAwFGujQxJ8j40NIgm/W40DgJxrd6tnWaC4gZyMs/NuN6bO7YXnnTukcfqvQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716529074678270.97050482043176; Thu, 23 May 2024 22:37:54 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sANbV-0008Cz-KT; Fri, 24 May 2024 01:36:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sANbS-0008CG-TA for qemu-devel@nongnu.org; Fri, 24 May 2024 01:36:30 -0400 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sANbQ-00015A-Ne for qemu-devel@nongnu.org; Fri, 24 May 2024 01:36:30 -0400 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-6f8ea3e9543so449059b3a.2 for ; Thu, 23 May 2024 22:36:28 -0700 (PDT) Received: from localhost ([157.82.204.135]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-6f8fd4d58easm457201b3a.198.2024.05.23.22.36.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 23 May 2024 22:36:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1716528987; x=1717133787; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=AfvWh15VwJ7bsi1zE22Wn8t3fv0MNqUE1xgb1xRqaAQ=; b=mHfKrR1cXZMm2ZZ3ds1WlsFsSYyg0w1Pjv3uzXH45J0EN75t9ReqsmTy6PfyO5S2sC 4h9zWltjnc/SDcLWqNN7EaWqW2aLxN2HLK2Wd0eVGjK4VhrmRyEKOw8zHxmbNCnF7u0x RJVFHu+Mpa8BrJCLQK1h9qMIkoc82BXH1nhnlLmA3CENeh4HB3XZirdWLOfp08cKOyT5 kWn3MnNwOerQT8obMgUU21t18ooUyb2m8dzVIBl6K9jTkzI72W3on2J1qjNYiM6xQw0P oNrXxAA2eI8sIBTFfiJ1ABKbiuOytDxRXjUd6DIiy2xTyc8lRycZKTrvoWYJUyfY1wgQ 5gQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716528987; x=1717133787; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AfvWh15VwJ7bsi1zE22Wn8t3fv0MNqUE1xgb1xRqaAQ=; b=inn1bmMEWFdSAtLq0a8kELn2MCEhQi3WRUQDhfGQyuUfeIV0EXlXrFRC+lNsQS/Jkr ikBcTc1LRtNfDxn7MVo5FBnolVlWLUdgp6VZR5LuAbGCLGkUWH0iKbep9H8T1DzZCeHR lytQA8zvtHg9JIJEbZyEC8fRQaYvcitBc4/ap4fuUwJlgR5T50uFC7sLUfSmPogxdLhW fUme4CXwm+auMlq5uSV15BIrzqyPfke52L62gNBVFj5aDPjBzSFTQw+Abr6hJHYcUDxV BshePkcta1Yz0Zct7yZM74p6Iug4fkKVAuPYK4I90YTnogfEde9LmQzyJumZNGUDzAAF hHAQ== X-Gm-Message-State: AOJu0YwDJrjUrel7EPQDSssQ3vaAAmLFRwQM2sCq7a30HKWKQGgVtwJY jdpNvPsktU939NZJkIGCMXK+7hWATvQiVcD7jMN0A7A7mVHHF8xbFNKWnEcs++w= X-Google-Smtp-Source: AGHT+IFG3fAGmsAARtFqlWWNC2/jCc7tD//SUhB73tK0XCnUrtlh0XI/XihPvDzmS0liqvJzastt7w== X-Received: by 2002:a05:6a00:8c11:b0:6f0:b53c:dfb4 with SMTP id d2e1a72fcca58-6f8f3e7d47emr1398064b3a.22.1716528987279; Thu, 23 May 2024 22:36:27 -0700 (PDT) From: Akihiko Odaki Date: Fri, 24 May 2024 14:35:49 +0900 Subject: [PATCH v4 3/4] qapi: Do not cast function pointers MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240524-xkb-v4-3-2de564e5c859@daynix.com> References: <20240524-xkb-v4-0-2de564e5c859@daynix.com> In-Reply-To: <20240524-xkb-v4-0-2de564e5c859@daynix.com> To: Michael Tokarev , Laurent Vivier , Paolo Bonzini , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Thomas Huth , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , =?utf-8?q?Alex_Benn=C3=A9e?= , Wainer dos Santos Moschetta , Beraldo Leal , Richard Henderson , Laurent Vivier Cc: qemu-devel@nongnu.org, Akihiko Odaki X-Mailer: b4 0.14-dev-01a33 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::434; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x434.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1716529075412100001 -fsanitize=3Dundefined complains if function pointers are casted. It also prevents enabling teh strict mode of CFI which is currently disabled with -fsanitize-cfi-icall-generalize-pointers. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2346 Signed-off-by: Akihiko Odaki Reviewed-by: Markus Armbruster --- include/qapi/clone-visitor.h | 37 ++++++++++++++++++++++++------------- qapi/qapi-clone-visitor.c | 30 ++++-------------------------- 2 files changed, 28 insertions(+), 39 deletions(-) diff --git a/include/qapi/clone-visitor.h b/include/qapi/clone-visitor.h index adf9a788e232..ebc182b034d7 100644 --- a/include/qapi/clone-visitor.h +++ b/include/qapi/clone-visitor.h @@ -11,6 +11,7 @@ #ifndef QAPI_CLONE_VISITOR_H #define QAPI_CLONE_VISITOR_H =20 +#include "qapi/error.h" #include "qapi/visitor.h" =20 /* @@ -20,11 +21,8 @@ */ typedef struct QapiCloneVisitor QapiCloneVisitor; =20 -void *qapi_clone(const void *src, bool (*visit_type)(Visitor *, const char= *, - void **, Error **)); -void qapi_clone_members(void *dst, const void *src, size_t sz, - bool (*visit_type_members)(Visitor *, void *, - Error **)); +Visitor *qapi_clone_visitor_new(void); +Visitor *qapi_clone_members_visitor_new(void); =20 /* * Deep-clone QAPI object @src of the given @type, and return the result. @@ -32,10 +30,18 @@ void qapi_clone_members(void *dst, const void *src, siz= e_t sz, * Not usable on QAPI scalars (integers, strings, enums), nor on a * QAPI object that references the 'any' type. Safe when @src is NULL. */ -#define QAPI_CLONE(type, src) \ - ((type *)qapi_clone(src, \ - (bool (*)(Visitor *, const char *, void **, \ - Error **))visit_type_ ## type)) +#define QAPI_CLONE(type, src) \ + ({ \ + Visitor *v_; \ + type *dst_ =3D (type *) (src); /* Cast away const */ \ + \ + if (dst_) { \ + v_ =3D qapi_clone_visitor_new(); \ + visit_type_ ## type(v_, NULL, &dst_, &error_abort); \ + visit_free(v_); \ + } \ + dst_; \ + }) =20 /* * Copy deep clones of @type members from @src to @dst. @@ -43,9 +49,14 @@ void qapi_clone_members(void *dst, const void *src, size= _t sz, * Not usable on QAPI scalars (integers, strings, enums), nor on a * QAPI object that references the 'any' type. */ -#define QAPI_CLONE_MEMBERS(type, dst, src) \ - qapi_clone_members(dst, src, sizeof(type), \ - (bool (*)(Visitor *, void *, \ - Error **))visit_type_ ## type ## _members) +#define QAPI_CLONE_MEMBERS(type, dst, src) \ + ({ \ + Visitor *v_; \ + \ + v_ =3D qapi_clone_members_visitor_new(); = \ + *(type *)(dst) =3D *(src); = \ + visit_type_ ## type ## _members(v_, (type *)(dst), &error_abort); \ + visit_free(v_); \ + }) =20 #endif diff --git a/qapi/qapi-clone-visitor.c b/qapi/qapi-clone-visitor.c index c45c5caa3b89..bbf953698f38 100644 --- a/qapi/qapi-clone-visitor.c +++ b/qapi/qapi-clone-visitor.c @@ -149,7 +149,7 @@ static void qapi_clone_free(Visitor *v) g_free(v); } =20 -static Visitor *qapi_clone_visitor_new(void) +Visitor *qapi_clone_visitor_new(void) { QapiCloneVisitor *v; =20 @@ -174,31 +174,9 @@ static Visitor *qapi_clone_visitor_new(void) return &v->visitor; } =20 -void *qapi_clone(const void *src, bool (*visit_type)(Visitor *, const char= *, - void **, Error **)) +Visitor *qapi_clone_members_visitor_new(void) { - Visitor *v; - void *dst =3D (void *) src; /* Cast away const */ - - if (!src) { - return NULL; - } - - v =3D qapi_clone_visitor_new(); - visit_type(v, NULL, &dst, &error_abort); - visit_free(v); - return dst; -} - -void qapi_clone_members(void *dst, const void *src, size_t sz, - bool (*visit_type_members)(Visitor *, void *, - Error **)) -{ - Visitor *v; - - v =3D qapi_clone_visitor_new(); - memcpy(dst, src, sz); + Visitor *v =3D qapi_clone_visitor_new(); to_qcv(v)->depth++; - visit_type_members(v, dst, &error_abort); - visit_free(v); + return v; } --=20 2.45.1