shadow_shim/
syscall.rs

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
use core::fmt::Write;
use core::sync::atomic;

use formatting_nostd::{BorrowedFdWriter, FormatBuffer};
use linux_api::errno::Errno;
use linux_api::ucontext::ucontext;
use rustix::fd::BorrowedFd;
use shadow_shim_helper_rs::emulated_time::EmulatedTime;
use shadow_shim_helper_rs::option::FfiOption;
use shadow_shim_helper_rs::shim_event::{
    ShimEventAddThreadRes, ShimEventSyscall, ShimEventSyscallComplete, ShimEventToShadow,
    ShimEventToShim,
};
use shadow_shim_helper_rs::syscall_types::{SyscallArgs, SyscallReg};
use shadow_shim_helper_rs::util::time::TimeParts;

use crate::{bindings, global_host_shmem, tls_ipc, tls_thread_shmem};

/// # Safety
///
/// The specified syscall must be safe to make.
unsafe fn native_syscall(args: &SyscallArgs) -> SyscallReg {
    if args.number == libc::SYS_clone {
        panic!("Shouldn't get here. Should have gone through ShimEventAddThreadReq");
    } else if args.number == libc::SYS_exit {
        let exit_status = i32::from(args.args[0]);
        // This thread is exiting. Arrange for its thread-local-storage and
        // signal stack to be freed.
        unsafe { bindings::shim_freeSignalStack() };
        // SAFETY: We don't try to recover from panics.
        // TODO: make shim fully no_std and install a panic handler that aborts.
        // https://doc.rust-lang.org/nomicon/panic-handler.html
        unsafe { crate::release_and_exit_current_thread(exit_status) };
    } else {
        let rv: i64;
        // SAFETY: Caller is responsible for ensuring this syscall is safe to make.
        unsafe {
            core::arch::asm!(
                "syscall",
                inout("rax") args.number => rv,
                in("rdi") u64::from(args.args[0]),
                in("rsi") u64::from(args.args[1]),
                in("rdx") u64::from(args.args[2]),
                in("r10") u64::from(args.args[3]),
                in("r8") u64::from(args.args[4]),
                in("r9") u64::from(args.args[5]))
        };
        return rv.into();
    }
}

