Skip to content

Commit

Permalink
tests/xdp: don't leak memory in AF_XDP test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuetuopay committed Oct 18, 2023
1 parent 284c352 commit 9c7373f
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions test/integration-test/src/tests/xdp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::{
ffi::CStr, mem::MaybeUninit, net::UdpSocket, num::NonZeroU32, ptr::NonNull, time::Duration,
};
use std::{ffi::CStr, mem::MaybeUninit, net::UdpSocket, num::NonZeroU32, time::Duration};

use aya::{
maps::{Array, CpuMap, XskMap},
Expand Down Expand Up @@ -28,12 +26,18 @@ fn af_xdp() {
xdp.load().unwrap();
xdp.attach("lo", XdpFlags::default()).unwrap();

// The shared memory area with the kernel must be page-aligned.
#[repr(align(4096))]
struct PacketMap(MaybeUninit<[u8; 4096]>);

let mem = Box::new(PacketMap(MaybeUninit::uninit()));
let mem = NonNull::new(Box::leak(mem).0.as_mut_ptr()).unwrap();
let umem = unsafe { Umem::new(UmemConfig::default(), mem).unwrap() };
// Safety: don't access alloc down the line.
let mut alloc = Box::new(PacketMap(MaybeUninit::uninit()));
let umem = {
// Safety: this is a shared buffer between the kernel and us, uninitialized memory is valid.
let mem = unsafe { alloc.0.assume_init_mut() }.as_mut().into();
// Safety: we cannot access `mem` further down the line because it falls out of scope.
unsafe { Umem::new(UmemConfig::default(), mem).unwrap() }
};

let mut iface = IfInfo::invalid();
iface
Expand Down

0 comments on commit 9c7373f

Please sign in to comment.