Skip to content

Commit 2ad5c71

Browse files
committed
Update to minicbor v2, remove error nonsense
1 parent 70e0623 commit 2ad5c71

File tree

5 files changed

+36
-95
lines changed

5 files changed

+36
-95
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ leb128 = { version = "0.2.5", default-features = false }
9292
lpc55-pac = { version = "0.4", default-features = false }
9393
memchr = { version = "2.4", default-features = false }
9494
memoffset = { version = "0.6.5", default-features = false }
95-
minicbor = { version = "0.26.4", default-features = false }
95+
minicbor = { version = "2.0.0", default-features = false }
9696
multimap = { version = "0.8.3", default-features = false }
9797
nb = { version = "1", default-features = false }
9898
num = { version = "0.4", default-features = false }

lib/minicbor-lease/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "minicbor-lease"
33
version = "0.1.0"
4-
edition = "2021"
4+
edition = "2024"
55

66
[dependencies]
77
minicbor = { workspace = true }

lib/minicbor-lease/src/lib.rs

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,43 @@ where
1818
ran_out_of_space: bool,
1919
}
2020

21-
/// Errors returned by [`LeasedWriter::check_err`].
22-
#[derive(Copy, Clone, PartialEq)]
21+
/// Errors returned by the [`minicbor::encode::write::Write`] implementation for
22+
/// [`LeasedWriter`].
23+
#[derive(Copy, Clone, PartialEq, Debug)]
2324
pub enum Error {
2425
/// The other side of the lease has gone away.
2526
WentAway,
2627
/// Data could not be written as there was no room left in the lease.
2728
EndOfLease,
2829
}
2930

30-
/// Errors returned by the [`minicbor::encode::write::Write`] implementation for
31-
/// [`LeasedWriter`].
32-
#[derive(Copy, Clone, PartialEq)]
33-
pub struct WriteError;
31+
impl core::fmt::Display for Error {
32+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
33+
f.write_str(match self {
34+
Self::WentAway => "lease went away",
35+
Self::EndOfLease => "end of lease",
36+
})
37+
}
38+
}
3439

3540
impl<A> minicbor::encode::write::Write for LeasedWriter<'_, A>
3641
where
3742
A: idol_runtime::AttributeWrite,
3843
{
39-
type Error = WriteError;
44+
type Error = Error;
4045

4146
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
4247
let Some(end) = self.pos.checked_add(buf.len()) else {
4348
self.ran_out_of_space = true;
44-
return Err(WriteError);
49+
return Err(Error::EndOfLease);
4550
};
4651
if end >= self.lease.len() {
4752
self.ran_out_of_space = true;
48-
return Err(WriteError);
53+
return Err(Error::EndOfLease);
4954
}
5055
self.lease
5156
.write_range(self.pos..end, buf)
52-
.map_err(|_| WriteError)?;
57+
.map_err(|_| Error::WentAway)?;
5358

5459
self.pos += buf.len();
5560

@@ -101,49 +106,10 @@ where
101106
self.lease
102107
}
103108

104-
/// Returns `true` if the last `write_all` call to return an error failed
105-
/// due to running out of space.
106-
///
107-
/// This is an unfortunate workaround for a limitation of the `minicbor`
108-
/// API: the errors our `Write` implementation can return are wrapped in an
109-
/// `encode::Error`, and we can't actually get our errors *out* of that
110-
/// wrapper, so there's no way to tell whether the error was because we ran
111-
/// out of space in the buffer, or because the lease client went away. So,
112-
/// we track it here, so that the caller can just ask us if we didn't have
113-
/// space to encode something.
114-
///
115-
/// Note that this is separate from `pos == lease.len()`, because we don't
116-
/// actually write anything (and thus don't advance `pos`) if asked to write
117-
/// a chunk of bytes that don't fit.
109+
/// Returns `true` if the last `w
118110
pub fn ran_out_of_space(&self) -> bool {
119111
self.ran_out_of_space
120112
}
121-
122-
/// Determine whether an error returned by the [`minicbor::encode::Write`]
123-
/// implementation indicates that there was no space left in the lease, or
124-
/// that the client went away.
125-
///
126-
/// This is an unfortunate workaround for a limitation of the `minicbor`
127-
/// API: the errors our `Write` implementation can return are wrapped in an
128-
/// `encode::Error`, and we can't actually get our errors *out* of that
129-
/// wrapper, so there's no way to tell whether the error was becasue we ran
130-
/// out of space in the buffer, or because the lease client went away. So,
131-
/// we track it here, so that the caller can just ask us if we didn't have
132-
/// space to encode something.
133-
///
134-
pub fn check_err(&self, err: minicbor::encode::Error<WriteError>) -> Error {
135-
if err.is_write() {
136-
if self.ran_out_of_space {
137-
Error::EndOfLease
138-
} else {
139-
Error::WentAway
140-
}
141-
} else {
142-
// This is an encoder error, which we have no good way to
143-
// recover from.
144-
panic!()
145-
}
146-
}
147113
}
148114

