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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use crate::{IntPtr, IntRet, OcallError, Result, VmMemory};
use log::Level;

const OCALL_N_ARGS: usize = 4;

pub(crate) struct StackedArgs<Args> {
    args: Args,
}

impl StackedArgs<()> {
    pub(crate) const fn empty() -> Self {
        StackedArgs { args: () }
    }
}

impl<A: Nargs> StackedArgs<A> {
    pub(crate) fn load(mut raw: &[IntPtr]) -> Option<Self> {
        Some(check_args_length(StackedArgs {
            args: Nargs::load(&mut raw)?,
        }))
    }

    pub(crate) fn dump(self) -> [IntPtr; OCALL_N_ARGS] {
        let mut ret = [Default::default(); OCALL_N_ARGS];
        let data = check_args_length(self).args.dump();
        ret[..data.len()].copy_from_slice(&data);
        ret
    }
}

impl<A, B> StackedArgs<(A, B)> {
    fn pop(self) -> (A, StackedArgs<B>) {
        let (a, args) = self.args;
        (a, StackedArgs { args })
    }
}

impl<B> StackedArgs<B> {
    fn push<A>(self, arg: A) -> StackedArgs<(A, B)> {
        StackedArgs {
            args: (arg, self.args),
        }
    }
}

pub(crate) trait Nargs {
    const N_ARGS: usize;
    fn load(buf: &mut &[IntPtr]) -> Option<Self>
    where
        Self: Sized;

    // Since #![feature(generic_const_exprs)] is not yet stable, we use OCALL_N_ARGS instead of
    // Self::N_ARGS
    fn dump(self) -> [IntPtr; OCALL_N_ARGS];
}

impl Nargs for () {
    const N_ARGS: usize = 0;
    fn load(_buf: &mut &[IntPtr]) -> Option<Self> {
        Some(())
    }
    fn dump(self) -> [IntPtr; OCALL_N_ARGS] {
        Default::default()
    }
}

impl Nargs for IntPtr {
    const N_ARGS: usize = 1;
    fn load(buf: &mut &[IntPtr]) -> Option<Self> {
        let me = *buf.first()?;
        *buf = &buf[1..];
        Some(me)
    }

    fn dump(self) -> [IntPtr; OCALL_N_ARGS] {
        let mut ret = [0; OCALL_N_ARGS];
        ret[0] = self;
        ret
    }
}

impl<A, B> Nargs for (A, B)
where
    A: Nargs,
    B: Nargs,
{
    const N_ARGS: usize = A::N_ARGS + B::N_ARGS;

    fn load(buf: &mut &[IntPtr]) -> Option<Self> {
        let b = B::load(buf)?;
        let a = A::load(buf)?;
        Some((a, b))
    }

    fn dump(self) -> [IntPtr; OCALL_N_ARGS] {
        let (a, b) = self;
        let mut buf = [IntPtr::default(); OCALL_N_ARGS];
        buf[0..B::N_ARGS].copy_from_slice(&b.dump()[0..B::N_ARGS]);
        buf[B::N_ARGS..Self::N_ARGS].copy_from_slice(&a.dump()[..A::N_ARGS]);
        buf
    }
}

// Since the const evaluation of Rust is not powerful enough yet, we use this trick to statically
// check the argument types encode output do not exceed the maximum number of arguments.
pub(crate) trait NotTooManyArgs {
    const TOO_MANY_ARGUMENTS: ();
}
impl<T: Nargs> NotTooManyArgs for T {
    const TOO_MANY_ARGUMENTS: () = [()][(Self::N_ARGS > OCALL_N_ARGS) as usize];
}

pub(crate) fn check_args_length<T: Nargs + NotTooManyArgs>(v: StackedArgs<T>) -> StackedArgs<T> {
    #[allow(clippy::let_unit_value)]
    let _ = T::TOO_MANY_ARGUMENTS;
    v
}

pub(crate) trait I32Convertible {
    fn to_i32(&self) -> i32;
    fn from_i32(i: i32) -> Result<Self>
    where
        Self: Sized;
}

pub(crate) trait ArgEncode {
    type Encoded;

    fn encode_arg<A>(self, stack: StackedArgs<A>) -> StackedArgs<(Self::Encoded, A)>;
}

pub(crate) trait ArgDecode<'a> {
    type Encoded;
    fn decode_arg<R>(
        stack: StackedArgs<(Self::Encoded, R)>,
        vm: &'a impl VmMemory,
    ) -> Result<(Self, StackedArgs<R>)>
    where
        Self: Sized;
}

