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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//! Reader of [`Gcov`] format.
//!
//! The file format of GCNO/GCDA is documented in the [GCC source code][gcov-io.h].
//!
//! [`Gcov`]: ../raw/struct.Gcov.html
//! [gcov-io.h]: https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/gcov-io.h;hb=HEAD

use error::*;
use intern::{Interner, Symbol, UNKNOWN_SYMBOL};
use raw::*;
use utils::IntoStringLossy;

use byteorder::{BigEndian, ByteOrder, LittleEndian, ReadBytesExt};

use std::io::{self, Read, Take};
use std::iter::FromIterator;
use std::result::Result as StdResult;

/// The reader of a GCNO/GCDA file.
///
/// # Examples
///
/// ```rust
/// use cov::reader::Reader;
/// use cov::Interner;
/// # use cov::Result;
/// use std::io::Read;
/// use std::fs::File;
///
/// # fn main() { run().unwrap(); }
/// # fn run() -> Result<()> {
/// let mut interner = Interner::new();
/// let file = File::open("test-data/trivial.clang/x.gcno")?;
///
/// // read the header.
/// let mut reader = Reader::new(file, &mut interner)?;
/// // read the content.
/// let _gcov = reader.parse()?;
/// # Ok(()) }
/// ```
#[derive(Debug)]
pub struct Reader<'si, R> {
    reader: R,
    cursor: u64,
    ty: Type,
    version: Version,
    stamp: u32,
    is_big_endian: bool,
    interner: &'si mut Interner,
}

/// Consumes the whole reader to the end.
fn consume_to_end<R: Read>(reader: &mut R) -> Result<()> {
    loop {
        let mut buf = [0_u8; 64];
        match reader.read(&mut buf) {
            Ok(0) => break,
            Ok(_) => continue,
            Err(e) => {
                if e.kind() == io::ErrorKind::Interrupted {
                    continue;
                } else {
                    bail!(e);
                }
            },
        }
    }
    Ok(())
}

#[test]
fn test_consume_to_end() {
    (|| -> Result<()> {
        let mut reader = &b"abcde123456fghijkl"[..];
        let mut top = [0u8; 5];
        reader.read_exact(&mut top)?;
        consume_to_end(&mut reader.by_ref().take(6))?;
        let mut bottom = [0u8; 7];
        reader.read_exact(&mut bottom)?;
        assert_eq!(&top, b"abcde");
        assert_eq!(&bottom, b"fghijkl");
        assert_eq!(reader, b"");
        Ok(())
    })().unwrap();
}

impl<'si, R: Read> Reader<'si, R> {
    /// Advances the reader cursor by `count` bytes. If `res` is an error, include the file position information to the
    /// error, otherwise return `res` as-is.
    fn advance_cursor<T, E: Into<Error>>(&mut self, count: u64, res: StdResult<T, E>) -> Result<T> {
        Location::Cursor(self.cursor).wrap(|| {
            self.cursor += count;
            res
        })
    }

