Skip to content

Avoid dynamic allocation #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: avoid-allocation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d5efca7
Use new asm macro in src/interrupt/
maelgui Jan 14, 2021
dc53135
Use asm macro in error.rs
maelgui Jan 14, 2021
53d250b
Changes some llvm_asm to asm macro
AntoineRR Jan 15, 2021
3cb0d1f
Change logger asm output code to new `asm!` macro
paulcacheux Jan 15, 2021
075cfc7
Change asm macro in percore.rs
maelgui Jan 15, 2021
2319303
Change VMX enable method to use new `asm!` macro
paulcacheux Jan 15, 2021
8d3c866
Fix formatting in {push,pop}_regs macros
paulcacheux Jan 15, 2021
ca9a114
Merge remote-tracking branch 'origin/mael.51_use_asm_macro' into mael…
paulcacheux Jan 15, 2021
d672ec6
Change VMXON and VMXOFF methods to use new `asm!` macro
paulcacheux Jan 15, 2021
ee5ce75
Last ASM macro changes in vmx.rs
paulcacheux Jan 21, 2021
62cc2fb
Changes the vmcs file to use asm macro
AntoineRR Jan 21, 2021
ec3b733
Fix asm! macro use in idt.rs
paulcacheux Jan 21, 2021
5502384
Remove unused llvm_asm feature
paulcacheux Jan 21, 2021
ed4c654
Fix asm! macro options
paulcacheux Jan 21, 2021
abc1056
Merge branch 'master' into mael.51_use_asm_macro
maelgui Jan 21, 2021
6711887
Remove hardcoded register
maelgui Jan 21, 2021
1925e0a
Replace magic numbers with constants in linux.rs
Jan 22, 2021
e30e203
Format cleanup
Jan 22, 2021
61d35ce
Merge pull request #125 from benfogle/118-clean-up-const-usage
ALSchwalm Jan 23, 2021
f2e3436
Merge pull request #115 from maelgui/mael.51_use_asm_macro
ALSchwalm Jan 28, 2021
af161a4
Merge pull request #114 from mythril-hypervisor/avoid-allocation
ALSchwalm Feb 26, 2021
ced6a82
Removal of strings in Error
francis-starlab Jan 22, 2021
6782d9a
removed table_name format!() allocation.
francis-starlab Jan 22, 2021
197837f
got rid of check_vm_instruction related allocations.
francis-starlab Jan 22, 2021
1636191
handle testing of log outputs.
francis-starlab Jan 22, 2021
bc3969b
change Makefile slightly to not fail clean if already clean
francis-starlab Jan 22, 2021
e9015eb
put flag before dir.
francis-starlab Feb 26, 2021
a479042
fix LEN_OF_ETC_MYTHRIL
francis-starlab Feb 26, 2021
dc6423a
removed some dynamic allocations added since this MR was opened.
francis-starlab Feb 26, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ test: test_common
.PHONY: clean
clean:
$(CARGO) clean $(CARGO_MANIFEST)
rm $(seabios_blob)
rm -f $(seabios_blob)
make -C seabios clean
rm $(GUEST_ASSETS)
rm -f $(GUEST_ASSETS)

.PHONY: dev-init
dev-init: install-git-hooks
Expand Down
10 changes: 10 additions & 0 deletions mythril/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions mythril/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ spin = "0.5"
ux = { version = "0.1.3", default-features = false }
managed = { version = "0.8.0", features = ["map", "alloc"], default-features = false }

[dev-dependencies]
testing_logger = "0.1.1"

[dependencies.arrayvec]
version = "0.5.2"
default-features = false
Expand Down
41 changes: 20 additions & 21 deletions mythril/src/acpi/madt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ impl IcsType {
if length == self.expected_len() as usize - 2 {
Ok(())
} else {
Err(Error::InvalidValue(format!(
"Invalid length={} for type=0x{:x}",
*self as u8, length
)))
error!("Invalid length={} for type=0x{:x}", *self as u8, length);
Err(Error::InvalidValue)
}
}
}
Expand Down Expand Up @@ -254,11 +252,14 @@ impl Ics {
),
apic_proc_uid: NativeEndian::read_u32(&bytes[10..14]),
}),
_ => Err(Error::NotImplemented(format!(
"type=0x{:x} length={} not implemented",
ty as u8,
bytes.len()
))),
_ => {
error!(
"type=0x{:x} length={} not implemented",
ty as u8,
bytes.len()
);
Err(Error::NotImplemented)
}
}
}

