mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
arch: Helper functions for u32 hex (de-) serialization
These helper functions will later be used to (de-) serialize CPUID leaves and MSR register addresses. Signed-off-by: Oliver Anderson <oliver.anderson@cyberus-technology.de> On-behalf-of: SAP oliver.anderson@sap.com
This commit is contained in:
committed by
Rob Bradford
parent
110c295edf
commit
7700f4b585
@@ -27,6 +27,7 @@ vmm-sys-util = { workspace = true, features = ["with-serde"] }
|
||||
|
||||
[dev-dependencies]
|
||||
proptest = "1.0.0"
|
||||
serde_json = { workspace = true }
|
||||
|
||||
[target.'cfg(any(target_arch = "aarch64", target_arch = "riscv64"))'.dependencies]
|
||||
fdt_parser = { version = "0.1.5", package = "fdt" }
|
||||
|
||||
80
arch/src/x86_64/helpers.rs
Normal file
80
arch/src/x86_64/helpers.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
// Copyright © 2026 Cyberus Technology GmbH
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
use serde::{Deserialize, Deserializer, Serializer};
|
||||
|
||||
/// Serializes the given `input` as a hex string (starting with "0x").
|
||||
///
|
||||
/// As an example if `input:=5` then this function will feed the given
|
||||
/// `serializer` the string "0x5".
|
||||
pub(crate) fn serialize_u32_hex<S: Serializer>(
|
||||
input: &u32,
|
||||
serializer: S,
|
||||
) -> std::result::Result<S::Ok, S::Error> {
|
||||
serializer.serialize_str(&format!("{input:#x}"))
|
||||
}
|
||||
|
||||
/// Deserializes a u32 from a hex string representation.
|
||||
pub(crate) fn deserialize_u32_hex<'de, D: Deserializer<'de>>(
|
||||
deserializer: D,
|
||||
) -> std::result::Result<u32, D::Error> {
|
||||
let hex: &str = <&str>::deserialize(deserializer)?;
|
||||
u32::from_str_radix(hex.strip_prefix("0x").unwrap_or(""), 16).map_err(|_| {
|
||||
<D::Error as serde::de::Error>::custom(format!("{hex} is not a hex encoded 32 bit integer"))
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod unit_tests {
|
||||
use proptest::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
|
||||
struct TestStruct {
|
||||
#[serde(
|
||||
serialize_with = "serialize_u32_hex",
|
||||
deserialize_with = "deserialize_u32_hex"
|
||||
)]
|
||||
foo: u32,
|
||||
#[serde(
|
||||
serialize_with = "serialize_u32_hex",
|
||||
deserialize_with = "deserialize_u32_hex"
|
||||
)]
|
||||
bar: u32,
|
||||
}
|
||||
|
||||
// Check that our hex serializers satisfy the two following invariants
|
||||
// 1. Serialization followed by deserialization is the identity.
|
||||
// 2. Values of type u32 are serialized to strings starting with "0x" and then
|
||||
// a sub-string where all characters are ascii hex digits (with the letters [a-f] always in lowercase).
|
||||
proptest! {
|
||||
#[test]
|
||||
fn hex_serialization_works(foo in any::<u32>(), bar in any::<u32>()) {
|
||||
let t = TestStruct { foo , bar };
|
||||
|
||||
let t_string = serde_json::to_string(&t).unwrap();
|
||||
let t_deserialized = serde_json::from_str(&t_string).unwrap();
|
||||
prop_assert_eq!(t, t_deserialized);
|
||||
|
||||
let t_json = serde_json::to_value(t).unwrap();
|
||||
|
||||
let check_str_invariants = |value: &str| {
|
||||
prop_assert!(value.starts_with("0x"));
|
||||
prop_assert!(value.as_bytes()[2..].iter().all(u8::is_ascii_hexdigit));
|
||||
prop_assert!(!value.as_bytes()[2..].iter().any(u8::is_ascii_uppercase));
|
||||
Ok(())
|
||||
};
|
||||
|
||||
let foo_str = t_json.get("foo").unwrap().as_str().unwrap();
|
||||
let bar_str = t_json.get("bar").unwrap().as_str().unwrap();
|
||||
|
||||
check_str_invariants(foo_str)?;
|
||||
check_str_invariants(bar_str)?;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ pub mod regs;
|
||||
#[cfg(feature = "tdx")]
|
||||
pub mod tdx;
|
||||
|
||||
mod helpers;
|
||||
mod mpspec;
|
||||
mod mptable;
|
||||
mod smbios;
|
||||
@@ -21,6 +22,7 @@ mod smbios;
|
||||
use std::arch::x86_64;
|
||||
use std::mem;
|
||||
|
||||
use helpers::{deserialize_u32_hex, serialize_u32_hex};
|
||||
use hypervisor::arch::x86::{CPUID_FLAG_VALID_INDEX, CpuIdEntry};
|
||||
use hypervisor::{CpuVendor, HypervisorCpuError, HypervisorError};
|
||||
use linux_loader::loader::bootparam::{boot_params, setup_header};
|
||||
|
||||
Reference in New Issue
Block a user