    /// Reads a 32-bit number in gcov format.
    ///
    /// # Errors
    ///
    /// Returns [`Io`] on I/O failure, e.g. reaching end-of-file.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn read_32(&mut self) -> Result<u32> {
        let value = self.reader.read_u32::<LittleEndian>();
        let mut value = self.advance_cursor(4, value)?;
        if self.is_big_endian {
            value = value.swap_bytes();
        }
        Ok(value)
    }

    /// Reads a 64-bit number in gcov format.
    ///
    /// # Errors
    ///
    /// Returns [`Io`] on I/O failure, e.g. reaching end-of-file.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn read_64(&mut self) -> Result<u64> {
        let value = self.reader.read_u64::<LittleEndian>();
        let mut value = self.advance_cursor(8, value)?;
        if self.is_big_endian {
            value = value.rotate_left(32).swap_bytes();
        }
        Ok(value)
    }

    /// Reads eight 32-bit numbers in gcov format.
    ///
    /// # Errors
    ///
    /// Returns [`Io`] on I/O failure, e.g. reaching end-of-file.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn read_histogram_bitvector(&mut self) -> Result<[u32; 8]> {
        let mut buf = [0; 32];
        let res = self.reader.read_exact(&mut buf);
        self.advance_cursor(32, res)?;

        let mut decoded = [0; 8];
        let decoder = if self.is_big_endian {
            BigEndian::read_u32
        } else {
            LittleEndian::read_u32
        };

        for (i, slot) in decoded.iter_mut().enumerate() {
            *slot = decoder(&buf[(i * 4)..])
        }

        Ok(decoded)
    }

    /// Reads a string in gcov format.
    ///
    /// # Errors
    ///
    /// * Returns [`Io`] on I/O failure, e.g. reaching end-of-file.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn read_string(&mut self) -> Result<Symbol> {
        let length = (self.read_32()? as u64) * 4;
        let mut buf = Vec::with_capacity(length as usize);
        let value = self.reader.by_ref().take(length).read_to_end(&mut buf);
        let _ = self.advance_cursor(length, value)?;
        let actual_length = buf.iter().rposition(|b| *b != 0).unwrap_or(!0).wrapping_add(1);
        buf.truncate(actual_length);
        let string = buf.into_string_lossy().into_boxed_str();
        Ok(self.interner.intern(string))
    }

    /// Reads something from this reader using the provided function `f`, until end-of-file is encountered.
    ///
    /// The result is a collection of returned values of `f`.
    fn until_eof<C, T, F>(&mut self, f: F) -> Result<C>
    where
        F: FnMut(&mut Self) -> Result<T>,
        C: FromIterator<T>,
    {
        UntilEof(self, f).collect()
    }

    /// Parses the header of the file, and creates a new gcov reader.
    ///
    /// # Errors
    ///
    /// * Returns [`UnknownFileType`] if the reader is not a in GCNO/GCDA format.
    /// * Returns [`UnsupportedVersion`] if the GCNO/GCDA version is not supported by this crate.
    /// * Returns [`Io`] on I/O failure.
    ///
    /// [`UnknownFileType`]: ../error/enum.ErrorKind.html#variant.UnknownFileType
    /// [`UnsupportedVersion`]: ../error/enum.ErrorKind.html#variant.UnsupportedVersion
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    pub fn new(mut reader: R, interner: &'si mut Interner) -> Result<Reader<'si, R>> {
        trace!("gcov-magic");
        let (ty, is_big_endian) = match reader.read_u32::<LittleEndian>()? {
            0x67_63_6e_6f => (Type::Gcno, false),
            0x6f_6e_63_67 => (Type::Gcno, true),
            0x67_63_64_61 => (Type::Gcda, false),
            0x61_64_63_67 => (Type::Gcda, true),
            magic => bail!(ErrorKind::UnknownFileType(magic)),
        };
        let mut result = Reader {
            reader,
            ty,
            is_big_endian,
            interner,
            cursor: 4,
            version: INVALID_VERSION,
            stamp: 0,
        };
        trace!("gcov-version @ 0x{:x}", result.cursor);
        let version = result.read_32()?;
        let version = Location::Cursor(result.cursor - 4).wrap(|| Version::try_from(version))?;
        result.version = version;
        trace!("gcov-stamp @ 0x{:x}", result.cursor);
        result.stamp = result.read_32()?;
        Ok(result)
    }

    /// Parses the content of the reader, to produce a [`Gcov`] structure.
    ///
    /// # Errors
    ///
    /// * Returns [`UnknownTag`] if the GCNO/GCDA contains an unrecognized record tag.
    /// * Returns [`Io`] on I/O failure.
    ///
    /// [`Gcov`]: ../raw/struct.Gcov.html
    /// [`UnknownTag`]: ../error/enum.ErrorKind.html#variant.UnknownTag
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    pub fn parse(&mut self) -> Result<Gcov> {
        let records = self.until_eof(|s| {
            let cursor = s.cursor;
            let (tag, mut subreader) = s.read_record_header()?;
            trace!("parse-record @ 0x{:x}; tag = 0x{:08x}", cursor, tag);
            Ok(match tag {
                FUNCTION_TAG => {
                    let (ident, function) = subreader.parse_function()?;
                    Record::Function(ident, function)
                },
                BLOCKS_TAG => Record::Blocks(subreader.parse_blocks()?),
                ARCS_TAG => Record::Arcs(subreader.parse_arcs()?),
                LINES_TAG => Record::Lines(subreader.parse_lines()?),
                COUNTER_BASE_TAG => Record::ArcCounts(subreader.parse_arc_counts()?),
                OBJECT_SUMMARY_TAG | PROGRAM_SUMMARY_TAG => Record::Summary(subreader.parse_summary()?),
                EOF_TAG => bail!(ErrorKind::Eof),
                tag => bail!(Location::Cursor(cursor).wrap_error(ErrorKind::UnknownTag(tag.0))),
            })
        })?;
        Ok(Gcov {
            ty: self.ty,
            version: self.version,
            stamp: self.stamp,
            records,
            src: None,
        })
    }

    /// Reads the header of a record. Returns the record type, and a reader that is specialized for
    /// reading this record.
    ///
    /// # Errors
    ///
    /// * Returns [`Io`] on I/O failure.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn read_record_header(&mut self) -> Result<(Tag, Reader<Take<&mut R>>)> {
        trace!("record-tag @ 0x{:x}", self.cursor);
        let tag = Tag(self.read_32()?);
        trace!("record-length @ 0x{:x}", self.cursor);
        let length = (self.read_32()? as u64) * 4;
        let subreader = Reader {
            reader: self.reader.by_ref().take(length),
            cursor: self.cursor,
            ty: self.ty,
            version: self.version,
            stamp: self.stamp,
            is_big_endian: self.is_big_endian,
            interner: self.interner,
        };
        debug!(
            "record-header: tag = {0:08x}, length = {1} (0x{1:x}), range = 0x{2:x} .. 0x{3:x}",
            tag,
            length,
            self.cursor,
            self.cursor + length
        );
        self.cursor += length;
        Ok((tag, subreader))
    }

    /// Parses the `ANNOUNCE_FUNCTION` record.
    ///
    /// # Errors
    ///
    /// * Returns [`Io`] on I/O failure.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn parse_function(&mut self) -> Result<(Ident, Function)> {
        trace!("function-ident @ 0x{:x}", self.cursor);
        let ident = Ident(self.read_32()?);
        trace!("function-lineno-checksum @ 0x{:x}", self.cursor);
        let lineno_checksum = self.read_32()?;
        let cfg_checksum = if self.version >= VERSION_4_7 {
            trace!("function-cfg-checksum @ 0x{:x}", self.cursor);
            self.read_32()?
        } else {
            0
        };
        let source = if self.ty == Type::Gcno {
            trace!("function-source @ 0x{:x}", self.cursor);
            Some(self.read_source()?)
        } else if self.version < VERSION_4_7 {
            trace!("function-source-name @ 0x{:x}", self.cursor);
            let name = self.read_string()?;
            Some(Source {
                name,
                filename: UNKNOWN_SYMBOL,
                line: 0,
            })
        } else {
            None
        };

        consume_to_end(&mut self.reader)?;

        Ok((
            ident,
            Function {
                lineno_checksum,
                cfg_checksum,
                source,
            },
        ))
    }

    /// Reads the source of a function.
    ///
    /// # Errors
    ///
    /// * Returns [`Io`] on I/O failure.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn read_source(&mut self) -> Result<Source> {
        trace!("source-name @ 0x{:x}", self.cursor);
        let name = self.read_string()?;
        trace!("source-filename @ 0x{:x}", self.cursor);
        let filename = self.read_string()?;
        trace!("source-line @ 0x{:x}", self.cursor);
        let line = self.read_32()?;
        Ok(Source {
            name,
            filename,
            line,
        })
    }

    /// Parses the `BASIC_BLOCK` record.
    ///
    /// # Errors
    ///
    /// * Returns [`Io`] on I/O failure.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn parse_blocks(&mut self) -> Result<Blocks> {
        trace!("blocks-flags @ 0x{:x}", self.cursor);
        let flags = self.until_eof(|s| {
            let raw_flag = s.read_32()?;
            Location::Cursor(s.cursor - 4).wrap(|| BlockAttr::from_gcno(raw_flag))
        })?;
        Ok(Blocks { flags })
    }

    /// Parses the `ARCS` record.
    ///
    /// # Errors
    ///
    /// * Returns [`Io`] on I/O failure.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn parse_arcs(&mut self) -> Result<Arcs> {
        trace!("arcs-block-no @ 0x{:x}", self.cursor);
        let src_block = BlockIndex(self.read_32()?);
        trace!("arcs-arcs @ 0x{:x}", self.cursor);
        let arcs = self.until_eof(|s| {
            trace!("arc-dest-block @ 0x{:x}", s.cursor);
            let dest_block = BlockIndex(s.read_32()?);
            trace!("arc-flags @ 0x{:x}", s.cursor);
            let raw_flags = s.read_32()?;
            let flags = Location::Cursor(s.cursor - 4).wrap(|| ArcAttr::from_gcno(raw_flags))?;
            Ok(Arc { dest_block, flags })
        })?;
        Ok(Arcs { src_block, arcs })
    }

    /// Parses the `LINES` record.
    ///
    /// # Errors
    ///
    /// * Returns [`Io`] on I/O failure.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn parse_lines(&mut self) -> Result<Lines> {
        trace!("lines-block-no @ 0x{:x}", self.cursor);
        let block_number = BlockIndex(self.read_32()?);
        trace!("lines-lines @ 0x{:x}", self.cursor);
        let mut lines: Vec<_> = self.until_eof(|s| {
            trace!("line-line-no @ 0x{:x}", s.cursor);
            let line_number = s.read_32()?;
            let line = if line_number == 0 {
                trace!("line-filename @ 0x{:x}", s.cursor);
                let filename = s.read_string()?;
                Line::FileName(filename)
            } else {
                Line::LineNumber(line_number)
            };
            Ok(line)
        })?;
        let _ = lines.pop(); // the last entry must be a null string which is useless.
        Ok(Lines {
            block_number,
            lines,
        })
    }

    /// Parses the `ARC_COUNTS` record.
    ///
    /// # Errors
    ///
    /// * Returns [`Io`] on I/O failure.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn parse_arc_counts(&mut self) -> Result<ArcCounts> {
        trace!("arc-counts-counts @ 0x{:x}", self.cursor);
        let counts = self.until_eof(Self::read_64)?;
        Ok(ArcCounts { counts })
    }

    /// Parses the `SUMMARY` record.
    ///
    /// # Errors
    ///
    /// * Returns [`Io`] on I/O failure.
    ///
    /// [`Io`]: ../error/enum.ErrorKind.html#variant.Io
    fn parse_summary(&mut self) -> Result<Summary> {
        trace!("summary-checksum @ 0x{:x}", self.cursor);
        let checksum = self.read_32()?;
        trace!("summary-num @ 0x{:x}", self.cursor);
        let num = self.read_32()?;
        trace!("summary-runs @ 0x{:x}", self.cursor);
        let runs = self.read_32()?;
        trace!("summary-sum @ 0x{:x}", self.cursor);
        let sum = self.read_64()?;
        trace!("summary-max @ 0x{:x}", self.cursor);
        let max = self.read_64()?;
        trace!("summary-sum-max @ 0x{:x}", self.cursor);
        let sum_max = self.read_64()?;

        trace!("summary-histogram @ 0x{:x}", self.cursor);
        let histogram = match self.read_histogram_bitvector() {
            Ok(bitvector) => {
                let mut bitpos = bitvector // @rustfmt-force-break
                    .iter()
                    .flat_map(|num| (0..32).map(move |i| num & 1 << i))
                    .enumerate()
                    .filter_map(|(i, b)| if b == 0 { None } else { Some(i as u32) });
                trace!("summary-histogram-buckets @ 0x{:x}", self.cursor);
                let buckets = self.until_eof(|s| {
                    let index = bitpos.next().unwrap_or(256);
                    trace!("histogram-bucket-num @ 0x{:x}", s.cursor);
                    let num = s.read_32()?;
                    trace!("histogram-bucket-min @ 0x{:x}", s.cursor);
                    let min = s.read_64()?;
                    trace!("histogram-bucket-sum @ 0x{:x}", s.cursor);
                    let sum = s.read_64()?;
                    Ok((index, HistogramBucket { num, min, sum }))
                })?;
                Some(Histogram { buckets })
            },
            Err(ref e) if e.is_eof() => None,
            Err(e) => bail!(e),
        };

        Ok(Summary {
            checksum,
            num,
            runs,
            sum,
            max,
            sum_max,
            histogram,
        })
    }
}

/// An iterator which reads from a reader until it produces an end-of-file error.
struct UntilEof<'a, S: 'a, T, F>(&'a mut S, F)
where
    F: FnMut(&mut S) -> Result<T>;

impl<'a, S: 'a, T, F> Iterator for UntilEof<'a, S, T, F>
where
    F: FnMut(&mut S) -> Result<T>,
{
    type Item = Result<T>;
    fn next(&mut self) -> Option<Result<T>> {
        match (self.1)(self.0) {
            Err(ref e) if e.is_eof() => {
                trace!("**** reached eof");
                None
            },
            x => Some(x),
        }
    }
}