Skip to content

Commit 0e4c970

Browse files
committed
refactor(aya-ebpf): make InnerMap a sealed trait
Seal the InnerMap trait to prevent external implementations. This ensures only aya-ebpf map types can be used as inner maps in map-of-maps structures.
1 parent 2e016b1 commit 0e4c970

File tree

17 files changed

+25
-4
lines changed

17 files changed

+25
-4
lines changed

ebpf/aya-ebpf/src/maps/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ pub struct Array<T> {
1515
}
1616

1717
unsafe impl<T: Sync> Sync for Array<T> {}
18+
impl<T> super::private::Sealed for Array<T> {}
1819
unsafe impl<T> InnerMap for Array<T> {}
1920

20-
2121
impl<T> Array<T> {
2222
map_constructors!(u32, T, BPF_MAP_TYPE_ARRAY, phantom _t);
2323

ebpf/aya-ebpf/src/maps/bloom_filter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct BloomFilter<T> {
1212
_t: PhantomData<T>,
1313
}
1414

15+
impl<T> super::private::Sealed for BloomFilter<T> {}
1516
unsafe impl<T> InnerMap for BloomFilter<T> {}
1617

1718
impl<T> BloomFilter<T> {

ebpf/aya-ebpf/src/maps/hash_map.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ macro_rules! define_hash_map {
2020
_kv: PhantomData<(K, V)>,
2121
}
2222

23+
impl<K: Sync, V: Sync> super::private::Sealed for $name<K, V> {}
2324
unsafe impl<K: Sync, V: Sync> InnerMap for $name<K, V> {}
2425

2526
impl<K, V> $name<K, V> {

ebpf/aya-ebpf/src/maps/lpm_trie.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct LpmTrie<K, V> {
1717
}
1818

1919
unsafe impl<K: Sync, V: Sync> Sync for LpmTrie<K, V> {}
20+
impl<K, V> super::private::Sealed for LpmTrie<K, V> {}
2021
unsafe impl<K, V> InnerMap for LpmTrie<K, V> {}
2122

2223
#[repr(C, packed)]

ebpf/aya-ebpf/src/maps/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,16 @@ pub use stack::Stack;
109109
pub use stack_trace::StackTrace;
110110
pub use xdp::{CpuMap, DevMap, DevMapHash, XskMap};
111111

112+
mod private {
113+
pub trait Sealed {}
114+
}
115+
112116
/// Marker trait for all eBPF maps that can be used in a map of maps.
113117
///
118+
/// This trait is sealed and cannot be implemented outside this crate.
119+
///
114120
/// # Safety
115121
///
116-
/// Only implement this trait for map types that can be safely used as inner maps.
117-
pub unsafe trait InnerMap {}
122+
/// This trait must only be implemented for map types that the kernel accepts
123+
/// as inner maps in a map-of-maps structure.
124+
pub unsafe trait InnerMap: private::Sealed {}

ebpf/aya-ebpf/src/maps/per_cpu_array.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct PerCpuArray<T> {
1313
}
1414

1515
unsafe impl<T> Sync for PerCpuArray<T> {}
16+
impl<T> super::private::Sealed for PerCpuArray<T> {}
1617
unsafe impl<T> InnerMap for PerCpuArray<T> {}
1718

1819
impl<T> PerCpuArray<T> {

ebpf/aya-ebpf/src/maps/queue.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct Queue<T> {
1313
}
1414

1515
unsafe impl<T: Sync> Sync for Queue<T> {}
16+
impl<T> super::private::Sealed for Queue<T> {}
1617
unsafe impl<T> InnerMap for Queue<T> {}
1718

1819
impl<T> Queue<T> {

ebpf/aya-ebpf/src/maps/ring_buf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct RingBuf {
2323
}
2424

2525
unsafe impl Sync for RingBuf {}
26+
impl super::private::Sealed for RingBuf {}
2627
unsafe impl InnerMap for RingBuf {}
2728

2829
/// A ring buffer entry, returned from [`RingBuf::reserve_bytes`].

ebpf/aya-ebpf/src/maps/sock_hash.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct SockHash<K> {
2323
}
2424

2525
unsafe impl<K: Sync> Sync for SockHash<K> {}
26+
impl<K> super::private::Sealed for SockHash<K> {}
2627
unsafe impl<K> InnerMap for SockHash<K> {}
2728

2829
impl<K> SockHash<K> {

ebpf/aya-ebpf/src/maps/sock_map.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct SockMap {
1616
}
1717

1818
unsafe impl Sync for SockMap {}
19+
impl super::private::Sealed for SockMap {}
1920
unsafe impl InnerMap for SockMap {}
2021

2122
impl SockMap {

0 commit comments

Comments
 (0)