squid 2.0.3

A RISC-V emulator with AOT compilation for fuzzing
Documentation
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
use std::cmp::Ordering;

use goblin;
use paste::paste;

use crate::{
    event::EventPool,
    frontend::{
        error::LoaderError,
        idmap::{
            idmap_functions,
            HasId,
            HasIdMut,
            Id,
            IdMap,
            IdMapValues,
            IdMapValuesMut,
        },
        image::VAddr,
        perms::Perms,
        symbol::{
            Symbol,
            SymbolParser,
        },
    },
    listing::ListingManager,
};

/// A Section is consecutive group of symbols that share the same permissions
#[derive(Debug, Hash)]
pub struct Section {
    id: Id,
    perms: Perms,
    vaddr: VAddr,
    offset: Option<usize>,
    size: usize,
    idmap: IdMap<Symbol>,
    cursor: usize,
}

impl Section {
    pub(crate) fn new(perms: Perms, vaddr: VAddr, offset: Option<usize>, size: usize) -> Self {
        Self {
            id: Id::default(),
            perms,
            vaddr,
            offset,
            size,
            idmap: IdMap::new(),
            cursor: 0,
        }
    }

    pub(crate) fn offset(&self) -> Option<usize> {
        self.offset
    }

    /// The last virtual address occupied by this section (size - 1)
    pub fn last_addr(&self) -> VAddr {
        self.vaddr + self.size as VAddr - 1
    }

    /// Check whether this section contains the givem virtual address
    pub fn contains_address(&self, vaddr: VAddr) -> bool {
        self.vaddr <= vaddr && vaddr <= self.last_addr()
    }

    /// The size of this section
    pub fn size(&self) -> usize {
        self.size
    }

    /// The permissions of this section
    pub fn perms(&self) -> Perms {
        self.perms
    }

    /// The virtual address of this section
    pub fn vaddr(&self) -> VAddr {
        self.vaddr
    }

    /// Change the size of this section
    pub fn set_size(&mut self, size: usize) {
        self.size = size;
    }

    /// Change the permissions of this section
    pub fn set_perms(&mut self, perms: Perms) {
        self.perms = perms;
    }

    /// Change the virtual address of this section
    pub fn set_vaddr(&mut self, vaddr: VAddr) {
        self.vaddr = vaddr;
    }

    /// Create a [`SectionBuilder`] that can build Sections from scratch
    pub fn builder() -> SectionBuilder {
        SectionBuilder {
            perms: None,
            vaddr: 0,
            size: None,
        }
    }
}

idmap_functions!(Section, Symbol, symbol);

impl HasId for Section {
    fn id(&self) -> Id {
        self.id
    }
}

impl HasIdMut for Section {
    fn id_mut(&mut self) -> &mut Id {
        &mut self.id
    }
}

/// The SectionBuilder can be used to build a [`Section`] from scratch
pub struct SectionBuilder {
    perms: Option<Perms>,
    vaddr: VAddr,
    size: Option<usize>,
}

impl SectionBuilder {
    /// Set the permissions of this section
    pub fn perms(mut self, perms: Perms) -> Self {
        self.perms = Some(perms);
        self
    }

    /// Set the virtual address of this section
    pub fn vaddr(mut self, vaddr: VAddr) -> Self {
        self.vaddr = vaddr;
        self
    }

    /// Set the size of this section
    pub fn size(mut self, size: usize) -> Self {
        self.size = Some(size);
        self
    }

    /// Finally, create the [`Section`]
    pub fn build(self) -> Result<Section, &'static str> {
        let perms = self.perms.ok_or("Section permissions were not set")?;
        let size = self.size.ok_or("Section size was not set")?;
        Ok(Section::new(perms, self.vaddr, None, size))
    }
}

pub(crate) struct SectionParser {
    sections: Vec<Section>,
}

