Skip to content

Commit c9fa2f4

Browse files
got rid of check_vm_instruction related allocations.
1 parent b0bfd75 commit c9fa2f4

22 files changed

+160
-167
lines changed

mythril/src/acpi/madt.rs

+23-21
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use num_enum::TryFromPrimitive;
1616
/// SDT (the end of the Creator Revision at offset 36).
1717
mod offsets {
1818
use super::*;
19-
2019
/// 32-bit physical address at which each processor can access its
2120
/// local APIC.
2221
pub const LOCAL_INT_CTRL_ADDR: Range<usize> = 0..4;
@@ -81,8 +80,7 @@ impl IcsType {
8180
if length == self.expected_len() as usize - 2 {
8281
Ok(())
8382
} else {
84-
error!("Invalid length={} for type=0x{:x}",
85-
*self as u8, length);
83+
error!("Invalid length={} for type=0x{:x}", *self as u8, length);
8684
Err(Error::InvalidValue)
8785
}
8886
}
@@ -255,16 +253,18 @@ impl Ics {
255253
apic_proc_uid: NativeEndian::read_u32(&bytes[10..14]),
256254
}),
257255
_ => {
258-
error!("type=0x{:x} length={} not implemented",
259-
ty as u8,
260-
bytes.len());
256+
error!(
257+
"type=0x{:x} length={} not implemented",
258+
ty as u8,
259+
bytes.len()
260+
);
261261
Err(Error::NotImplemented)
262262
}
263263
}
264264
}
265265

