Skip to content

Commit 9cba328

Browse files
Serban Iorgaalxiord
authored andcommitted
[memory_model] rename DataInit to ByteValued
Signed-off-by: Serban Iorga <[email protected]>
1 parent 7a51067 commit 9cba328

File tree

9 files changed

+89
-77
lines changed

9 files changed

+89
-77
lines changed

src/arch/src/x86_64/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ pub mod regs;
1717
use std::mem;
1818

1919
use arch_gen::x86::bootparam::{boot_params, E820_RAM};
20-
use memory_model::{Address, DataInit, GuestAddress, GuestMemory};
20+
use memory_model::{Address, ByteValued, GuestAddress, GuestMemory};
2121

2222
// This is a workaround to the Rust enforcement specifying that any implementation of a foreign
23-
// trait (in this case `DataInit`) where:
23+
// trait (in this case `ByteValued`) where:
2424
// * the type that is implementing the trait is foreign or
2525
// * all of the parameters being passed to the trait (if there are any) are also foreign
2626
// is prohibited.
27-
#[derive(Copy, Clone)]
27+
#[derive(Copy, Clone, Default)]
2828
struct BootParamsWrapper(boot_params);
2929

3030
// It is safe to initialize BootParamsWrap which is a wrapper over `boot_params` (a series of ints).
31-
unsafe impl DataInit for BootParamsWrapper {}
31+
unsafe impl ByteValued for BootParamsWrapper {}
3232

3333
/// Errors thrown while configuring x86_64 system.
3434
#[derive(Debug, PartialEq)]

src/arch/src/x86_64/mptable.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,36 @@ use std::slice;
1313
use libc::c_char;
1414

1515
use arch_gen::x86::mpspec;
16-
use memory_model::{Address, DataInit, GuestAddress, GuestMemory};
16+
use memory_model::{Address, ByteValued, GuestAddress, GuestMemory};
1717

1818
// This is a workaround to the Rust enforcement specifying that any implementation of a foreign
19-
// trait (in this case `DataInit`) where:
19+
// trait (in this case `ByteValued`) where:
2020
// * the type that is implementing the trait is foreign or
2121
// * all of the parameters being passed to the trait (if there are any) are also foreign
2222
// is prohibited.
23-
#[derive(Copy, Clone)]
23+
#[derive(Copy, Clone, Default)]
2424
struct MpcBusWrapper(mpspec::mpc_bus);
25-
#[derive(Copy, Clone)]
25+
#[derive(Copy, Clone, Default)]
2626
struct MpcCpuWrapper(mpspec::mpc_cpu);
27-
#[derive(Copy, Clone)]
27+
#[derive(Copy, Clone, Default)]
2828
struct MpcIntsrcWrapper(mpspec::mpc_intsrc);
29-
#[derive(Copy, Clone)]
29+
#[derive(Copy, Clone, Default)]
3030
struct MpcIoapicWrapper(mpspec::mpc_ioapic);
31-
#[derive(Copy, Clone)]
31+
#[derive(Copy, Clone, Default)]
3232
struct MpcTableWrapper(mpspec::mpc_table);
33-
#[derive(Copy, Clone)]
33+
#[derive(Copy, Clone, Default)]
3434
struct MpcLintsrcWrapper(mpspec::mpc_lintsrc);
35-
#[derive(Copy, Clone)]
35+
#[derive(Copy, Clone, Default)]
3636
struct MpfIntelWrapper(mpspec::mpf_intel);
3737

