|
| 1 | +#![no_main] |
| 2 | + |
| 3 | +use std::ops::ControlFlow; |
| 4 | + |
| 5 | +use arbitrary::{Arbitrary, Result as ArbitraryResult, Unstructured}; |
| 6 | +use everscale_types::arbitrary::OrdinaryCell; |
| 7 | +use everscale_types::cell::CellBuilder; |
| 8 | +use libfuzzer_sys::{fuzz_target, Corpus}; |
| 9 | +use num_bigint::{BigInt, Sign}; |
| 10 | +use tycho_vm::{CustomSmcInfo, GasParams, RcStackValue, SafeRc, Stack, Tuple, VmState}; |
| 11 | + |
| 12 | +const MAX_BIGINT_BYTES: usize = 32; // Max bytes for BigInt generation (32 bytes = 256 bits) |
| 13 | +const MAX_TUPLE_ELEMENTS: u32 = 16; |
| 14 | + |
| 15 | +#[derive(Debug)] |
| 16 | +enum ArbitraryStackValue { |
| 17 | + Null, |
| 18 | + Int(BigInt), |
| 19 | + NaN, |
| 20 | + Cell(OrdinaryCell), |
| 21 | + EmptyBuilder, |
| 22 | + Tuple(Vec<ArbitraryStackValue>), |
| 23 | +} |
| 24 | + |
| 25 | +impl<'a> Arbitrary<'a> for ArbitraryStackValue { |
| 26 | + fn arbitrary(u: &mut Unstructured<'a>) -> ArbitraryResult<Self> { |
| 27 | + match u.int_in_range(0..=5)? { |
| 28 | + 0 => Ok(ArbitraryStackValue::Null), |
| 29 | + 1 => { |
| 30 | + let sign = match u.int_in_range(0..=2)? { |
| 31 | + 0 => Sign::Plus, |
| 32 | + 1 => Sign::Minus, |
| 33 | + _ => Sign::NoSign, |
| 34 | + }; |
| 35 | + |
| 36 | + let num_bytes = u.int_in_range(0..=MAX_BIGINT_BYTES)?; |
| 37 | + let bytes_slice: &[u8] = u.bytes(num_bytes)?; |
| 38 | + Ok(ArbitraryStackValue::Int(BigInt::from_bytes_be( |
| 39 | + sign, |
| 40 | + bytes_slice, |
| 41 | + ))) |
| 42 | + } |
| 43 | + 2 => Ok(ArbitraryStackValue::NaN), |
| 44 | + 3 => { |
| 45 | + let cell: OrdinaryCell = u.arbitrary()?; |
| 46 | + Ok(ArbitraryStackValue::Cell(cell)) |
| 47 | + } |
| 48 | + 4 => Ok(ArbitraryStackValue::EmptyBuilder), |
| 49 | + 5 => { |
| 50 | + // Tuple (recursive, up to MAX_TUPLE_ELEMENTS) |
| 51 | + let mut items = Vec::new(); |
| 52 | + u.arbitrary_loop(None, Some(MAX_TUPLE_ELEMENTS), |u_inner| { |
| 53 | + items.push(u_inner.arbitrary()?); |
| 54 | + Ok(ControlFlow::Continue(())) |
| 55 | + })?; |
| 56 | + Ok(ArbitraryStackValue::Tuple(items)) |
| 57 | + } |
| 58 | + _ => unreachable!(), |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fn arbitrary_to_rc(item: ArbitraryStackValue) -> Option<RcStackValue> { |
| 64 | + Some(match item { |
| 65 | + ArbitraryStackValue::Null => Stack::make_null(), |
| 66 | + ArbitraryStackValue::Int(val) => SafeRc::new_dyn_value(val), |
| 67 | + ArbitraryStackValue::NaN => Stack::make_nan(), |
| 68 | + ArbitraryStackValue::Cell(OrdinaryCell(cell)) => SafeRc::new_dyn_value(cell), |
| 69 | + ArbitraryStackValue::EmptyBuilder => SafeRc::new_dyn_value(CellBuilder::new()), |
| 70 | + ArbitraryStackValue::Tuple(items) => { |
| 71 | + let tuple_items: Tuple = items |
| 72 | + .into_iter() |
| 73 | + .map(arbitrary_to_rc) |
| 74 | + .collect::<Option<Vec<_>>>()?; |
| 75 | + SafeRc::new_dyn_value(tuple_items) |
| 76 | + } |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +const MAX_ITEMS: u32 = 32; |
| 81 | + |
| 82 | +#[derive(Debug)] |
| 83 | +struct Input { |
| 84 | + code: OrdinaryCell, |
| 85 | + initial_stack_items: Vec<ArbitraryStackValue>, |
| 86 | + c7_items: Vec<ArbitraryStackValue>, |
| 87 | +} |
| 88 | + |
| 89 | +impl<'a> Arbitrary<'a> for Input { |
| 90 | + fn arbitrary(u: &mut Unstructured<'a>) -> ArbitraryResult<Self> { |
| 91 | + let code: OrdinaryCell = u.arbitrary()?; |
| 92 | + |
| 93 | + let mut initial_stack_items = Vec::new(); |
| 94 | + u.arbitrary_loop(None, Some(MAX_ITEMS), |u_inner| { |
| 95 | + initial_stack_items.push(u_inner.arbitrary()?); |
| 96 | + Ok(ControlFlow::Continue(())) |
| 97 | + })?; |
| 98 | + |
| 99 | + let mut c7_items = Vec::new(); |
| 100 | + u.arbitrary_loop(None, Some(MAX_ITEMS), |u_inner| { |
| 101 | + c7_items.push(u_inner.arbitrary()?); |
| 102 | + Ok(ControlFlow::Continue(())) |
| 103 | + })?; |
| 104 | + |
| 105 | + Ok(Input { |
| 106 | + code, |
| 107 | + initial_stack_items, |
| 108 | + c7_items, |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +fuzz_target!(|input: Input| -> Corpus { |
| 114 | + let OrdinaryCell(code) = input.code; |
| 115 | + |
| 116 | + let stack_items: Option<Vec<RcStackValue>> = input |
| 117 | + .initial_stack_items |
| 118 | + .into_iter() |
| 119 | + .map(arbitrary_to_rc) |
| 120 | + .collect(); |
| 121 | + |
| 122 | + let stack_items = match stack_items { |
| 123 | + Some(items) => items, |
| 124 | + None => return Corpus::Reject, |
| 125 | + }; |
| 126 | + |
| 127 | + let c7_rc_items: Option<Vec<RcStackValue>> = |
| 128 | + input.c7_items.into_iter().map(arbitrary_to_rc).collect(); |
| 129 | + let c7_rc_items = match c7_rc_items { |
| 130 | + Some(items) => items, |
| 131 | + None => return Corpus::Reject, |
| 132 | + }; |
| 133 | + |
| 134 | + let stack = Stack::with_items(stack_items); |
| 135 | + |
| 136 | + let start = std::time::Instant::now(); |
| 137 | + let mut vm = VmState::builder() |
| 138 | + .with_code(code) |
| 139 | + .with_raw_stack(stack.into()) |
| 140 | + .with_smc_info(CustomSmcInfo { |
| 141 | + version: VmState::DEFAULT_VERSION, |
| 142 | + c7: SafeRc::new(c7_rc_items), |
| 143 | + }) |
| 144 | + .with_gas(GasParams::getter()) |
| 145 | + .build(); |
| 146 | + |
| 147 | + let _ = vm.run(); |
| 148 | + |
| 149 | + let elapsed = start.elapsed().as_millis(); |
| 150 | + if elapsed > 500 { |
| 151 | + panic!("Execution took too long: {} ms", elapsed); |
| 152 | + } |
| 153 | + |
| 154 | + Corpus::Keep |
| 155 | +}); |
0 commit comments