Skip to content

Commit

Permalink
fixup! Test execution of projects
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Oct 13, 2024
1 parent a1cb382 commit 940de92
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
24 changes: 13 additions & 11 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ TEMPLATE_DIR=$1
if [ -z "${TEMPLATE_DIR}" ]; then echo "template dir required"; exit 1; fi
PROG_TYPE=$2
if [ -z "${PROG_TYPE}" ]; then echo "program type required"; exit 1; fi
CRATE_NAME=aya-test-crate

TMP_DIR=$(mktemp -d)
clean_up() {
Expand All @@ -26,7 +27,7 @@ case "${PROG_TYPE}" in
ADDITIONAL_ARGS=(-d fn_name=try_to_wake_up)
;;
"kprobe"|"kretprobe")
ADDITIONAL_ARGS=(-d kprobe=test)
ADDITIONAL_ARGS=(-d kprobe_fn_name=do_unlinkat)
;;
"lsm")
ADDITIONAL_ARGS=(-d lsm_hook=file_open)
Expand All @@ -44,17 +45,17 @@ case "${PROG_TYPE}" in
ADDITIONAL_ARGS=(-d tracepoint_category=net -d tracepoint_name=net_dev_queue)
;;
"uprobe"|"uretprobe")
ADDITIONAL_ARGS=(-d uprobe_target=testlib -d uprobe_fn_name=testfn)
ADDITIONAL_ARGS=(-d uprobe_target=/proc/self/exe -d uprobe_fn_name=main)
;;
*)
ADDITIONAL_ARGS=()
esac

cargo generate --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" "${ADDITIONAL_ARGS[@]}"
pushd test
cargo generate --path "${TEMPLATE_DIR}" -n "${CRATE_NAME}" -d program_type="${PROG_TYPE}" "${ADDITIONAL_ARGS[@]}"
pushd "${CRATE_NAME}"
cargo +nightly fmt --all -- --check

cargo build --package test
cargo build --package "${CRATE_NAME}"

expect << EOF
set timeout 30 ;# Increase timeout if necessary
Expand All @@ -79,11 +80,12 @@ expect << EOF
}
EOF

cargo build --package test --release
# We cannot run clippy over the whole workspace at once due to feature unification. Since both test
# and test-ebpf depend on test-common and test activates test-common's aya dependency, we end up
# trying to compile the panic handler twice: once from the bpf program, and again from std via aya.
cargo clippy --exclude test-ebpf --all-targets --workspace -- --deny warnings
cargo clippy --package test-ebpf --all-targets -- --deny warnings
cargo build --package "${CRATE_NAME}" --release
# We cannot run clippy over the whole workspace at once due to feature unification. Since both
# ${CRATE_NAME} and ${CRATE_NAME}-ebpf depend on ${CRATE_NAME}-common and ${CRATE_NAME} activates
# ${CRATE_NAME}-common's aya dependency, we end up trying to compile the panic handler twice: once
# from the bpf program, and again from std via aya.
cargo clippy --exclude "${CRATE_NAME}-ebpf" --all-targets --workspace -- --deny warnings
cargo clippy --package "${CRATE_NAME}-ebpf" --all-targets -- --deny warnings

popd
4 changes: 2 additions & 2 deletions {{project-name}}-ebpf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn {{crate_name}}(ctx: ProbeContext) -> u32 {
}

fn try_{{crate_name}}(ctx: ProbeContext) -> Result<u32, u32> {
info!(&ctx, "function {{kprobe}} called");
info!(&ctx, "kprobe called");
Ok(0)
}
{%- when "kretprobe" %}
Expand All @@ -30,7 +30,7 @@ pub fn {{crate_name}}(ctx: RetProbeContext) -> u32 {
}

fn try_{{crate_name}}(ctx: RetProbeContext) -> Result<u32, u32> {
info!(&ctx, "function {{kprobe}} called");
info!(&ctx, "kretprobe called");
Ok(0)
}
{%- when "fentry" %}
Expand Down
8 changes: 3 additions & 5 deletions {{project-name}}/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ use aya::{
{%- when "tp_btf" -%}
use aya::{programs::BtfTracePoint, Btf};
{%- when "socket_filter" -%}
use std::net::TcpStream;

use aya::programs::SocketFilter;
{%- when "raw_tracepoint" -%}
use aya::programs::RawTracePoint;
Expand Down Expand Up @@ -106,7 +104,7 @@ async fn main() -> anyhow::Result<()> {
use anyhow::Context as _;
let program: &mut KProbe = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?;
program.load().context("load")?;
program.attach("{{kprobe}}", 0).context("attach")?;
program.attach("{{kprobe_fn_name}}", 0).context("attach")?;
{%- when "fentry" -%}
let btf = Btf::from_sys_fs().context("BTF from sysfs")?;
let program: &mut FEntry = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?;
Expand Down Expand Up @@ -172,10 +170,10 @@ async fn main() -> anyhow::Result<()> {
program.load("{{tracepoint_name}}", &btf)?;
program.attach()?;
{%- when "socket_filter" -%}
let client = TcpStream::connect("127.0.0.1:1234")?;
let listener = std::net::TcpListener::bind("localhost:0")?;
let prog: &mut SocketFilter = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?;
prog.load()?;
prog.attach(client)?;
prog.attach(&listener)?;
{%- when "cgroup_sysctl" -%}
let program: &mut CgroupSysctl = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?;
let cgroup = std::fs::File::open(&opt.cgroup_path)
Expand Down

0 comments on commit 940de92

Please sign in to comment.