fuchsia_fuzzctl/
diagnostics.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::writer::{OutputSink, Writer};
use anyhow::{anyhow, bail, Context as _, Error, Result};
use diagnostics_data::LogsData;
use fidl_fuchsia_fuzzer as fuzz;
use futures::io::ReadHalf;
use futures::{try_join, AsyncReadExt};
use serde_json::Deserializer;
use std::cell::RefCell;
use std::path::Path;
use std::rc::Rc;

/// Compostion of `SocketForwarder`s for standard output, standard errors, and system logs.
#[derive(Debug)]
pub struct Forwarder<O: OutputSink> {
    stdout: Option<SocketForwarder<O>>,
    stderr: Option<SocketForwarder<O>>,
    syslog: Option<SocketForwarder<O>>,
    writer: Writer<O>,
}

impl<O: OutputSink> Forwarder<O> {
    /// Creates a `Forwarder` that can forward data to the `writer`.
    ///
    /// Output will also be saved to the following files under the given `logs_dir` directory:
    ///
    ///   * fuzzer.stdout.txt
    ///   * fuzzer.stderr.txt
    ///   * fuzzer.syslog.json
    ///
    pub fn new(writer: &Writer<O>) -> Self {
        Self { stdout: None, stderr: None, syslog: None, writer: writer.clone() }
    }

    /// Registers the provided output socket.
    pub fn set_output<P: AsRef<Path>>(
        &mut self,
        socket: fidl::Socket,
        output: fuzz::TestOutput,
        logs_dir: &Option<P>,
    ) -> Result<()> {
        match output {
            fuzz::TestOutput::Stdout => {
                let forwarder = self.create_forwarder(logs_dir, "stdout", "txt", socket)?;
                self.stdout = Some(forwarder);
            }
            fuzz::TestOutput::Stderr => {
                let forwarder = self.create_forwarder(logs_dir, "stderr", "txt", socket)?;
                self.stderr = Some(forwarder);
            }
            fuzz::TestOutput::Syslog => {
                let forwarder = self.create_forwarder(logs_dir, "syslog", "json", socket)?;
                self.syslog = Some(forwarder);
            }
            _ => unreachable!(),
        }
        Ok(())
    }

    fn create_forwarder<P: AsRef<Path>>(
        &self,
        logs_dir: &Option<P>,
        name: &str,
        extension: &str,
        socket: fidl::Socket,
    ) -> Result<SocketForwarder<O>> {
        let writer = match logs_dir {
            Some(logs_dir) => self
                .writer
                .tee(logs_dir, format!("fuzzer.{}.{}", name, extension))
                .context(format!("failed to create file for {}", name)),
            None => Ok(self.writer.clone()),
        }?;
        let forwarder = SocketForwarder::try_new(socket, &writer)
            .context(format!("failed to create forwarder for {}", name))?;
        Ok(forwarder)
    }

    /// Forwards output from the fuzzer to the `Writer` using each of the `SocketForwarder`s.
    pub async fn forward_all(&self) -> Result<()> {
        let stdout = self.stdout.clone();
        let stdout_fut = || async move {
            if let Some(stdout) = stdout {
                stdout.forward_text("stdout").await.context("failed to forward stdout")?;
            }
            Ok::<(), Error>(())
        };

        let stderr = self.stderr.clone();
        let stderr_fut = || async move {
            if let Some(stderr) = stderr {
                stderr.forward_text("stderr").await.context("failed to forward stderr")?;
            }
            Ok::<(), Error>(())
        };

        let syslog = self.syslog.clone();
        let syslog_fut = || async move {
            if let Some(syslog) = syslog {
                syslog.forward_json("syslog").await.context("failed to forward syslog")?;
            }
            Ok::<(), Error>(())
        };

        try_join!(stdout_fut(), stderr_fut(), syslog_fut())?;
        Ok(())
    }
}

/// Forwarder for a single output stream.
#[derive(Debug)]
pub struct SocketForwarder<O: OutputSink> {
    reader: Rc<RefCell<ReadHalf<fidl::AsyncSocket>>>,
    writer: Writer<O>,
}

impl<O: OutputSink> Clone for SocketForwarder<O> {
    fn clone(&self) -> Self {
        Self { reader: Rc::clone(&self.reader), writer: self.writer.clone() }
    }
}

impl<O: OutputSink> SocketForwarder<O> {
    /// Converts a a socket into a SocketForwarder.
    ///
    /// Returns an error if conversion to an async socket fails.
    pub fn try_new(socket: fidl::Socket, writer: &Writer<O>) -> Result<Self> {
        let socket = fidl::AsyncSocket::from_socket(socket);
        let (reader, _) = socket.split();
        Ok(Self { reader: Rc::new(RefCell::new(reader)), writer: writer.clone() })
    }

