aya-rs/aya-log

Logging signed integers smaller than i64 fails unless opt-level is set to 3

willfindlay opened this issue · 1 comments

Logging signed integers smaller than i64 with aya-log causes the following verifier error on Linux 5.14:

/* Lots of output, truncated to save space */
349: (71) r5 = *(u8 *)(r5 +0)
invalid read from stack off -20+0 size 1
verification time 4497 usec
stack depth 40+20+0
processed 557 insns (limit 1000000) max_states_per_insn 3 total_states 9 peak_states 9 mark_read 8
: Permission denied (os error 13)

Here's a minimal repro with latest aya-bpf:

#![no_std]
#![no_main]

use aya_bpf::{macros::lsm, programs::LsmContext};
use aya_log_ebpf as log;

#[lsm(name = "task_alloc")]
pub fn task_alloc(ctx: LsmContext) -> i32 {
    match unsafe { try_task_alloc(ctx) } {
        Ok(ret) => ret,
        Err(ret) => ret,
    }
}

unsafe fn try_task_alloc(ctx: LsmContext) -> Result<i32, i32> {
    let clone_flags: i32 = ctx.arg(1); // This would normally be an i64, but let's just take i32 for sake of argument

    log::info!(
        &ctx,
        "task_alloc called with flags {}",
        clone_flags
    );

    Ok(0)
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    unsafe { core::hint::unreachable_unchecked() }
}

Curiously, it works if we set opt-level = 3 instead of 2 in the Cargo.toml of the eBPF program.

closing this as we've changed the formatting implementation