| 1 |
use wasm_smith::Config; |
| 2 |
use wasmparser::{types::Types, Validator, WasmFeatures}; |
| 3 |
|
| 4 |
pub fn parser_features_from_config(config: &Config) -> WasmFeatures { |
| 5 |
let mut features = WasmFeatures::MUTABLE_GLOBAL | WasmFeatures::WASM1; |
| 6 |
features.set( |
| 7 |
WasmFeatures::SATURATING_FLOAT_TO_INT, |
| 8 |
config.saturating_float_to_int_enabled, |
| 9 |
); |
| 10 |
features.set( |
| 11 |
WasmFeatures::SIGN_EXTENSION, |
| 12 |
config.sign_extension_ops_enabled, |
| 13 |
); |
| 14 |
features.set( |
| 15 |
WasmFeatures::REFERENCE_TYPES, |
| 16 |
config.reference_types_enabled, |
| 17 |
); |
| 18 |
features.set(WasmFeatures::MULTI_VALUE, config.multi_value_enabled); |
| 19 |
features.set(WasmFeatures::BULK_MEMORY, config.bulk_memory_enabled); |
| 20 |
features.set(WasmFeatures::SIMD, config.simd_enabled); |
| 21 |
features.set(WasmFeatures::RELAXED_SIMD, config.relaxed_simd_enabled); |
| 22 |
features.set(WasmFeatures::MULTI_MEMORY, config.max_memories > 1); |
| 23 |
features.set(WasmFeatures::EXCEPTIONS, config.exceptions_enabled); |
| 24 |
features.set(WasmFeatures::MEMORY64, config.memory64_enabled); |
| 25 |
features.set(WasmFeatures::TAIL_CALL, config.tail_call_enabled); |
| 26 |
features.set(WasmFeatures::FUNCTION_REFERENCES, config.gc_enabled); |
| 27 |
features.set(WasmFeatures::GC, config.gc_enabled); |
| 28 |
features.set(WasmFeatures::THREADS, config.threads_enabled); |
| 29 |
features.set( |
| 30 |
WasmFeatures::CUSTOM_PAGE_SIZES, |
| 31 |
config.custom_page_sizes_enabled, |
| 32 |
); |
| 33 |
features |
| 34 |
} |
| 35 |
|
| 36 |
pub fn validate(validator: &mut Validator, bytes: &[u8]) -> Types { |
| 37 |
let err = match validator.validate_all(bytes) { |
| 38 |
Ok(types) => return types, |
| 39 |
Err(e) => e, |
| 40 |
}; |
| 41 |
eprintln!("Writing Wasm to `test.wasm`"); |
| 42 |
drop(std::fs::write("test.wasm", &bytes)); |
| 43 |
if let Ok(text) = wasmprinter::print_bytes(bytes) { |
| 44 |
eprintln!("Writing WAT to `test.wat`"); |
| 45 |
drop(std::fs::write("test.wat", &text)); |
| 46 |
} |
| 47 |
panic!("wasm failed to validate: {}", err); |
| 48 |
} |