From nobody Sun Feb 23 05:25:03 2025 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; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1740051462; cv=none; d=zohomail.com; s=zohoarc; b=f35ikQR2rXlKvJd8ehTuDDYt+73SY1SUMgSkb8l5oVQyA3IA5QwzSpxIY7YCd0NTgsZJ1BMT/9s7bRQE8ZJatxTqoM486IkVP+6vmFv14PQz96goRwhU9ckY7mJ/XCXLrUG96u2EvfpsxKrMUmKF/Mwn89eaCKaGrlPck1lQuTA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1740051462; h=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=6S3+h0KILMZzEFjUXbmLqWGpqxuaheqFJv+xCK/jNY0=; b=ixZIWGbV58+57GcdsAO1ipINZwNB1BaZnMVqZ7LmM9qoDzNPlAZ9vz9a2nxDrZZEtjOwug+ZBRFa279oSo/BnH6PoD4pd7hXocgBjAzYAEbRF6ZHmKSL3eN7quPbKwEItuc0Bo5ZIEtzky3qyDn9MGkYjkrEe9fznE9bQp/3WUE= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174005146258588.82646650004597; Thu, 20 Feb 2025 03:37:42 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tl4rk-0004ZB-Cx; Thu, 20 Feb 2025 06:37:16 -0500 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 1tl4rf-0004Yp-Vs for qemu-devel@nongnu.org; Thu, 20 Feb 2025 06:37:12 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tl4re-0000rC-2q for qemu-devel@nongnu.org; Thu, 20 Feb 2025 06:37:11 -0500 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-640-PH49Mo7LPSOQCKEf43RfpA-1; Thu, 20 Feb 2025 06:37:06 -0500 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4399c5ba9e4so4069375e9.3 for ; Thu, 20 Feb 2025 03:37:06 -0800 (PST) Received: from [192.168.10.48] ([176.206.122.109]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4398a44264csm101397705e9.25.2025.02.20.03.37.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 03:37:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740051429; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6S3+h0KILMZzEFjUXbmLqWGpqxuaheqFJv+xCK/jNY0=; b=XaKgTc8eX5apiHNUvGMjXbRmtJ8U71xVqd9KbfUINvYfLF7uueUBRnm/GZ13c0RT+HgY33 Piu08L74Gfk3xDaqogskv+tF/S9TD7wapHHxbEyOcQ8+sytdjCkfa9AI+/IJAP3Gp7NbTg uJokDktspeG4OU9Z+meNJI/KWNkCuhc= X-MC-Unique: PH49Mo7LPSOQCKEf43RfpA-1 X-Mimecast-MFC-AGG-ID: PH49Mo7LPSOQCKEf43RfpA_1740051426 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740051425; x=1740656225; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6S3+h0KILMZzEFjUXbmLqWGpqxuaheqFJv+xCK/jNY0=; b=ZwNX1P4knzU7fgimzm1yhRnL3cS04X3TnQxaF4xEnwWah6SbCaP9GX44oY4OsK5Ulr +lwnSHGe+BTxasiw9nEodUhUfhRTIP91430Qi9L1YAzXHNA9Fz4kqIvp1t6+dEg8DLHp UPOngnMe+8q4Mo/JE+y7+BooDGXNRk4Jp1kNotTSKFWzeBYas7huSlgaZXujNY4SDoIm 4gjvMoL5Uhfz1W4jekG9TsIJ8IHdkEW2Py2YzkbrSFWL99yUpnRqf9uiVAdZ+Ubrk3iq YPOUPdOsKbvlU/VXj/IRMXBluxGSvW8luicVzbik1eiJ4shAsGfGTOF/2h9+S+4h+ANs R03w== X-Gm-Message-State: AOJu0Yz6IyBwSyg7kpS9+W1eCg4ZRIuxJg6jk1MJWJ3yWtIvTG+Q6EPO kE03gdzUQQqAFeZAVz/S4oGxHj4oYNte2g41t8iWfeLDMJ0YHJ63VJ3GYEeiXEdsqrVnlizKsd9 ZBeABKRyUrQWkVi8fcT5WjI8vQ8hunS1RjiV96o5Z4CMMlhnDpCsZiCoVqTWhFTc+RDAZQPBTvJ tyncR4bP1ZzP3H63e/BBPZn9sDfpzjlsAb9oGxqOk= X-Gm-Gg: ASbGncv/CeOPo8ScBZkaWj+Eg1aX7lJVsmA3xs2bhWIsh/jeR4IWLrVqy0cn858cctU pQOu4tavdrmRACCD8z/kVXujWTGypc1iLGArBWubEmQNIc8iKWmyH1asRidomQ9/05sESPbY1WF R4iLE/tEc1Gt6HPwc8palhxe7024v6g22EwNkyf0I/TTcgb4VIT1tNhEyp29NXFkmRH+kR/2Tm7 4xSV9444PsnAEp4UWbKSzAUbYhodFpFBPa1nbUPeJgJVzuVhHbnFwIfupSfQclCvCb4CEhbVELY 3qUv6Njqb1s= X-Received: by 2002:a05:600c:4f91:b0:439:9a40:aa09 with SMTP id 5b1f17b1804b1-4399a40ad23mr54452275e9.25.1740051424880; Thu, 20 Feb 2025 03:37:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IHdDebQN0Va1EShZ+Ns5WBCt7aWD3b6SYwM2p91cnbSoO84AKFL/zFeNW+wBGX5EPRNpv9bLw== X-Received: by 2002:a05:600c:4f91:b0:439:9a40:aa09 with SMTP id 5b1f17b1804b1-4399a40ad23mr54451965e9.25.1740051424346; Thu, 20 Feb 2025 03:37:04 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: zhao1.liu@intel.com, kwolf@redhat.com Subject: [PATCH v3 1/2] rust: subprojects: add libc crate Date: Thu, 20 Feb 2025 12:36:58 +0100 Message-ID: <20250220113659.863332-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250220113659.863332-1-pbonzini@redhat.com> References: <20250220113659.863332-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.191, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-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 @redhat.com) X-ZM-MESSAGEID: 1740051464723019000 Content-Type: text/plain; charset="utf-8" This allows access to errno values. Reviewed-by: Zhao Liu Signed-off-by: Paolo Bonzini --- rust/Cargo.lock | 7 ++++ rust/qemu-api/Cargo.toml | 1 + scripts/archive-source.sh | 2 +- scripts/make-release | 2 +- subprojects/.gitignore | 1 + subprojects/libc-0.2-rs.wrap | 7 ++++ .../packagefiles/libc-0.2-rs/meson.build | 37 +++++++++++++++++++ 7 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 subprojects/libc-0.2-rs.wrap create mode 100644 subprojects/packagefiles/libc-0.2-rs/meson.build diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 79e142723b8..2ebf0a11ea4 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -54,6 +54,12 @@ dependencies =3D [ "either", ] =20 +[[package]] +name =3D "libc" +version =3D "0.2.162" +source =3D "registry+https://github.com/rust-lang/crates.io-index" +checksum =3D "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef0150= 6398" + [[package]] name =3D "pl011" version =3D "0.1.0" @@ -100,6 +106,7 @@ dependencies =3D [ name =3D "qemu_api" version =3D "0.1.0" dependencies =3D [ + "libc", "qemu_api_macros", "version_check", ] diff --git a/rust/qemu-api/Cargo.toml b/rust/qemu-api/Cargo.toml index a51dd142852..57747bc9341 100644 --- a/rust/qemu-api/Cargo.toml +++ b/rust/qemu-api/Cargo.toml @@ -16,6 +16,7 @@ rust-version =3D "1.63.0" =20 [dependencies] qemu_api_macros =3D { path =3D "../qemu-api-macros" } +libc =3D "0.2.162" =20 [build-dependencies] version_check =3D "~0.9" diff --git a/scripts/archive-source.sh b/scripts/archive-source.sh index 30677c3ec90..e461c1531ed 100755 --- a/scripts/archive-source.sh +++ b/scripts/archive-source.sh @@ -28,7 +28,7 @@ sub_file=3D"${sub_tdir}/submodule.tar" # different to the host OS. subprojects=3D"keycodemapdb libvfio-user berkeley-softfloat-3 berkeley-testfloat-3 arbitrary-int-1-rs bilge-0.2-rs - bilge-impl-0.2-rs either-1-rs itertools-0.11-rs proc-macro2-1-rs + bilge-impl-0.2-rs either-1-rs itertools-0.11-rs libc-0.2-rs proc-macro2-= 1-rs proc-macro-error-1-rs proc-macro-error-attr-1-rs quote-1-rs syn-2-rs unicode-ident-1-rs" sub_deinit=3D"" diff --git a/scripts/make-release b/scripts/make-release index 1b89b3423a8..8c3594a1a47 100755 --- a/scripts/make-release +++ b/scripts/make-release @@ -41,7 +41,7 @@ fi # Only include wraps that are invoked with subproject() SUBPROJECTS=3D"libvfio-user keycodemapdb berkeley-softfloat-3 berkeley-testfloat-3 arbitrary-int-1-rs bilge-0.2-rs - bilge-impl-0.2-rs either-1-rs itertools-0.11-rs proc-macro2-1-rs + bilge-impl-0.2-rs either-1-rs itertools-0.11-rs libc-0.2-rs proc-macro2-= 1-rs proc-macro-error-1-rs proc-macro-error-attr-1-rs quote-1-rs syn-2-rs unicode-ident-1-rs" =20 diff --git a/subprojects/.gitignore b/subprojects/.gitignore index 50f173f90db..d12d34618cc 100644 --- a/subprojects/.gitignore +++ b/subprojects/.gitignore @@ -11,6 +11,7 @@ /bilge-impl-0.2.0 /either-1.12.0 /itertools-0.11.0 +/libc-0.2.162 /proc-macro-error-1.0.4 /proc-macro-error-attr-1.0.4 /proc-macro2-1.0.84 diff --git a/subprojects/libc-0.2-rs.wrap b/subprojects/libc-0.2-rs.wrap new file mode 100644 index 00000000000..bbe08f87883 --- /dev/null +++ b/subprojects/libc-0.2-rs.wrap @@ -0,0 +1,7 @@ +[wrap-file] +directory =3D libc-0.2.162 +source_url =3D https://crates.io/api/v1/crates/libc/0.2.162/download +source_filename =3D libc-0.2.162.tar.gz +source_hash =3D 18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01= 506398 +#method =3D cargo +patch_directory =3D libc-0.2-rs diff --git a/subprojects/packagefiles/libc-0.2-rs/meson.build b/subprojects= /packagefiles/libc-0.2-rs/meson.build new file mode 100644 index 00000000000..ac4f80dba98 --- /dev/null +++ b/subprojects/packagefiles/libc-0.2-rs/meson.build @@ -0,0 +1,37 @@ +project('libc-0.2-rs', 'rust', + meson_version: '>=3D1.5.0', + version: '0.2.162', + license: 'MIT OR Apache-2.0', + default_options: []) + +_libc_rs =3D static_library( + 'libc', + files('src/lib.rs'), + gnu_symbol_visibility: 'hidden', + override_options: ['rust_std=3D2015', 'build.rust_std=3D2015'], + rust_abi: 'rust', + rust_args: [ + '--cap-lints', 'allow', + '--cfg', 'freebsd11', + '--cfg', 'libc_priv_mod_use', + '--cfg', 'libc_union', + '--cfg', 'libc_const_size_of', + '--cfg', 'libc_align', + '--cfg', 'libc_int128', + '--cfg', 'libc_core_cvoid', + '--cfg', 'libc_packedN', + '--cfg', 'libc_cfg_target_vendor', + '--cfg', 'libc_non_exhaustive', + '--cfg', 'libc_long_array', + '--cfg', 'libc_ptr_addr_of', + '--cfg', 'libc_underscore_const_names', + '--cfg', 'libc_const_extern_fn', + ], + dependencies: [], +) + +libc_dep =3D declare_dependency( + link_with: _libc_rs, +) + +meson.override_dependency('libc-0.2-rs', libc_dep) --=20 2.48.1 From nobody Sun Feb 23 05:25:03 2025 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; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1740051482; cv=none; d=zohomail.com; s=zohoarc; b=TXBqXyWP962YIjwAzwQuZ7ly/XcFQSTrkSySgVxnyIs7j1wiDIB9bA+RBgYnjPm/HJwnP/aT/Tb8wA+tYHVdUfRUbLUV2yWTnAYAw7cLbavrbdEE1ZSxtKbrUSwVRM+ntK23drLih7Y9liXkxdYi1z3Cp9AJoeaQdBk4SYe+MGM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1740051482; h=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=UYVR4Thso7YkttRx5HDs7IzBZPwLwHhmg04Df3xboRY=; b=CTOR5Mgm8nWc7aVOeL+BLHSUfHCgCL1IIsdMpGayU/kElmsr/d7GEaxtLuGVbX82FtRDUykx7UC6neEFMlGDfs52UVgBNmJGfcNmmECWbAgJJW8XUFBzbZm20tbNoBn34vLdNGZDqGcWFenqmYcT9BshKl5IIAU2yRHXCou14Wk= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1740051482659497.33324661309973; Thu, 20 Feb 2025 03:38:02 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tl4rs-0004a4-1s; Thu, 20 Feb 2025 06:37:24 -0500 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 1tl4rj-0004ZV-Rl for qemu-devel@nongnu.org; Thu, 20 Feb 2025 06:37:16 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tl4rh-0000rX-IH for qemu-devel@nongnu.org; Thu, 20 Feb 2025 06:37:15 -0500 Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-278-RMMaQG_kPPaPKs2vLSwKAA-1; Thu, 20 Feb 2025 06:37:11 -0500 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-38f42f21f54so374142f8f.1 for ; Thu, 20 Feb 2025 03:37:10 -0800 (PST) Received: from [192.168.10.48] ([176.206.122.109]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38f25913f5asm20685554f8f.52.2025.02.20.03.37.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 03:37:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740051432; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UYVR4Thso7YkttRx5HDs7IzBZPwLwHhmg04Df3xboRY=; b=L1sQmNLAb7fd9qfXzxJ8ELwn4gFYkHHtBLm8VpV46rCNpgiL9dpcuKpoDwcQL2+CEJAR5I fTnzhzUaH3eu2ycXMHS3hSThaCDsM2Qqgagnz7x6mnINi15puNhtwyXM+yIX4UzAmvDrYY RiNmRK2lchaxtNCLZ+HR+Ql26vcaWlo= X-MC-Unique: RMMaQG_kPPaPKs2vLSwKAA-1 X-Mimecast-MFC-AGG-ID: RMMaQG_kPPaPKs2vLSwKAA_1740051430 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740051429; x=1740656229; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UYVR4Thso7YkttRx5HDs7IzBZPwLwHhmg04Df3xboRY=; b=cCOjnbd0aQDGkZQsKJowWo1Nl50YSFEQ9MWXiBKdZJsHzoUekexRdT31JV/4JsI3g9 M5NZlEg7whtFD1s28uk8J0WbSPDI9MVNTnn14tmyLUnVoWovkGUJdCEXrV1qO7ZQnpHm UzZdUEGjw/wjZEarr8fvLgy8YR7YS7OPggOw9rYeG8x9JoI0wS09xywoC+a1vn/0jvMl KsoOMzwu4uOsWnOZGYu24wyiIi5ZVaQc1veL9MVZfBJ8TGxzkbqsMcD1D4LoF7IhhRu/ bTIFy9GIOYzc0puafbTlNd3oNZFOQ6Enos0SCDHj5iSt7WVs+T6gUU76l26HPAG2UdO1 qGyQ== X-Gm-Message-State: AOJu0Yyjc+wHOhXP+NnsPoqka69Cm24fy2jCDOAPapchmTRASIC9/GH1 gfPPoqnMx4rKRuTpfT8EQ+eEkNllgUvOghJjzzVjKpay6Q3Yfh4L05ua3gw5Pfx4fXvwgLuePyY lb8T9fqw0alEZKZJ8CYJPkfYbKxwm3TceivLHsxD7Mt0JfLsUzi4LKbgL27RIFhPqivIjj6Z7Pr QTmRvPNMYm4pRPIpcRgRCFs+Jd4HEr6DnCFaqj8TA= X-Gm-Gg: ASbGncuS7Kehg8bAGxOgdP9g6wjxozXiIbweVV7LZBaLix/TS73YvyEyh1Rttjqex9A lC22ISELU/acWhR//EASfVOScoT4kgdUJaODDFugh3jV+qCZoT5guZkjAeWbcvv4uGPkanyMp5c HE2Tkd0uuRgD6uyqEjYMy7OjlRm053v7osNC3Sr81jaaWuD8GHo7aIVOK+P4RrVrqXflwhl35iW hFY9KJWQWpTFBy+v27DjqeRgTbWgN/8OtEntkuJRIB3yaVl59ABKFOXvIOMAfqQNl4yEoCqrJXZ zn9ul751K4Q= X-Received: by 2002:a05:6000:1887:b0:38f:3c01:fb2c with SMTP id ffacd0b85a97d-38f3c01fe81mr19483788f8f.48.1740051428694; Thu, 20 Feb 2025 03:37:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IEZIr8eN55QWSoczzSYg8M4rFjdf2crzxSrzpju5mABBV1jbJ6ERwG84KePhIip1dVR4LWl7g== X-Received: by 2002:a05:6000:1887:b0:38f:3c01:fb2c with SMTP id ffacd0b85a97d-38f3c01fe81mr19483622f8f.48.1740051426840; Thu, 20 Feb 2025 03:37:06 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: zhao1.liu@intel.com, kwolf@redhat.com Subject: [PATCH v3 2/2] rust: add module to convert between success/-errno and io::Result Date: Thu, 20 Feb 2025 12:36:59 +0100 Message-ID: <20250220113659.863332-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250220113659.863332-1-pbonzini@redhat.com> References: <20250220113659.863332-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.191, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-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 @redhat.com) X-ZM-MESSAGEID: 1740051484639019100 Content-Type: text/plain; charset="utf-8" It is a common convention in QEMU to return a positive value in case of success, and a negated errno value in case of error. Unfortunately, using errno portably in Rust is a bit complicated; on Unix the errno values are supported natively by io::Error, but on Windows they are not; so, use the libc crate. This is a set of utility functions that are used by both chardev and block layer bindings. Signed-off-by: Paolo Bonzini --- docs/devel/rust.rst | 1 + rust/qemu-api/meson.build | 4 + rust/qemu-api/src/assertions.rs | 28 +++ rust/qemu-api/src/errno.rs | 343 ++++++++++++++++++++++++++++++++ rust/qemu-api/src/lib.rs | 1 + rust/qemu-api/src/prelude.rs | 2 + 6 files changed, 379 insertions(+) create mode 100644 rust/qemu-api/src/errno.rs diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst index 90958e5a306..c75dccdbb7c 100644 --- a/docs/devel/rust.rst +++ b/docs/devel/rust.rst @@ -179,6 +179,7 @@ module status ``callbacks`` complete ``cell`` stable ``c_str`` complete +``errno`` complete ``irq`` complete ``memory`` stable ``module`` complete diff --git a/rust/qemu-api/meson.build b/rust/qemu-api/meson.build index 2e9c1078b9b..bcf1cf780f3 100644 --- a/rust/qemu-api/meson.build +++ b/rust/qemu-api/meson.build @@ -2,6 +2,8 @@ _qemu_api_cfg =3D run_command(rustc_args, '--config-headers', config_host_h, '--features', files('Cargo.toml'), capture: true, check: true).stdout().strip().splitlines() =20 +libc_dep =3D dependency('libc-0.2-rs') + # _qemu_api_cfg +=3D ['--cfg', 'feature=3D"allocator"'] if rustc.version().version_compare('>=3D1.77.0') _qemu_api_cfg +=3D ['--cfg', 'has_offset_of'] @@ -22,6 +24,7 @@ _qemu_api_rs =3D static_library( 'src/cell.rs', 'src/chardev.rs', 'src/c_str.rs', + 'src/errno.rs', 'src/irq.rs', 'src/memory.rs', 'src/module.rs', @@ -39,6 +42,7 @@ _qemu_api_rs =3D static_library( override_options: ['rust_std=3D2021', 'build.rust_std=3D2021'], rust_abi: 'rust', rust_args: _qemu_api_cfg, + dependencies: libc_dep, ) =20 rust.test('rust-qemu-api-tests', _qemu_api_rs, diff --git a/rust/qemu-api/src/assertions.rs b/rust/qemu-api/src/assertions= .rs index fa1a18de6fe..104dec39774 100644 --- a/rust/qemu-api/src/assertions.rs +++ b/rust/qemu-api/src/assertions.rs @@ -92,3 +92,31 @@ fn types_must_be_equal(_: T) }; }; } + +/// Assert that an expression matches a pattern. This can also be +/// useful to compare enums that do not implement `Eq`. +/// +/// # Examples +/// +/// ``` +/// # use qemu_api::assert_match; +/// // JoinHandle does not implement `Eq`, therefore the result +/// // does not either. +/// let result: Result, u32> =3D Err(42); +/// assert_match!(result, Err(42)); +/// ``` +#[macro_export] +macro_rules! assert_match { + ($a:expr, $b:pat) =3D> { + assert!( + match $a { + $b =3D> true, + _ =3D> false, + }, + "{} =3D {:?} does not match {}", + stringify!($a), + $a, + stringify!($b) + ); + }; +} diff --git a/rust/qemu-api/src/errno.rs b/rust/qemu-api/src/errno.rs new file mode 100644 index 00000000000..bfd972df951 --- /dev/null +++ b/rust/qemu-api/src/errno.rs @@ -0,0 +1,343 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +//! Utility functions to convert `errno` to and from +//! [`io::Error`]/[`io::Result`] +//! +//! QEMU C functions often have a "positive success/negative `errno`" call= ing +//! convention. This module provides functions to portably convert an int= eger +//! into an [`io::Result`] and back. + +use std::{convert::TryFrom, io, io::ErrorKind}; + +/// An `errno` value that can be converted into an [`io::Error`] +pub struct Errno(pub u16); + +// On Unix, from_raw_os_error takes an errno value and OS errors +// are printed using strerror. On Windows however it takes a +// GetLastError() value; therefore we need to convert errno values +// into io::Error by hand. This is the same mapping that the +// standard library uses to retrieve the kind of OS errors +// (`std::sys::pal::unix::decode_error_kind`). +impl From for ErrorKind { + fn from(value: Errno) -> ErrorKind { + let Errno(errno) =3D value; + match i32::from(errno) { + libc::EPERM | libc::EACCES =3D> ErrorKind::PermissionDenied, + libc::ENOENT =3D> ErrorKind::NotFound, + libc::EINTR =3D> ErrorKind::Interrupted, + x if x =3D=3D libc::EAGAIN || x =3D=3D libc::EWOULDBLOCK =3D> = ErrorKind::WouldBlock, + libc::ENOMEM =3D> ErrorKind::OutOfMemory, + libc::EEXIST =3D> ErrorKind::AlreadyExists, + libc::EINVAL =3D> ErrorKind::InvalidInput, + libc::EPIPE =3D> ErrorKind::BrokenPipe, + libc::EADDRINUSE =3D> ErrorKind::AddrInUse, + libc::EADDRNOTAVAIL =3D> ErrorKind::AddrNotAvailable, + libc::ECONNABORTED =3D> ErrorKind::ConnectionAborted, + libc::ECONNREFUSED =3D> ErrorKind::ConnectionRefused, + libc::ECONNRESET =3D> ErrorKind::ConnectionReset, + libc::ENOTCONN =3D> ErrorKind::NotConnected, + libc::ENOTSUP =3D> ErrorKind::Unsupported, + libc::ETIMEDOUT =3D> ErrorKind::TimedOut, + _ =3D> ErrorKind::Other, + } + } +} + +// This is used on Windows for all io::Errors, but also on Unix if the +// io::Error does not have a raw OS error. This is the reversed +// mapping of the above. +impl From for Errno { + fn from(value: io::ErrorKind) -> Errno { + let errno =3D match value { + // can be both EPERM or EACCES :( pick one + ErrorKind::PermissionDenied =3D> libc::EPERM, + ErrorKind::NotFound =3D> libc::ENOENT, + ErrorKind::Interrupted =3D> libc::EINTR, + ErrorKind::WouldBlock =3D> libc::EAGAIN, + ErrorKind::OutOfMemory =3D> libc::ENOMEM, + ErrorKind::AlreadyExists =3D> libc::EEXIST, + ErrorKind::InvalidInput =3D> libc::EINVAL, + ErrorKind::BrokenPipe =3D> libc::EPIPE, + ErrorKind::AddrInUse =3D> libc::EADDRINUSE, + ErrorKind::AddrNotAvailable =3D> libc::EADDRNOTAVAIL, + ErrorKind::ConnectionAborted =3D> libc::ECONNABORTED, + ErrorKind::ConnectionRefused =3D> libc::ECONNREFUSED, + ErrorKind::ConnectionReset =3D> libc::ECONNRESET, + ErrorKind::NotConnected =3D> libc::ENOTCONN, + ErrorKind::Unsupported =3D> libc::ENOTSUP, + ErrorKind::TimedOut =3D> libc::ETIMEDOUT, + _ =3D> libc::EIO, + }; + Errno(errno as u16) + } +} + +impl From for io::Error { + #[cfg(unix)] + fn from(value: Errno) -> io::Error { + let Errno(errno) =3D value; + io::Error::from_raw_os_error(errno.into()) + } + + #[cfg(windows)] + fn from(value: Errno) -> io::Error { + let error_kind: ErrorKind =3D value.into(); + error_kind.into() + } +} + +impl From for Errno { + fn from(value: io::Error) -> Errno { + if cfg!(unix) { + if let Some(errno) =3D value.raw_os_error() { + return Errno(u16::try_from(errno).unwrap()); + } + } + value.kind().into() + } +} + +/// Internal traits; used to enable [`into_io_result`] and [`into_neg_errn= o`] +/// for the "right" set of types. +mod traits { + use super::Errno; + + /// A signed type that can be converted into an + /// [`io::Result`](std::io::Result) + pub trait GetErrno { + /// Unsigned variant of `Self`, used as the type for the `Ok` case. + type Out; + + /// Return `Ok(self)` if positive, `Err(Errno(-self))` if negative + fn into_errno_result(self) -> Result; + } + + /// A type that can be taken out of an [`io::Result`](std::io::Result)= and + /// converted into "positive success/negative `errno`" convention. + pub trait MergeErrno { + /// Signed variant of `Self`, used as the return type of + /// [`into_neg_errno`](super::into_neg_errno). + type Out: From + std::ops::Neg; + + /// Return `self`, asserting that it is in range + fn map_ok(self) -> Self::Out; + } + + macro_rules! get_errno { + ($t:ty, $out:ty) =3D> { + impl GetErrno for $t { + type Out =3D $out; + fn into_errno_result(self) -> Result { + match self { + 0.. =3D> Ok(self as $out), + -65535..=3D-1 =3D> Err(Errno(-self as u16)), + _ =3D> panic!("{self} is not a negative errno"), + } + } + } + }; + } + + get_errno!(i32, u32); + get_errno!(i64, u64); + get_errno!(isize, usize); + + macro_rules! merge_errno { + ($t:ty, $out:ty) =3D> { + impl MergeErrno for $t { + type Out =3D $out; + fn map_ok(self) -> Self::Out { + self.try_into().unwrap() + } + } + }; + } + + merge_errno!(u8, i32); + merge_errno!(u16, i32); + merge_errno!(u32, i32); + merge_errno!(u64, i64); + + impl MergeErrno for () { + type Out =3D i32; + fn map_ok(self) -> i32 { + 0 + } + } +} + +use traits::{GetErrno, MergeErrno}; + +/// Convert an integer value into a [`io::Result`]. +/// +/// Positive values are turned into an `Ok` result; negative values +/// are interpreted as negated `errno` and turned into an `Err`. +/// +/// ``` +/// # use qemu_api::errno::into_io_result; +/// # use std::io::ErrorKind; +/// let ok =3D into_io_result(1i32).unwrap(); +/// assert_eq!(ok, 1u32); +/// +/// let err =3D into_io_result(-1i32).unwrap_err(); // -EPERM +/// assert_eq!(err.kind(), ErrorKind::PermissionDenied); +/// ``` +/// +/// # Panics +/// +/// Since the result is an unsigned integer, negative values must +/// be close to 0; values that are too far away are considered +/// likely overflows and will panic: +/// +/// ```should_panic +/// # use qemu_api::errno::into_io_result; +/// # #[allow(dead_code)] +/// let err =3D into_io_result(-0x1234_5678i32); // panic +/// ``` +pub fn into_io_result(value: T) -> io::Result { + value.into_errno_result().map_err(Into::into) +} + +/// Convert a [`Result`] into an integer value, using negative `errno` +/// values to report errors. +/// +/// ``` +/// # use qemu_api::errno::into_neg_errno; +/// # use std::io::{self, ErrorKind}; +/// let ok: io::Result<()> =3D Ok(()); +/// assert_eq!(into_neg_errno(ok), 0); +/// +/// let err: io::Result<()> =3D Err(ErrorKind::InvalidInput.into()); +/// assert_eq!(into_neg_errno(err), -22); // -EINVAL +/// ``` +/// +/// Since this module also provides the ability to convert [`io::Error`] +/// to an `errno` value, [`io::Result`] is the most commonly used type +/// for the argument of this function: +/// +/// # Panics +/// +/// Since the result is a signed integer, integer `Ok` values must remain +/// positive: +/// +/// ```should_panic +/// # use qemu_api::errno::into_neg_errno; +/// # use std::io; +/// let err: io::Result =3D Ok(0x8899_AABB); +/// into_neg_errno(err) // panic +/// # ; +/// ``` +pub fn into_neg_errno>(value: Result) = -> T::Out { + match value { + Ok(x) =3D> x.map_ok(), + Err(err) =3D> -T::Out::from(err.into().0), + } +} + +#[cfg(test)] +mod tests { + use std::io::ErrorKind; + + use super::*; + use crate::assert_match; + + #[test] + pub fn test_from_u8() { + let ok: io::Result<_> =3D Ok(42u8); + assert_eq!(into_neg_errno(ok), 42); + + let err: io::Result =3D Err(io::ErrorKind::PermissionDenied.in= to()); + assert_eq!(into_neg_errno(err), -1); + + if cfg!(unix) { + let os_err: io::Result =3D Err(io::Error::from_raw_os_erro= r(10)); + assert_eq!(into_neg_errno(os_err), -10); + } + } + + #[test] + pub fn test_from_u16() { + let ok: io::Result<_> =3D Ok(1234u16); + assert_eq!(into_neg_errno(ok), 1234); + + let err: io::Result =3D Err(io::ErrorKind::PermissionDenied.i= nto()); + assert_eq!(into_neg_errno(err), -1); + + if cfg!(unix) { + let os_err: io::Result =3D Err(io::Error::from_raw_os_err= or(10)); + assert_eq!(into_neg_errno(os_err), -10); + } + } + + #[test] + pub fn test_i32() { + assert_match!(into_io_result(1234i32), Ok(1234)); + + let err =3D into_io_result(-1i32).unwrap_err(); + #[cfg(unix)] + assert_match!(err.raw_os_error(), Some(1)); + assert_match!(err.kind(), ErrorKind::PermissionDenied); + } + + #[test] + pub fn test_from_u32() { + let ok: io::Result<_> =3D Ok(1234u32); + assert_eq!(into_neg_errno(ok), 1234); + + let err: io::Result =3D Err(io::ErrorKind::PermissionDenied.i= nto()); + assert_eq!(into_neg_errno(err), -1); + + if cfg!(unix) { + let os_err: io::Result =3D Err(io::Error::from_raw_os_err= or(10)); + assert_eq!(into_neg_errno(os_err), -10); + } + } + + #[test] + pub fn test_i64() { + assert_match!(into_io_result(1234i64), Ok(1234)); + + let err =3D into_io_result(-22i64).unwrap_err(); + #[cfg(unix)] + assert_match!(err.raw_os_error(), Some(22)); + assert_match!(err.kind(), ErrorKind::InvalidInput); + } + + #[test] + pub fn test_from_u64() { + let ok: io::Result<_> =3D Ok(1234u64); + assert_eq!(into_neg_errno(ok), 1234); + + let err: io::Result =3D Err(io::ErrorKind::InvalidInput.into(= )); + assert_eq!(into_neg_errno(err), -22); + + if cfg!(unix) { + let os_err: io::Result =3D Err(io::Error::from_raw_os_err= or(6)); + assert_eq!(into_neg_errno(os_err), -6); + } + } + + #[test] + pub fn test_isize() { + assert_match!(into_io_result(1234isize), Ok(1234)); + + let err =3D into_io_result(-4isize).unwrap_err(); + #[cfg(unix)] + assert_match!(err.raw_os_error(), Some(4)); + assert_match!(err.kind(), ErrorKind::Interrupted); + } + + #[test] + pub fn test_from_unit() { + let ok: io::Result<_> =3D Ok(()); + assert_eq!(into_neg_errno(ok), 0); + + let err: io::Result<()> =3D Err(io::ErrorKind::OutOfMemory.into()); + assert_eq!(into_neg_errno(err), -12); + + if cfg!(unix) { + let os_err: io::Result<()> =3D Err(io::Error::from_raw_os_erro= r(2)); + assert_eq!(into_neg_errno(os_err), -2); + } + } +} diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs index ed1a8f9a2b4..05f38b51d30 100644 --- a/rust/qemu-api/src/lib.rs +++ b/rust/qemu-api/src/lib.rs @@ -19,6 +19,7 @@ pub mod callbacks; pub mod cell; pub mod chardev; +pub mod errno; pub mod irq; pub mod memory; pub mod module; diff --git a/rust/qemu-api/src/prelude.rs b/rust/qemu-api/src/prelude.rs index fbf0ee23e0b..634acf37a85 100644 --- a/rust/qemu-api/src/prelude.rs +++ b/rust/qemu-api/src/prelude.rs @@ -9,6 +9,8 @@ pub use crate::cell::BqlCell; pub use crate::cell::BqlRefCell; =20 +pub use crate::errno; + pub use crate::qdev::DeviceMethods; =20 pub use crate::qom::InterfaceType; --=20 2.48.1