xref: /aosp_15_r20/external/crosvm/disk/src/android_sparse.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2019 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // https://android.googlesource.com/platform/system/core/+/7b444f0/libsparse/sparse_format.h
6 
7 use std::collections::BTreeMap;
8 use std::fs::File;
9 use std::io;
10 use std::io::ErrorKind;
11 use std::io::Read;
12 use std::io::Seek;
13 use std::io::SeekFrom;
14 use std::mem;
15 use std::sync::Arc;
16 
17 use async_trait::async_trait;
18 use base::AsRawDescriptor;
19 use base::FileAllocate;
20 use base::FileReadWriteAtVolatile;
21 use base::FileSetLen;
22 use base::RawDescriptor;
23 use base::VolatileSlice;
24 use cros_async::BackingMemory;
25 use cros_async::Executor;
26 use cros_async::IoSource;
27 use data_model::Le16;
28 use data_model::Le32;
29 use remain::sorted;
30 use thiserror::Error;
31 use zerocopy::AsBytes;
32 use zerocopy::FromBytes;
33 use zerocopy::FromZeroes;
34 
35 use crate::AsyncDisk;
36 use crate::DiskFile;
37 use crate::DiskGetLen;
38 use crate::Error as DiskError;
39 use crate::Result as DiskResult;
40 use crate::ToAsyncDisk;
41 
42 #[sorted]
43 #[derive(Error, Debug)]
44 pub enum Error {
45     #[error("invalid magic header for android sparse format")]
46     InvalidMagicHeader,
47     #[error("invalid specification: \"{0}\"")]
48     InvalidSpecification(String),
49     #[error("failed to read specification: \"{0}\"")]
50     ReadSpecificationError(io::Error),
51 }
52 
53 pub type Result<T> = std::result::Result<T, Error>;
54 
55 pub const SPARSE_HEADER_MAGIC: u32 = 0xed26ff3a;
56 const MAJOR_VERSION: u16 = 1;
57 
58 #[repr(C)]
59 #[derive(Clone, Copy, Debug, AsBytes, FromZeroes, FromBytes)]
60 struct SparseHeader {
61     magic: Le32,          // SPARSE_HEADER_MAGIC
62     major_version: Le16,  // (0x1) - reject images with higher major versions
63     minor_version: Le16,  // (0x0) - allow images with higer minor versions
64     file_hdr_sz: Le16,    // 28 bytes for first revision of the file format
65     chunk_hdr_size: Le16, // 12 bytes for first revision of the file format
66     blk_sz: Le32,         // block size in bytes, must be a multiple of 4 (4096)
67     total_blks: Le32,     // total blocks in the non-sparse output image
68     total_chunks: Le32,   // total chunks in the sparse input image
69     // CRC32 checksum of the original data, counting "don't care" as 0. Standard 802.3 polynomial,
70     // use a Public Domain table implementation
71     image_checksum: Le32,
72 }
73 
74 const CHUNK_TYPE_RAW: u16 = 0xCAC1;
75 const CHUNK_TYPE_FILL: u16 = 0xCAC2;
76 const CHUNK_TYPE_DONT_CARE: u16 = 0xCAC3;
77 const CHUNK_TYPE_CRC32: u16 = 0xCAC4;
78 
79 #[repr(C)]
80 #[derive(Clone, Copy, Debug, AsBytes, FromZeroes, FromBytes)]
81 struct ChunkHeader {
82     chunk_type: Le16, /* 0xCAC1 -> raw; 0xCAC2 -> fill; 0xCAC3 -> don't care */
83     reserved1: u16,
84     chunk_sz: Le32, /* in blocks in output image */
85     total_sz: Le32, /* in bytes of chunk input file including chunk header and data */
86 }
87 
88 #[derive(Clone, Debug, PartialEq, Eq)]
89 enum Chunk {
90     Raw(u64), // Offset into the file
91     Fill([u8; 4]),
92     DontCare,
93 }
94 
95 #[derive(Clone, Debug, PartialEq, Eq)]
96 struct ChunkWithSize {
97     chunk: Chunk,
98     expanded_size: u64,
99 }
100 
101 /* Following a Raw or Fill or CRC32 chunk is data.
102  *  For a Raw chunk, it's the data in chunk_sz * blk_sz.
103  *  For a Fill chunk, it's 4 bytes of the fill data.
104  *  For a CRC32 chunk, it's 4 bytes of CRC32
105  */
106 #[derive(Debug)]
107 pub struct AndroidSparse {
108     file: File,
109     total_size: u64,
110     chunks: BTreeMap<u64, ChunkWithSize>,
111 }
112 
parse_chunk<T: Read + Seek>(input: &mut T, blk_sz: u64) -> Result<Option<ChunkWithSize>>113 fn parse_chunk<T: Read + Seek>(input: &mut T, blk_sz: u64) -> Result<Option<ChunkWithSize>> {
114     const HEADER_SIZE: usize = mem::size_of::<ChunkHeader>();
115     let current_offset = input
116         .stream_position()
117         .map_err(Error::ReadSpecificationError)?;
118     let mut chunk_header = ChunkHeader::new_zeroed();
119     input
120         .read_exact(chunk_header.as_bytes_mut())
121         .map_err(Error::ReadSpecificationError)?;
122     let chunk_body_size = (chunk_header.total_sz.to_native() as usize)
123         .checked_sub(HEADER_SIZE)
124         .ok_or(Error::InvalidSpecification(format!(
125             "chunk total_sz {} smaller than header size {}",
126             chunk_header.total_sz.to_native(),
127             HEADER_SIZE
128         )))?;
129     let chunk = match chunk_header.chunk_type.to_native() {
130         CHUNK_TYPE_RAW => {
131             input
132                 .seek(SeekFrom::Current(chunk_body_size as i64))
133                 .map_err(Error::ReadSpecificationError)?;
134             Chunk::Raw(current_offset + HEADER_SIZE as u64)
135         }
136         CHUNK_TYPE_FILL => {
137             let mut fill_bytes = [0u8; 4];
138             if chunk_body_size != fill_bytes.len() {
139                 return Err(Error::InvalidSpecification(format!(
140                     "Fill chunk had bad size. Expected {}, was {}",
141                     fill_bytes.len(),
142                     chunk_body_size
143                 )));
144             }
145             input
146                 .read_exact(&mut fill_bytes)
147                 .map_err(Error::ReadSpecificationError)?;
148             Chunk::Fill(fill_bytes)
149         }
150         CHUNK_TYPE_DONT_CARE => Chunk::DontCare,
151         CHUNK_TYPE_CRC32 => return Ok(None), // TODO(schuffelen): Validate crc32s in input
152         unknown_type => {
153             return Err(Error::InvalidSpecification(format!(
154                 "Chunk had invalid type, was {:x}",
155                 unknown_type
156             )))
157         }
158     };
159     let expanded_size = chunk_header.chunk_sz.to_native() as u64 * blk_sz;
160     Ok(Some(ChunkWithSize {
161         chunk,
162         expanded_size,
163     }))
164 }
165 
166 impl AndroidSparse {
from_file(mut file: File) -> Result<AndroidSparse>167     pub fn from_file(mut file: File) -> Result<AndroidSparse> {
168         file.seek(SeekFrom::Start(0))
169             .map_err(Error::ReadSpecificationError)?;
170         let mut sparse_header = SparseHeader::new_zeroed();
171         file.read_exact(sparse_header.as_bytes_mut())
172             .map_err(Error::ReadSpecificationError)?;
173         if sparse_header.magic != SPARSE_HEADER_MAGIC {
174             return Err(Error::InvalidSpecification(format!(
175                 "Header did not match magic constant. Expected {:x}, was {:x}",
176                 SPARSE_HEADER_MAGIC,
177                 sparse_header.magic.to_native()
178             )));
179         } else if sparse_header.major_version != MAJOR_VERSION {
180             return Err(Error::InvalidSpecification(format!(
181                 "Header major version did not match. Expected {}, was {}",
182                 MAJOR_VERSION,
183                 sparse_header.major_version.to_native(),
184             )));
185         } else if sparse_header.chunk_hdr_size.to_native() as usize != mem::size_of::<ChunkHeader>()
186         {
187             // The canonical parser for this format allows `chunk_hdr_size >= sizeof(ChunkHeader)`,
188             // but we've chosen to be stricter for simplicity.
189             return Err(Error::InvalidSpecification(format!(
190                 "Chunk header size does not match chunk header struct, expected {}, was {}",
191                 sparse_header.chunk_hdr_size.to_native(),
192                 mem::size_of::<ChunkHeader>()
193             )));
194         }
195         let block_size = sparse_header.blk_sz.to_native() as u64;
196         let chunks = (0..sparse_header.total_chunks.to_native())
197             .filter_map(|_| parse_chunk(&mut file, block_size).transpose())
198             .collect::<Result<Vec<ChunkWithSize>>>()?;
199         let total_size =
200             sparse_header.total_blks.to_native() as u64 * sparse_header.blk_sz.to_native() as u64;
201         AndroidSparse::from_parts(file, total_size, chunks)
202     }
203 
from_parts(file: File, size: u64, chunks: Vec<ChunkWithSize>) -> Result<AndroidSparse>204     fn from_parts(file: File, size: u64, chunks: Vec<ChunkWithSize>) -> Result<AndroidSparse> {
205         let mut chunks_map: BTreeMap<u64, ChunkWithSize> = BTreeMap::new();
206         let mut expanded_location: u64 = 0;
207         for chunk_with_size in chunks {
208             let size = chunk_with_size.expanded_size;
209             if chunks_map
210                 .insert(expanded_location, chunk_with_size)
211                 .is_some()
212             {
213                 return Err(Error::InvalidSpecification(format!(
214                     "Two chunks were at {}",
215                     expanded_location
216                 )));
217             }
218             expanded_location += size;
219         }
220         let image = AndroidSparse {
221             file,
222             total_size: size,
223             chunks: chunks_map,
224         };
225         let calculated_len: u64 = image.chunks.iter().map(|x| x.1.expanded_size).sum();
226         if calculated_len != size {
227             return Err(Error::InvalidSpecification(format!(
228                 "Header promised size {}, chunks added up to {}",
229                 size, calculated_len
230             )));
231         }
232         Ok(image)
233     }
234 }
235 
236 impl DiskGetLen for AndroidSparse {
get_len(&self) -> io::Result<u64>237     fn get_len(&self) -> io::Result<u64> {
238         Ok(self.total_size)
239     }
240 }
241 
242 impl FileSetLen for AndroidSparse {
set_len(&self, _len: u64) -> io::Result<()>243     fn set_len(&self, _len: u64) -> io::Result<()> {
244         Err(io::Error::new(
245             ErrorKind::PermissionDenied,
246             "unsupported operation",
247         ))
248     }
249 }
250 
251 impl AsRawDescriptor for AndroidSparse {
as_raw_descriptor(&self) -> RawDescriptor252     fn as_raw_descriptor(&self) -> RawDescriptor {
253         self.file.as_raw_descriptor()
254     }
255 }
256 
257 // Performs reads up to the chunk boundary.
258 impl FileReadWriteAtVolatile for AndroidSparse {
read_at_volatile(&self, slice: VolatileSlice, offset: u64) -> io::Result<usize>259     fn read_at_volatile(&self, slice: VolatileSlice, offset: u64) -> io::Result<usize> {
260         let found_chunk = self.chunks.range(..=offset).next_back();
261         let (
262             chunk_start,
263             ChunkWithSize {
264                 chunk,
265                 expanded_size,
266             },
267         ) = found_chunk.ok_or_else(|| {
268             io::Error::new(
269                 ErrorKind::UnexpectedEof,
270                 format!("no chunk for offset {}", offset),
271             )
272         })?;
273         let chunk_offset = offset - chunk_start;
274         let chunk_size = *expanded_size;
275         let subslice = if chunk_offset + (slice.size() as u64) > chunk_size {
276             slice
277                 .sub_slice(0, (chunk_size - chunk_offset) as usize)
278                 .map_err(|e| io::Error::new(ErrorKind::InvalidData, format!("{:?}", e)))?
279         } else {
280             slice
281         };
282         match chunk {
283             Chunk::DontCare => {
284                 subslice.write_bytes(0);
285                 Ok(subslice.size())
286             }
287             Chunk::Raw(file_offset) => self
288                 .file
289                 .read_at_volatile(subslice, *file_offset + chunk_offset),
290             Chunk::Fill(fill_bytes) => {
291                 let chunk_offset_mod = chunk_offset % fill_bytes.len() as u64;
292                 let filled_memory: Vec<u8> = fill_bytes
293                     .iter()
294                     .cloned()
295                     .cycle()
296                     .skip(chunk_offset_mod as usize)
297                     .take(subslice.size())
298                     .collect();
299                 subslice.copy_from(&filled_memory);
300                 Ok(subslice.size())
301             }
302         }
303     }
write_at_volatile(&self, _slice: VolatileSlice, _offset: u64) -> io::Result<usize>304     fn write_at_volatile(&self, _slice: VolatileSlice, _offset: u64) -> io::Result<usize> {
305         Err(io::Error::new(
306             ErrorKind::PermissionDenied,
307             "unsupported operation",
308         ))
309     }
310 }
311 
312 // TODO(b/271381851): implement `try_clone`. It allows virtio-blk to run multiple workers.
313 impl DiskFile for AndroidSparse {}
314 
315 /// An Android Sparse disk that implements `AsyncDisk` for access.
316 pub struct AsyncAndroidSparse {
317     inner: IoSource<File>,
318     total_size: u64,
319     chunks: BTreeMap<u64, ChunkWithSize>,
320 }
321 
322 impl ToAsyncDisk for AndroidSparse {
to_async_disk(self: Box<Self>, ex: &Executor) -> DiskResult<Box<dyn AsyncDisk>>323     fn to_async_disk(self: Box<Self>, ex: &Executor) -> DiskResult<Box<dyn AsyncDisk>> {
324         Ok(Box::new(AsyncAndroidSparse {
325             inner: ex.async_from(self.file).map_err(DiskError::ToAsync)?,
326             total_size: self.total_size,
327             chunks: self.chunks,
328         }))
329     }
330 }
331 
332 impl DiskGetLen for AsyncAndroidSparse {
get_len(&self) -> io::Result<u64>333     fn get_len(&self) -> io::Result<u64> {
334         Ok(self.total_size)
335     }
336 }
337 
338 impl FileSetLen for AsyncAndroidSparse {
set_len(&self, _len: u64) -> io::Result<()>339     fn set_len(&self, _len: u64) -> io::Result<()> {
340         Err(io::Error::new(
341             ErrorKind::PermissionDenied,
342             "unsupported operation",
343         ))
344     }
345 }
346 
347 impl FileAllocate for AsyncAndroidSparse {
allocate(&self, _offset: u64, _length: u64) -> io::Result<()>348     fn allocate(&self, _offset: u64, _length: u64) -> io::Result<()> {
349         Err(io::Error::new(
350             ErrorKind::PermissionDenied,
351             "unsupported operation",
352         ))
353     }
354 }
355 
356 #[async_trait(?Send)]
357 impl AsyncDisk for AsyncAndroidSparse {
flush(&self) -> crate::Result<()>358     async fn flush(&self) -> crate::Result<()> {
359         // android sparse is read-only, nothing to flush.
360         Ok(())
361     }
362 
fsync(&self) -> DiskResult<()>363     async fn fsync(&self) -> DiskResult<()> {
364         // Do nothing because it's read-only.
365         Ok(())
366     }
367 
fdatasync(&self) -> DiskResult<()>368     async fn fdatasync(&self) -> DiskResult<()> {
369         // Do nothing because it's read-only.
370         Ok(())
371     }
372 
373     /// Reads data from `file_offset` to the end of the current chunk and write them into memory
374     /// `mem` at `mem_offsets`.
read_to_mem<'a>( &'a self, file_offset: u64, mem: Arc<dyn BackingMemory + Send + Sync>, mem_offsets: cros_async::MemRegionIter<'a>, ) -> DiskResult<usize>375     async fn read_to_mem<'a>(
376         &'a self,
377         file_offset: u64,
378         mem: Arc<dyn BackingMemory + Send + Sync>,
379         mem_offsets: cros_async::MemRegionIter<'a>,
380     ) -> DiskResult<usize> {
381         let found_chunk = self.chunks.range(..=file_offset).next_back();
382         let (
383             chunk_start,
384             ChunkWithSize {
385                 chunk,
386                 expanded_size,
387             },
388         ) = found_chunk.ok_or(DiskError::ReadingData(io::Error::new(
389             ErrorKind::UnexpectedEof,
390             format!("no chunk for offset {}", file_offset),
391         )))?;
392         let chunk_offset = file_offset - chunk_start;
393         let chunk_size = *expanded_size;
394 
395         // Truncate `mem_offsets` to the remaining size of the current chunk.
396         let mem_offsets = mem_offsets.take_bytes((chunk_size - chunk_offset) as usize);
397         let mem_size = mem_offsets.clone().map(|x| x.len).sum();
398         match chunk {
399             Chunk::DontCare => {
400                 for region in mem_offsets {
401                     mem.get_volatile_slice(region)
402                         .map_err(DiskError::GuestMemory)?
403                         .write_bytes(0);
404                 }
405                 Ok(mem_size)
406             }
407             Chunk::Raw(offset) => self
408                 .inner
409                 .read_to_mem(Some(offset + chunk_offset), mem, mem_offsets)
410                 .await
411                 .map_err(DiskError::ReadToMem),
412             Chunk::Fill(fill_bytes) => {
413                 let chunk_offset_mod = chunk_offset % fill_bytes.len() as u64;
414                 let filled_memory: Vec<u8> = fill_bytes
415                     .iter()
416                     .cloned()
417                     .cycle()
418                     .skip(chunk_offset_mod as usize)
419                     .take(mem_size)
420                     .collect();
421 
422                 let mut filled_count = 0;
423                 for region in mem_offsets {
424                     let buf = &filled_memory[filled_count..filled_count + region.len];
425                     mem.get_volatile_slice(region)
426                         .map_err(DiskError::GuestMemory)?
427                         .copy_from(buf);
428                     filled_count += region.len;
429                 }
430                 Ok(mem_size)
431             }
432         }
433     }
434 
write_from_mem<'a>( &'a self, _file_offset: u64, _mem: Arc<dyn BackingMemory + Send + Sync>, _mem_offsets: cros_async::MemRegionIter<'a>, ) -> DiskResult<usize>435     async fn write_from_mem<'a>(
436         &'a self,
437         _file_offset: u64,
438         _mem: Arc<dyn BackingMemory + Send + Sync>,
439         _mem_offsets: cros_async::MemRegionIter<'a>,
440     ) -> DiskResult<usize> {
441         Err(DiskError::UnsupportedOperation)
442     }
443 
punch_hole(&self, _file_offset: u64, _length: u64) -> DiskResult<()>444     async fn punch_hole(&self, _file_offset: u64, _length: u64) -> DiskResult<()> {
445         Err(DiskError::UnsupportedOperation)
446     }
447 
write_zeroes_at(&self, _file_offset: u64, _length: u64) -> DiskResult<()>448     async fn write_zeroes_at(&self, _file_offset: u64, _length: u64) -> DiskResult<()> {
449         Err(DiskError::UnsupportedOperation)
450     }
451 }
452 
453 #[cfg(test)]
454 mod tests {
455     use std::io::Cursor;
456     use std::io::Write;
457 
458     use super::*;
459 
460     const CHUNK_SIZE: usize = mem::size_of::<ChunkHeader>();
461 
462     #[test]
parse_raw()463     fn parse_raw() {
464         let chunk_raw = ChunkHeader {
465             chunk_type: CHUNK_TYPE_RAW.into(),
466             reserved1: 0,
467             chunk_sz: 1.into(),
468             total_sz: (CHUNK_SIZE as u32 + 123).into(),
469         };
470         let header_bytes = chunk_raw.as_bytes();
471         let mut chunk_bytes: Vec<u8> = Vec::new();
472         chunk_bytes.extend_from_slice(header_bytes);
473         chunk_bytes.extend_from_slice(&[0u8; 123]);
474         let mut chunk_cursor = Cursor::new(chunk_bytes);
475         let chunk = parse_chunk(&mut chunk_cursor, 123)
476             .expect("Failed to parse")
477             .expect("Failed to determine chunk type");
478         let expected_chunk = ChunkWithSize {
479             chunk: Chunk::Raw(CHUNK_SIZE as u64),
480             expanded_size: 123,
481         };
482         assert_eq!(expected_chunk, chunk);
483     }
484 
485     #[test]
parse_dont_care()486     fn parse_dont_care() {
487         let chunk_raw = ChunkHeader {
488             chunk_type: CHUNK_TYPE_DONT_CARE.into(),
489             reserved1: 0,
490             chunk_sz: 100.into(),
491             total_sz: (CHUNK_SIZE as u32).into(),
492         };
493         let header_bytes = chunk_raw.as_bytes();
494         let mut chunk_cursor = Cursor::new(header_bytes);
495         let chunk = parse_chunk(&mut chunk_cursor, 123)
496             .expect("Failed to parse")
497             .expect("Failed to determine chunk type");
498         let expected_chunk = ChunkWithSize {
499             chunk: Chunk::DontCare,
500             expanded_size: 12300,
501         };
502         assert_eq!(expected_chunk, chunk);
503     }
504 
505     #[test]
parse_fill()506     fn parse_fill() {
507         let chunk_raw = ChunkHeader {
508             chunk_type: CHUNK_TYPE_FILL.into(),
509             reserved1: 0,
510             chunk_sz: 100.into(),
511             total_sz: (CHUNK_SIZE as u32 + 4).into(),
512         };
513         let header_bytes = chunk_raw.as_bytes();
514         let mut chunk_bytes: Vec<u8> = Vec::new();
515         chunk_bytes.extend_from_slice(header_bytes);
516         chunk_bytes.extend_from_slice(&[123u8; 4]);
517         let mut chunk_cursor = Cursor::new(chunk_bytes);
518         let chunk = parse_chunk(&mut chunk_cursor, 123)
519             .expect("Failed to parse")
520             .expect("Failed to determine chunk type");
521         let expected_chunk = ChunkWithSize {
522             chunk: Chunk::Fill([123, 123, 123, 123]),
523             expanded_size: 12300,
524         };
525         assert_eq!(expected_chunk, chunk);
526     }
527 
528     #[test]
parse_crc32()529     fn parse_crc32() {
530         let chunk_raw = ChunkHeader {
531             chunk_type: CHUNK_TYPE_CRC32.into(),
532             reserved1: 0,
533             chunk_sz: 0.into(),
534             total_sz: (CHUNK_SIZE as u32 + 4).into(),
535         };
536         let header_bytes = chunk_raw.as_bytes();
537         let mut chunk_bytes: Vec<u8> = Vec::new();
538         chunk_bytes.extend_from_slice(header_bytes);
539         chunk_bytes.extend_from_slice(&[123u8; 4]);
540         let mut chunk_cursor = Cursor::new(chunk_bytes);
541         let chunk = parse_chunk(&mut chunk_cursor, 123).expect("Failed to parse");
542         assert_eq!(None, chunk);
543     }
544 
test_image(chunks: Vec<ChunkWithSize>) -> AndroidSparse545     fn test_image(chunks: Vec<ChunkWithSize>) -> AndroidSparse {
546         let file = tempfile::tempfile().expect("failed to create tempfile");
547         let size = chunks.iter().map(|x| x.expanded_size).sum();
548         AndroidSparse::from_parts(file, size, chunks).expect("Could not create image")
549     }
550 
551     #[test]
read_dontcare()552     fn read_dontcare() {
553         let chunks = vec![ChunkWithSize {
554             chunk: Chunk::DontCare,
555             expanded_size: 100,
556         }];
557         let image = test_image(chunks);
558         let mut input_memory = [55u8; 100];
559         image
560             .read_exact_at_volatile(VolatileSlice::new(&mut input_memory[..]), 0)
561             .expect("Could not read");
562         let expected = [0u8; 100];
563         assert_eq!(&expected[..], &input_memory[..]);
564     }
565 
566     #[test]
read_fill_simple()567     fn read_fill_simple() {
568         let chunks = vec![ChunkWithSize {
569             chunk: Chunk::Fill([10, 20, 10, 20]),
570             expanded_size: 8,
571         }];
572         let image = test_image(chunks);
573         let mut input_memory = [55u8; 8];
574         image
575             .read_exact_at_volatile(VolatileSlice::new(&mut input_memory[..]), 0)
576             .expect("Could not read");
577         let expected = [10, 20, 10, 20, 10, 20, 10, 20];
578         assert_eq!(&expected[..], &input_memory[..]);
579     }
580 
581     #[test]
read_fill_edges()582     fn read_fill_edges() {
583         let chunks = vec![ChunkWithSize {
584             chunk: Chunk::Fill([10, 20, 30, 40]),
585             expanded_size: 8,
586         }];
587         let image = test_image(chunks);
588         let mut input_memory = [55u8; 6];
589         image
590             .read_exact_at_volatile(VolatileSlice::new(&mut input_memory[..]), 1)
591             .expect("Could not read");
592         let expected = [20, 30, 40, 10, 20, 30];
593         assert_eq!(&expected[..], &input_memory[..]);
594     }
595 
596     #[test]
read_fill_offset_edges()597     fn read_fill_offset_edges() {
598         let chunks = vec![
599             ChunkWithSize {
600                 chunk: Chunk::DontCare,
601                 expanded_size: 20,
602             },
603             ChunkWithSize {
604                 chunk: Chunk::Fill([10, 20, 30, 40]),
605                 expanded_size: 100,
606             },
607         ];
608         let image = test_image(chunks);
609         let mut input_memory = [55u8; 7];
610         image
611             .read_exact_at_volatile(VolatileSlice::new(&mut input_memory[..]), 39)
612             .expect("Could not read");
613         let expected = [40, 10, 20, 30, 40, 10, 20];
614         assert_eq!(&expected[..], &input_memory[..]);
615     }
616 
617     #[test]
read_raw()618     fn read_raw() {
619         let chunks = vec![ChunkWithSize {
620             chunk: Chunk::Raw(0),
621             expanded_size: 100,
622         }];
623         let mut image = test_image(chunks);
624         write!(image.file, "hello").expect("Failed to write into internal file");
625         let mut input_memory = [55u8; 5];
626         image
627             .read_exact_at_volatile(VolatileSlice::new(&mut input_memory[..]), 0)
628             .expect("Could not read");
629         let expected = [104, 101, 108, 108, 111];
630         assert_eq!(&expected[..], &input_memory[..]);
631     }
632 
633     #[test]
read_two_fills()634     fn read_two_fills() {
635         let chunks = vec![
636             ChunkWithSize {
637                 chunk: Chunk::Fill([10, 20, 10, 20]),
638                 expanded_size: 4,
639             },
640             ChunkWithSize {
641                 chunk: Chunk::Fill([30, 40, 30, 40]),
642                 expanded_size: 4,
643             },
644         ];
645         let image = test_image(chunks);
646         let mut input_memory = [55u8; 8];
647         image
648             .read_exact_at_volatile(VolatileSlice::new(&mut input_memory[..]), 0)
649             .expect("Could not read");
650         let expected = [10, 20, 10, 20, 30, 40, 30, 40];
651         assert_eq!(&expected[..], &input_memory[..]);
652     }
653 
654     /**
655      * Tests for Async.
656      */
657     use cros_async::MemRegion;
658     use cros_async::MemRegionIter;
659     use vm_memory::GuestAddress;
660     use vm_memory::GuestMemory;
661 
test_async_image( chunks: Vec<ChunkWithSize>, ex: &Executor, ) -> DiskResult<Box<dyn AsyncDisk>>662     fn test_async_image(
663         chunks: Vec<ChunkWithSize>,
664         ex: &Executor,
665     ) -> DiskResult<Box<dyn AsyncDisk>> {
666         Box::new(test_image(chunks)).to_async_disk(ex)
667     }
668 
669     /// Reads `len` bytes of data from `image` at 'offset'.
read_exact_at(image: &dyn AsyncDisk, offset: usize, len: usize) -> Vec<u8>670     async fn read_exact_at(image: &dyn AsyncDisk, offset: usize, len: usize) -> Vec<u8> {
671         let guest_mem = Arc::new(GuestMemory::new(&[(GuestAddress(0), 4096)]).unwrap());
672         // Fill in guest_mem with dirty data.
673         guest_mem
674             .write_all_at_addr(&vec![55u8; len], GuestAddress(0))
675             .unwrap();
676 
677         let mut count = 0usize;
678         while count < len {
679             let result = image
680                 .read_to_mem(
681                     (offset + count) as u64,
682                     guest_mem.clone(),
683                     MemRegionIter::new(&[MemRegion {
684                         offset: count as u64,
685                         len: len - count,
686                     }]),
687                 )
688                 .await;
689             count += result.unwrap();
690         }
691 
692         let mut buf = vec![0; len];
693         guest_mem.read_at_addr(&mut buf, GuestAddress(0)).unwrap();
694         buf
695     }
696 
697     #[test]
async_read_dontcare()698     fn async_read_dontcare() {
699         let ex = Executor::new().unwrap();
700         ex.run_until(async {
701             let chunks = vec![ChunkWithSize {
702                 chunk: Chunk::DontCare,
703                 expanded_size: 100,
704             }];
705             let image = test_async_image(chunks, &ex).unwrap();
706             let buf = read_exact_at(&*image, 0, 100).await;
707             assert!(buf.iter().all(|x| *x == 0));
708         })
709         .unwrap();
710     }
711 
712     #[test]
async_read_dontcare_with_offsets()713     fn async_read_dontcare_with_offsets() {
714         let ex = Executor::new().unwrap();
715         ex.run_until(async {
716             let chunks = vec![ChunkWithSize {
717                 chunk: Chunk::DontCare,
718                 expanded_size: 10,
719             }];
720             let image = test_async_image(chunks, &ex).unwrap();
721             // Prepare guest_mem with dirty data.
722             let guest_mem = Arc::new(GuestMemory::new(&[(GuestAddress(0), 4096)]).unwrap());
723             guest_mem
724                 .write_all_at_addr(&[55u8; 20], GuestAddress(0))
725                 .unwrap();
726 
727             // Pass multiple `MemRegion` to `read_to_mem`.
728             image
729                 .read_to_mem(
730                     0,
731                     guest_mem.clone(),
732                     MemRegionIter::new(&[
733                         MemRegion { offset: 1, len: 3 },
734                         MemRegion { offset: 6, len: 2 },
735                     ]),
736                 )
737                 .await
738                 .unwrap();
739             let mut buf = vec![0; 10];
740             guest_mem.read_at_addr(&mut buf, GuestAddress(0)).unwrap();
741             let expected = [55, 0, 0, 0, 55, 55, 0, 0, 55, 55];
742             assert_eq!(expected[..], buf[..]);
743         })
744         .unwrap();
745     }
746 
747     #[test]
async_read_fill_simple()748     fn async_read_fill_simple() {
749         let ex = Executor::new().unwrap();
750         ex.run_until(async {
751             let chunks = vec![ChunkWithSize {
752                 chunk: Chunk::Fill([10, 20, 10, 20]),
753                 expanded_size: 8,
754             }];
755             let image = test_async_image(chunks, &ex).unwrap();
756             let buf = read_exact_at(&*image, 0, 8).await;
757             let expected = [10, 20, 10, 20, 10, 20, 10, 20];
758             assert_eq!(expected[..], buf[..]);
759         })
760         .unwrap();
761     }
762 
763     #[test]
async_read_fill_simple_with_offset()764     fn async_read_fill_simple_with_offset() {
765         let ex = Executor::new().unwrap();
766         ex.run_until(async {
767             let chunks = vec![ChunkWithSize {
768                 chunk: Chunk::Fill([10, 20, 10, 20]),
769                 expanded_size: 8,
770             }];
771             let image = test_async_image(chunks, &ex).unwrap();
772             // Prepare guest_mem with dirty data.
773             let guest_mem = Arc::new(GuestMemory::new(&[(GuestAddress(0), 4096)]).unwrap());
774             guest_mem
775                 .write_all_at_addr(&[55u8; 20], GuestAddress(0))
776                 .unwrap();
777 
778             // Pass multiple `MemRegion` to `read_to_mem`.
779             image
780                 .read_to_mem(
781                     0,
782                     guest_mem.clone(),
783                     MemRegionIter::new(&[
784                         MemRegion { offset: 1, len: 3 },
785                         MemRegion { offset: 6, len: 2 },
786                     ]),
787                 )
788                 .await
789                 .unwrap();
790             let mut buf = vec![0; 10];
791             guest_mem.read_at_addr(&mut buf, GuestAddress(0)).unwrap();
792             let expected = [55, 10, 20, 10, 55, 55, 20, 10, 55, 55];
793             assert_eq!(expected[..], buf[..]);
794         })
795         .unwrap();
796     }
797 
798     #[test]
async_read_fill_edges()799     fn async_read_fill_edges() {
800         let ex = Executor::new().unwrap();
801         ex.run_until(async {
802             let chunks = vec![ChunkWithSize {
803                 chunk: Chunk::Fill([10, 20, 30, 40]),
804                 expanded_size: 8,
805             }];
806             let image = test_async_image(chunks, &ex).unwrap();
807             let buf = read_exact_at(&*image, 1, 6).await;
808             let expected = [20, 30, 40, 10, 20, 30];
809             assert_eq!(expected[..], buf[..]);
810         })
811         .unwrap();
812     }
813 
814     #[test]
async_read_fill_offset_edges()815     fn async_read_fill_offset_edges() {
816         let ex = Executor::new().unwrap();
817         ex.run_until(async {
818             let chunks = vec![
819                 ChunkWithSize {
820                     chunk: Chunk::DontCare,
821                     expanded_size: 20,
822                 },
823                 ChunkWithSize {
824                     chunk: Chunk::Fill([10, 20, 30, 40]),
825                     expanded_size: 100,
826                 },
827             ];
828             let image = test_async_image(chunks, &ex).unwrap();
829             let buf = read_exact_at(&*image, 39, 7).await;
830             let expected = [40, 10, 20, 30, 40, 10, 20];
831             assert_eq!(expected[..], buf[..]);
832         })
833         .unwrap();
834     }
835 
836     #[test]
async_read_raw()837     fn async_read_raw() {
838         let ex = Executor::new().unwrap();
839         ex.run_until(async {
840             let chunks = vec![ChunkWithSize {
841                 chunk: Chunk::Raw(0),
842                 expanded_size: 100,
843             }];
844             let mut image = Box::new(test_image(chunks));
845             write!(image.file, "hello").unwrap();
846             let async_image = image.to_async_disk(&ex).unwrap();
847             let buf = read_exact_at(&*async_image, 0, 5).await;
848             let expected = [104, 101, 108, 108, 111];
849             assert_eq!(&expected[..], &buf[..]);
850         })
851         .unwrap();
852     }
853 
854     #[test]
async_read_fill_raw_with_offset()855     fn async_read_fill_raw_with_offset() {
856         let ex = Executor::new().unwrap();
857         ex.run_until(async {
858             let chunks = vec![ChunkWithSize {
859                 chunk: Chunk::Raw(0),
860                 expanded_size: 100,
861             }];
862             let mut image = Box::new(test_image(chunks));
863             write!(image.file, "hello").unwrap();
864             let async_image = image.to_async_disk(&ex).unwrap();
865             // Prepare guest_mem with dirty data.
866             let guest_mem = Arc::new(GuestMemory::new(&[(GuestAddress(0), 4096)]).unwrap());
867             guest_mem
868                 .write_all_at_addr(&[55u8; 20], GuestAddress(0))
869                 .unwrap();
870 
871             // Pass multiple `MemRegion` to `read_to_mem`.
872             async_image
873                 .read_to_mem(
874                     0,
875                     guest_mem.clone(),
876                     MemRegionIter::new(&[
877                         MemRegion { offset: 1, len: 3 },
878                         MemRegion { offset: 6, len: 2 },
879                     ]),
880                 )
881                 .await
882                 .unwrap();
883             let mut buf = vec![0; 10];
884             guest_mem.read_at_addr(&mut buf, GuestAddress(0)).unwrap();
885             let expected = [55, 104, 101, 108, 55, 55, 108, 111, 55, 55];
886             assert_eq!(expected[..], buf[..]);
887         })
888         .unwrap();
889     }
890 
891     #[test]
async_read_two_fills()892     fn async_read_two_fills() {
893         let ex = Executor::new().unwrap();
894         ex.run_until(async {
895             let chunks = vec![
896                 ChunkWithSize {
897                     chunk: Chunk::Fill([10, 20, 10, 20]),
898                     expanded_size: 4,
899                 },
900                 ChunkWithSize {
901                     chunk: Chunk::Fill([30, 40, 30, 40]),
902                     expanded_size: 4,
903                 },
904             ];
905             let image = test_async_image(chunks, &ex).unwrap();
906             let buf = read_exact_at(&*image, 0, 8).await;
907             let expected = [10, 20, 10, 20, 30, 40, 30, 40];
908             assert_eq!(&expected[..], &buf[..]);
909         })
910         .unwrap();
911     }
912 }
913