Skip to content

Commit

Permalink
feat(wasm-gen): customize logic on reaching stack limit
Browse files Browse the repository at this point in the history
  • Loading branch information
StackOverflowExcept1on committed Oct 24, 2023
1 parent a721177 commit 27b7b7d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ debug = true
parity-wasm = { version = "0.45.0", git = "https://github.com/gear-tech/parity-wasm", branch = "v0.45.0-sign-ext" }
wasmi-validation = { version = "0.5.0", git = "https://github.com/gear-tech/wasmi", branch = "v0.13.2-sign-ext" }
wasm-instrument = { version = "0.3.0", git = "https://github.com/gear-tech/wasm-instrument", branch = "v0.3.0-sign-ext" }
gwasm-instrument = { version = "0.2.1", git = "https://github.com/StackOverflowExcept1on/wasm-instrument", branch = "v0.2.1-sign-ext-stack-height" }

# TODO: remove after https://github.com/BLAKE3-team/BLAKE3/pull/230
blake3 = { git = "https://github.com/gear-tech/BLAKE3", branch = "fix-clang-cl-cross" }
Expand Down
3 changes: 1 addition & 2 deletions pallets/gear/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,7 @@ impl Default for Limits {
// To avoid potential stack overflow problems we have a panic in sandbox in case,
// execution is ended with stack overflow error. So, process queue execution will be
// stopped and we will be able to investigate the problem and decrease this constant if needed.
// TODO #3435. Disabled stack height is a temp solution.
stack_height: cfg!(not(feature = "fuzz")).then_some(20_000),
stack_height: Some(20_000),
globals: 256,
locals: 1024,
parameters: 128,
Expand Down
2 changes: 1 addition & 1 deletion utils/runtime-fuzzer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dd if=/dev/urandom of=fuzz/corpus/main/fuzzer-seed-corpus bs=1 count=350000

# Run fuzzer for at least 20 minutes and then press Ctrl-C to stop fuzzing.
# You can also remove RUST_LOG to avoid printing tons of logs on terminal.
RUST_LOG=debug,syscalls,gear_wasm_gen=trace,runtime_fuzzer=trace,gear_core_backend=trace \
RUST_LOG=debug,syscalls,runtime::sandbox=trace,gear_wasm_gen=trace,runtime_fuzzer=trace,gear_core_backend=trace \
cargo fuzz run \
--release \
--sanitizer=none \
Expand Down
2 changes: 2 additions & 0 deletions utils/wasm-gen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ impl<'a, 'b> GearWasmGenerator<'a, 'b> {
.into_wasm_module()
.into_inner();

let module = utils::inject_stack_limiter(module);

Ok(if config.remove_recursions {
log::trace!("Removing recursions");
utils::remove_recursion(module)
Expand Down
31 changes: 28 additions & 3 deletions utils/wasm-gen/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use gear_wasm_instrument::parity_wasm::{
builder,
elements::{self, FuncBody, ImportCountType, Instruction, Module, Type, ValueType},
use gear_wasm_instrument::{
parity_wasm::{
builder,
elements::{self, FuncBody, ImportCountType, Instruction, Module, Type, ValueType},
},
wasm_instrument,
};
use gsys::HashWithValue;
use std::{
Expand Down Expand Up @@ -215,6 +218,28 @@ fn find_recursion_impl<Callback>(
path.pop();
}

pub fn inject_stack_limiter(module: Module) -> Module {
wasm_instrument::inject_custom_stack_limiter(module, 15_000, |signature| {
let results = signature.results();
let mut body = Vec::with_capacity(results.len() + 1);

for result in results {
let instruction = match result {
ValueType::I32 => Instruction::I32Const(u32::MAX as i32),
ValueType::I64 => Instruction::I64Const(u64::MAX as i64),
ValueType::F32 | ValueType::F64 => unreachable!("f32/64 types are not supported"),
};

body.push(instruction);
}

body.push(Instruction::Return);

body
})
.expect("Failed to inject stack height limits")
}

pub(crate) fn hash_with_value_to_vec(hash_with_value: &HashWithValue) -> Vec<u8> {
let address_data_size = mem::size_of::<HashWithValue>();
let address_data_slice = unsafe {
Expand Down

0 comments on commit 27b7b7d

Please sign in to comment.