Skip to content

Commit

Permalink
Update wasm-smith
Browse files Browse the repository at this point in the history
  • Loading branch information
ark0f committed Dec 6, 2024
1 parent 9342e3a commit a342a7d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
23 changes: 8 additions & 15 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ runtime-primitives = { package = "gear-runtime-primitives", path = "runtime/prim
service = { package = "gear-service", path = "node/service", default-features = false }
testing = { package = "gear-node-testing", path = "node/testing" }
vara-runtime = { path = "runtime/vara", default-features = false }
wasm-smith = { version = "0.12.21", git = "https://github.com/gear-tech/wasm-tools.git", branch = "gear-stable" }

# ethexe deps
ethexe-cli = { path = "ethexe/cli", default-features = false }
Expand Down Expand Up @@ -553,6 +552,7 @@ smallvec = "1.13.2" # utils/node-wrappe
fs4 = "0.11.1" # utils/gear-wasmer-cache
bytes = "1.8.0" # utils/gear-wasmer-cache
loom = "0.7.2" # utils/gear-wasmer-cache
wasm-smith = "0.220.0" # utils/wasm-gen

[profile.dev.package.corosensei]
opt-level = 3
Expand Down
76 changes: 43 additions & 33 deletions utils/wasm-gen/src/config/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,30 @@

//! Config entities related to generating plain wasm module using `wasm-smith`.
//!
//! We don't give access to [`wasm_smith::SwarmConfig`] directly, but with several adaptors,
//! We don't give access to [`wasm_smith::Config`] directly, but with several adaptors,
//! because valid wasm module is not always valid gear module. So, some configurational variables
//! can be arbitrary, but some must be constantly set. That's implemented with [`ArbitraryParams`]
//! and [`ConstantParams`].
use crate::MemoryLayout;
use arbitrary::{Arbitrary, Result, Unstructured};
use std::num::NonZero;
use wasm_smith::{InstructionKind::*, InstructionKinds, SwarmConfig};
use wasm_smith::{Config, InstructionKind::*, InstructionKinds, MemoryOffsetChoices};

pub use wasm_smith::InstructionKind;

const WASM_PAGE_SIZE: u64 = 0x10_000;

/// Wasm module generation config.
///
/// This config wraps the [`wasm_smith::SwarmConfig`]. That's to make it
/// This config wraps the [`wasm_smith::Config`]. That's to make it
/// easy creating a configuration, which is custom, from one side, and,
/// from another side, results in generating valid gear wasm modules.
#[derive(Debug, Clone)]
pub struct WasmModuleConfig(SwarmConfig);
pub struct WasmModuleConfig(Config);

impl WasmModuleConfig {
/// Unwrap the inner `wasm-smith` config.
pub fn into_inner(self) -> SwarmConfig {
pub fn into_inner(self) -> Config {
self.0
}
}
Expand Down Expand Up @@ -74,16 +75,14 @@ impl From<(SelectableParams, ArbitraryParams)> for WasmModuleConfig {
tail_call_enabled,
relaxed_simd_enabled,
saturating_float_to_int_enabled,
sign_extension_enabled,
sign_extension_ops_enabled,
simd_enabled,
float_enabled,
memory_grow_enabled,
allow_floats,
max_data_segments,
min_data_segments,
max_types,
min_types,
memory_offset_choices,
reserved_memory_size,
} = ConstantParams::default();

let SelectableParams {
Expand Down Expand Up @@ -126,14 +125,17 @@ impl From<(SelectableParams, ArbitraryParams)> for WasmModuleConfig {

let allowed_instructions = InstructionKinds::new(&allowed_instructions);

Self(SwarmConfig {
Self(Config {
allow_start_export,
available_imports,
bulk_memory_enabled,
canonicalize_nans,
disallow_traps,
exceptions_enabled,
export_everything,
gc_enabled: false,
custom_page_sizes_enabled: false,
generate_custom_sections: false,
max_aliases,
max_components,
max_data_segments,
Expand All @@ -146,7 +148,9 @@ impl From<(SelectableParams, ArbitraryParams)> for WasmModuleConfig {
max_instances,
max_instructions,
max_memories,
max_memory_pages,
max_memory32_bytes: max_memory_pages * WASM_PAGE_SIZE,
// we don't support 64-bit WASM
max_memory64_bytes: 0,
max_modules,
max_nesting_depth,
max_tables,
Expand All @@ -156,7 +160,11 @@ impl From<(SelectableParams, ArbitraryParams)> for WasmModuleConfig {
max_values,
memory64_enabled,
memory_max_size_required,
memory_offset_choices,
memory_offset_choices: MemoryOffsetChoices(
memory_offset_choices.0,
memory_offset_choices.1,
memory_offset_choices.2,
),
min_data_segments,
min_element_segments,
min_elements,
Expand All @@ -171,26 +179,30 @@ impl From<(SelectableParams, ArbitraryParams)> for WasmModuleConfig {
min_uleb_size,
multi_value_enabled,
reference_types_enabled,
reserved_memory_size,
tail_call_enabled,
relaxed_simd_enabled,
saturating_float_to_int_enabled,
sign_extension_enabled,
sign_extension_ops_enabled,
shared_everything_threads_enabled: false,
simd_enabled,
float_enabled,
threads_enabled,
allow_invalid_funcs: false,
wide_arithmetic_enabled: false,
allowed_instructions,
max_table_elements,
table_max_size_required,
memory_grow_enabled,
// do not export anything to pass our checks
exports: Some(Vec::new()),
allow_floats,
extended_const_enabled: false,
})
}
}

/// Arbitrary wasm module generation params.
///
/// These are params that are allowed to be randomly set.
/// All of them are later used to instantiate `wasm_smith::SwarmConfig`.
/// All of them are later used to instantiate `wasm_smith::Config`.
#[derive(Debug, Clone)]
pub struct ArbitraryParams {
available_imports: Option<Vec<u8>>,
Expand All @@ -215,15 +227,15 @@ pub struct ArbitraryParams {
min_tags: usize,
min_uleb_size: u8,
threads_enabled: bool,
max_table_elements: u32,
max_table_elements: u64,
table_max_size_required: bool,
max_memory_pages: u64,
}

impl Arbitrary<'_> for ArbitraryParams {
fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> {
let random_swarm = u.arbitrary()?;
let SwarmConfig {
let random_config = u.arbitrary()?;
let Config {
available_imports,
canonicalize_nans,
export_everything,
Expand All @@ -248,9 +260,9 @@ impl Arbitrary<'_> for ArbitraryParams {
threads_enabled,
max_table_elements,
table_max_size_required,
max_memory_pages,
max_memory32_bytes,
..
} = random_swarm;
} = random_config;

Ok(ArbitraryParams {
available_imports,
Expand All @@ -277,14 +289,14 @@ impl Arbitrary<'_> for ArbitraryParams {
threads_enabled,
max_table_elements,
table_max_size_required,
max_memory_pages,
max_memory_pages: max_memory32_bytes / WASM_PAGE_SIZE,
})
}
}

/// Constant wasm module generation params.
///
/// Wraps params, which are used to create `wasm_smith::SwarmConfig`, but they
/// Wraps params, which are used to create `wasm_smith::Config`, but they
/// must have pre-defined values to make `wasm-smith` generate valid gear modules.
pub struct ConstantParams {
allow_start_export: bool,
Expand All @@ -307,34 +319,31 @@ pub struct ConstantParams {
tail_call_enabled: bool,
relaxed_simd_enabled: bool,
saturating_float_to_int_enabled: bool,
sign_extension_enabled: bool,
sign_extension_ops_enabled: bool,
simd_enabled: bool,
float_enabled: bool,
memory_grow_enabled: bool,
allow_floats: bool,
min_types: usize,
memory_offset_choices: (u32, u32, u32),
reserved_memory_size: Option<u64>,
}

impl Default for ConstantParams {
fn default() -> Self {
ConstantParams {
bulk_memory_enabled: false,
sign_extension_enabled: false,
sign_extension_ops_enabled: false,
saturating_float_to_int_enabled: false,
reference_types_enabled: false,
tail_call_enabled: false,
// This is related to reference_types_enabled.
max_tables: 1,
simd_enabled: false,
float_enabled: false,
allow_floats: false,
relaxed_simd_enabled: false,
exceptions_enabled: false,
memory64_enabled: false,
disallow_traps: true,
allow_start_export: false,
multi_value_enabled: false,
memory_grow_enabled: false,
min_memories: 0,
max_memories: 1,
min_exports: 0,
Expand All @@ -346,7 +355,8 @@ impl Default for ConstantParams {
max_types: 100,
min_types: 5,
memory_offset_choices: (75, 25, 0),
reserved_memory_size: Some(MemoryLayout::RESERVED_MEMORY_SIZE as u64),
// TODO: revert
//reserved_memory_size: Some(MemoryLayout::RESERVED_MEMORY_SIZE as u64),
}
}
}
Expand Down

0 comments on commit a342a7d

Please sign in to comment.