wasmparser/readers/core/
names.rs

1/* Copyright 2018 Mozilla Foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16use crate::{
17    BinaryReader, BinaryReaderError, FromReader, Result, SectionLimited, Subsection, Subsections,
18};
19use core::ops::Range;
20
21/// Represents a name map from the names custom section.
22pub type NameMap<'a> = SectionLimited<'a, Naming<'a>>;
23
24/// Represents a name for an index from the names section.
25#[derive(Debug, Copy, Clone)]
26pub struct Naming<'a> {
27    /// The index being named.
28    pub index: u32,
29    /// The name for the index.
30    pub name: &'a str,
31}
32
33impl<'a> FromReader<'a> for Naming<'a> {
34    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
35        let index = reader.read_var_u32()?;
36        // This seems to match what browsers do where they don't limit the
37        // length of names in the `name` section while they do limit the names
38        // in the import and export section for example.
39        let name = reader.read_unlimited_string()?;
40        Ok(Naming { index, name })
41    }
42}
43
44/// Represents a reader for indirect names from the names custom section.
45pub type IndirectNameMap<'a> = SectionLimited<'a, IndirectNaming<'a>>;
46
47/// Represents an indirect name in the names custom section.
48#[derive(Debug, Clone)]
49pub struct IndirectNaming<'a> {
50    /// The indirect index of the name.
51    pub index: u32,
52    /// The map of names within the `index` prior.
53    pub names: NameMap<'a>,
54}
55
56impl<'a> FromReader<'a> for IndirectNaming<'a> {
57    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
58        let index = reader.read_var_u32()?;
59
60        // Skip the `NameMap` manually here.
61        //
62        // FIXME(#188) shouldn't need to skip here
63        let names = reader.skip(|reader| {
64            let count = reader.read_var_u32()?;
65            for _ in 0..count {
66                reader.read_var_u32()?;
67                reader.skip_string()?;
68            }
69            Ok(())
70        })?;
71
72        Ok(IndirectNaming {
73            index,
74            names: NameMap::new(names)?,
75        })
76    }
77}
78
79/// Represents a name read from the names custom section.
80#[derive(Clone)]
81pub enum Name<'a> {
82    /// The name is for the module.
83    Module {
84        /// The specified name.
85        name: &'a str,
86        /// The byte range that `name` occupies in the original binary.
87        name_range: Range<usize>,
88    },
89    /// The name is for the functions.
90    Function(NameMap<'a>),
91    /// The name is for the function locals.
92    Local(IndirectNameMap<'a>),
93    /// The name is for the function labels.
94    Label(IndirectNameMap<'a>),
95    /// The name is for the types.
96    Type(NameMap<'a>),
97    /// The name is for the tables.
98    Table(NameMap<'a>),
99    /// The name is for the memories.
100    Memory(NameMap<'a>),
101    /// The name is for the globals.
102    Global(NameMap<'a>),
103    /// The name is for the element segments.
104    Element(NameMap<'a>),
105    /// The name is for the data segments.
106    Data(NameMap<'a>),
107    /// The name is for fields.
108    Field(IndirectNameMap<'a>),
109    /// The name is for tags.
110    Tag(NameMap<'a>),
111    /// An unknown [name subsection](https://webassembly.github.io/spec/core/appendix/custom.html#subsections).
112    Unknown {
113        /// The identifier for this subsection.
114        ty: u8,
115        /// The contents of this subsection.
116        data: &'a [u8],
117        /// The range of bytes, relative to the start of the original data
118        /// stream, that the contents of this subsection reside in.
119        range: Range<usize>,
120    },
121}
122
123/// A reader for the name custom section of a WebAssembly module.
124pub type NameSectionReader<'a> = Subsections<'a, Name<'a>>;
125
126impl<'a> Subsection<'a> for Name<'a> {
127    fn from_reader(id: u8, mut reader: BinaryReader<'a>) -> Result<Self> {
128        let data = reader.remaining_buffer();
129        let offset = reader.original_position();
130        Ok(match id {
131            0 => {
132                let name = reader.read_string()?;
133                if !reader.eof() {
134                    return Err(BinaryReaderError::new(
135                        "trailing data at the end of a name",
136                        reader.original_position(),
137                    ));
138                }
139                Name::Module {
140                    name,
141                    name_range: offset..reader.original_position(),
142                }
143            }
144            1 => Name::Function(NameMap::new(reader)?),
145            2 => Name::Local(IndirectNameMap::new(reader)?),
146            3 => Name::Label(IndirectNameMap::new(reader)?),
147            4 => Name::Type(NameMap::new(reader)?),
148            5 => Name::Table(NameMap::new(reader)?),
149            6 => Name::Memory(NameMap::new(reader)?),
150            7 => Name::Global(NameMap::new(reader)?),
151            8 => Name::Element(NameMap::new(reader)?),
152            9 => Name::Data(NameMap::new(reader)?),
153            10 => Name::Field(IndirectNameMap::new(reader)?),
154            11 => Name::Tag(NameMap::new(reader)?),
155            ty => Name::Unknown {
156                ty,
157                data,
158                range: offset..offset + data.len(),
159            },
160        })
161    }
162}