Coverage Report

Created: 2023-04-25 07:07

/rust/registry/src/index.crates.io-6f17d22bba15001f/wasmparser-nostd-0.91.0/src/readers.rs
Line
Count
Source (jump to first uncovered line)
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
16
use crate::{BinaryReaderError, Result};
17
use ::core::ops::Range;
18
19
mod component;
20
mod core;
21
22
pub use self::component::*;
23
pub use self::core::*;
24
25
/// A trait implemented by section readers.
26
pub trait SectionReader {
27
    /// The item returned by the reader.
28
    type Item;
29
30
    /// Reads an item from the section.
31
    fn read(&mut self) -> Result<Self::Item>;
32
33
    /// Determines if the reader is at end-of-section.
34
    fn eof(&self) -> bool;
35
36
    /// Gets the original position of the reader.
37
    fn original_position(&self) -> usize;
38
39
    /// Gets the range of the reader.
40
    fn range(&self) -> Range<usize>;
41
42
    /// Ensures the reader is at the end of the section.
43
    ///
44
    /// This methods returns an error if there is more data in the section
45
    /// than what is described by the section's header.
46
26.0k
    fn ensure_end(&self) -> Result<()> {
47
26.0k
        if self.eof() {
48
26.0k
            return Ok(());
49
0
        }
50
0
        Err(BinaryReaderError::new(
51
0
            "section size mismatch: unexpected data at the end of the section",
52
0
            self.original_position(),
53
0
        ))
54
26.0k
    }
<wasmparser_nostd::readers::core::data::DataSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Line
Count
Source
46
901
    fn ensure_end(&self) -> Result<()> {
47
901
        if self.eof() {
48
901
            return Ok(());
49
0
        }
50
0
        Err(BinaryReaderError::new(
51
0
            "section size mismatch: unexpected data at the end of the section",
52
0
            self.original_position(),
53
0
        ))
54
901
    }
<wasmparser_nostd::readers::core::globals::GlobalSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Line
Count
Source
46
3.97k
    fn ensure_end(&self) -> Result<()> {
47
3.97k
        if self.eof() {
48
3.97k
            return Ok(());
49
0
        }
50
0
        Err(BinaryReaderError::new(
51
0
            "section size mismatch: unexpected data at the end of the section",
52
0
            self.original_position(),
53
0
        ))
54
3.97k
    }
<wasmparser_nostd::readers::core::types::TypeSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Line
Count
Source
46
4.33k
    fn ensure_end(&self) -> Result<()> {
47
4.33k
        if self.eof() {
48
4.33k
            return Ok(());
49
0
        }
50
0
        Err(BinaryReaderError::new(
51
0
            "section size mismatch: unexpected data at the end of the section",
52
0
            self.original_position(),
53
0
        ))
54
4.33k
    }
<wasmparser_nostd::readers::core::elements::ElementSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Line
Count
Source
46
1.31k
    fn ensure_end(&self) -> Result<()> {
47
1.31k
        if self.eof() {
48
1.31k
            return Ok(());
49
0
        }
50
0
        Err(BinaryReaderError::new(
51
0
            "section size mismatch: unexpected data at the end of the section",
52
0
            self.original_position(),
53
0
        ))
54
1.31k
    }
Unexecuted instantiation: <wasmparser_nostd::readers::component::canonicals::ComponentCanonicalSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
<wasmparser_nostd::readers::core::tables::TableSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Line
Count
Source
46
2.22k
    fn ensure_end(&self) -> Result<()> {
47
2.22k
        if self.eof() {
48
2.22k
            return Ok(());
49
0
        }
50
0
        Err(BinaryReaderError::new(
51
0
            "section size mismatch: unexpected data at the end of the section",
52
0
            self.original_position(),
53
0
        ))
54
2.22k
    }
<wasmparser_nostd::readers::core::memories::MemorySectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Line
Count
Source
46
1.89k
    fn ensure_end(&self) -> Result<()> {
47
1.89k
        if self.eof() {
48
1.89k
            return Ok(());
49
0
        }
50
0
        Err(BinaryReaderError::new(
51
0
            "section size mismatch: unexpected data at the end of the section",
52
0
            self.original_position(),
53
0
        ))
54
1.89k
    }
Unexecuted instantiation: <wasmparser_nostd::readers::component::exports::ComponentExportSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
<wasmparser_nostd::readers::core::functions::FunctionSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Line
Count
Source
46
4.33k
    fn ensure_end(&self) -> Result<()> {
47
4.33k
        if self.eof() {
48
4.33k
            return Ok(());
49
0
        }
50
0
        Err(BinaryReaderError::new(
51
0
            "section size mismatch: unexpected data at the end of the section",
52
0
            self.original_position(),
53
0
        ))
54
4.33k
    }
Unexecuted instantiation: <wasmparser_nostd::readers::component::imports::ComponentImportSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Unexecuted instantiation: <wasmparser_nostd::readers::component::aliases::ComponentAliasSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
<wasmparser_nostd::readers::core::exports::ExportSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Line
Count
Source
46
4.33k
    fn ensure_end(&self) -> Result<()> {
47
4.33k
        if self.eof() {
48
4.33k
            return Ok(());
49
0
        }
50
0
        Err(BinaryReaderError::new(
51
0
            "section size mismatch: unexpected data at the end of the section",
52
0
            self.original_position(),
53
0
        ))
54
4.33k
    }
<wasmparser_nostd::readers::core::imports::ImportSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Line
Count
Source
46
2.72k
    fn ensure_end(&self) -> Result<()> {
47
2.72k
        if self.eof() {
48
2.72k
            return Ok(());
49
0
        }
50
0
        Err(BinaryReaderError::new(
51
0
            "section size mismatch: unexpected data at the end of the section",
52
0
            self.original_position(),
53
0
        ))
54
2.72k
    }
Unexecuted instantiation: <wasmparser_nostd::readers::component::instances::ComponentInstanceSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Unexecuted instantiation: <wasmparser_nostd::readers::component::instances::InstanceSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Unexecuted instantiation: <wasmparser_nostd::readers::core::tags::TagSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Unexecuted instantiation: <wasmparser_nostd::readers::component::types::CoreTypeSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
Unexecuted instantiation: <wasmparser_nostd::readers::component::types::ComponentTypeSectionReader as wasmparser_nostd::readers::SectionReader>::ensure_end
55
}
56
57
/// Implemented by sections with a limited number of items.
58
pub trait SectionWithLimitedItems: SectionReader {
59
    /// Gets the count of the items in the section.
60
    fn get_count(&self) -> u32;
61
62
    /// Returns an iterator over the items within this section where the offset
63
    /// in the original section is provided with the item.
64
0
    fn into_iter_with_offsets(self) -> IntoIterWithOffsets<Self>
65
0
    where
66
0
        Self: Sized,
67
0
    {
68
0
        IntoIterWithOffsets {
69
0
            iter: SectionIteratorLimited::new(self),
70
0
        }
71
0
    }
72
}
73
74
/// An iterator over items in a section.
75
pub struct SectionIterator<R>
76
where
77
    R: SectionReader,
