From 5124bb95f7b67789f5611eb07b1708c813be543d Mon Sep 17 00:00:00 2001 From: Xiaobo Liu Date: Sat, 16 Aug 2025 17:44:42 +0800 Subject: [PATCH] tweak Signed-off-by: Xiaobo Liu --- test/integration-ebpf/src/queue_test.rs | 26 +++++++++--------- test/integration-test/src/tests/queue.rs | 34 +++++++++++++----------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/test/integration-ebpf/src/queue_test.rs b/test/integration-ebpf/src/queue_test.rs index 52eab381..a8bc146d 100644 --- a/test/integration-ebpf/src/queue_test.rs +++ b/test/integration-ebpf/src/queue_test.rs @@ -2,7 +2,7 @@ #![no_main] use aya_ebpf::{ - cty::c_long, + cty::{c_int, c_long}, macros::{map, uprobe}, maps::{Array, Queue}, programs::ProbeContext, @@ -14,14 +14,14 @@ const PEEK_INDEX: u32 = 0; const POP_INDEX: u32 = 1; #[map] -static RESULT: Array = Array::::with_max_entries(2, 0); +static RESULT: Array = Array::::with_max_entries(10, 0); #[map] -static TEST_QUEUE: Queue = Queue::with_max_entries(10, 0); +static TEST_QUEUE: Queue = Queue::with_max_entries(10, 0); #[uprobe] pub fn test_queue_push(ctx: ProbeContext) -> Result<(), c_long> { - let value: i64 = ctx.arg(0).ok_or(-1)?; + let value: c_int = ctx.arg(0).ok_or(-2)?; TEST_QUEUE.push(&value, 0)?; Ok(()) } @@ -29,19 +29,21 @@ pub fn test_queue_push(ctx: ProbeContext) -> Result<(), c_long> { #[uprobe] pub fn test_queue_peek(_: ProbeContext) -> Result<(), c_long> { let value = TEST_QUEUE.peek().unwrap_or(-1); - let result = RESULT.get_ptr_mut(PEEK_INDEX).ok_or(-1)?; - unsafe { - *result = value; - } + result_set(PEEK_INDEX, value)?; Ok(()) } #[uprobe] pub fn test_queue_pop(_: ProbeContext) -> Result<(), c_long> { let value = TEST_QUEUE.pop().unwrap_or(-1); - let result = RESULT.get_ptr_mut(POP_INDEX).ok_or(-1)?; - unsafe { - *result = value; - } + result_set(POP_INDEX, value)?; + Ok(()) +} + +fn result_set(index: u32, value: c_int) -> Result<(), c_long> { + let ptr = RESULT.get_ptr_mut(index).ok_or(-1)?; + let dst = unsafe { ptr.as_mut() }; + let dst_res = dst.ok_or(-1)?; + *dst_res = value; Ok(()) } diff --git a/test/integration-test/src/tests/queue.rs b/test/integration-test/src/tests/queue.rs index 8a5b3805..0e0b03f4 100644 --- a/test/integration-test/src/tests/queue.rs +++ b/test/integration-test/src/tests/queue.rs @@ -1,24 +1,25 @@ use aya::{EbpfLoader, maps::Array, programs::UProbe}; +use std::ffi::c_int; const PEEK_INDEX: u32 = 0; const POP_INDEX: u32 = 1; #[unsafe(no_mangle)] #[inline(never)] -pub extern "C" fn trigger_push(arg: i64) { +pub extern "C" fn trigger_queue_push(arg: c_int) { core::hint::black_box(arg); } #[unsafe(no_mangle)] #[inline(never)] -pub extern "C" fn trigger_peek() { - core::hint::black_box(trigger_peek); +pub extern "C" fn trigger_queue_peek() { + core::hint::black_box(trigger_queue_peek); } #[unsafe(no_mangle)] #[inline(never)] -pub extern "C" fn trigger_pop() { - core::hint::black_box(trigger_pop); +pub extern "C" fn trigger_queue_pop() { + core::hint::black_box(trigger_queue_pop); } #[test_log::test] @@ -31,7 +32,7 @@ fn queue_basic() { .try_into() .unwrap(); prog.load().unwrap(); - prog.attach("trigger_push", "/proc/self/exe", None, None) + prog.attach("trigger_queue_push", "/proc/self/exe", None, None) .unwrap(); let prog: &mut UProbe = bpf @@ -40,7 +41,7 @@ fn queue_basic() { .try_into() .unwrap(); prog.load().unwrap(); - prog.attach("trigger_peek", "/proc/self/exe", None, None) + prog.attach("trigger_queue_peek", "/proc/self/exe", None, None) .unwrap(); let prog: &mut UProbe = bpf @@ -49,18 +50,19 @@ fn queue_basic() { .try_into() .unwrap(); prog.load().unwrap(); - prog.attach("trigger_pop", "/proc/self/exe", None, None) + prog.attach("trigger_queue_pop", "/proc/self/exe", None, None) .unwrap(); - //let queue = Queue::<_, u64>::try_from(bpf.take_map("TEST_QUEUE").unwrap()).unwrap(); - let array = Array::<_, i64>::try_from(bpf.map("RESULT").unwrap()).unwrap(); + let array_map = bpf.map("RESULT").unwrap(); + let array = Array::<_, c_int>::try_from(array_map).unwrap(); - for i in 0..5 { - trigger_push(i); - trigger_peek(); - trigger_pop(); + for i in 0..9 { + trigger_queue_push(i); - assert_eq!(array.get(&PEEK_INDEX, 0).unwrap(), i); - assert_eq!(array.get(&POP_INDEX, 0).unwrap(), i); + trigger_queue_peek(); + assert_eq!(array.get(&PEEK_INDEX, 0).unwrap(), i as c_int); + + trigger_queue_pop(); + assert_eq!(array.get(&POP_INDEX, 0).unwrap(), i as c_int); } }