    /// Continuously forwards messages from the socket to the writer until the socket is closed.
    pub async fn forward_text(&self, name: &str) -> Result<()> {
        let mut reader = self.reader.borrow_mut();
        let mut buf: [u8; 2048] = [0; 2048];
        let mut raw = Vec::new();
        let newline = '\n' as u8;
        let done_marker = format!("{}\n", fuzz::DONE_MARKER);
        let done_marker = done_marker.as_bytes();
        loop {
            match reader
                .read(&mut buf)
                .await
                .context(format!("failed to read text data from {} socket", name))?
            {
                0 => {
                    self.writer.write_all(&raw);
                    bail!("{} from fuzzer ended prematurely", name);
                }
                num_read => raw.extend_from_slice(&buf[0..num_read]),
            };
            let data = raw;
            raw = Vec::new();
            for message in data.split_inclusive(|&x| x == newline) {
                if message == done_marker {
                    return Ok(());
                } else if message.last() == Some(&newline) {
                    self.writer.write_all(&message);
                } else {
                    raw = message.to_vec();
                }
            }
        }
    }

    /// Continuously forwards JSON data from the socket to the writer until the socket is closed.
    pub async fn forward_json(&self, name: &str) -> Result<()> {
        let mut reader = self.reader.borrow_mut();
        let mut buf: [u8; 2048] = [0; 2048];
        let mut raw = Vec::new();
        loop {
            match reader
                .read(&mut buf)
                .await
                .context(format!("failed to read JSON data from {} socket", name))?
            {
                0 => {
                    self.writer.write_all(&raw);
                    bail!("{} from fuzzer ended prematurely", name);
                }
                num_read => raw.extend_from_slice(&buf[0..num_read]),
            };
            let deserializer = Deserializer::from_slice(&raw);
            let mut stream = deserializer.into_iter::<Vec<LogsData>>();
            while let Some(items) = stream.next() {
                let logs_data = match items {
                    Err(e) if e.is_eof() => break,
                    other => other,
                }
                .map_err(|e| anyhow!(format!("serde_json: {:?}", e)))
                .context("failed to deserialize")?;
                for log_data in logs_data.into_iter() {
                    if let Some(message) = log_data.msg() {
                        if message == fuzz::DONE_MARKER {
                            return Ok(());
                        }
                    }
                    self.writer.log(log_data);
                }
            }
            let num_read = stream.byte_offset();
            raw.drain(0..num_read);
        }
    }
}

#[cfg(test)]
mod tests {
    use anyhow::{Error, Result};
    use diagnostics_data::LogsData;
    use fidl::Socket;
    use fidl_fuchsia_fuzzer as fuzz;
    use fuchsia_fuzzctl::{Forwarder, SocketForwarder};
    use fuchsia_fuzzctl_test::{send_log_entry, Test};
    use futures::{try_join, AsyncWriteExt};
    use std::fs;

    #[fuchsia::test]
    async fn test_forward_text() -> Result<()> {
        let mut test = Test::try_new()?;
        let (tx, rx) = Socket::create_stream();
        let forwarder = SocketForwarder::try_new(rx, test.writer())?;
        let socket_fut = || async move {
            let mut tx = fidl::AsyncSocket::from_socket(tx);
            tx.write_all(b"hello\nworld!\n").await?;
            let done_marker = format!("{}\n", fuzz::DONE_MARKER);
            tx.write_all(done_marker.as_bytes()).await?;
            Ok::<(), Error>(())
        };
        test.output_matches("hello");
        test.output_matches("world!");
        try_join!(forwarder.forward_text("test"), socket_fut())?;
        test.verify_output()
    }

    #[fuchsia::test]
    async fn test_forward_json() -> Result<()> {
        let mut test = Test::try_new()?;
        let (tx, rx) = Socket::create_stream();
        let forwarder = SocketForwarder::try_new(rx, test.writer())?;
        let socket_fut = || async move {
            let mut tx = fidl::AsyncSocket::from_socket(tx);
            send_log_entry(&mut tx, "hello world").await?;
            send_log_entry(&mut tx, fuzz::DONE_MARKER).await?;
            Ok::<(), Error>(())
        };
        try_join!(forwarder.forward_json("test"), socket_fut())?;
        test.output_matches("[0.000][moniker][][I] hello world");
        test.verify_output()
    }

