Coverage Report

Created: 2025-01-09 07:53

/src/wasm-tools/crates/wasmparser/src/readers/component/names.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::{BinaryReader, BinaryReaderError, NameMap, Result, Subsection, Subsections};
2
use core::ops::Range;
3
4
/// Type used to iterate and parse the contents of the `component-name` custom
5
/// section in compnents, similar to the `name` section of core modules.
6
pub type ComponentNameSectionReader<'a> = Subsections<'a, ComponentName<'a>>;
7
8
/// Represents a name read from the names custom section.
9
#[derive(Clone)]
10
#[allow(missing_docs)]
11
pub enum ComponentName<'a> {
12
    Component {
13
        name: &'a str,
14
        name_range: Range<usize>,
15
    },
16
    CoreFuncs(NameMap<'a>),
17
    CoreGlobals(NameMap<'a>),
18
    CoreMemories(NameMap<'a>),
19
    CoreTables(NameMap<'a>),
20
    CoreModules(NameMap<'a>),
21
    CoreInstances(NameMap<'a>),
22
    CoreTypes(NameMap<'a>),
23
    Types(NameMap<'a>),
24
    Instances(NameMap<'a>),
25
    Components(NameMap<'a>),
26
    Funcs(NameMap<'a>),
27
    Values(NameMap<'a>),
28
29
    /// An unknown [name subsection](https://webassembly.github.io/spec/core/appendix/custom.html#subsections).
30
    Unknown {
31
        /// The identifier for this subsection.
32
        ty: u8,
33
        /// The contents of this subsection.
34
        data: &'a [u8],
35
        /// The range of bytes, relative to the start of the original data
36
        /// stream, that the contents of this subsection reside in.
37
        range: Range<usize>,
38
    },
39
}
40
41
impl<'a> Subsection<'a> for ComponentName<'a> {
42
0
    fn from_reader(id: u8, mut reader: BinaryReader<'a>) -> Result<Self> {
43
0
        let data = reader.remaining_buffer();
44
0
        let offset = reader.original_position();
45
0
        Ok(match id {
46
            0 => {
47
0
                let name = reader.read_string()?;
48
0
                if !reader.eof() {
49
0
                    return Err(BinaryReaderError::new(
50
0
                        "trailing data at the end of a name",
51
0
                        reader.original_position(),
52
0
                    ));
53
0
                }
54
0
                ComponentName::Component {
55
0
                    name,
56
0
                    name_range: offset..reader.original_position(),
57
0
                }
58
            }
59
            1 => {
60
0
                let ctor: fn(NameMap<'a>) -> ComponentName<'a> = match reader.read_u8()? {
61
0
                    0x00 => match reader.read_u8()? {
62
0
                        0x00 => ComponentName::CoreFuncs,
63
0
                        0x01 => ComponentName::CoreTables,
64
0
                        0x02 => ComponentName::CoreMemories,
65
0
                        0x03 => ComponentName::CoreGlobals,
66
0
                        0x10 => ComponentName::CoreTypes,
67
0
                        0x11 => ComponentName::CoreModules,
68
0
                        0x12 => ComponentName::CoreInstances,
69
                        _ => {
70
0
                            return Ok(ComponentName::Unknown {
71
0
                                ty: 1,
72
0
                                data,
73
0
                                range: offset..offset + data.len(),
74
0
                            });
75
                        }
76
                    },
77
0
                    0x01 => ComponentName::Funcs,
78
0
                    0x02 => ComponentName::Values,
79
0
                    0x03 => ComponentName::Types,
80
0
                    0x04 => ComponentName::Components,
81
0
                    0x05 => ComponentName::Instances,
82
                    _ => {
83
0
                        return Ok(ComponentName::Unknown {
84
0
                            ty: 1,
85
0
                            data,
86
0
                            range: offset..offset + data.len(),
87
0
                        });
88
                    }
89
                };
90
0
                ctor(NameMap::new(reader.shrink())?)
91
            }
92
0
            ty => ComponentName::Unknown {
93
0
                ty,
94
0
                data,
95
0
                range: offset..offset + data.len(),
96
0
            },
97
        })
98
0
    }
99
}