Merge pull request #411 from abhijeetbhagat/fix-warnings

fix all clippy warnings
pull/409/head
Michal Rostecki 2 years ago committed by GitHub
commit 94bc93ea07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -40,9 +40,7 @@ impl<T: Deref<Target = Map>, V: Pod> Array<T, V> {
fn new(map: T) -> Result<Array<T, V>, MapError> { fn new(map: T) -> Result<Array<T, V>, MapError> {
let map_type = map.obj.map_type(); let map_type = map.obj.map_type();
if map_type != BPF_MAP_TYPE_ARRAY as u32 { if map_type != BPF_MAP_TYPE_ARRAY as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
let expected = mem::size_of::<u32>(); let expected = mem::size_of::<u32>();
let size = map.obj.key_size() as usize; let size = map.obj.key_size() as usize;

@ -59,9 +59,7 @@ impl<T: Deref<Target = Map>, V: Pod> PerCpuArray<T, V> {
fn new(map: T) -> Result<PerCpuArray<T, V>, MapError> { fn new(map: T) -> Result<PerCpuArray<T, V>, MapError> {
let map_type = map.obj.map_type(); let map_type = map.obj.map_type();
if map_type != BPF_MAP_TYPE_PERCPU_ARRAY as u32 { if map_type != BPF_MAP_TYPE_PERCPU_ARRAY as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
let expected = mem::size_of::<u32>(); let expected = mem::size_of::<u32>();
let size = map.obj.key_size() as usize; let size = map.obj.key_size() as usize;

@ -57,9 +57,7 @@ impl<T: Deref<Target = Map>> ProgramArray<T> {
fn new(map: T) -> Result<ProgramArray<T>, MapError> { fn new(map: T) -> Result<ProgramArray<T>, MapError> {
let map_type = map.obj.map_type(); let map_type = map.obj.map_type();
if map_type != BPF_MAP_TYPE_PROG_ARRAY as u32 { if map_type != BPF_MAP_TYPE_PROG_ARRAY as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
let expected = mem::size_of::<u32>(); let expected = mem::size_of::<u32>();
let size = map.obj.key_size() as usize; let size = map.obj.key_size() as usize;

@ -44,9 +44,7 @@ impl<T: Deref<Target = Map>, V: Pod> BloomFilter<T, V> {
// validate the map definition // validate the map definition
if map_type != BPF_MAP_TYPE_BLOOM_FILTER as u32 { if map_type != BPF_MAP_TYPE_BLOOM_FILTER as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
let size = mem::size_of::<V>(); let size = mem::size_of::<V>();

@ -44,9 +44,7 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> HashMap<T, K, V> {
// validate the map definition // validate the map definition
if map_type != BPF_MAP_TYPE_HASH as u32 && map_type != BPF_MAP_TYPE_LRU_HASH as u32 { if map_type != BPF_MAP_TYPE_HASH as u32 && map_type != BPF_MAP_TYPE_LRU_HASH as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
hash_map::check_kv_size::<K, V>(&map)?; hash_map::check_kv_size::<K, V>(&map)?;
let _ = map.fd_or_err()?; let _ = map.fd_or_err()?;

@ -56,9 +56,7 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
if map_type != BPF_MAP_TYPE_PERCPU_HASH as u32 if map_type != BPF_MAP_TYPE_PERCPU_HASH as u32
&& map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH as u32 && map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH as u32
{ {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
hash_map::check_kv_size::<K, V>(&map)?; hash_map::check_kv_size::<K, V>(&map)?;
let _ = map.fd_or_err()?; let _ = map.fd_or_err()?;

@ -102,9 +102,7 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> LpmTrie<T, K, V> {
// validate the map definition // validate the map definition
if map_type != BPF_MAP_TYPE_LPM_TRIE as u32 { if map_type != BPF_MAP_TYPE_LPM_TRIE as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
let size = mem::size_of::<Key<K>>(); let size = mem::size_of::<Key<K>>();
let expected = map.obj.key_size() as usize; let expected = map.obj.key_size() as usize;

@ -164,9 +164,7 @@ impl<T: DerefMut<Target = Map>> PerfEventArray<T> {
pub(crate) fn new(map: T) -> Result<PerfEventArray<T>, MapError> { pub(crate) fn new(map: T) -> Result<PerfEventArray<T>, MapError> {
let map_type = map.obj.map_type(); let map_type = map.obj.map_type();
if map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY as u32 { if map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
let _fd = map.fd_or_err()?; let _fd = map.fd_or_err()?;

@ -39,9 +39,7 @@ impl<T: Deref<Target = Map>, V: Pod> Queue<T, V> {
fn new(map: T) -> Result<Queue<T, V>, MapError> { fn new(map: T) -> Result<Queue<T, V>, MapError> {
let map_type = map.obj.map_type(); let map_type = map.obj.map_type();
if map_type != BPF_MAP_TYPE_QUEUE as u32 { if map_type != BPF_MAP_TYPE_QUEUE as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
let expected = 0; let expected = 0;
let size = map.obj.key_size() as usize; let size = map.obj.key_size() as usize;

@ -71,9 +71,7 @@ impl<T: Deref<Target = Map>, K: Pod> SockHash<T, K> {
// validate the map definition // validate the map definition
if map_type != BPF_MAP_TYPE_SOCKHASH as u32 { if map_type != BPF_MAP_TYPE_SOCKHASH as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
hash_map::check_kv_size::<K, u32>(&map)?; hash_map::check_kv_size::<K, u32>(&map)?;
let _ = map.fd_or_err()?; let _ = map.fd_or_err()?;

@ -47,9 +47,7 @@ impl<T: Deref<Target = Map>> SockMap<T> {
fn new(map: T) -> Result<SockMap<T>, MapError> { fn new(map: T) -> Result<SockMap<T>, MapError> {
let map_type = map.obj.map_type(); let map_type = map.obj.map_type();
if map_type != BPF_MAP_TYPE_SOCKMAP as u32 { if map_type != BPF_MAP_TYPE_SOCKMAP as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
let expected = mem::size_of::<u32>(); let expected = mem::size_of::<u32>();
let size = map.obj.key_size() as usize; let size = map.obj.key_size() as usize;

@ -39,9 +39,7 @@ impl<T: Deref<Target = Map>, V: Pod> Stack<T, V> {
fn new(map: T) -> Result<Stack<T, V>, MapError> { fn new(map: T) -> Result<Stack<T, V>, MapError> {
let map_type = map.obj.map_type(); let map_type = map.obj.map_type();
if map_type != BPF_MAP_TYPE_STACK as u32 { if map_type != BPF_MAP_TYPE_STACK as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
let expected = 0; let expected = 0;
let size = map.obj.key_size() as usize; let size = map.obj.key_size() as usize;

@ -72,9 +72,7 @@ impl<T: Deref<Target = Map>> StackTraceMap<T> {
fn new(map: T) -> Result<StackTraceMap<T>, MapError> { fn new(map: T) -> Result<StackTraceMap<T>, MapError> {
let map_type = map.obj.map_type(); let map_type = map.obj.map_type();
if map_type != BPF_MAP_TYPE_STACK_TRACE as u32 { if map_type != BPF_MAP_TYPE_STACK_TRACE as u32 {
return Err(MapError::InvalidMapType { return Err(MapError::InvalidMapType { map_type });
map_type: map_type as u32,
});
} }
let expected = mem::size_of::<u32>(); let expected = mem::size_of::<u32>();
let size = map.obj.key_size() as usize; let size = map.obj.key_size() as usize;

@ -196,7 +196,7 @@ fn relocate_btf_program<'target>(
) -> Result<(), ErrorWrapper> { ) -> Result<(), ErrorWrapper> {
for rel in relos { for rel in relos {
let instructions = &mut program.function.instructions; let instructions = &mut program.function.instructions;
let ins_index = rel.ins_offset as usize / std::mem::size_of::<bpf_insn>(); let ins_index = rel.ins_offset / std::mem::size_of::<bpf_insn>();
if ins_index >= instructions.len() { if ins_index >= instructions.len() {
return Err(RelocationError::InvalidInstructionIndex { return Err(RelocationError::InvalidInstructionIndex {
index: ins_index, index: ins_index,
@ -616,7 +616,7 @@ impl<'a> AccessSpec<'a> {
index: parts[0], index: parts[0],
name: None, name: None,
}]; }];
let mut bit_offset = accessors[0].index as usize * btf.type_size(type_id)?; let mut bit_offset = accessors[0].index * btf.type_size(type_id)?;
for index in parts.iter().skip(1).cloned() { for index in parts.iter().skip(1).cloned() {
type_id = btf.resolve_type(type_id)?; type_id = btf.resolve_type(type_id)?;
let ty = btf.type_by_id(type_id)?; let ty = btf.type_by_id(type_id)?;
@ -770,12 +770,12 @@ impl ComputedRelocation {
) -> Result<(), ErrorWrapper> { ) -> Result<(), ErrorWrapper> {
let instructions = &mut program.function.instructions; let instructions = &mut program.function.instructions;
let num_instructions = instructions.len(); let num_instructions = instructions.len();
let ins_index = rel.ins_offset as usize / std::mem::size_of::<bpf_insn>(); let ins_index = rel.ins_offset / std::mem::size_of::<bpf_insn>();
let mut ins = let mut ins =
instructions instructions
.get_mut(ins_index) .get_mut(ins_index)
.ok_or(RelocationError::InvalidInstructionIndex { .ok_or(RelocationError::InvalidInstructionIndex {
index: rel.ins_offset as usize, index: rel.ins_offset,
num_instructions, num_instructions,
relocation_number: rel.number, relocation_number: rel.number,
})?; })?;

@ -907,7 +907,7 @@ impl BtfType {
pub(crate) unsafe fn read(data: &[u8], endianness: Endianness) -> Result<BtfType, BtfError> { pub(crate) unsafe fn read(data: &[u8], endianness: Endianness) -> Result<BtfType, BtfError> {
let ty = unsafe { read_array::<u32>(data, 3)? }; let ty = unsafe { read_array::<u32>(data, 3)? };
let data = &data[mem::size_of::<u32>() * 3..]; let data = &data[mem::size_of::<u32>() * 3..];
let vlen = type_vlen(ty[1]) as usize; let vlen = type_vlen(ty[1]);
Ok(match type_kind(ty[1])? { Ok(match type_kind(ty[1])? {
BtfKind::Unknown => BtfType::Unknown, BtfKind::Unknown => BtfType::Unknown,
BtfKind::Fwd => BtfType::Fwd(Fwd { BtfKind::Fwd => BtfType::Fwd(Fwd {

@ -741,7 +741,7 @@ impl Object {
let mut line_info = btf_ext.line_info.get(section.name); let mut line_info = btf_ext.line_info.get(section.name);
line_info.line_info.retain(|l| { line_info.line_info.retain(|l| {
l.insn_off >= bytes_offset l.insn_off >= bytes_offset
&& l.insn_off < (bytes_offset + section_size_bytes) as u32 && l.insn_off < (bytes_offset + section_size_bytes)
}); });
( (
@ -1144,7 +1144,7 @@ fn get_map_field(btf: &Btf, type_id: u32) -> Result<u32, BtfError> {
BtfType::Ptr(pty) => pty, BtfType::Ptr(pty) => pty,
other => { other => {
return Err(BtfError::UnexpectedBtfType { return Err(BtfError::UnexpectedBtfType {
type_id: other.btf_type().unwrap_or(0) as u32, type_id: other.btf_type().unwrap_or(0),
}) })
} }
}; };
@ -1153,7 +1153,7 @@ fn get_map_field(btf: &Btf, type_id: u32) -> Result<u32, BtfError> {
BtfType::Array(Array { array, .. }) => array, BtfType::Array(Array { array, .. }) => array,
other => { other => {
return Err(BtfError::UnexpectedBtfType { return Err(BtfError::UnexpectedBtfType {
type_id: other.btf_type().unwrap_or(0) as u32, type_id: other.btf_type().unwrap_or(0),
}) })
} }
}; };
@ -1231,7 +1231,7 @@ fn parse_btf_map_def(btf: &Btf, info: &DataSecEntry) -> Result<(String, BtfMapDe
BtfType::Var(var) => var, BtfType::Var(var) => var,
other => { other => {
return Err(BtfError::UnexpectedBtfType { return Err(BtfError::UnexpectedBtfType {
type_id: other.btf_type().unwrap_or(0) as u32, type_id: other.btf_type().unwrap_or(0),
}) })
} }
}; };
@ -1244,7 +1244,7 @@ fn parse_btf_map_def(btf: &Btf, info: &DataSecEntry) -> Result<(String, BtfMapDe
BtfType::Struct(s) => s, BtfType::Struct(s) => s,
other => { other => {
return Err(BtfError::UnexpectedBtfType { return Err(BtfError::UnexpectedBtfType {
type_id: other.btf_type().unwrap_or(0) as u32, type_id: other.btf_type().unwrap_or(0),
}) })
} }
}; };

@ -90,7 +90,7 @@ pub(crate) unsafe fn netlink_qdisc_add_clsact(if_index: i32) -> Result<(), io::E
// add the TCA_KIND attribute // add the TCA_KIND attribute
let attrs_buf = request_attributes(&mut req, nlmsg_len); let attrs_buf = request_attributes(&mut req, nlmsg_len);
let attr_len = write_attr_bytes(attrs_buf, 0, TCA_KIND as u16, b"clsact\0")?; let attr_len = write_attr_bytes(attrs_buf, 0, TCA_KIND as u16, b"clsact\0")?;
req.header.nlmsg_len += align_to(attr_len as usize, NLA_ALIGNTO as usize) as u32; req.header.nlmsg_len += align_to(attr_len, NLA_ALIGNTO as usize) as u32;
sock.send(&bytes_of(&req)[..req.header.nlmsg_len as usize])?; sock.send(&bytes_of(&req)[..req.header.nlmsg_len as usize])?;
sock.recv()?; sock.recv()?;
@ -135,7 +135,7 @@ pub(crate) unsafe fn netlink_qdisc_attach(
options.write_attr(TCA_BPF_FLAGS as u16, flags)?; options.write_attr(TCA_BPF_FLAGS as u16, flags)?;
let options_len = options.finish()?; let options_len = options.finish()?;
req.header.nlmsg_len += align_to(kind_len + options_len as usize, NLA_ALIGNTO as usize) as u32; req.header.nlmsg_len += align_to(kind_len + options_len, NLA_ALIGNTO as usize) as u32;
sock.send(&bytes_of(&req)[..req.header.nlmsg_len as usize])?; sock.send(&bytes_of(&req)[..req.header.nlmsg_len as usize])?;
// find the RTM_NEWTFILTER reply and read the tcm_info field which we'll // find the RTM_NEWTFILTER reply and read the tcm_info field which we'll
@ -440,7 +440,7 @@ impl<'a> NestedAttrs<'a> {
fn finish(self) -> Result<usize, io::Error> { fn finish(self) -> Result<usize, io::Error> {
let nla_len = self.offset; let nla_len = self.offset;
let attr = nlattr { let attr = nlattr {
nla_type: NLA_F_NESTED as u16 | self.top_attr_type as u16, nla_type: NLA_F_NESTED as u16 | self.top_attr_type,
nla_len: nla_len as u16, nla_len: nla_len as u16,
}; };
@ -467,7 +467,7 @@ fn write_attr_bytes(
value: &[u8], value: &[u8],
) -> Result<usize, io::Error> { ) -> Result<usize, io::Error> {
let attr = nlattr { let attr = nlattr {
nla_type: attr_type as u16, nla_type: attr_type,
nla_len: ((NLA_HDR_LEN + value.len()) as u16), nla_len: ((NLA_HDR_LEN + value.len()) as u16),
}; };
@ -575,10 +575,7 @@ impl From<NlAttrError> for io::Error {
} }
unsafe fn request_attributes<T>(req: &mut T, msg_len: usize) -> &mut [u8] { unsafe fn request_attributes<T>(req: &mut T, msg_len: usize) -> &mut [u8] {
let attrs_addr = align_to( let attrs_addr = align_to(req as *const _ as usize + msg_len, NLMSG_ALIGNTO as usize);
req as *const _ as usize + msg_len as usize,
NLMSG_ALIGNTO as usize,
);
let attrs_end = req as *const _ as usize + mem::size_of::<T>(); let attrs_end = req as *const _ as usize + mem::size_of::<T>();
slice::from_raw_parts_mut(attrs_addr as *mut u8, attrs_end - attrs_addr) slice::from_raw_parts_mut(attrs_addr as *mut u8, attrs_end - attrs_addr)
} }

@ -1,6 +1,5 @@
//! Utility functions. //! Utility functions.
use std::{ use std::{
cmp,
collections::BTreeMap, collections::BTreeMap,
ffi::{CStr, CString}, ffi::{CStr, CString},
fs::{self, File}, fs::{self, File},
@ -173,10 +172,7 @@ impl VerifierLog {
} }
pub(crate) fn grow(&mut self) { pub(crate) fn grow(&mut self) {
let len = cmp::max( let len = (self.buf.capacity() * 10).clamp(MIN_LOG_BUF_SIZE, MAX_LOG_BUF_SIZE);
MIN_LOG_BUF_SIZE,
cmp::min(MAX_LOG_BUF_SIZE, self.buf.capacity() * 10),
);
self.buf.resize(len, 0); self.buf.resize(len, 0);
self.reset(); self.reset();
} }

@ -279,7 +279,7 @@ pub unsafe fn bpf_probe_read_str(src: *const u8, dest: &mut [u8]) -> Result<usiz
// bounded // bounded
len = dest.len(); len = dest.len();
} }
Ok(len as usize) Ok(len)
} }
/// Read a null-terminated string from _user space_ stored at `src` into `dest`. /// Read a null-terminated string from _user space_ stored at `src` into `dest`.
@ -323,7 +323,7 @@ pub unsafe fn bpf_probe_read_user_str(src: *const u8, dest: &mut [u8]) -> Result
// bounded // bounded
len = dest.len(); len = dest.len();
} }
Ok(len as usize) Ok(len)
} }
/// Returns a byte slice read from _user space_ address `src`. /// Returns a byte slice read from _user space_ address `src`.
@ -474,7 +474,7 @@ pub unsafe fn bpf_probe_read_kernel_str(src: *const u8, dest: &mut [u8]) -> Resu
// bounded // bounded
len = dest.len(); len = dest.len();
} }
Ok(len as usize) Ok(len)
} }
/// Returns a byte slice read from _kernel space_ address `src`. /// Returns a byte slice read from _kernel space_ address `src`.

@ -28,7 +28,7 @@ impl SkMsgContext {
} }
pub fn push_data(&self, start: u32, len: u32, flags: u64) -> Result<(), i64> { pub fn push_data(&self, start: u32, len: u32, flags: u64) -> Result<(), i64> {
let ret = unsafe { bpf_msg_push_data(self.msg, start, len as u32, flags) }; let ret = unsafe { bpf_msg_push_data(self.msg, start, len, flags) };
if ret == 0 { if ret == 0 {
Ok(()) Ok(())
} else { } else {
@ -37,7 +37,7 @@ impl SkMsgContext {
} }
pub fn pop_data(&self, start: u32, len: u32, flags: u64) -> Result<(), i64> { pub fn pop_data(&self, start: u32, len: u32, flags: u64) -> Result<(), i64> {
let ret = unsafe { bpf_msg_pop_data(self.msg, start, len as u32, flags) }; let ret = unsafe { bpf_msg_pop_data(self.msg, start, len, flags) };
if ret == 0 { if ret == 0 {
Ok(()) Ok(())
} else { } else {

Loading…
Cancel
Save