Re: [PATCH 2/2] lib/tests: Add KUnit test for struct stack_trace __counted_by_ptr attribute

From: Bill Wendling

Date: Thu Sep 10 2026 - 15:54:15 EST


On Sun, Aug 23, 2026 at 11:41 PM Thomas Weißschuh
<thomas.weissschuh@xxxxxxxxxxxxx> wrote:
>
> On Sun, Aug 23, 2026 at 12:35:33PM +0000, Bill Wendling wrote:
> > Add a custom KUnit test suite 'stacktrace_counted_by' to verify that the
> > __counted_by_ptr annotation on the 'entries' field of 'struct stack_trace'
> > behaves correctly.
>
> The implementation of __counted_by_ptr should be tested by a dedicated unittest
> for that feature. And I am fairly sure that already exists. It should not be
> tested in random subsystems.
>
I'm looking at `tools/unittests` and...what in the world is it
supposed to be testing? It looks like it's testing absolutely nothing
of substance. I assume you want there to be a test that runs and
produces a stack trace triggered by an invalid access, right? I'm
unable to find any tests in `tools/unittests` that do anything like
that...maybe I'm missing something?

-bw

> > The test verifies that 'max_entries' correctly limits and validates access
> > to 'entries' when CONFIG_ARCH_STACKWALK is not defined. If it is defined,
> > the test is cleanly skipped at runtime to prevent compile-time or runtime
> > failures due to 'struct stack_trace' being undefined on modern
> > architectures.
> >
> > Assisted-by: Gemini Next
> > Signed-off-by: Bill Wendling <morbo@xxxxxxxxxx>
> > ---
> > Cc: Kees Cook <kees@xxxxxxxxxx>
> > Cc: "Gustavo A. R. Silva" <gustavoars@xxxxxxxxxx>
> > Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> > Cc: Brendan Higgins <brendan.higgins@xxxxxxxxx>
> > Cc: David Gow <david@xxxxxxxxxxxx>
> > Cc: Rae Moar <raemoar63@xxxxxxxxx>
> > Cc: Ryota Sakamoto <sakamo.ryota@xxxxxxxxx>
> > Cc: Kuan-Wei Chiu <visitorckw@xxxxxxxxx>
> > Cc: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
> > Cc: Dmitry Antipov <dmantipov@xxxxxxxxx>
> > Cc: Petr Mladek <pmladek@xxxxxxxx>
> > Cc: Kir Chou <note351@xxxxxxxxxxx>
> > Cc: codemender-patching+linux@xxxxxxxxxx
> > Cc: linux-kernel@xxxxxxxxxxxxxxx
> > Cc: linux-hardening@xxxxxxxxxxxxxxx
> > Cc: linux-kselftest@xxxxxxxxxxxxxxx
> > Cc: kunit-dev@xxxxxxxxxxxxxxxx
> > Cc: linux-hardening@xxxxxxxxxxxxxxx
> > ---
> > lib/Kconfig.debug | 10 +++++++
> > lib/kunit/.kunitconfig | 1 +
> > lib/tests/Makefile | 1 +
> > lib/tests/stacktrace_kunit.c | 51 ++++++++++++++++++++++++++++++++++++
> > 4 files changed, 63 insertions(+)
> > create mode 100644 lib/tests/stacktrace_kunit.c
>
> (...)
>
> > diff --git a/lib/tests/stacktrace_kunit.c b/lib/tests/stacktrace_kunit.c
> > new file mode 100644
> > index 000000000000..7ec48edf84fe
> > --- /dev/null
> > +++ b/lib/tests/stacktrace_kunit.c
> > @@ -0,0 +1,51 @@
>
> (...)
>
> > +}
> > +#else
> > +static void test_stack_trace_counted_by(struct kunit *test)
> > +{
> > + kunit_skip(test, "CONFIG_ARCH_STACKWALK is enabled, struct stack_trace is not defined");
>
> If the test requires a specific kconfig symbol, then depend on it in kconfig.
> Putting all of the code behind a single, giant ifdef is pointless.
>
> > +}
> > +#endif
>
> (...)