wasmparser/readers/core/
code.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::{BinaryReader, FromReader, OperatorsReader, Result, SectionLimited, ValType};
17use core::ops::Range;
18
19/// A reader for the code section of a WebAssembly module.
20pub type CodeSectionReader<'a> = SectionLimited<'a, FunctionBody<'a>>;
21
22/// Represents a WebAssembly function body.
23#[derive(Debug, Clone)]
24pub struct FunctionBody<'a> {
25    reader: BinaryReader<'a>,
26}
27
28impl<'a> FunctionBody<'a> {
29    /// Constructs a new `FunctionBody` for the given data and offset.
30    pub fn new(reader: BinaryReader<'a>) -> Self {
31        Self { reader }
32    }
33
34    /// Gets a binary reader for this function body.
35    pub fn get_binary_reader(&self) -> BinaryReader<'a> {
36        self.reader.clone()
37    }
38
39    fn skip_locals(reader: &mut BinaryReader) -> Result<()> {
40        let count = reader.read_var_u32()?;
41        for _ in 0..count {
42            reader.read_var_u32()?;
43            reader.read::<ValType>()?;
44        }
45        Ok(())
46    }
47
48    /// Gets the locals reader for this function body.
49    pub fn get_locals_reader(&self) -> Result<LocalsReader<'a>> {
50        let mut reader = self.reader.clone();
51        let declaration_count = reader.read_var_u32()?;
52        Ok(LocalsReader {
53            reader,
54            declaration_count,
55            total_count: 0,
56        })
57    }
58
59    /// Gets a binary reader for this function body, after skipping locals.
60    pub fn get_binary_reader_for_operators(&self) -> Result<BinaryReader<'a>> {
61        let mut reader = self.reader.clone();
62        Self::skip_locals(&mut reader)?;
63        Ok(reader)
64    }
65
66    /// Uses [`FunctionBody::get_binary_reader_for_operators`] and then converts
67    /// that to an [`OperatorsReader`].
68    pub fn get_operators_reader(&self) -> Result<OperatorsReader<'a>> {
69        Ok(OperatorsReader::new(
70            self.get_binary_reader_for_operators()?,
71        ))
72    }
73
74    /// Gets the range of the function body.
75    pub fn range(&self) -> Range<usize> {
76        self.reader.range()
77    }
78
79    /// Returns the body of this function as a list of bytes.
80    ///
81    /// Note that the returned bytes start with the function locals declaration.
82    pub fn as_bytes(&self) -> &'a [u8] {
83        self.reader.remaining_buffer()
84    }
85}
86
87impl<'a> FromReader<'a> for FunctionBody<'a> {
88    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
89        let reader = reader.read_reader()?;
90        Ok(FunctionBody::new(reader))
91    }
92}
93
94/// A reader for a function body's locals.
95pub struct LocalsReader<'a> {
96    reader: BinaryReader<'a>,
97    declaration_count: u32,
98    total_count: u32,
99}
100
101impl<'a> LocalsReader<'a> {
102    /// Gets the count of locals declarations in the function body.
103    pub fn get_count(&self) -> u32 {
104        self.declaration_count
105    }
106
107    /// Gets the original position of the reader.
108    pub fn original_position(&self) -> usize {
109        self.reader.original_position()
110    }
111
112    /// Reads an item from the reader.
113    pub fn read(&mut self) -> Result<(u32, ValType)> {
114        let count = self.reader.read()?;
115        match self.total_count.checked_add(count) {
116            Some(total) => self.total_count = total,
117            None => bail!(self.reader.original_position(), "too many locals"),
118        }
119        let value_type = self.reader.read()?;
120        Ok((count, value_type))
121    }
122
123    /// Gets the binary reader from this LocalsReader
124    pub fn get_binary_reader(self) -> BinaryReader<'a> {
125        self.reader
126    }
127}
128
129impl<'a> IntoIterator for LocalsReader<'a> {
130    type Item = Result<(u32, ValType)>;
131    type IntoIter = LocalsIterator<'a>;
132    fn into_iter(self) -> Self::IntoIter {
133        let count = self.declaration_count;
134        LocalsIterator {
135            reader: self,
136            left: count,
137            err: false,
138        }
139    }
140}
141
142/// An iterator over locals in a function body.
143pub struct LocalsIterator<'a> {
144    reader: LocalsReader<'a>,
145    left: u32,
146    err: bool,
147}
148
149impl<'a> Iterator for LocalsIterator<'a> {
150    type Item = Result<(u32, ValType)>;
151    fn next(&mut self) -> Option<Self::Item> {
152        if self.err || self.left == 0 {
153            return None;
154        }
155        let result = self.reader.read();
156        self.err = result.is_err();
157        self.left -= 1;
158        Some(result)
159    }
160    fn size_hint(&self) -> (usize, Option<usize>) {
161        let count = self.reader.get_count() as usize;
162        (count, Some(count))
163    }
164}
165
166impl<'a> LocalsIterator<'a> {
167    /// After reading the locals, the BinaryReader is ready to read the operators.
168    pub fn into_binary_reader_for_operators(self) -> BinaryReader<'a> {
169        debug_assert!(self.err || self.left == 0);
170        self.reader.get_binary_reader()
171    }
172
173    /// After reading the locals, the BinaryReader is ready to read the operators.
174    pub fn into_operators_reader(self) -> OperatorsReader<'a> {
175        OperatorsReader::new(self.reader.get_binary_reader())
176    }
177}