Skip to content

Commit 130bb27

Browse files
committed
fix(virtq): convert debug asserts into errors
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
1 parent 75b263c commit 130bb27

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

  • src/hyperlight_common/src/virtq

src/hyperlight_common/src/virtq/ring.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ impl RingCursor {
396396
/// Advance to next position, wrapping around and toggling wrap counter if needed
397397
#[inline]
398398
fn advance(&mut self) {
399+
debug_assert!(self.head.checked_add(1).is_some());
399400
self.head += 1;
400401
if self.head >= self.size {
401402
self.head = 0;
@@ -406,6 +407,7 @@ impl RingCursor {
406407
/// Advance by n positions using modular arithmetic.
407408
#[inline]
408409
fn advance_by(&mut self, n: u16) {
410+
debug_assert!(self.head.checked_add(n).is_some());
409411
let new = self.head + n;
410412
let wraps = new / self.size;
411413
self.head = new % self.size;
@@ -1078,6 +1080,12 @@ impl<M: MemOps> RingConsumer<M> {
10781080
return Err(RingError::BadChain);
10791081
}
10801082

1083+
// Check if next inflight will exceed ring capacity - this should never happen if driver is
1084+
// well-behaved and we correctly track inflight count.
1085+
if self.num_inflight + chain_len as usize > self.desc_table.len() {
1086+
return Err(RingError::InvalidState);
1087+
}
1088+
10811089
let readables = elements.len() - writables;
10821090

10831091
// Since driver wrote the same id everywhere, head_desc.id is valid.
@@ -1097,8 +1105,6 @@ impl<M: MemOps> RingConsumer<M> {
10971105
// Update inflight count
10981106
self.num_inflight += chain_len as usize;
10991107

1100-
debug_assert!(self.num_inflight <= self.desc_table.len());
1101-
11021108
Ok((
11031109
id,
11041110
BufferChain {
@@ -1126,7 +1132,7 @@ impl<M: MemOps> RingConsumer<M> {
11261132
.get(id as usize)
11271133
.ok_or(RingError::InvalidState)?;
11281134

1129-
if chain_len == 0 {
1135+
if chain_len == 0 || chain_len > self.desc_table.len() as u16 {
11301136
return Err(RingError::InvalidState);
11311137
}
11321138

@@ -1152,8 +1158,6 @@ impl<M: MemOps> RingConsumer<M> {
11521158
self.id_num[id as usize] = 0;
11531159

11541160
self.num_inflight -= chain_len as usize;
1155-
debug_assert!(self.num_inflight <= self.desc_table.len());
1156-
11571161
Ok(())
11581162
}
11591163

0 commit comments

Comments
 (0)