    #[fuchsia::test]
    async fn test_forward_all() -> Result<()> {
        let mut test = Test::try_new()?;
        let logs_dir = test.create_dir("logs")?;
        let logs_dir = Some(logs_dir);
        let mut forwarder = Forwarder::new(test.writer());

        let (stdout_tx, stdout_rx) = Socket::create_stream();
        forwarder.set_output(stdout_rx, fuzz::TestOutput::Stdout, &logs_dir)?;

        let (stderr_tx, stderr_rx) = Socket::create_stream();
        forwarder.set_output(stderr_rx, fuzz::TestOutput::Stderr, &logs_dir)?;

        let (syslog_tx, syslog_rx) = Socket::create_stream();
        forwarder.set_output(syslog_rx, fuzz::TestOutput::Syslog, &logs_dir)?;

        let done_marker = format!("{}\n", fuzz::DONE_MARKER);
        let done_marker_bytes = done_marker.as_bytes();

        let a_done_marker = format!("a{}\n", fuzz::DONE_MARKER);
        test.output_matches(a_done_marker.clone());

        let done_marker_a = format!("{}a\n", fuzz::DONE_MARKER);
        test.output_matches(done_marker_a.clone());

        let socket_fut = || async move {
            let mut stdout_tx = fidl::AsyncSocket::from_socket(stdout_tx);
            let mut stderr_tx = fidl::AsyncSocket::from_socket(stderr_tx);
            let mut syslog_tx = fidl::AsyncSocket::from_socket(syslog_tx);

            // Streams can be sent in any order
            send_log_entry(&mut syslog_tx, fuzz::DONE_MARKER).await?;

            // Data sent after the done marker should not be received.
            stdout_tx.write_all(done_marker_bytes).await?;
            stdout_tx.write_all(b"after\n").await?;

            // Done marker must be exactly delimited by newlines, and can arrive in pieces.
            stderr_tx.write_all(a_done_marker.as_bytes()).await?;
            stderr_tx.write_all(done_marker_a.as_bytes()).await?;
            for i in 0..done_marker_bytes.len() {
                stderr_tx.write_all(&done_marker_bytes[i..i + 1]).await?;
            }
            stderr_tx.write_all(b"after\n").await?;
            Ok::<(), Error>(())
        };

        try_join!(forwarder.forward_all(), socket_fut())?;
        test.verify_output()
    }

    #[fuchsia::test]
    async fn test_forward_to_file() -> Result<()> {
        let test = Test::try_new()?;

        let logs_dir = test.create_dir("logs")?;
        let logs_dir = Some(logs_dir);
        let mut forwarder = Forwarder::new(test.writer());

        let (stdout_tx, stdout_rx) = Socket::create_stream();
        forwarder.set_output(stdout_rx, fuzz::TestOutput::Stdout, &logs_dir)?;

        let (stderr_tx, stderr_rx) = Socket::create_stream();
        forwarder.set_output(stderr_rx, fuzz::TestOutput::Stderr, &logs_dir)?;

        let (syslog_tx, syslog_rx) = Socket::create_stream();
        forwarder.set_output(syslog_rx, fuzz::TestOutput::Syslog, &logs_dir)?;

        let sockets_fut = || async move {
            let done_marker = format!("{}\n", fuzz::DONE_MARKER);
            let done_marker_bytes = done_marker.as_bytes();

            // Write all in one shot.
            let mut stdout_tx = fidl::AsyncSocket::from_socket(stdout_tx);
            stdout_tx.write_all(b"hello world!\n").await?;
            stdout_tx.write_all(done_marker_bytes).await?;

            // Write all in pieces.
            let mut stderr_tx = fidl::AsyncSocket::from_socket(stderr_tx);
            stderr_tx.write_all(b"hel").await?;
            stderr_tx.write_all(b"lo ").await?;
            stderr_tx.write_all(b"wor").await?;
            stderr_tx.write_all(b"ld!\n").await?;
            stderr_tx.write_all(done_marker_bytes).await?;

            // Write JSON. This should be made prettier when copying, e.g. newlines, spaces, etc.
            let mut syslog_tx = fidl::AsyncSocket::from_socket(syslog_tx);
            send_log_entry(&mut syslog_tx, "hello world!").await?;
            send_log_entry(&mut syslog_tx, fuzz::DONE_MARKER).await?;
            Ok::<(), Error>(())
        };
        let sockets_fut = sockets_fut();
        try_join!(forwarder.forward_all(), sockets_fut)?;
        let logs_dir = logs_dir.unwrap();
        assert_eq!(fs::read(logs_dir.join("fuzzer.stdout.txt"))?, b"hello world!\n");
        assert_eq!(fs::read(logs_dir.join("fuzzer.stderr.txt"))?, b"hello world!\n");
        let data = fs::read(logs_dir.join("fuzzer.syslog.json"))?;
        let logs_data: LogsData = serde_json::from_slice(&data)?;
        assert_eq!(logs_data.msg(), Some("hello world!"));
        Ok(())
    }
}