impl SectionParser {
    pub(crate) fn parse(
        elf: &goblin::elf::Elf,
        content: &[u8],
        listing: &ListingManager,
        event_pool: &mut EventPool,
    ) -> Result<IdMap<Section>, LoaderError> {
        /* Parse sections */
        let mut parser = Self::new();
        parser.parse_program_headers(elf)?;
        parser.parse_section_headers(elf)?;
        parser.merge_sections();
        parser.verify(elf)?;

        /*
        #[cfg(test)]
        {
            parser.dump_sections();
        }
        */

        /* Parse symbols */
        for section in &mut parser.sections {
            section.idmap = SymbolParser::parse(elf, section, content, listing, event_pool)?;
        }

        /* Build IdMap */
        let mut map = IdMap::new();

        for section in parser.sections {
            map.insert(section);
        }

        Ok(map)
    }

    fn new() -> Self {
        Self {
            sections: Vec::new(),
        }
    }

    fn parse_program_headers(&mut self, elf: &goblin::elf::Elf) -> Result<(), LoaderError> {
        for ph in &elf.program_headers {
            if ph.p_type == goblin::elf::program_header::PT_LOAD {
                /* Parse permissions.
                  The compiler might merge r-x and r-- sections into the
                  same r-x segment so we ignore the x bit in the segment flags
                  since we don't want to lift data into the IR.
                  The only time a squid::Section can become executable is when the
                  underlying ELF section is executable.
                */
                let mut perms = Perms::from_segment_flags(ph.p_flags);

                if perms.is_inaccessible() {
                    continue;
                }

                if perms.is_readable() || perms.is_writable() {
                    perms.clear_executable();
                }

                if ph.p_memsz < ph.p_filesz {
                    return Err(LoaderError::InvalidELF("Segment memsz < filesz".to_string()));
                }

                /* Parse memory layout. Split segment on p_filesz. */
                let uninit_size = ph.p_memsz - ph.p_filesz;

                if ph.p_filesz > 0 {
                    self.add_section(
                        perms,
                        ph.p_vaddr as VAddr,
                        Some(ph.p_offset as usize),
                        ph.p_filesz as usize,
                        true,
                    )?;
                }

                if uninit_size > 0 {
                    self.add_section(perms, (ph.p_vaddr + ph.p_filesz) as VAddr, None, uninit_size as usize, true)?;
                }
            }
        }

        Ok(())
    }

    fn parse_section_headers(&mut self, elf: &goblin::elf::Elf) -> Result<(), LoaderError> {
        for sh in &elf.section_headers {
            if sh.is_alloc() && (sh.sh_flags & goblin::elf::section_header::SHF_TLS as u64) == 0 {
                let offset = match sh.sh_type {
                    goblin::elf::section_header::SHT_NOBITS => None,
                    _ => Some(sh.sh_offset as usize),
                };

                self.add_section(
                    Perms::from_section_header(sh),
                    sh.sh_addr as VAddr,
                    offset,
                    sh.sh_size as usize,
                    false,
                )?;
            }
        }

        Ok(())
    }

    fn add_section(
        &mut self,
        perms: Perms,
        vaddr: VAddr,
        offset: Option<usize>,
        size: usize,
        from_segment: bool,
    ) -> Result<(), LoaderError> {
        if size == 0 {
            return Ok(());
        } else if perms.is_writable() && perms.is_executable() {
            return Err(LoaderError::InvalidELF("Binary contains rwx sections/segments".to_string()));
        }

        let new_section = Section::new(perms, vaddr, offset, size);

        match self.locate_section(&new_section) {
            Ok(idx) => {
                if from_segment {
                    return Err(LoaderError::InvalidELF("Found overlapping segments".to_string()));
                }

                /* If we have a duplicate just update the permissions and offset */
                let old_section = &mut self.sections[idx];
                if new_section.vaddr == old_section.vaddr && new_section.last_addr() == old_section.last_addr() {
                    old_section.perms = new_section.perms;
                    old_section.offset = new_section.offset;
                    return Ok(());
                }

                /* Otherwise we have to split the old section into two or three parts */
                let mut old_section = self.sections.remove(idx);

                if new_section.vaddr() == old_section.vaddr() {
                    old_section.vaddr += new_section.size() as VAddr;
                    if let Some(offset) = &mut old_section.offset {
                        *offset += new_section.size();
                    }
                    old_section.size -= new_section.size();

                    self.sections.insert(idx, new_section);
                    self.sections.insert(idx + 1, old_section);
                } else if new_section.last_addr() == old_section.last_addr() {
                    old_section.size -= new_section.size();

                    self.sections.insert(idx, old_section);
                    self.sections.insert(idx + 1, new_section);
                } else {
                    let mut left =
                        Section::new(old_section.perms, old_section.vaddr, old_section.offset, old_section.size);
                    let mut right = old_section;

                    left.size = (new_section.vaddr - left.vaddr) as usize;

                    let shift_amount = left.size() + new_section.size();
                    right.vaddr += shift_amount as VAddr;
                    if let Some(offset) = &mut right.offset {
                        *offset += shift_amount;
                    }
                    right.size -= shift_amount;

                    self.sections.insert(idx, left);
                    self.sections.insert(idx + 1, new_section);
                    self.sections.insert(idx + 2, right);
                }
            },
            Err(idx) => {
                if !from_segment {
                    return Err(LoaderError::InvalidELF(format!("Section not in any segment: {:#x}", vaddr)));
                }

                self.sections.insert(idx, new_section);
            },
        }

        Ok(())
    }

