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
// SPDX-License-Identifier: Apache-2.0

use super::super::types::Argv;
use super::Alloc;
use crate::guest::alloc::{Allocator, Collector};
use crate::libc::{
    SYS_close, SYS_dup, SYS_dup2, SYS_dup3, SYS_epoll_create1, SYS_eventfd2, SYS_exit,
    SYS_exit_group, SYS_listen, SYS_socket, SYS_sync,
};
use crate::Result;

use core::ffi::{c_int, c_long};

/// Trait implemented by allocatable syscalls, which are passed through directly to the host and do
/// not require custom handling logic.
///
/// # Safety
///
/// This trait is unsafe, because it allows execution arbitrary syscalls on the host, which is
/// intrinsically unsafe.
///
/// # Example
/// ```rust
/// use sallyport::guest::call::types::Argv;
/// use sallyport::guest::syscall::PassthroughAlloc;
/// use sallyport::Result;
/// #
/// # use sallyport::libc;
/// # use core::ffi::{c_int, c_long};
///
/// pub struct Exit {
///     pub status: c_int,
/// }
///
/// unsafe impl PassthroughAlloc for Exit {
///     const NUM: c_long = libc::SYS_exit;
///
///     type Argv = Argv<1>;
///     type Ret = ();
///
///     fn stage(self) -> Self::Argv {
///         Argv([self.status as _])
///     }
/// }
/// ```
pub unsafe trait PassthroughAlloc {
    /// Syscall number.
    ///
    /// For example, [`libc::SYS_exit`].
    const NUM: c_long;

    /// The syscall argument vector.
    ///
    /// For example, [`call::types::Argv<1>`](crate::guest::call::types::Argv<1>).
    type Argv: Into<[usize; 6]>;

    /// Syscall return value.
    ///
    /// For example, `()`.
    type Ret;

    /// Returns argument vector registers.
    fn stage(self) -> Self::Argv;
}

unsafe impl<'a, T: PassthroughAlloc> Alloc<'a> for T {
    const NUM: c_long = T::NUM;

    type Argv = T::Argv;
    type Ret = T::Ret;

    type Staged = ();
    type Committed = ();
    type Collected = Result<T::Ret>;

    fn stage(self, _: &mut impl Allocator) -> Result<(Self::Argv, Self::Staged)> {
        Ok((T::stage(self), ()))
    }

    fn collect(_: Self::Committed, ret: Result<Self::Ret>, _: &impl Collector) -> Self::Collected {
        ret
    }
}

pub struct Close {
    pub fd: c_int,
}

unsafe impl PassthroughAlloc for Close {
    const NUM: c_long = SYS_close;

    type Argv = Argv<1>;
    type Ret = ();

    fn stage(self) -> Self::Argv {
        Argv([self.fd as _])
    }
}

pub struct Dup {
    pub oldfd: c_int,
}

unsafe impl PassthroughAlloc for Dup {
    const NUM: c_long = SYS_dup;

    type Argv = Argv<1>;
    type Ret = ();

    fn stage(self) -> Self::Argv {
        Argv([self.oldfd as _])
    }
}

pub struct Dup2 {
    pub oldfd: c_int,
    pub newfd: c_int,
}

unsafe impl PassthroughAlloc for Dup2 {
    const NUM: c_long = SYS_dup2;

    type Argv = Argv<2>;
    type Ret = ();

    fn stage(self) -> Self::Argv {
        Argv([self.oldfd as _, self.newfd as _])
    }
}

pub struct Dup3 {
    pub oldfd: c_int,
    pub newfd: c_int,
    pub flags: c_int,
}

unsafe impl PassthroughAlloc for Dup3 {
    const NUM: c_long = SYS_dup3;

    type Argv = Argv<3>;
    type Ret = ();

    fn stage(self) -> Self::Argv {
        Argv([self.oldfd as _, self.newfd as _, self.flags as _])
    }
}

pub struct EpollCreate1 {
    pub flags: c_int,
}

unsafe impl PassthroughAlloc for EpollCreate1 {
    const NUM: c_long = SYS_epoll_create1;

    type Argv = Argv<1>;
    type Ret = c_int;

    fn stage(self) -> Self::Argv {
        Argv([self.flags as _])
    }
}

pub struct Eventfd2 {
    pub initval: c_int,
    pub flags: c_int,
}

unsafe impl PassthroughAlloc for Eventfd2 {
    const NUM: c_long = SYS_eventfd2;

    type Argv = Argv<2>;
    type Ret = c_int;

    fn stage(self) -> Self::Argv {
        Argv([self.initval as _, self.flags as _])
    }
}

pub struct Exit {
    pub status: c_int,
}

unsafe impl PassthroughAlloc for Exit {
    const NUM: c_long = SYS_exit;

    type Argv = Argv<1>;
    type Ret = ();

    fn stage(self) -> Self::Argv {
        Argv([self.status as _])
    }
}

pub struct ExitGroup {
    pub status: c_int,
}

unsafe impl PassthroughAlloc for ExitGroup {
    const NUM: c_long = SYS_exit_group;

    type Argv = Argv<1>;
    type Ret = ();

    fn stage(self) -> Self::Argv {
        Argv([self.status as _])
    }
}

pub struct Listen {
    pub sockfd: c_int,
    pub backlog: c_int,
}

unsafe impl PassthroughAlloc for Listen {
    const NUM: c_long = SYS_listen;

    type Argv = Argv<2>;
    type Ret = ();

    fn stage(self) -> Self::Argv {
        Argv([self.sockfd as _, self.backlog as _])
    }
}

pub struct Socket {
    pub domain: c_int,
    pub typ: c_int,
    pub protocol: c_int,
}

unsafe impl PassthroughAlloc for Socket {
    const NUM: c_long = SYS_socket;

    type Argv = Argv<3>;
    type Ret = c_int;

    fn stage(self) -> Self::Argv {
        Argv([self.domain as _, self.typ as _, self.protocol as _])
    }
}

pub struct Sync;

unsafe impl PassthroughAlloc for Sync {
    const NUM: c_long = SYS_sync;

    type Argv = Argv<0>;
    type Ret = ();

    fn stage(self) -> Self::Argv {
        Argv([])
    }
}