Browse Source

Minor tweaks to make the verifier's job easier

Alessandro Decina 3 years ago
parent
commit
2ac4334
2 changed files with 13 additions and 3 deletions
  1. 9 2
      aya-log/aya-log-ebpf/src/lib.rs
  2. 4 1
      aya-log/aya-log-ebpf/src/macros.rs

+ 9 - 2
aya-log/aya-log-ebpf/src/lib.rs

@@ -11,7 +11,8 @@ use aya_bpf::{
     maps::{PerCpuArray, PerfEventByteArray},
 };
 pub use aya_log_common::Level;
-use aya_log_common::{RecordField, LOG_BUF_CAPACITY};
+use aya_log_common::RecordField;
+pub use aya_log_common::LOG_BUF_CAPACITY;
 
 #[doc(hidden)]
 #[repr(C)]
@@ -59,12 +60,18 @@ impl<'a> ufmt::uWrite for LogBufWriter<'a> {
     fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
         let bytes = s.as_bytes();
         let len = bytes.len();
+
+        // this is to make sure the verifier knows about the upper bound
+        if len > LOG_BUF_CAPACITY {
+            return Err(());
+        }
+
         let available = self.data.len() - self.pos;
         if available < len {
             return Err(());
         }
 
-        self.data[self.pos..self.pos + len].copy_from_slice(bytes);
+        self.data[self.pos..self.pos + len].copy_from_slice(&bytes[..len]);
         self.pos += len;
         Ok(())
     }

+ 4 - 1
aya-log/aya-log-ebpf/src/macros.rs

@@ -183,7 +183,10 @@ macro_rules! log {
         if let Some(buf) = unsafe { $crate::AYA_LOG_BUF.get_mut(0) } {
             if let Ok(header_len) = $crate::write_record_header(&mut buf.buf, module_path!(), $lvl, module_path!(), file!(), line!()) {
                 if let Ok(message_len) = $crate::write_record_message!(&mut buf.buf[header_len..], $($arg)+) {
-                    let _ = unsafe { $crate::AYA_LOGS.output($ctx, &buf.buf[..header_len + message_len], 0) };
+                    let record_len = header_len + message_len;
+                    if record_len <= $crate::LOG_BUF_CAPACITY {
+                        let _ = unsafe { $crate::AYA_LOGS.output($ctx, &buf.buf[..header_len + message_len], 0) };
+                    }
                 };
             }
         }