Skip to content

Commit f7e90d7

Browse files
committed
Make the 'rawfd' feature a NOP on windows
When `rawfd' feature was introduced on commit 0b7a437, it was enabled by default to avoid breaking the default behavior on linux. So, on Windows, vm-memory must be included with 'default-features = false' otherwise it fails to compile. This is problematic for projects that depend indirectly on vm-memory and fail if their dependencies don't set 'default-features = false'. Since there is no current way to set the default feature set for a specific target OS, let's make the 'rawfd' feature a NOP for Windows builds, the feature will still be enabled, but silently just not do anything. Signed-off-by: German Maglione <gmaglione@redhat.com>
1 parent ea84a32 commit f7e90d7

6 files changed

Lines changed: 31 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
### Changed
8+
- \[[#373](https://github.com/rust-vmm/vm-memory/pull/373)\] Make the 'rawfd' feature a NOP on Windows
89

910
### Fixed
1011

benches/mmap/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
extern crate criterion;
88
extern crate vm_memory;
99

10-
#[cfg(feature = "rawfd")]
10+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
1111
use std::fs::{File, OpenOptions};
1212
use std::mem::size_of;
1313

14-
#[cfg(feature = "rawfd")]
14+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
1515
use std::path::Path;
1616

1717
use core::hint::black_box;
@@ -88,9 +88,9 @@ pub fn benchmark_for_mmap(c: &mut Criterion) {
8888
let mut image = make_image(ACCESS_SIZE);
8989
let buf = &mut [0u8; ACCESS_SIZE];
9090

91-
#[cfg(feature = "rawfd")]
91+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
9292
let mut file = File::open(Path::new("/dev/zero")).expect("Could not open /dev/zero");
93-
#[cfg(feature = "rawfd")]
93+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
9494
let mut file_to_write = OpenOptions::new()
9595
.write(true)
9696
.open("/dev/null")
@@ -116,7 +116,7 @@ pub fn benchmark_for_mmap(c: &mut Criterion) {
116116
})
117117
});
118118

119-
#[cfg(feature = "rawfd")]
119+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
120120
c.bench_function(format!("read_from_file_{offset:#0X}").as_str(), |b| {
121121
b.iter(|| {
122122
black_box(&memory)
@@ -165,7 +165,7 @@ pub fn benchmark_for_mmap(c: &mut Criterion) {
165165
})
166166
});
167167

168-
#[cfg(feature = "rawfd")]
168+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
169169
c.bench_function(format!("write_to_file_{offset:#0X}").as_str(), |b| {
170170
b.iter(|| {
171171
black_box(&memory)

src/io.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use crate::volatile_memory::copy_slice_impl::{copy_from_volatile_slice, copy_to_
88
use crate::{VolatileMemoryError, VolatileSlice};
99
use std::io::{Cursor, ErrorKind};
1010

11-
#[cfg(feature = "rawfd")]
11+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
1212
use std::io::Stdout;
1313

14-
#[cfg(feature = "rawfd")]
14+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
1515
use std::os::fd::{AsFd, AsRawFd, BorrowedFd};
1616

1717
macro_rules! retry_eintr {
@@ -131,7 +131,7 @@ pub trait WriteVolatile {
131131

132132
macro_rules! impl_read_write_volatile_for_raw_fd {
133133
($raw_fd_ty:ty) => {
134-
#[cfg(feature = "rawfd")]
134+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
135135
impl ReadVolatile for $raw_fd_ty {
136136
fn read_volatile<B: BitmapSlice>(
137137
&mut self,
@@ -141,7 +141,7 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
141141
}
142142
}
143143

144-
#[cfg(feature = "rawfd")]
144+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
145145
impl ReadVolatile for &$raw_fd_ty {
146146
fn read_volatile<B: BitmapSlice>(
147147
&mut self,
@@ -151,7 +151,7 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
151151
}
152152
}
153153

154-
#[cfg(feature = "rawfd")]
154+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
155155
impl ReadVolatile for &mut $raw_fd_ty {
156156
fn read_volatile<B: BitmapSlice>(
157157
&mut self,
@@ -161,7 +161,7 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
161161
}
162162
}
163163

164-
#[cfg(feature = "rawfd")]
164+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
165165
impl WriteVolatile for $raw_fd_ty {
166166
fn write_volatile<B: BitmapSlice>(
167167
&mut self,
@@ -171,7 +171,7 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
171171
}
172172
}
173173

174-
#[cfg(feature = "rawfd")]
174+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
175175
impl WriteVolatile for &$raw_fd_ty {
176176
fn write_volatile<B: BitmapSlice>(
177177
&mut self,
@@ -181,7 +181,7 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
181181
}
182182
}
183183

184-
#[cfg(feature = "rawfd")]
184+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
185185
impl WriteVolatile for &mut $raw_fd_ty {
186186
fn write_volatile<B: BitmapSlice>(
187187
&mut self,
@@ -193,7 +193,7 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
193193
};
194194
}
195195

196-
#[cfg(feature = "rawfd")]
196+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
197197
impl WriteVolatile for Stdout {
198198
fn write_volatile<B: BitmapSlice>(
199199
&mut self,
@@ -203,7 +203,7 @@ impl WriteVolatile for Stdout {
203203
}
204204
}
205205

206-
#[cfg(feature = "rawfd")]
206+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
207207
impl WriteVolatile for &Stdout {
208208
fn write_volatile<B: BitmapSlice>(
209209
&mut self,
@@ -223,7 +223,7 @@ impl_read_write_volatile_for_raw_fd!(std::os::fd::BorrowedFd<'_>);
223223
/// the given [`VolatileSlice`].
224224
///
225225
/// Returns the numbers of bytes read.
226-
#[cfg(feature = "rawfd")]
226+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
227227
fn read_volatile_raw_fd(
228228
raw_fd: BorrowedFd<'_>,
229229
buf: &mut VolatileSlice<impl BitmapSlice>,
@@ -254,7 +254,7 @@ fn read_volatile_raw_fd(
254254
/// data stored in the given [`VolatileSlice`].
255255
///
256256
/// Returns the numbers of bytes written.
257-
#[cfg(feature = "rawfd")]
257+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
258258
fn write_volatile_raw_fd(
259259
raw_fd: BorrowedFd<'_>,
260260
buf: &VolatileSlice<impl BitmapSlice>,
@@ -435,9 +435,9 @@ mod tests {
435435
use crate::io::{ReadVolatile, WriteVolatile};
436436
use crate::{VolatileMemoryError, VolatileSlice};
437437
use std::io::{Cursor, ErrorKind};
438-
#[cfg(feature = "rawfd")]
438+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
439439
use std::io::{Read, Seek, Write};
440-
#[cfg(feature = "rawfd")]
440+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
441441
use vmm_sys_util::tempfile::TempFile;
442442

443443
// ---- Test ReadVolatile for &[u8] ----
@@ -474,7 +474,7 @@ mod tests {
474474
}
475475

476476
// ---- Test ReadVolatile for File ----
477-
#[cfg(all(feature = "rawfd", not(miri)))]
477+
#[cfg(all(feature = "rawfd", not(miri), not(target_os = "windows")))]
478478
fn read_4_bytes_from_file(source: Vec<u8>, expected_output: [u8; 5]) {
479479
let mut temp_file = TempFile::new().unwrap().into_file();
480480
temp_file.write_all(source.as_ref()).unwrap();
@@ -518,7 +518,7 @@ mod tests {
518518

519519
for (input, output) in test_cases {
520520
read_4_bytes_to_5_byte_memory(input.clone(), output);
521-
#[cfg(all(feature = "rawfd", not(miri)))]
521+
#[cfg(all(feature = "rawfd", not(miri), not(target_os = "windows")))]
522522
read_4_bytes_from_file(input, output);
523523
}
524524
}
@@ -559,7 +559,7 @@ mod tests {
559559
}
560560

561561
// ---- Test ẂriteVolatile for File works ----
562-
#[cfg(all(feature = "rawfd", not(miri)))]
562+
#[cfg(all(feature = "rawfd", not(miri), not(target_os = "windows")))]
563563
fn write_5_bytes_to_file(mut source: Vec<u8>) {
564564
// Test write_volatile for File works
565565
let mut temp_file = TempFile::new().unwrap().into_file();
@@ -603,7 +603,7 @@ mod tests {
603603

604604
for (input, output) in test_cases {
605605
write_4_bytes_to_5_byte_vec(input.clone(), output);
606-
#[cfg(all(feature = "rawfd", not(miri)))]
606+
#[cfg(all(feature = "rawfd", not(miri), not(target_os = "windows")))]
607607
write_5_bytes_to_file(input);
608608
}
609609
}

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
#[cfg(not(target_pointer_width = "64"))]
2626
compile_error!("vm-memory only supports 64-bit targets!");
2727

28-
#[cfg(all(target_family = "windows", feature = "rawfd"))]
29-
compile_error!("rawfd feature is not supported on Windows targets!");
30-
3128
#[cfg(all(target_family = "windows", feature = "xen"))]
3229
compile_error!("xen feature is not supported on Windows targets!");
3330

src/mmap/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ mod tests {
242242
use crate::{Bytes, GuestMemoryBackend, GuestMemoryError};
243243

244244
use std::io::Write;
245-
#[cfg(feature = "rawfd")]
245+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
246246
use std::{fs::File, path::Path};
247247
use vmm_sys_util::tempfile::TempFile;
248248

@@ -473,7 +473,7 @@ mod tests {
473473
}
474474

475475
#[test]
476-
#[cfg(feature = "rawfd")]
476+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
477477
#[cfg(not(miri))]
478478
fn read_to_and_write_from_mem() {
479479
use std::mem;

src/volatile_memory.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,11 +1466,11 @@ mod tests {
14661466
use super::*;
14671467
use std::alloc::Layout;
14681468

1469-
#[cfg(feature = "rawfd")]
1469+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
14701470
use std::fs::File;
14711471
#[cfg(feature = "backend-bitmap")]
14721472
use std::mem::size_of_val;
1473-
#[cfg(feature = "rawfd")]
1473+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
14741474
use std::path::Path;
14751475
use std::sync::atomic::{AtomicUsize, Ordering};
14761476
use std::sync::{Arc, Barrier};
@@ -1479,7 +1479,7 @@ mod tests {
14791479
use matches::assert_matches;
14801480
#[cfg(feature = "backend-bitmap")]
14811481
use std::num::NonZeroUsize;
1482-
#[cfg(feature = "rawfd")]
1482+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
14831483
use vmm_sys_util::tempfile::TempFile;
14841484

14851485
#[cfg(feature = "backend-bitmap")]
@@ -1972,7 +1972,7 @@ mod tests {
19721972
}
19731973

19741974
#[test]
1975-
#[cfg(feature = "rawfd")]
1975+
#[cfg(all(feature = "rawfd", not(target_os = "windows")))]
19761976
fn mem_read_and_write() {
19771977
let mut backing = vec![0u8; 5];
19781978
let a = VolatileSlice::from(backing.as_mut_slice());

0 commit comments

Comments
 (0)