Skip to contentSkip to conversation

lib/test_string.c: Add strncmp() tests

PATCH In review Palmer Dabbelt 2 participants 2 messages Feb 22, 2023 – Feb 23, 2023 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/test_string.c: Add strncmp() tests

The RISC-V strncmp() fails on some inputs, see the linked thread for more details. It turns out there were no strncmp() calls in the self tests, this adds one. It currently fails on RISC-V systems with Zbb enabled with

    [    0.683479] String selftest failure 7.00001001

---
This reports a checkpatch error for the __initconst, but I think it's spurious as I've just pattern matched the above test.

---
 lib/test_string.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
lib/test_string.c +39 -0
index c5cb92fb710e..8420379963ba 100644
@@ -207,6 +207,40 @@ static __init int strspn_selftest(void)
 	return 0;
 }
 
+static __init int strncmp_selftest(void)
+{
+	static const struct strncmp_test {
+		const char *str_a;
+		const char *str_b;
+		unsigned long count;
+		unsigned long max_off;
+		size_t retval;
+	} tests[] __initconst = {
+		{ "/dev/vda", "/dev/",    5, 4, 0 },
+		{ "/dev/vda", "/dev/vdb", 5, 4, 0 },
+	};
+	size_t i;
+
+	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
+		const struct strncmp_test *s = tests + i;
+		size_t off;
+
+		for (off = 0; off <= s->max_off; ++off) {
+			size_t res = strncmp(s->str_a + off,
+					     s->str_b + off,
+					     s->count - off);
+
+			if (res == 0 && s->retval != 0)
+				return 0x1000 + 0x100*off + 0x10*i + 0x0;
+			if (res > 0 && s->retval <= 0)
+				return 0x1000 + 0x100*off + 0x10*i + 0x1;
+			if (res < 0 && s->retval >= 0)
+				return 0x1000 + 0x100*off + 0x10*i + 0x2;
+		}
+	}
+	return 0;
+}
+
 static __exit void string_selftest_remove(void)
 {
 }
@@ -245,6 +279,11 @@ static __init int string_selftest_init(void)
 	if (subtest)
 		goto fail;
 
+	test = 7;
+	subtest = strncmp_selftest();
+	if (subtest)
+		goto fail;
+
 	pr_info("String selftests succeeded\n");
 	return 0;
 fail:
Signature · 1 line
2.39.1
#1 lore raw
Quoted 7 lines · Palmer Dabbelt

The RISC-V strncmp() fails on some inputs, see the linked thread for more details. It turns out there were no strncmp() calls in the self tests, this adds one. It currently fails on RISC-V systems with Zbb enabled with

    [    0.683479] String selftest failure 7.00001001

...

Quoted 2 lines

This reports a checkpatch error for the __initconst, but I think it's spurious as I've just pattern matched the above test.

Does moving outside of function help?

...

lib/test_string.c static __init int strspn_selftest(void) · line 220

Quoted 6 lines
lib/test_string.c +6 -0
+               unsigned long count;
+               unsigned long max_off;
+               size_t retval;
+       } tests[] __initconst = {
+               { "/dev/vda", "/dev/",    5, 4, 0 },
+               { "/dev/vda", "/dev/vdb", 5, 4, 0 },

In the current state the max_off is redundant.

Quoted 1 line

+ for (off = 0; off <= s->max_off; ++off) {

Why pre-increment?

...

lib/test_string.c static __init int strspn_selftest(void) · line 238

Quoted 6 lines
lib/test_string.c +6 -0
+                       if (res == 0 && s->retval != 0)
+                               return 0x1000 + 0x100*off + 0x10*i + 0x0;
+                       if (res > 0 && s->retval <= 0)
+                               return 0x1000 + 0x100*off + 0x10*i + 0x1;
+                       if (res < 0 && s->retval >= 0)
+                               return 0x1000 + 0x100*off + 0x10*i + 0x2;

But you don't have cases for +1 -1 results (positive, negative), why not add them?

Signature · 2 lines
With Best Regards,
Andy Shevchenko