1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
//! This module provides traits for calling rust functions dynamically.
//!
//! All functions which implement the DynamicFunction trait can be called by passing an array of
//! [`EvaluatedTerm`]s to it. The return value is again of type [`EvaluatedTerm`].
//!
//! Rust is a statically typed language. That means the compiler would be able to statically verify
//! that a term evaluates without any type errors.
//!
//! While this is generally an advance, in the case of our fuzzer this is not very helpful.
//! The fuzzer should be able to mutate the term trees arbitrarily. Of course, we also have
//! to check for the types during runtime. If types are not compatible then, the evaluation
//! of the term will fail. But this is not something that can be done during compile time.
//! Therefore, we introduced a trait for dynamically typed functions on top of statically
//! typed Rust functions.
//!
//! Each function which implements the following trait can be made into a dynamic function:
//!
//! ```rust
//! use puffin::algebra::error::FnError;
//!
//! type ConcreteFunction<A1, A2, A3, R> = dyn Fn(A1, A2, A3) -> Result<R, FnError>;
//! ```
//!
//! where `A1`, `A2`, `A3` are argument types and `R` is the return type. From these statically
//! typed function we can generate dynamically types ones which implement the following trait:
//!
//! ```rust
//! use std::any::Any;
//!
//! use puffin::algebra::error::FnError;
//! use puffin::protocol::{EvaluatedTerm, ProtocolTypes};
//!
//! pub trait DynamicFunction<PT: ProtocolTypes>:
//! Fn(&Vec<Box<dyn EvaluatedTerm<PT>>>) -> Result<Box<dyn EvaluatedTerm<PT>>, FnError>
//! {
//! }
//! ```
//!
//! Note, that both functions return a `Result` and therefore can gracefully fail.
//!
//! `DynamicFunctions` can be called with an array of any type implementing the EvaluatedTerm
//! trait. The result must also implement EvaluatedTerm. Rust offers a unique ID for each type.
//! Using this type we can check during runtime whether types are available. The types of each
//! variable, constant and function are preserved and stored alongside the `DynamicFunction`.
//!
//! The following function is a simple example for a constant:
//!
//! ```rust
//! use puffin::algebra::error::FnError;
//!
//! pub fn fn_some_value() -> Result<u32, FnError> {
//! Ok(42)
//! }
//! ```
//!
//! It returns one possibility for the cipher suites which could be sent during a `ClientHello`.
use std::any::{type_name, TypeId};
use std::collections::hash_map::DefaultHasher;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use itertools::Itertools;
use serde::de::Visitor;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use super::error::FnError;
use crate::protocol::{EvaluatedTerm, ProtocolTypes};
/// Describes the shape of a [`DynamicFunction`]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(bound = "PT: ProtocolTypes")]
pub struct DynamicFunctionShape<PT: ProtocolTypes> {
pub name: &'static str,
pub argument_types: Vec<TypeShape<PT>>,
pub return_type: TypeShape<PT>,
}
impl<PT: ProtocolTypes> Eq for DynamicFunctionShape<PT> {}
impl<PT: ProtocolTypes> PartialEq for DynamicFunctionShape<PT> {
fn eq(&self, other: &Self) -> bool {
self.name.eq(other.name) // name is unique
}
}
impl<PT: ProtocolTypes> Hash for DynamicFunctionShape<PT> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state)
}
}
impl<PT: ProtocolTypes> DynamicFunctionShape<PT> {
pub fn arity(&self) -> u16 {
self.argument_types.len() as u16
}
pub fn is_constant(&self) -> bool {
self.arity() == 0
}
}
impl<PT: ProtocolTypes> fmt::Display for DynamicFunctionShape<PT> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}({}) -> {}",
self.name,
self.argument_types
.iter()
.map(|typ| typ.name.to_string())
.join(","),
self.return_type.name
)
}
}
/// Hashes [`TypeId`]s to be more readable
fn hash_type_id(type_id: &TypeId) -> u64 {
let mut hasher = DefaultHasher::new();
type_id.hash(&mut hasher);
hasher.finish()
}
fn format_args<PT: ProtocolTypes, P: AsRef<dyn EvaluatedTerm<PT>>>(anys: &[P]) -> String {
format!(
"({})",
anys.iter()
.map(|any| {
let id = &any.as_ref().as_any().type_id();
format!("{:x}", hash_type_id(id))
})
.join(",")
)
}
/// Cloneable type for dynamic functions. This trait is automatically implemented for arbitrary
/// closures and functions of the form: `Fn(&Vec<Box<dyn Any>>) -> Box<dyn Any>`
///
/// [`Clone`] is implemented for `Box<dyn DynamicFunction>` using this trick:
/// <https://users.rust-lang.org/t/how-to-clone-a-boxed-closure/31035/25>
///
/// We want to use Any here and not VariableData (which implements Clone). Else all returned types
/// in functions op_impl.rs would need to return a cloneable struct. Message for example is not.
pub trait DynamicFunction<PT: ProtocolTypes>:
Fn(&Vec<Box<dyn EvaluatedTerm<PT>>>) -> Result<Box<dyn EvaluatedTerm<PT>>, FnError> + Send + Sync
{
fn clone_box(&self) -> Box<dyn DynamicFunction<PT>>;
}
impl<F, PT: ProtocolTypes> DynamicFunction<PT> for F
where
F: 'static
+ Fn(&Vec<Box<dyn EvaluatedTerm<PT>>>) -> Result<Box<dyn EvaluatedTerm<PT>>, FnError>
+ Clone
+ Send
+ Sync,
{
fn clone_box(&self) -> Box<dyn DynamicFunction<PT>> {
Box::new(self.clone())
}
}
impl<PT: ProtocolTypes> fmt::Debug for Box<dyn DynamicFunction<PT>> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "DynamicFunction")
}
}
impl<PT: ProtocolTypes> fmt::Display for Box<dyn DynamicFunction<PT>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DynamicFunction")
}
}
impl<PT: ProtocolTypes> Clone for Box<dyn DynamicFunction<PT>> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
/// This trait is implemented for function traits in order to:
/// * describe their shape during runtime
/// * wrap them into a [`DynamicFunction`] which is callable with arbitrary data
///
/// Adapted from <https://jsdw.me/posts/rust-fn-traits/> but using type ids
pub trait DescribableFunction<PT: ProtocolTypes, Types> {
fn name(&'static self) -> &'static str;
fn shape() -> DynamicFunctionShape<PT>;
fn make_dynamic(&'static self) -> Box<dyn DynamicFunction<PT>>;
}
macro_rules! dynamic_fn {
($($arg:ident)* => $res:ident) => (
impl<F,PT : ProtocolTypes, $res: 'static, $($arg: 'static),*>
DescribableFunction<PT, ($res, $($arg),*)> for F
where
F: (Fn($(&$arg),*) -> Result<$res, FnError>) + Send + Sync,
$res: Send + Sync,
R: EvaluatedTerm<PT>,
$($arg: Send + Sync),*
{
fn shape() -> DynamicFunctionShape<PT> {
DynamicFunctionShape::<PT> {
name: std::any::type_name::<F>(),
argument_types: vec![$(TypeShape::<PT>::of::<$arg>()),*],
return_type: TypeShape::<PT>::of::<$res>(),
}
}
fn name(&'static self) -> &'static str {
std::any::type_name::<F>()
}
fn make_dynamic(&'static self) -> Box<dyn DynamicFunction<PT>> {
#[allow(unused_variables)]
Box::new(move |args: &Vec<Box<dyn EvaluatedTerm<PT>>>| {
#[allow(unused_mut)]
let mut index = 0;
let result: Result<$res, FnError> = self($(
#[allow(unused_assignments)]
#[allow(clippy::mixed_read_write_in_expression)]
{
if let Some(arg_) = args.get(index)
.ok_or_else(|| {
let shape = Self::shape();
FnError::Unknown(format!("Missing argument #{} while calling {}.", index + 1, shape.name))
})?
.as_any().downcast_ref::<$arg>() {
index += 1;
arg_
} else {
let shape = Self::shape();
return Err(FnError::Unknown(format!(
"Passed argument #{} of {} did not match the shape {}. Hashes of passed types are {}.",
index + 1,
shape.name,
shape,
format_args(args)
)));
}
}
),*);
result.map(|result| Box::new(result) as Box<dyn EvaluatedTerm<PT>>)
})
}
}
)
}
dynamic_fn!( => R);
dynamic_fn!(T1 => R);
dynamic_fn!(T1 T2 => R);
dynamic_fn!(T1 T2 T3 => R);
dynamic_fn!(T1 T2 T3 T4 => R);
dynamic_fn!(T1 T2 T3 T4 T5 => R);
dynamic_fn!(T1 T2 T3 T4 T5 T6 => R);
dynamic_fn!(T1 T2 T3 T4 T5 T6 T7 => R);
dynamic_fn!(T1 T2 T3 T4 T5 T6 T7 T8 => R);
dynamic_fn!(T1 T2 T3 T4 T5 T6 T7 T8 T9 => R);
pub fn make_dynamic<F, PT: ProtocolTypes, Types>(
f: &'static F,
) -> (DynamicFunctionShape<PT>, Box<dyn DynamicFunction<PT>>)
where
F: DescribableFunction<PT, Types> + 'static,
{
(F::shape(), f.make_dynamic())
}
#[derive(Copy, Clone, Debug)]
pub struct TypeShape<PT: ProtocolTypes> {
inner_type_id: TypeId,
pub name: &'static str,
phantom: PhantomData<PT>,
}
impl<PT: ProtocolTypes> TypeShape<PT> {
pub fn of<T: 'static>() -> Self {
Self {
inner_type_id: TypeId::of::<T>(),
name: type_name::<T>(),
phantom: PhantomData,
}
}
}
impl<PT: ProtocolTypes> From<TypeShape<PT>> for TypeId {
fn from(shape: TypeShape<PT>) -> Self {
shape.inner_type_id
}
}
impl<PT: ProtocolTypes> Eq for TypeShape<PT> {}
impl<PT: ProtocolTypes> PartialEq for TypeShape<PT> {
fn eq(&self, other: &Self) -> bool {
self.inner_type_id == other.inner_type_id
}
}
impl<PT: ProtocolTypes> Hash for TypeShape<PT> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.inner_type_id.hash(state);
}
}
impl<PT: ProtocolTypes> fmt::Display for TypeShape<PT> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name)
}
}
impl<PT: ProtocolTypes> Serialize for TypeShape<PT> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.name)
}
}
impl<'de, PT: ProtocolTypes> Deserialize<'de> for TypeShape<PT> {
fn deserialize<D>(deserializer: D) -> Result<TypeShape<PT>, D::Error>
where
D: Deserializer<'de>,
{
struct TypeShapeVisitor<PT: ProtocolTypes>(PhantomData<PT>);
impl<'de, PT: ProtocolTypes> Visitor<'de> for TypeShapeVisitor<PT> {
type Value = TypeShape<PT>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a TypeShape")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
let typ = PT::signature()
.types_by_name
.get(v)
.ok_or_else(|| de::Error::missing_field("could not find type"))?;
Ok(typ.clone())
}
}
deserializer.deserialize_str(TypeShapeVisitor(PhantomData))
}
}