Expand Down Expand Up @@ -294,10 +295,11 @@ impl Ics {
NativeEndian::write_u32(&mut tmp_buf[8..12], gsi_base);
}
_ => {
return Err(Error::NotImplemented(format!(
error!(
"The ICS Type {:?} has not been implemented",
self.ics_type()
)))
);
return Err(Error::NotImplemented);
}
}
buffer.try_extend_from_slice(
Expand Down Expand Up @@ -393,26 +395,23 @@ impl<'a> Iterator for IcsIterator<'a> {
let ty = match IcsType::try_from(self.bytes[0]) {
Ok(ty) => ty,
_ => {
return Some(Err(Error::InvalidValue(format!(
"Invalid ICS type: {}",
self.bytes[0]
))));
error!("Invalid ICS type: {}", self.bytes[0]);
return Some(Err(Error::InvalidValue));
}
};
let len = self.bytes[1] as usize;

if len > self.bytes.len() {
return Some(Err(Error::InvalidValue(format!(
error!(
"Payload for type=0x{:x} and len={} to big for buffer len={}",
ty as u8,
len,
self.bytes.len()
))));
);
return Some(Err(Error::InvalidValue));
} else if len < 3 {
return Some(Err(Error::InvalidValue(format!(
"length `{}` provided is too small",
len,
))));
error!("length `{}` provided is too small", len);
return Some(Err(Error::InvalidValue));
}

let bytes = &self.bytes[2..len];
Expand Down
18 changes: 9 additions & 9 deletions mythril/src/acpi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ pub(self) fn verify_checksum(bytes: &[u8], cksum_idx: usize) -> Result<()> {
if (result & 0xff) == 0x00 {
Ok(())
} else {
Err(Error::InvalidValue(format!(
error!(
"Checksum mismatch checksum={:x} {:x} != 0x00",
bytes[cksum_idx],
result & 0xff,
)))
result & 0xff
);
Err(Error::InvalidValue)
}
}

Expand Down Expand Up @@ -137,11 +138,12 @@ impl GenericAddressStructure {
/// Create a new GAS from a slice of bytes.
pub fn new(bytes: &[u8]) -> Result<GenericAddressStructure> {
if bytes.len() != GAS_SIZE {
return Err(Error::InvalidValue(format!(
error!(
"Invalid number of bytes for GAS: {} != {}",
bytes.len(),
GAS_SIZE
)));
);
return Err(Error::InvalidValue);
}

let address_space =
Expand All @@ -168,10 +170,8 @@ impl GenericAddressStructure {

// verify that the address is only 32 bits for 32-bit platforms.
if !is_64bit && ((address >> 32) & 0xFFFFFFFF) != 0 {
return Err(Error::InvalidValue(format!(
"Invalid address for a 32-bit system: {:x}",
address
)));
error!("Invalid address for a 32-bit system: {:x}", address);
return Err(Error::InvalidValue);
}
}

Expand Down
29 changes: 15 additions & 14 deletions mythril/src/acpi/rsdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub const RSDP_SIGNATURE: &[u8; 8] = b"RSD PTR ";
/// Offsets from `ACPI § 5.2.5.3`
pub mod offsets {
use super::*;

/// Well known bytes, "RST PTR ".
pub const SIGNATURE: Range<usize> = 0..8;
/// Checksum of the fields defined by ACPI 1.0.
Expand Down Expand Up @@ -116,11 +117,8 @@ impl RSDP {
xsdt_addr: NativeEndian::read_u64(&bytes[offsets::XSDT_ADDR]),
},
_ => {
return Err(Error::InvalidValue(format!(
"Invalid RSDP revision: {}",
bytes[offsets::REVISION]
)))
.into();
error!("Invalid RSDP revision: {}", bytes[offsets::REVISION]);
return Err(Error::InvalidValue).into();
}
};

Expand Down Expand Up @@ -178,11 +176,14 @@ impl RSDP {
2 if rsdp_v2_end < range.len() => {
Ok(&range[i..rsdp_v2_end])
}
_ => Err(Error::InvalidValue(format!(
"Invalid RSDP revision: {} at {:p}",
candidate[offsets::REVISION],
candidate.as_ptr()
))),
_ => {
error!(
"Invalid RSDP revision: {} at {:p}",
candidate[offsets::REVISION],
candidate.as_ptr()
);
Err(Error::InvalidValue)
}
};
}
}
Expand All @@ -203,10 +204,10 @@ impl RSDP {
&bytes[..offsets::RESERVED.end],
offsets::EXT_CHECKSUM,
),
_ => Err(Error::InvalidValue(format!(
"Invalid RSDP revision: {}",
bytes[offsets::REVISION]
))),
_ => {
error!("Invalid RSDP revision: {}", bytes[offsets::REVISION]);
Err(Error::InvalidValue)
}
}
}

