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
/* Copyright (c) Fortanix, Inc.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Trait-based usercall dispatching based on the ABI specification.
//!
//! The macros in this module implement a `trait Usercalls` and a `fn dispatch`
//! that together implement usercall handling according to the parsed ABI.

#![allow(unused)]

use fortanix_sgx_abi::*;

use std::ptr::NonNull;
use std::sync::atomic::{AtomicUsize, Ordering};

use futures::future::Future;

type Register = u64;

trait RegisterArgument {
    fn from_register(_: Register) -> Self;
    fn into_register(self) -> Register;
}

type EnclaveAbort = super::EnclaveAbort<bool>;

pub(crate) type UsercallResult<T> = ::std::result::Result<T, EnclaveAbort>;
pub(crate) type DispatchResult = UsercallResult<(Register, Register)>;

trait ReturnValue {
    fn into_registers(self) -> DispatchResult;
}

macro_rules! define_usercalls {
    // Using `$r:tt` because `$r:ty` doesn't match ! in `dispatch_return_type`
    ($(fn $f:ident($($n:ident: $t:ty),*) $(-> $r:tt)*; )*) => {
        #[repr(C)]
        #[allow(non_camel_case_types)]
        pub(crate) enum UsercallList {
            __enclave_usercalls_invalid,
            $($f,)*
        }

        pub(super) trait Usercalls <'future>: Sized {
            $(fn $f (self, $($n: $t),*) -> dispatch_return_type!($(-> $r )* 'future);)*
            fn other(self, n: u64, a1: u64, a2: u64, a3: u64, a4: u64) -> (Self, DispatchResult) {
                (self, Err($crate::usercalls::EnclaveAbort::InvalidUsercall(n)))
            }

            fn is_exiting(&self) -> bool;
        }

        #[allow(unused_variables)]
        pub(super) async fn dispatch<'future,  H: Usercalls<'future>> (mut handler: H, n: u64, a1: u64, a2: u64, a3: u64, a4: u64) -> (H, DispatchResult) {
            // using if/else because you can't match an integer against enum variants
            let (handler, ret) = $(
                if n == UsercallList::$f as Register {
                    //let mut handler_ref = &mut handler;
                    let (handler, ret) = unsafe {
                        enclave_usercalls_internal_define_usercalls!(handler, replace_args a1,a2,a3,a4 $f($($n),*))
                    };
                    (handler, ReturnValue::into_registers(ret))
                } else
            )*
            {
                handler.other(n, a1, a2, a3, a4)
            };
            if ret.is_ok() && handler.is_exiting() {
                (handler, Err(super::EnclaveAbort::Secondary))
            } else {
                (handler, ret)
            }
        }
    };
}

macro_rules! define_ra {
    (< $i:ident > $t:ty) => {
        impl<$i> RegisterArgument for $t {
            fn from_register(a: Register) -> Self {
                a as _
            }
            fn into_register(self) -> Register {
                self as _
            }
        }
    };
    ($i:ty as $t:ty) => {
        impl RegisterArgument for $t {
            fn from_register(a: Register) -> Self {
                a as $i as _
            }
            fn into_register(self) -> Register {
                self as $i as _
            }
        }
    };
    ($t:ty) => {
        impl RegisterArgument for $t {
            fn from_register(a: Register) -> Self {
                a as _
            }
            fn into_register(self) -> Register {
                self as _
            }
        }
    };
}

define_ra!(Register);
define_ra!(i64);
define_ra!(u32);
define_ra!(u32 as i32);
define_ra!(u16);
define_ra!(u16 as i16);
define_ra!(u8);
define_ra!(u8 as i8);
define_ra!(usize);
define_ra!(usize as isize);
define_ra!(<T> *const T);
define_ra!(<T> *mut T);

impl RegisterArgument for () {
    fn from_register(_: Register) -> () {
        ()
    }
    fn into_register(self) -> Register {
        0
    }
}

impl RegisterArgument for bool {
    fn from_register(a: Register) -> bool {
        if a != 0 {
            true
        } else {
            false
        }
    }
    fn into_register(self) -> Register {
        self as _
    }
}

impl<T: RegisterArgument> RegisterArgument for Option<NonNull<T>> {
    fn from_register(a: Register) -> Option<NonNull<T>> {
        NonNull::new(a as _)
    }
    fn into_register(self) -> Register {
        self.map_or(0 as _, NonNull::as_ptr) as _
    }
}

impl ReturnValue for EnclaveAbort {
    fn into_registers(self) -> DispatchResult {
        Err(self)
    }
}

impl<T: RegisterArgument> ReturnValue for UsercallResult<T> {
    fn into_registers(self) -> DispatchResult {
        self.map(|v| (v.into_register(), 0))
    }
}

impl<T: RegisterArgument, U: RegisterArgument> ReturnValue for UsercallResult<(T, U)> {
    fn into_registers(self) -> DispatchResult {
        self.map(|(a, b)| (a.into_register(), b.into_register()))
    }
}

macro_rules! dispatch_return_type {
    (-> ! $l:lifetime) => { std::pin::Pin<Box<dyn Future<Output = (Self, EnclaveAbort)> + $l>> };
    (-> $r:tt $l:lifetime) => {
                std::pin::Pin<Box<dyn Future<Output = (Self, UsercallResult<$r>)> + $l>>
            };
    ($l:lifetime) => {
                std::pin::Pin<Box<dyn Future<Output = (Self,UsercallResult<()>)> + $l>>
            };
}

macro_rules! enclave_usercalls_internal_define_usercalls {
    (
        $h:ident,replace_args
        $a1:ident,
        $a2:ident,
        $a3:ident,
        $a4:ident
        $f:ident($n1:ident, $n2:ident, $n3:ident, $n4:ident)
    ) => {
        H::$f(
            $h,
            RegisterArgument::from_register($a1),
            RegisterArgument::from_register($a2),
            RegisterArgument::from_register($a3),
            RegisterArgument::from_register($a4),
        )
        .await
    };
    (
        $h:ident,replace_args
        $a1:ident,
        $a2:ident,
        $a3:ident,
        $a4:ident
        $f:ident($n1:ident, $n2:ident, $n3:ident)
    ) => {{
        assert_eq!(
            $a4,
            0,
            "Usercall {}: expected {} argument to be 0",
            stringify!($f),
            "4th"
        );
        H::$f(
            $h,
            RegisterArgument::from_register($a1),
            RegisterArgument::from_register($a2),
            RegisterArgument::from_register($a3),
        )
        .await
    }};
    (
        $h:ident,replace_args
        $a1:ident,
        $a2:ident,
        $a3:ident,
        $a4:ident
        $f:ident($n1:ident, $n2:ident)
    ) => {{
        assert_eq!(
            $a3,
            0,
            "Usercall {}: expected {} argument to be 0",
            stringify!($f),
            "3rd"
        );
        assert_eq!(
            $a4,
            0,
            "Usercall {}: expected {} argument to be 0",
            stringify!($f),
            "4th"
        );
        H::$f(
            $h,
            RegisterArgument::from_register($a1),
            RegisterArgument::from_register($a2),
        )
        .await
    }};
    ($h:ident,replace_args $a1:ident, $a2:ident, $a3:ident, $a4:ident $f:ident($n1:ident)) => {{
        assert_eq!(
            $a2,
            0,
            "Usercall {}: expected {} argument to be 0",
            stringify!($f),
            "2nd"
        );
        assert_eq!(
            $a3,
            0,
            "Usercall {}: expected {} argument to be 0",
            stringify!($f),
            "3rd"
        );
        assert_eq!(
            $a4,
            0,
            "Usercall {}: expected {} argument to be 0",
            stringify!($f),
            "4th"
        );
        H::$f($h, RegisterArgument::from_register($a1)).await
    }};
    ($h:ident,replace_args $a1:ident, $a2:ident, $a3:ident, $a4:ident $f:ident()) => {{
        assert_eq!(
            $a1,
            0,
            "Usercall {}: expected {} argument to be 0",
            stringify!($f),
            "1st"
        );
        assert_eq!(
            $a2,
            0,
            "Usercall {}: expected {} argument to be 0",
            stringify!($f),
            "2nd"
        );
        assert_eq!(
            $a3,
            0,
            "Usercall {}: expected {} argument to be 0",
            stringify!($f),
            "3rd"
        );
        assert_eq!(
            $a4,
            0,
            "Usercall {}: expected {} argument to be 0",
            stringify!($f),
            "4th"
        );
        H::$f($h).await
    }};
}

invoke_with_usercalls!(define_usercalls);