Skip to content

Commit 6a81068

Browse files
committed
di: Remove the BTF map anonymization fixup
It was never necessary in the first place. There is no place in the kernel that enforces BTF map structs themselves to be anonymous. Only pointer types (that are members of BTF maps) have to be anonymous, but that's handled by a separate fixup. BTF maps work just fine without it.
1 parent 0758647 commit 6a81068

File tree

4 files changed

+4
-163
lines changed

4 files changed

+4
-163
lines changed

src/llvm/di.rs

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ use gimli::{DW_TAG_pointer_type, DW_TAG_structure_type, DW_TAG_variant_part};
1111
use llvm_sys::{core::*, debuginfo::*, prelude::*};
1212
use tracing::{Level, span, trace, warn};
1313

14-
use super::types::{
15-
di::DIType,
16-
ir::{Function, MDNode, Metadata, Value},
17-
};
14+
use super::types::ir::{Function, MDNode, Metadata, Value};
1815
use crate::llvm::{LLVMContext, LLVMModule, iter::*, types::di::DISubprogram};
1916

2017
// KSYM_NAME_LEN from linux kernel intentionally set
@@ -90,8 +87,6 @@ impl<'ctx> DISanitizer<'ctx> {
9087
}
9188

9289
let mut is_data_carrying_enum = false;
93-
let mut remove_name = false;
94-
let mut members: Vec<DIType<'_>> = Vec::new();
9590
for element in di_composite_type.elements() {
9691
match element {
9792
Metadata::DICompositeType(di_composite_type_inner) => {
@@ -123,50 +118,13 @@ impl<'ctx> DISanitizer<'ctx> {
123118
_ => {}
124119
}
125120
}
126-
Metadata::DIDerivedType(di_derived_type) => {
127-
let base_type = di_derived_type.base_type();
128-
129-
match base_type {
130-
Metadata::DICompositeType(base_type_di_composite_type) => {
131-
if let Some(base_type_name) =
132-
base_type_di_composite_type.name()
133-
{
134-
// `AyaBtfMapMarker` is a type which is used in fields of BTF map
135-
// structs. We need to make such structs anonymous in order to get
136-
// BTF maps accepted by the Linux kernel.
137-
if base_type_name == b"AyaBtfMapMarker" {
138-
// Remove the name from the struct.
139-
remove_name = true;
140-
// And don't include the field in the sanitized DI.
141-
} else {
142-
members.push(di_derived_type.into());
143-
}
144-
} else {
145-
members.push(di_derived_type.into());
146-
}
147-
}
148-
_ => {
149-
members.push(di_derived_type.into());
150-
}
151-
}
152-
}
153121
_ => {}
154122
}
155123
}
156124
if is_data_carrying_enum {
157125
di_composite_type.replace_elements(MDNode::empty(self.context));
158-
} else if !members.is_empty() {
159-
members.sort_by_cached_key(|di_type| di_type.offset_in_bits());
160-
let sorted_elements =
161-
MDNode::with_elements(self.context, members.as_mut_slice());
162-
di_composite_type.replace_elements(sorted_elements);
163126
}
164-
if remove_name {
165-
// `AyaBtfMapMarker` is a type which is used in fields of BTF map
166-
// structs. We need to make such structs anonymous in order to get
167-
// BTF maps accepted by the Linux kernel.
168-
di_composite_type.replace_name(self.context, &[])
169-
} else if let Some((_, sanitized_name)) = names {
127+
if let Some((_, sanitized_name)) = names {
170128
// Clear the name from characters incompatible with C.
171129
di_composite_type.replace_name(self.context, sanitized_name.as_slice())
172130
}

src/llvm/types/di.rs

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use llvm_sys::{
55
core::{LLVMGetNumOperands, LLVMGetOperand, LLVMReplaceMDNodeOperandWith, LLVMValueAsMetadata},
66
debuginfo::{
77
LLVMDIFileGetFilename, LLVMDIFlags, LLVMDIScopeGetFile, LLVMDISubprogramGetLine,
8-
LLVMDITypeGetFlags, LLVMDITypeGetLine, LLVMDITypeGetName, LLVMDITypeGetOffsetInBits,
9-
LLVMGetDINodeTag,
8+
LLVMDITypeGetFlags, LLVMDITypeGetLine, LLVMDITypeGetName, LLVMGetDINodeTag,
109
},
1110
prelude::{LLVMContextRef, LLVMMetadataRef, LLVMValueRef},
1211
};
@@ -108,60 +107,6 @@ unsafe fn di_type_name<'a>(metadata_ref: LLVMMetadataRef) -> Option<&'a [u8]> {
108107
(!ptr.is_null()).then(|| unsafe { slice::from_raw_parts(ptr.cast(), len) })
109108
}
110109

111-
/// Represents the debug information for a primitive type in LLVM IR.
112-
pub(crate) struct DIType<'ctx> {
113-
pub(super) metadata_ref: LLVMMetadataRef,
114-
pub(super) value_ref: LLVMValueRef,
115-
_marker: PhantomData<&'ctx ()>,
116-
}
117-
118-
impl DIType<'_> {
119-
/// Constructs a new [`DIType`] from the given `value`.
120-
///
121-
/// # Safety
122-
///
123-
/// This method assumes that the given `value` corresponds to a valid
124-
/// instance of [LLVM `DIType`](https://llvm.org/doxygen/classllvm_1_1DIType.html).
125-
/// It's the caller's responsibility to ensure this invariant, as this
126-
/// method doesn't perform any validation checks.
127-
pub(crate) unsafe fn from_value_ref(value_ref: LLVMValueRef) -> Self {
128-
let metadata_ref = unsafe { LLVMValueAsMetadata(value_ref) };
129-
Self {
130-
metadata_ref,
131-
value_ref,
132-
_marker: PhantomData,
133-
}
134-
}
135-
136-
/// Returns the offset of the type in bits. This offset is used in case the
137-
/// type is a member of a composite type.
138-
pub(crate) fn offset_in_bits(&self) -> u64 {
139-
unsafe { LLVMDITypeGetOffsetInBits(self.metadata_ref) }
140-
}
141-
}
142-
143-
impl<'ctx> From<DIDerivedType<'ctx>> for DIType<'ctx> {
144-
fn from(di_derived_type: DIDerivedType<'_>) -> Self {
145-
unsafe { Self::from_value_ref(di_derived_type.value_ref) }
146-
}
147-
}
148-
149-
/// Represents the operands for a [`DIDerivedType`]. The enum values correspond
150-
/// to the operand indices within metadata nodes.
151-
#[repr(u32)]
152-
enum DIDerivedTypeOperand {
153-
/// [`DIType`] representing a base type of the given derived type.
154-
/// Reference in [LLVM 19-20][llvm-19] and [LLVM 21][llvm-21].
155-
///
156-
/// [llvm-19]: https://github.com/llvm/llvm-project/blob/llvmorg-19.1.7/llvm/include/llvm/IR/DebugInfoMetadata.h#L1084
157-
/// [llvm-21]: https://github.com/llvm/llvm-project/blob/llvmorg-21.1.0-rc3/llvm/include/llvm/IR/DebugInfoMetadata.h#L1386
158-
///
159-
#[cfg(any(feature = "llvm-19", feature = "llvm-20"))]
160-
BaseType = 3,
161-
#[cfg(feature = "llvm-21")]
162-
BaseType = 5,
163-
}
164-
165110
/// Represents the debug information for a derived type in LLVM IR.
166111
///
167112
/// The types derived from other types usually add a level of indirection or an
@@ -191,14 +136,6 @@ impl DIDerivedType<'_> {
191136
}
192137
}
193138

