rust: bindings for Error
Subsystems (2)
cover rust: bindings for Error
This was one of the first bindings that I wrote in
https://lore.kernel.org/qemu-devel/[email protected]/T/,
but there were a few obstacles to clear before including them:
1) cleaning up the *foreign*() functions (patches 3-4 from the above series) or deciding not to use them. The functions are now part of their own crate, available on crates.io and imported in this series. For this series I am including them, but if people prefer to stick to "normal" Rust FFI I'm open to that as well.
2) cleaning up the "cause" member. This series uses the anyhow crate instead of Box<dyn std::error::Error>.
3) cleaning up &error_abort, because Rust does not have a NULL-terminated filename and does not have an equivalent to __func__ at all. The preliminary code did not pass file and line at all, while now I am using #[track_caller] and making the C code collaborate with Rust (see patches 5-6 of the series).
The final driver for fixing all this was to enable the removal of BqlCell<> from HPETState::num_timers.
Integration tests for the new class are still missing.
Markus... this is your first exposure to Rust in QEMU. You may find some information at https://www.qemu.org/docs/master/devel/rust.html but just to be clear, there are no stupid questions, only stupid answers. :)
Paolo
Paolo Bonzini (12):
rust: make declaration of dependent crates more consistent
subprojects: add the anyhow crate
subprojects: add the foreign crate
util/error: expose Error definition to Rust code util/error: allow non-NUL-terminated err->src util/error: make func optional
qemu-api: add bindings to Error
rust: qdev: support returning errors from realize rust/hpet: change timer of num_timers to usize
hpet: return errors from realize if properties are incorrect rust/hpet: return errors from realize if properties are incorrect
Zhao Liu (12):
rust/hpet: Drop BqlCell wrapper for num_timers
include/qapi/error-internal.h | 27 ++ rust/wrapper.h | 1 + hw/timer/hpet.c | 15 +- util/error.c | 24 +- rust/Cargo.lock | 17 ++ rust/Cargo.toml | 1 + rust/hw/char/pl011/meson.build | 12 +- rust/hw/char/pl011/src/device.rs | 5 +- rust/hw/timer/hpet/src/fw_cfg.rs | 7 +- rust/hw/timer/hpet/src/hpet.rs | 62 ++-- rust/meson.build | 20 ++ rust/qemu-api-macros/meson.build | 14 +- rust/qemu-api/Cargo.toml | 2 + rust/qemu-api/meson.build | 5 +- rust/qemu-api/src/error.rs | 273 ++++++++++++++++++ rust/qemu-api/src/lib.rs | 3 + rust/qemu-api/src/qdev.rs | 10 +- subprojects/.gitignore | 2 + subprojects/anyhow-1.0-rs.wrap | 7 + subprojects/foreign-0.2-rs.wrap | 7 + .../packagefiles/anyhow-1.0-rs/meson.build | 33 +++ .../packagefiles/foreign-0.2-rs/meson.build | 26 ++ 22 files changed, 488 insertions(+), 85 deletions(-) create mode 100644 include/qapi/error-internal.h create mode 100644 rust/qemu-api/src/error.rs create mode 100644 subprojects/anyhow-1.0-rs.wrap create mode 100644 subprojects/foreign-0.2-rs.wrap create mode 100644 subprojects/packagefiles/anyhow-1.0-rs/meson.build create mode 100644 subprojects/packagefiles/foreign-0.2-rs/meson.build
Signature · 1 line
2.49.0
PATCH 1/12 rust: make declaration of dependent crates more consistent
Crates like "bilge" and "libc" can be shared by more than one directory, so declare them directly in rust/meson.build. While at it, make their variable names end with "_rs" and always add a subproject() statement (as that pinpoints the error better if the subproject is missing and cannot be downloaded).
--- rust/hw/char/pl011/meson.build | 12 +++--------- rust/meson.build | 16 ++++++++++++++++ rust/qemu-api-macros/meson.build | 14 +++----------- rust/qemu-api/meson.build | 4 +--- 4 files changed, 23 insertions(+), 23 deletions(-)
rust/hw/char/pl011/meson.build +3 -9
@@ -1,17 +1,11 @@ -subproject('bilge-0.2-rs', required: true) -subproject('bilge-impl-0.2-rs', required: true) - -bilge_dep = dependency('bilge-0.2-rs') -bilge_impl_dep = dependency('bilge-impl-0.2-rs') - _libpl011_rs = static_library( 'pl011', files('src/lib.rs'), override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_abi: 'rust', dependencies: [ - bilge_dep, - bilge_impl_dep, + bilge_rs, + bilge_impl_rs, qemu_api, qemu_api_macros, ],
@@ -21,6 +15,6 @@ rust_devices_ss.add(when: 'CONFIG_X_PL011_RUST', if_true: [declare_dependency( link_whole: [_libpl011_rs], # Putting proc macro crates in `dependencies` is necessary for Meson to find # them when compiling the root per-target static rust lib. - dependencies: [bilge_impl_dep, qemu_api_macros], + dependencies: [bilge_impl_rs, qemu_api_macros], variables: {'crate': 'pl011'}, )])
rust/meson.build +16 -0
@@ -1,3 +1,19 @@ +subproject('bilge-0.2-rs', required: true) +subproject('bilge-impl-0.2-rs', required: true) +subproject('libc-0.2-rs', required: true) + +bilge_rs = dependency('bilge-0.2-rs') +bilge_impl_rs = dependency('bilge-impl-0.2-rs') +libc_rs = dependency('libc-0.2-rs') + +subproject('proc-macro2-1-rs', required: true) +subproject('quote-1-rs', required: true) +subproject('syn-2-rs', required: true) + +quote_rs_native = dependency('quote-1-rs', native: true) +syn_rs_native = dependency('syn-2-rs', native: true) +proc_macro2_rs_native = dependency('proc-macro2-1-rs', native: true) + subdir('qemu-api-macros') subdir('qemu-api')
rust/qemu-api-macros/meson.build +3 -11
@@ -1,11 +1,3 @@ -subproject('proc-macro2-1-rs', required: true) -subproject('quote-1-rs', required: true) -subproject('syn-2-rs', required: true) - -quote_dep = dependency('quote-1-rs', native: true) -syn_dep = dependency('syn-2-rs', native: true) -proc_macro2_dep = dependency('proc-macro2-1-rs', native: true) - _qemu_api_macros_rs = rust.proc_macro( 'qemu_api_macros', files('src/lib.rs'),
@@ -16,9 +8,9 @@ _qemu_api_macros_rs = rust.proc_macro( '--cfg', 'feature="proc-macro"', ], dependencies: [ - proc_macro2_dep, - quote_dep, - syn_dep, + proc_macro2_rs_native, + quote_rs_native, + syn_rs_native, ], )
rust/qemu-api/meson.build +1 -3
@@ -2,8 +2,6 @@ _qemu_api_cfg = run_command(rustc_args, '--config-headers', config_host_h, '--features', files('Cargo.toml'), capture: true, check: true).stdout().strip().splitlines() -libc_dep = dependency('libc-0.2-rs') - # _qemu_api_cfg += ['--cfg', 'feature="allocator"'] if get_option('debug_mutex') _qemu_api_cfg += ['--cfg', 'feature="debug_cell"']
@@ -37,7 +35,7 @@ _qemu_api_rs = static_library( override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_abi: 'rust', rust_args: _qemu_api_cfg, - dependencies: [libc_dep, qemu_api_macros], + dependencies: [libc_rs, qemu_api_macros], ) rust.test('rust-qemu-api-tests', _qemu_api_rs,
Signature · 1 line
2.49.0
Re: [PATCH 01/12] rust: make declaration of dependent crates more consistent
Quoted 12 lines · Paolo Bonzini
Date: Mon, 26 May 2025 16:24:44 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 01/12] rust: make declaration of dependent crates more
consistent
X-Mailer: git-send-email 2.49.0Crates like "bilge" and "libc" can be shared by more than one directory, so declare them directly in
rust/meson.build. While at it, make their variable names end with "_rs" and always add asubproject()statement (as that pinpoints the error better if the subproject is missing and cannot be downloaded).
Quoted 7 lines
Signed-off-by Paolo Bonzini <[email protected]>--- rust/hw/char/pl011/meson.build | 12 +++--------- rust/meson.build | 16 ++++++++++++++++ rust/qemu-api-macros/meson.build | 14 +++----------- rust/qemu-api/meson.build | 4 +--- 4 files changed, 23 insertions(+), 23 deletions(-)
PATCH 2/12 subprojects: add the anyhow crate
In reply to Paolo BonziniThis is a standard replacement for Box<dyn Error> which is more efficient (it only occcupies one word) and provides a backtrace of the error. This could be plumbed into &error_abort in the future.
--- rust/meson.build | 2 ++ subprojects/.gitignore | 1 + subprojects/anyhow-1.0-rs.wrap | 7 ++++ .../packagefiles/anyhow-1.0-rs/meson.build | 33 +++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 subprojects/anyhow-1.0-rs.wrap create mode 100644 subprojects/packagefiles/anyhow-1.0-rs/meson.build
rust/meson.build +2 -0
@@ -1,7 +1,9 @@ +subproject('anyhow-1.0-rs', required: true) subproject('bilge-0.2-rs', required: true) subproject('bilge-impl-0.2-rs', required: true) subproject('libc-0.2-rs', required: true) +anyhow_rs = dependency('anyhow-1.0-rs') bilge_rs = dependency('bilge-0.2-rs') bilge_impl_rs = dependency('bilge-impl-0.2-rs') libc_rs = dependency('libc-0.2-rs')
subprojects/.gitignore +1 -0
@@ -6,6 +6,7 @@ /keycodemapdb /libvfio-user /slirp +/anyhow-1.0.98 /arbitrary-int-1.2.7 /bilge-0.2.0 /bilge-impl-0.2.0
subprojects/anyhow-1.0-rs.wrap new file +7 -0
@@ -0,0 +1,7 @@ +[wrap-file] +directory = anyhow-1.0.98 +source_url = https://crates.io/api/v1/crates/anyhow/1.0.98/download +source_filename = anyhow-1.0.98.tar.gz +source_hash = e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487 +#method = cargo +patch_directory = anyhow-1.0-rs
subprojects/packagefiles/anyhow-1.0-rs/meson.build new file +33 -0
@@ -0,0 +1,33 @@ +project('anyhow-1.0-rs', 'rust', + meson_version: '>=1.5.0', + version: '1.0.98', + license: 'MIT OR Apache-2.0', + default_options: []) + +rustc = meson.get_compiler('rust') + +rust_args = ['--cap-lints', 'allow'] +rust_args += ['--cfg', 'feature="std"'] +if rustc.version().version_compare('<1.65.0') + error('rustc version ' + rustc.version() + ' is unsupported. Please upgrade to at least 1.65.0') +endif +rust_args += [ '--cfg', 'std_backtrace' ] # >= 1.65.0 +if rustc.version().version_compare('<1.81.0') + rust_args += [ '--cfg', 'anyhow_no_core_error' ] +endif + +_anyhow_rs = static_library( + 'anyhow', + files('src/lib.rs'), + gnu_symbol_visibility: 'hidden', + override_options: ['rust_std=2018', 'build.rust_std=2018'], + rust_abi: 'rust', + rust_args: rust_args, + dependencies: [], +) + +anyhow_dep = declare_dependency( + link_with: _anyhow_rs, +) + +meson.override_dependency('anyhow-1.0-rs', anyhow_dep)
Signature · 1 line
2.49.0
Re: [PATCH 02/12] subprojects: add the anyhow crate
Quoted 10 lines · Paolo Bonzini
Date: Mon, 26 May 2025 16:24:45 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 02/12] subprojects: add the anyhow crate
X-Mailer: git-send-email 2.49.0This is a standard replacement for Box<dyn Error> which is more efficient (it only occcupies one word) and provides a backtrace of the error. This could be plumbed into
&error_abortin the future.Signed-off-by Paolo Bonzini <[email protected]>
Quoted 8 lines
--- rust/meson.build | 2 ++ subprojects/.gitignore | 1 + subprojects/anyhow-1.0-rs.wrap | 7 ++++ .../packagefiles/anyhow-1.0-rs/meson.build | 33 +++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 subprojects/anyhow-1.0-rs.wrap create mode 100644 subprojects/packagefiles/anyhow-1.0-rs/meson.build
Missed to change scripts/archive-source.sh & scripts/make-release?
Otherwise,
Re: [PATCH 02/12] subprojects: add the anyhow crate
Quoted 20 lines · Zhao Liu
Quoted 10 lines · Paolo Bonzini
Date: Mon, 26 May 2025 16:24:45 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 02/12] subprojects: add the anyhow crate
X-Mailer: git-send-email 2.49.0This is a standard replacement for Box<dyn Error> which is more efficient (it only occcupies one word) and provides a backtrace of the error. This could be plumbed into
&error_abortin the future.Signed-off-by Paolo Bonzini <[email protected]>Quoted 8 lines
--- rust/meson.build | 2 ++ subprojects/.gitignore | 1 + subprojects/anyhow-1.0-rs.wrap | 7 ++++ .../packagefiles/anyhow-1.0-rs/meson.build | 33 +++++++++++++++++++ 4 files changed, 43 insertions(+)create mode 100644 subprojects/anyhow-1.0-rs.wrap
create mode 100644 subprojects/packagefiles/anyhow-1.0-rs/meson.build
Quoted 1 line
Missed to change
scripts/archive-source.sh&scripts/make-release?
Yes, also the wrap name should be anyhow-1-rs because only the "0" version gets an extra component.
Paolo
PATCH 3/12 subprojects: add the foreign crate
In reply to Paolo BonziniThis is a cleaned up and separated version of the patches at
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
Its first user will be the Error bindings; for example a QEMU Error ** can be converted to a Rust Option using
unsafe { Option::<Error>::from_foreign(c_error) }--- subprojects/.gitignore | 1 + subprojects/foreign-0.2-rs.wrap | 7 +++++ .../packagefiles/foreign-0.2-rs/meson.build | 26 +++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 subprojects/foreign-0.2-rs.wrap create mode 100644 subprojects/packagefiles/foreign-0.2-rs/meson.build
subprojects/.gitignore +1 -0
@@ -11,6 +11,7 @@ /bilge-0.2.0 /bilge-impl-0.2.0 /either-1.12.0 +/foreign-0.2.0 /itertools-0.11.0 /libc-0.2.162 /proc-macro-error-1.0.4
subprojects/foreign-0.2-rs.wrap new file +7 -0
@@ -0,0 +1,7 @@ +[wrap-file] +directory = foreign-0.2.0 +source_url = https://crates.io/api/v1/crates/foreign/0.2.0/download +source_filename = foreign-0.2.0.tar.gz +source_hash = 37dd09e47ea8fd592a333f59fc52b894a97fe966ae9c6b7ef21ae38de6043462 +#method = cargo +patch_directory = foreign-0.2-rs
subprojects/packagefiles/foreign-0.2-rs/meson.build new file +26 -0
@@ -0,0 +1,26 @@ +project('foreign-0.2-rs', 'rust', + meson_version: '>=1.5.0', + version: '0.2.0', + license: 'MIT OR Apache-2.0', + default_options: []) + +subproject('libc-0.2-rs', required: true) +libc_rs = dependency('libc-0.2-rs') + +_foreign_rs = static_library( + 'foreign', + files('src/lib.rs'), + gnu_symbol_visibility: 'hidden', + override_options: ['rust_std=2021', 'build.rust_std=2021'], + rust_abi: 'rust', + rust_args: [ + '--cap-lints', 'allow', + ], + dependencies: [libc_rs], +) + +foreign_dep = declare_dependency( + link_with: _foreign_rs, +) + +meson.override_dependency('foreign-0.2-rs', foreign_dep)
Signature · 1 line
2.49.0
Re: [PATCH 03/12] subprojects: add the foreign crate
Quoted 12 lines · Paolo Bonzini
Date: Mon, 26 May 2025 16:24:46 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 03/12] subprojects: add the foreign crate
X-Mailer: git-send-email 2.49.0This is a cleaned up and separated version of the patches at
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/Its first user will be the Error bindings; for example a QEMU Error ** can be converted to a Rust Option using
Quoted 6 lines
unsafe { Option::<Error>::from_foreign(c_error) }Signed-off-by Paolo Bonzini <[email protected]>--- subprojects/.gitignore | 1 + subprojects/foreign-0.2-rs.wrap | 7 +++++
It seems you've already released v0.3.0.
Quoted 4 lines
.../packagefiles/foreign-0.2-rs/meson.build | 26 +++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 subprojects/foreign-0.2-rs.wrap create mode 100644 subprojects/packagefiles/foreign-0.2-rs/meson.build
I went through foreign crate and it was really helpful (and there are also missing changes as anyhow crate patch). With nits fixed,
Maybe off topic, it seems the Owned<T> wrapper is very similar to OwnedPointer<T>. Is it possible to integrate the two? i.e., build Owned<T: ObjectType> based on OwnedPointer<T: FreeForeign>?
Thanks,
Zhao
PATCH 4/12 util/error: expose Error definition to Rust code
In reply to Paolo BonziniThis is used to preserve the file and line in a roundtrip from C Error to Rust and back to C.
--- include/qapi/error-internal.h | 26 ++++++++++++++++++++++++++ rust/wrapper.h | 1 + util/error.c | 10 +--------- 3 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 include/qapi/error-internal.h
include/qapi/error-internal.h new file +26 -0
@@ -0,0 +1,26 @@ +/* +* QEMU Error Objects - struct definition +* +* Copyright IBM, Corp. 2011 +* Copyright (C) 2011-2015 Red Hat, Inc. +* +* Authors: +* Anthony Liguori <[email protected]> +* Markus Armbruster <[email protected]>, +* +* This work is licensed under the terms of the GNU LGPL, version 2. See +* the COPYING.LIB file in the top-level directory. +*/ + +#ifndef QAPI_ERROR_INTERNAL_H + +struct Error +{ +char *msg; +ErrorClass err_class; +const char *src, *func; +int line; +GString *hint; +}; + +#endif
rust/wrapper.h +1 -0
@@ -60,6 +60,7 @@ typedef enum memory_order {#include "hw/qdev-properties-system.h" #include "hw/irq.h" #include "qapi/error.h" +#include "qapi/error-internal.h" #include "migration/vmstate.h" #include "chardev/char-serial.h" #include "exec/memattrs.h"
util/error.c +1 -9
@@ -15,15 +15,7 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "qemu/error-report.h" - -struct Error -{ -char *msg; -ErrorClass err_class; -const char *src, *func; -int line; -GString *hint; -}; +#include "qapi/error-internal.h" Error *error_abort;Error *error_fatal;
Signature · 1 line
2.49.0
Re: [PATCH 04/12] util/error: expose Error definition to Rust code
Quoted 63 lines · Paolo Bonzini
This is used to preserve the file and line in a roundtrip from C Error to Rust and back to C.
Signed-off-by Paolo Bonzini <[email protected]>--- include/qapi/error-internal.h | 26 ++++++++++++++++++++++++++ rust/wrapper.h | 1 + util/error.c | 10 +--------- 3 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 include/qapi/error-internal.hinclude/qapi/error-internal.h new file +26
-0@@ -0,0 +1,26 @@ +/* +* QEMU Error Objects - struct definition +* +* Copyright IBM, Corp. 2011 +* Copyright (C) 2011-2015 Red Hat, Inc. +* +* Authors: +* Anthony Liguori <[email protected]> +* Markus Armbruster <[email protected]>, +* +* This work is licensed under the terms of the GNU LGPL, version 2. See +* the COPYING.LIB file in the top-level directory. +*/ + +#ifndef QAPI_ERROR_INTERNAL_H + +struct Error +{ +char *msg; +ErrorClass err_class; +const char *src, *func; +int line; +GString *hint; +}; + +#endif rust/wrapper.h +1
-0@@ -60,6 +60,7 @@ typedef enum memory_order {#include "hw/qdev-properties-system.h" #include "hw/irq.h" #include "qapi/error.h" +#include "qapi/error-internal.h" #include "migration/vmstate.h" #include "chardev/char-serial.h" #include "exec/memattrs.h" util/error.c +0
-0@@ -15,15 +15,7 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "qemu/error-report.h"
util/error.c line 18
Quoted 10 lines
util/error.c +1
-9- -struct Error -{ -char *msg; -ErrorClass err_class; -const char *src, *func; -int line; -GString *hint; -}; +#include "qapi/error-internal.h"
Please move this up to keep the #include sorted (except for qemu/osdep.h, which is special).
util/error.c line 21
Quoted 3 lines
util/error.c +0
-0Error *error_abort;Error *error_fatal;
Since error.h is always included when error-internal.h is, we could make error-internal.h include error.h. Matter of taste, up to you.
PATCH 5/12 util/error: allow non-NUL-terminated err->src
In reply to Paolo BonziniRust makes the current file available as a statically-allocated string, but without a NUL terminator. Allow this by storing an optional maximum length in the Error.
Note that for portability I am not relying on fprintf's precision specifier not accessing memory beyond what will be printed.
--- include/qapi/error-internal.h | 1 + util/error.c | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-)
include/qapi/error-internal.h +1 -0
@@ -19,6 +19,7 @@ struct Errorchar *msg;ErrorClass err_class;const char *src, *func; +ssize_t src_len;int line;GString *hint; };
util/error.c +7 -1
@@ -24,8 +24,13 @@ Error *error_warn;static void error_handle (Error **errp,Error *err) {if (errp == &error_abort) { +const char *src = err->src; +if (err->src_len >=0 ) { +/* No need to free it, the program will abort very soon... */ + src =g_strndup (err->src, err->src_len); + }fprintf (stderr,"Unexpected error in %s() at %s:%d:\n" , - err->func, err->src, err->line); + err->func, src, err->line);error_report ("%s" ,error_get_pretty (err));if (err->hint) {error_printf ("%s" , err->hint->str);
@@ -67,6 +72,7 @@ static void error_setv(Error **errp,g_free (msg); } err->err_class = err_class; + err->src_len =-1 ; err->src = src; err->line = line; err->func = func;
Signature · 1 line
2.49.0
Re: [PATCH 05/12] util/error: allow non-NUL-terminated err->src
Quoted 6 lines · Paolo Bonzini
Rust makes the current file available as a statically-allocated string, but without a NUL terminator. Allow this by storing an optional maximum length in the Error.
Note that for portability I am not relying on fprintf's precision specifier not accessing memory beyond what will be printed.
Can you elaborate on the portability problem? I figure ...
Quoted 24 lines
Signed-off-by Paolo Bonzini <[email protected]>--- include/qapi/error-internal.h | 1 + util/error.c | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-)include/qapi/error-internal.h +1
-0@@ -19,6 +19,7 @@ struct Errorchar *msg;ErrorClass err_class;const char *src, *func; +ssize_t src_len;int line;GString *hint; };util/error.c +0
-0@@ -24,8 +24,13 @@ Error *error_warn;static void error_handle (Error **errp,Error *err)
util/error.c Error *error_warn; · line 33
Quoted 10 lines
util/error.c +6
-1{if (errp == &error_abort) { +const char *src = err->src; +if (err->src_len >=0 ) { +/* No need to free it, the program will abort very soon... */ + src =g_strndup (err->src, err->src_len); + }fprintf (stderr,"Unexpected error in %s() at %s:%d:\n" , - err->func, err->src, err->line); + err->func, src, err->line);
... you're avoiding the simpler
fprintf(stderr, "Unexpected error in %s() at %.*s:%d:\n",
err->func, err->src_len, err->src, err->line);because of it.
(@src_len needs to be int then, and its default value below INT_MAX)
Quoted 11 lines
util/error.c +1
-0error_report ("%s" ,error_get_pretty (err));if (err->hint) {error_printf ("%s" , err->hint->str);@@ -67,6 +72,7 @@ static void error_setv(Error **errp,g_free (msg); } err->err_class = err_class; + err->src_len =-1 ; err->src = src; err->line = line; err->func = func;
Re: [PATCH 05/12] util/error: allow non-NUL-terminated err->src
Quoted 22 lines · Markus Armbruster
Quoted 6 lines · Paolo Bonzini
Rust makes the current file available as a statically-allocated string, but without a NUL terminator. Allow this by storing an optional maximum length in the Error.
Note that for portability I am not relying on fprintf's precision specifier not accessing memory beyond what will be printed.
Can you elaborate on the portability problem? I figure ...
util/error.c Error *error_warn; · line 33
Quoted 10 lines
{ if (errp == &error_abort) {util/error.c +6
-1+const char *src = err->src; +if (err->src_len >=0 ) { +/* No need to free it, the program will abort very soon... */ + src =g_strndup (err->src, err->src_len); + }fprintf (stderr,"Unexpected error in %s() at %s:%d:\n" , - err->func, err->src, err->line); + err->func, src, err->line);
Quoted 6 lines
... you're avoiding the simpler
fprintf(stderr, "Unexpected error in %s() at %.*s:%d:\n", err->func, err->src_len, err->src, err->line);because of it.
I couldn't find anything that says %s is allowed to not be NUL-terminated if a precision is given. That is, whether something like this:
char foo[] = {'H', 'e', 'l', 'l', 'o'};
printf("%.5s\n", foo);is guaranteed to work.
This is opposed to:
1) strnlen (https://pubs.opengroup.org/onlinepubs/9699919799/functions/strnlen.html), which is guaranteed to examine no more than the number of bytes given by the second character;
2) strndup, for which I found at least a clarification at https://www.austingroupbugs.net/view.php?id=1397.
3) g_strndup, which guarantees that the allocated block is of length n+1 and padded with NULs (though in the case above there will be just one NUL anyway)
And also, for strndup/g_strndup it would be quite asinine to implement it using some kind of min(strlen(s), n) but for printf the complexity is greater so you never know. I erred on the side of caution because avoiding an allocation before an abort() isn't particularly interesting.
Paolo
Re: [PATCH 05/12] util/error: allow non-NUL-terminated err->src
Quoted 26 lines · Paolo Bonzini
Quoted 20 lines · Markus Armbruster
Quoted 6 lines · Paolo Bonzini
Rust makes the current file available as a statically-allocated string, but without a NUL terminator. Allow this by storing an optional maximum length in the Error.
Note that for portability I am not relying on fprintf's precision specifier not accessing memory beyond what will be printed.
Can you elaborate on the portability problem? I figure ...
util/error.c Error *error_warn; · line 33
Quoted 10 lines
{ if (errp == &error_abort) {util/error.c +6
-1+const char *src = err->src; +if (err->src_len >=0 ) { +/* No need to free it, the program will abort very soon... */ + src =g_strndup (err->src, err->src_len); + }fprintf (stderr,"Unexpected error in %s() at %s:%d:\n" , - err->func, err->src, err->line); + err->func, src, err->line);Quoted 4 lines
... you're avoiding the simpler
fprintf(stderr, "Unexpected error in %s() at %.*s:%d:\n", err->func, err->src_len, err->src, err->line);because of it.
Quoted 6 lines
I couldn't find anything that says %s is allowed to not be NUL-terminated if a precision is given. That is, whether something like this:
char foo[] = {'H', 'e', 'l', 'l', 'o'}; printf("%.5s\n", foo);is guaranteed to work.
From ISO/IEC 9899:1999 §7.19.6.1 "The fprintf function":
[#8] The conversion specifiers and their meanings are:
[...]
s If no l length modifier is present, the argument
shall be a pointer to the initial element of an array of character type.237) Characters from the array are written up to (but not including) the
terminating null character. If the precision is
specified, no more than that many bytes are written.
--> If the precision is not specified or is greater than --> the size of the array, the array shall contain a --> null character.
____________________
237No special provisions are made for multibyte characters.
This clearly implies that the string need not be null-terminated when a suitable precision is specified. Which it is here.
Quoted 9 lines
This is opposed to:
1) strnlen (https://pubs.opengroup.org/onlinepubs/9699919799/functions/strnlen.html), which is guaranteed to examine no more than the number of bytes given by the second character;
2) strndup, for which I found at least a clarification at https://www.austingroupbugs.net/view.php?id=1397.
3)
g_strndup, which guarantees that the allocated block is of length n+1 and padded with NULs (though in the case above there will be just one NUL anyway)And also, for strndup/g_strndup it would be quite asinine to implement it using some kind of
min(strlen(s), n)but for printf the complexity is greater so you never know. I erred on the side of caution because avoiding an allocation before anabort()isn't particularly interesting.
Keeping the code simple is always interesting, though :)
PATCH 6/12 util/error: make func optional
In reply to Paolo BonziniThe function name is not available in Rust, so make it optional.
--- util/error.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
util/error.c +6 -2
@@ -29,8 +29,12 @@ static void error_handle(Error **errp, Error *err)/* No need to free it, the program will abort very soon... */ src =g_strndup (err->src, err->src_len); } -fprintf (stderr,"Unexpected error in %s() at %s:%d:\n" , - err->func, src, err->line); +if (err->func) { +fprintf (stderr,"Unexpected error in %s() at %s:%d:\n" , + err->func, src, err->line); + }else { +fprintf (stderr,"Unexpected error at %s:%d:\n" , src, err->line); + }error_report ("%s" ,error_get_pretty (err));if (err->hint) {error_printf ("%s" , err->hint->str);
Signature · 1 line
2.49.0
Re: [PATCH 06/12] util/error: make func optional
Quoted 11 lines · Paolo Bonzini
Date: Mon, 26 May 2025 16:24:49 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 06/12] util/error: make func optional
X-Mailer: git-send-email 2.49.0The function name is not available in Rust, so make it optional.
Signed-off-by Paolo Bonzini <[email protected]>--- util/error.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
panic::Location does not provide function name information. Although there are macros that could print function names [*] (as I'm sure you've noticed :) ), that way - printing the information based on some macros - would definitely require some wrapping or modification of Err().
Comparing with that, current implementation looks better in general.
[*]: https://stackoverflow.com/a/63904992/24336517
PATCH 7/12 qemu-api: add bindings to Error
In reply to Paolo BonziniProvide an implementation of std::error::Error that bridges the Rust anyhow::Error and std::panic::Location types with QEMU's Error*. It also has several utility methods, analogous to error_propagate(), that convert a Result into a return value + Error** pair.
--- rust/Cargo.lock | 17 +++ rust/Cargo.toml | 1 + rust/meson.build | 2 + rust/qemu-api/Cargo.toml | 2 + rust/qemu-api/meson.build | 3 +- rust/qemu-api/src/error.rs | 273 +++++++++++++++++++++++++++++++++++++ rust/qemu-api/src/lib.rs | 3 + 7 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 rust/qemu-api/src/error.rs
rust/Cargo.lock +17 -0
@@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anyhow" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" + [[package]] name = "arbitrary-int" version = "1.2.7"
@@ -37,6 +43,15 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +[[package]] +name = "foreign" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37dd09e47ea8fd592a333f59fc52b894a97fe966ae9c6b7ef21ae38de6043462" +dependencies = [ + "libc", +] + [[package]] name = "hpet" version = "0.1.0"
@@ -106,6 +121,8 @@ dependencies = [ name = "qemu_api" version = "0.1.0" dependencies = [ + "anyhow", + "foreign", "libc", "qemu_api_macros", ]
rust/Cargo.toml +1 -0
@@ -67,6 +67,7 @@ multiple_crate_versions = "deny" mut_mut = "deny" needless_bitwise_bool = "deny" needless_pass_by_ref_mut = "deny" +needless_update = "deny" no_effect_underscore_binding = "deny" option_option = "deny" or_fun_call = "deny"
rust/meson.build +2 -0
@@ -1,11 +1,13 @@ subproject('anyhow-1.0-rs', required: true) subproject('bilge-0.2-rs', required: true) subproject('bilge-impl-0.2-rs', required: true) +subproject('foreign-0.2-rs', required: true) subproject('libc-0.2-rs', required: true) anyhow_rs = dependency('anyhow-1.0-rs') bilge_rs = dependency('bilge-0.2-rs') bilge_impl_rs = dependency('bilge-impl-0.2-rs') +foreign_rs = dependency('foreign-0.2-rs') libc_rs = dependency('libc-0.2-rs') subproject('proc-macro2-1-rs', required: true)
rust/qemu-api/Cargo.toml +2 -0
@@ -15,7 +15,9 @@ rust-version.workspace = true [dependencies] qemu_api_macros = { path = "../qemu-api-macros" } +anyhow = "~1.0" libc = "0.2.162" +foreign = "~0.2" [features] default = ["debug_cell"]
rust/qemu-api/meson.build +2 -1
@@ -19,6 +19,7 @@ _qemu_api_rs = static_library( 'src/cell.rs', 'src/chardev.rs', 'src/errno.rs', + 'src/error.rs', 'src/irq.rs', 'src/memory.rs', 'src/module.rs',
@@ -35,7 +36,7 @@ _qemu_api_rs = static_library( override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_abi: 'rust', rust_args: _qemu_api_cfg, - dependencies: [libc_rs, qemu_api_macros], + dependencies: [anyhow_rs, foreign_rs, libc_rs, qemu_api_macros], ) rust.test('rust-qemu-api-tests', _qemu_api_rs,
rust/qemu-api/src/error.rs new file +273 -0
273 lines not on this page · raw message
rust/qemu-api/src/lib.rs +3 -0
@@ -19,6 +19,7 @@pub mod cell;pub mod chardev;pub mod errno; +pub mod error;pub mod irq;pub mod memory;pub mod module;
@@ -34,6 +35,8 @@ ffi::c_void, }; +pub use error::{Error ,Result }; +#[cfg( HAVE_GLIB_WITH_ALIGNED_ALLOC )] extern "C" { fn g_aligned_alloc0(
Signature · 1 line
2.49.0
Re: [PATCH 07/12] qemu-api: add bindings to Error
Quoted 6 lines · Paolo Bonzini
Provide an implementation of
std::error::Errorthat bridges the Rustanyhow::Errorandstd::panic::Locationtypes with QEMU's Error*. It also has several utility methods, analogous toerror_propagate(), that convert a Result into a return value + Error** pair.Signed-off-by Paolo Bonzini <[email protected]>
My Rust-fu is far to weak for an actual review. But let me try to stumble through your patch. I'll pretend to explain the Rust code to a noob like myself. Please correct my mistakes.
Quoted 110 lines
--- rust/Cargo.lock | 17 +++ rust/Cargo.toml | 1 + rust/meson.build | 2 + rust/qemu-api/Cargo.toml | 2 + rust/qemu-api/meson.build | 3 +- rust/qemu-api/src/error.rs | 273 +++++++++++++++++++++++++++++++++++++ rust/qemu-api/src/lib.rs | 3 + 7 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 rust/qemu-api/src/error.rsrust/Cargo.lock +17
-0@@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anyhow" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" + [[package]] name = "arbitrary-int" version = "1.2.7"@@ -37,6 +43,15 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +[[package]] +name = "foreign" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37dd09e47ea8fd592a333f59fc52b894a97fe966ae9c6b7ef21ae38de6043462" +dependencies = [ + "libc", +] + [[package]] name = "hpet" version = "0.1.0"@@ -106,6 +121,8 @@ dependencies = [ name = "qemu_api" version = "0.1.0" dependencies = [ + "anyhow", + "foreign", "libc", "qemu_api_macros", ]rust/Cargo.toml +1
-0@@ -67,6 +67,7 @@ multiple_crate_versions = "deny" mut_mut = "deny" needless_bitwise_bool = "deny" needless_pass_by_ref_mut = "deny" +needless_update = "deny" no_effect_underscore_binding = "deny" option_option = "deny" or_fun_call = "deny"rust/meson.build +2
-0@@ -1,11 +1,13 @@ subproject('anyhow-1.0-rs', required: true) subproject('bilge-0.2-rs', required: true) subproject('bilge-impl-0.2-rs', required: true) +subproject('foreign-0.2-rs', required: true) subproject('libc-0.2-rs', required: true) anyhow_rs = dependency('anyhow-1.0-rs') bilge_rs = dependency('bilge-0.2-rs') bilge_impl_rs = dependency('bilge-impl-0.2-rs') +foreign_rs = dependency('foreign-0.2-rs') libc_rs = dependency('libc-0.2-rs') subproject('proc-macro2-1-rs', required: true)rust/qemu-api/Cargo.toml +2
-0@@ -15,7 +15,9 @@ rust-version.workspace = true [dependencies] qemu_api_macros = { path = "../qemu-api-macros" } +anyhow = "~1.0" libc = "0.2.162" +foreign = "~0.2" [features] default = ["debug_cell"]rust/qemu-api/meson.build +1
-0@@ -19,6 +19,7 @@ _qemu_api_rs = static_library( 'src/cell.rs', 'src/chardev.rs', 'src/errno.rs', + 'src/error.rs', 'src/irq.rs', 'src/memory.rs', 'src/module.rs',
rust/qemu-api/meson.build _qemu_api_rs = static_library( · line 42
Quoted 8 lines
rust/qemu-api/meson.build +1
-1@@ -35,7 +36,7 @@ _qemu_api_rs = static_library( override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_abi: 'rust', rust_args: _qemu_api_cfg, - dependencies: [libc_rs, qemu_api_macros], + dependencies: [anyhow_rs, foreign_rs, libc_rs, qemu_api_macros], ) rust.test('rust-qemu-api-tests', _qemu_api_rs,
Mere plumbing so far. I assume I don't need to understand it at this point :)
Quoted 20 lines
rust/qemu-api/src/error.rs new file +14
-0@@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +//! Error class for QEMU Rust code +//! +//! @author Paolo Bonzini + +use std::{ + borrow::Cow , + ffi::{c_char, c_int, c_void,CStr }, + fmt::{self,Display }, + panic, ptr, +}; + +use foreign::{prelude::*,OwnedPointer };
rust/qemu-api/src/error.rs line 24
Quoted 10 lines
rust/qemu-api/src/error.rs new file +10
-0+ +use crate ::{ + bindings, + bindings::{error_free, error_get_pretty}, +}; + +pub type Result <T > = std::result::Result <T ,Error >; + +#[derive( Debug , Default )] +pub struct Error {
We're defining a custom error type Error for use with Result<>. This requires implementing a number of traits. For trait Debug, we take the auto-generated solution here. Other traits are implemented below, in particular Display.
I don't yet understand the role of trait Default.
Does the name Error risk confusion with std::error::Error?
rust/qemu-api/src/error.rs line 30
Quoted 6 lines
rust/qemu-api/src/error.rs new file +6
-0+ msg: Option<Cow<'static, str>>, +/// Appends the print string of the error to the msg if not None + cause: Option<anyhow::Error>, + file: &'static str, + line: u32, +}
This is the Rust equivalent to C struct Error. High-level differences:
* No @err_class. It's almost always ERROR_CLASS_GENERIC_ERROR in C nowadays. You're hardcoding that value in Rust for now. Good.
* @cause, optional. This is the bridge to idiomatic Rust error types.
* @msg is optional. This is so you can wrap a @cause without having to add some useless message.
Is having Errors with neither @msg nor @cause a good idea?
rust/qemu-api/src/error.rs line 33
Quoted 3 lines
rust/qemu-api/src/error.rs new file +3
-0+ +impl std::error::Error for Error { +fn source (&self) ->Option <&(dyn std::error::Error + 'static)> {
Provide the optional method source() that returns the lower-level source of the error, if any. I guess the system will put it to good use :)
rust/qemu-api/src/error.rs line 34
Quoted 1 line
rust/qemu-api/src/error.rs new file +1
-0+ self.cause.as_ref ().map (AsRef ::as_ref)
It returns the std::error::Error that's wrapped in @cause, if any. Reasonable.
rust/qemu-api/src/error.rs line 37
Quoted 3 lines
rust/qemu-api/src/error.rs new file +3
-0+ } + +#[allow(deprecated)]
Needed for what? Oh, method description() is deprecated since 1.42.0:
"use the Display impl or to_string()". I figure we need it because the new way doesn't work with our oldest supported Rust version. Could perhaps use a comment to help future garbage collectors.
rust/qemu-api/src/error.rs line 42
Quoted 5 lines
rust/qemu-api/src/error.rs new file +5
-0+fn description (&self) -> &str { + self.msg + .as_deref () + .or_else (|| self.cause.as_deref ().map (std::error::Error ::description)) + .unwrap_or ("error" )
This gives us @msg, or else @cause, or else "error".
Is it a good idea to ignore @cause when we have @msg?
Above, I doubted the wisdom of having Errors with neither @msg nor @cause. description() returns the rather unhelpful "error" then.
Quoted 7 lines
rust/qemu-api/src/error.rs new file +7
-0+ } +} + +impl Display for Error { +fn fmt (&self, f: &mut fmt::Formatter ) -> fmt::Result { +let mut prefix ="" ; +if let Some (ref msg) = self.msg {
rust/qemu-api/src/error.rs line 59
Quoted 10 lines
rust/qemu-api/src/error.rs new file +10
-0+write! (f,"{msg}" )?; + prefix =": " ; + } +if let Some (ref cause) = self.cause { +write! (f,"{prefix}{cause}" )?; + }else if prefix.is_empty () { + f.write_str ("unknown error" )?; + } +Ok (()) + }
This combines @msg and @cause:
* If we have both: msg:cause
* If we have just msg: msg
* If we have just cause: cause
* If we have neither: "unknown error"
Differs from description(). Why?
rust/qemu-api/src/error.rs line 63
Quoted 4 lines
rust/qemu-api/src/error.rs new file +4
-0+} + +impl From <String >for Error { +#[track_caller]
This enables use of panic::Location::caller().file() and .line() right below. Neat!
rust/qemu-api/src/error.rs line 70
Quoted 7 lines
rust/qemu-api/src/error.rs new file +7
-0+fn from (msg:String ) ->Self { +let location = panic::Location ::caller (); +Error { + msg:Some (Cow ::Owned (msg)), + file: location.file (), + line: location.line (), + ..Default ::default ()
I don't understand this line, I'm afraid.
rust/qemu-api/src/error.rs line 72
Quoted 2 lines
rust/qemu-api/src/error.rs new file +2
-0+ } + }
This creates an Error from an error message string, recording the spot where it's done. The resulting Error has @msg, but not @cause.
Quoted 4 lines
rust/qemu-api/src/error.rs new file +4
-0+} + +impl From <&'staticstr >for Error { +#[track_caller]
rust/qemu-api/src/error.rs line 86
Quoted 10 lines
rust/qemu-api/src/error.rs new file +10
-0+fn from (msg: &'staticstr ) ->Self { +let location = panic::Location ::caller (); +Error { + msg:Some (Cow ::Borrowed (msg)), + file: location.file (), + line: location.line (), + ..Default ::default () + } + } +}
Same for another string type.
rust/qemu-api/src/error.rs line 99
Quoted 13 lines
rust/qemu-api/src/error.rs new file +13
-0+ +impl From <anyhow::Error >for Error { +#[track_caller] +fn from (error: anyhow::Error ) ->Self { +let location = panic::Location ::caller (); +Error { + cause:Some (error), + file: location.file (), + line: location.line (), + ..Default ::default () + } + } +}
This creates an Error from an anyhow::Error, recording the spot where it's done. The resulting Error has @cause, but not @msg.
Quoted 4 lines
rust/qemu-api/src/error.rs new file +4
-0+ +impl Error { +/// Create a new error, prepending `msg` to the +/// description of `cause`
rust/qemu-api/src/error.rs line 113
Quoted 10 lines
rust/qemu-api/src/error.rs new file +10
-0+#[track_caller] +pub fn with_error <E : std::error::Error +Send +Sync + 'static>(msg: &'staticstr , cause:E ) ->Self { +let location = panic::Location ::caller (); +Error { + msg:Some (Cow ::Borrowed (msg)), + cause:Some (cause.into ()), + file: location.file (), + line: location.line (), + } + }
This creates an Error from an error message string and an anyhow::Error, recording the spot where it's done. The resulting Error has both @msg and @cause.
Is there a way to create an Error with neither @msg nor @cause?
rust/qemu-api/src/error.rs line 125
Quoted 12 lines
rust/qemu-api/src/error.rs new file +12
-0+ +/// Consume a result, returning false if it is an error and +/// true if it is successful. The error is propagated into +/// `errp` like the C API `error_propagate` would do. +/// +/// # Safety +/// +/// `errp` must be valid; typically it is received from C code +pub unsafe fn bool_or_propagate (result:Result <()>, errp: *mut *mut bindings::Error ) ->bool { +// SAFETY: caller guarantees errp is valid +unsafe {Self ::ok_or_propagate (result, errp) }.is_some () + }
Note: I jumped down to ok_or_propagate(), then returned here.
Note: I'm ignoring everything about safety in this first pass.
Alright, this is "return true on success (nothing propagated), return false and propagate on error". Okay.
Quoted 7 lines
rust/qemu-api/src/error.rs new file +7
-0+ +/// Consume a result, returning a `NULL` pointer if it is an +/// error and a C representation of the contents if it is +/// successful. The error is propagated into `errp` like +/// the C API `error_propagate` would do. +/// +/// # Safety
rust/qemu-api/src/error.rs line 142
Quoted 10 lines
rust/qemu-api/src/error.rs new file +10
-0+/// +/// `errp` must be valid; typically it is received from C code +#[must_use] +pub unsafe fn ptr_or_propagate <T :CloneToForeign >( + result:Result <T >, + errp: *mut *mut bindings::Error , + ) -> *mut T ::Foreign { +// SAFETY: caller guarantees errp is valid +unsafe {Self ::ok_or_propagate (result, errp) }.clone_to_foreign_ptr () + }
And this is "return result as foreign pointer on success (nothing propagated), return NULL and propagate on error". Okay.
rust/qemu-api/src/error.rs line 152
Quoted 10 lines
rust/qemu-api/src/error.rs new file +10
-0+ +/// Consume a result in the same way as `self.ok()`, but also propagate +/// a possible error into `errp`, like the C API `error_propagate` +/// would do. +/// +/// # Safety +/// +/// `errp` must be valid; typically it is received from C code +pub unsafe fn ok_or_propagate <T >( + result: Result<T>,
@result is either a success value of type T, or an error value.
rust/qemu-api/src/error.rs line 155
Quoted 3 lines
rust/qemu-api/src/error.rs new file +3
-0+ errp: *mut *mut bindings::Error, + ) -> Option<T> { +let Err (err) = resultelse {
If @result is an Error, put the error value in @err.
rust/qemu-api/src/error.rs line 156
Quoted 1 line
rust/qemu-api/src/error.rs new file +1
-0+return result.ok ();
Else succeed and return the success value.
rust/qemu-api/src/error.rs line 163
Quoted 7 lines
rust/qemu-api/src/error.rs new file +7
-0+ }; + +// SAFETY: caller guarantees errp is valid +unsafe { + err.propagate (errp); + } +None
@result is an Error. Propagate it, and return None.
This is indeed like self.ok() with propagation added.
rust/qemu-api/src/error.rs line 168
Quoted 5 lines
rust/qemu-api/src/error.rs new file +5
-0+ } + +/// Equivalent of the C function `error_propagate`. Fill `*errp` +/// with the information container in `result` if `errp` is not NULL; +/// then consume it.
Note error_propagate() has the arguments in the opposite order.
rust/qemu-api/src/error.rs line 121
Quoted 4 lines
rust/qemu-api/src/error.rs new file +4
-0+/// +/// # Safety +/// +/// `errp` must be valid; typically it is received from C code
What does "valid" mean exactly?
C error_propagate()'s function comment:
/*
* Propagate error object (if any) from @local_err to @dst_errp.
* If @local_err is NULL, do nothing (because there's nothing to
* propagate).
* Else, if @dst_errp is NULL, errors are being ignored. Free the
* error object.
* Else, if @dst_errp is &error_abort, print a suitable message and
* abort().
* Else, if @dst_errp is &error_fatal, print a suitable message and
* exit(1).
* Else, if @dst_errp already contains an error, ignore this one: free
* the error object.
* Else, move the error object from @local_err to *@dst_errp.
* On return, @local_err is invalid.
* Please use ERRP_GUARD() instead when possible.
* Please don't error_propagate(&error_fatal, ...), use
* error_report_err() and exit(), because that's more obvious. */
rust/qemu-api/src/error.rs line 173
Quoted 1 line
rust/qemu-api/src/error.rs new file +1
-0+pub unsafe fn propagate (self, errp: *mut *mut bindings::Error ) {
The function comment mentions result. The function signature doesn't have it. Do you mean self?
If yes, what does it mean to consume self?
Ignored for now: how exactly the code below consumes.
rust/qemu-api/src/error.rs line 174
Quoted 1 line
rust/qemu-api/src/error.rs new file +1
-0+if errp.is_null () {
Errors are being ignored.
rust/qemu-api/src/error.rs line 182
Quoted 8 lines
rust/qemu-api/src/error.rs new file +8
-0+return ; + } + +let err = self.clone_to_foreign_ptr (); + +// SAFETY: caller guarantees errp is valid +unsafe { + errp.write (err);
I *guess* this writes the foreign pointer we just obtained to @errp.
rust/qemu-api/src/error.rs line 184
Quoted 2 lines
rust/qemu-api/src/error.rs new file +2
-0+ } + }
Brief switch to the design level...
In C, you're almost always better off with ERRP_GUARD(). Would you like me to elaborate?
You still have to use error_propagate() to accumulate multiple errors so that the first one wins. That's commonly a dumb idea. Should we avoid this pattern in Rust?
rust/qemu-api/src/error.rs line 192
Quoted 8 lines
rust/qemu-api/src/error.rs new file +8
-0+ +/// Convert a C `Error*` into a Rust `Result`, using +/// `Ok(())` if `c_error` is NULL. +/// +/// # Safety +/// +/// `c_error` must be valid; typically it has been filled by a C +/// function.
What does "valid" mean exactly?
Quoted 14 lines
rust/qemu-api/src/error.rs new file +14
-0+pub unsafe fn err_or_unit (c_error: *mut bindings::Error ) ->Result <()> { +// SAFETY: caller guarantees c_error is valid +unsafe {Self ::err_or_else (c_error, || ()) } + } + +/// Convert a C `Error*` into a Rust `Result`, calling `f()` to +/// obtain an `Ok` value if `c_error` is NULL. +/// +/// # Safety +/// +/// `c_error` must be valid; typically it has been filled by a C +/// function. +pub unsafe fn err_or_else <T ,F :FnOnce () ->T >( + c_error: *mut bindings::Error,
rust/qemu-api/src/error.rs line 216
Quoted 10 lines
rust/qemu-api/src/error.rs new file +10
-0+ f: F, + ) -> Result<T> { +// SAFETY: caller guarantees c_error is valid +let err =unsafe {Option ::<Self >::from_foreign (c_error) }; +match err { +None =>Ok (f ()), +Some (err) =>Err (err), + } + } +}
Getting tired...
Quoted 21 lines
rust/qemu-api/src/error.rs new file +21
-0+ +impl FreeForeign for Error { +type Foreign = bindings::Error ; + +unsafe fn free_foreign (p: *mut bindings::Error ) { +// SAFETY: caller guarantees p is valid +unsafe { +error_free (p); + } + } +} + +impl CloneToForeign for Error { +fn clone_to_foreign (&self) ->OwnedPointer <Self > { +// SAFETY: all arguments are controlled by this function +unsafe { +let err: *mut c_void = libc::malloc (std::mem::size_of ::<bindings::Error >()); +let err: &mut bindings::Error = &mut *err.cast (); + *err = bindings::Error { + msg:format! ("{self}" ).clone_to_foreign_ptr (), + err_class: bindings::ERROR_CLASS_GENERIC_ERROR ,
rust/qemu-api/src/error.rs line 247
Quoted 10 lines
rust/qemu-api/src/error.rs new file +10
-0+ src_len: self.file.len() as isize, + src: self.file.as_ptr().cast::<c_char>(), + line: self.line as c_int, + func: ptr::null_mut(), + hint: ptr::null_mut(), + }; +OwnedPointer ::new (err) + } + } +}
Plausible to this Rust ignoramus :)
Quoted 16 lines
rust/qemu-api/src/error.rs new file +16
-0+ +impl FromForeign for Error { +unsafe fn cloned_from_foreign (c_error: *const bindings::Error ) ->Self { +// SAFETY: caller guarantees c_error is valid +unsafe { +let error = &*c_error; +let file =if error.src_len <0 { +// NUL-terminated +CStr ::from_ptr (error.src).to_str () + }else { +// Can become str::from_utf8 with Rust 1.87.0 + std::str::from_utf8 (std::slice::from_raw_parts ( + &*error.src.cast ::<u8 >(), + error.src_lenas usize , + )) + };
rust/qemu-api/src/error.rs line 273
Quoted 10 lines
rust/qemu-api/src/error.rs new file +10
-0+ +Error { + msg:FromForeign ::cloned_from_foreign (error_get_pretty (error)), + cause:None , + file: file.unwrap (), + line: error.lineas u32 , + } + } + } +}
Likewise.
Quoted 21 lines
rust/qemu-api/src/lib.rs +3
-0@@ -19,6 +19,7 @@pub mod cell;pub mod chardev;pub mod errno; +pub mod error;pub mod irq;pub mod memory;pub mod module;@@ -34,6 +35,8 @@ ffi::c_void, }; +pub use error::{Error ,Result }; +#[cfg( HAVE_GLIB_WITH_ALIGNED_ALLOC )] extern "C" { fn g_aligned_alloc0(
Re: [PATCH 07/12] qemu-api: add bindings to Error
Quoted 36 lines · Markus Armbruster
Quoted 20 lines
rust/qemu-api/src/error.rs new file +14
-0@@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +//! Error class for QEMU Rust code +//! +//! @author Paolo Bonzini + +use std::{ + borrow::Cow , + ffi::{c_char, c_int, c_void,CStr }, + fmt::{self,Display }, + panic, ptr, +}; + +use foreign::{prelude::*,OwnedPointer };rust/qemu-api/src/error.rs line 24
Quoted 10 lines
rust/qemu-api/src/error.rs new file +10
-0+ +use crate ::{ + bindings, + bindings::{error_free, error_get_pretty}, +}; + +pub type Result <T > = std::result::Result <T ,Error >; + +#[derive( Debug , Default )] +pub struct Error {We're defining a custom error type Error for use with Result<>. This requires implementing a number of traits. For trait Debug, we take the auto-generated solution here. Other traits are implemented below, in particular Display.
Quoted 1 line
I don't yet understand the role of trait Default.
It defines an Error without any frills attached. It is used below but on the other hand it results in those "unknown error"s that you rightly despised.
Quoted 1 line
Does the name Error risk confusion with
std::error::Error?
Maybe, but as you can see from e.g. ayhow::Error it's fairly common to have each crate or module define its own Error type. In the end you always convert them to another type with "?" or ".into()".
Quoted 11 lines
This is the Rust equivalent to C struct Error. High-level differences:
* No @err_class. It's almost always
ERROR_CLASS_GENERIC_ERRORin C nowadays. You're hardcoding that value in Rust for now. Good.* @cause, optional. This is the bridge to idiomatic Rust error types.
* @msg is optional. This is so you can wrap a @cause without having to add some useless message.
Is having Errors with neither @msg nor @cause a good idea?
It makes for slightly nicer code, and avoids having to worry about panics from ".unwrap()" in error handling code (where panicking probably won't help much). Otherwise it's probably not a good idea, but also not something that people will use since (see later patches) it's easier to return a decent error message than an empty Error.
Quoted 15 lines
Needed for what? Oh, method
description()is deprecated since 1.42.0:
"use the Display impl orto_string()". I figure we need it because the new way doesn't work with our oldest supported Rust version. Could perhaps use a comment to help future garbage collectors.rust/qemu-api/src/error.rs line 42
Quoted 5 lines
rust/qemu-api/src/error.rs new file +5
-0+fn description (&self) -> &str { + self.msg + .as_deref () + .or_else (|| self.cause.as_deref ().map (std::error::Error ::description)) + .unwrap_or ("error" )This gives us @msg, or else @cause, or else "error".
Is it a good idea to ignore @cause when we have @msg?
Quoted 8 lines
rust/qemu-api/src/error.rs line 52
Quoted 4 lines
rust/qemu-api/src/error.rs new file +3
-0+impl Display for Error { +fn fmt (&self, f: &mut fmt::Formatter ) -> fmt::Result { + }This combines @msg and @cause:
Differs from
description(). Why?
Because description() cannot build a dynamically-allocated string, it must return something that is already available in the Error. That limitation is probably why it was deprecated.
Since it's deprecated we can expect that it won't be used and not worry too much.
Quoted 9 lines
rust/qemu-api/src/error.rs line 70
Quoted 7 lines
rust/qemu-api/src/error.rs new file +7
-0+fn from (msg:String ) ->Self { +let location = panic::Location ::caller (); +Error { + msg:Some (Cow ::Owned (msg)), + file: location.file (), + line: location.line (), + ..Default ::default ()I don't understand this line, I'm afraid.
It says "every other field comes from "..Default::default()". I can replace it with "cause: None", and likewise below.
Quoted 8 lines
Quoted 8 lines
rust/qemu-api/src/error.rs new file +8
-0+} + +impl From <&'staticstr >for Error { +#[track_caller] +fn from (msg: &'staticstr ) ->Self { +let location = panic::Location ::caller (); +Error { + msg:Some (Cow ::Borrowed (msg)),
Quoted 8 lines
rust/qemu-api/src/error.rs line 73
Quoted 6 lines
rust/qemu-api/src/error.rs new file +6
-0+ file: location.file(), + line: location.line(), + ..Default ::default () + } + } +}Same for another string type.
Yes, this is for strings that are not allocated and are always valid---such as string constants.
Quoted 1 line
Is there a way to create an Error with neither @msg nor @cause?
Yes, Default::default()
Quoted 12 lines
rust/qemu-api/src/error.rs line 163
Quoted 11 lines
rust/qemu-api/src/error.rs new file +11
-0+ errp: *mut *mut bindings::Error, + ) -> Option<T> { +let Err (err) = resultelse { +return result.ok (); + }; + +// SAFETY: caller guarantees errp is valid +unsafe { + err.propagate (errp); + } +None
Quoted 3 lines
@result is an Error. Propagate it, and return None.
This is indeed like
self.ok()with propagation added.
Alternatively:
result
.map_err(|err| unsafe { err.propagate(errp) }) .ok()
Shorter, but the functional style can be off putting. What do you prefer?
Quoted 7 lines
rust/qemu-api/src/error.rs line 168
Quoted 5 lines
rust/qemu-api/src/error.rs new file +5
-0+ } + +/// Equivalent of the C function `error_propagate`. Fill `*errp` +/// with the information container in `result` if `errp` is not NULL; +/// then consume it. Note
error_propagate()has the arguments in the opposite order.
Yeah, here "self" must be first so that you use it as a method. Though, s/result/self/ as you noted.
Quoted 5 lines
rust/qemu-api/src/error.rs line 121
Quoted 3 lines
rust/qemu-api/src/error.rs new file +3
-0+/// # Safety +/// +/// `errp` must be valid; typically it is received from C code What does "valid" mean exactly?
I will copy from error_propagate in v2.
Quoted 8 lines
Brief switch to the design level...
In C, you're almost always better off with
ERRP_GUARD(). Would you like me to elaborate?You still have to use
error_propagate()to accumulate multiple errors so that the first one wins. That's commonly a dumb idea. Should we avoid this pattern in Rust?
In Rust there are three kinds of functions that use errors. Two are in qemu_api:
(1) bridges from C to Rust function pointers. They receive a Result<> from Rust functions and use error_propagate() (probably via functions such as ok_or_propagate, bool_or_propagate, etc.) to prepare a C return value.
(2) bridges from Rust to C functions. They pass an Error** to the C function and use err_or_else() or err_or_unit() to prepare a Rust return value
Functions of kind (1) are like functions in C that do a single call and just pass down errp, for example user_creatable_add_qapi(). These do not need ERRP_GUARD() because:
* the conversion from Result<> to C is pretty much a necessity, and it's done with functions that guarantee the propagation
* the conversion function *consumes* the Result<>, guaranteeing that you do not propagate more than once with tragic results
Function of kind (2) do not need ERRP_GUARD() because they do not take an Error** at all. They pass one down to C, but they return a Result<>.
The third kind is Rust functions that are called from (1) and that themselves call (2). These are where ERRP_GUARD() would be used in C, but in Rust these do not see Error** at all. The "?" operator has the same effect as ERRP_GUARD(), i.e. it handles passing the error from called function to return value. What in C would be
ERRP_GUARD();
if (!func_that_returns_bool(...)) { return; }In Rust is
func_that_returns_result(...)?;
which is a bit disconcerting in the beginning but grows on you. (Generally I find that to be the experience with Rust. It's downright weird, but unlike C++ the good parts outweigh the weirdness).
Quoted 18 lines
Quoted 18 lines
rust/qemu-api/src/error.rs new file +18
-0+pub unsafe fn err_or_unit (c_error: *mut bindings::Error ) ->Result <()> { +// SAFETY: caller guarantees c_error is valid +unsafe {Self ::err_or_else (c_error, || ()) } + } + +/// Convert a C `Error*` into a Rust `Result`, calling `f()` to +/// obtain an `Ok` value if `c_error` is NULL. +/// +/// # Safety +/// +/// `c_error` must be valid; typically it has been filled by a C +/// function. +pub unsafe fn err_or_else <T ,F :FnOnce () ->T >( + c_error: *mut bindings::Error , + f:F , + ) ->Result <T > { +// SAFETY: caller guarantees c_error is valid +let err =unsafe {Option ::<Self >::from_foreign (c_error) };
Quoted 8 lines
rust/qemu-api/src/error.rs line 216
Quoted 6 lines
rust/qemu-api/src/error.rs new file +6
-0+match err { +None =>Ok (f ()), +Some (err) =>Err (err), + } + } +}Getting tired...
No problem. While this is kinda important, it's not used yet. The clients would look like this:
fn type_get_or_load_by_name(name: &str) -> Result<&TypeImpl> {
unsafe {
let err: *mut bindings::Error = ptr::null_mut();
let typ: *mut TypeImpl = bindings::type_get_or_load_by_name(
name.clone_to_foreign().as_ptr(), &mut err);
// on success, "typ" can be accessed safely
// on failure, turn the Error* into a qemu_api::Error and free it
Result::err_or_else(err, || &*typ)
}This is why I need to write tests.
Quoted 13 lines
Quoted 13 lines
rust/qemu-api/src/error.rs new file +13
-0+impl CloneToForeign for Error { +fn clone_to_foreign (&self) ->OwnedPointer <Self > { +// SAFETY: all arguments are controlled by this function +unsafe { +let err: *mut c_void = libc::malloc (std::mem::size_of ::<bindings::Error >()); +let err: &mut bindings::Error = &mut *err.cast (); + *err = bindings::Error { + msg:format! ("{self}" ).clone_to_foreign_ptr (), + err_class: bindings::ERROR_CLASS_GENERIC_ERROR , + src_len: self.file.len ()as isize , + src: self.file.as_ptr ().cast ::<c_char >(), + line: self.lineas c_int , + func: ptr::null_mut (),
Quoted 8 lines
rust/qemu-api/src/error.rs line 247
Quoted 6 lines
rust/qemu-api/src/error.rs new file +6
-0+ hint: ptr::null_mut(), + }; +OwnedPointer ::new (err) + } + } +}Plausible to this Rust ignoramus :)
Good, since these *Foreign traits are not part of the standard library, but rather something that I concocted and published outside QEMU. If you had no problems understanding how they were used (e.g. stuff like into_native or clone_to_foreign_ptr), that is good.
Paolo
Paolo
Re: [PATCH 07/12] qemu-api: add bindings to Error
Quoted 37 lines · Paolo Bonzini
Quoted 36 lines · Markus Armbruster
Quoted 20 lines
rust/qemu-api/src/error.rs new file +14
-0@@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +//! Error class for QEMU Rust code +//! +//! @author Paolo Bonzini + +use std::{ + borrow::Cow , + ffi::{c_char, c_int, c_void,CStr }, + fmt::{self,Display }, + panic, ptr, +}; + +use foreign::{prelude::*,OwnedPointer };rust/qemu-api/src/error.rs line 24
Quoted 10 lines
rust/qemu-api/src/error.rs new file +10
-0+ +use crate ::{ + bindings, + bindings::{error_free, error_get_pretty}, +}; + +pub type Result <T > = std::result::Result <T ,Error >; + +#[derive( Debug , Default )] +pub struct Error {We're defining a custom error type Error for use with Result<>. This requires implementing a number of traits. For trait Debug, we take the auto-generated solution here. Other traits are implemented below, in particular Display.
Quoted 5 lines
Quoted 1 line
I don't yet understand the role of trait Default.
It defines an Error without any frills attached. It is used below but on the other hand it results in those "unknown error"s that you rightly despised.
I think I understand how this works now.
Quoted 5 lines
Quoted 1 line
Does the name Error risk confusion with
std::error::Error?Maybe, but as you can see from e.g.
ayhow::Errorit's fairly common to have each crate or module define its own Error type. In the end you always convert them to another type with "?" or ".into()".
Fair enough.
Quoted 10 lines
Quoted 10 lines
This is the Rust equivalent to C struct Error. High-level differences:
* No @err_class. It's almost always
ERROR_CLASS_GENERIC_ERRORin C nowadays. You're hardcoding that value in Rust for now. Good.* @cause, optional. This is the bridge to idiomatic Rust error types.
* @msg is optional. This is so you can wrap a @cause without having to add some useless message.
Quoted 7 lines
Quoted 1 line
Is having Errors with neither @msg nor @cause a good idea?
It makes for slightly nicer code, and avoids having to worry about panics from ".unwrap()" in error handling code (where panicking probably won't help much). Otherwise it's probably not a good idea, but also not something that people will use since (see later patches) it's easier to return a decent error message than an empty Error.
I accept that making "need at least one of @msg, @cause" an invariant could complicate the code a bit.
Having a way to create an Error with neither bothers me, even when it's obscure. Making an interface hard to misuse is good. Making it impossible to misuse is better.
Quoted 22 lines
Quoted 22 lines
Needed for what? Oh, method
description()is deprecated since 1.42.0:
"use the Display impl orto_string()". I figure we need it because the new way doesn't work with our oldest supported Rust version. Could perhaps use a comment to help future garbage collectors.rust/qemu-api/src/error.rs line 42
Quoted 5 lines
rust/qemu-api/src/error.rs new file +5
-0+fn description (&self) -> &str { + self.msg + .as_deref () + .or_else (|| self.cause.as_deref ().map (std::error::Error ::description)) + .unwrap_or ("error" )This gives us @msg, or else @cause, or else "error".
Is it a good idea to ignore @cause when we have @msg?
rust/qemu-api/src/error.rs line 52
Quoted 4 lines
rust/qemu-api/src/error.rs new file +3
-0+impl Display for Error { +fn fmt (&self, f: &mut fmt::Formatter ) -> fmt::Result { + }This combines @msg and @cause:
Quoted 8 lines
Quoted 1 line
Differs from
description(). Why?Because
description()cannot build a dynamically-allocated string, it must return something that is already available in the Error. That limitation is probably why it was deprecated.Since it's deprecated we can expect that it won't be used and not worry too much.
Could we somehow get away with not implementing it?
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
rust/qemu-api/src/error.rs new file +7
-0+fn from (msg:String ) ->Self { +let location = panic::Location ::caller (); +Error { + msg:Some (Cow ::Owned (msg)), + file: location.file (), + line: location.line (), + ..Default ::default ()
Quoted 4 lines
Quoted 1 line
I don't understand this line, I'm afraid.
It says "every other field comes from "..Default::default()". I can replace it with "cause: None", and likewise below.
I've since learned a bit more about Default. Since the only field being defaulted is @cause, I guess I'd use "cause: None" myself for simplicity. Matter of taste, you choose.
However, if this is the only real reason for Error having the Default trait, I'd suggest to kill the trait with fire, because ...
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
rust/qemu-api/src/error.rs new file +8
-0+} + +impl From <&'staticstr >for Error { +#[track_caller] +fn from (msg: &'staticstr ) ->Self { +let location = panic::Location ::caller (); +Error { + msg:Some (Cow ::Borrowed (msg)),Quoted 8 lines
rust/qemu-api/src/error.rs line 73
Quoted 6 lines
rust/qemu-api/src/error.rs new file +6
-0+ file: location.file(), + line: location.line(), + ..Default ::default () + } + } +}Same for another string type.
Quoted 6 lines
Yes, this is for strings that are not allocated and are always valid---such as string constants.
Quoted 1 line
Is there a way to create an Error with neither @msg nor @cause?
Yes,
Default::default()
... I do dislike being able to create such Errors :)
Quoted 16 lines
Quoted 12 lines
rust/qemu-api/src/error.rs line 163
Quoted 11 lines
rust/qemu-api/src/error.rs new file +11
-0+ errp: *mut *mut bindings::Error, + ) -> Option<T> { +let Err (err) = resultelse { +return result.ok (); + }; + +// SAFETY: caller guarantees errp is valid +unsafe { + err.propagate (errp); + } +None Quoted 3 lines
@result is an Error. Propagate it, and return None.
This is indeed like
self.ok()with propagation added.
Quoted 7 lines
Alternatively:
result
.map_err(|err| unsafe {err.propagate(errp)}) .ok()Shorter, but the functional style can be off putting. What do you prefer?
I'm fine with the functional style, and I like brevity.
Quoted 28 lines
Quoted 7 lines
rust/qemu-api/src/error.rs line 168
Quoted 5 lines
rust/qemu-api/src/error.rs new file +5
-0+ } + +/// Equivalent of the C function `error_propagate`. Fill `*errp` +/// with the information container in `result` if `errp` is not NULL; +/// then consume it. Note
error_propagate()has the arguments in the opposite order.Yeah, here "self" must be first so that you use it as a method. Though, s/result/self/ as you noted.
Quoted 5 lines
rust/qemu-api/src/error.rs line 121
Quoted 3 lines
rust/qemu-api/src/error.rs new file +3
-0+/// # Safety +/// +/// `errp` must be valid; typically it is received from C code What does "valid" mean exactly?
I will copy from
error_propagatein v2.Quoted 8 lines
Brief switch to the design level...
In C, you're almost always better off with
ERRP_GUARD(). Would you like me to elaborate?You still have to use
error_propagate()to accumulate multiple errors so that the first one wins. That's commonly a dumb idea. Should we avoid this pattern in Rust?
Quoted 6 lines
In Rust there are three kinds of functions that use errors. Two are in
qemu_api:(1) bridges from C to Rust function pointers. They receive a Result<> from Rust functions and use
error_propagate()(probably via functions such asok_or_propagate,bool_or_propagate, etc.) to prepare a C return value.
We received an idiomatic Rust Result<>. We want to pass it to a C-like caller, i.e. satisfy the contract "on success, return a value and leave argument @errp alone; on error, return an error value and store an Error object in @errp".
(I'm ignoring void values for brevity)
The functions you mentioned split up the Result<>, do the storing, and return the value to be returned.
Quoted 3 lines
(2) bridges from Rust to C functions. They pass an Error** to the C function and use
err_or_else()orerr_or_unit()to prepare a Rust return value
We received a return value and possibly a C Error object. We want to pass it to a Rust caller, i.e. return an idiomatic Result<>.
The functions you mentioned assemble the Result<> from the two values.
There's difference between Result<> and what we do in C: Result<> is either a success value or an Error value. I.e. we have a success value on success, an Error on failure. In C, we have a success value on success, and both an error value and an Error on failure.
The (1) functions need to make up the missing error value. That's fine, it's what we do in C, too.
The (2) functions throw away the error value. Usually, there's exactly one error value, so no information is thrown away. But not necessarily.
I think it's okay to design for the common case now.
Quoted 95 lines
Functions of kind (1) are like functions in C that do a single call and just pass down errp, for example
user_creatable_add_qapi(). These do not needERRP_GUARD()because:* the conversion from Result<> to C is pretty much a necessity, and it's done with functions that guarantee the propagation
* the conversion function *consumes* the Result<>, guaranteeing that you do not propagate more than once with tragic results
Function of kind (2) do not need
ERRP_GUARD()because they do not take an Error** at all. They pass one down to C, but they return a Result<>.The third kind is Rust functions that are called from (1) and that themselves call (2). These are where
ERRP_GUARD()would be used in C, but in Rust these do not see Error** at all. The "?" operator has the same effect asERRP_GUARD(), i.e. it handles passing the error from called function to return value. What in C would beERRP_GUARD(); if (!func_that_returns_bool(...)) { return; }In Rust is
func_that_returns_result(...)?;which is a bit disconcerting in the beginning but grows on you. (Generally I find that to be the experience with Rust. It's downright weird, but unlike C++ the good parts outweigh the weirdness).
Quoted 18 lines
Quoted 18 lines
rust/qemu-api/src/error.rs new file +18
-0+pub unsafe fn err_or_unit (c_error: *mut bindings::Error ) ->Result <()> { +// SAFETY: caller guarantees c_error is valid +unsafe {Self ::err_or_else (c_error, || ()) } + } + +/// Convert a C `Error*` into a Rust `Result`, calling `f()` to +/// obtain an `Ok` value if `c_error` is NULL. +/// +/// # Safety +/// +/// `c_error` must be valid; typically it has been filled by a C +/// function. +pub unsafe fn err_or_else <T ,F :FnOnce () ->T >( + c_error: *mut bindings::Error , + f:F , + ) ->Result <T > { +// SAFETY: caller guarantees c_error is valid +let err =unsafe {Option ::<Self >::from_foreign (c_error) };Quoted 8 lines
rust/qemu-api/src/error.rs line 216
Quoted 6 lines
rust/qemu-api/src/error.rs new file +6
-0+match err { +None =>Ok (f ()), +Some (err) =>Err (err), + } + } +}Getting tired...
No problem. While this is kinda important, it's not used yet. The clients would look like this:
fn type_get_or_load_by_name(name: &str) -> Result<&TypeImpl> { unsafe { let err: *mut bindings::Error = ptr::null_mut(); let typ: *mut TypeImpl = bindings::type_get_or_load_by_name( name.clone_to_foreign().as_ptr(), &mut err); // on success, "typ" can be accessed safely // on failure, turn the Error* into a qemu_api::Error and free it Result::err_or_else(err, || &*typ) }This is why I need to write tests.
Quoted 13 lines
Quoted 13 lines
rust/qemu-api/src/error.rs new file +13
-0+impl CloneToForeign for Error { +fn clone_to_foreign (&self) ->OwnedPointer <Self > { +// SAFETY: all arguments are controlled by this function +unsafe { +let err: *mut c_void = libc::malloc (std::mem::size_of ::<bindings::Error >()); +let err: &mut bindings::Error = &mut *err.cast (); + *err = bindings::Error { + msg:format! ("{self}" ).clone_to_foreign_ptr (), + err_class: bindings::ERROR_CLASS_GENERIC_ERROR , + src_len: self.file.len ()as isize , + src: self.file.as_ptr ().cast ::<c_char >(), + line: self.lineas c_int , + func: ptr::null_mut (),Quoted 8 lines
rust/qemu-api/src/error.rs line 247
Quoted 6 lines
rust/qemu-api/src/error.rs new file +6
-0+ hint: ptr::null_mut(), + }; +OwnedPointer ::new (err) + } + } +}Plausible to this Rust ignoramus :)
Quoted 4 lines
Good, since these *Foreign traits are not part of the standard library, but rather something that I concocted and published outside QEMU. If you had no problems understanding how they were used (e.g. stuff like
into_nativeorclone_to_foreign_ptr), that is good.
"Understanding" is a bit strong. I relied on my voodoo-programming skills.
Thank you!
PATCH 8/12 rust: qdev: support returning errors from realize
In reply to Paolo Bonzini--- rust/hw/char/pl011/src/device.rs | 5 +++-- rust/hw/timer/hpet/src/hpet.rs | 5 +++-- rust/qemu-api/src/qdev.rs | 10 ++++++---- 3 files changed, 12 insertions(+), 8 deletions(-)
rust/hw/char/pl011/src/device.rs +3 -2
@@ -175,7 +175,7 @@ fn properties() -> &'static [Property] {fn vmsd () ->Option <&'staticVMStateDescription > {Some (&device_class::VMSTATE_PL011 ) } -const REALIZE :Option <fn (&Self )> =Some (Self ::realize); +const REALIZE :Option <fn (&Self ) -> qemu_api::Result <()>> =Some (Self ::realize); }impl ResettablePhasesImpl for PL011State {
@@ -619,9 +619,10 @@ fn event(&self, event: Event) { } } -fn realize (&self) { +fn realize (&self) -> qemu_api::Result <()> { self.char_backend .enable_handlers (self,Self ::can_receive,Self ::receive,Self ::event); +Ok (()) }fn reset_hold (&self, _type:ResetType ) {
rust/hw/timer/hpet/src/hpet.rs +3 -2
@@ -724,7 +724,7 @@ fn post_init(&self) { } } -fn realize (&self) { +fn realize (&self) -> qemu_api::Result <()> {if self.int_route_cap ==0 {// TODO: Add error binding: warn_report() println! ("Hpet's hpet-intcap property not initialized" );
@@ -751,6 +751,7 @@ fn realize(&self) { self.init_gpio_in (2 ,HPETState ::handle_legacy_irq); self.init_gpio_out (from_ref (&self.pit_enabled)); +Ok (()) }fn reset_hold (&self, _type:ResetType ) {
@@ -1042,7 +1043,7 @@ fn vmsd() -> Option<&'static VMStateDescription> {Some (&VMSTATE_HPET ) } -const REALIZE :Option <fn (&Self )> =Some (Self ::realize); +const REALIZE :Option <fn (&Self ) -> qemu_api::Result <()>> =Some (Self ::realize); }impl ResettablePhasesImpl for HPETState {
rust/qemu-api/src/qdev.rs +6 -4
@@ -12,10 +12,11 @@pub use bindings::{ClockEvent ,DeviceClass ,Property ,ResetType };use crate ::{ - bindings::{self, qdev_init_gpio_in, qdev_init_gpio_out,Error ,ResettableClass }, + bindings::{self, qdev_init_gpio_in, qdev_init_gpio_out,ResettableClass }, callbacks::FnCall , cell::{bql_locked,Opaque }, chardev::Chardev , + error::{Error ,Result }, irq::InterruptSource , prelude::*, qom::{ObjectClass ,ObjectImpl ,Owned },
@@ -108,7 +109,7 @@ pub trait DeviceImpl: ObjectImpl + ResettablePhasesImpl + IsA<DeviceState> {/// /// If not `None`, the parent class's `realize` method is overridden /// with the function pointed to by `REALIZE`. -const REALIZE :Option <fn (&Self )> =None ; +const REALIZE :Option <fn (&Self ) ->Result <()>> =None ;/// An array providing the properties that the user can set on the /// device. Not a `const` because referencing statics in constants
@@ -134,10 +135,11 @@ fn vmsd() -> Option<&'static VMStateDescription> {/// readable/writeable from one thread at any time. unsafe extern "C" fn rust_realize_fn <T :DeviceImpl >( dev: *mut bindings::DeviceState , - _errp: *mut *mut Error , + errp: *mut *mut bindings::Error , ) {let state =NonNull ::new (dev).unwrap ().cast ::<T >(); -T ::REALIZE .unwrap ()(unsafe { state.as_ref () }); +let result =T ::REALIZE .unwrap ()(unsafe { state.as_ref () }); +unsafe {Error ::ok_or_propagate (result, errp); } }unsafe impl InterfaceType for ResettableClass {
Signature · 1 line
2.49.0
Re: [PATCH 08/12] rust: qdev: support returning errors from realize
Quoted 11 lines · Paolo Bonzini
Date: Mon, 26 May 2025 16:24:51 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 08/12] rust: qdev: support returning errors from realize
X-Mailer: git-send-email 2.49.0Signed-off-by Paolo Bonzini <[email protected]>--- rust/hw/char/pl011/src/device.rs | 5 +++-- rust/hw/timer/hpet/src/hpet.rs | 5 +++-- rust/qemu-api/src/qdev.rs | 10 ++++++---- 3 files changed, 12 insertions(+), 8 deletions(-)
PATCH 9/12 rust/hpet: change timer of num_timers to usize
In reply to Paolo BonziniRemove the need to convert after every read of the BqlCell. Because the vmstate uses a u8 as the size of the VARRAY, this requires switching the VARRAY to use num_timers_save; which in turn requires ensuring that the num_timers_save is always there. For simplicity do this by removing support for version 1, which QEMU has not been producing for ~15 years.
--- rust/hw/timer/hpet/src/hpet.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
rust/hw/timer/hpet/src/hpet.rs +12 -12
@@ -12,7 +12,7 @@use qemu_api::{ bindings::{ address_space_memory, address_space_stl_le, qdev_prop_bit, qdev_prop_bool, - qdev_prop_uint32, qdev_prop_uint8, + qdev_prop_uint32, qdev_prop_usize, }, cell::{BqlCell ,BqlRefCell }, irq::InterruptSource ,
@@ -36,9 +36,9 @@const HPET_REG_SPACE_LEN :u64 =0x400 ;// 1024 bytes /// Minimum recommended hardware implementation. -const HPET_MIN_TIMERS :u8 =3 ; +const HPET_MIN_TIMERS :usize =3 ;/// Maximum timers in each timer block. -const HPET_MAX_TIMERS :u8 =32 ; +const HPET_MAX_TIMERS :usize =32 ;/// Flags that HPETState.flags supports. const HPET_FLAG_MSI_SUPPORT_SHIFT :usize =0 ;
@@ -561,8 +561,8 @@ pub struct HPETState {/// HPET timer array managed by this timer block. #[doc(alias = "timer" )] - timers: [BqlRefCell<HPETTimer>; HPET_MAX_TIMERS as usize], - num_timers: BqlCell<u8>, + timers: [BqlRefCell<HPETTimer>; HPET_MAX_TIMERS], + num_timers: BqlCell<usize>, num_timers_save: BqlCell<u8>,/// Instance id (HPET timer block ID).
@@ -572,7 +572,7 @@ pub struct HPETState {impl HPETState {// Get num_timers with `usize` type, which is useful to play with array index. fn get_num_timers (&self) ->usize { - self.num_timers.get ().into () + self.num_timers.get () }const fn has_msi_flag (&self) ->bool {
@@ -854,7 +854,7 @@ fn pre_save(&self) -> i32 { * also added to the migration stream. Check that it matches the value * that was configured. */ - self.num_timers_save.set (self.num_timers.get ()); + self.num_timers_save.set (self.num_timers.get ()as u8 );0 }
@@ -884,7 +884,7 @@ fn is_offset_needed(&self) -> bool { }fn validate_num_timers (&self, _version_id:u8 ) ->bool { - self.num_timers.get () == self.num_timers_save.get () + self.num_timers.get () == self.num_timers_save.get ().into () } }
@@ -911,7 +911,7 @@ impl ObjectImpl for HPETState { c"timers", HPETState, num_timers, - unsafe { &qdev_prop_uint8 }, + unsafe { &qdev_prop_usize }, u8, default =HPET_MIN_TIMERS ),
@@ -1016,16 +1016,16 @@ impl ObjectImpl for HPETState {static VMSTATE_HPET :VMStateDescription =VMStateDescription { name:c"hpet" .as_ptr (), version_id:2 , - minimum_version_id:1 , + minimum_version_id:2 , pre_save:Some (hpet_pre_save), post_load:Some (hpet_post_load), fields:vmstate_fields! {vmstate_of! (HPETState , config),vmstate_of! (HPETState , int_status),vmstate_of! (HPETState , counter), -vmstate_of! (HPETState , num_timers_save).with_version_id (2 ), +vmstate_of! (HPETState , num_timers_save),vmstate_validate! (HPETState ,VALIDATE_TIMERS_NAME ,HPETState ::validate_num_timers), -vmstate_struct! (HPETState , timers[0 .. num_timers], &VMSTATE_HPET_TIMER ,BqlRefCell <HPETTimer >,HPETState ::validate_num_timers).with_version_id (0 ), +vmstate_struct! (HPETState , timers[0 .. num_timers_save], &VMSTATE_HPET_TIMER ,BqlRefCell <HPETTimer >,HPETState ::validate_num_timers).with_version_id (0 ), }, subsections:vmstate_subsections! {VMSTATE_HPET_RTC_IRQ_LEVEL ,
Signature · 1 line
2.49.0
Re: [PATCH 09/12] rust/hpet: change timer of num_timers to usize
Quoted 8 lines
rust/hw/timer/hpet/src/hpet.rs +1
-1@@ -1016,16 +1016,16 @@ impl ObjectImpl for HPETState {static VMSTATE_HPET :VMStateDescription =VMStateDescription { name:c"hpet" .as_ptr (), version_id:2 , - minimum_version_id:1 , + minimum_version_id:2 , pre_save:Some (hpet_pre_save), post_load:Some (hpet_post_load),
rust/hw/timer/hpet/src/hpet.rs impl ObjectImpl for HPETState { · line 1029
Quoted 10 lines
rust/hw/timer/hpet/src/hpet.rs +2
-2fields: vmstate_fields! {vmstate_of! (HPETState , config),vmstate_of! (HPETState , int_status),vmstate_of! (HPETState , counter), -vmstate_of! (HPETState , num_timers_save).with_version_id (2 ), +vmstate_of! (HPETState , num_timers_save),vmstate_validate! (HPETState ,VALIDATE_TIMERS_NAME ,HPETState ::validate_num_timers), -vmstate_struct! (HPETState , timers[0 .. num_timers], &VMSTATE_HPET_TIMER ,BqlRefCell <HPETTimer >,HPETState ::validate_num_timers).with_version_id (0 ), +vmstate_struct! (HPETState , timers[0 .. num_timers_save], &VMSTATE_HPET_TIMER ,BqlRefCell <HPETTimer >,HPETState ::validate_num_timers).with_version_id (0 ), },
Here Rust and C take slightly different paths.
Either changes should be made in the C version as well, or the "functional\ [#issues]_ replacements" issue note in docs/devel/rust.rst needs to be updated as well. I think the former is simpler!
Otherwise,
PATCH 10/12 hpet: return errors from realize if properties are incorrect
In reply to Paolo BonziniDo not silently adjust num_timers, and fail if intcap is 0.
--- hw/timer/hpet.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
hw/timer/hpet.c +8 -7
@@ -689,8 +689,14 @@ static void hpet_realize(DeviceState *dev, Error **errp)int i;HPETTimer *timer; +if (s->num_timers <HPET_MIN_TIMERS || s->num_timers >HPET_MAX_TIMERS ) { +error_setg (errp,"hpet.num_timers must be between %d and %d" , +HPET_MIN_TIMERS ,HPET_MAX_TIMERS ); +return ; + }if (!s->intcap) { -warn_report ("Hpet's intcap not initialized" ); +error_setg (errp,"hpet.hpet-intcap not initialized" ); +return ; }if (hpet_fw_cfg.count ==UINT8_MAX ) {/* first instance */
@@ -698,7 +704,7 @@ static void hpet_realize(DeviceState *dev, Error **errp) }if (hpet_fw_cfg.count ==8 ) { -error_setg (errp,"Only 8 instances of HPET is allowed" ); +error_setg (errp,"Only 8 instances of HPET are allowed" );return ; }
@@ -708,11 +714,6 @@ static void hpet_realize(DeviceState *dev, Error **errp)sysbus_init_irq (sbd, &s->irqs[i]); } -if (s->num_timers <HPET_MIN_TIMERS ) { - s->num_timers =HPET_MIN_TIMERS ; - }else if (s->num_timers >HPET_MAX_TIMERS ) { - s->num_timers =HPET_MAX_TIMERS ; - }for (i =0 ; i <HPET_MAX_TIMERS ; i++) { timer = &s->timer[i]; timer->qemu_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , hpet_timer, timer);
Signature · 1 line
2.49.0
Re: [PATCH 10/12] hpet: return errors from realize if properties are incorrect
Quoted 1 line · Paolo Bonzini
Do not silently adjust
num_timers, and fail if intcap is 0.
A bad habit of ours.
Quoted 36 lines
Signed-off-by Paolo Bonzini <[email protected]>--- hw/timer/hpet.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)hw/timer/hpet.c +8
-2@@ -689,8 +689,14 @@ static void hpet_realize(DeviceState *dev, Error **errp)int i;HPETTimer *timer; +if (s->num_timers <HPET_MIN_TIMERS || s->num_timers >HPET_MAX_TIMERS ) { +error_setg (errp,"hpet.num_timers must be between %d and %d" , +HPET_MIN_TIMERS ,HPET_MAX_TIMERS ); +return ; + }if (!s->intcap) { -warn_report ("Hpet's intcap not initialized" ); +error_setg (errp,"hpet.hpet-intcap not initialized" ); +return ; }if (hpet_fw_cfg.count ==UINT8_MAX ) {/* first instance */ @@ -698,7 +704,7 @@ static void hpet_realize(DeviceState *dev, Error **errp) }if (hpet_fw_cfg.count ==8 ) { -error_setg (errp,"Only 8 instances of HPET is allowed" ); +error_setg (errp,"Only 8 instances of HPET are allowed" );return ; }@@ -708,11 +714,6 @@ static void hpet_realize(DeviceState *dev, Error **errp)sysbus_init_irq (sbd, &s->irqs[i]);
hw/timer/hpet.c static void hpet_realize(DeviceState *dev, Error **errp) · line 719
Quoted 10 lines
hw/timer/hpet.c +0
-5} -if (s->num_timers <HPET_MIN_TIMERS ) { - s->num_timers =HPET_MIN_TIMERS ; - }else if (s->num_timers >HPET_MAX_TIMERS ) { - s->num_timers =HPET_MAX_TIMERS ; - }for (i =0 ; i <HPET_MAX_TIMERS ; i++) { timer = &s->timer[i]; timer->qemu_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , hpet_timer, timer);
This device is not user-creatable. It is only ever created (and realized) by board code. Errors should not happen. If they happen anyway, it's a board code bug.
The code creating it is pc_basic_device_init():
if (pcms->hpet_enabled) {
qemu_irq rtc_irq;
hpet = qdev_try_new(TYPE_HPET);
if (!hpet) {
error_report("couldn't create HPET device");
exit(1);
}Could just as well use qdev_new(). Differently confusing error message, though.
/*
* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-*,
* use IRQ16~23, IRQ8 and IRQ2. If the user has already set
* the property, use whatever mask they specified.
*/
uint8_t compat = object_property_get_uint(OBJECT(hpet),
HPET_INTCAP, NULL);
if (!compat) {
qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
}
sysbus_realize_and_unref(SYS_BUS_DEVICE(hpet), &error_fatal);If this fails, it's a programming error, i.e. &error_abort is more appropriate. Hmm, can the user mess with property values via -global? If yes, it could be a user error.
[...]
}I'm rambling. The patch is fine.
Re: [PATCH 10/12] hpet: return errors from realize if properties are incorrect
In reply to Paolo BonziniQuoted 6 lines · Paolo Bonzini
Date: Mon, 26 May 2025 16:24:53 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 10/12] hpet: return errors from realize if properties are
incorrect
X-Mailer: git-send-email 2.49.0
Quoted 6 lines
Do not silently adjust
num_timers, and fail if intcap is 0.Signed-off-by Paolo Bonzini <[email protected]>--- hw/timer/hpet.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
PATCH 11/12 rust/hpet: return errors from realize if properties are incorrect
In reply to Paolo BonziniMatch the code in hpet.c; this also allows removing the BqlCell from the num_timers field.
--- rust/hw/timer/hpet/src/fw_cfg.rs | 7 +++---- rust/hw/timer/hpet/src/hpet.rs | 16 +++++++--------- 2 files changed, 10 insertions(+), 13 deletions(-)
rust/hw/timer/hpet/src/fw_cfg.rs +3 -4
@@ -36,7 +36,7 @@ unsafe impl Zeroable for HPETFwConfig {} };impl HPETFwConfig { -pub (crate )fn assign_hpet_id () ->usize { +pub (crate )fn assign_hpet_id () ->Result <usize , &'staticstr > {assert! (bql_locked ());// SAFETY: all accesses go through these methods, which guarantee // that the accesses are protected by the BQL.
@@ -48,13 +48,12 @@ pub(crate) fn assign_hpet_id() -> usize { }if fw_cfg.count ==8 { -// TODO: Add error binding: error_setg() -panic! ("Only 8 instances of HPET is allowed" ); +Err ("Only 8 instances of HPET are allowed" )?; }let id:usize = fw_cfg.count.into (); fw_cfg.count +=1 ; - id +Ok (id) }pub (crate )fn update_hpet_cfg (hpet_id:usize , timer_block_id:u32 , address:u64 ) {
rust/hw/timer/hpet/src/hpet.rs +7 -9
@@ -725,18 +725,16 @@ fn post_init(&self) { }fn realize (&self) -> qemu_api::Result <()> { +if self.num_timers.get () <HPET_MIN_TIMERS || self.num_timers.get () >HPET_MAX_TIMERS { +Err (format! ( +"hpet.num_timers must be between {HPET_MIN_TIMERS} and {HPET_MAX_TIMERS}" + ))?; + }if self.int_route_cap ==0 { -// TODO: Add error binding: warn_report() -println! ("Hpet's hpet-intcap property not initialized" ); +Err ("hpet.hpet-intcap property not initialized" )?; } - self.hpet_id.set (HPETFwConfig ::assign_hpet_id ()); - -if self.num_timers.get () <HPET_MIN_TIMERS { - self.num_timers.set (HPET_MIN_TIMERS ); - }else if self.num_timers.get () >HPET_MAX_TIMERS { - self.num_timers.set (HPET_MAX_TIMERS ); - } + self.hpet_id.set (HPETFwConfig ::assign_hpet_id ()?); self.init_timer ();// 64-bit General Capabilities and ID Register; LegacyReplacementRoute.
Signature · 1 line
2.49.0
Re: [PATCH 11/12] rust/hpet: return errors from realize if properties are incorrect
Quoted 6 lines · Paolo Bonzini
Date: Mon, 26 May 2025 16:24:54 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 11/12]rust/hpet: return errors from realize if properties
are incorrect
X-Mailer: git-send-email 2.49.0
Quoted 8 lines
Match the code in
hpet.c; this also allows removing the BqlCell from thenum_timersfield.Signed-off-by Paolo Bonzini <[email protected]>--- rust/hw/timer/hpet/src/fw_cfg.rs | 7 +++---- rust/hw/timer/hpet/src/hpet.rs | 16 +++++++--------- 2 files changed, 10 insertions(+), 13 deletions(-)
Patch is fine for me,
rust/hw/timer/hpet/src/hpet.rs fn post_init(&self) { · line 735
Quoted 5 lines
if self.int_route_cap == 0 {rust/hw/timer/hpet/src/hpet.rs +1
-2-// TODO: Add error binding: warn_report() -println! ("Hpet's hpet-intcap property not initialized" ); +Err ("hpet.hpet-intcap property not initialized" )?; }
Though here we don't need print warning...do we still need to provide the warn_report() binding? Or println!() is enough in Rust side?
Thanks,
Zhao
Re: [PATCH 11/12] rust/hpet: return errors from realize if properties are incorrect
Quoted 8 lines · Zhao Liu
rust/hw/timer/hpet/src/hpet.rs fn post_init(&self) { · line 735
Quoted 5 lines
if self.int_route_cap == 0 {rust/hw/timer/hpet/src/hpet.rs +1
-2-// TODO: Add error binding: warn_report() -println! ("Hpet's hpet-intcap property not initialized" ); +Err ("hpet.hpet-intcap property not initialized" )?; }Though here we don't need print warning...do we still need to provide the
warn_report()binding? Or println!() is enough in Rust side?
I think it will be enough to have Error ** and
LOG_GUEST_ERROR/LOG_UNIMP... but we'll see.
Paolo
PATCH 12/12 rust/hpet: Drop BqlCell wrapper for num_timers
In reply to Paolo BonziniFrom: Zhao Liu <[email protected]>
Now that the num_timers field is initialized as a property, someone may change its default value using qdev_prop_set_uint8(), but the value is fixed after the Rust code sees it first. Since there is no need to modify it after realize(), it is not to be necessary to have a BqlCell wrapper.
--- rust/hw/timer/hpet/src/hpet.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-)
rust/hw/timer/hpet/src/hpet.rs +11 -16
@@ -562,7 +562,7 @@ pub struct HPETState {/// HPET timer array managed by this timer block. #[doc(alias = "timer" )] timers: [BqlRefCell<HPETTimer>; HPET_MAX_TIMERS], - num_timers: BqlCell<usize>, + num_timers: usize, num_timers_save: BqlCell<u8>,/// Instance id (HPET timer block ID).
@@ -570,11 +570,6 @@ pub struct HPETState { }impl HPETState { -// Get num_timers with `usize` type, which is useful to play with array index. -fn get_num_timers (&self) ->usize { - self.num_timers.get () - } -const fn has_msi_flag (&self) ->bool { self.flags & (1 <<HPET_FLAG_MSI_SUPPORT_SHIFT ) !=0 }
@@ -636,7 +631,7 @@ fn set_cfg_reg(&self, shift: u32, len: u32, val: u64) { self.hpet_offset .set (ticks_to_ns (self.counter.get ()) -CLOCK_VIRTUAL .get_ns ()); -for timerin self.timers.iter ().take (self.get_num_timers ()) { +for timerin self.timers.iter ().take (self.num_timers) {let mut t = timer.borrow_mut ();if t.is_int_enabled () && t.is_int_active () {
@@ -648,7 +643,7 @@ fn set_cfg_reg(&self, shift: u32, len: u32, val: u64) {// Halt main counter and disable interrupt generation. self.counter.set (self.get_ticks ()); -for timerin self.timers.iter ().take (self.get_num_timers ()) { +for timerin self.timers.iter ().take (self.num_timers) { timer.borrow_mut ().del_timer (); } }
@@ -671,7 +666,7 @@ fn set_int_status_reg(&self, shift: u32, _len: u32, val: u64) {let new_val = val << shift;let cleared = new_val & self.int_status.get (); -for (index, timer)in self.timers.iter ().take (self.get_num_timers ()).enumerate () { +for (index, timer)in self.timers.iter ().take (self.num_timers).enumerate () {if cleared & (1 << index) !=0 { timer.borrow_mut ().update_irq (false ); }
@@ -725,7 +720,7 @@ fn post_init(&self) { }fn realize (&self) -> qemu_api::Result <()> { -if self.num_timers.get () <HPET_MIN_TIMERS || self.num_timers.get () >HPET_MAX_TIMERS { +if self.num_timers <HPET_MIN_TIMERS || self.num_timers >HPET_MAX_TIMERS {Err (format! ("hpet.num_timers must be between {HPET_MIN_TIMERS} and {HPET_MAX_TIMERS}" ))?;
@@ -743,7 +738,7 @@ fn realize(&self) -> qemu_api::Result<()> {1 <<HPET_CAP_COUNT_SIZE_CAP_SHIFT |1 <<HPET_CAP_LEG_RT_CAP_SHIFT |HPET_CAP_VENDER_ID_VALUE <<HPET_CAP_VENDER_ID_SHIFT | - ((self.get_num_timers () -1 )as u64 ) <<HPET_CAP_NUM_TIM_SHIFT |// indicate the last timer + ((self.num_timers -1 )as u64 ) <<HPET_CAP_NUM_TIM_SHIFT |// indicate the last timer (HPET_CLK_PERIOD * FS_PER_NS) << HPET_CAP_CNT_CLK_PERIOD_SHIFT, // 10 ns );
@@ -753,7 +748,7 @@ fn realize(&self) -> qemu_api::Result<()> { }fn reset_hold (&self, _type:ResetType ) { -for timerin self.timers.iter ().take (self.get_num_timers ()) { +for timerin self.timers.iter ().take (self.num_timers) { timer.borrow_mut ().reset (); }
@@ -781,7 +776,7 @@ fn decode(&self, mut addr: hwaddr, size: u32) -> HPETAddrDecode {GlobalRegister ::try_from (addr).map (HPETRegister ::Global ) }else {let timer_id:usize = ((addr -0x100 ) /0x20 )as usize ; -if timer_id <= self.get_num_timers () { +if timer_id < self.num_timers {// TODO: Add trace point - trace_hpet_ram_[read|write]_timer_id(timer_id) TimerRegister ::try_from (addr &0x18 ) .map (|reg|HPETRegister ::Timer (&self.timers[timer_id], reg))
@@ -852,12 +847,12 @@ fn pre_save(&self) -> i32 { * also added to the migration stream. Check that it matches the value * that was configured. */ - self.num_timers_save.set (self.num_timers.get ()as u8 ); + self.num_timers_save.set (self.num_timersas u8 );0 }fn post_load (&self, _version_id:u8 ) ->i32 { -for timerin self.timers.iter ().take (self.get_num_timers ()) { +for timerin self.timers.iter ().take (self.num_timers) {let mut t = timer.borrow_mut (); t.cmp64 = t.calculate_cmp64 (t.get_state ().counter.get (), t.cmp);
@@ -882,7 +877,7 @@ fn is_offset_needed(&self) -> bool { }fn validate_num_timers (&self, _version_id:u8 ) ->bool { - self.num_timers.get () == self.num_timers_save.get ().into () + self.num_timers == self.num_timers_save.get ().into () } }
Signature · 1 line
2.49.0
Re: [PATCH 12/12] rust/hpet: Drop BqlCell wrapper for num_timers
Quoted 12 lines · Paolo Bonzini
Date: Mon, 26 May 2025 16:24:55 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 12/12]rust/hpet: Drop BqlCell wrapper fornum_timers
X-Mailer: git-send-email 2.49.0From: Zhao Liu <[email protected]>
Now that the
num_timersfield is initialized as a property, someone may change its default value usingqdev_prop_set_uint8(), but the value is fixed after the Rust code sees it first. Since there is no need to modify it afterrealize(), it is not to be necessary to have a BqlCell wrapper.
Quoted 7 lines
Signed-off-by Zhao Liu <[email protected]>[Remove .into() as well. - Paolo]Signed-off-by Paolo Bonzini <[email protected]>--- rust/hw/timer/hpet/src/hpet.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-)
Thank you! Now we're finally here.