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 use std::io;
6 use std::os::unix::io::AsRawFd;
7
8 use base::Protection;
9
10 use crate::filesystem::DirEntry;
11 use crate::filesystem::DirectoryIterator;
12 use crate::filesystem::FileSystem;
13 use crate::filesystem::ZeroCopyReader;
14 use crate::filesystem::ZeroCopyWriter;
15 use crate::server::Mapper;
16 use crate::server::Reader;
17 use crate::server::Server;
18 use crate::server::Writer;
19
20 // Use a file system that does nothing since we are fuzzing the server implementation.
21 struct NullFs;
22 impl FileSystem for NullFs {
23 type Inode = u64;
24 type Handle = u64;
25 type DirIter = NullIter;
26 }
27
28 struct NullIter;
29 impl DirectoryIterator for NullIter {
next(&mut self) -> Option<DirEntry>30 fn next(&mut self) -> Option<DirEntry> {
31 None
32 }
33 }
34
35 struct NullMapper;
36 impl Mapper for NullMapper {
map( &self, _mem_offset: u64, _size: usize, _fd: &dyn AsRawFd, _file_offset: u64, _prot: Protection, ) -> io::Result<()>37 fn map(
38 &self,
39 _mem_offset: u64,
40 _size: usize,
41 _fd: &dyn AsRawFd,
42 _file_offset: u64,
43 _prot: Protection,
44 ) -> io::Result<()> {
45 Err(io::Error::from_raw_os_error(libc::EOPNOTSUPP))
46 }
47
unmap(&self, _offset: u64, _size: u64) -> io::Result<()>48 fn unmap(&self, _offset: u64, _size: u64) -> io::Result<()> {
49 Err(io::Error::from_raw_os_error(libc::EOPNOTSUPP))
50 }
51 }
52
53 /// Fuzz the server implementation.
fuzz_server<R: Reader + ZeroCopyReader, W: Writer + ZeroCopyWriter>(r: R, w: W)54 pub fn fuzz_server<R: Reader + ZeroCopyReader, W: Writer + ZeroCopyWriter>(r: R, w: W) {
55 let server = Server::new(NullFs);
56 let mapper = NullMapper {};
57
58 let _ = server.handle_message(r, w, mapper);
59 }
60