aya-log-ebpf: avoid requiring Copy

Before this change:
```
error[E0382]: use of moved value: `no_copy`
  --> test/integration-ebpf/src/log.rs:35:9
   |
33 |         let no_copy = NoCopy {};
   |             ------- move occurs because `no_copy` has type `NoCopy`, which does not implement the `Copy` trait
34 |
35 |         debug!(&ctx, "{:x}", no_copy.consume());
   |         ^^^^^^^^^^^^^^^^^^^^^-------^---------^
   |         |                    |       |
   |         |                    |       `no_copy` moved due to this method call
   |         |                    use occurs due to use in closure
   |         value used here after move
   |
note: `NoCopy::consume` takes ownership of the receiver `self`, which moves `no_copy`
  --> test/integration-ebpf/src/log.rs:28:24
   |
28 |             fn consume(self) -> u64 {
   |                        ^^^^
   = note: this error originates in the macro `debug` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0382`.
error: could not compile `integration-ebpf` (bin "log") due to previous error
```
pull/611/head
Tamir Duberstein 1 year ago
parent 85ad0197e0
commit de7972483b
No known key found for this signature in database

@ -86,17 +86,29 @@ pub trait UpperMacFormatter {}
impl UpperMacFormatter for [u8; 6] {} impl UpperMacFormatter for [u8; 6] {}
#[inline(always)] #[inline(always)]
pub fn check_impl_default<T: DefaultFormatter>(_v: T) {} pub fn check_impl_default<T: DefaultFormatter>(t: T) -> T {
t
}
#[inline(always)] #[inline(always)]
pub fn check_impl_lower_hex<T: LowerHexFormatter>(_v: T) {} pub fn check_impl_lower_hex<T: LowerHexFormatter>(t: T) -> T {
t
}
#[inline(always)] #[inline(always)]
pub fn check_impl_upper_hex<T: UpperHexFormatter>(_v: T) {} pub fn check_impl_upper_hex<T: UpperHexFormatter>(t: T) -> T {
t
}
#[inline(always)] #[inline(always)]
pub fn check_impl_ip<T: IpFormatter>(_v: T) {} pub fn check_impl_ip<T: IpFormatter>(t: T) -> T {
t
}
#[inline(always)] #[inline(always)]
pub fn check_impl_lower_mac<T: LowerMacFormatter>(_v: T) {} pub fn check_impl_lower_mac<T: LowerMacFormatter>(t: T) -> T {
t
}
#[inline(always)] #[inline(always)]
pub fn check_impl_upper_mac<T: UpperMacFormatter>(_v: T) {} pub fn check_impl_upper_mac<T: UpperMacFormatter>(t: T) -> T {
t
}
#[repr(u8)] #[repr(u8)]
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]

@ -4,7 +4,7 @@ use syn::{
parse::{Parse, ParseStream}, parse::{Parse, ParseStream},
parse_str, parse_str,
punctuated::Punctuated, punctuated::Punctuated,
Error, Expr, LitStr, Result, Token, Error, Expr, ExprCall, LitStr, Result, Token,
}; };
use aya_log_common::DisplayHint; use aya_log_common::DisplayHint;
@ -125,8 +125,6 @@ pub(crate) fn log(args: LogArgs, level: Option<TokenStream>) -> Result<TokenStre
let mut arg_i = 0; let mut arg_i = 0;
let mut values = Vec::new(); let mut values = Vec::new();
let mut f_keys = Vec::new();
let mut f_values = Vec::new();
for fragment in fragments { for fragment in fragments {
match fragment { match fragment {
Fragment::Literal(s) => { Fragment::Literal(s) => {
@ -137,11 +135,16 @@ pub(crate) fn log(args: LogArgs, level: Option<TokenStream>) -> Result<TokenStre
Some(ref args) => args[arg_i].clone(), Some(ref args) => args[arg_i].clone(),
None => return Err(Error::new(format_string.span(), "no arguments provided")), None => return Err(Error::new(format_string.span(), "no arguments provided")),
}; };
values.push(hint_to_expr(p.hint)?); let hint = hint_to_expr(p.hint)?;
values.push(arg.clone()); let format_check = hint_to_format_check(p.hint)?;
values.push(hint);
values.push(Expr::Call(ExprCall {
attrs: Vec::new(),
func: Box::new(format_check),
paren_token: Default::default(),
args: Punctuated::from_iter(std::iter::once(arg)),
}));
f_keys.push(hint_to_format_check(p.hint)?);
f_values.push(arg.clone());
arg_i += 1; arg_i += 1;
} }
} }
@ -150,15 +153,8 @@ pub(crate) fn log(args: LogArgs, level: Option<TokenStream>) -> Result<TokenStre
let num_args = values.len(); let num_args = values.len();
let values_iter = values.iter(); let values_iter = values.iter();
let f_keys = f_keys.iter();
let f_values = f_values.iter();
Ok(quote! { Ok(quote! {
{ {
#(
#f_keys(#f_values);
)*
if let Some(buf_ptr) = unsafe { ::aya_log_ebpf::AYA_LOG_BUF.get_ptr_mut(0) } { if let Some(buf_ptr) = unsafe { ::aya_log_ebpf::AYA_LOG_BUF.get_ptr_mut(0) } {
let buf = unsafe { &mut *buf_ptr }; let buf = unsafe { &mut *buf_ptr };
if let Ok(header_len) = ::aya_log_ebpf::write_record_header( if let Ok(header_len) = ::aya_log_ebpf::write_record_header(

@ -19,6 +19,21 @@ pub fn test_log(ctx: ProbeContext) {
warn!(&ctx, "hex lc: {:x}, hex uc: {:X}", hex, hex); warn!(&ctx, "hex lc: {:x}, hex uc: {:X}", hex, hex);
let hex = [0xde, 0xad, 0xbe, 0xef].as_slice(); let hex = [0xde, 0xad, 0xbe, 0xef].as_slice();
debug!(&ctx, "hex lc: {:x}, hex uc: {:X}", hex, hex); debug!(&ctx, "hex lc: {:x}, hex uc: {:X}", hex, hex);
// Testing compilation only.
if false {
struct NoCopy {}
impl NoCopy {
fn consume(self) -> u64 {
0xdeadbeef
}
}
let no_copy = NoCopy {};
debug!(&ctx, "{:x}", no_copy.consume());
}
} }
#[panic_handler] #[panic_handler]

Loading…
Cancel
Save