Skip to content

Commit da1cc8a

Browse files
ShadowCurseroypat
authored andcommitted
feat: more descriptive panic messages in uffd examples
Add additional info to the panic messages in uffd examples to aid with potential errors. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 525e686 commit da1cc8a

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/firecracker/examples/uffd/uffd_utils.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,30 @@ impl UffdHandler {
6161
let mut message_buf = vec![0u8; 1024];
6262
let (bytes_read, file) = stream
6363
.recv_with_fd(&mut message_buf[..])
64-
.expect("Cannot recv_with_fd");
64+
.expect("Cannot read from a stream");
6565
message_buf.resize(bytes_read, 0);
6666

67-
let body = String::from_utf8(message_buf).unwrap();
68-
let file = file.expect("Uffd not passed through UDS!");
67+
let body = String::from_utf8(message_buf.clone()).unwrap_or_else(|_| {
68+
panic!(
69+
"Received body is not a utf-8 valid string. Raw bytes received: {message_buf:#?}"
70+
)
71+
});
72+
let file =
73+
file.unwrap_or_else(|| panic!("Did not receive Uffd from UDS. Received body: {body}"));
6974

70-
let mappings = serde_json::from_str::<Vec<GuestRegionUffdMapping>>(&body)
71-
.expect("Cannot deserialize memory mappings.");
75+
let mappings =
76+
serde_json::from_str::<Vec<GuestRegionUffdMapping>>(&body).unwrap_or_else(|_| {
77+
panic!("Cannot deserialize memory mappings. Received body: {body}")
78+
});
7279
let memsize: usize = mappings.iter().map(|r| r.size).sum();
7380
// Page size is the same for all memory regions, so just grab the first one
74-
let page_size = mappings.first().unwrap().page_size_kib;
81+
let first_mapping = mappings.first().unwrap_or_else(|| {
82+
panic!(
83+
"Cannot get the first mapping. Mappings size is {}. Received body: {body}",
84+
mappings.len()
85+
)
86+
});
87+
let page_size = first_mapping.page_size_kib;
7588

7689
// Make sure memory size matches backing data size.
7790
assert_eq!(memsize, size);

0 commit comments

Comments
 (0)