266266
/// Encode into the byte sequence
267-
pub fn encode<T: Array<Item=u8>>(
267+
pub fn encode<T: Array<Item = u8>>(
268268
&self,
269269
buffer: &mut ArrayVec<T>,
270270
) -> Result<()> {
@@ -295,8 +295,10 @@ impl Ics {
295295
NativeEndian::write_u32(&mut tmp_buf[8..12], gsi_base);
296296
}
297297
_ => {
298-
error!("The ICS Type {:?} has not been implemented",
299-
self.ics_type());
298+
error!(
299+
"The ICS Type {:?} has not been implemented",
300+
self.ics_type()
301+
);
300302
return Err(Error::NotImplemented);
301303
}
302304
}
@@ -393,22 +395,22 @@ impl<'a> Iterator for IcsIterator<'a> {
393395
let ty = match IcsType::try_from(self.bytes[0]) {
394396
Ok(ty) => ty,
395397
_ => {
396-
error!("Invalid ICS type: {}",
397-
self.bytes[0]);
398+
error!("Invalid ICS type: {}", self.bytes[0]);
398399
return Some(Err(Error::InvalidValue));
399400
}
400401
};
401402
let len = self.bytes[1] as usize;
402403

403404
if len > self.bytes.len() {
404-
error!("Payload for type=0x{:x} and len={} to big for buffer len={}",
405-
ty as u8,
406-
len,
407-
self.bytes.len());
405+
error!(
406+
"Payload for type=0x{:x} and len={} to big for buffer len={}",
407+
ty as u8,
408+
len,
409+
self.bytes.len()
410+
);
408411
return Some(Err(Error::InvalidValue));
409412
} else if len < 3 {
410-
error!("length `{}` provided is too small",
411-
len);
413+
error!("length `{}` provided is too small", len);
412414
return Some(Err(Error::InvalidValue));
413415
}
414416

@@ -434,7 +436,7 @@ pub struct MADTBuilder<T: Array> {
434436
structures: ArrayVec<T>,
435437
}
436438

437-
impl<T: Array<Item=Ics>> MADTBuilder<T> {
439+
impl<T: Array<Item = Ics>> MADTBuilder<T> {
438440
/// Create a new builder for the MADT SDT.
439441
pub fn new() -> MADTBuilder<T> {
440442
MADTBuilder {
@@ -462,8 +464,8 @@ impl<T: Array<Item=Ics>> MADTBuilder<T> {
462464
}
463465

464466
impl<U> SDTBuilder for MADTBuilder<U>
465-
where
466-
U: Array<Item=Ics>,
467+
where
468+
U: Array<Item = Ics>,
467469
{
468470
const SIGNATURE: [u8; 4] = [b'A', b'P', b'I', b'C'];
469471

@@ -472,7 +474,7 @@ impl<U> SDTBuilder for MADTBuilder<U>
472474
5u8
473475
}
474476

475-
fn encode_table<T: Array<Item=u8>>(
477+
fn encode_table<T: Array<Item = u8>>(
476478
&mut self,
477479
buffer: &mut ArrayVec<T>,
478480
) -> Result<()> {

mythril/src/acpi/mod.rs

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

mythril/src/acpi/rsdp.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,8 @@ impl RSDP {
117117
xsdt_addr: NativeEndian::read_u64(&bytes[offsets::XSDT_ADDR]),
118118
},
119119
_ => {
120-
error!("Invalid RSDP revision: {}",
121-
bytes[offsets::REVISION]);
122-
return Err(Error::InvalidValue)
123-
.into();
120+
error!("Invalid RSDP revision: {}", bytes[offsets::REVISION]);
121+
return Err(Error::InvalidValue).into();
124122
}
125123
};
126124

@@ -179,9 +177,11 @@ impl RSDP {
179177
Ok(&range[i..rsdp_v2_end])
180178
}
181179
_ => {
182-
error!("Invalid RSDP revision: {} at {:p}",
183-
candidate[offsets::REVISION],
184-
candidate.as_ptr());
180+
error!(
181+
"Invalid RSDP revision: {} at {:p}",
182+
candidate[offsets::REVISION],
183+
candidate.as_ptr()
184+
);
185185
Err(Error::InvalidValue)
186186
}
187187
};
@@ -205,8 +205,7 @@ impl RSDP {
205205
offsets::EXT_CHECKSUM,
206206
),
207207
_ => {
208-
error!("Invalid RSDP revision: {}",
209-
bytes[offsets::REVISION]);
208+
error!("Invalid RSDP revision: {}", bytes[offsets::REVISION]);
210209
Err(Error::InvalidValue)
211210
}
212211
}
@@ -222,11 +221,11 @@ impl RSDP {
222221
}
223222

224223
/// Builder structure for the RSDP
225-
pub struct RSDPBuilder<'a, T: Array<Item=u8>> {
224+
pub struct RSDPBuilder<'a, T: Array<Item = u8>> {
226225
builder: RSDTBuilder<'a, T>,
227226
}
228227

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

mythril/src/acpi/rsdt.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,11 @@ 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-
error!("Buffer length should be at least `{}` but was `{}`",
250-
sdt_len,
251-
buffer.len());
249+
error!(
250+
"Buffer length should be at least `{}` but was `{}`",
251+
sdt_len,
252+
buffer.len()
253+
);
252254
return Err(Error::InvalidValue);
253255
}
254256
// Fill in the SDT header with the implementations values
@@ -338,8 +340,10 @@ impl<'a, T: Array<Item = u8>> RSDTBuilder<'a, T> {
338340
Ok(())
339341
}
340342
} else {
341-
error!("The key `{}` already exists",
342-
str::from_utf8(&U::SIGNATURE).unwrap());
343+
error!(
344+
"The key `{}` already exists",
345+
str::from_utf8(&U::SIGNATURE).unwrap()
346+
);
343347
Err(Error::InvalidValue)
344348
}
345349
}
@@ -371,10 +375,11 @@ impl<'a, T: Array<Item = u8>> RSDTBuilder<'a, T> {
371375
})?;
372376

373377
for (i, (name, (sdt, size))) in self.map.iter().enumerate() {
374-
const LEN_OF_ETC_MYTHRIL :usize = 12;
378+
const LEN_OF_ETC_MYTHRIL: usize = 12;
375379
const LEN_OF_NAME: usize = 4;
376-
let mut table_name_bytes = [0u8;LEN_OF_ETC_MYTHRIL + LEN_OF_NAME];
377-
table_name_bytes[0..LEN_OF_ETC_MYTHRIL].copy_from_slice("etc/mythril/".as_bytes());
380+
let mut table_name_bytes = [0u8; LEN_OF_ETC_MYTHRIL + LEN_OF_NAME];
381+
table_name_bytes[0..LEN_OF_ETC_MYTHRIL]
382+
.copy_from_slice("etc/mythril/".as_bytes());
378383
table_name_bytes[LEN_OF_ETC_MYTHRIL..].copy_from_slice(name);
379384
let table_name = str::from_utf8(&table_name_bytes)?;
380385

mythril/src/emulate/controlreg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn emulate_access(
6161
},
6262
_ => {
6363
error!("Unsupported CR number access");
64-
return Err(Error::InvalidValue)
64+
return Err(Error::InvalidValue);
6565
}
6666
}
6767
Ok(())

