From: Karl Mehltretter <kmehltretter@gmail.com>
To: Vlastimil Babka <vbabka@kernel.org>, Harry Yoo <harry@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Karl Mehltretter <kmehltretter@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Hao Li <hao.li@linux.dev>, Christoph Lameter <cl@gentwo.org>,
David Rientjes <rientjes@google.com>,
Roman Gushchin <roman.gushchin@linux.dev>,
Catalin Marinas <catalin.marinas@arm.com>,
Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
linux-hardening@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, llvm@lists.linux.dev
Subject: [PATCH v2 3/3] slab: test zero-size allocations in slub_kunit
Date: Tue, 11 Aug 2026 16:12:40 +0200 [thread overview]
Message-ID: <20260811141240.62519-4-kmehltretter@gmail.com> (raw)
In-Reply-To: <20260811141240.62519-1-kmehltretter@gmail.com>
Add KUnit coverage for the zero-size allocation contract, including
ZERO_SIZE_PTR alignment and exact ZERO_OR_NULL_PTR() matching.
Assisted-by: Claude:claude-fable-5
Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
---
lib/tests/slub_kunit.c | 43 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/lib/tests/slub_kunit.c b/lib/tests/slub_kunit.c
index fa6d31dbca166..a02fc6b401e3e 100644
--- a/lib/tests/slub_kunit.c
+++ b/lib/tests/slub_kunit.c
@@ -380,6 +380,48 @@ static void test_kmalloc_kfree_nolock(struct kunit *test)
}
#endif
+static void test_zero_size_alloc(struct kunit *test)
+{
+ unsigned long zsp = (unsigned long)ZERO_SIZE_PTR;
+ void *p, *r;
+
+ static_assert((unsigned long)ZERO_SIZE_PTR % ARCH_KMALLOC_MINALIGN == 0);
+
+ p = kmalloc(0, GFP_KERNEL);
+ KUNIT_EXPECT_PTR_EQ(test, p, ZERO_SIZE_PTR);
+ KUNIT_EXPECT_EQ(test, ksize(p), 0);
+ kfree(p);
+
+ KUNIT_EXPECT_PTR_EQ(test, kzalloc(0, GFP_KERNEL), ZERO_SIZE_PTR);
+ KUNIT_EXPECT_PTR_EQ(test, kmalloc_array(0, 8, GFP_KERNEL), ZERO_SIZE_PTR);
+ KUNIT_EXPECT_PTR_EQ(test, kcalloc(4, 0, GFP_KERNEL), ZERO_SIZE_PTR);
+
+ p = kvmalloc(0, GFP_KERNEL);
+ KUNIT_EXPECT_PTR_EQ(test, p, ZERO_SIZE_PTR);
+ kvfree(p);
+
+ p = krealloc(NULL, 0, GFP_KERNEL);
+ KUNIT_EXPECT_PTR_EQ(test, p, ZERO_SIZE_PTR);
+ r = krealloc(p, 64, GFP_KERNEL);
+ KUNIT_EXPECT_FALSE(test, ZERO_OR_NULL_PTR(r));
+ p = krealloc(r, 0, GFP_KERNEL);
+ KUNIT_EXPECT_PTR_EQ(test, p, ZERO_SIZE_PTR);
+ kfree(p);
+
+ /* Only NULL and the zero-size sentinel match. */
+ KUNIT_EXPECT_TRUE(test, ZERO_OR_NULL_PTR(NULL));
+ KUNIT_EXPECT_TRUE(test, ZERO_OR_NULL_PTR(ZERO_SIZE_PTR));
+ KUNIT_EXPECT_FALSE(test, ZERO_OR_NULL_PTR((void *)1));
+ KUNIT_EXPECT_FALSE(test, ZERO_OR_NULL_PTR((void *)(zsp - 1)));
+ KUNIT_EXPECT_FALSE(test, ZERO_OR_NULL_PTR((void *)(zsp + 1)));
+ KUNIT_EXPECT_FALSE(test, ZERO_OR_NULL_PTR((void *)(zsp * 2)));
+
+ /* freeing the sentinel must stay a no-op */
+ kfree(ZERO_SIZE_PTR);
+ kfree_sensitive(ZERO_SIZE_PTR);
+ kvfree(ZERO_SIZE_PTR);
+}
+
static int test_init(struct kunit *test)
{
slab_errors = 0;
@@ -404,6 +446,7 @@ static struct kunit_case test_cases[] = {
KUNIT_CASE(test_kfree_rcu_wq_destroy),
KUNIT_CASE(test_leak_destroy),
KUNIT_CASE(test_krealloc_redzone_zeroing),
+ KUNIT_CASE(test_zero_size_alloc),
#ifdef CONFIG_PERF_EVENTS
KUNIT_CASE_SLOW(test_kmalloc_kfree_nolock),
#endif
--
2.53.0
prev parent reply other threads:[~2026-08-11 14:13 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-11 14:12 [PATCH v2 0/3] slab: ZERO_SIZE_PTR alignment and exact matching Karl Mehltretter
2026-08-11 14:12 ` [PATCH v2 1/3] slab: align ZERO_SIZE_PTR to ARCH_KMALLOC_MINALIGN Karl Mehltretter
2026-08-11 14:12 ` [PATCH v2 2/3] slab: check for ZERO_SIZE_PTR by exact match Karl Mehltretter
2026-08-12 0:17 ` Kees Cook
2026-08-15 0:28 ` Karl Mehltretter
2026-08-11 14:12 ` Karl Mehltretter [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260811141240.62519-4-kmehltretter@gmail.com \
--to=kmehltretter@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=catalin.marinas@arm.com \
--cc=cl@gentwo.org \
--cc=gustavoars@kernel.org \
--cc=hao.li@linux.dev \
--cc=harry@kernel.org \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux@rasmusvillemoes.dk \
--cc=llvm@lists.linux.dev \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=vbabka@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.