Skip to content
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Can be set to allow or prohibit unlisted additional exports.
The following presets are provided:
- `ewasm`: Verifies that the `main` function and `memory` is exported. Disallows any unlisted exports.

### dropsection

Removes selected sections from the module.

### deployer

Wraps module into an ewasm-compatible constructor. It has two presets:
Expand Down
166 changes: 166 additions & 0 deletions libchisel/src/dropsection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
use super::{ModuleError, ModuleTranslator};

use parity_wasm::builder::*;
use parity_wasm::elements::*;

/// Enum on which ModuleTranslator is implemented.
pub enum DropSection<'a> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably will rewrite to have this as DropSectionKind and DropSection as a vector of DropSectionKind. In that case the entire code can be rewritten somewhat, potentially with a single loop, though one must be careful when removing sections with a lower index.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this rewrite I could also introduce with_preset with ewasm which removes the namessection and all custom sections with names debug.

NamesSection,
/// Name of the custom section.
CustomSectionByName(&'a String),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps another mode for DropSection would be identifying data sections that are used for rust debugging and panic messages.
Identifying these is not exactly straightforward, but one option is just using a plain regex lookup for things like libcore in the data sections. Not sure what the risk of false positives for this would be.

/// Index of the custom section.
CustomSectionByIndex(usize),
/// Index of the unknown section.
UnknownSectionByIndex(usize),
}

fn names_section_index_for(module: &Module) -> Option<usize> {
module.sections().iter().position(|e| match e {
Section::Name(_) => true,
_ => false,
})
}

fn custom_section_index_for(module: &Module, name: &String) -> Option<usize> {
module.sections().iter().position(|e| match e {
Section::Custom(_section) => _section.name() == name,
_ => false,
})
}

impl<'a> DropSection<'a> {
fn find_index(&self, module: &Module) -> Option<usize> {
match &self {
DropSection::NamesSection => names_section_index_for(module),
DropSection::CustomSectionByName(name) => custom_section_index_for(module, &name),
DropSection::CustomSectionByIndex(index) => Some(*index),
DropSection::UnknownSectionByIndex(index) => Some(*index),
}
}

fn drop_section(&self, module: &mut Module) -> Result<bool, ModuleError> {
let index = self.find_index(&module);
if index.is_none() {
return Ok(false);
}
let index = index.unwrap();

let sections = module.sections_mut();
if index >= sections.len() {
return Ok(false);
}
sections.remove(index);

Ok(true)
}
}

impl<'a> ModuleTranslator for DropSection<'a> {
fn translate_inplace(&self, module: &mut Module) -> Result<bool, ModuleError> {
Ok(self.drop_section(module)?)
}

fn translate(&self, module: &Module) -> Result<Option<Module>, ModuleError> {
let mut ret = module.clone();
if self.drop_section(&mut ret)? {
Ok(Some(ret))
} else {
Ok(None)
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use parity_wasm::builder;

#[test]
fn keep_intact() {
let mut module = builder::module().build();
let name = "empty".to_string();
let dropper = DropSection::CustomSectionByName(&name);
let did_change = dropper.translate_inplace(&mut module).unwrap();
assert_eq!(did_change, false);
}

#[test]
fn keep_intact_custom_section() {
let mut module = builder::module()
.with_section(Section::Custom(CustomSection::new(
"test".to_string(),
vec![],
)))
.build();
let name = "empty".to_string();
let dropper = DropSection::CustomSectionByName(&name);
let did_change = dropper.translate_inplace(&mut module).unwrap();
assert_eq!(did_change, false);
}

#[test]
fn remove_custom_section() {
let mut module = builder::module()
.with_section(Section::Custom(CustomSection::new(
"test".to_string(),
vec![],
)))
.build();
let name = "test".to_string();
let dropper = DropSection::CustomSectionByName(&name);
let did_change = dropper.translate_inplace(&mut module).unwrap();
assert_eq!(did_change, true);
}

#[test]
fn remove_custom_section_by_index() {
let mut module = builder::module()
.with_section(Section::Custom(CustomSection::new(
"test".to_string(),
vec![],
)))
.build();
let dropper = DropSection::CustomSectionByIndex(0);
let did_change = dropper.translate_inplace(&mut module).unwrap();
assert_eq!(did_change, true);
}

#[test]
fn remove_oob_custom_section_by_index() {
let mut module = builder::module()
.with_section(Section::Custom(CustomSection::new(
"test".to_string(),
vec![],
)))
.build();
let dropper = DropSection::CustomSectionByIndex(1);
let did_change = dropper.translate_inplace(&mut module).unwrap();
assert_eq!(did_change, false);
}

#[test]
fn remove_custom_unknown_by_index() {
let mut module = builder::module()
.with_section(Section::Custom(CustomSection::new(
"test".to_string(),
vec![],
)))
.build();
let dropper = DropSection::UnknownSectionByIndex(0);
let did_change = dropper.translate_inplace(&mut module).unwrap();
assert_eq!(did_change, true);
}

#[test]
fn remove_oob_unknown_section_by_index() {
let mut module = builder::module()
.with_section(Section::Custom(CustomSection::new(
"test".to_string(),
vec![],
)))
.build();
let dropper = DropSection::UnknownSectionByIndex(1);
let did_change = dropper.translate_inplace(&mut module).unwrap();
assert_eq!(did_change, false);
}
}
1 change: 1 addition & 0 deletions libchisel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod imports;

pub mod checkstartfunc;
pub mod deployer;
pub mod dropsection;
pub mod remapimports;
pub mod remapstart;
pub mod repack;
Expand Down