/// # Safety
///
/// `ctx` must be valid if provided.
unsafe fn emulated_syscall_event(
    mut ctx: Option<&mut ucontext>,
    syscall_event: &ShimEventSyscall,
) -> SyscallReg {
    log::trace!(
        "sending syscall {} event",
        syscall_event.syscall_args.number
    );

    crate::tls_ipc::with(|ipc| {
        ipc.to_shadow()
            .send(ShimEventToShadow::Syscall(*syscall_event))
    });

    loop {
        log::trace!("waiting for event");
        let res = crate::tls_ipc::with(|ipc| ipc.from_shadow().receive().unwrap());
        log::trace!("got response {res:?}");

        match res {
            ShimEventToShim::SyscallComplete(syscall_complete) => {
                // Shadow has returned a result for the emulated syscall

                if crate::global_host_shmem::try_get().is_none() {
                    // We should only get here during early initialization. We don't have what
                    // we need to process signals yet, so just return the result.
                    return syscall_complete.retval;
                }

                if let Some(ctx) = ctx.as_mut() {
                    // Set the syscall return value now, before potentially
                    // invoking signal handlers. This appears to be the behavior
                    // in the kernel; i.e. a handler for a signal that
                    // is interrupted a blocking syscall should see the syscall
                    // result (-EINTR) in the context passed to that handler.
                    ctx.uc_mcontext.rax = syscall_complete.retval.into();
                }

                // SAFETY: `ctx` should be valid if present.
                let all_sigactions_had_sa_restart =
                    unsafe { crate::signals::process_signals(ctx.as_deref_mut()) };

                if i64::from(syscall_complete.retval) == Errno::EINTR.to_negated_i64()
                    && all_sigactions_had_sa_restart
                    && syscall_complete.restartable
                {
                    // Restart syscall interrupted syscall
                    crate::tls_ipc::with(|ipc| {
                        ipc.to_shadow()
                            .send(ShimEventToShadow::Syscall(*syscall_event))
                    });
                    continue;
                } else {
                    // Return syscall result
                    return syscall_complete.retval;
                }
            }
            ShimEventToShim::SyscallDoNative => {
                // "Emulate" the syscall by executing it natively.

                let rv = unsafe { native_syscall(&syscall_event.syscall_args) };

                if let FfiOption::Some(strace_fd) =
                    crate::tls_process_shmem::with(|process| process.strace_fd)
                {
                    let emulated_time = global_host_shmem::get()
                        .sim_time
                        .load(atomic::Ordering::Relaxed)
                        - EmulatedTime::SIMULATION_START;
                    let tid = tls_thread_shmem::with(|thread| thread.tid);
                    let parts = TimeParts::from_nanos(emulated_time.as_nanos());
                    let mut buffer = FormatBuffer::<200>::new();
                    writeln!(
                        &mut buffer,
                        "{} [tid {}] ^^^ = {:?}",
                        parts.fmt_hr_min_sec_nano(),
                        tid,
                        rv
                    )
                    .unwrap();
                    // SAFETY: file descriptor should be valid and open.
                    let strace_fd = unsafe { BorrowedFd::borrow_raw(strace_fd) };
                    let mut strace_file_writer = BorrowedFdWriter::new(strace_fd);
                    if let Err(e) = strace_file_writer.write_str(buffer.as_str()) {
                        log::warn!("Couldn't write to strace_fd:{strace_fd:?}: {e:?}");
                    }
                }

                return rv;
            }
            ShimEventToShim::Syscall(syscall) => {
                // Execute the syscall and return the result to Shadow.

                let res = unsafe { native_syscall(&syscall.syscall_args) };
                tls_ipc::with(|ipc| {
                    ipc.to_shadow().send(ShimEventToShadow::SyscallComplete(
                        ShimEventSyscallComplete {
                            retval: res,
                            restartable: false,
                        },
                    ))
                });
            }
            ShimEventToShim::AddThreadReq(r) => {
                // Create a new native thread under our control

                let clone_res = unsafe { crate::clone::do_clone(ctx.as_mut().unwrap(), &r) };
                tls_ipc::with(|ipc| {
                    ipc.to_shadow()
                        .send(ShimEventToShadow::AddThreadRes(ShimEventAddThreadRes {
                            clone_res,
                        }))
                })
            }
            e @ ShimEventToShim::StartRes => {
                panic!("Unexpected event: {e:?}");
            }
        }
    }
}

pub mod export {
    use super::*;

    /// # Safety
    ///
    /// `ctx` must be valid if provided.
    #[no_mangle]
    pub unsafe extern "C-unwind" fn shim_emulated_syscallv(
        ctx: *mut libc::ucontext_t,
        n: core::ffi::c_long,
        mut args: va_list::VaList,
    ) -> core::ffi::c_long {
        let old_native_syscall_flag = crate::tls_allow_native_syscalls::swap(true);

        let syscall_args = SyscallArgs {
            number: n,
            args: core::array::from_fn(|_| {
                // SAFETY: syscall args all "fit" in an i64. Reading more arguments
                // than actually provided is sound because any bit pattern is a
                // valid i64.
                let arg = unsafe { args.get::<i64>() };
                SyscallReg::from(arg)
            }),
        };

        let event = ShimEventSyscall { syscall_args };

        let ctx = ctx.cast::<ucontext>();
        let ctx = unsafe { ctx.as_mut() };
        let retval = unsafe { emulated_syscall_event(ctx, &event) };

        crate::tls_allow_native_syscalls::swap(old_native_syscall_flag);

        retval.into()
    }

    /// # Safety
    ///
    /// The specified syscall must be safe to make.
    #[no_mangle]
    pub unsafe extern "C-unwind" fn shim_native_syscallv(
        n: core::ffi::c_long,
        mut args: va_list::VaList,
    ) -> core::ffi::c_long {
        let syscall_args = SyscallArgs {
            number: n,
            args: core::array::from_fn(|_| {
                // SAFETY: syscall args all "fit" in an i64. Reading more arguments
                // than actually provided is sound because any bit pattern is a
                // valid i64.
                let arg = unsafe { args.get::<i64>() };
                SyscallReg::from(arg)
            }),
        };
        // SAFETY: Ensured by caller.
        let rv = unsafe { native_syscall(&syscall_args) };
        rv.into()
    }
}