78
{
79
    reader: R,
80
    err: bool,
81
}
82
83
impl<R> SectionIterator<R>
84
where
85
    R: SectionReader,
86
{
87
    /// Constructs a new `SectionIterator` for the given section reader.
88
0
    pub fn new(reader: R) -> SectionIterator<R> {
89
0
        SectionIterator { reader, err: false }
90
0
    }
91
}
92
93
impl<R> Iterator for SectionIterator<R>
94
where
95
    R: SectionReader,
96
{
97
    type Item = Result<R::Item>;
98
99
0
    fn next(&mut self) -> Option<Self::Item> {
100
0
        if self.err || self.reader.eof() {
101
0
            return None;
102
0
        }
103
0
        let result = self.reader.read();
104
0
        self.err = result.is_err();
105
0
        Some(result)
106
0
    }
107
}
108
109
/// An iterator over a limited section iterator.
110
pub struct SectionIteratorLimited<R> {
111
    reader: R,
112
    left: u32,
113
    end: bool,
114
}
115
116
impl<R> SectionIteratorLimited<R>
117
where
118
    R: SectionWithLimitedItems,
119
{
120
    /// Constructs a new `SectionIteratorLimited` for the given limited section reader.
121
0
    pub fn new(reader: R) -> SectionIteratorLimited<R> {
122
0
        let left = reader.get_count();
123
0
        SectionIteratorLimited {
124
0
            reader,
125
0
            left,
126
0
            end: false,
127
0
        }
128
0
    }
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::data::DataSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::types::TypeSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::relocs::RelocSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::component::types::ComponentTypeSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::component::instances::InstanceSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::linking::LinkingSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::component::canonicals::ComponentCanonicalSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::functions::FunctionSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::tables::TableSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::component::instances::ComponentInstanceSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::memories::MemorySectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::component::exports::ComponentExportSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::globals::GlobalSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::elements::ElementSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::code::CodeSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::tags::TagSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::producers::ProducersSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::component::types::CoreTypeSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::component::imports::ComponentImportSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::imports::ImportSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::component::aliases::ComponentAliasSectionReader>>::new
Unexecuted instantiation: <wasmparser_nostd::readers::SectionIteratorLimited<wasmparser_nostd::readers::core::exports::ExportSectionReader>>::new
129
}
130
131
impl<R> Iterator for SectionIteratorLimited<R>
132
where
133
    R: SectionWithLimitedItems,
134
{
135
    type Item = Result<R::Item>;
136
137
0
    fn next(&mut self) -> Option<Self::Item> {
138
0
        if self.end {
139
0
            return None;
140
0
        }
141
0
        if self.left == 0 {
142
0
            return match self.reader.ensure_end() {
143
0
                Ok(()) => None,
144
0
                Err(err) => {
145
0
                    self.end = true;
146
0
                    Some(Err(err))
147
                }
148
            };
149
0
        }
150
0
        let result = self.reader.read();
151
0
        self.end = result.is_err();
152
0
        self.left -= 1;
153
0
        Some(result)
154
0
    }
155
156
0
    fn size_hint(&self) -> (usize, Option<usize>) {
157
0
        let count = self.reader.get_count() as usize;
158
0
        (count, Some(count))
159
0
    }
160
}
161
162
/// An iterator over a limited section iterator.
163
pub struct IntoIterWithOffsets<R> {
164
    iter: SectionIteratorLimited<R>,
165
}
166
167
impl<R> Iterator for IntoIterWithOffsets<R>
168
where
169
    R: SectionWithLimitedItems,
170
{
171
    type Item = Result<(usize, R::Item)>;
172
173
0
    fn next(&mut self) -> Option<Self::Item> {
174
0
        let pos = self.iter.reader.original_position();
175
0
        Some(self.iter.next()?.map(|item| (pos, item)))
176
0
    }
177
178
0
    fn size_hint(&self) -> (usize, Option<usize>) {
179
0
        self.iter.size_hint()
180
0
    }
181
}