Re: [PATCH v3 1/3] btf_encoder: skip functions consuming packed structs passed by value on stack

Alexei Starovoitov
2025-07-07T17:05:28Z

On Mon, Jul 7, 2025 at 7:02 AM Alexis Lothoré (eBPF Foundation)
<alexis.lothore@bootlin.com> wrote:
>
> Most ABIs allow functions to receive structs passed by value, if they
> fit in a register or a pair of registers, depending on the exact ABI.
> However, when there is a struct passed by value but all registers are
> already used for parameters passing, the struct is still passed by value
> but on the stack. This becomes an issue if the passed struct is defined
> with some attributes like __attribute__((packed)) or
> __attribute__((aligned(X)), as its location on the stack is altered, but
> this change is not reflected in dwarf information. The corresponding BTF
> data generated from this can lead to incorrect BPF trampolines
> generation (eg to attach bpf tracing programs to kernel functions) in
> the Linux kernel.
>
> Prevent those wrong cases by not encoding functions consuming structs
> passed by value on stack, when those structs do not have the expected
> alignment due to some attribute usage.
>
> Signed-off-by: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>

...

> +static bool ftype__has_uncertain_arg_loc(struct cu *cu, struct ftype *ftype)
> +{
> + struct parameter *param;
> + int param_idx = 0;
> +
> + if (ftype->nr_parms < cu->nr_register_params)
> + return false;
> +
> + ftype__for_each_parameter(ftype, param) {
> + if (param_idx++ < cu->nr_register_params)
> + continue;
> +
> + struct tag *type = cu__type(cu, param->tag.type);
> +
> + if (type == NULL || !tag__is_struct(type))
> + continue;
> +
> + struct type *ctype = tag__type(type);
> + if (ctype->namespace.name == 0)
> + continue;
> +
> + struct class *class = tag__class(type);
> +
> + class__infer_packed_attributes(class, cu);
> +
> + if (class->is_packed)
> + return true;
> + }
> +
> + return false;
> +}
> +

The logic looks good to me.