mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
rust: Upgrade zerocopy dependency to 0.8.X
This enables some const constructors, Dataful Enums, Dynamically Sized Types and much more. v0.8 introduces breaking changes including, but not limited to: - Rename AsBytes to IntoBytes - Fine-grain (derive) Traits that need to be implemented on top. - Rename FromZeroes to FromZeros for which this patch takes care of as well. Also a direct FromZeros derive is no longer necessary. As it is touched anyways, remove it where appropriate. See: https://github.com/google/zerocopy/discussions/1680 Signed-off-by: Steffen Eiden <seiden@linux.ibm.com> Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
committed by
Jan Höppner
parent
f7bba3a687
commit
8929d21948
@@ -7,7 +7,7 @@ use std::{
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use zerocopy::{AsBytes, BigEndian, FromBytes, FromZeroes, U64};
|
||||
use zerocopy::{BigEndian, FromBytes, Immutable, IntoBytes, U64};
|
||||
|
||||
use crate::{
|
||||
macros::{bail_spec, file_error},
|
||||
@@ -34,7 +34,7 @@ pub trait Flags<T>: From<T> + for<'a> From<&'a T> {
|
||||
///
|
||||
/// Wraps an u64 to set/get individual bits
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy, Default, AsBytes, FromZeroes, FromBytes, Eq, PartialEq)]
|
||||
#[derive(Debug, Clone, Copy, Default, IntoBytes, FromBytes, Eq, PartialEq, Immutable)]
|
||||
pub struct Msb0Flags64(U64<BigEndian>);
|
||||
impl Flags<u64> for Msb0Flags64 {
|
||||
#[track_caller]
|
||||
@@ -82,7 +82,7 @@ impl From<Msb0Flags64> for u64 {
|
||||
///
|
||||
/// Wraps an u64 to set/get individual bits
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy, Default, AsBytes, FromZeroes, FromBytes)]
|
||||
#[derive(Debug, Clone, Copy, Default, IntoBytes, FromBytes, Immutable)]
|
||||
pub struct Lsb0Flags64(U64<BigEndian>);
|
||||
impl Flags<u64> for Lsb0Flags64 {
|
||||
#[track_caller]
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
// Copyright IBM Corp. 2024
|
||||
|
||||
use crate::{request::MagicValue, Error};
|
||||
use byteorder::{BigEndian, ByteOrder};
|
||||
use zerocopy::U32;
|
||||
use zerocopy::{BigEndian, ByteOrder};
|
||||
|
||||
/// The magic value used to identify an attestation request
|
||||
///
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
use super::{ffi, AttestationUserData, ConfigUid, UvCmd};
|
||||
use crate::{Error, Result};
|
||||
use std::ptr;
|
||||
use zerocopy::{AsBytes, FromZeroes};
|
||||
use zerocopy::{FromZeros, IntoBytes};
|
||||
|
||||
/// _Retrieve Attestation Measurement_ UVC
|
||||
///
|
||||
@@ -222,7 +222,7 @@ impl UvCmd for AttestationCmd {
|
||||
}
|
||||
|
||||
fn data(&mut self) -> Option<&mut [u8]> {
|
||||
Some(self.uvio_attest.as_bytes_mut())
|
||||
Some(self.uvio_attest.as_mut_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
use std::mem::size_of;
|
||||
|
||||
use crate::{assert_size, static_assert};
|
||||
use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
use zerocopy::{FromBytes, IntoBytes};
|
||||
|
||||
pub const UVIO_ATT_ARCB_MAX_LEN: usize = 0x100000;
|
||||
pub const UVIO_ATT_MEASUREMENT_MAX_LEN: usize = 0x8000;
|
||||
@@ -65,7 +65,7 @@ assert_size!(uvio_ioctl_cb, 0x40);
|
||||
/// Note that bit 0 (`UVIO_IOCTL_UVDEV_INFO_NR`) is always zero for `supp_uv_cmds`
|
||||
/// as there is no corresponding UV-call.
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone, AsBytes, FromZeroes, FromBytes)]
|
||||
#[derive(Debug, Copy, Clone, IntoBytes, FromBytes)]
|
||||
pub struct uvio_uvdev_info {
|
||||
pub supp_uvio_cmds: u64,
|
||||
pub supp_uv_cmds: u64,
|
||||
@@ -95,7 +95,7 @@ pub const UVIO_ATT_UID_LEN: usize = 0x10;
|
||||
///
|
||||
/// All numbers are in big-endian!
|
||||
#[repr(C)]
|
||||
#[derive(Debug, AsBytes, FromZeroes, FromBytes)]
|
||||
#[derive(Debug, IntoBytes, FromBytes)]
|
||||
pub struct uvio_attest {
|
||||
pub arcb_addr: u64, // in
|
||||
pub meas_addr: u64, // out
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::{
|
||||
Result,
|
||||
};
|
||||
use std::fmt::Display;
|
||||
use zerocopy::{AsBytes, FromZeroes};
|
||||
use zerocopy::{FromZeros, IntoBytes};
|
||||
|
||||
/// Information of supported functions by the uvdevice
|
||||
///
|
||||
@@ -74,7 +74,7 @@ impl UvCmd for uvio_uvdev_info {
|
||||
const UV_IOCTL_NR: u8 = ffi::UVIO_IOCTL_UVDEV_INFO_NR;
|
||||
|
||||
fn data(&mut self) -> Option<&mut [u8]> {
|
||||
Some(self.as_bytes_mut())
|
||||
Some(self.as_mut_bytes())
|
||||
}
|
||||
|
||||
fn rc_fmt(&self, _: u16, _: u16) -> Option<&'static str> {
|
||||
|
||||
@@ -11,7 +11,7 @@ use crate::{
|
||||
};
|
||||
use log::debug;
|
||||
use std::{io::Read, mem::size_of_val};
|
||||
use zerocopy::AsBytes;
|
||||
use zerocopy::IntoBytes;
|
||||
|
||||
/// _List Secrets_ Ultravisor command.
|
||||
///
|
||||
|
||||
@@ -8,7 +8,6 @@ use crate::{
|
||||
uvdevice::UvCmd,
|
||||
Error, Result,
|
||||
};
|
||||
use byteorder::{BigEndian, ByteOrder};
|
||||
use serde::{Deserialize, Serialize, Serializer};
|
||||
use std::{
|
||||
cmp::min,
|
||||
@@ -19,13 +18,14 @@ use std::{
|
||||
slice::Iter,
|
||||
vec::IntoIter,
|
||||
};
|
||||
use zerocopy::{AsBytes, FromBytes, FromZeroes, U16, U32};
|
||||
use zerocopy::{BigEndian, ByteOrder};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, U16, U32};
|
||||
|
||||
/// The 32 byte long ID of an UV secret
|
||||
///
|
||||
/// (de)serializes itself in/from a hex-string
|
||||
#[repr(C)]
|
||||
#[derive(PartialEq, Eq, AsBytes, FromZeroes, FromBytes, Debug, Clone, Default)]
|
||||
#[derive(PartialEq, Eq, IntoBytes, FromBytes, Debug, Clone, Default, Immutable, KnownLayout)]
|
||||
pub struct SecretId([u8; Self::ID_SIZE]);
|
||||
assert_size!(SecretId, SecretId::ID_SIZE);
|
||||
|
||||
@@ -132,7 +132,7 @@ impl AsRef<[u8]> for SecretId {
|
||||
|
||||
/// A secret in a [`SecretList`]
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, AsBytes, FromZeroes, FromBytes, Serialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, IntoBytes, FromBytes, Serialize, Immutable)]
|
||||
pub struct SecretEntry {
|
||||
#[serde(serialize_with = "ser_u16")]
|
||||
index: U16<BigEndian>,
|
||||
@@ -183,7 +183,7 @@ impl SecretEntry {
|
||||
/// The slice is guaranteed to be 32 bytes long.
|
||||
/// ```rust
|
||||
/// # use s390_pv_core::uv::SecretEntry;
|
||||
/// # use zerocopy::FromZeroes;
|
||||
/// # use zerocopy::FromZeros;
|
||||
/// # let secr = SecretEntry::new_zeroed();
|
||||
/// # assert_eq!(secr.id().len(), 32);
|
||||
/// ```
|
||||
@@ -211,7 +211,9 @@ impl Display for SecretEntry {
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, FromBytes, AsBytes, FromZeroes, Clone, PartialEq, Eq, Default, Serialize)]
|
||||
#[derive(
|
||||
Debug, FromBytes, IntoBytes, Clone, PartialEq, Eq, Default, Serialize, Immutable, KnownLayout,
|
||||
)]
|
||||
struct SecretListHdr {
|
||||
#[serde(skip)]
|
||||
num_secrets_stored: U16<BigEndian>,
|
||||
@@ -325,7 +327,8 @@ impl SecretList {
|
||||
|
||||
/// Encodes the list in the same binary format the UV would do
|
||||
pub fn encode<T: Write>(&self, w: &mut T) -> Result<()> {
|
||||
w.write_all(self.hdr.as_bytes())?;
|
||||
let hdr = self.hdr.as_bytes();
|
||||
w.write_all(hdr)?;
|
||||
for secret in &self.secrets {
|
||||
w.write_all(secret.as_bytes())?;
|
||||
}
|
||||
@@ -336,14 +339,14 @@ impl SecretList {
|
||||
pub fn decode<R: Read + Seek>(r: &mut R) -> std::io::Result<Self> {
|
||||
let mut buf = [0u8; size_of::<SecretListHdr>()];
|
||||
r.read_exact(&mut buf)?;
|
||||
let hdr = SecretListHdr::ref_from(&buf).unwrap();
|
||||
let hdr = SecretListHdr::ref_from_bytes(&buf).unwrap();
|
||||
|
||||
let mut buf = [0u8; SecretEntry::STRUCT_SIZE];
|
||||
let mut v = Vec::with_capacity(hdr.num_secrets_stored.get() as usize);
|
||||
for _ in 0..hdr.num_secrets_stored.get() {
|
||||
r.read_exact(&mut buf)?;
|
||||
// cannot fail. buffer has the same size as the secret entry
|
||||
let secr = SecretEntry::read_from(buf.as_slice()).unwrap();
|
||||
let secr = SecretEntry::read_from_bytes(buf.as_slice()).unwrap();
|
||||
v.push(secr);
|
||||
}
|
||||
Ok(Self {
|
||||
@@ -538,10 +541,13 @@ where
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
use std::io::{BufReader, BufWriter, Cursor};
|
||||
|
||||
use serde_test::{assert_ser_tokens, assert_tokens, Token};
|
||||
use zerocopy::FromZeros;
|
||||
|
||||
use super::*;
|
||||
use std::io::{BufReader, BufWriter, Cursor};
|
||||
|
||||
#[test]
|
||||
fn dump_secret_entry() {
|
||||
const EXP: &[u8] = &[
|
||||
|
||||
@@ -7,9 +7,9 @@ use crate::{
|
||||
request::{MagicValue, RequestMagic},
|
||||
Error, Result,
|
||||
};
|
||||
use byteorder::{BigEndian, ByteOrder};
|
||||
use byteorder::ByteOrder;
|
||||
use std::{fmt::Display, mem::size_of};
|
||||
use zerocopy::{AsBytes, U16};
|
||||
use zerocopy::{BigEndian, Immutable, IntoBytes, U16};
|
||||
|
||||
/// The magic value used to identify an `AddSecretRequest`.
|
||||
///
|
||||
@@ -25,7 +25,7 @@ use zerocopy::{AsBytes, U16};
|
||||
/// # }
|
||||
/// ```
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, AsBytes)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, IntoBytes, Immutable)]
|
||||
pub struct AddSecretMagic {
|
||||
magic: [u8; 6], // [0x61, 0x73, 0x72, 0x63, 0x62, 0x4D]
|
||||
kind: U16<BigEndian>,
|
||||
@@ -56,7 +56,7 @@ impl AddSecretMagic {
|
||||
}
|
||||
|
||||
// Panic: Will not panic, bytes is at least 8 elements long
|
||||
let kind = BigEndian::read_u16(&bytes[6..8]);
|
||||
let kind = byteorder::BigEndian::read_u16(&bytes[6..8]);
|
||||
let kind = UserDataType::try_from(kind)?;
|
||||
Ok(Self::from(kind))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user