From de7972483b5b97c073b337568328a022378ebca9 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Fri, 26 May 2023 11:44:02 -0400 Subject: [PATCH] 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 ``` --- aya-log-common/src/lib.rs | 24 ++++++++++++++++++------ aya-log-ebpf-macros/src/expand.rs | 24 ++++++++++-------------- test/integration-ebpf/src/log.rs | 15 +++++++++++++++ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/aya-log-common/src/lib.rs b/aya-log-common/src/lib.rs index b4e0af18..84208303 100644 --- a/aya-log-common/src/lib.rs +++ b/aya-log-common/src/lib.rs @@ -86,17 +86,29 @@ pub trait UpperMacFormatter {} impl UpperMacFormatter for [u8; 6] {} #[inline(always)] -pub fn check_impl_default(_v: T) {} +pub fn check_impl_default(t: T) -> T { + t +} #[inline(always)] -pub fn check_impl_lower_hex(_v: T) {} +pub fn check_impl_lower_hex(t: T) -> T { + t +} #[inline(always)] -pub fn check_impl_upper_hex(_v: T) {} +pub fn check_impl_upper_hex(t: T) -> T { + t +} #[inline(always)] -pub fn check_impl_ip(_v: T) {} +pub fn check_impl_ip(t: T) -> T { + t +} #[inline(always)] -pub fn check_impl_lower_mac(_v: T) {} +pub fn check_impl_lower_mac(t: T) -> T { + t +} #[inline(always)] -pub fn check_impl_upper_mac(_v: T) {} +pub fn check_impl_upper_mac(t: T) -> T { + t +} #[repr(u8)] #[derive(Copy, Clone, Debug)] diff --git a/aya-log-ebpf-macros/src/expand.rs b/aya-log-ebpf-macros/src/expand.rs index 21eab976..d25cf519 100644 --- a/aya-log-ebpf-macros/src/expand.rs +++ b/aya-log-ebpf-macros/src/expand.rs @@ -4,7 +4,7 @@ use syn::{ parse::{Parse, ParseStream}, parse_str, punctuated::Punctuated, - Error, Expr, LitStr, Result, Token, + Error, Expr, ExprCall, LitStr, Result, Token, }; use aya_log_common::DisplayHint; @@ -125,8 +125,6 @@ pub(crate) fn log(args: LogArgs, level: Option) -> Result { @@ -137,11 +135,16 @@ pub(crate) fn log(args: LogArgs, level: Option) -> Result args[arg_i].clone(), None => return Err(Error::new(format_string.span(), "no arguments provided")), }; - values.push(hint_to_expr(p.hint)?); - values.push(arg.clone()); + let hint = hint_to_expr(p.hint)?; + 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; } } @@ -150,15 +153,8 @@ pub(crate) fn log(args: LogArgs, level: Option) -> Result u64 { + 0xdeadbeef + } + } + + let no_copy = NoCopy {}; + + debug!(&ctx, "{:x}", no_copy.consume()); + } } #[panic_handler]