Skip to content

Commit

Permalink
misc: Fix reported clippy warnings
Browse files Browse the repository at this point in the history
Various components are throwing some trivial clippy warnings which are
against the idiomatic rust pattterns.

Signed-off-by: Jinank Jain <[email protected]>
  • Loading branch information
jinankjain authored and jyao1 committed Dec 25, 2023
1 parent 0b87fe7 commit e197fdd
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 60 deletions.
10 changes: 5 additions & 5 deletions cc-measurement/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl<'a> CcEventLogWriter<'a> {

// Write the event header into event log memory and update the 'size' and 'last'
let event_offset = self
.log_cc_event_header(mr_index, event_type, &sha384, event_data_size as u32)
.log_cc_event_header(mr_index, event_type, sha384, event_data_size as u32)
.ok_or(CcEventLogError::OutOfResource)?;

let mut data_offset = size_of::<CcEventHeader>();
Expand Down Expand Up @@ -242,10 +242,10 @@ impl<'a> Iterator for CcEvents<'a> {
if end_of_event < self.bytes.len() {
let event_data = &self.bytes[size_of::<CcEventHeader>()..end_of_event];
self.bytes = &self.bytes[end_of_event..];
return Some((event_header, event_data));
Some((event_header, event_data))
} else {
return None;
};
None
}
}
}

Expand Down Expand Up @@ -277,7 +277,7 @@ impl<'a> CcEventLogReader<'a> {
specific_id_event,
};

return Some(cc_event_log);
Some(cc_event_log)
}

