Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ missing_debug_implementations = "warn"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(kani)'] }

[workspace.lints.clippy]
ptr_as_ptr = "warn"
as_conversions = "warn"
undocumented_unsafe_blocks = "warn"
cast_possible_truncation = "warn"
cast_possible_wrap = "warn"
cast_sign_loss = "warn"
exit = "warn"
tests_outside_test_module = "warn"
assertions_on_result_states = "warn"
Expand Down
104 changes: 76 additions & 28 deletions src/acpi-tables/src/aml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl Aml for Usize {
TryInto::<u16>::try_into(*self)
.unwrap()
.append_aml_bytes(bytes)
} else if *self <= u32::MAX as usize {
} else if *self <= u32::MAX.try_into().unwrap_or(usize::MAX) {
TryInto::<u32>::try_into(*self)
.unwrap()
.append_aml_bytes(bytes)
Expand Down Expand Up @@ -407,6 +407,16 @@ enum AddressSpaceType {
BusNumber,
}

impl From<AddressSpaceType> for u8 {
fn from(val: AddressSpaceType) -> u8 {
match val {
AddressSpaceType::Memory => 0,
AddressSpaceType::Io => 1,
AddressSpaceType::BusNumber => 2,
}
}
}

#[derive(Copy, Clone)]
pub enum AddressSpaceCacheable {
NotCacheable,
Expand All @@ -415,6 +425,17 @@ pub enum AddressSpaceCacheable {
PreFetchable,
}

impl From<AddressSpaceCacheable> for u8 {
fn from(val: AddressSpaceCacheable) -> u8 {
match val {
AddressSpaceCacheable::NotCacheable => 0,
AddressSpaceCacheable::Cacheable => 1,
AddressSpaceCacheable::WriteCombining => 2,
AddressSpaceCacheable::PreFetchable => 3,
}
}
}

pub struct AddressSpace<T> {
r#type: AddressSpaceType,
min: T,
Expand All @@ -439,7 +460,7 @@ where
r#type: AddressSpaceType::Memory,
min,
max,
type_flags: ((cacheable as u8) << 1) | u8::from(read_write),
type_flags: ((u8::from(cacheable)) << 1) | u8::from(read_write),
})
}

