Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ time = "0.1"
default = ["std"]
std = []

# Reduce stack usage for buffered read operations.
# This feature is useful when integrating on resource constrained devices such as microcontroler
# where the stack size is fixed (stacks do not grow) and limited to a few (k)bytes.
reduced-stack-buffer = []

#
# Features for enabling non-MVP proposals.
# These features should be tested as part of Travis CI build.
Expand Down
9 changes: 8 additions & 1 deletion src/elements/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ use alloc::{string::String, vec::Vec};
use crate::{io, elements};
use super::{Error, Deserialize, Serialize};


#[cfg(feature = "reduced-stack-buffer")]
const PRIMITIVES_BUFFER_LENGTH: usize = 256;

#[cfg(not(feature = "reduced-stack-buffer"))]
const PRIMITIVES_BUFFER_LENGTH: usize = 1024;

/// Unsigned variable-length integer, limited to 32 bits,
/// represented by at most 5 bytes that may contain padding 0x80 bytes.
#[derive(Debug, Copy, Clone, PartialEq)]
Expand Down Expand Up @@ -520,7 +527,7 @@ impl Deserialize for String {
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let length = u32::from(VarUint32::deserialize(reader)?) as usize;
if length > 0 {
String::from_utf8(buffered_read!(1024, length, reader)).map_err(|_| Error::NonUtf8String)
String::from_utf8(buffered_read!(PRIMITIVES_BUFFER_LENGTH, length, reader)).map_err(|_| Error::NonUtf8String)
}
else {
Ok(String::new())
Expand Down
6 changes: 5 additions & 1 deletion src/elements/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ use super::types::Type;
use super::name_section::NameSection;
use super::reloc_section::RelocSection;

#[cfg(feature = "reduced-stack-buffer")]
const ENTRIES_BUFFER_LENGTH: usize = 256;

#[cfg(not(feature = "reduced-stack-buffer"))]
const ENTRIES_BUFFER_LENGTH: usize = 16384;

/// Section in the WebAssembly module.
Expand Down Expand Up @@ -331,7 +335,7 @@ impl Deserialize for CustomSection {

fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let section_length: usize = u32::from(VarUint32::deserialize(reader)?) as usize;
let buf = buffered_read!(16384, section_length, reader);
let buf = buffered_read!(ENTRIES_BUFFER_LENGTH, section_length, reader);
let mut cursor = io::Cursor::new(&buf[..]);
let name = String::deserialize(&mut cursor)?;
let payload = buf[cursor.position() as usize..].to_vec();
Expand Down
10 changes: 8 additions & 2 deletions src/elements/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const FLAG_PASSIVE: u32 = 1;
#[cfg(feature="bulk")]
const FLAG_MEM_NONZERO: u32 = 2;

#[cfg(feature = "reduced-stack-buffer")]
const VALUES_BUFFER_LENGTH: usize = 256;

#[cfg(not(feature = "reduced-stack-buffer"))]
const VALUES_BUFFER_LENGTH: usize = 16384;

/// Entry in the element section.
#[derive(Debug, Clone, PartialEq)]
pub struct ElementSegment {
Expand Down Expand Up @@ -217,7 +223,7 @@ impl Deserialize for DataSegment {
let index = VarUint32::deserialize(reader)?;
let offset = InitExpr::deserialize(reader)?;
let value_len = u32::from(VarUint32::deserialize(reader)?) as usize;
let value_buf = buffered_read!(65536, value_len, reader);
let value_buf = buffered_read!(VALUES_BUFFER_LENGTH, value_len, reader);

Ok(DataSegment {
index: index.into(),
Expand All @@ -242,7 +248,7 @@ impl Deserialize for DataSegment {
Some(InitExpr::deserialize(reader)?)
};
let value_len = u32::from(VarUint32::deserialize(reader)?) as usize;
let value_buf = buffered_read!(65536, value_len, reader);
let value_buf = buffered_read!(VALUES_BUFFER_LENGTH, value_len, reader);

Ok(DataSegment {
index: index,
Expand Down