pub fn query(&self, key: &[u8]) -> Option<CcEventHeader> {
Expand Down
10 changes: 5 additions & 5 deletions td-exception/src/idt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ impl Idt {
current_idt[20].set_func(interrupt::default_exception as usize);
current_idt[21].set_func(interrupt::control_flow as usize);
// reset exception reserved
for i in 22..32 {
current_idt[i].set_func(interrupt::default_exception as usize);
for idt in current_idt.iter_mut().take(32).skip(22) {
idt.set_func(interrupt::default_exception as usize);
}
// Setup reset potential interrupt handler.
for i in 32..IDT_ENTRY_COUNT {
current_idt[i].set_func(interrupt::default_interrupt as usize);
for idt in current_idt.iter_mut().take(IDT_ENTRY_COUNT).skip(32) {
idt.set_func(interrupt::default_interrupt as usize);
}
}

Expand Down Expand Up @@ -179,7 +179,7 @@ impl IdtEntry {
// A function to set the offset more easily
pub fn set_func(&mut self, func: usize) {
self.set_flags(IdtFlags::PRESENT | IdtFlags::INTERRUPT);
self.set_offset(CS::get_reg().0, func as usize); // GDT_KERNEL_CODE 1u16
self.set_offset(CS::get_reg().0, func); // GDT_KERNEL_CODE 1u16
}

pub fn set_ist(&mut self, index: u8) {
Expand Down
28 changes: 11 additions & 17 deletions td-exception/src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,15 @@ fn handle_tdx_ioexit(ve_info: &tdx::TdVeInfo, stack: &mut InterruptNoErrorStack)
let io_read = |size, port| match size {
1 => tdx::tdvmcall_io_read_8(port) as u32,
2 => tdx::tdvmcall_io_read_16(port) as u32,
4 => tdx::tdvmcall_io_read_32(port) as u32,
4 => tdx::tdvmcall_io_read_32(port),
_ => 0,
};

// Define closure to perform IO port write with different size operands
let io_write = |size, port, data| match size {
1 => tdx::tdvmcall_io_write_8(port, data as u8),
2 => tdx::tdvmcall_io_write_16(port, data as u16),
4 => tdx::tdvmcall_io_write_32(port, data as u32),
4 => tdx::tdvmcall_io_write_32(port, data),
_ => {}
};

Expand All @@ -525,36 +525,30 @@ fn handle_tdx_ioexit(ve_info: &tdx::TdVeInfo, stack: &mut InterruptNoErrorStack)
if read {
let val = io_read(size, port);
unsafe {
let rsi = core::slice::from_raw_parts_mut(
stack.scratch.rdi as *mut u8,
size as usize,
);
let rsi = core::slice::from_raw_parts_mut(stack.scratch.rdi as *mut u8, size);
// Safety: size is smaller than 4
rsi.copy_from_slice(&u32::to_le_bytes(val)[..size])
}
stack.scratch.rdi += size as usize;
stack.scratch.rdi += size;
} else {
let mut val = 0;
unsafe {
let rsi =
core::slice::from_raw_parts(stack.scratch.rsi as *mut u8, size as usize);
let rsi = core::slice::from_raw_parts(stack.scratch.rsi as *mut u8, size);
for (idx, byte) in rsi.iter().enumerate() {
val |= (*byte as u32) << (idx * 8);
}
}
io_write(size, port, val);
stack.scratch.rsi += size as usize;
stack.scratch.rsi += size;
}
stack.scratch.rcx -= 1;
}
} else if read {
// Write the IO read result to the low $size-bytes of rax
stack.scratch.rax = (stack.scratch.rax & !(2_usize.pow(size as u32 * 8) - 1))
| (io_read(size, port) as usize & (2_usize.pow(size as u32 * 8) - 1));
} else {
if read {
// Write the IO read result to the low $size-bytes of rax
stack.scratch.rax = (stack.scratch.rax & !(2_usize.pow(size as u32 * 8) - 1))
| (io_read(size, port) as usize & (2_usize.pow(size as u32 * 8) - 1));
} else {
io_write(size, port, stack.scratch.rax as u32);
}
io_write(size, port, stack.scratch.rax as u32);
}

true
Expand Down
2 changes: 1 addition & 1 deletion td-layout/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl RuntimeMemoryLayout {
self.regions
.iter()
.find(|item| item.name == name.as_str())
.map(|region| *region)
.copied()
}

pub unsafe fn get_mem_slice(&self, name: SliceType) -> Option<&'static [u8]> {
Expand Down
2 changes: 1 addition & 1 deletion td-layout/src/runtime/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub const PAYLOAD_PAGE_TABLE_SIZE: usize = 0x20000; // 128 KB
pub const RELOCATED_MAILBOX_SIZE: usize = 0x2000; // 8 KB
pub const EVENT_LOG_SIZE: usize = 0x100000; // 1 MB

pub const MEMORY_LAYOUT_CONFIG: &[(&'static str, usize, &'static str)] = &[
pub const MEMORY_LAYOUT_CONFIG: &[(&str, usize, &str)] = &[
// (name of memory region, region size, region type)
("Bootloader", 0x800000, "Memory"),
("TdHob", 0x20000, "Memory"),
Expand Down
2 changes: 1 addition & 1 deletion td-layout/src/runtime/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub const PAYLOAD_PAGE_TABLE_SIZE: usize = 0x20000; // 128 KB
pub const RELOCATED_MAILBOX_SIZE: usize = 0x2000; // 8 KB
pub const EVENT_LOG_SIZE: usize = 0x100000; // 1 MB

pub const MEMORY_LAYOUT_CONFIG: &[(&'static str, usize, &'static str)] = &[
pub const MEMORY_LAYOUT_CONFIG: &[(&str, usize, &str)] = &[
// (name of memory region, region size, region type)
("Bootloader", 0x800000, "Memory"),
("TdHob", 0x20000, "Memory"),
Expand Down
10 changes: 5 additions & 5 deletions td-loader/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ pub fn relocate_elf_with_per_program_header(
}

Some((
elf.header.e_entry.checked_add(new_image_base as u64)? as u64,
bottom as u64,
top.checked_sub(bottom)? as u64,
elf.header.e_entry.checked_add(new_image_base as u64)?,
bottom,
top.checked_sub(bottom)?,
))
}

Expand All @@ -108,9 +108,9 @@ pub fn parse_finit_array_section(loaded_image: &[u8]) -> Option<Range<usize>> {
/// flag true align to low address else high address
fn align_value(value: u64, align: u64, flag: bool) -> u64 {
if flag {
value & ((!(align - 1)) as u64)
value & (!(align - 1))
} else {
value - (value & (align - 1)) as u64 + align
value - (value & (align - 1)) + align
}
}

Expand Down
17 changes: 7 additions & 10 deletions td-loader/src/pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub fn relocate_with_per_section(
.pwrite(new_image_base as u64, coff_optional_offset)
.ok()?;

let sections = Sections::parse(sections_buffer, num_sections as usize)?;
let sections = Sections::parse(sections_buffer, num_sections)?;
// Load the PE header into the destination memory
for section in sections {
let section_size = section.section_size() as usize;
Expand All @@ -200,8 +200,8 @@ pub fn relocate_with_per_section(
let dst_start = section.virtual_address as usize;
let dst_end = dst_start.checked_add(section_size)?;

image_buffer.len().checked_sub(src_end as usize)?;
loaded_buffer.len().checked_sub(dst_end as usize)?;
image_buffer.len().checked_sub(src_end)?;
loaded_buffer.len().checked_sub(dst_end)?;
loaded_buffer[dst_start..dst_end].copy_from_slice(&image_buffer[src_start..src_end]);
if section.virtual_size as usize > section_size {
let fill_end = dst_start.checked_add(section.virtual_size as usize)?;
Expand All @@ -210,15 +210,15 @@ pub fn relocate_with_per_section(
}
}

let sections = Sections::parse(sections_buffer, num_sections as usize)?;
let sections = Sections::parse(sections_buffer, num_sections)?;
for section in sections {
if &section.name == b".reloc\0\0" && image_base != new_image_base as u64 {
reloc_to_base(
loaded_buffer,
image_buffer,
&section,
image_base as usize,
new_image_base as usize,
new_image_base,
)?;
}
}
Expand Down Expand Up @@ -435,7 +435,7 @@ impl<'a> Iterator for Relocations<'a> {
bytes.len().checked_sub(block_size as usize)?;
self.offset += block_size as usize;

let entries = &bytes[(core::mem::size_of::<u32>() * 2) as usize..block_size as usize];
let entries = &bytes[(core::mem::size_of::<u32>() * 2)..block_size as usize];
Some(Relocation {
page_rva,
block_size,
Expand Down Expand Up @@ -466,10 +466,7 @@ fn reloc_to_base(
.checked_sub(image_base as u64)?
.checked_add(new_image_base as u64)?;
loaded_buffer
.pwrite(
value - image_base as u64 + new_image_base as u64,
location as usize,
)
.pwrite(value - image_base as u64 + new_image_base as u64, location)
.ok()?;
log::trace!(
"reloc {:08x}: {:012x} -> {:012x}",
Expand Down
2 changes: 1 addition & 1 deletion td-logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const SERIAL_IO_PORT: u16 = 0x3F8;

#[cfg(feature = "tdx")]
fn dbg_port_write(byte: u8) {
let _ = tdx_tdcall::tdx::tdvmcall_io_write_8(SERIAL_IO_PORT, byte);
tdx_tdcall::tdx::tdvmcall_io_write_8(SERIAL_IO_PORT, byte);
}

#[cfg(all(not(feature = "tdx"), feature = "serial-port"))]
Expand Down
2 changes: 1 addition & 1 deletion td-paging/src/page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn create_mapping_with_flags(
|| va.as_u64() & (ALIGN_4K - 1) != 0
|| sz & (ALIGN_4K - 1) != 0
|| ps.count_ones() != 1
|| ps < ALIGN_4K as u64
|| ps < ALIGN_4K
{
return Err(Error::InvalidArguments);
}
Expand Down
2 changes: 1 addition & 1 deletion td-payload/src/arch/x86_64/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn encrypt(addr: u64, length: usize) {
if tdx_tdcall::tdx::tdvmcall_mapgpa(false, addr, length).is_err() {
panic!("Fail to map GPA to private memory with TDVMCALL");
}
accept_memory(addr, length as usize);
accept_memory(addr, length);
}

fn accept_memory(addr: u64, length: usize) {
Expand Down
2 changes: 1 addition & 1 deletion td-payload/src/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn get_usable(size: usize) -> Option<u64> {

if entry.r#type == E820Type::Memory as u32 && entry.size >= size as u64 {
entry.size -= size as u64;
return Some(entry.addr + entry.size as u64);
return Some(entry.addr + entry.size);
}
}

Expand Down
2 changes: 1 addition & 1 deletion td-shim/src/acpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Xsdt {
let table_num =
(self.header.length as usize - size_of::<GenericSdtHeader>()) / size_of::<u64>();
if table_num < ACPI_TABLES_MAX_NUM {
self.tables[table_num] = addr as u64;
self.tables[table_num] = addr;
self.header.length += size_of::<u64>() as u32;
Ok(())
} else {
Expand Down
2 changes: 1 addition & 1 deletion td-shim/src/bin/td-shim/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl<'a> Memory<'a> {
false
}

#[cfg(all(feature = "tdx"))]
#[cfg(feature = "tdx")]
/// Build a 2M granularity bitmap for kernel to track the unaccepted memory
pub fn build_unaccepted_memory_bitmap(&self) -> u64 {
#[cfg(not(feature = "lazy-accept"))]
Expand Down
10 changes: 5 additions & 5 deletions td-shim/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl TdxMetadataGuid {
/// * `buffer` - A buffer contains TdxMetadata guid.
pub fn from_bytes(buffer: &[u8; 16]) -> Option<TdxMetadataGuid> {
let guid = Guid::from_bytes(buffer);
let metadata_guid = TdxMetadataGuid { guid: guid };
let metadata_guid = TdxMetadataGuid { guid };
if metadata_guid.is_valid() {
Some(metadata_guid)
} else {
Expand Down Expand Up @@ -428,10 +428,10 @@ pub fn validate_sections(sections: &[TdxMetadataSection]) -> Result<(), TdxMetad
}

//TdInfo. If present, it shall be included in BFV section.
if td_info_cnt != 0 {
if td_info_start < bfv_start || td_info_start >= bfv_end || td_info_end > bfv_end {
return Err(TdxMetadataError::InvalidSection);
}
if td_info_cnt != 0
&& (td_info_start < bfv_start || td_info_start >= bfv_end || td_info_end > bfv_end)
{
return Err(TdxMetadataError::InvalidSection);
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions td-uefi-pi/src/pi/fv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ impl FfsFileHeader {
// Validate the checksum of the FfsFileHeader
pub fn validate_checksum(&self) -> bool {
let sum = sum8(self.as_bytes());
sum ^ ((EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID) as u8
+ FFS_FIXED_CHECKSUM as u8)
sum ^ ((EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID)
+ FFS_FIXED_CHECKSUM)
== 0
}
}
Expand Down
2 changes: 1 addition & 1 deletion td-uefi-pi/src/pi/hob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl MemoryAllocation {
pub fn dump(&self) {
log::info!(
"MemoryAllocation type: 0x{:08x} base: 0x{:016x} length: 0x{:016x}\n",
self.alloc_descriptor.memory_type as u32,
self.alloc_descriptor.memory_type,
self.alloc_descriptor.memory_base_address,
self.alloc_descriptor.memory_length
);
Expand Down
2 changes: 1 addition & 1 deletion tdx-tdcall/src/tdx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ pub fn tdvmcall_cpuid(eax: u32, ecx: u32) -> CpuIdInfo {
pub fn tdvmcall_setup_event_notify(vector: u64) -> Result<(), TdVmcallError> {
let mut args = TdVmcallArgs {
r11: TDVMCALL_SETUPEVENTNOTIFY,
r12: vector as u64,
r12: vector,
..Default::default()
};

Expand Down

0 comments on commit e197fdd

Please sign in to comment.