From edf67eb1e0682ba550d46a6f219c453a89da6a60 Mon Sep 17 00:00:00 2001 From: Mihail Kirov Date: Thu, 24 Aug 2023 14:27:53 +0300 Subject: [PATCH] fix: remove stack limiter, add explainer for MB conversion --- src/injecting/injections.rs | 7 ++- src/lib.rs | 1 - src/stack_limiter.rs | 118 ------------------------------------ 3 files changed, 6 insertions(+), 120 deletions(-) delete mode 100644 src/stack_limiter.rs diff --git a/src/injecting/injections.rs b/src/injecting/injections.rs index bb06983..1dbe141 100644 --- a/src/injecting/injections.rs +++ b/src/injecting/injections.rs @@ -113,7 +113,12 @@ fn inject_noops(module: &mut Module, function_name: &str, size: Option) -> module.map_function(function_name, |func_body: &mut FuncBody| { let code = func_body.code_mut(); let size = size.expect("No size given"); - let nops_count = (size as usize) * 1024 * 1024; + + // MB and MiB both represent digital storage units, whereas Mb refers to data transmission rates. + // MB is based on decimal prefixes (1 MB = 1,000,000 bytes), while MiB uses binary prefixes (1 MiB = 1,048,576 bytes). + // To convert between Megabytes and Mebibytes, one can use the conversion factor of 1 MB ≈ 0.9537 MiB + // That's why we multiply by 1000 instead of 1024 + let nops_count = (size as usize) * 1000 * 1000; let mut nops = vec![Instruction::Nop; nops_count]; nops.append(code.elements_mut()); diff --git a/src/lib.rs b/src/lib.rs index 178ef9b..2415da7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,6 @@ //! ``` pub mod injecting; -pub mod stack_limiter; pub mod util; pub use self::injecting::injections::Injection; diff --git a/src/stack_limiter.rs b/src/stack_limiter.rs deleted file mode 100644 index 3f4c755..0000000 --- a/src/stack_limiter.rs +++ /dev/null @@ -1,118 +0,0 @@ -#![allow(unused)] - -extern crate alloc; - -use alloc::{vec, vec::Vec}; -use core::mem; -use wasm_instrument::parity_wasm::{ - builder, - elements::{self, Instruction, Instructions, Type}, -}; - -use sp_maybe_compressed_blob::{compress, decompress}; -use std::fs::{self, read}; -use std::io::Write; -pub use wasm_instrument; -use wasm_instrument::parity_wasm::{deserialize_buffer, elements::Module, serialize}; - -/// Generate a new global that will be used for tracking a timer -fn generate_timer_global(module: &mut elements::Module) -> u32 { - let global_entry = builder::global() - .value_type() - .i32() - .mutable() - .init_expr(Instruction::I32Const(0)) - .build(); - - // Try to find an existing global section. - for section in module.sections_mut() { - if let elements::Section::Global(gs) = section { - gs.entries_mut().push(global_entry); - return (gs.entries().len() as u32) - 1; - } - } - - // Existing section not found, create one! - module.sections_mut().push(elements::Section::Global( - elements::GlobalSection::with_entries(vec![global_entry]), - )); - 0 -} - -pub fn inject_single(path: &str, file_name: &str) { - let full_path = &format!("{}/{}", path, file_name); - let orig_bytes = &read(full_path).unwrap(); - let decompressed_bytes = decompress(orig_bytes, 10_000_000).expect("Couldn't decompress"); - - let orig_module: Module = deserialize_buffer(decompressed_bytes.as_ref()).unwrap(); - println!("Original module len: {}", orig_bytes.len()); - - let injected_module = - wasm_instrument::inject_stack_limiter(orig_module, 1024).expect("Couldn't inject limiter"); - - let injected_bytes = serialize(injected_module).unwrap(); - match fs::OpenOptions::new() - .create(true) // To create a new file - .write(true) - .open(format!("{}/injected_{}", path, file_name)) - { - Ok(mut file) => { - file.write_all(&injected_bytes) - .expect("Couldn't write to file"); - println!("Wrote stack-injected wasm to file") - } - Err(e) => println!("Error: {}", e), - } - - let compressed_bytes = compress(&injected_bytes, 100_000_000).unwrap(); - - println!("Injected module len: {}", injected_bytes.len()); - println!("Compressed module len: {}", compressed_bytes.len()); - - match fs::OpenOptions::new() - .create(true) // To create a new file - .write(true) - .open(format!("{}/injected_compressed_{}", path, file_name)) - { - Ok(mut file) => { - file.write_all(&compressed_bytes) - .expect("Couldn't write to file"); - println!("Wrote stack-injected wasm to file") - } - Err(e) => println!("Error: {}", e), - } -} - -fn decompress_wasm(path: &str, file_name: &str) { - let full_path = &format!("{}/{}", path, file_name); - let orig_bytes = &read(full_path).unwrap(); - let decompressed_bytes = decompress(orig_bytes, 10_000_000).expect("Couldn't decompress"); - let _ = std::fs::write( - format!("{}/decompressed_{}", path, file_name), - decompressed_bytes, - ); -} - -fn show_information(path: &str, file_name: &str) { - let full_path = &format!("{}/{}", path, file_name); - let orig_bytes = &read(full_path).unwrap(); - let decompressed_bytes = decompress(orig_bytes, 10_000_000).expect("Couldn't decompress"); - - let module: Module = deserialize_buffer(&decompressed_bytes).unwrap(); - println!("Original module len: {}", orig_bytes.len()); - if module.code_section().is_none() { - println!("no code in module!"); - std::process::exit(1); - } - - module - .parse_names() - .expect("Couldn't parse names") - .export_section() - .expect("Empty names section") - .entries() - .iter() - .for_each(|entry| { - println!("entry: {:?}", entry); - }); -}