Skip to content

Commit 9287177

Browse files
committed
chore: address checkbuild
- add missing safety blocks - add missing lisence for the seqpacket c server - collapse if statement in vsock.rs Signed-off-by: aerosouund <aerosound161@gmail.com>
1 parent 419bc2e commit 9287177

4 files changed

Lines changed: 36 additions & 25 deletions

File tree

src/vmm/src/devices/virtio/vsock/csm/connection.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,7 @@ mod tests {
14621462
impl SeqpacketTestStream {
14631463
fn new() -> Self {
14641464
let mut fds = [0i32; 2];
1465+
// SAFETY: valid AF_UNIX socketpair call; fds is a valid 2-element array.
14651466
let ret = unsafe {
14661467
libc::socketpair(
14671468
libc::AF_UNIX,
@@ -1479,19 +1480,21 @@ mod tests {
14791480

14801481
// Write one seqpacket message into the remote end.
14811482
fn push_message(&self, data: &[u8]) {
1483+
// SAFETY: `remote_fd` is valid; `data` is a valid slice for the duration of the call.
14821484
let ret = unsafe {
14831485
libc::write(
14841486
self.remote_fd,
1485-
data.as_ptr() as *const libc::c_void,
1487+
data.as_ptr().cast::<libc::c_void>(),
14861488
data.len(),
14871489
)
14881490
};
1489-
assert_eq!(ret as usize, data.len(), "push_message write failed");
1491+
assert_eq!(ret.cast_unsigned(), data.len(), "push_message write failed");
14901492
}
14911493
}
14921494

14931495
impl Drop for SeqpacketTestStream {
14941496
fn drop(&mut self) {
1497+
// SAFETY: Both fds are valid and owned by this struct; closing them on drop.
14951498
unsafe {
14961499
libc::close(self.local_fd);
14971500
libc::close(self.remote_fd);
@@ -1511,36 +1514,40 @@ mod tests {
15111514
buf: &mut VolatileSlice<B>,
15121515
) -> Result<usize, VolatileMemoryError> {
15131516
let mut tmp = vec![0u8; buf.len()];
1517+
// SAFETY: `local_fd` is valid; `tmp` is a valid writable buffer for the duration of
1518+
// the call.
15141519
let ret = unsafe {
15151520
libc::recv(
15161521
self.local_fd,
1517-
tmp.as_mut_ptr() as *mut libc::c_void,
1522+
tmp.as_mut_ptr().cast::<libc::c_void>(),
15181523
tmp.len(),
15191524
0,
15201525
)
15211526
};
15221527
if ret < 0 {
15231528
return Err(VolatileMemoryError::IOError(IoError::last_os_error()));
15241529
}
1525-
let n = ret as usize;
1530+
let n = ret.cast_unsigned();
15261531
buf.copy_from(&tmp[..n]);
15271532
Ok(n)
15281533
}
15291534
}
15301535

15311536
impl Write for SeqpacketTestStream {
15321537
fn write(&mut self, data: &[u8]) -> Result<usize, IoError> {
1538+
// SAFETY: `local_fd` is valid; `data` is a valid readable slice for the duration of
1539+
// the call.
15331540
let ret = unsafe {
15341541
libc::write(
15351542
self.local_fd,
1536-
data.as_ptr() as *const libc::c_void,
1543+
data.as_ptr().cast::<libc::c_void>(),
15371544
data.len(),
15381545
)
15391546
};
15401547
if ret < 0 {
15411548
Err(IoError::last_os_error())
15421549
} else {
1543-
Ok(ret as usize)
1550+
Ok(ret.cast_unsigned())
15441551
}
15451552
}
15461553

@@ -1556,17 +1563,19 @@ mod tests {
15561563
) -> Result<usize, VolatileMemoryError> {
15571564
let mut tmp = vec![0u8; buf.len()];
15581565
buf.copy_to(&mut tmp);
1566+
// SAFETY: `local_fd` is valid; `tmp` is a valid readable buffer for the duration of
1567+
// the call.
15591568
let ret = unsafe {
15601569
libc::write(
15611570
self.local_fd,
1562-
tmp.as_ptr() as *const libc::c_void,
1571+
tmp.as_ptr().cast::<libc::c_void>(),
15631572
tmp.len(),
15641573
)
15651574
};
15661575
if ret < 0 {
15671576
Err(VolatileMemoryError::IOError(IoError::last_os_error()))
15681577
} else {
1569-
Ok(ret as usize)
1578+
Ok(ret.cast_unsigned())
15701579
}
15711580
}
15721581
}
@@ -1657,7 +1666,7 @@ mod tests {
16571666
// First call: fills the descriptor (4096 bytes), does not set EOM.
16581667
let res1 = conn.recv_pkt(&mut rx_pkt).unwrap();
16591668
assert_eq!(rx_pkt.hdr.op(), uapi::VSOCK_OP_RW);
1660-
assert_eq!(res1.bytes_read, BUF_SIZE as u32);
1669+
assert_eq!(res1.bytes_read, u32::try_from(BUF_SIZE).unwrap());
16611670
assert!(res1.should_retrigger);
16621671
assert_eq!(rx_pkt.hdr.flags() & VIRTIO_VSOCK_SEQ_EOM, 0);
16631672
// Connection must still have pending RX for the remainder.
@@ -1687,7 +1696,7 @@ mod tests {
16871696
let res = conn.recv_pkt(&mut rx_pkt).unwrap();
16881697

16891698
assert_eq!(rx_pkt.hdr.op(), uapi::VSOCK_OP_RW);
1690-
assert_eq!(res.bytes_read, BUF_SIZE as u32);
1699+
assert_eq!(res.bytes_read, u32::try_from(BUF_SIZE).unwrap());
16911700
assert!(!res.should_retrigger);
16921701
assert_ne!(rx_pkt.hdr.flags() & VIRTIO_VSOCK_SEQ_EOM, 0);
16931702
assert!(!conn.has_pending_rx());
@@ -1704,8 +1713,7 @@ mod tests {
17041713

17051714
let stream = SeqpacketTestStream::new();
17061715
stream.push_message(&vec![0u8; MSG_LEN]);
1707-
let (mut conn, mut rx_pkt, _ctx) =
1708-
make_established_seqpacket(stream, Some(SMALL_BUF));
1716+
let (mut conn, mut rx_pkt, _ctx) = make_established_seqpacket(stream, Some(SMALL_BUF));
17091717

17101718
conn.notify(EventSet::IN);
17111719

src/vmm/src/devices/virtio/vsock/unix/seqpacket.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ impl SeqpacketConn {
2828
}
2929

3030
// Set non-blocking via FIONBIO ioctl (already allowed by seccomp filter)
31-
// SAFETY: Valid fd, errors checked.
3231
let mut nonblocking: libc::c_int = 1;
32+
// SAFETY: `fd` is valid (checked above); `nonblocking` is a valid int pointer.
3333
let ret = unsafe { libc::ioctl(fd, libc::FIONBIO, &mut nonblocking) };
3434
if ret < 0 {
35-
// Close fd before returning error to avoid fd leak
35+
// SAFETY: `fd` is a valid open fd; closing it to avoid a leak.
3636
unsafe { libc::close(fd) };
3737
return Err(io::Error::last_os_error());
3838
}
@@ -41,7 +41,7 @@ impl SeqpacketConn {
4141
unsafe {
4242
if libc::connect(
4343
fd,
44-
&addr as *const libc::sockaddr_un as *const libc::sockaddr,
44+
(&addr as *const libc::sockaddr_un).cast::<libc::sockaddr>(),
4545
addr_len,
4646
) == -1
4747
{
@@ -171,7 +171,7 @@ impl SeqpacketListener {
171171
unsafe {
172172
if libc::bind(
173173
fd,
174-
&addr as *const libc::sockaddr_un as *const libc::sockaddr,
174+
(&addr as *const libc::sockaddr_un).cast::<libc::sockaddr>(),
175175
addr_len,
176176
) == -1
177177
{
@@ -226,11 +226,11 @@ impl Socket for SeqpacketListener {
226226
}
227227

228228
// Set non-blocking via FIONBIO ioctl (already allowed by seccomp filter)
229-
// SAFETY: Valid fd, errors checked.
230229
let mut nonblocking: libc::c_int = 1;
230+
// SAFETY: `fd` is valid (checked above); `nonblocking` is a valid int pointer.
231231
let ret = unsafe { libc::ioctl(fd, libc::FIONBIO, &mut nonblocking) };
232232
if ret < 0 {
233-
// Close fd before returning error to avoid fd leak
233+
// SAFETY: `fd` is a valid open fd; closing it to avoid a leak.
234234
unsafe { libc::close(fd) };
235235
return Err(io::Error::last_os_error());
236236
}

src/vmm/src/vmm_config/vsock.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ where
3838
{
3939
let v = Option::<usize>::deserialize(deserializer)?;
4040

41-
if let Some(n) = v {
42-
if n > MAX_CONN_BUF {
43-
return Err(serde::de::Error::custom(format!(
44-
"conn_buffer_size too large (max {})",
45-
MAX_CONN_BUF
46-
)));
47-
}
41+
if let Some(n) = v
42+
&& n > MAX_CONN_BUF
43+
{
44+
return Err(serde::de::Error::custom(format!(
45+
"conn_buffer_size too large (max {})",
46+
MAX_CONN_BUF
47+
)));
4848
}
4949

5050
Ok(v)

tests/host_tools/vsock_seq_server.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
#include <stdio.h>
25
#include <stdarg.h>
36
#include <stdlib.h>

0 commit comments

Comments
 (0)