Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update syscall handlers to take explicit argument types #2664

Merged
merged 5 commits into from
Jan 26, 2023

Conversation

stevenengler
Copy link
Contributor

@stevenengler stevenengler commented Jan 13, 2023

Currently our syscall handlers are written as:

pub fn dup3(&self, ctx: &mut ThreadContext, args: &SysCallArgs) -> SyscallResult {
    let old_fd = libc::c_int::from(args.get(0));
    let new_fd = libc::c_int::from(args.get(1));
    let flags = libc::c_int::from(args.get(2));
    [...]
}

This has the downside that we need to convert the SysCallReg values to the correct type. And if we don't do so explicitly like in the example above, Rust may use type inference and convert it to the wrong type (see discussion in #2658). It would be nice if we could force syscalls to provide the types explicitly in the function arguments:

pub fn dup3(
    ctx: &mut SyscallContext,
    old_fd: libc::c_int,
    new_fd: libc::c_int,
    flags: libc::c_int,
) -> SyscallResult {
    [...]
}

With these explicit types in the function arguments it's still possible to provide an incorrect type, but would prevent type-inference mistakes and hopefully would make the syscall handlers easier to check.

@stevenengler stevenengler self-assigned this Jan 13, 2023
@github-actions github-actions bot added the Component: Main Composing the core Shadow executable label Jan 13, 2023
@codecov
Copy link

codecov bot commented Jan 13, 2023

Codecov Report

Base: 68.18% // Head: 68.06% // Decreases project coverage by -0.12% ⚠️

Coverage data is based on head (10c97fc) compared to base (c8d6c68).
Patch coverage: 86.64% of modified lines in pull request are covered.

❗ Current head 10c97fc differs from pull request most recent head 52f016c. Consider uploading reports for the commit 52f016c to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2664      +/-   ##
==========================================
- Coverage   68.18%   68.06%   -0.12%     
==========================================
  Files         202      202              
  Lines       30401    30407       +6     
  Branches     5935     5938       +3     
==========================================
- Hits        20730    20698      -32     
- Misses       4977     5012      +35     
- Partials     4694     4697       +3     
Flag Coverage Δ
tests 68.06% <86.64%> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/main/host/descriptor/descriptor_table.rs 63.71% <0.00%> (-7.72%) ⬇️
src/main/host/syscall/handler/eventfd.rs 68.57% <50.00%> (+0.15%) ⬆️
src/main/host/syscall/handler/fcntl.rs 59.15% <50.00%> (-5.71%) ⬇️
src/main/host/syscall/handler/sysinfo.rs 91.30% <66.66%> (ø)
src/main/host/syscall/handler/sched.rs 45.31% <78.94%> (+3.52%) ⬆️
src/main/host/syscall/handler/unistd.rs 70.21% <82.60%> (-2.34%) ⬇️
src/main/host/syscall/handler/ioctl.rs 55.88% <85.71%> (-1.27%) ⬇️
src/main/host/syscall/handler/time.rs 68.18% <86.66%> (+0.73%) ⬆️
src/main/host/syscall/handler/mod.rs 83.75% <92.39%> (+3.40%) ⬆️
src/main/host/syscall/handler/socket.rs 67.10% <96.66%> (-1.88%) ⬇️
... and 20 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@github-actions github-actions bot added the Component: Libraries Support functions like LD_PRELOAD and logging label Jan 25, 2023
@stevenengler stevenengler marked this pull request as ready for review January 26, 2023 00:06
Copy link
Contributor

@sporksmith sporksmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

src/main/host/syscall/handler/file.rs Show resolved Hide resolved
This allows us to write the syscall handlers to take explicit argument types.
For example the previous

```rust
pub fn dup3(ctx: &mut SyscallContext, args: &SysCallArgs) -> SyscallResult {
    let old_fd = libc::c_int::from(args.get(0));
    let new_fd = libc::c_int::from(args.get(1));
    let flags = libc::c_int::from(args.get(2));
    [...]
}
```

becomes

```rust
pub fn dup3(
    ctx: &mut SyscallContext,
    old_fd: libc::c_int,
    new_fd: libc::c_int,
    flags: libc::c_int,
) -> SyscallResult {
    [...]
}
```
@stevenengler stevenengler merged commit abeb80e into shadow:main Jan 26, 2023
@stevenengler stevenengler deleted the syscall-handler-extractors branch January 26, 2023 18:33
stevenengler added a commit that referenced this pull request Feb 23, 2023
Similar to #2664 but for return types.

Syscalls can return various types (`int`, `size_t`, pointers, etc). We
probably don't want to return the wrong type, which the plugin may
interpret incorrectly. For example if a syscall is supposed to return an
`int`, we probably don't want to return a `u64::MAX` from our syscall
handler.

Currently our syscall handlers return a `SysCallReg`, which is a 64-bit
union of various types, and there is no type checking to make sure our
syscall handler is returning a valid type. This PR allows our syscall
handlers to specify an explicit return type (ex: `libc::c_int` instead
of `SysCallReg`).

Only the "sched.h" syscall handlers have been updated to use this. I
didn't bother with the others. We can update them later if we want to,
but for now this change allows us to use this for new syscall handlers
that we write in the future.

(Related #2658.)

Before:

```rust
pub fn sched_yield(_ctx: &mut SyscallContext) -> SyscallResult {
    Ok(0.into())
}
```

After:

```rust
pub fn sched_yield(_ctx: &mut SyscallContext) -> Result<libc::c_int, SyscallError> {
    Ok(0)
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Libraries Support functions like LD_PRELOAD and logging Component: Main Composing the core Shadow executable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants