/src/wasmi/crates/fuzz/src/value.rs
Line | Count | Source |
1 | | use arbitrary::{Arbitrary, Unstructured}; |
2 | | use wasmi::{ValType, V128}; |
3 | | |
4 | | /// A Wasm value type supported by the Wasmi fuzzing infrastructure. |
5 | | #[derive(Debug, Copy, Clone)] |
6 | | pub enum FuzzValType { |
7 | | /// The Wasm `i32` type. |
8 | | I32, |
9 | | /// The Wasm `i64` type. |
10 | | I64, |
11 | | /// The Wasm `f32` type. |
12 | | F32, |
13 | | /// The Wasm `f64` type. |
14 | | F64, |
15 | | /// The Wasm `v128` type. |
16 | | V128, |
17 | | /// The Wasm `funcref` type. |
18 | | FuncRef, |
19 | | /// The Wasm `externref` type. |
20 | | ExternRef, |
21 | | } |
22 | | |
23 | | impl From<ValType> for FuzzValType { |
24 | 2.75M | fn from(ty: ValType) -> Self { |
25 | 2.75M | match ty { |
26 | 1.03M | ValType::I32 => Self::I32, |
27 | 608k | ValType::I64 => Self::I64, |
28 | 133k | ValType::F32 => Self::F32, |
29 | 550k | ValType::F64 => Self::F64, |
30 | 421k | ValType::V128 => Self::V128, |
31 | 0 | ValType::FuncRef => Self::FuncRef, |
32 | 0 | ValType::ExternRef => Self::ExternRef, |
33 | | } |
34 | 2.75M | } <wasmi_fuzz::value::FuzzValType as core::convert::From<wasmi_core::value::ValType>>::from Line | Count | Source | 24 | 1.00M | fn from(ty: ValType) -> Self { | 25 | 1.00M | match ty { | 26 | 136k | ValType::I32 => Self::I32, | 27 | 350k | ValType::I64 => Self::I64, | 28 | 45.6k | ValType::F32 => Self::F32, | 29 | 313k | ValType::F64 => Self::F64, | 30 | 160k | ValType::V128 => Self::V128, | 31 | 0 | ValType::FuncRef => Self::FuncRef, | 32 | 0 | ValType::ExternRef => Self::ExternRef, | 33 | | } | 34 | 1.00M | } |
<wasmi_fuzz::value::FuzzValType as core::convert::From<wasmi_core::value::ValType>>::from Line | Count | Source | 24 | 1.74M | fn from(ty: ValType) -> Self { | 25 | 1.74M | match ty { | 26 | 901k | ValType::I32 => Self::I32, | 27 | 258k | ValType::I64 => Self::I64, | 28 | 88.3k | ValType::F32 => Self::F32, | 29 | 236k | ValType::F64 => Self::F64, | 30 | 261k | ValType::V128 => Self::V128, | 31 | 0 | ValType::FuncRef => Self::FuncRef, | 32 | 0 | ValType::ExternRef => Self::ExternRef, | 33 | | } | 34 | 1.74M | } |
|
35 | | } |
36 | | |
37 | | /// A Wasm value supported by the Wasmi fuzzing infrastructure. |
38 | | #[derive(Debug, Clone)] |
39 | | pub enum FuzzVal { |
40 | | I32(i32), |
41 | | I64(i64), |
42 | | F32(f32), |
43 | | F64(f64), |
44 | | V128(u128), |
45 | | FuncRef { is_null: bool }, |
46 | | ExternRef { is_null: bool }, |
47 | | } |
48 | | |
49 | | impl PartialEq for FuzzVal { |
50 | 908k | fn eq(&self, other: &Self) -> bool { |
51 | 908k | match (self, other) { |
52 | 238k | (Self::I32(l), Self::I32(r)) => l == r, |
53 | 335k | (Self::I64(l), Self::I64(r)) => l == r, |
54 | 66.3k | (Self::F32(l), Self::F32(r)) => l.to_bits() == r.to_bits(), |
55 | 187k | (Self::F64(l), Self::F64(r)) => l.to_bits() == r.to_bits(), |
56 | 79.9k | (Self::V128(l), Self::V128(r)) => l == r, |
57 | 0 | (Self::FuncRef { is_null: l }, Self::FuncRef { is_null: r }) => l == r, |
58 | 0 | (Self::ExternRef { is_null: l }, Self::ExternRef { is_null: r }) => l == r, |
59 | 0 | _ => false, |
60 | | } |
61 | 908k | } <wasmi_fuzz::value::FuzzVal as core::cmp::PartialEq>::eq Line | Count | Source | 50 | 908k | fn eq(&self, other: &Self) -> bool { | 51 | 908k | match (self, other) { | 52 | 238k | (Self::I32(l), Self::I32(r)) => l == r, | 53 | 335k | (Self::I64(l), Self::I64(r)) => l == r, | 54 | 66.3k | (Self::F32(l), Self::F32(r)) => l.to_bits() == r.to_bits(), | 55 | 187k | (Self::F64(l), Self::F64(r)) => l.to_bits() == r.to_bits(), | 56 | 79.9k | (Self::V128(l), Self::V128(r)) => l == r, | 57 | 0 | (Self::FuncRef { is_null: l }, Self::FuncRef { is_null: r }) => l == r, | 58 | 0 | (Self::ExternRef { is_null: l }, Self::ExternRef { is_null: r }) => l == r, | 59 | 0 | _ => false, | 60 | | } | 61 | 908k | } |
Unexecuted instantiation: <wasmi_fuzz::value::FuzzVal as core::cmp::PartialEq>::eq |
62 | | } |
63 | | |
64 | | impl Eq for FuzzVal {} |
65 | | |
66 | | impl FuzzVal { |
67 | | /// Creates a new [`FuzzVal`] of the given `ty` initialized by `u`. |
68 | 2.75M | pub fn with_type(ty: FuzzValType, u: &mut Unstructured) -> Self { |
69 | 2.75M | match ty { |
70 | 1.03M | FuzzValType::I32 => Self::I32(i32::arbitrary(u).unwrap_or_default()), |
71 | 608k | FuzzValType::I64 => Self::I64(i64::arbitrary(u).unwrap_or_default()), |
72 | 133k | FuzzValType::F32 => Self::F32(f32::arbitrary(u).unwrap_or_default()), |
73 | 550k | FuzzValType::F64 => Self::F64(f64::arbitrary(u).unwrap_or_default()), |
74 | 421k | FuzzValType::V128 => Self::V128(u128::arbitrary(u).unwrap_or_default()), |
75 | 0 | FuzzValType::FuncRef => Self::FuncRef { is_null: true }, |
76 | 0 | FuzzValType::ExternRef => Self::ExternRef { is_null: true }, |
77 | | } |
78 | 2.75M | } <wasmi_fuzz::value::FuzzVal>::with_type Line | Count | Source | 68 | 1.00M | pub fn with_type(ty: FuzzValType, u: &mut Unstructured) -> Self { | 69 | 1.00M | match ty { | 70 | 136k | FuzzValType::I32 => Self::I32(i32::arbitrary(u).unwrap_or_default()), | 71 | 350k | FuzzValType::I64 => Self::I64(i64::arbitrary(u).unwrap_or_default()), | 72 | 45.6k | FuzzValType::F32 => Self::F32(f32::arbitrary(u).unwrap_or_default()), | 73 | 313k | FuzzValType::F64 => Self::F64(f64::arbitrary(u).unwrap_or_default()), | 74 | 160k | FuzzValType::V128 => Self::V128(u128::arbitrary(u).unwrap_or_default()), | 75 | 0 | FuzzValType::FuncRef => Self::FuncRef { is_null: true }, | 76 | 0 | FuzzValType::ExternRef => Self::ExternRef { is_null: true }, | 77 | | } | 78 | 1.00M | } |
<wasmi_fuzz::value::FuzzVal>::with_type Line | Count | Source | 68 | 1.74M | pub fn with_type(ty: FuzzValType, u: &mut Unstructured) -> Self { | 69 | 1.74M | match ty { | 70 | 901k | FuzzValType::I32 => Self::I32(i32::arbitrary(u).unwrap_or_default()), | 71 | 258k | FuzzValType::I64 => Self::I64(i64::arbitrary(u).unwrap_or_default()), | 72 | 88.3k | FuzzValType::F32 => Self::F32(f32::arbitrary(u).unwrap_or_default()), | 73 | 236k | FuzzValType::F64 => Self::F64(f64::arbitrary(u).unwrap_or_default()), | 74 | 261k | FuzzValType::V128 => Self::V128(u128::arbitrary(u).unwrap_or_default()), | 75 | 0 | FuzzValType::FuncRef => Self::FuncRef { is_null: true }, | 76 | 0 | FuzzValType::ExternRef => Self::ExternRef { is_null: true }, | 77 | | } | 78 | 1.74M | } |
|
79 | | } |
80 | | |
81 | | impl From<FuzzVal> for wasmi::Val { |
82 | 2.75M | fn from(value: FuzzVal) -> Self { |
83 | 2.75M | match value { |
84 | 1.03M | FuzzVal::I32(value) => Self::I32(value), |
85 | 608k | FuzzVal::I64(value) => Self::I64(value), |
86 | 133k | FuzzVal::F32(value) => Self::F32(value.into()), |
87 | 550k | FuzzVal::F64(value) => Self::F64(value.into()), |
88 | 421k | FuzzVal::V128(value) => Self::V128(V128::from(value)), |
89 | 0 | FuzzVal::FuncRef { is_null } => { |
90 | 0 | assert!(is_null); |
91 | 0 | Self::FuncRef(<wasmi::Ref<wasmi::Func>>::Null) |
92 | | } |
93 | 0 | FuzzVal::ExternRef { is_null } => { |
94 | 0 | assert!(is_null); |
95 | 0 | Self::ExternRef(<wasmi::Ref<wasmi::ExternRef>>::Null) |
96 | | } |
97 | | } |
98 | 2.75M | } <wasmi::value::Val as core::convert::From<wasmi_fuzz::value::FuzzVal>>::from Line | Count | Source | 82 | 1.00M | fn from(value: FuzzVal) -> Self { | 83 | 1.00M | match value { | 84 | 136k | FuzzVal::I32(value) => Self::I32(value), | 85 | 350k | FuzzVal::I64(value) => Self::I64(value), | 86 | 45.6k | FuzzVal::F32(value) => Self::F32(value.into()), | 87 | 313k | FuzzVal::F64(value) => Self::F64(value.into()), | 88 | 160k | FuzzVal::V128(value) => Self::V128(V128::from(value)), | 89 | 0 | FuzzVal::FuncRef { is_null } => { | 90 | 0 | assert!(is_null); | 91 | 0 | Self::FuncRef(<wasmi::Ref<wasmi::Func>>::Null) | 92 | | } | 93 | 0 | FuzzVal::ExternRef { is_null } => { | 94 | 0 | assert!(is_null); | 95 | 0 | Self::ExternRef(<wasmi::Ref<wasmi::ExternRef>>::Null) | 96 | | } | 97 | | } | 98 | 1.00M | } |
<wasmi::value::Val as core::convert::From<wasmi_fuzz::value::FuzzVal>>::from Line | Count | Source | 82 | 1.74M | fn from(value: FuzzVal) -> Self { | 83 | 1.74M | match value { | 84 | 901k | FuzzVal::I32(value) => Self::I32(value), | 85 | 258k | FuzzVal::I64(value) => Self::I64(value), | 86 | 88.3k | FuzzVal::F32(value) => Self::F32(value.into()), | 87 | 236k | FuzzVal::F64(value) => Self::F64(value.into()), | 88 | 261k | FuzzVal::V128(value) => Self::V128(V128::from(value)), | 89 | 0 | FuzzVal::FuncRef { is_null } => { | 90 | 0 | assert!(is_null); | 91 | 0 | Self::FuncRef(<wasmi::Ref<wasmi::Func>>::Null) | 92 | | } | 93 | 0 | FuzzVal::ExternRef { is_null } => { | 94 | 0 | assert!(is_null); | 95 | 0 | Self::ExternRef(<wasmi::Ref<wasmi::ExternRef>>::Null) | 96 | | } | 97 | | } | 98 | 1.74M | } |
|
99 | | } |