mythril/src/emulate/memio.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn read_register_value(
145145

146146
_ => {
147147
error!("Invalid register '{:?}'", register);
148-
return Err(Error::InvalidValue)
148+
return Err(Error::InvalidValue);
149149
}
150150
}
151151

@@ -351,9 +351,8 @@ fn do_mmio_read(
351351
}
352352

353353
register => {
354-
error!("mmio read into invalid register '{:?}'",
355-
register);
356-
return Err(Error::InvalidValue)
354+
error!("mmio read into invalid register '{:?}'", register);
355+
return Err(Error::InvalidValue);
357356
}
358357
},
359358
_ => return Err(Error::NotSupported),
@@ -431,10 +430,12 @@ fn process_memio_op(
431430
{
432431
do_mmio_read(addr, vcpu, guest_cpu, responses, instr, on_read)?;
433432
} else {
434-
error!("Unsupported mmio instruction: {:?} (rip=0x{:x}, bytes={:?})",
435-
instr.code(),
436-
ip,
437-
bytes);
433+
error!(
434+
"Unsupported mmio instruction: {:?} (rip=0x{:x}, bytes={:?})",
435+
instr.code(),
436+
ip,
437+
bytes
438+
);
438439
return Err(Error::InvalidValue);
439440
}
440441
Ok(())

mythril/src/error.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::vmcs;
2-
use alloc::string::String;
32
use arrayvec::CapacityError;
43
use core::convert::TryFrom;
54
use core::num::TryFromIntError;
@@ -44,11 +43,11 @@ pub enum VmInstructionError {
4443
InvalidOperandToInveptInvvpid = 28,
4544
}
4645

47-
pub fn check_vm_insruction(rflags: u64, error: String) -> Result<()> {
46+
pub fn check_vm_instruction(rflags: u64, log_error: impl Fn()) -> Result<()> {
4847
let rflags = rflags::RFlags::from_bits_truncate(rflags);
4948

5049
if rflags.contains(RFlags::FLAGS_CF) {
51-
error!("{}",error);
50+
log_error();
5251
Err(Error::VmFailInvalid)
5352
} else if rflags.contains(RFlags::FLAGS_ZF) {
5453
let errno = unsafe {
@@ -63,8 +62,8 @@ pub fn check_vm_insruction(rflags: u64, error: String) -> Result<()> {
6362
let vm_error = VmInstructionError::try_from(errno)
6463
.unwrap_or(VmInstructionError::UnknownError);
6564

66-
error!("{:?}",vm_error);
67-
error!("{}",error);
65+
error!("{:?}", vm_error);
66+
log_error();
6867
Err(Error::VmFailValid)
6968
} else {
7069
Ok(())
@@ -88,21 +87,21 @@ pub enum Error {
8887

8988
impl<T: TryFromPrimitive> From<TryFromPrimitiveError<T>> for Error {
9089
fn from(error: TryFromPrimitiveError<T>) -> Error {
91-
error!("{}",error);
90+
error!("{}", error);
9291
Error::InvalidValue
9392
}
9493
}
9594

9695
impl From<TryFromIntError> for Error {
9796
fn from(error: TryFromIntError) -> Error {
98-
error!("{}",error);
97+
error!("{}", error);
9998
Error::InvalidValue
10099
}
101100
}
102101

103102
impl From<core::str::Utf8Error> for Error {
104103
fn from(error: core::str::Utf8Error) -> Error {
105-
error!("{}",error);
104+
error!("{}", error);
106105
Error::InvalidValue
107106
}
108107
}

0 commit comments

Comments
 (0)