wasm/validation/
custom_section.rs1use crate::{
2 core::reader::{
3 section_header::{SectionHeader, SectionTy},
4 WasmReader,
5 },
6 ValidationError,
7};
8
9#[derive(Debug, Clone)]
10pub struct CustomSection<'wasm> {
11 pub name: &'wasm str,
12 pub contents: &'wasm [u8],
13}
14
15impl<'wasm> CustomSection<'wasm> {
16 pub(crate) fn read_and_validate(
17 wasm: &mut WasmReader<'wasm>,
18 header: SectionHeader,
19 ) -> Result<CustomSection<'wasm>, ValidationError> {
20 assert_eq!(header.ty, SectionTy::Custom);
21
22 let name = wasm.read_name()?;
27
28 let section_start = wasm.pc;
29 let section_end = header
30 .contents
31 .from()
32 .checked_add(header.contents.len())
33 .ok_or(ValidationError::InvalidCustomSectionLength)?;
34
35 let contents = wasm
36 .full_wasm_binary
37 .get(section_start..section_end)
38 .ok_or(ValidationError::InvalidCustomSectionLength)?;
39
40 let section_len = section_end
41 .checked_sub(section_start)
42 .expect("section start <= section end always");
43
44 wasm.skip(section_len)?;
45
46 Ok(CustomSection { name, contents })
47 }
48}