瀏覽代碼

btf: fixup func protos

If an argument has a type, it must also have a name, see btf_func_check
in the kernel.

Given:

SEC("lsm/syslog")
int BPF_PROG(syslog_audit, int type, int ret_prev)
{
        return 0;
}

Fixes:

error: BTF error: the BPF_BTF_LOAD syscall failed. Verifier output: magic: 0xeb9f
version: 1
flags: 0x0
hdr_len: 24
type_off: 0
type_len: 76
str_off: 76
str_len: 128
btf_total_size: 228
[1] FUNC_PROTO (anon) return=2 args=(3 (anon))
[2] INT int size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[3] PTR (anon) type_id=4
[4] INT long long unsigned int size=8 bits_offset=0 nr_bits=64 encoding=(none)
[5] FUNC syslog_audit type_id=1
[5] FUNC syslog_audit type_id=1 Invalid arg#1
: Invalid argument (os error 22)
Alessandro Decina 3 年之前
父節點
當前提交
9ba2e147a1
共有 1 個文件被更改,包括 9 次插入4 次删除
  1. 9 4
      aya/src/obj/btf/btf.rs

+ 9 - 4
aya/src/obj/btf/btf.rs

@@ -15,9 +15,7 @@ use object::Endianness;
 use thiserror::Error;
 
 use crate::{
-    generated::{
-        btf_enum, btf_ext_header, btf_func_linkage, btf_header, btf_member, btf_var_secinfo,
-    },
+    generated::{btf_enum, btf_ext_header, btf_func_linkage, btf_header, btf_member},
     obj::btf::{relocation::Relocation, BtfKind, BtfType},
     util::bytes_of,
     Features,
@@ -504,6 +502,13 @@ impl Btf {
                         }
                     }
                 }
+                BtfType::FuncProto(_ty, params) => {
+                    for (i, mut param) in params.iter_mut().enumerate() {
+                        if param.name_off == 0 && param.type_ != 0 {
+                            param.name_off = self.add_string(format!("param{}", i));
+                        }
+                    }
+                }
                 // The type does not need fixing up
                 _ => {}
             }
@@ -860,7 +865,7 @@ pub(crate) struct SecInfo<'a> {
 
 #[cfg(test)]
 mod tests {
-    use crate::generated::{btf_param, BTF_INT_SIGNED, BTF_VAR_STATIC};
+    use crate::generated::{btf_param, btf_var_secinfo, BTF_INT_SIGNED, BTF_VAR_STATIC};
 
     use super::*;