194-
/// Returns the base type of this derived type.
195-
pub(crate) fn base_type(&self) -> Metadata<'_> {
196-
unsafe {
197-
let value = LLVMGetOperand(self.value_ref, DIDerivedTypeOperand::BaseType as u32);
198-
Metadata::from_value_ref(value)
199-
}
200-
}
201-
202139
/// Replaces the name of the type with a new name.
203140
///
204141
/// # Errors

src/llvm/types/ir.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::llvm::{
1919
Message,
2020
iter::IterBasicBlocks as _,
2121
symbol_name,
22-
types::di::{DICompositeType, DIDerivedType, DISubprogram, DIType},
22+
types::di::{DICompositeType, DIDerivedType, DISubprogram},
2323
};
2424

2525
pub(crate) fn replace_name(
@@ -229,26 +229,6 @@ impl MDNode<'_> {
229229
let metadata = unsafe { LLVMMDNodeInContext2(context, core::ptr::null_mut(), 0) };
230230
unsafe { Self::from_metadata_ref(context, metadata) }
231231
}
232-
233-
/// Constructs a new metadata node from an array of [`DIType`] elements.
234-
///
235-
/// This function is used to create composite metadata structures, such as
236-
/// arrays or tuples of different types or values, which can then be used
237-
/// to represent complex data structures within the metadata system.
238-
pub(crate) fn with_elements(context: LLVMContextRef, elements: &[DIType<'_>]) -> Self {
239-
let metadata = unsafe {
240-
let mut elements: Vec<LLVMMetadataRef> = elements
241-
.iter()
242-
.map(|di_type| LLVMValueAsMetadata(di_type.value_ref))
243-
.collect();
244-
LLVMMDNodeInContext2(
245-
context,
246-
elements.as_mut_slice().as_mut_ptr(),
247-
elements.len(),
248-
)
249-
};
250-
unsafe { Self::from_metadata_ref(context, metadata) }
251-
}
252232
}
253233

254234
pub(crate) struct MetadataEntries {

tests/btf/assembly/anon_rust.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)