/// Trait for types that can be encoded to a return value of a ocall.
pub trait RetEncode {
    /// Encode the ocall return value into a IntRet
    fn encode_ret(self) -> IntRet;
}

pub(crate) trait RetDecode {
    fn decode_ret(encoded: IntRet) -> Self
    where
        Self: Sized;
}

impl ArgEncode for &[u8] {
    type Encoded = (IntPtr, IntPtr);

    fn encode_arg<A>(self, stack: StackedArgs<A>) -> StackedArgs<(Self::Encoded, A)> {
        let ptr = self.as_ptr() as IntPtr;
        let len = self.len() as IntPtr;
        stack.push((len, ptr))
    }
}

impl<'a> ArgDecode<'a> for &'a [u8] {
    type Encoded = (IntPtr, IntPtr);

    fn decode_arg<A>(
        stack: StackedArgs<(Self::Encoded, A)>,
        vm: &'a impl VmMemory,
    ) -> Result<(Self, StackedArgs<A>)>
    where
        Self: Sized,
    {
        let ((len, ptr), stack) = stack.pop();
        let bytes = vm.slice_from_vm(ptr, len)?;
        Ok((bytes, stack))
    }
}

impl ArgEncode for &str {
    type Encoded = (IntPtr, IntPtr);

    fn encode_arg<A>(self, stack: StackedArgs<A>) -> StackedArgs<(Self::Encoded, A)> {
        let bytes = self.as_bytes();
        bytes.encode_arg(stack)
    }
}

impl<'a> ArgDecode<'a> for &'a str {
    type Encoded = (IntPtr, IntPtr);

    fn decode_arg<A>(
        stack: StackedArgs<(Self::Encoded, A)>,
        vm: &'a impl VmMemory,
    ) -> Result<(Self, StackedArgs<A>)>
    where
        Self: Sized,
    {
        let (bytes, stack): (&[u8], _) = ArgDecode::decode_arg(stack, vm)?;
        Ok((
            core::str::from_utf8(bytes).or(Err(OcallError::InvalidEncoding))?,
            stack,
        ))
    }
}

impl ArgEncode for &mut [u8] {
    type Encoded = (IntPtr, IntPtr);

    fn encode_arg<A>(self, stack: StackedArgs<A>) -> StackedArgs<(Self::Encoded, A)> {
        let ptr = self.as_mut_ptr() as IntPtr;
        let len = self.len() as IntPtr;
        stack.push((len, ptr))
    }
}

impl<'a> ArgDecode<'a> for &'a mut [u8] {
    type Encoded = (IntPtr, IntPtr);

    fn decode_arg<A>(
        stack: StackedArgs<(Self::Encoded, A)>,
        vm: &'a impl VmMemory,
    ) -> Result<(Self, StackedArgs<A>)>
    where
        Self: Sized,
    {
        let ((len, ptr), stack) = stack.pop();
        let bytes = vm.slice_from_vm_mut(ptr, len)?;
        Ok((bytes, stack))
    }
}

impl<B> StackedArgs<B> {
    pub(crate) fn push_arg<Arg: ArgEncode>(self, v: Arg) -> StackedArgs<(Arg::Encoded, B)> {
        v.encode_arg(self)
    }
}

impl<A, B> StackedArgs<(A, B)> {
    pub(crate) fn pop_arg<'a, Arg: ArgDecode<'a, Encoded = A>>(
        self,
        vm: &'a impl VmMemory,
    ) -> Result<(Arg, StackedArgs<B>)> {
        Arg::decode_arg(self, vm)
    }
}

macro_rules! impl_codec_i {
    ($typ: ty) => {
        impl I32Convertible for $typ {
            fn to_i32(&self) -> i32 {
                *self as i32
            }
            fn from_i32(i: i32) -> Result<Self> {
                if i > <$typ>::MAX as i32 || i < (-<$typ>::MAX - 1) as i32 {
                    Err(OcallError::InvalidEncoding)
                } else {
                    Ok(i as Self)
                }
            }
        }
    };
}
impl_codec_i!(i8);
impl_codec_i!(i16);

macro_rules! impl_codec_u {
    ($typ: ty) => {
        impl I32Convertible for $typ {
            fn to_i32(&self) -> i32 {
                *self as i32
            }
            fn from_i32(i: i32) -> Result<Self> {
                if i as u32 > <$typ>::MAX as u32 {
                    Err(OcallError::InvalidEncoding)
                } else {
                    Ok(i as Self)
                }
            }
        }
    };
}
impl_codec_u!(u8);
impl_codec_u!(u16);