3838
// These `mpspec` wrapper types are only data, reading them from data is a safe initialization.
39-
unsafe impl DataInit for MpcBusWrapper {}
40-
unsafe impl DataInit for MpcCpuWrapper {}
41-
unsafe impl DataInit for MpcIntsrcWrapper {}
42-
unsafe impl DataInit for MpcIoapicWrapper {}
43-
unsafe impl DataInit for MpcTableWrapper {}
44-
unsafe impl DataInit for MpcLintsrcWrapper {}
45-
unsafe impl DataInit for MpfIntelWrapper {}
39+
unsafe impl ByteValued for MpcBusWrapper {}
40+
unsafe impl ByteValued for MpcCpuWrapper {}
41+
unsafe impl ByteValued for MpcIntsrcWrapper {}
42+
unsafe impl ByteValued for MpcIoapicWrapper {}
43+
unsafe impl ByteValued for MpcTableWrapper {}
44+
unsafe impl ByteValued for MpcLintsrcWrapper {}
45+
unsafe impl ByteValued for MpfIntelWrapper {}
4646

4747
// MPTABLE, describing VCPUS.
4848
const MPTABLE_START: u64 = 0x9fc00;

src/devices/src/virtio/block.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::sync::mpsc;
1818
use std::sync::Arc;
1919

