Skip to content

Commit a266a31

Browse files
Removal of strings in Error
1 parent c334aa8 commit a266a31

File tree

23 files changed

+312
-349
lines changed

23 files changed

+312
-349
lines changed

mythril/src/acpi/madt.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use num_enum::TryFromPrimitive;
1616
/// SDT (the end of the Creator Revision at offset 36).
1717
mod offsets {
1818
use super::*;
19+
1920
/// 32-bit physical address at which each processor can access its
2021
/// local APIC.
2122
pub const LOCAL_INT_CTRL_ADDR: Range<usize> = 0..4;
@@ -80,10 +81,9 @@ impl IcsType {
8081
if length == self.expected_len() as usize - 2 {
8182
Ok(())
8283
} else {
83-
Err(Error::InvalidValue(format!(
84-
"Invalid length={} for type=0x{:x}",
85-
*self as u8, length
86-
)))
84+
error!("Invalid length={} for type=0x{:x}",
85+
*self as u8, length);
86+
Err(Error::InvalidValue)
8787
}
8888
}
8989
}
@@ -254,16 +254,17 @@ impl Ics {
254254
),
255255
apic_proc_uid: NativeEndian::read_u32(&bytes[10..14]),
256256
}),
257-
_ => Err(Error::NotImplemented(format!(
258-
"type=0x{:x} length={} not implemented",
259-
ty as u8,
260-
bytes.len()
261-
))),
257+
_ => {
258+
error!("type=0x{:x} length={} not implemented",
259+
ty as u8,
260+
bytes.len());
261+
Err(Error::NotImplemented)
262+
}
262263
}
263264
}
264265