    fn locate_section(&self, section: &Section) -> Result<usize, usize> {
        self.sections.binary_search_by(|x| {
            if x.contains_address(section.vaddr) && x.contains_address(section.last_addr()) {
                Ordering::Equal
            } else {
                let l = x.vaddr.cmp(&section.vaddr);
                let r = x.vaddr.cmp(&section.last_addr());
                assert_eq!(l, r, "Overlapping sections: x={:?} section={:?}", x, section);
                l
            }
        })
    }

    fn merge_sections(&mut self) {
        let mut i = 1;

        while i < self.sections.len() {
            let l = &self.sections[i - 1];
            let r = &self.sections[i];

            let same_perms = l.perms() == r.perms();
            let cont_vaddr = l.vaddr() + l.size() as VAddr == r.vaddr();
            let cont_offset = l.offset().map(|x| x + l.size()) == r.offset();

            if same_perms && cont_vaddr && cont_offset {
                let section = self.sections.remove(i);
                self.sections[i - 1].size += section.size();
            } else {
                i += 1;
            }
        }
    }

    fn verify(&self, elf: &goblin::elf::Elf) -> Result<(), LoaderError> {
        /* Check for continuous memory layout */
        for ph in &elf.program_headers {
            if ph.p_type == goblin::elf::program_header::PT_LOAD {
                /* Find starting index */
                let mut idx = 0;

                while idx < self.sections.len() && self.sections[idx].vaddr < ph.p_vaddr {
                    idx += 1;
                }

                assert!(idx < self.sections.len());

                /* Check for continuity */
                let end_addr = ph.p_vaddr + ph.p_filesz;
                let mut vaddr_cursor = ph.p_vaddr as VAddr;

                while idx < self.sections.len() && self.sections[idx].vaddr < end_addr {
                    assert_eq!(self.sections[idx].vaddr(), vaddr_cursor);
                    vaddr_cursor += self.sections[idx].size() as VAddr;
                    idx += 1;
                }

                assert_eq!(vaddr_cursor, end_addr);
            }
        }

        /* Verify each section */
        for section in &self.sections {
            assert!(!section.perms().is_inaccessible());
            assert!(section.size() > 0);
        }

        Ok(())
    }

    /*
    #[cfg(test)]
    pub fn dump_sections(&self) {
        for section in &self.sections {
            print!("{:08x}  ", section.vaddr());

            if let Some(offset) = section.offset() {
                print!("{:06x}", offset);
            } else {
                print!("      ");
            }

            print!("  {:04x}  ", section.size());

            let perms = section.perms();
            let r = if perms.is_readable() { "r" } else { "-" };
            let w = if perms.is_writable() { "w" } else { "-" };
            let x = if perms.is_executable() { "x" } else { "-" };
            println!("{}{}{}", r, w, x);
        }
    }
    */
}