fuse/
fuzzing.rs

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
5use std::io;
6use std::os::unix::io::AsRawFd;
7
8use base::Protection;
9
10use crate::filesystem::DirEntry;
11use crate::filesystem::DirectoryIterator;
12use crate::filesystem::FileSystem;
13use crate::filesystem::ZeroCopyReader;
14use crate::filesystem::ZeroCopyWriter;
15use crate::server::Mapper;
16use crate::server::Reader;
17use crate::server::Server;
18use crate::server::Writer;
19
20// Use a file system that does nothing since we are fuzzing the server implementation.
21struct NullFs;
22impl FileSystem for NullFs {
23    type Inode = u64;
24    type Handle = u64;
25    type DirIter = NullIter;
26}
27
28struct NullIter;
29impl DirectoryIterator for NullIter {
30    fn next(&mut self) -> Option<DirEntry> {
31        None
32    }
33}
34
35struct NullMapper;
36impl Mapper for NullMapper {
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
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.
54pub 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}