Skip to content

Commit

Permalink
use % == 0, add extra check for init_called
Browse files Browse the repository at this point in the history
  • Loading branch information
StackOverflowExcept1on committed Jan 16, 2024
1 parent bc2f951 commit b708f2e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
29 changes: 27 additions & 2 deletions utils/wasm-gen/src/generator/entry_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

use crate::{
generator::{CallIndexes, FrozenGearWasmGenerator, GearWasmGenerator, ModuleWithCallIndexes},
EntryPointsSet, WasmModule,
wasm::{PageCount as WasmPageCount, WasmModule},
EntryPointsSet,
};
use arbitrary::{Result, Unstructured};
use gear_wasm_instrument::parity_wasm::{
builder,
elements::{FunctionType, Instruction, Instructions, Type, ValueType},
};
use std::mem;

/// Gear wasm entry points generator.
///
Expand Down Expand Up @@ -164,8 +166,31 @@ impl<'a, 'b> EntryPointsGenerator<'a, 'b> {
(module, func_type)
});

let export_body_instructions =
let mut export_body_instructions =
self.generate_export_body(export_body_call_idx, export_body_call_func_type)?;

// after initializing the program, we will write about this in a special pointer
if name == "init" {
if let Some(memory_size_pages) = self.module.initial_mem_size() {
let mem_size = Into::<WasmPageCount>::into(memory_size_pages).memory_size();

let upper_limit = mem_size.saturating_sub(1) as i32;
let wait_called_ptr = upper_limit.saturating_sub(50);
let init_called_ptr = wait_called_ptr + mem::size_of::<bool>() as i32;

let end_instruction = export_body_instructions.pop();
export_body_instructions.extend_from_slice(&[
// *init_called_ptr = true
Instruction::I32Const(init_called_ptr),
Instruction::I32Const(1),
Instruction::I32Store8(0, 0),
]);
if let Some(end_instruction) = end_instruction {
export_body_instructions.push(end_instruction);
}
}
}

self.module.with(|module| {
let module = builder::from_module(module)
.function()
Expand Down
11 changes: 9 additions & 2 deletions utils/wasm-gen/src/generator/syscalls/invocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,30 +338,37 @@ impl<'a, 'b> SyscallsInvocator<'a, 'b> {

let upper_limit = mem_size.saturating_sub(1) as i32;
let wait_called_ptr = upper_limit.saturating_sub(50);
let init_called_ptr = wait_called_ptr + mem::size_of::<bool>() as i32;

// add instructions before calling wait syscall
instructions.splice(
0..0,
[
Instruction::I32Const(init_called_ptr),
Instruction::I32Load8U(0, 0),
// if *init_called_ptr { .. }
Instruction::If(BlockType::NoResult),
Instruction::I32Const(wait_called_ptr),
Instruction::I32Load(2, 0),
Instruction::I32Const(wait_frequency as i32),
Instruction::I32RemU,
Instruction::I32Const(1),
Instruction::I32Eq,
Instruction::I32Eqz,
// if *wait_called_ptr % wait_frequency == 0 { orig_wait_syscall(); }
Instruction::If(BlockType::NoResult),
],
);

// add instructions after calling wait syscall
instructions.extend_from_slice(&[
Instruction::End,
// *wait_called_ptr += 1
Instruction::I32Const(wait_called_ptr),
Instruction::I32Const(wait_called_ptr),
Instruction::I32Load(2, 0),
Instruction::I32Const(1),
Instruction::I32Add,
Instruction::I32Store(2, 0),
Instruction::End,
]);
}

Expand Down

0 comments on commit b708f2e

Please sign in to comment.