Re: [PATCH] kasan: add KUnit tests for __counted_by and __counted_by_ptr

From: Bill Wendling

Date: Mon Sep 28 2026 - 21:00:01 EST


On Mon, Sep 28, 2026 at 4:42 PM Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> On Mon, 28 Sep 2026 23:17:37 +0000 Bill Wendling <morbo@xxxxxxxxxx> wrote:
>
> > The '__counted_by' and '__counted_by_ptr' attributes associate a
> > flexible array member or pointer member with a struct field that holds
> > its element count. Supporting compilers use these annotations to
> > compute dynamic object sizes via '__builtin_dynamic_object_size()' and
> > perform runtime bounds checking with KASAN.
> >
> > Add KUnit tests ('counted_by_flex_oob_access' and
> > 'counted_by_ptr_oob_access', guarded by CONFIG_CC_HAS_COUNTED_BY and
> > CONFIG_CC_HAS_COUNTED_BY_PTR respectively) to verify that:
> >
> > - '__builtin_dynamic_object_size()' returns the expected byte size for
> > annotated flexible array and pointer members.
> > - KASAN detects out-of-bounds read and write accesses beyond the
> > annotated count.
> >
> > Allocate the test structures in 'noinline' helpers and hide the
> > returned pointers with OPTIMIZER_HIDE_VAR() so allocation-size
> > attributes and compiler optimizations do not mask the '__counted_by'
> > and '__counted_by_ptr' checks.
>
> Thanks. Are any of Sashiko's comments pertinent?
> https://sashiko.dev/#/patchset/20260928231737.2092716-1-morbo@xxxxxxxxxx

Yes. I found a better place to put these tests. I'll send a v2.

-bw