Skip to content

Commit cf9be0a

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 b22398a commit cf9be0a

File tree

17 files changed

+28
-3
lines changed

17 files changed

+28
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ 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

2021
impl<T> Array<T> {

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct HashMap<K, V> {
2020
}
2121

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

2526
impl<K, V> HashMap<K, V> {
@@ -102,6 +103,7 @@ pub struct LruHashMap<K, V> {
102103
}
103104

104105
unsafe impl<K: Sync, V: Sync> Sync for LruHashMap<K, V> {}
106+
impl<K: Sync, V: Sync> super::private::Sealed for LruHashMap<K, V> {}
105107
unsafe impl<K: Sync, V: Sync> InnerMap for LruHashMap<K, V> {}
106108

107109
impl<K, V> LruHashMap<K, V> {
@@ -184,6 +186,7 @@ pub struct PerCpuHashMap<K, V> {
184186
}
185187

186188
unsafe impl<K, V> Sync for PerCpuHashMap<K, V> {}
189+
impl<K: Sync, V: Sync> super::private::Sealed for PerCpuHashMap<K, V> {}
187190
unsafe impl<K: Sync, V: Sync> InnerMap for PerCpuHashMap<K, V> {}
188191

189192
impl<K, V> PerCpuHashMap<K, V> {
@@ -266,6 +269,7 @@ pub struct LruPerCpuHashMap<K, V> {
266269
}
267270

268271
unsafe impl<K, V> Sync for LruPerCpuHashMap<K, V> {}
272+
impl<K: Sync, V: Sync> super::private::Sealed for LruPerCpuHashMap<K, V> {}
269273
unsafe impl<K: Sync, V: Sync> InnerMap for LruPerCpuHashMap<K, V> {}
270274

271275
impl<K, V> LruPerCpuHashMap<K, V> {

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

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

2020
unsafe impl<K: Sync, V: Sync> Sync for LpmTrie<K, V> {}
21+
impl<K, V> super::private::Sealed for LpmTrie<K, V> {}
2122
unsafe impl<K, V> InnerMap for LpmTrie<K, V> {}
2223

2324
#[repr(C, packed)]

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

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

42+
mod private {
43+
pub trait Sealed {}
44+
}
45+
4246
/// Marker trait for all eBPF maps that can be used in a map of maps.
4347
///
48+
/// This trait is sealed and cannot be implemented outside this crate.
49+
///
4450
/// # Safety
4551
///
46-
/// Only implement this trait for map types that can be safely used as inner maps.
47-
pub unsafe trait InnerMap {}
52+
/// This trait must only be implemented for map types that the kernel accepts
53+
/// as inner maps in a map-of-maps structure.
54+
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
@@ -24,6 +24,7 @@ pub struct SockHash<K> {
2424
}
2525

2626
unsafe impl<K: Sync> Sync for SockHash<K> {}
27+
impl<K> super::private::Sealed for SockHash<K> {}
2728
unsafe impl<K> InnerMap for SockHash<K> {}
2829

2930
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
@@ -18,6 +18,7 @@ pub struct SockMap {
1818
}
1919

2020
unsafe impl Sync for SockMap {}
21+
impl super::private::Sealed for SockMap {}
2122
unsafe impl InnerMap for SockMap {}
2223

2324
impl SockMap {

0 commit comments

Comments
 (0)