|
| 1 | +// Copyright (c) 2022-present INESC-ID. |
| 2 | +// Distributed under the MIT license that can be found in the LICENSE file. |
| 3 | + |
| 4 | +use std::any::{Any, TypeId}; |
| 5 | +use std::marker::PhantomData; |
| 6 | +use std::rc::Rc; |
| 7 | + |
| 8 | +use crate::rc::{AnyPtr, ErasedPtr}; |
| 9 | +use crate::reinterpret::ByteRepr; |
| 10 | + |
| 11 | +#[derive(Clone)] |
| 12 | +pub(crate) struct FnState { |
| 13 | + addr: usize, |
| 14 | + cast_history: Vec<Option<Rc<dyn Any>>>, |
| 15 | +} |
| 16 | + |
| 17 | +pub struct FnPtr<T> { |
| 18 | + state: Option<Rc<FnState>>, |
| 19 | + // FnPtr does not use T, hence wrap in PhantomData |
| 20 | + _marker: PhantomData<T>, |
| 21 | +} |
| 22 | + |
| 23 | +impl<T> FnPtr<T> { |
| 24 | + #[inline] |
| 25 | + pub fn null() -> Self { |
| 26 | + FnPtr { |
| 27 | + state: None, |
| 28 | + _marker: PhantomData, |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + #[inline] |
| 33 | + pub fn is_null(&self) -> bool { |
| 34 | + self.state.is_none() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<T: 'static> FnPtr<T> { |
| 39 | + pub fn new(f: T, addr: usize) -> Self { |
| 40 | + FnPtr { |
| 41 | + state: Some(Rc::new(FnState { |
| 42 | + addr, |
| 43 | + cast_history: vec![Some(Rc::new(f))], |
| 44 | + })), |
| 45 | + _marker: PhantomData, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + pub fn cast<U: 'static>(&self, adapter: Option<U>) -> FnPtr<U> { |
| 50 | + let state = self.state.as_ref().expect("ub: null fn pointer cast"); |
| 51 | + |
| 52 | + for (i, entry) in state.cast_history.iter().enumerate() { |
| 53 | + if let Some(ref rc) = entry { |
| 54 | + if (*rc).as_ref().type_id() == TypeId::of::<U>() { |
| 55 | + return FnPtr { |
| 56 | + state: Some(Rc::new(FnState { |
| 57 | + addr: state.addr, |
| 58 | + cast_history: state.cast_history[..=i].to_vec(), |
| 59 | + })), |
| 60 | + _marker: PhantomData, |
| 61 | + }; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + let mut new_stack = state.cast_history.clone(); |
| 67 | + new_stack.push(adapter.map(|a| Rc::new(a) as Rc<dyn Any>)); |
| 68 | + |
| 69 | + FnPtr { |
| 70 | + state: Some(Rc::new(FnState { |
| 71 | + addr: state.addr, |
| 72 | + cast_history: new_stack, |
| 73 | + })), |
| 74 | + _marker: PhantomData, |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + pub fn call(&self) -> T |
| 79 | + where |
| 80 | + T: Copy, |
| 81 | + { |
| 82 | + let state = self.state.as_ref().expect("ub: null fn pointer call"); |
| 83 | + let entry = state |
| 84 | + .cast_history |
| 85 | + .last() |
| 86 | + .expect("empty fn pointer cast_history"); |
| 87 | + match entry { |
| 88 | + Some(rc) => *rc |
| 89 | + .downcast_ref::<T>() |
| 90 | + .expect("ub: fn pointer type mismatch"), |
| 91 | + None => panic!("ub: calling through incompatible fn pointer type"), |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl<T> Clone for FnPtr<T> { |
| 97 | + fn clone(&self) -> Self { |
| 98 | + FnPtr { |
| 99 | + state: self.state.clone(), |
| 100 | + _marker: PhantomData, |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl<T> Default for FnPtr<T> { |
| 106 | + fn default() -> Self { |
| 107 | + Self::null() |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl<T> PartialEq for FnPtr<T> { |
| 112 | + fn eq(&self, other: &Self) -> bool { |
| 113 | + match (&self.state, &other.state) { |
| 114 | + (None, None) => true, |
| 115 | + (Some(a), Some(b)) => a.addr == b.addr, |
| 116 | + _ => false, |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl<T> Eq for FnPtr<T> {} |
| 122 | + |
| 123 | +impl<T> std::fmt::Debug for FnPtr<T> { |
| 124 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 125 | + match &self.state { |
| 126 | + None => write!(f, "FnPtr(null)"), |
| 127 | + Some(s) => write!(f, "FnPtr(0x{:x})", s.addr), |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +impl<T: 'static> ByteRepr for FnPtr<T> {} |
| 133 | + |
| 134 | +impl<T: 'static> ErasedPtr for FnPtr<T> { |
| 135 | + fn pointee_type_id(&self) -> TypeId { |
| 136 | + TypeId::of::<T>() |
| 137 | + } |
| 138 | + fn memcpy(&self, _src: &dyn ErasedPtr, _len: usize) { |
| 139 | + panic!("memcpy not supported on fn pointer"); |
| 140 | + } |
| 141 | + fn as_any(&self) -> &dyn Any { |
| 142 | + self |
| 143 | + } |
| 144 | + fn equals(&self, other: &dyn ErasedPtr) -> Option<bool> { |
| 145 | + if self.pointee_type_id() != other.pointee_type_id() { |
| 146 | + return None; |
| 147 | + } |
| 148 | + other.as_any().downcast_ref::<FnPtr<T>>().map(|o| self == o) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +impl<T: 'static> FnPtr<T> { |
| 153 | + pub fn to_any(&self) -> AnyPtr { |
| 154 | + AnyPtr { |
| 155 | + ptr: Rc::new(self.clone()), |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +impl AnyPtr { |
| 161 | + pub fn cast_fn<T: 'static>(&self) -> Option<FnPtr<T>> { |
| 162 | + self.ptr.as_any().downcast_ref::<FnPtr<T>>().cloned() |
| 163 | + } |
| 164 | +} |
0 commit comments