Add a KUnit test for strlen() to verify correctness across
different string lengths and memory alignments. Use vmalloc()
to place the NUL character at the page boundary to ensure
over-reads are detected.
Suggested-by: Kees Cook <kees@kernel.org>
Signed-off-by: Feng Jiang <jiangfeng@kylinos.cn>
---
lib/tests/string_kunit.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c
index f9a8e557ba77..335f2e6c2ed6 100644
--- a/lib/tests/string_kunit.c
+++ b/lib/tests/string_kunit.c
@@ -10,6 +10,7 @@
#include <linux/printk.h>
#include <linux/slab.h>
#include <linux/string.h>
+#include <linux/vmalloc.h>
#define STRCMP_LARGE_BUF_LEN 2048
#define STRCMP_CHANGE_POINT 1337
@@ -17,6 +18,9 @@
#define STRCMP_TEST_EXPECT_LOWER(test, fn, ...) KUNIT_EXPECT_LT(test, fn(__VA_ARGS__), 0)
#define STRCMP_TEST_EXPECT_GREATER(test, fn, ...) KUNIT_EXPECT_GT(test, fn(__VA_ARGS__), 0)
+#define STRING_TEST_MAX_LEN 128
+#define STRING_TEST_MAX_OFFSET 16
+
static void string_test_memset16(struct kunit *test)
{
unsigned i, j, k;
@@ -104,6 +108,30 @@ static void string_test_memset64(struct kunit *test)
}
}
+static void string_test_strlen(struct kunit *test)
+{
+ size_t buf_size;
+ char *buf, *s;
+
+ buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);
+ buf = vmalloc(buf_size);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
+
+ memset(buf, 'A', buf_size);
+
+ for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {
+ for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {
+ s = buf + buf_size - 1 - offset - len;
+ s[len] = '\0';
+ KUNIT_EXPECT_EQ_MSG(test, strlen(s), len,
+ "offset:%zu len:%zu", offset, len);
+ s[len] = 'A';
+ }
+ }
+
+ vfree(buf);
+}
+
static void string_test_strchr(struct kunit *test)
{
const char *test_string = "abcdefghijkl";
@@ -618,6 +646,7 @@ static struct kunit_case string_test_cases[] = {
KUNIT_CASE(string_test_memset16),
KUNIT_CASE(string_test_memset32),
KUNIT_CASE(string_test_memset64),
+ KUNIT_CASE(string_test_strlen),
KUNIT_CASE(string_test_strchr),
KUNIT_CASE(string_test_strnchr),
KUNIT_CASE(string_test_strspn),
--
2.25.1
Hi Feng,
kernel test robot noticed the following build errors:
[auto build test ERROR on kees/for-next/hardening]
[also build test ERROR on akpm-mm/mm-nonmm-unstable akpm-mm/mm-everything linus/master v6.19-rc7 next-20260128]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Feng-Jiang/lib-string_kunit-add-correctness-test-for-strlen/20260129-151036
base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/hardening
patch link: https://lore.kernel.org/r/20260129070227.220866-2-jiangfeng%40kylinos.cn
patch subject: [PATCH v6 1/8] lib/string_kunit: add correctness test for strlen()
config: x86_64-randconfig-001-20260129 (https://download.01.org/0day-ci/archive/20260130/202601300014.gbv48Y5D-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260130/202601300014.gbv48Y5D-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202601300014.gbv48Y5D-lkp@intel.com/
All errors (new ones prefixed by >>):
>> lib/tests/string_kunit.c:116:13: error: call to undeclared function 'PAGE_ALIGN'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
116 | buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);
| ^
1 error generated.
vim +/PAGE_ALIGN +116 lib/tests/string_kunit.c
110
111 static void string_test_strlen(struct kunit *test)
112 {
113 size_t buf_size;
114 char *buf, *s;
115
> 116 buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);
117 buf = vmalloc(buf_size);
118 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
119
120 memset(buf, 'A', buf_size);
121
122 for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {
123 for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {
124 s = buf + buf_size - 1 - offset - len;
125 s[len] = '\0';
126 KUNIT_EXPECT_EQ_MSG(test, strlen(s), len,
127 "offset:%zu len:%zu", offset, len);
128 s[len] = 'A';
129 }
130 }
131
132 vfree(buf);
133 }
134
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
On 2026/1/30 00:55, kernel test robot wrote:
> Hi Feng,
>
> kernel test robot noticed the following build errors:
>
> [auto build test ERROR on kees/for-next/hardening]
> [also build test ERROR on akpm-mm/mm-nonmm-unstable akpm-mm/mm-everything linus/master v6.19-rc7 next-20260128]
> [If your patch is applied to the wrong git tree, kindly drop us a note.
> And when submitting patch, we suggest to use '--base' as documented in
> https://git-scm.com/docs/git-format-patch#_base_tree_information]
>
> url: https://github.com/intel-lab-lkp/linux/commits/Feng-Jiang/lib-string_kunit-add-correctness-test-for-strlen/20260129-151036
> base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/hardening
> patch link: https://lore.kernel.org/r/20260129070227.220866-2-jiangfeng%40kylinos.cn
> patch subject: [PATCH v6 1/8] lib/string_kunit: add correctness test for strlen()
> config: x86_64-randconfig-001-20260129 (https://download.01.org/0day-ci/archive/20260130/202601300014.gbv48Y5D-lkp@intel.com/config)
> compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260130/202601300014.gbv48Y5D-lkp@intel.com/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202601300014.gbv48Y5D-lkp@intel.com/
>
> All errors (new ones prefixed by >>):
>
>>> lib/tests/string_kunit.c:116:13: error: call to undeclared function 'PAGE_ALIGN'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
> 116 | buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);
> | ^
> 1 error generated.
>
>
> vim +/PAGE_ALIGN +116 lib/tests/string_kunit.c
>
Thanks for the report.
I apologize for the build error. This was caused by the missing <linux/mm.h> header
for the PAGE_ALIGN macro in the new test logic. I will include the necessary header
and fix this in the v7 patch set.
> 110
> 111 static void string_test_strlen(struct kunit *test)
> 112 {
> 113 size_t buf_size;
> 114 char *buf, *s;
> 115
> > 116 buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);
> 117 buf = vmalloc(buf_size);
> 118 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
> 119
> 120 memset(buf, 'A', buf_size);
> 121
> 122 for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {
> 123 for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {
> 124 s = buf + buf_size - 1 - offset - len;
> 125 s[len] = '\0';
> 126 KUNIT_EXPECT_EQ_MSG(test, strlen(s), len,
> 127 "offset:%zu len:%zu", offset, len);
> 128 s[len] = 'A';
> 129 }
> 130 }
> 131
> 132 vfree(buf);
> 133 }
> 134
>
--
With Best Regards,
Feng Jiang
Hi Feng,
kernel test robot noticed the following build errors:
[auto build test ERROR on kees/for-next/hardening]
[also build test ERROR on akpm-mm/mm-nonmm-unstable akpm-mm/mm-everything linus/master v6.19-rc7 next-20260128]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Feng-Jiang/lib-string_kunit-add-correctness-test-for-strlen/20260129-151036
base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/hardening
patch link: https://lore.kernel.org/r/20260129070227.220866-2-jiangfeng%40kylinos.cn
patch subject: [PATCH v6 1/8] lib/string_kunit: add correctness test for strlen()
config: m68k-defconfig (https://download.01.org/0day-ci/archive/20260129/202601292204.xA8dUDti-lkp@intel.com/config)
compiler: m68k-linux-gcc (GCC) 15.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260129/202601292204.xA8dUDti-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202601292204.xA8dUDti-lkp@intel.com/
All errors (new ones prefixed by >>):
lib/tests/string_kunit.c: In function 'string_test_strlen':
>> lib/tests/string_kunit.c:116:20: error: implicit declaration of function 'PAGE_ALIGN'; did you mean 'PTR_ALIGN'? [-Wimplicit-function-declaration]
116 | buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);
| ^~~~~~~~~~
| PTR_ALIGN
vim +116 lib/tests/string_kunit.c
110
111 static void string_test_strlen(struct kunit *test)
112 {
113 size_t buf_size;
114 char *buf, *s;
115
> 116 buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);
117 buf = vmalloc(buf_size);
118 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
119
120 memset(buf, 'A', buf_size);
121
122 for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {
123 for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {
124 s = buf + buf_size - 1 - offset - len;
125 s[len] = '\0';
126 KUNIT_EXPECT_EQ_MSG(test, strlen(s), len,
127 "offset:%zu len:%zu", offset, len);
128 s[len] = 'A';
129 }
130 }
131
132 vfree(buf);
133 }
134
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
© 2016 - 2026 Red Hat, Inc.