Skip to content

Commit af30a53

Browse files
authored
Make fields of BinaryReader private (#1543)
Don't allow arbitrary modifications/access throughout the crate to be sure arbitrate through the methods on this type. This is a pretty core type to the crate so it's best to not spread implementation details too much.
1 parent 9d7c979 commit af30a53

6 files changed

Lines changed: 36 additions & 33 deletions

File tree

crates/wasmparser/src/binary_reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ impl BinaryReaderError {
103103
/// A binary reader of the WebAssembly structures and types.
104104
#[derive(Clone, Debug, Hash)]
105105
pub struct BinaryReader<'a> {
106-
pub(crate) buffer: &'a [u8],
107-
pub(crate) position: usize,
106+
buffer: &'a [u8],
107+
position: usize,
108108
original_offset: usize,
109109
allow_memarg64: bool,
110110
}

crates/wasmparser/src/parser.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,17 @@ impl Parser {
533533
};
534534
// TODO: thread through `offset: u64` to `BinaryReader`, remove
535535
// the cast here.
536-
let mut reader = BinaryReader::new_with_offset(data, self.offset as usize);
536+
let starting_offset = self.offset as usize;
537+
let mut reader = BinaryReader::new_with_offset(data, starting_offset);
537538
match self.parse_reader(&mut reader, eof) {
538539
Ok(payload) => {
539540
// Be sure to update our offset with how far we got in the
540541
// reader
541-
self.offset += usize_to_u64(reader.position);
542-
self.max_size -= usize_to_u64(reader.position);
542+
let consumed = reader.original_position() - starting_offset;
543+
self.offset += usize_to_u64(consumed);
544+
self.max_size -= usize_to_u64(consumed);
543545
Ok(Chunk::Parsed {
544-
consumed: reader.position,
546+
consumed: consumed,
545547
payload,
546548
})
547549
}
@@ -595,7 +597,7 @@ impl Parser {
595597
return Ok(Payload::End(reader.original_position()));
596598
}
597599

598-
let id_pos = reader.position;
600+
let id_pos = reader.original_position();
599601
let id = reader.read_u8()?;
600602
if id & 0x80 != 0 {
601603
return Err(BinaryReaderError::new("malformed section id", id_pos));
@@ -608,9 +610,10 @@ impl Parser {
608610
// but it is required for nested modules/components to correctly ensure
609611
// that all sections live entirely within their section of the
610612
// file.
613+
let consumed = reader.original_position() - id_pos;
611614
let section_overflow = self
612615
.max_size
613-
.checked_sub(usize_to_u64(reader.position))
616+
.checked_sub(usize_to_u64(consumed))
614617
.and_then(|s| s.checked_sub(len.into()))
615618
.is_none();
616619
if section_overflow {
@@ -1057,9 +1060,9 @@ fn delimited<'a, T>(
10571060
len: &mut u32,
10581061
f: impl FnOnce(&mut BinaryReader<'a>) -> Result<T>,
10591062
) -> Result<T> {
1060-
let start = reader.position;
1063+
let start = reader.original_position();
10611064
let ret = f(reader)?;
1062-
*len = match (reader.position - start)
1065+
*len = match (reader.original_position() - start)
10631066
.try_into()
10641067
.ok()
10651068
.and_then(|i| len.checked_sub(i))

crates/wasmparser/src/readers/component/names.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a> Subsection<'a> for ComponentName<'a> {
5353
}
5454
ComponentName::Component {
5555
name,
56-
name_range: offset..offset + reader.position,
56+
name_range: offset..reader.original_position(),
5757
}
5858
}
5959
1 => {

crates/wasmparser/src/readers/component/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub enum ComponentValType {
125125
impl<'a> FromReader<'a> for ComponentValType {
126126
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
127127
if let Some(ty) = PrimitiveValType::from_byte(reader.peek()?) {
128-
reader.position += 1;
128+
reader.read_u8()?;
129129
return Ok(ComponentValType::Primitive(ty));
130130
}
131131

@@ -319,7 +319,7 @@ impl<'a> FromReader<'a> for ComponentTypeDeclaration<'a> {
319319
// variant of imports; check for imports here or delegate to
320320
// `InstanceTypeDeclaration` with the appropriate conversions.
321321
if reader.peek()? == 0x03 {
322-
reader.position += 1;
322+
reader.read_u8()?;
323323
return Ok(ComponentTypeDeclaration::Import(reader.read()?));
324324
}
325325

crates/wasmparser/src/readers/core/names.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'a> Subsection<'a> for Name<'a> {
135135
}
136136
Name::Module {
137137
name,
138-
name_range: offset..offset + reader.position,
138+
name_range: offset..reader.original_position(),
139139
}
140140
}
141141
1 => Name::Function(NameMap::new(data, offset)?),

crates/wasmparser/src/readers/core/types.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,11 +1368,11 @@ impl<'a> FromReader<'a> for StorageType {
13681368
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
13691369
match reader.peek()? {
13701370
0x78 => {
1371-
reader.position += 1;
1371+
reader.read_u8()?;
13721372
Ok(StorageType::I8)
13731373
}
13741374
0x77 => {
1375-
reader.position += 1;
1375+
reader.read_u8()?;
13761376
Ok(StorageType::I16)
13771377
}
13781378
_ => Ok(StorageType::Val(reader.read()?)),
@@ -1384,23 +1384,23 @@ impl<'a> FromReader<'a> for ValType {
13841384
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
13851385
match reader.peek()? {
13861386
0x7F => {
1387-
reader.position += 1;
1387+
reader.read_u8()?;
13881388
Ok(ValType::I32)
13891389
}
13901390
0x7E => {
1391-
reader.position += 1;
1391+
reader.read_u8()?;
13921392
Ok(ValType::I64)
13931393
}
13941394
0x7D => {
1395-
reader.position += 1;
1395+
reader.read_u8()?;
13961396
Ok(ValType::F32)
13971397
}
13981398
0x7C => {
1399-
reader.position += 1;
1399+
reader.read_u8()?;
14001400
Ok(ValType::F64)
14011401
}
14021402
0x7B => {
1403-
reader.position += 1;
1403+
reader.read_u8()?;
14041404
Ok(ValType::V128)
14051405
}
14061406
0x70 | 0x6F | 0x64 | 0x63 | 0x6E | 0x71 | 0x72 | 0x73 | 0x74 | 0x6D | 0x6B | 0x6A
@@ -1440,51 +1440,51 @@ impl<'a> FromReader<'a> for HeapType {
14401440
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
14411441
match reader.peek()? {
14421442
0x70 => {
1443-
reader.position += 1;
1443+
reader.read_u8()?;
14441444
Ok(HeapType::Func)
14451445
}
14461446
0x6F => {
1447-
reader.position += 1;
1447+
reader.read_u8()?;
14481448
Ok(HeapType::Extern)
14491449
}
14501450
0x6E => {
1451-
reader.position += 1;
1451+
reader.read_u8()?;
14521452
Ok(HeapType::Any)
14531453
}
14541454
0x71 => {
1455-
reader.position += 1;
1455+
reader.read_u8()?;
14561456
Ok(HeapType::None)
14571457
}
14581458
0x72 => {
1459-
reader.position += 1;
1459+
reader.read_u8()?;
14601460
Ok(HeapType::NoExtern)
14611461
}
14621462
0x73 => {
1463-
reader.position += 1;
1463+
reader.read_u8()?;
14641464
Ok(HeapType::NoFunc)
14651465
}
14661466
0x6D => {
1467-
reader.position += 1;
1467+
reader.read_u8()?;
14681468
Ok(HeapType::Eq)
14691469
}
14701470
0x6B => {
1471-
reader.position += 1;
1471+
reader.read_u8()?;
14721472
Ok(HeapType::Struct)
14731473
}
14741474
0x6A => {
1475-
reader.position += 1;
1475+
reader.read_u8()?;
14761476
Ok(HeapType::Array)
14771477
}
14781478
0x6C => {
1479-
reader.position += 1;
1479+
reader.read_u8()?;
14801480
Ok(HeapType::I31)
14811481
}
14821482
0x69 => {
1483-
reader.position += 1;
1483+
reader.read_u8()?;
14841484
Ok(HeapType::Exn)
14851485
}
14861486
0x74 => {
1487-
reader.position += 1;
1487+
reader.read_u8()?;
14881488
Ok(HeapType::NoExn)
14891489
}
14901490
_ => {

0 commit comments

Comments
 (0)