Skip to content

Commit 500277b

Browse files
Iulian Barbujiangliu
authored andcommitted
benches: safe conversion
Previously benches were benefiting from conversion from u64 to usize, which might imply data loss. This commit fixes this issue, converting from usize to u64. Signed-off-by: Iulian Barbu <[email protected]>
1 parent bc0f7c2 commit 500277b

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

benches/guest_memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
pub use criterion::{black_box, Criterion};
77
use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap};
88

9-
const REGION_SIZE: u64 = 0x10_0000;
9+
const REGION_SIZE: usize = 0x10_0000;
1010
const REGIONS_COUNT: u64 = 256;
1111

1212
pub fn benchmark_for_guest_memory(c: &mut Criterion) {
@@ -16,7 +16,7 @@ pub fn benchmark_for_guest_memory(c: &mut Criterion) {
1616
fn find_region(mem: &GuestMemoryMmap) {
1717
for i in 0..REGIONS_COUNT {
1818
let _ = mem
19-
.find_region(black_box(GuestAddress(i * REGION_SIZE)))
19+
.find_region(black_box(GuestAddress(i * REGION_SIZE as u64)))
2020
.unwrap();
2121
}
2222
}

benches/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ mod volatile;
1515
use volatile::benchmark_for_volatile;
1616

1717
#[cfg(feature = "backend-mmap")]
18-
pub fn create_guest_memory_mmap(size: u64, count: u64) -> GuestMemoryMmap {
18+
// Use this function with caution. It does not check against overflows
19+
// and `GuestMemoryMmap::from_ranges` errors.
20+
fn create_guest_memory_mmap(size: usize, count: u64) -> GuestMemoryMmap {
1921
let mut regions: Vec<(GuestAddress, usize)> = Vec::new();
2022
for i in 0..count {
21-
regions.push((GuestAddress(i * size), size as usize));
23+
regions.push((GuestAddress(i * size as u64), size));
2224
}
2325

2426
GuestMemoryMmap::from_ranges(regions.as_slice()).unwrap()

benches/mmap/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use criterion::{black_box, Criterion};
1515

1616
use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemory};
1717

18-
const REGION_SIZE: u64 = 0x8000_0000;
18+
const REGION_SIZE: usize = 0x8000_0000;
1919
const REGIONS_COUNT: u64 = 8;
2020
const ACCESS_SIZE: usize = 0x200;
2121

@@ -55,8 +55,10 @@ enum AccessKind {
5555
impl AccessKind {
5656
fn make_offset(&self, access_size: usize) -> u64 {
5757
match *self {
58-
AccessKind::InRegion(idx) => REGION_SIZE * idx,
59-
AccessKind::CrossRegion(idx) => REGION_SIZE * (idx + 1) - (access_size / 2) as u64,
58+
AccessKind::InRegion(idx) => REGION_SIZE as u64 * idx,
59+
AccessKind::CrossRegion(idx) => {
60+
REGION_SIZE as u64 * (idx + 1) - (access_size as u64 / 2)
61+
}
6062
}
6163
}
6264
}
@@ -67,7 +69,7 @@ pub fn benchmark_for_mmap(c: &mut Criterion) {
6769
// Just a sanity check.
6870
assert_eq!(
6971
memory.last_addr(),
70-
GuestAddress(REGION_SIZE * REGIONS_COUNT - 0x01)
72+
GuestAddress(REGION_SIZE as u64 * REGIONS_COUNT - 0x01)
7173
);
7274

7375
let some_small_dummy = SmallDummy {

0 commit comments

Comments
 (0)