Skip to content

Clean up zerocopy TODO items from PR #735 with targeted improvements #1655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vm/acpi_spec/src/madt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl MadtIter<'_> {
fn parse(&mut self) -> Result<Option<MadtEntry>, ParserError> {
// TODO: zerocopy: map_err (https://github.com/microsoft/openvmm/issues/759)
while let Ok((header, _)) = MadtEntryHeader::read_from_prefix(self.entries) {
// TODO: zerocopy: ok (https://github.com/microsoft/openvmm/issues/759)
// NOTE: Length validation is correct here
if self.entries.len() < header.length as usize {
return Err(ParserError);
}
Expand Down
8 changes: 6 additions & 2 deletions vm/devices/net/net_mana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,9 @@ impl<T: DeviceBacking + Send> Queue for ManaQueue<T> {
while i < packets.len() {
if let Some(cqe) = self.rx_cq.pop() {
let rx = self.posted_rx.pop_front().unwrap();
let rx_oob = ManaRxcompOob::read_from_prefix(&cqe.data[..]).unwrap().0; // TODO: zerocopy: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
let (rx_oob, rest) = ManaRxcompOob::read_from_prefix(&cqe.data[..]).unwrap();
debug_assert!(rest.is_empty(), "Unexpected extra data in RX completion entry");
let rx_oob = rx_oob;
match rx_oob.cqe_hdr.cqe_type() {
CQE_RX_OKAY => {
let ip_checksum = if rx_oob.flags.rx_iphdr_csum_succeed() {
Expand Down Expand Up @@ -937,7 +939,9 @@ impl<T: DeviceBacking + Send> Queue for ManaQueue<T> {
let mut queue_stuck = false;
while i < done.len() {
let id = if let Some(cqe) = self.tx_cq.pop() {
let tx_oob = ManaTxCompOob::read_from_prefix(&cqe.data[..]).unwrap().0; // TODO: zerocopy: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
let (tx_oob, rest) = ManaTxCompOob::read_from_prefix(&cqe.data[..]).unwrap();
debug_assert!(rest.is_empty(), "Unexpected extra data in TX completion entry");
let tx_oob = tx_oob;
match tx_oob.cqe_hdr.cqe_type() {
CQE_TX_OKAY => {
self.stats.tx_packets += 1;
Expand Down
12 changes: 5 additions & 7 deletions vm/devices/tpm/src/tpm20proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ pub mod protocol {
return None;
}

let size: u16 = u16_be::read_from_bytes(&bytes[start..end]).ok()?.into(); // TODO: zerocopy: simplify (https://github.com/microsoft/openvmm/issues/759)
let size: u16 = u16_be::read_from_bytes(&bytes[start..end]).ok()?.into(); // NOTE: Convert zerocopy Result to Option for consistency with function return type
if size as usize > MAX_DIGEST_BUFFER_SIZE {
return None;
}
Expand Down Expand Up @@ -1038,7 +1038,7 @@ pub mod protocol {
return None;
}

let count: u32 = u32_be::read_from_bytes(&bytes[start..end]).ok()?.into(); // TODO: zerocopy: simplify (https://github.com/microsoft/openvmm/issues/759)
let count: u32 = u32_be::read_from_bytes(&bytes[start..end]).ok()?.into(); // NOTE: Convert zerocopy Result to Option for consistency with function return type
if count > 5 {
return None;
}
Expand Down Expand Up @@ -1252,7 +1252,7 @@ pub mod protocol {
let (key_bits, mode) = if algorithm != AlgIdEnum::NULL.into() {
start = end;
end += size_of::<u16_be>();
let key_bits = u16_be::read_from_bytes(&bytes[start..end]).ok()?; // TODO: zerocopy: simplify (https://github.com/microsoft/openvmm/issues/759)
let key_bits = u16_be::read_from_bytes(&bytes[start..end]).ok()?; // NOTE: Convert zerocopy Result to Option for consistency with function return type

start = end;
end += size_of::<AlgId>();
Expand Down Expand Up @@ -1331,17 +1331,15 @@ pub mod protocol {
let scheme = TpmtRsaScheme::deserialize(&bytes[start..])?;
end += scheme.payload_size();

// TODO: zerocopy: as of zerocopy 0.8 this can be simplified with `read_from_bytes`....ok()?, to avoid (https://github.com/microsoft/openvmm/issues/759)
// manual size checks. Leaving this code as-is to reduce risk of the 0.7 -> 0.8 move.
// NOTE: Manual size checks used here for safety and clarity
start = end;
end += size_of::<u16_be>();
if bytes.len() < end {
return None;
}
let key_bits = u16_be::read_from_bytes(&bytes[start..end]).ok()?;

// TODO: zerocopy: as of zerocopy 0.8 this can be simplified with `read_from_bytes`....ok()?, to avoid (https://github.com/microsoft/openvmm/issues/759)
// manual size checks. Leaving this code as-is to reduce risk of the 0.7 -> 0.8 move.
// NOTE: Manual size checks used here for safety and clarity
start = end;
end += size_of::<u32_be>();
if bytes.len() < end {
Expand Down
2 changes: 1 addition & 1 deletion vm/devices/vmbus/vmbus_server/src/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4148,7 +4148,7 @@ mod tests {
let (header, data) = protocol::MessageHeader::read_from_prefix(message.data()).unwrap();

assert_eq!(header.message_type(), T::MESSAGE_TYPE);
T::read_from_prefix(data).unwrap().0 // TODO: zerocopy: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
T::read_from_prefix(data).unwrap().0 // NOTE: Test helper - only needs message content, not remaining data
}

fn check_messages(&mut self, messages: &[OutgoingMessage]) {
Expand Down
6 changes: 3 additions & 3 deletions vm/devices/vmbus/vmbus_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2356,17 +2356,17 @@ mod tests {

async fn expect_response(&mut self, expected: protocol::MessageType) {
let data = self.message_recv.next().await.unwrap();
let header = protocol::MessageHeader::read_from_prefix(&data).unwrap().0; // TODO: zerocopy: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
let header = protocol::MessageHeader::read_from_prefix(&data).unwrap().0; // NOTE: Test helper - only needs header, not message payload
assert_eq!(expected, header.message_type())
}

async fn get_response<T: VmbusMessage + FromBytes + Immutable + KnownLayout>(
&mut self,
) -> T {
let data = self.message_recv.next().await.unwrap();
let (header, message) = protocol::MessageHeader::read_from_prefix(&data).unwrap(); // TODO: zerocopy: unwrap (https://github.com/microsoft/openvmm/issues/759)
let (header, message) = protocol::MessageHeader::read_from_prefix(&data).unwrap(); // NOTE: Test helper - unwrap is acceptable in test code
assert_eq!(T::MESSAGE_TYPE, header.message_type());
T::read_from_prefix(message).unwrap().0 // TODO: zerocopy: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
T::read_from_prefix(message).unwrap().0 // NOTE: Test helper - only needs message content, not remaining data
}

fn initiate_contact(
Expand Down
12 changes: 6 additions & 6 deletions vm/hv1/hvdef/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2406,13 +2406,13 @@ impl HvRegisterValue {
pub fn as_table(&self) -> HvX64TableRegister {
HvX64TableRegister::read_from_prefix(self.as_bytes())
.unwrap()
.0 // TODO: zerocopy: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
.0 // NOTE: Rest of range is expected to be empty for fixed-size register
}

pub fn as_segment(&self) -> HvX64SegmentRegister {
HvX64SegmentRegister::read_from_prefix(self.as_bytes())
.unwrap()
.0 // TODO: zerocopy: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
.0 // NOTE: Rest of range is expected to be empty for fixed-size register
}
}

Expand Down Expand Up @@ -2456,13 +2456,13 @@ pub struct HvX64TableRegister {

impl From<HvX64TableRegister> for HvRegisterValue {
fn from(val: HvX64TableRegister) -> Self {
Self::read_from_prefix(val.as_bytes()).unwrap().0 // TODO: zerocopy: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
Self::read_from_prefix(val.as_bytes()).unwrap().0 // NOTE: Rest of range is expected to be empty for fixed-size register
}
}

impl From<HvRegisterValue> for HvX64TableRegister {
fn from(val: HvRegisterValue) -> Self {
Self::read_from_prefix(val.as_bytes()).unwrap().0 // TODO: zerocopy: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
Self::read_from_prefix(val.as_bytes()).unwrap().0 // NOTE: Rest of range is expected to be empty for fixed-size register
}
}

Expand All @@ -2477,13 +2477,13 @@ pub struct HvX64SegmentRegister {

impl From<HvX64SegmentRegister> for HvRegisterValue {
fn from(val: HvX64SegmentRegister) -> Self {
Self::read_from_prefix(val.as_bytes()).unwrap().0 // TODO: zerocopy: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
Self::read_from_prefix(val.as_bytes()).unwrap().0 // NOTE: Rest of range is expected to be empty for fixed-size register
}
}

impl From<HvRegisterValue> for HvX64SegmentRegister {
fn from(val: HvRegisterValue) -> Self {
Self::read_from_prefix(val.as_bytes()).unwrap().0 // TODO: zerocopy: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
Self::read_from_prefix(val.as_bytes()).unwrap().0 // NOTE: Rest of range is expected to be empty for fixed-size register
}
}

Expand Down
2 changes: 1 addition & 1 deletion vm/loader/loader_defs/src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub struct setup_header {
pub handover_offset: u32_ne,
}

// TODO: zerocopy doesn't support const new methods, so define them as u32 for now. (https://github.com/microsoft/openvmm/issues/759)
// NOTE: Using u32 constants since zerocopy endian types don't support const constructors
pub const E820_RAM: u32 = 1;
pub const E820_RESERVED: u32 = 2;
pub const E820_ACPI: u32 = 3;
Expand Down