gsym-rs 0.1.4

Pure-Rust reader, writer, and Linux ELF/DWARF converter for LLVM GSYM
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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
use std::num::NonZeroUsize;

use crate::endian::{Cursor, Encoder, Endian};
use crate::error::{Error, Result};
use crate::format::{INFO_CALL_SITE, INFO_END, INFO_INLINE, INFO_LINE_TABLE, INFO_MERGED};
use crate::model::{AddressRange, LineEntry};

use super::leb::{read_uleb, write_uleb};
use super::line;

pub(crate) const MAX_INLINE_DEPTH: usize = 256;

pub(crate) const MAX_MERGED_DEPTH: usize = 16;

pub(crate) const fn check_inline_depth(depth: usize) -> Result<()> {
    if depth > MAX_INLINE_DEPTH {
        return Err(Error::Limit {
            context: "inline tree depth",
            value: depth as u64,
            limit: MAX_INLINE_DEPTH as u64,
        });
    }
    Ok(())
}

pub(crate) const fn check_merged_depth(depth: usize) -> Result<()> {
    if depth > MAX_MERGED_DEPTH {
        return Err(Error::Limit {
            context: "merged-function tree depth",
            value: depth as u64,
            limit: MAX_MERGED_DEPTH as u64,
        });
    }
    Ok(())
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum InfoType {
    LineTable,
    Inline,
    Merged,
    CallSite,
    Unknown(u32),
}

impl InfoType {
    const fn from_raw(value: u32) -> Self {
        match value {
            INFO_LINE_TABLE => Self::LineTable,
            INFO_INLINE => Self::Inline,
            INFO_MERGED => Self::Merged,
            INFO_CALL_SITE => Self::CallSite,
            other => Self::Unknown(other),
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub(crate) struct InfoRecord<'data> {
    pub(crate) kind: InfoType,
    pub(crate) payload: &'data [u8],
}

#[derive(Default)]
pub(crate) struct RecordSet {
    bits: u8,
}

impl RecordSet {
    pub(crate) const fn observe(&mut self, kind: InfoType) -> Result<()> {
        let (bit, message) = match kind {
            InfoType::LineTable => (1 << 0, "duplicate line-table record"),
            InfoType::Inline => (1 << 1, "duplicate inline-info record"),
            InfoType::Merged => (1 << 2, "duplicate merged-functions record"),
            InfoType::CallSite => (1 << 3, "duplicate call-site record"),
            InfoType::Unknown(_) => return Ok(()),
        };
        if self.bits & bit != 0 {
            Err(Error::InvalidFormat(message))
        } else {
            self.bits |= bit;
            Ok(())
        }
    }
}

pub(crate) fn next_record<'data>(
    cursor: &mut Cursor<'data>,
    ended: &mut bool,
) -> Result<Option<InfoRecord<'data>>> {
    if *ended {
        return Ok(None);
    }
    let raw_kind = cursor.read_u32()?;
    let length = usize::try_from(cursor.read_u32()?)
        .map_err(|_| Error::Overflow("FunctionInfo record length"))?;
    if raw_kind == INFO_END {
        *ended = true;
        if length != 0 {
            return Err(Error::InvalidFormat(
                "FunctionInfo end marker has a nonzero length",
            ));
        }
        return Ok(None);
    }
    Ok(Some(InfoRecord {
        kind: InfoType::from_raw(raw_kind),
        payload: cursor.take(length)?,
    }))
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(crate) struct EncodedInlineNode {
    pub(crate) ranges: Vec<AddressRange>,
    pub(crate) name: u64,
    pub(crate) call_file: u32,
    pub(crate) call_line: u32,
    pub(crate) children: Vec<Self>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(crate) struct EncodedCallSite {
    pub(crate) return_offset: u64,
    pub(crate) flags: u8,
    pub(crate) match_regex: Vec<u64>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(crate) struct EncodedFunction {
    pub(crate) range: AddressRange,
    pub(crate) name: u64,
    pub(crate) lines: Option<Vec<LineEntry>>,
    pub(crate) inline: Option<EncodedInlineNode>,
    pub(crate) merged: Vec<Self>,
    pub(crate) call_sites: Vec<EncodedCallSite>,
}

pub(crate) fn decode(
    bytes: &[u8],
    endian: Endian,
    string_offset_size: u8,
    base: u64,
) -> Result<EncodedFunction> {
    let _ = string_offset_width(string_offset_size)?;
    let mut cursor = Cursor::new(bytes, endian);
    decode_from(&mut cursor, string_offset_size, base, 0)
}

#[cfg(test)]
pub(crate) fn decode_exact(
    bytes: &[u8],
    endian: Endian,
    string_offset_size: u8,
    base: u64,
) -> Result<EncodedFunction> {
    decode_exact_at(bytes, endian, string_offset_size, base, 0)
}

fn decode_exact_at(
    bytes: &[u8],
    endian: Endian,
    string_offset_size: u8,
    base: u64,
    depth: usize,
) -> Result<EncodedFunction> {
    let _ = string_offset_width(string_offset_size)?;
    let mut cursor = Cursor::new(bytes, endian);
    let function = decode_from(&mut cursor, string_offset_size, base, depth)?;
    if !cursor.is_empty() {
        return Err(Error::InvalidFormat(
            "bytes remain after the FunctionInfo end marker",
        ));
    }
    Ok(function)
}

pub(crate) fn decode_from(
    cursor: &mut Cursor<'_>,
    string_offset_size: u8,
    base: u64,
    depth: usize,
) -> Result<EncodedFunction> {
    check_merged_depth(depth)?;
    let _ = string_offset_width(string_offset_size)?;
    let size = u64::from(cursor.read_u32()?);
    let name = cursor.read_uint(string_offset_size)?;
    if name == 0 {
        return Err(Error::ZeroNameOffset);
    }
    let end = base
        .checked_add(size)
        .ok_or(Error::Overflow("FunctionInfo range end"))?;
    let mut function = EncodedFunction {
        range: AddressRange::new(base, end),
        name,
        ..EncodedFunction::default()
    };
    let mut seen = RecordSet::default();
    let mut ended = false;

    while let Some(record) = next_record(cursor, &mut ended)? {
        seen.observe(record.kind)?;
        match record.kind {
            InfoType::LineTable => {
                function.lines = Some(line::decode(record.payload, cursor.endian(), base)?);
            }
            InfoType::Inline => {
                let mut data = Cursor::new(record.payload, cursor.endian());
                let inline = decode_inline(&mut data, string_offset_size, base, 0)?;
                if inline.ranges.is_empty() {
                    return Err(Error::InvalidFormat(
                        "top-level inline info has no address ranges",
                    ));
                }
                if !data.is_empty() {
                    return Err(Error::InvalidFormat("trailing inline-info bytes"));
                }
                function.inline = Some(inline);
            }
            InfoType::Merged => {
                function.merged = decode_merged(
                    record.payload,
                    cursor.endian(),
                    string_offset_size,
                    base,
                    depth.saturating_add(1),
                )?;
            }
            InfoType::CallSite => {
                function.call_sites =
                    decode_call_sites(record.payload, cursor.endian(), string_offset_size)?;
            }
            InfoType::Unknown(other) => return Err(Error::UnsupportedInfoType(other)),
        }
    }
    Ok(function)
}

#[cfg(test)]
pub(crate) fn encode(
    function: &EncodedFunction,
    endian: Endian,
    string_offset_size: u8,
) -> Result<Vec<u8>> {
    let mut output = Encoder::new(endian);
    encode_into(function, &mut output, string_offset_size, false)?;
    Ok(output.into_inner())
}

pub(crate) fn encode_into(
    function: &EncodedFunction,
    output: &mut Encoder,
    string_offset_size: u8,
    align: bool,
) -> Result<usize> {
    encode_into_at(function, output, string_offset_size, align, 0)
}

fn encode_into_at(
    function: &EncodedFunction,
    output: &mut Encoder,
    string_offset_size: u8,
    align: bool,
    depth: usize,
) -> Result<usize> {
    check_merged_depth(depth)?;
    let _ = string_offset_width(string_offset_size)?;
    if align {
        output.align_to(4)?;
    }
    let start = output.len();
    if function.name == 0 {
        return Err(Error::ZeroNameOffset);
    }
    if function.range.end < function.range.start {
        return Err(Error::InvalidModel("function range end precedes its start"));
    }
    let size = function.range.end.saturating_sub(function.range.start);
    output.write_u32(u32::try_from(size).map_err(|_| Error::Limit {
        context: "FunctionInfo size",
        value: size,
        limit: u64::from(u32::MAX),
    })?);
    output.write_uint(function.name, string_offset_size)?;

    if let Some(lines) = &function.lines {
        write_record(output, INFO_LINE_TABLE, |output| {
            line::encode_into(lines, output, function.range.start)
        })?;
    }
    if let Some(inline) = &function.inline {
        write_record(output, INFO_INLINE, |output| {
            encode_inline(inline, output, string_offset_size, function.range.start, 0)
        })?;
    }
    if !function.merged.is_empty() {
        write_record(output, INFO_MERGED, |output| {
            encode_merged_into(
                &function.merged,
                output,
                string_offset_size,
                function.range.start,
                depth.saturating_add(1),
            )
        })?;
    }
    if !function.call_sites.is_empty() {
        write_record(output, INFO_CALL_SITE, |output| {
            encode_call_sites_into(&function.call_sites, output, string_offset_size)
        })?;
    }
    output.write_u32(INFO_END);
    output.write_u32(0);
    Ok(start)
}

fn write_record(
    output: &mut Encoder,
    info_type: u32,
    encode_payload: impl FnOnce(&mut Encoder) -> Result<()>,
) -> Result<()> {
    output.write_u32(info_type);
    let length_offset = output.len();
    output.write_u32(0);
    let payload_offset = output.len();
    encode_payload(output)?;
    let payload_len = output
        .len()
        .checked_sub(payload_offset)
        .ok_or(Error::Overflow("FunctionInfo record length"))?;
    let payload_len = u32::try_from(payload_len).map_err(|_| Error::Limit {
        context: "FunctionInfo record",
        value: payload_len as u64,
        limit: u64::from(u32::MAX),
    })?;
    output.patch_u32(length_offset, payload_len)
}

fn string_offset_width(size: u8) -> Result<NonZeroUsize> {
    NonZeroUsize::new(usize::from(size))
        .filter(|width| matches!(width.get(), 1 | 2 | 4 | 8))
        .ok_or_else(|| Error::OutOfRange {
            field: "string offset size",
            value: u64::from(size),
            max: 8,
        })
}

fn decode_ranges(cursor: &mut Cursor<'_>, base: u64) -> Result<Vec<AddressRange>> {
    let count = read_uleb(cursor)?;
    let count = usize::try_from(count).map_err(|_| Error::Overflow("address-range count"))?;
    if count > cursor.remaining() / 2 {
        return Err(Error::InvalidFormat(
            "address-range count exceeds remaining input",
        ));
    }
    let mut ranges: Vec<AddressRange> = Vec::with_capacity(count);
    for _ in 0..count {
        let start = base
            .checked_add(read_uleb(cursor)?)
            .ok_or(Error::Overflow("relative address range start"))?;
        let end = start
            .checked_add(read_uleb(cursor)?)
            .ok_or(Error::Overflow("address range end"))?;
        let range = AddressRange::new(start, end);
        if let Some(previous) = ranges.last()
            && range.start < previous.end
        {
            return Err(Error::InvalidFormat(
                "address ranges overlap or are not sorted",
            ));
        }
        ranges.push(range);
    }
    Ok(ranges)
}

fn encode_ranges(ranges: &[AddressRange], output: &mut Encoder, base: u64) -> Result<()> {
    write_uleb(
        output,
        u64::try_from(ranges.len()).map_err(|_| Error::Overflow("address-range count"))?,
    );
    let mut previous_end = None;
    for range in ranges {
        if range.start < base || range.end < range.start {
            return Err(Error::InvalidModel("invalid relative address range"));
        }
        if previous_end.is_some_and(|end| range.start < end) {
            return Err(Error::InvalidModel(
                "address ranges overlap or are not sorted",
            ));
        }
        write_uleb(
            output,
            range.start.checked_sub(base).ok_or(Error::InvalidModel(
                "address range precedes the base address",
            ))?,
        );
        write_uleb(output, range.end.saturating_sub(range.start));
        previous_end = Some(range.end);
    }
    Ok(())
}

fn decode_inline(
    cursor: &mut Cursor<'_>,
    string_offset_size: u8,
    base: u64,
    depth: usize,
) -> Result<EncodedInlineNode> {
    check_inline_depth(depth)?;
    let ranges = decode_ranges(cursor, base)?;
    if ranges.is_empty() {
        return Ok(EncodedInlineNode::default());
    }
    let has_children = cursor.read_u8()? != 0;
    let name = cursor.read_uint(string_offset_size)?;
    let call_file = read_uleb(cursor)?;
    let call_line = read_uleb(cursor)?;
    let mut node = EncodedInlineNode {
        ranges,
        name,
        call_file: u32::try_from(call_file).map_err(|_| Error::OutOfRange {
            field: "inline call-file index",
            value: call_file,
            max: u64::from(u32::MAX),
        })?,
        call_line: u32::try_from(call_line).map_err(|_| Error::OutOfRange {
            field: "inline call-line",
            value: call_line,
            max: u64::from(u32::MAX),
        })?,
        children: Vec::new(),
    };
    if has_children {
        let child_base = node
            .ranges
            .first()
            .ok_or(Error::InvalidFormat(
                "inline node with children has no range",
            ))?
            .start;
        loop {
            let child = decode_inline(
                cursor,
                string_offset_size,
                child_base,
                depth.saturating_add(1),
            )?;
            if child.ranges.is_empty() {
                break;
            }
            if child.ranges.iter().any(|range| {
                !node
                    .ranges
                    .iter()
                    .any(|parent| parent.contains_range(*range))
            }) {
                return Err(Error::InvalidFormat(
                    "inline child range is outside its parent",
                ));
            }
            node.children.push(child);
        }
    }
    Ok(node)
}

fn encode_inline(
    node: &EncodedInlineNode,
    output: &mut Encoder,
    string_offset_size: u8,
    base: u64,
    depth: usize,
) -> Result<()> {
    check_inline_depth(depth)?;
    if node.ranges.is_empty() {
        return Err(Error::InvalidModel("inline node must contain a range"));
    }
    encode_ranges(&node.ranges, output, base)?;
    output.write_u8(u8::from(!node.children.is_empty()));
    output.write_uint(node.name, string_offset_size)?;
    write_uleb(output, u64::from(node.call_file));
    write_uleb(output, u64::from(node.call_line));
    if !node.children.is_empty() {
        let child_base = node
            .ranges
            .first()
            .ok_or(Error::InvalidModel(
                "inline node with children has no range",
            ))?
            .start;
        for child in &node.children {
            if child.ranges.iter().any(|range| {
                !node
                    .ranges
                    .iter()
                    .any(|parent| parent.contains_range(*range))
            }) {
                return Err(Error::InvalidModel(
                    "inline child range is outside its parent",
                ));
            }
            encode_inline(
                child,
                output,
                string_offset_size,
                child_base,
                depth.saturating_add(1),
            )?;
        }
        write_uleb(output, 0);
    }
    Ok(())
}

fn decode_merged(
    bytes: &[u8],
    endian: Endian,
    string_offset_size: u8,
    base: u64,
    depth: usize,
) -> Result<Vec<EncodedFunction>> {
    let mut cursor = Cursor::new(bytes, endian);
    let count = usize::try_from(cursor.read_u32()?)
        .map_err(|_| Error::Overflow("merged-function count"))?;
    if count > cursor.remaining() / 4 {
        return Err(Error::InvalidFormat(
            "merged-function count exceeds remaining input",
        ));
    }
    let mut functions = Vec::with_capacity(count);
    for _ in 0..count {
        let length = usize::try_from(cursor.read_u32()?)
            .map_err(|_| Error::Overflow("merged FunctionInfo length"))?;
        let data = cursor.take(length)?;
        functions.push(decode_exact_at(
            data,
            endian,
            string_offset_size,
            base,
            depth,
        )?);
    }
    if !cursor.is_empty() {
        return Err(Error::InvalidFormat("trailing merged-function bytes"));
    }
    Ok(functions)
}

fn encode_merged_into(
    functions: &[EncodedFunction],
    output: &mut Encoder,
    string_offset_size: u8,
    base: u64,
    depth: usize,
) -> Result<()> {
    output.write_u32(u32::try_from(functions.len()).map_err(|_| Error::Limit {
        context: "merged-function count",
        value: functions.len() as u64,
        limit: u64::from(u32::MAX),
    })?);
    for function in functions {
        if function.range.start != base {
            return Err(Error::InvalidModel(
                "merged function start differs from its parent",
            ));
        }
        let length_offset = output.len();
        output.write_u32(0);
        let function_offset = output.len();
        encode_into_at(function, output, string_offset_size, false, depth)?;
        let function_len = output
            .len()
            .checked_sub(function_offset)
            .ok_or(Error::Overflow("merged FunctionInfo length"))?;
        let function_len = u32::try_from(function_len).map_err(|_| Error::Limit {
            context: "merged FunctionInfo length",
            value: function_len as u64,
            limit: u64::from(u32::MAX),
        })?;
        output.patch_u32(length_offset, function_len)?;
    }
    Ok(())
}

fn decode_call_sites(
    bytes: &[u8],
    endian: Endian,
    string_offset_size: u8,
) -> Result<Vec<EncodedCallSite>> {
    let mut cursor = Cursor::new(bytes, endian);
    let count =
        usize::try_from(cursor.read_u32()?).map_err(|_| Error::Overflow("call-site count"))?;
    let minimum_record_size = 8 + 1 + 4;
    if count > cursor.remaining() / minimum_record_size {
        return Err(Error::InvalidFormat(
            "call-site count exceeds remaining input",
        ));
    }
    let mut call_sites = Vec::with_capacity(count);
    for _ in 0..count {
        let return_offset = cursor.read_u64()?;
        let flags = cursor.read_u8()?;
        let regex_count = usize::try_from(cursor.read_u32()?)
            .map_err(|_| Error::Overflow("call-site regex count"))?;
        if regex_count > cursor.remaining() / string_offset_width(string_offset_size)? {
            return Err(Error::InvalidFormat(
                "call-site regex count exceeds remaining input",
            ));
        }
        let mut match_regex = Vec::with_capacity(regex_count);
        for _ in 0..regex_count {
            match_regex.push(cursor.read_uint(string_offset_size)?);
        }
        call_sites.push(EncodedCallSite {
            return_offset,
            flags,
            match_regex,
        });
    }
    if !cursor.is_empty() {
        return Err(Error::InvalidFormat("trailing call-site bytes"));
    }
    Ok(call_sites)
}

fn encode_call_sites_into(
    call_sites: &[EncodedCallSite],
    output: &mut Encoder,
    string_offset_size: u8,
) -> Result<()> {
    output.write_u32(u32::try_from(call_sites.len()).map_err(|_| Error::Limit {
        context: "call-site count",
        value: call_sites.len() as u64,
        limit: u64::from(u32::MAX),
    })?);
    for call_site in call_sites {
        output.write_u64(call_site.return_offset);
        output.write_u8(call_site.flags);
        output.write_u32(
            u32::try_from(call_site.match_regex.len()).map_err(|_| Error::Limit {
                context: "call-site regex count",
                value: call_site.match_regex.len() as u64,
                limit: u64::from(u32::MAX),
            })?,
        );
        for offset in &call_site.match_regex {
            output.write_uint(*offset, string_offset_size)?;
        }
    }
    Ok(())
}