summaryrefslogtreecommitdiffstats
path: root/third_party/rust/parity-wasm/examples/inject.rs
blob: 0e58e23167fbcb849ee2233ab96d9fa7fb78f0e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
extern crate parity_wasm;

use std::env;

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

pub fn inject_nop(instructions: &mut elements::Instructions) {
	use parity_wasm::elements::Instruction::*;
	let instructions = instructions.elements_mut();
	let mut position = 0;
	loop {
		let need_inject = match &instructions[position] {
			&Block(_) | &If(_) => true,
			_ => false,
		};
		if need_inject {
			instructions.insert(position + 1, Nop);
		}

		position += 1;
		if position >= instructions.len() {
			break;
		}
	}
}

fn main() {
	let args = env::args().collect::<Vec<_>>();
	if args.len() != 3 {
		println!("Usage: {} input_file.wasm output_file.wasm", args[0]);
		return;
	}

	let mut module = parity_wasm::deserialize_file(&args[1]).unwrap();

	for section in module.sections_mut() {
		match section {
			&mut elements::Section::Code(ref mut code_section) => {
				for ref mut func_body in code_section.bodies_mut() {
					inject_nop(func_body.code_mut());
				}
			},
			_ => { }
		}
	}

	let mut build = builder::from_module(module);
	let import_sig = build.push_signature(
		builder::signature()
			.param().i32()
			.param().i32()
			.return_type().i32()
			.build_sig()
	);
	let build = build.import()
		.module("env")
		.field("log")
		.external().func(import_sig)
		.build();

	parity_wasm::serialize_to_file(&args[2], build.build()).unwrap();
}