Expand Down Expand Up @@ -470,7 +491,7 @@ where
fn push_header(&self, bytes: &mut Vec<u8>, descriptor: u8, length: usize) {
bytes.push(descriptor); // Word Address Space Descriptor
bytes.extend_from_slice(&(TryInto::<u16>::try_into(length).unwrap()).to_le_bytes());
bytes.push(self.r#type as u8); // type
bytes.push(u8::from(self.r#type)); // type
let generic_flags = (1 << 2) /* Min Fixed */ | (1 << 3); // Max Fixed
bytes.push(generic_flags);
bytes.push(self.type_flags);
Expand Down Expand Up @@ -725,13 +746,35 @@ pub enum FieldAccessType {
Buffer,
}

impl From<FieldAccessType> for u8 {
fn from(val: FieldAccessType) -> u8 {
match val {
FieldAccessType::Any => 0,
FieldAccessType::Byte => 1,
FieldAccessType::Word => 2,
FieldAccessType::DWord => 3,
FieldAccessType::QWord => 4,
FieldAccessType::Buffer => 5,
}
}
}

#[derive(Clone, Copy)]
pub enum FieldUpdateRule {
Preserve = 0,
WriteAsOnes = 1,
WriteAsZeroes = 2,
}

impl From<FieldUpdateRule> for u8 {
fn from(val: FieldUpdateRule) -> u8 {
#[allow(clippy::as_conversions)]
{
val as u8
}
}
}

pub enum FieldEntry {
Named([u8; 4], usize),
Reserved(usize),
Expand Down Expand Up @@ -766,7 +809,7 @@ impl Aml for Field {
let mut tmp = Vec::new();
self.path.append_aml_bytes(&mut tmp)?;

let flags: u8 = self.access_type as u8 | ((self.update_rule as u8) << 5);
let flags: u8 = u8::from(self.access_type) | ((u8::from(self.update_rule)) << 5);
tmp.push(flags);

for field in self.fields.iter() {
Expand Down Expand Up @@ -806,6 +849,23 @@ pub enum OpRegionSpace {
GenericSerialBus,
}

impl From<OpRegionSpace> for u8 {
fn from(val: OpRegionSpace) -> u8 {
match val {
OpRegionSpace::SystemMemory => 0,
OpRegionSpace::SystemIo => 1,
OpRegionSpace::PConfig => 2,
OpRegionSpace::EmbeddedControl => 3,
OpRegionSpace::Smbus => 4,
OpRegionSpace::SystemCmos => 5,
OpRegionSpace::PciBarTarget => 6,
OpRegionSpace::Ipmi => 7,
OpRegionSpace::GeneralPurposeIo => 8,
OpRegionSpace::GenericSerialBus => 9,
}
}
}

pub struct OpRegion {
path: Path,
space: OpRegionSpace,
Expand All @@ -829,7 +889,7 @@ impl Aml for OpRegion {
bytes.push(0x5b); // ExtOpPrefix
bytes.push(0x80); // OpRegionOp
self.path.append_aml_bytes(bytes)?;
bytes.push(self.space as u8);
bytes.push(u8::from(self.space));
self.offset.append_aml_bytes(bytes)?; // RegionOffset
self.length.append_aml_bytes(bytes)?; // RegionLen
Ok(())
Expand Down Expand Up @@ -1500,7 +1560,7 @@ mod tests {
assert_eq!(
create_pkg_length(&[0u8; 4096], true),
vec![
(2 << 6) | (4099 & 0xf) as u8,
u8::try_from((2 << 6) | (4099 & 0xf)).unwrap(),
((4099 >> 4) & 0xff).try_into().unwrap(),
((4099 >> 12) & 0xff).try_into().unwrap()
]
Expand Down Expand Up @@ -1535,28 +1595,18 @@ mod tests {
}
#[test]
fn test_name_path() {
let path: &Path = &"_SB_".try_into().unwrap();
assert_eq!(path.to_aml_bytes().unwrap(), [0x5Fu8, 0x53, 0x42, 0x5F]);
let path: &Path = &"\\_SB_".try_into().unwrap();
assert_eq!(path.to_aml_bytes().unwrap(), [0x5C, 0x5F, 0x53, 0x42, 0x5F]);
let path: &Path = &"_SB_.COM1".try_into().unwrap();
assert_eq!(
(&"_SB_".try_into().unwrap() as &Path)
.to_aml_bytes()
.unwrap(),
[0x5Fu8, 0x53, 0x42, 0x5F]
);
assert_eq!(
(&"\\_SB_".try_into().unwrap() as &Path)
.to_aml_bytes()
.unwrap(),
[0x5C, 0x5F, 0x53, 0x42, 0x5F]
);
assert_eq!(
(&"_SB_.COM1".try_into().unwrap() as &Path)
.to_aml_bytes()
.unwrap(),
path.to_aml_bytes().unwrap(),
[0x2E, 0x5F, 0x53, 0x42, 0x5F, 0x43, 0x4F, 0x4D, 0x31]
);
let path: &Path = &"_SB_.PCI0._HID".try_into().unwrap();
assert_eq!(
(&"_SB_.PCI0._HID".try_into().unwrap() as &Path)
.to_aml_bytes()
.unwrap(),
path.to_aml_bytes().unwrap(),
[
0x2F, 0x03, 0x5F, 0x53, 0x42, 0x5F, 0x50, 0x43, 0x49, 0x30, 0x5F, 0x48, 0x49, 0x44
]
Expand Down Expand Up @@ -1599,10 +1649,8 @@ mod tests {

#[test]
fn test_string() {
assert_eq!(
(&"ACPI" as &dyn Aml).to_aml_bytes().unwrap(),
[0x0d, b'A', b'C', b'P', b'I', 0]
);
let s: &dyn Aml = &"ACPI";
assert_eq!(s.to_aml_bytes().unwrap(), [0x0d, b'A', b'C', b'P', b'I', 0]);
assert_eq!(
"ACPI".to_owned().to_aml_bytes().unwrap(),
[0x0d, b'A', b'C', b'P', b'I', 0]
Expand Down
4 changes: 2 additions & 2 deletions src/acpi-tables/src/dsdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ impl Dsdt {

impl Sdt for Dsdt {
fn len(&self) -> usize {
self.header.length.get() as usize
usize::try_from(self.header.length.get()).unwrap()
}

fn write_to_guest<AS: GuestMemory>(&mut self, mem: &AS, address: GuestAddress) -> Result<()> {
mem.write_slice(self.header.as_bytes(), address)?;
let address = address
.checked_add(size_of::<SdtHeader>() as u64)
.checked_add(u64::try_from(size_of::<SdtHeader>()).unwrap())
.ok_or(AcpiError::InvalidGuestAddress)?;
mem.write_slice(self.definition_block.as_slice(), address)?;

Expand Down
2 changes: 1 addition & 1 deletion src/acpi-tables/src/madt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Sdt for Madt {
fn write_to_guest<M: GuestMemory>(&mut self, mem: &M, address: GuestAddress) -> Result<()> {
mem.write_slice(self.header.as_bytes(), address)?;
let address = address
.checked_add(size_of::<MadtHeader>() as u64)
.checked_add(u64::try_from(size_of::<MadtHeader>()).unwrap())
.ok_or(AcpiError::InvalidGuestAddress)?;
mem.write_slice(self.interrupt_controllers.as_bytes(), address)?;

Expand Down
2 changes: 1 addition & 1 deletion src/acpi-tables/src/xsdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Sdt for Xsdt {
fn write_to_guest<M: GuestMemory>(&mut self, mem: &M, address: GuestAddress) -> Result<()> {
mem.write_slice(self.header.as_bytes(), address)?;
let address = address
.checked_add(size_of::<SdtHeader>() as u64)
.checked_add(u64::try_from(size_of::<SdtHeader>()).unwrap())
.ok_or(AcpiError::InvalidGuestAddress)?;
mem.write_slice(self.tables.as_slice(), address)?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/cpu-template-helper/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn main() {
// SAFETY: This is safe as long as `header` is valid as `KernelHeader`.
let header_bytes = unsafe {
std::slice::from_raw_parts(
(&header as *const KernelHeader).cast::<u8>(),
std::ptr::from_ref::<KernelHeader>(&header).cast::<u8>(),
std::mem::size_of::<KernelHeader>(),
)
};
Expand Down
2 changes: 2 additions & 0 deletions src/cpu-template-helper/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

#![allow(clippy::as_conversions)]

use std::ffi::OsString;
use std::fmt::Display;
use std::fs::read_to_string;
Expand Down
2 changes: 2 additions & 0 deletions src/firecracker/examples/uffd/fault_all_handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

#![allow(clippy::as_conversions)]

//! Provides functionality for a userspace page fault handler
//! which loads the whole region from the backing memory file
//! when a page fault occurs.
Expand Down
1 change: 1 addition & 0 deletions src/firecracker/examples/uffd/uffd_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::undocumented_unsafe_blocks,
clippy::as_conversions,
// Not everything is used by both binaries
dead_code
)]
Expand Down
2 changes: 2 additions & 0 deletions src/firecracker/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

#![allow(clippy::as_conversions)]

mod api_server;
mod api_server_adapter;
mod generated;
Expand Down
10 changes: 5 additions & 5 deletions src/jailer/src/resource_limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ impl From<Resource> for u32 {
fn from(resource: Resource) -> u32 {
match resource {
#[allow(clippy::unnecessary_cast)]
#[allow(clippy::cast_possible_wrap)]
#[allow(clippy::cast_possible_wrap, clippy::as_conversions)]
// Definition of libc::RLIMIT_FSIZE depends on the target_env:
// * when equals to "musl" -> libc::RLIMIT_FSIZE is a c_int (which is an i32)
// * when equals to "gnu" -> libc::RLIMIT_FSIZE is __rlimit_resource_t which is a
// c_uint (which is an u32)
Resource::RlimitFsize => libc::RLIMIT_FSIZE as u32,
#[allow(clippy::unnecessary_cast)]
#[allow(clippy::unnecessary_cast, clippy::as_conversions)]
#[allow(clippy::cast_possible_wrap)]
// Definition of libc::RLIMIT_NOFILE depends on the target_env:
// * when equals to "musl" -> libc::RLIMIT_NOFILE is a c_int (which is an i32)
Expand All @@ -47,14 +47,14 @@ impl From<Resource> for u32 {
impl From<Resource> for i32 {
fn from(resource: Resource) -> i32 {
match resource {
#[allow(clippy::unnecessary_cast)]
#[allow(clippy::unnecessary_cast, clippy::as_conversions)]
#[allow(clippy::cast_possible_wrap)]
// Definition of libc::RLIMIT_FSIZE depends on the target_env:
// * when equals to "musl" -> libc::RLIMIT_FSIZE is a c_int (which is an i32)
// * when equals to "gnu" -> libc::RLIMIT_FSIZE is __rlimit_resource_t which is a
// c_uint (which is an u32)
Resource::RlimitFsize => libc::RLIMIT_FSIZE as i32,
#[allow(clippy::unnecessary_cast)]
#[allow(clippy::unnecessary_cast, clippy::as_conversions)]
#[allow(clippy::cast_possible_wrap)]
// Definition of libc::RLIMIT_NOFILE depends on the target_env:
// * when equals to "musl" -> libc::RLIMIT_NOFILE is a c_int (which is an i32)
Expand Down Expand Up @@ -129,7 +129,7 @@ mod tests {
use super::*;

#[test]
#[allow(clippy::unnecessary_cast)]
#[allow(clippy::unnecessary_cast, clippy::as_conversions)]
fn test_from_resource() {
assert_eq!(u32::from(Resource::RlimitFsize), libc::RLIMIT_FSIZE as u32);
assert_eq!(
Expand Down
3 changes: 2 additions & 1 deletion src/rebase-snap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn rebase(base_file: &mut File, diff_file: &mut File) -> Result<(), FileError> {
libc::sendfile64(
base_file.as_raw_fd(),
diff_file.as_raw_fd(),
(&mut cursor as *mut u64).cast::<i64>(),
std::ptr::from_mut::<u64>(&mut cursor).cast::<i64>(),
usize::try_from(block_end.saturating_sub(cursor)).unwrap(),
)
};
Expand Down Expand Up @@ -258,6 +258,7 @@ mod tests {
.unwrap();

// 2. Diff file that has only holes
#[allow(clippy::as_conversions)]
diff_file
.set_len(initial_base_file_content.len() as u64)
.unwrap();
Expand Down
6 changes: 4 additions & 2 deletions src/seccompiler/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ pub const SCMP_ACT_TRAP: u32 = 0x00030000;
pub const SCMP_ACT_ERRNO_MASK: u32 = 0x00050000;
/// Return the specified error code
#[must_use]
#[allow(clippy::as_conversions)]
pub const fn SCMP_ACT_ERRNO(x: u16) -> u32 {
SCMP_ACT_ERRNO_MASK | x as u32
SCMP_ACT_ERRNO_MASK | (x as u32)
}
pub const SCMP_ACT_TRACE_MASK: u32 = 0x7ff00000;
/// Notify a tracing process with the specified value
#[must_use]
#[allow(clippy::as_conversions)]
pub const fn SCMP_ACT_TRACE(x: u16) -> u32 {
SCMP_ACT_TRACE_MASK | x as u32
SCMP_ACT_TRACE_MASK | (x as u32)
}
/// Allow the syscall to be executed after the action has been logged
pub const SCMP_ACT_LOG: u32 = 0x7ffc0000;
Expand Down
4 changes: 2 additions & 2 deletions src/seccompiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn compile_bpf(
// SAFETY: Safe as all args are correct.
// We can assume no one will define u32::MAX
// filters for a syscall.
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_possible_truncation, clippy::as_conversions)]
unsafe {
if seccomp_rule_add_array(
bpf_filter,
Expand Down Expand Up @@ -173,7 +173,7 @@ pub fn compile_bpf(
memfd.rewind().map_err(CompilationError::MemfdRewind)?;

// Cast is safe because usize == u64
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_possible_truncation, clippy::as_conversions)]
let size = memfd.metadata().unwrap().size() as usize;
// Bpf instructions are 8 byte values and 4 byte alignment.
// We use u64 to satisfy these requirements.
Expand Down
Loading