macro_rules! impl_codec {
    ($typ: ty) => {
        impl I32Convertible for $typ {
            fn to_i32(&self) -> i32 {
                *self as i32
            }
            fn from_i32(i: i32) -> Result<Self> {
                Ok(i as Self)
            }
        }
    };
}
impl_codec!(i32);
impl_codec!(u32);

macro_rules! impl_codec64 {
    ($typ: ty) => {
        impl ArgEncode for $typ {
            type Encoded = (IntPtr, IntPtr);

            fn encode_arg<R>(self, stack: StackedArgs<R>) -> StackedArgs<(Self::Encoded, R)> {
                let low = (self & 0xffffffff) as IntPtr;
                let high = ((self >> 32) & 0xffffffff) as IntPtr;
                stack.push((low, high))
            }
        }

        impl<'a> ArgDecode<'a> for $typ {
            type Encoded = (IntPtr, IntPtr);

            fn decode_arg<R>(
                stack: StackedArgs<(Self::Encoded, R)>,
                _vm: &'a impl VmMemory,
            ) -> Result<(Self, StackedArgs<R>)>
            where
                Self: Sized,
            {
                let ((low, high), stack) = stack.pop();
                let high = ((high as Self) << 32);
                let v = high & (low as Self);
                Ok((v, stack))
            }
        }
    };
}

impl_codec64!(i64);
impl_codec64!(u64);

impl<I: I32Convertible> ArgEncode for I {
    type Encoded = IntPtr;

    fn encode_arg<R>(self, stack: StackedArgs<R>) -> StackedArgs<(Self::Encoded, R)> {
        stack.push(self.to_i32() as _)
    }
}

impl<'a, I: I32Convertible> ArgDecode<'a> for I {
    type Encoded = IntPtr;

    fn decode_arg<R>(
        stack: StackedArgs<(Self::Encoded, R)>,
        _vm: &'a impl VmMemory,
    ) -> Result<(Self, StackedArgs<R>)>
    where
        Self: Sized,
    {
        let (v, stack) = stack.pop();
        Ok((I::from_i32(v as _)?, stack))
    }
}

impl I32Convertible for bool {
    fn to_i32(&self) -> i32 {
        *self as i32
    }
    fn from_i32(i: i32) -> Result<Self> {
        match i {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(OcallError::InvalidEncoding),
        }
    }
}

impl I32Convertible for OcallError {
    fn to_i32(&self) -> i32 {
        *self as u8 as i32
    }
    fn from_i32(i: i32) -> Result<Self> {
        let code = u8::from_i32(i)?;
        OcallError::try_from(code).or(Err(OcallError::InvalidEncoding))
    }
}

impl I32Convertible for () {
    fn to_i32(&self) -> i32 {
        0
    }
    fn from_i32(i: i32) -> Result<()> {
        if i == 0 {
            Ok(())
        } else {
            Err(OcallError::InvalidEncoding)
        }
    }
}

impl I32Convertible for Level {
    fn to_i32(&self) -> i32 {
        match self {
            Level::Error => 1,
            Level::Warn => 2,
            Level::Info => 3,
            Level::Debug => 4,
            Level::Trace => 5,
        }
    }

    fn from_i32(i: i32) -> Result<Self> {
        match i {
            1 => Ok(Level::Error),
            2 => Ok(Level::Warn),
            3 => Ok(Level::Info),
            4 => Ok(Level::Debug),
            5 => Ok(Level::Trace),
            _ => Err(OcallError::InvalidEncoding),
        }
    }
}

impl<A, B> RetEncode for Result<A, B>
where
    A: I32Convertible,
    B: I32Convertible,
{
    fn encode_ret(self) -> IntRet {
        let (tp, val) = match self {
            Ok(v) => (0, v.to_i32()),
            Err(err) => (1, err.to_i32()),
        };
        ((tp as u32 as i64) << 32) | (val as u32 as i64)
    }
}

impl<A, B> RetDecode for Result<A, B>
where
    A: I32Convertible,
    B: I32Convertible,
{
    fn decode_ret(encoded: IntRet) -> Self {
        let tp = ((encoded >> 32) & 0xffffffff) as i32;
        let val = (encoded & 0xffffffff) as i32;
        if tp == 0 {
            Ok(A::from_i32(val).expect("Invalid ocall return"))
        } else {
            Err(B::from_i32(val).expect("Invalid ocall return"))
        }
    }
}