Expand Down
22 changes: 15 additions & 7 deletions mythril/src/acpi/rsdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,12 @@ fn write_sdt_header(
// The SDT length value is the value of the entire SDT including
// the header.
if buffer.len() < sdt_len {
return Err(Error::InvalidValue(format!(
error!(
"Buffer length should be at least `{}` but was `{}`",
sdt_len,
buffer.len()
)));
);
return Err(Error::InvalidValue);
}
// Fill in the SDT header with the implementations values
buffer[offsets::SIGNATURE].copy_from_slice(signature);
Expand Down Expand Up @@ -339,10 +340,11 @@ impl<'a, T: Array<Item = u8>> RSDTBuilder<'a, T> {
Ok(())
}
} else {
Err(Error::InvalidValue(format!(
error!(
"The key `{}` already exists",
str::from_utf8(&U::SIGNATURE).unwrap()
)))
);
Err(Error::InvalidValue)
}
}

Expand Down Expand Up @@ -373,16 +375,22 @@ impl<'a, T: Array<Item = u8>> RSDTBuilder<'a, T> {
})?;

for (i, (name, (sdt, size))) in self.map.iter().enumerate() {
let table_name = format!("etc/mythril/{}", str::from_utf8(name)?);
const ETC_MYTHRIL: &'static str = "etc/mythril/";
const LEN_OF_NAME: usize = 4;
let mut table_name_bytes = [0u8; ETC_MYTHRIL.len() + LEN_OF_NAME];
table_name_bytes[0..ETC_MYTHRIL.len()]
.copy_from_slice(ETC_MYTHRIL.as_bytes());
table_name_bytes[ETC_MYTHRIL.len()..].copy_from_slice(name);
let table_name = str::from_utf8(&table_name_bytes)?;

table_loader.add_command(TableLoaderCommand::Allocate {
file: &table_name,
file: table_name,
align: 8,
zone: AllocZone::Fseg,
})?;

table_loader.add_command(TableLoaderCommand::AddPointer {
src: &table_name,
src: table_name,
dst: "etc/mythril/xsdt",
offset: ((i * 8) + offsets::CREATOR_REVISION.end) as u32,
size: 8,
Expand Down
5 changes: 2 additions & 3 deletions mythril/src/emulate/controlreg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ pub fn emulate_access(
op => panic!("Unsupported MovFromCr cr0 operation: {:?}", op),
},
_ => {
return Err(Error::InvalidValue(format!(
"Unsupported CR number access"
)))
error!("Unsupported CR number access");
return Err(Error::InvalidValue);
}
}
Ok(())
Expand Down
22 changes: 10 additions & 12 deletions mythril/src/emulate/memio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ macro_rules! read_register {
($out:ident, $value:expr, $type:ty) => {{
let data = ($value as $type).to_be_bytes();
$out.try_extend_from_slice(&data).map_err(|_| {
Error::InvalidValue("Invalid length with reading register".into())
error!("Invalid length with reading register");
Error::InvalidValue
})?;
}};
}
Expand Down Expand Up @@ -143,10 +144,8 @@ fn read_register_value(
iced_x86::Register::RBP => read_register!(res, guest_cpu.rbp, u64),

_ => {
return Err(Error::InvalidValue(format!(
"Invalid register '{:?}'",
register
)))
error!("Invalid register '{:?}'", register);
return Err(Error::InvalidValue);
}
}

Expand Down Expand Up @@ -352,10 +351,8 @@ fn do_mmio_read(
}

register => {
return Err(Error::InvalidValue(format!(
"mmio read into invalid register '{:?}'",
register
)))
error!("mmio read into invalid register '{:?}'", register);
return Err(Error::InvalidValue);
}
},
_ => return Err(Error::NotSupported),
Expand Down Expand Up @@ -433,12 +430,13 @@ fn process_memio_op(
{
do_mmio_read(addr, vcpu, guest_cpu, responses, instr, on_read)?;
} else {
return Err(Error::InvalidValue(format!(
error!(
"Unsupported mmio instruction: {:?} (rip=0x{:x}, bytes={:?})",
instr.code(),
ip,
bytes,
)));
bytes
);
return Err(Error::InvalidValue);
}
Ok(())
}
Expand Down
Loading