mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-07-27 04:13:53 -06:00
rust: introduce a c_str macro
This allows CStr constants to be defined easily on Rust 1.63.0, while checking that there are no embedded NULs. c"" literals were only stabilized in Rust 1.77.0. Reviewed-by: Zhao Liu <zhao1.liu@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
9f7d4520d6
commit
718e255f0a
8 changed files with 78 additions and 19 deletions
|
@ -10,6 +10,7 @@ use std::{
|
||||||
|
|
||||||
use qemu_api::{
|
use qemu_api::{
|
||||||
bindings::{self, *},
|
bindings::{self, *},
|
||||||
|
c_str,
|
||||||
definitions::ObjectImpl,
|
definitions::ObjectImpl,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -135,7 +136,7 @@ impl PL011State {
|
||||||
/// location/instance. All its fields are expected to hold unitialized
|
/// location/instance. All its fields are expected to hold unitialized
|
||||||
/// values with the sole exception of `parent_obj`.
|
/// values with the sole exception of `parent_obj`.
|
||||||
unsafe fn init(&mut self) {
|
unsafe fn init(&mut self) {
|
||||||
const CLK_NAME: &CStr = c"clk";
|
const CLK_NAME: &CStr = c_str!("clk");
|
||||||
|
|
||||||
let dev = addr_of_mut!(*self).cast::<DeviceState>();
|
let dev = addr_of_mut!(*self).cast::<DeviceState>();
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
|
@ -598,7 +599,7 @@ pub unsafe extern "C" fn pl011_create(
|
||||||
let dev: *mut DeviceState = qdev_new(PL011State::TYPE_INFO.name);
|
let dev: *mut DeviceState = qdev_new(PL011State::TYPE_INFO.name);
|
||||||
let sysbus: *mut SysBusDevice = dev.cast::<SysBusDevice>();
|
let sysbus: *mut SysBusDevice = dev.cast::<SysBusDevice>();
|
||||||
|
|
||||||
qdev_prop_set_chr(dev, c"chardev".as_ptr(), chr);
|
qdev_prop_set_chr(dev, c_str!("chardev").as_ptr(), chr);
|
||||||
sysbus_realize_and_unref(sysbus, addr_of!(error_fatal) as *mut *mut Error);
|
sysbus_realize_and_unref(sysbus, addr_of!(error_fatal) as *mut *mut Error);
|
||||||
sysbus_mmio_map(sysbus, 0, addr);
|
sysbus_mmio_map(sysbus, 0, addr);
|
||||||
sysbus_connect_irq(sysbus, 0, irq);
|
sysbus_connect_irq(sysbus, 0, irq);
|
||||||
|
|
|
@ -2,14 +2,12 @@
|
||||||
// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
|
// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
use core::{
|
use core::ptr::NonNull;
|
||||||
ffi::{c_int, c_void},
|
use std::os::raw::{c_int, c_void};
|
||||||
ptr::NonNull,
|
|
||||||
};
|
|
||||||
|
|
||||||
use qemu_api::{
|
use qemu_api::{
|
||||||
bindings::*, vmstate_clock, vmstate_fields, vmstate_int32, vmstate_subsections, vmstate_uint32,
|
bindings::*, c_str, vmstate_clock, vmstate_fields, vmstate_int32, vmstate_subsections,
|
||||||
vmstate_uint32_array, vmstate_unused, zeroable::Zeroable,
|
vmstate_uint32, vmstate_uint32_array, vmstate_unused, zeroable::Zeroable,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::device::{PL011State, PL011_FIFO_DEPTH};
|
use crate::device::{PL011State, PL011_FIFO_DEPTH};
|
||||||
|
@ -24,7 +22,7 @@ extern "C" fn pl011_clock_needed(opaque: *mut c_void) -> bool {
|
||||||
|
|
||||||
/// Migration subsection for [`PL011State`] clock.
|
/// Migration subsection for [`PL011State`] clock.
|
||||||
pub static VMSTATE_PL011_CLOCK: VMStateDescription = VMStateDescription {
|
pub static VMSTATE_PL011_CLOCK: VMStateDescription = VMStateDescription {
|
||||||
name: c"pl011/clock".as_ptr(),
|
name: c_str!("pl011/clock").as_ptr(),
|
||||||
version_id: 1,
|
version_id: 1,
|
||||||
minimum_version_id: 1,
|
minimum_version_id: 1,
|
||||||
needed: Some(pl011_clock_needed),
|
needed: Some(pl011_clock_needed),
|
||||||
|
@ -48,7 +46,7 @@ extern "C" fn pl011_post_load(opaque: *mut c_void, version_id: c_int) -> c_int {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static VMSTATE_PL011: VMStateDescription = VMStateDescription {
|
pub static VMSTATE_PL011: VMStateDescription = VMStateDescription {
|
||||||
name: c"pl011".as_ptr(),
|
name: c_str!("pl011").as_ptr(),
|
||||||
version_id: 2,
|
version_id: 2,
|
||||||
minimum_version_id: 2,
|
minimum_version_id: 2,
|
||||||
post_load: Some(pl011_post_load),
|
post_load: Some(pl011_post_load),
|
||||||
|
@ -79,14 +77,14 @@ pub static VMSTATE_PL011: VMStateDescription = VMStateDescription {
|
||||||
qemu_api::declare_properties! {
|
qemu_api::declare_properties! {
|
||||||
PL011_PROPERTIES,
|
PL011_PROPERTIES,
|
||||||
qemu_api::define_property!(
|
qemu_api::define_property!(
|
||||||
c"chardev",
|
c_str!("chardev"),
|
||||||
PL011State,
|
PL011State,
|
||||||
char_backend,
|
char_backend,
|
||||||
unsafe { &qdev_prop_chr },
|
unsafe { &qdev_prop_chr },
|
||||||
CharBackend
|
CharBackend
|
||||||
),
|
),
|
||||||
qemu_api::define_property!(
|
qemu_api::define_property!(
|
||||||
c"migrate-clk",
|
c_str!("migrate-clk"),
|
||||||
PL011State,
|
PL011State,
|
||||||
migrate_clock,
|
migrate_clock,
|
||||||
unsafe { &qdev_prop_bool },
|
unsafe { &qdev_prop_bool },
|
||||||
|
|
|
@ -42,12 +42,14 @@ extern crate bilge;
|
||||||
extern crate bilge_impl;
|
extern crate bilge_impl;
|
||||||
extern crate qemu_api;
|
extern crate qemu_api;
|
||||||
|
|
||||||
|
use qemu_api::c_str;
|
||||||
|
|
||||||
pub mod device;
|
pub mod device;
|
||||||
pub mod device_class;
|
pub mod device_class;
|
||||||
pub mod memory_ops;
|
pub mod memory_ops;
|
||||||
|
|
||||||
pub const TYPE_PL011: &::std::ffi::CStr = c"pl011";
|
pub const TYPE_PL011: &::std::ffi::CStr = c_str!("pl011");
|
||||||
pub const TYPE_PL011_LUMINARY: &::std::ffi::CStr = c"pl011_luminary";
|
pub const TYPE_PL011_LUMINARY: &::std::ffi::CStr = c_str!("pl011_luminary");
|
||||||
|
|
||||||
/// Offset of each register from the base memory address of the device.
|
/// Offset of each register from the base memory address of the device.
|
||||||
///
|
///
|
||||||
|
|
|
@ -3,6 +3,7 @@ _qemu_api_rs = static_library(
|
||||||
structured_sources(
|
structured_sources(
|
||||||
[
|
[
|
||||||
'src/lib.rs',
|
'src/lib.rs',
|
||||||
|
'src/c_str.rs',
|
||||||
'src/definitions.rs',
|
'src/definitions.rs',
|
||||||
'src/device_class.rs',
|
'src/device_class.rs',
|
||||||
'src/vmstate.rs',
|
'src/vmstate.rs',
|
||||||
|
@ -18,6 +19,9 @@ _qemu_api_rs = static_library(
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
rust.test('rust-qemu-api-tests', _qemu_api_rs,
|
||||||
|
suite: ['unit', 'rust'])
|
||||||
|
|
||||||
qemu_api = declare_dependency(
|
qemu_api = declare_dependency(
|
||||||
link_with: _qemu_api_rs,
|
link_with: _qemu_api_rs,
|
||||||
dependencies: qemu_api_macros,
|
dependencies: qemu_api_macros,
|
||||||
|
|
53
rust/qemu-api/src/c_str.rs
Normal file
53
rust/qemu-api/src/c_str.rs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
// Copyright 2024 Red Hat, Inc.
|
||||||
|
// Author(s): Paolo Bonzini <pbonzini@redhat.com>
|
||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
/// Given a string constant _without_ embedded or trailing NULs, return
|
||||||
|
/// a `CStr`.
|
||||||
|
///
|
||||||
|
/// Needed for compatibility with Rust <1.77.
|
||||||
|
macro_rules! c_str {
|
||||||
|
($str:expr) => {{
|
||||||
|
const STRING: &str = concat!($str, "\0");
|
||||||
|
const BYTES: &[u8] = STRING.as_bytes();
|
||||||
|
|
||||||
|
// "for" is not allowed in const context... oh well,
|
||||||
|
// everybody loves some lisp. This could be turned into
|
||||||
|
// a procedural macro if this is a problem; alternatively
|
||||||
|
// Rust 1.72 makes CStr::from_bytes_with_nul a const function.
|
||||||
|
const fn f(b: &[u8], i: usize) {
|
||||||
|
if i == b.len() - 1 {
|
||||||
|
} else if b[i] == 0 {
|
||||||
|
panic!("c_str argument contains NUL")
|
||||||
|
} else {
|
||||||
|
f(b, i + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f(BYTES, 0);
|
||||||
|
|
||||||
|
// SAFETY: absence of NULs apart from the final byte was checked above
|
||||||
|
unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(BYTES) }
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::ffi::CStr;
|
||||||
|
|
||||||
|
use crate::c_str;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cstr_macro() {
|
||||||
|
let good = c_str!("🦀");
|
||||||
|
let good_bytes = b"\xf0\x9f\xa6\x80\0";
|
||||||
|
assert_eq!(good.to_bytes_with_nul(), good_bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cstr_macro_const() {
|
||||||
|
const GOOD: &CStr = c_str!("🦀");
|
||||||
|
const GOOD_BYTES: &[u8] = b"\xf0\x9f\xa6\x80\0";
|
||||||
|
assert_eq!(GOOD.to_bytes_with_nul(), GOOD_BYTES);
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,6 +29,7 @@ unsafe impl Sync for bindings::VMStateDescription {}
|
||||||
unsafe impl Sync for bindings::VMStateField {}
|
unsafe impl Sync for bindings::VMStateField {}
|
||||||
unsafe impl Sync for bindings::VMStateInfo {}
|
unsafe impl Sync for bindings::VMStateInfo {}
|
||||||
|
|
||||||
|
pub mod c_str;
|
||||||
pub mod definitions;
|
pub mod definitions;
|
||||||
pub mod device_class;
|
pub mod device_class;
|
||||||
pub mod vmstate;
|
pub mod vmstate;
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
macro_rules! vmstate_unused_buffer {
|
macro_rules! vmstate_unused_buffer {
|
||||||
($field_exists_fn:expr, $version_id:expr, $size:expr) => {{
|
($field_exists_fn:expr, $version_id:expr, $size:expr) => {{
|
||||||
$crate::bindings::VMStateField {
|
$crate::bindings::VMStateField {
|
||||||
name: c"unused".as_ptr(),
|
name: c_str!("unused").as_ptr(),
|
||||||
err_hint: ::core::ptr::null(),
|
err_hint: ::core::ptr::null(),
|
||||||
offset: 0,
|
offset: 0,
|
||||||
size: $size,
|
size: $size,
|
||||||
|
|
|
@ -6,7 +6,7 @@ use std::{ffi::CStr, os::raw::c_void};
|
||||||
|
|
||||||
use qemu_api::{
|
use qemu_api::{
|
||||||
bindings::*,
|
bindings::*,
|
||||||
declare_properties, define_property,
|
c_str, declare_properties, define_property,
|
||||||
definitions::{Class, ObjectImpl},
|
definitions::{Class, ObjectImpl},
|
||||||
device_class_init,
|
device_class_init,
|
||||||
zeroable::Zeroable,
|
zeroable::Zeroable,
|
||||||
|
@ -16,7 +16,7 @@ use qemu_api::{
|
||||||
fn test_device_decl_macros() {
|
fn test_device_decl_macros() {
|
||||||
// Test that macros can compile.
|
// Test that macros can compile.
|
||||||
pub static VMSTATE: VMStateDescription = VMStateDescription {
|
pub static VMSTATE: VMStateDescription = VMStateDescription {
|
||||||
name: c"name".as_ptr(),
|
name: c_str!("name").as_ptr(),
|
||||||
unmigratable: true,
|
unmigratable: true,
|
||||||
..Zeroable::ZERO
|
..Zeroable::ZERO
|
||||||
};
|
};
|
||||||
|
@ -36,7 +36,7 @@ fn test_device_decl_macros() {
|
||||||
declare_properties! {
|
declare_properties! {
|
||||||
DUMMY_PROPERTIES,
|
DUMMY_PROPERTIES,
|
||||||
define_property!(
|
define_property!(
|
||||||
c"migrate-clk",
|
c_str!("migrate-clk"),
|
||||||
DummyState,
|
DummyState,
|
||||||
migrate_clock,
|
migrate_clock,
|
||||||
unsafe { &qdev_prop_bool },
|
unsafe { &qdev_prop_bool },
|
||||||
|
@ -55,7 +55,7 @@ fn test_device_decl_macros() {
|
||||||
impl ObjectImpl for DummyState {
|
impl ObjectImpl for DummyState {
|
||||||
type Class = DummyClass;
|
type Class = DummyClass;
|
||||||
const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
|
const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
|
||||||
const TYPE_NAME: &'static CStr = c"dummy";
|
const TYPE_NAME: &'static CStr = c_str!("dummy");
|
||||||
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_DEVICE);
|
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_DEVICE);
|
||||||
const ABSTRACT: bool = false;
|
const ABSTRACT: bool = false;
|
||||||
const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
|
const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue