Browse Source

aya-log-ebpf: simplify argument validation

Tamir Duberstein 1 year ago
parent
commit
6feebef
3 changed files with 31 additions and 68 deletions
  1. 0 25
      aya-log-common/src/lib.rs
  2. 29 39
      aya-log-ebpf-macros/src/expand.rs
  3. 2 4
      bpf/aya-log-ebpf/src/lib.rs

+ 0 - 25
aya-log-common/src/lib.rs

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

+ 29 - 39
aya-log-ebpf-macros/src/expand.rs

@@ -2,9 +2,8 @@ use proc_macro2::TokenStream;
 use quote::quote;
 use syn::{
     parse::{Parse, ParseStream},
-    parse_str,
     punctuated::Punctuated,
-    Error, Expr, ExprCall, LitStr, Result, Token,
+    Error, Expr, LitStr, Result, Token,
 };
 
 use aya_log_common::DisplayHint;
@@ -70,32 +69,6 @@ impl Parse for LogArgs {
     }
 }
 
-fn string_to_expr(s: String) -> Result<Expr> {
-    parse_str(&format!("\"{s}\""))
-}
-
-fn hint_to_expr(hint: DisplayHint) -> Result<Expr> {
-    match hint {
-        DisplayHint::Default => parse_str("::aya_log_ebpf::macro_support::DisplayHint::Default"),
-        DisplayHint::LowerHex => parse_str("::aya_log_ebpf::macro_support::DisplayHint::LowerHex"),
-        DisplayHint::UpperHex => parse_str("::aya_log_ebpf::macro_support::DisplayHint::UpperHex"),
-        DisplayHint::Ip => parse_str("::aya_log_ebpf::macro_support::DisplayHint::Ip"),
-        DisplayHint::LowerMac => parse_str("::aya_log_ebpf::macro_support::DisplayHint::LowerMac"),
-        DisplayHint::UpperMac => parse_str("::aya_log_ebpf::macro_support::DisplayHint::UpperMac"),
-    }
-}
-
-fn hint_to_format_check(hint: DisplayHint) -> Result<Expr> {
-    match hint {
-        DisplayHint::Default => parse_str("::aya_log_ebpf::macro_support::check_impl_default"),
-        DisplayHint::LowerHex => parse_str("::aya_log_ebpf::macro_support::check_impl_lower_hex"),
-        DisplayHint::UpperHex => parse_str("::aya_log_ebpf::macro_support::check_impl_upper_hex"),
-        DisplayHint::Ip => parse_str("::aya_log_ebpf::macro_support::check_impl_ip"),
-        DisplayHint::LowerMac => parse_str("::aya_log_ebpf::macro_support::check_impl_lower_mac"),
-        DisplayHint::UpperMac => parse_str("::aya_log_ebpf::macro_support::check_impl_upper_mac"),
-    }
-}
-
 pub(crate) fn log(args: LogArgs, level: Option<TokenStream>) -> Result<TokenStream> {
     let ctx = args.ctx;
     let target = match args.target {
@@ -127,23 +100,40 @@ pub(crate) fn log(args: LogArgs, level: Option<TokenStream>) -> Result<TokenStre
     let mut values = Vec::new();
     for fragment in fragments {
         match fragment {
-            Fragment::Literal(s) => {
-                values.push(string_to_expr(s)?);
-            }
+            Fragment::Literal(s) => values.push(quote!(#s)),
             Fragment::Parameter(p) => {
                 let arg = match args.formatting_args {
                     Some(ref args) => args[arg_i].clone(),
                     None => return Err(Error::new(format_string.span(), "no arguments provided")),
                 };
-                let hint = hint_to_expr(p.hint)?;
-                let format_check = hint_to_format_check(p.hint)?;
+                let (hint, formatter) = match p.hint {
+                    DisplayHint::Default => {
+                        (quote!(DisplayHint::Default), quote!(DefaultFormatter))
+                    }
+                    DisplayHint::LowerHex => {
+                        (quote!(DisplayHint::LowerHex), quote!(LowerHexFormatter))
+                    }
+                    DisplayHint::UpperHex => {
+                        (quote!(DisplayHint::UpperHex), quote!(UpperHexFormatter))
+                    }
+                    DisplayHint::Ip => (quote!(DisplayHint::Ip), quote!(IpFormatter)),
+                    DisplayHint::LowerMac => {
+                        (quote!(DisplayHint::LowerMac), quote!(LowerMacFormatter))
+                    }
+                    DisplayHint::UpperMac => {
+                        (quote!(DisplayHint::UpperMac), quote!(UpperMacFormatter))
+                    }
+                };
+                let hint = quote!(::aya_log_ebpf::macro_support::#hint);
+                let arg = quote!(
+                    {
+                        let tmp = #arg;
+                        let _: &dyn ::aya_log_ebpf::macro_support::#formatter = &tmp;
+                        tmp
+                    }
+                );
                 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)),
-                }));
+                values.push(arg);
 
                 arg_i += 1;
             }

+ 2 - 4
bpf/aya-log-ebpf/src/lib.rs

@@ -23,10 +23,8 @@ pub static mut AYA_LOGS: PerfEventByteArray = PerfEventByteArray::new(0);
 #[doc(hidden)]
 pub mod macro_support {
     pub use aya_log_common::{
-        check_impl_default, check_impl_ip, check_impl_lower_hex, check_impl_lower_mac,
-        check_impl_upper_hex, check_impl_upper_mac, DefaultFormatter, DisplayHint, IpFormatter,
-        Level, LowerHexFormatter, LowerMacFormatter, UpperHexFormatter, UpperMacFormatter,
-        LOG_BUF_CAPACITY,
+        DefaultFormatter, DisplayHint, IpFormatter, Level, LowerHexFormatter, LowerMacFormatter,
+        UpperHexFormatter, UpperMacFormatter, LOG_BUF_CAPACITY,
     };
     pub use aya_log_ebpf_macros::log;
 }