Fixed BTF linkage of memset and memcpy to static

Signed-off-by: Quentin JEROME <qjerome@users.noreply.github.com>
pull/583/head
Quentin JEROME 2 years ago
parent 542ada3fe7
commit 4e41da6a86

@ -535,24 +535,44 @@ impl Btf {
let enum_type = BtfType::Enum(Enum::new(ty.name_offset, members)); let enum_type = BtfType::Enum(Enum::new(ty.name_offset, members));
types.types[i] = enum_type; types.types[i] = enum_type;
} }
// Sanitize FUNC // Sanitize FUNC
BtfType::Func(ty) if !features.btf_func => { BtfType::Func(ty) => {
debug!("{}: not supported. replacing with TYPEDEF", kind); let name = self.string_at(ty.name_offset)?;
let typedef_type = BtfType::Typedef(Typedef::new(ty.name_offset, ty.btf_type)); // Sanitize FUNC
types.types[i] = typedef_type; if !features.btf_func {
} debug!("{}: not supported. replacing with TYPEDEF", kind);
// Sanitize BTF_FUNC_GLOBAL let typedef_type =
BtfType::Func(ty) if !features.btf_func_global => { BtfType::Typedef(Typedef::new(ty.name_offset, ty.btf_type));
let mut fixed_ty = ty.clone(); types.types[i] = typedef_type;
if ty.linkage() == FuncLinkage::Global { } else if !features.btf_func_global || name == "memset" || name == "memcpy" {
debug!( // Sanitize BTF_FUNC_GLOBAL and memset, memcpy
"{}: BTF_FUNC_GLOBAL not supported. replacing with BTF_FUNC_STATIC", let mut fixed_ty = ty.clone();
kind if ty.linkage() == FuncLinkage::Global {
); debug!(
fixed_ty.set_linkage(FuncLinkage::Static); "{}: BTF_FUNC_GLOBAL not supported. replacing with BTF_FUNC_STATIC",
kind
);
fixed_ty.set_linkage(FuncLinkage::Static);
}
types.types[i] = BtfType::Func(fixed_ty);
} }
types.types[i] = BtfType::Func(fixed_ty);
// Sanitize BTF_FUNC_GLOBAL and memset, memcpy
/*let name = self.string_at(ty.name_offset)?;
if !features.btf_func_global || name == "memset" || name == "memcpy" {
let mut fixed_ty = ty.clone();
if ty.linkage() == FuncLinkage::Global {
debug!(
"{}: BTF_FUNC_GLOBAL not supported. replacing with BTF_FUNC_STATIC",
kind
);
fixed_ty.set_linkage(FuncLinkage::Static);
}
types.types[i] = BtfType::Func(fixed_ty);
}*/
} }
// Sanitize FLOAT // Sanitize FLOAT
BtfType::Float(ty) if !features.btf_float => { BtfType::Float(ty) if !features.btf_float => {
debug!("{}: not supported. replacing with STRUCT", kind); debug!("{}: not supported. replacing with STRUCT", kind);
@ -1350,6 +1370,59 @@ mod tests {
Btf::parse(&raw, Endianness::default()).unwrap(); Btf::parse(&raw, Endianness::default()).unwrap();
} }
#[test]
fn test_sanitize_memset_memcpy() {
let mut btf = Btf::new();
let name_offset = btf.add_string("int".to_string());
let int_type_id = btf.add_type(BtfType::Int(Int::new(
name_offset,
4,
IntEncoding::Signed,
0,
)));
let params = vec![
BtfParam {
name_offset: btf.add_string("a".to_string()),
btf_type: int_type_id,
},
BtfParam {
name_offset: btf.add_string("b".to_string()),
btf_type: int_type_id,
},
];
let func_proto_type_id =
btf.add_type(BtfType::FuncProto(FuncProto::new(params, int_type_id)));
["memset", "memcpy"].iter().for_each(|fname| {
let func_name_offset = btf.add_string(fname.to_string());
let func_type_id = btf.add_type(BtfType::Func(Func::new(
func_name_offset,
func_proto_type_id,
FuncLinkage::Global,
)));
let features = BtfFeatures {
btf_func: true,
btf_func_global: true, // to force function name check
..Default::default()
};
btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features)
.unwrap();
if let BtfType::Func(fixed) = btf.type_by_id(func_type_id).unwrap() {
assert!(fixed.linkage() == FuncLinkage::Static);
} else {
panic!("not a func")
}
// Ensure we can convert to bytes and back again
let raw = btf.to_bytes();
Btf::parse(&raw, Endianness::default()).unwrap();
});
}
#[test] #[test]
fn test_sanitize_float() { fn test_sanitize_float() {
let mut btf = Btf::new(); let mut btf = Btf::new();

Loading…
Cancel
Save