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
//! Contains logic related to finding logs in the elf and parsing them.

use std::collections::HashMap;

use gimli::Reader;
use object::{AddressSize, Object, ObjectSection, ReadRef};

use crate::{
    dwarf::Dwarf,
    log::{Log, MetadataV1, Schema},
    r#type::Type,
    var::Var,
    Error, Result,
};

/// Responsible for parsing logs from the elf.
pub struct Parser<'data> {
    log_cache: HashMap<usize, (MetadataV1, Type)>,
    logs_section: &'data [u8],
    build_id: &'data [u8],
    dwarf: Dwarf<'data>,
    address_size: AddressSize,
}

impl<'data> Parser<'data> {
    /// Creates a new Parser from elf data.
    pub fn new<R: ReadRef<'data>>(data: R) -> Result<Self> {
        let file = object::File::parse(data)?;
        let dwarf = Dwarf::new(&file)?;
        let build_id = file
            .build_id()?
            .ok_or(Error::Custom("Unable to find build ID in elf!"))?;

        let address_size = file.architecture().address_size().ok_or(Error::Custom(
            "Unsupported architecture, no address size information!",
        ))?;

        Ok(Parser {
            log_cache: Default::default(),
            logs_section: file
                .section_by_name(".cdefmt")
                .ok_or(Error::MissingSection)?
                .data()?,
            build_id,
            dwarf,
            address_size,
        })
    }

    // Parses a log and returns it.
    pub fn parse_log(&mut self, data: &[u8]) -> Result<Log> {
        let mut data = gimli::EndianSlice::new(data, self.dwarf.endian());
        // TODO: Make safer, maybe switch to u64 everywhere.
        let log_id = data.read_address(self.address_size.bytes())? as usize;

        if log_id >= self.logs_section.len() {
            return Err(Error::OutOfBounds(log_id, self.logs_section.len()));
        }

        if !self.log_cache.contains_key(&log_id) {
            // Parse log metadata and type if we don't have it cached.
            let log_info = self.parse_log_metadata(log_id)?;
            let ty = self.parse_log_args_type(&log_info)?;
            self.log_cache.insert(log_id, (log_info, ty));
        };

        // Unwrap safety: made sure that the entry exists right above here.
        let (metadata, ty) = self.log_cache.get(&log_id).unwrap();

        let args = self.parse_log_args(ty, data)?;
        let log = Log::new(metadata.clone(), args);

        if log_id == 0 {
            self.validate_init(&log)?
        }

        Ok(log)
    }

    // Parses the log's static information.
    fn parse_log_metadata(&self, log_id: usize) -> Result<MetadataV1> {
        let json = &self.logs_section[log_id..]
            .split(|b| *b == 0)
            .next()
            .ok_or(Error::NoNullTerm)?;
        let json = std::str::from_utf8(json).map_err(|e| Error::Utf8(log_id, e))?;
        let schema: Schema = serde_json::from_str(json)?;

        let mut metadata = match schema.schema {
            1 => serde_json::from_str::<MetadataV1>(json),
            _ => return Err(Error::Schema(schema.schema)),
        }?;

        metadata.id = log_id;

        Ok(metadata)
    }

    // Parses the log's arguments.
    fn parse_log_args_type(&self, metadata: &MetadataV1) -> Result<Type> {
        let type_name = format!("cdefmt_log_args_t{}", metadata.counter);
        self.dwarf
            .get_type(&metadata.file, &type_name)?
            .ok_or_else(|| Error::Custom("Unable to find arguments type information!"))
    }

    // Parses the log's arguments.
    fn parse_log_args<R: Reader>(&self, ty: &Type, mut data: R) -> Result<Vec<Var>> {
        let members = if let Type::Structure(members) = ty {
            members
        } else {
            return Err(Error::Custom("The log's args aren't a structure!"));
        };

        // Parse the raw data into `Var` representation.
        members
            .iter()
            // Due to the way the log is constructed in the c code, the first argument is always the
            // log id, we already have it.
            .skip(1)
            .map(|m| Ok(Var::parse(&m.ty, &mut data)?.0))
            .collect()
    }

    fn validate_init(&self, log: &Log) -> Result<()> {
        let args = log.get_args();
        if let Some(Var::Array(build_id)) = args.first() {
            let build_id = build_id
                .iter()
                .map(|b| match b {
                    Var::U8(b) => Ok(*b),
                    _ => Err(Error::Custom("Build ID data contains non u8 element!")),
                })
                .collect::<Result<Vec<_>>>()?;
            if self.build_id != build_id {
                Err(Error::Custom("Build ID mismatch!"))
            } else {
                Ok(())
            }
        } else {
            Err(Error::Custom("Build ID missing or not an array"))
        }
    }
}