265266
/// Encode into the byte sequence
266-
pub fn encode<T: Array<Item = u8>>(
267+
pub fn encode<T: Array<Item=u8>>(
267268
&self,
268269
buffer: &mut ArrayVec<T>,
269270
) -> Result<()> {
@@ -294,10 +295,9 @@ impl Ics {
294295
NativeEndian::write_u32(&mut tmp_buf[8..12], gsi_base);
295296
}
296297
_ => {
297-
return Err(Error::NotImplemented(format!(
298-
"The ICS Type {:?} has not been implemented",
299-
self.ics_type()
300-
)))
298+
error!("The ICS Type {:?} has not been implemented",
299+
self.ics_type());
300+
return Err(Error::NotImplemented);
301301
}
302302
}
303303
buffer.try_extend_from_slice(
@@ -393,26 +393,23 @@ impl<'a> Iterator for IcsIterator<'a> {
393393
let ty = match IcsType::try_from(self.bytes[0]) {
394394
Ok(ty) => ty,
395395
_ => {
396-
return Some(Err(Error::InvalidValue(format!(
397-
"Invalid ICS type: {}",
398-
self.bytes[0]
399-
))));
396+
error!("Invalid ICS type: {}",
397+
self.bytes[0]);
398+
return Some(Err(Error::InvalidValue));
400399
}
401400
};
402401
let len = self.bytes[1] as usize;
403402

404403
if len > self.bytes.len() {
405-
return Some(Err(Error::InvalidValue(format!(
406-
"Payload for type=0x{:x} and len={} to big for buffer len={}",
407-
ty as u8,
408-
len,
409-
self.bytes.len()
410-
))));
404+
error!("Payload for type=0x{:x} and len={} to big for buffer len={}",
405+
ty as u8,
406+
len,
407+
self.bytes.len());
408+
return Some(Err(Error::InvalidValue));
411409
} else if len < 3 {
412-
return Some(Err(Error::InvalidValue(format!(
413-
"length `{}` provided is too small",
414-
len,
415-
))));
410+
error!("length `{}` provided is too small",
411+
len);
412+
return Some(Err(Error::InvalidValue));
416413
}
417414

418415
let bytes = &self.bytes[2..len];
@@ -437,7 +434,7 @@ pub struct MADTBuilder<T: Array> {
437434
structures: ArrayVec<T>,
438435
}
439436

440-
impl<T: Array<Item = Ics>> MADTBuilder<T> {
437+
impl<T: Array<Item=Ics>> MADTBuilder<T> {
441438
/// Create a new builder for the MADT SDT.
442439
pub fn new() -> MADTBuilder<T> {
443440
MADTBuilder {
@@ -465,8 +462,8 @@ impl<T: Array<Item = Ics>> MADTBuilder<T> {
465462
}
466463

467464
impl<U> SDTBuilder for MADTBuilder<U>
468-
where
469-
U: Array<Item = Ics>,
465+
where
466+
U: Array<Item=Ics>,
470467
{
471468
const SIGNATURE: [u8; 4] = [b'A', b'P', b'I', b'C'];
472469

@@ -475,7 +472,7 @@ where
475472
5u8
476473
}
477474

478-
fn encode_table<T: Array<Item = u8>>(
475+
fn encode_table<T: Array<Item=u8>>(
479476
&mut self,
480477
buffer: &mut ArrayVec<T>,
481478
) -> Result<()> {

mythril/src/acpi/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ pub(self) fn verify_checksum(bytes: &[u8], cksum_idx: usize) -> Result<()> {
4848
if (result & 0xff) == 0x00 {
4949
Ok(())
5050
} else {
51-
Err(Error::InvalidValue(format!(
52-
"Checksum mismatch checksum={:x} {:x} != 0x00",
53-
bytes[cksum_idx],
54-
result & 0xff,
55-
)))
51+
error!("Checksum mismatch checksum={:x} {:x} != 0x00", bytes[cksum_idx], result & 0xff);
52+
Err(Error::InvalidValue)
5653
}
5754
}
5855

@@ -137,11 +134,12 @@ impl GenericAddressStructure {
137134
/// Create a new GAS from a slice of bytes.
138135
pub fn new(bytes: &[u8]) -> Result<GenericAddressStructure> {
139136
if bytes.len() != GAS_SIZE {
140-
return Err(Error::InvalidValue(format!(
137+
error!(
141138
"Invalid number of bytes for GAS: {} != {}",
142139
bytes.len(),
143140
GAS_SIZE
144-
)));
141+
);
142+
return Err(Error::InvalidValue);
145143
}
146144

147145
let address_space =
@@ -168,10 +166,8 @@ impl GenericAddressStructure {
168166

169167
// verify that the address is only 32 bits for 32-bit platforms.
170168
if !is_64bit && ((address >> 32) & 0xFFFFFFFF) != 0 {
171-
return Err(Error::InvalidValue(format!(
172-
"Invalid address for a 32-bit system: {:x}",
173-
address
174-
)));
169+
error!("Invalid address for a 32-bit system: {:x}", address);
170+
return Err(Error::InvalidValue);
175171
}
176172
}
177173

mythril/src/acpi/rsdp.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub const RSDP_SIGNATURE: &[u8; 8] = b"RSD PTR ";
2626
/// Offsets from `ACPI § 5.2.5.3`
2727
pub mod offsets {
2828
use super::*;
29+
2930
/// Well known bytes, "RST PTR ".
3031
pub const SIGNATURE: Range<usize> = 0..8;
3132
/// Checksum of the fields defined by ACPI 1.0.
@@ -116,11 +117,10 @@ impl RSDP {
116117
xsdt_addr: NativeEndian::read_u64(&bytes[offsets::XSDT_ADDR]),
117118
},
118119
_ => {
119-
return Err(Error::InvalidValue(format!(
120-
"Invalid RSDP revision: {}",
121-
bytes[offsets::REVISION]
122-
)))
123-
.into();
120+
error!("Invalid RSDP revision: {}",
121+
bytes[offsets::REVISION]);
122+
return Err(Error::InvalidValue)
123+
.into();
124124
}
125125
};
126126

@@ -178,11 +178,12 @@ impl RSDP {
178178
2 if rsdp_v2_end < range.len() => {
179179
Ok(&range[i..rsdp_v2_end])
180180
}
181-
_ => Err(Error::InvalidValue(format!(
182-
"Invalid RSDP revision: {} at {:p}",
183-
candidate[offsets::REVISION],
184-
candidate.as_ptr()
185-
))),
181+
_ => {
182+
error!("Invalid RSDP revision: {} at {:p}",
183+
candidate[offsets::REVISION],
184+
candidate.as_ptr());
185+
Err(Error::InvalidValue)
186+
}
186187
};
187188
}
188189
}
@@ -203,10 +204,11 @@ impl RSDP {
203204
&bytes[..offsets::RESERVED.end],
204205
offsets::EXT_CHECKSUM,
205206
),
206-
_ => Err(Error::InvalidValue(format!(
207-
"Invalid RSDP revision: {}",
208-
bytes[offsets::REVISION]
209-
))),
207+
_ => {
208+
error!("Invalid RSDP revision: {}",
209+
bytes[offsets::REVISION]);
210+
Err(Error::InvalidValue)
211+
}
210212
}
211213
}
212214

@@ -220,11 +222,11 @@ impl RSDP {
220222
}
221223

222224
/// Builder structure for the RSDP
223-
pub struct RSDPBuilder<'a, T: Array<Item = u8>> {
225+
pub struct RSDPBuilder<'a, T: Array<Item=u8>> {
224226
builder: RSDTBuilder<'a, T>,
225227
}
226228

227-
impl<'a, T: Array<Item = u8>> RSDPBuilder<'a, T> {
229+
impl<'a, T: Array<Item=u8>> RSDPBuilder<'a, T> {
228230
/// Create a new RSDP Builder.
229231
pub fn new(
230232
map: ManagedMap<'a, [u8; 4], (ArrayVec<T>, usize)>,

mythril/src/acpi/rsdt.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,10 @@ fn write_sdt_header(
246246
// The SDT length value is the value of the entire SDT including
247247
// the header.
248248
if buffer.len() < sdt_len {
249-
return Err(Error::InvalidValue(format!(
250-
"Buffer length should be at least `{}` but was `{}`",
251-
sdt_len,
252-
buffer.len()
253-
)));
249+
error!("Buffer length should be at least `{}` but was `{}`",
250+
sdt_len,
251+
buffer.len());
252+
return Err(Error::InvalidValue);
254253
}
255254
// Fill in the SDT header with the implementations values
256255
buffer[offsets::SIGNATURE].copy_from_slice(signature);
@@ -339,10 +338,9 @@ impl<'a, T: Array<Item = u8>> RSDTBuilder<'a, T> {
339338
Ok(())
340339
}
341340
} else {
342-
Err(Error::InvalidValue(format!(
343-
"The key `{}` already exists",
344-
str::from_utf8(&U::SIGNATURE).unwrap()
345-
)))
341+
error!("The key `{}` already exists",
342+
str::from_utf8(&U::SIGNATURE).unwrap());
343+
Err(Error::InvalidValue)
346344
}
347345
}
348346

mythril/src/emulate/controlreg.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ pub fn emulate_access(
6060
op => panic!("Unsupported MovFromCr cr0 operation: {:?}", op),
6161
},
6262
_ => {
63-
return Err(Error::InvalidValue(format!(
64-
"Unsupported CR number access"
65-
)))
63+
error!("Unsupported CR number access");
64+
return Err(Error::InvalidValue)
6665
}
6766
}
6867
Ok(())

mythril/src/emulate/memio.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ macro_rules! read_register {
3434
($out:ident, $value:expr, $type:ty) => {{
3535
let data = ($value as $type).to_be_bytes();
3636
$out.try_extend_from_slice(&data).map_err(|_| {
37-
Error::InvalidValue("Invalid length with reading register".into())
37+
error!("Invalid length with reading register");
38+
Error::InvalidValue
3839
})?;
3940
}};
4041
}
@@ -143,10 +144,8 @@ fn read_register_value(
143144
iced_x86::Register::RBP => read_register!(res, guest_cpu.rbp, u64),
144145

145146
_ => {
146-
return Err(Error::InvalidValue(format!(
147-
"Invalid register '{:?}'",
148-
register
149-
)))
147+
error!("Invalid register '{:?}'", register);
148+
return Err(Error::InvalidValue)
150149
}
151150
}
152151

@@ -352,10 +351,9 @@ fn do_mmio_read(
352351
}
353352

354353
register => {
355-
return Err(Error::InvalidValue(format!(
356-
"mmio read into invalid register '{:?}'",
357-
register
358-
)))
354+
error!("mmio read into invalid register '{:?}'",
355+
register);
356+
return Err(Error::InvalidValue)
359357
}
360358
},
361359
_ => return Err(Error::NotSupported),
@@ -433,12 +431,11 @@ fn process_memio_op(
433431
{
434432
do_mmio_read(addr, vcpu, guest_cpu, responses, instr, on_read)?;
435433
} else {
436-
return Err(Error::InvalidValue(format!(
437-
"Unsupported mmio instruction: {:?} (rip=0x{:x}, bytes={:?})",
438-
instr.code(),
439-
ip,
440-
bytes,
441-
)));
434+
error!("Unsupported mmio instruction: {:?} (rip=0x{:x}, bytes={:?})",
435+
instr.code(),
436+
ip,
437+
bytes);
438+
return Err(Error::InvalidValue);
442439
}
443440
Ok(())
444441
}

0 commit comments

Comments
 (0)