149115
impl From<Error> for idol_runtime::ClientError {

task/packrat/src/ereport.rs

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use minicbor::CborLen;
2020
use minicbor_lease::LeasedWriter;
2121
use ringbuf::{counted_ringbuf, ringbuf_entry};
2222
use task_packrat_api::{EreportReadError, VpdIdentity};
23-
use userlib::{kipc, sys_get_timer, RecvMessage, TaskId};
23+
use userlib::{kipc, sys_get_timer, RecvMessage, TaskId, UnwrapLite};
2424
use zerocopy::IntoBytes;
2525

2626
pub(crate) struct EreportStore {
@@ -160,11 +160,12 @@ impl EreportStore {
160160
&mut data,
161161
));
162162

163-
fn check_err(
164-
encoder: &minicbor::Encoder<LeasedWriter<'_, idol_runtime::W>>,
165-
err: minicbor::encode::Error<minicbor_lease::WriteError>,
163+
fn handle_encode_err(
164+
err: minicbor::encode::Error<minicbor_lease::Error>,
166165
) -> RequestError<EreportReadError> {
167-
match encoder.writer().check_err(err) {
166+
// These should always be write errors; everything we write should
167+
// always encode successfully.
168+
match err.into_write().unwrap_lite() {
168169
minicbor_lease::Error::WentAway => ClientError::WentAway.fail(),
169170
minicbor_lease::Error::EndOfLease => {
170171
ClientError::BadLease.fail()
@@ -176,20 +177,7 @@ impl EreportStore {
176177
//
177178
// MGS expects us to always include this, and to just have it be
178179
// empty if we didn't send any metadata.
179-
encoder
180-
.begin_map()
181-
// This pattern (which will occur every time we handle an encoder
182-
// error in this function) is goofy, but is necessary to placate the
183-
// borrow checker: the function passed to `map_err` must borrow the
184-
// encoder so that it can check whether the error indicates that we
185-
// ran out of space in the lease, or if the client disappeared.
186-
// However, because `minicbor::Encoder`'s methods return a mutable
187-
// borrow of the encoder, we must drop it first before borrowing it
188-
// into the `map_err` closure. Thus, every `map_err` must be
189-
// preceded by a `map` with a toilet closure that drops the mutable
190-
// borrow. Yuck.
191-
.map(|_| ())
192-
.map_err(|e| check_err(&encoder, e))?;
180+
encoder.begin_map().map_err(handle_encode_err)?;
193181

194182
// If the requested restart ID matches the current restart ID, then read
195183
// from the requested ENA. If not, start at ENA 0.
@@ -218,8 +206,7 @@ impl EreportStore {
218206
if let Some(vpd) = vpd {
219207
// Encode the metadata map.
220208
self.encode_metadata(&mut encoder, vpd)
221-
.map(|_| ())
222-
.map_err(|e| check_err(&encoder, e))?;
209+
.map_err(handle_encode_err)?;
223210
ringbuf_entry!(Trace::MetadataEncoded {
224211
len: encoder
225212
.writer()
@@ -233,10 +220,7 @@ impl EreportStore {
233220
};
234221

235222
// End metadata map.
236-
encoder
237-
.end()
238-
.map(|_| ())
239-
.map_err(|e| check_err(&encoder, e))?;
223+
encoder.end().map_err(handle_encode_err)?;
240224

241225
let mut reports = 0;
242226
// Beginning with the first
@@ -290,24 +274,15 @@ impl EreportStore {
290274
if first_written_ena.is_none() {
291275
first_written_ena = Some(r.ena);
292276
// Start the ereport array
293-
encoder
294-
.begin_array()
295-
.map(|_| ())
296-
.map_err(|e| check_err(&encoder, e))?;
277+
encoder.begin_array().map_err(handle_encode_err)?;
297278
}
298-
encoder
299-
.encode(&entry)
300-
.map(|_| ())
301-
.map_err(|e| check_err(&encoder, e))?;
279+
encoder.encode(&entry).map_err(handle_encode_err)?;
302280
reports += 1;
303281
}
304282

305283
if let Some(start_ena) = first_written_ena {
306284
// End CBOR array, if we wrote anything.
307-
encoder
308-
.end()
309-
.map(|_| ())
310-
.map_err(|e| check_err(&encoder, e))?;
285+
encoder.end().map_err(handle_encode_err)?;
311286

312287
ringbuf_entry!(Trace::Reported {
313288
start_ena,
@@ -335,7 +310,7 @@ impl EreportStore {
335310
&self,
336311
encoder: &mut minicbor::Encoder<LeasedWriter<'_, idol_runtime::W>>,
337312
vpd: &VpdIdentity,
338-
) -> Result<(), minicbor::encode::Error<minicbor_lease::WriteError>> {
313+
) -> Result<(), minicbor::encode::Error<minicbor_lease::Error>> {
339314
encoder
340315
.str("hubris_archive_id")?
341316
.bytes(&self.image_id[..])?;

0 commit comments

Comments
 (0)