2020
use logger::{Metric, METRICS};
21-
use memory_model::{DataInit, GuestAddress, GuestMemory, GuestMemoryError};
21+
use memory_model::{ByteValued, GuestAddress, GuestMemory, GuestMemoryError};
2222
use rate_limiter::{RateLimiter, TokenType};
2323
use utils::eventfd::EventFd;
2424
use virtio_gen::virtio_blk::*;
@@ -155,7 +155,7 @@ struct Request {
155155
///
156156
/// The header simplifies reading the request from memory as all request follow
157157
/// the same memory layout.
158-
#[derive(Copy, Clone)]
158+
#[derive(Copy, Clone, Default)]
159159
#[repr(C)]
160160
struct RequestHeader {
161161
request_type: u32,
@@ -164,7 +164,7 @@ struct RequestHeader {
164164
}
165165

166166
// Safe because RequestHeader only contains plain data.
167-
unsafe impl DataInit for RequestHeader {}
167+
unsafe impl ByteValued for RequestHeader {}
168168

169169
impl RequestHeader {
170170
/// Reads the request header from GuestMemory starting at `addr`.

src/devices/src/virtio/queue.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::cmp::min;
99
use std::num::Wrapping;
1010
use std::sync::atomic::{fence, Ordering};
1111

12-
use memory_model::{Address, DataInit, GuestAddress, GuestMemory};
12+
use memory_model::{Address, ByteValued, GuestAddress, GuestMemory};
1313

1414
pub(super) const VIRTQ_DESC_F_NEXT: u16 = 0x1;
1515
pub(super) const VIRTQ_DESC_F_WRITE: u16 = 0x2;
@@ -32,7 +32,7 @@ struct Descriptor {
3232
next: u16,
3333
}
3434

35-
unsafe impl DataInit for Descriptor {}
35+
unsafe impl ByteValued for Descriptor {}
3636

3737
/// A virtio descriptor chain.
3838
pub struct DescriptorChain<'a> {
@@ -382,10 +382,10 @@ pub mod tests {
382382
phantom: PhantomData<*const T>,
383383
}
384384

385-
// The DataInit trait is required to use mem.read_obj_from_addr and write_obj_at_addr.
385+
// The ByteValued trait is required to use mem.read_obj_from_addr and write_obj_at_addr.
386386
impl<'a, T> SomeplaceInMemory<'a, T>
387387
where
388-
T: memory_model::DataInit,
388+
T: memory_model::ByteValued,
389389
{
390390
fn new(location: GuestAddress, mem: &'a GuestMemory) -> Self {
391391
SomeplaceInMemory {
@@ -480,7 +480,7 @@ pub mod tests {
480480

481481
impl<'a, T> VirtqRing<'a, T>
482482
where
483-
T: memory_model::DataInit,
483+
T: memory_model::ByteValued,
484484
{
485485
fn new(start: GuestAddress, mem: &'a GuestMemory, qsize: u16, alignment: usize) -> Self {
486486
assert_eq!(start.0 & (alignment as u64 - 1), 0);
@@ -517,13 +517,13 @@ pub mod tests {
517517
}
518518

519519
#[repr(C)]
520-
#[derive(Clone, Copy)]
520+
#[derive(Clone, Copy, Default)]
521521
pub struct VirtqUsedElem {
522522
pub id: u32,
523523
pub len: u32,
524524
}
525525

526-
unsafe impl memory_model::DataInit for VirtqUsedElem {}
526+
unsafe impl memory_model::ByteValued for VirtqUsedElem {}
527527

528528
pub type VirtqAvail<'a> = VirtqRing<'a, u16>;
529529
pub type VirtqUsed<'a> = VirtqRing<'a, VirtqUsedElem>;

src/memory_model/src/bytes.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Portions Copyright 2019 Red Hat, Inc.
2+
//
3+
// Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
// SPDX-License-Identifier: Apache-2.0
5+
//
6+
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
7+
// Use of this source code is governed by a BSD-style license that can be
8+
// found in the THIRD-PARTY file.
9+
10+
//! Define the ByteValued trait to mark that it is safe to instantiate the struct with random data.
11+
12+
/// Types for which it is safe to initialize from raw data.
13+
///
14+
/// A type `T` is `ByteValued` if and only if it can be initialized by reading its contents from a
15+
/// byte array. This is generally true for all plain-old-data structs. It is notably not true for
16+
/// any type that includes a reference.
17+
///
18+
/// Implementing this trait guarantees that it is safe to instantiate the struct with random data.
19+
pub unsafe trait ByteValued: Copy + Default + Send + Sync {}
20+
21+
// All intrinsic types and arrays of intrinsic types are ByteValued. They are just numbers.
22+
macro_rules! byte_valued_array {
23+
($T:ty, $($N:expr)+) => {
24+
$(
25+
unsafe impl ByteValued for [$T; $N] {}
26+
)+
27+
}
28+
}
29+
macro_rules! byte_valued_type {
30+
($T:ty) => {
31+
unsafe impl ByteValued for $T {}
32+
byte_valued_array! {
33+
$T,
34+
0 1 2 3 4 5 6 7 8 9
35+
10 11 12 13 14 15 16 17 18 19
36+
20 21 22 23 24 25 26 27 28 29
37+
30 31 32
38+
}
39+
};
40+
}
41+
byte_valued_type!(u8);
42+
byte_valued_type!(u16);
43+
byte_valued_type!(u32);
44+
byte_valued_type!(u64);
45+
byte_valued_type!(usize);
46+
byte_valued_type!(i8);
47+
byte_valued_type!(i16);
48+
byte_valued_type!(i32);
49+
byte_valued_type!(i64);
50+
byte_valued_type!(isize);

src/memory_model/src/guest_memory.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{mem, result};
1313

1414
use guest_address::{Address, GuestAddress};
1515
use mmap::{self, MemoryMapping};
16-
use DataInit;
16+
use ByteValued;
1717

1818
/// Errors associated with handling guest memory regions.
1919
#[derive(Debug)]
@@ -276,7 +276,7 @@ impl GuestMemory {
276276
/// Ok(num1 + num2)
277277
/// }
278278
/// ```
279-
pub fn read_obj_from_addr<T: DataInit>(&self, guest_addr: GuestAddress) -> Result<T> {
279+
pub fn read_obj_from_addr<T: ByteValued>(&self, guest_addr: GuestAddress) -> Result<T> {
280280
self.do_in_region(guest_addr, mem::size_of::<T>(), |mapping, offset| {
281281
mapping
282282
.read_obj(offset)
@@ -302,7 +302,7 @@ impl GuestMemory {
302302
/// .map_err(|_| ())
303303
/// }
304304
/// ```
305-
pub fn write_obj_at_addr<T: DataInit>(&self, val: T, guest_addr: GuestAddress) -> Result<()> {
305+
pub fn write_obj_at_addr<T: ByteValued>(&self, val: T, guest_addr: GuestAddress) -> Result<()> {
306306
self.do_in_region(guest_addr, mem::size_of::<T>(), move |mapping, offset| {
307307
mapping
308308
.write_obj(val, offset)

src/memory_model/src/lib.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,12 @@
1111

1212
extern crate libc;
1313

14-
/// Types for which it is safe to initialize from raw data.
15-
///
16-
/// A type `T` is `DataInit` if and only if it can be initialized by reading its contents from a
17-
/// byte array. This is generally true for all plain-old-data structs. It is notably not true for
18-
/// any type that includes a reference.
19-
///
20-
/// Implementing this trait guarantees that it is safe to instantiate the struct with random data.
21-
pub unsafe trait DataInit: Copy + Send + Sync {}
22-
23-
// All intrinsic types and arrays of intrinsic types are DataInit. They are just numbers.
24-
macro_rules! array_data_init {
25-
($T:ty, $($N:expr)+) => {
26-
$(
27-
unsafe impl DataInit for [$T; $N] {}
28-
)+
29-
}
30-
}
31-
macro_rules! data_init_type {
32-
($T:ty) => {
33-
unsafe impl DataInit for $T {}
34-
array_data_init! {
35-
$T,
36-
0 1 2 3 4 5 6 7 8 9
37-
10 11 12 13 14 15 16 17 18 19
38-
20 21 22 23 24 25 26 27 28 29
39-
30 31 32
40-
}
41-
};
42-
}
43-
data_init_type!(u8);
44-
data_init_type!(u16);
45-
data_init_type!(u32);
46-
data_init_type!(u64);
47-
data_init_type!(usize);
48-
data_init_type!(i8);
49-
data_init_type!(i16);
50-
data_init_type!(i32);
51-
data_init_type!(i64);
52-
data_init_type!(isize);
53-
14+
mod bytes;
5415
mod guest_address;
5516
mod guest_memory;
5617
mod mmap;
5718

19+
pub use bytes::ByteValued;
5820
pub use guest_address::Address;
5921
pub use guest_address::GuestAddress;
6022
pub use guest_memory::Error as GuestMemoryError;

src/memory_model/src/mmap.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::ptr::null_mut;
1414

1515
use libc;
1616

17-
use DataInit;
17+
use ByteValued;
1818

1919
/// Errors associated with memory mapping.
2020
#[derive(Debug)]
@@ -154,7 +154,7 @@ impl MemoryMapping {
154154
/// let res = mem_map.write_obj(55u64, 16);
155155
/// assert!(res.is_ok());
156156
/// ```
157-
pub fn write_obj<T: DataInit>(&self, val: T, offset: usize) -> Result<()> {
157+
pub fn write_obj<T: ByteValued>(&self, val: T, offset: usize) -> Result<()> {
158158
unsafe {
159159
// Guest memory can't strictly be modeled as a slice because it is
160160
// volatile. Writing to it with what compiles down to a memcpy
@@ -184,7 +184,7 @@ impl MemoryMapping {
184184
/// let num: u64 = mem_map.read_obj(32).unwrap();
185185
/// assert_eq!(55, num);
186186
/// ```
187-
pub fn read_obj<T: DataInit>(&self, offset: usize) -> Result<T> {
187+
pub fn read_obj<T: ByteValued>(&self, offset: usize) -> Result<T> {
188188
let (end, fail) = offset.overflowing_add(std::mem::size_of::<T>());
189189
if fail || end > self.size() {
190190
return Err(Error::InvalidAddress);

tests/integration_tests/build/test_coverage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import host_tools.cargo_build as host # pylint: disable=import-error
2121

22-
COVERAGE_TARGET_PCT = 85.2
22+
COVERAGE_TARGET_PCT = 85.1
2323
COVERAGE_MAX_DELTA = 0.01
2424

2525
CARGO_KCOV_REL_PATH = os.path.join(host.CARGO_BUILD_REL_PATH, 'kcov')

0 commit comments

Comments
 (0)