Commit 03270c13 authored by Matthew Wilcox's avatar Matthew Wilcox Committed by Linus Torvalds

lib/string.c: add testcases for memset16/32/64

[[email protected]: minor tweaks]
Link: http://lkml.kernel.org/r/[email protected]Signed-off-by: default avatarMatthew Wilcox <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: "James E.J. Bottomley" <[email protected]>
Cc: "Martin K. Petersen" <[email protected]>
Cc: David Miller <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Ivan Kokshaysky <[email protected]>
Cc: Matt Turner <[email protected]>
Cc: Michael Ellerman <[email protected]>
Cc: Minchan Kim <[email protected]>
Cc: Ralf Baechle <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Russell King <[email protected]>
Cc: Sam Ravnborg <[email protected]>
Cc: Sergey Senozhatsky <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Signed-off-by: default avatarAndrew Morton <[email protected]>
Signed-off-by: default avatarLinus Torvalds <[email protected]>
parent 3b3c4bab
......@@ -575,4 +575,7 @@ config PARMAN
config PRIME_NUMBERS
tristate
config STRING_SELFTEST
bool "Test string functions"
endmenu
......@@ -1051,3 +1051,132 @@ void fortify_panic(const char *name)
BUG();
}
EXPORT_SYMBOL(fortify_panic);
#ifdef CONFIG_STRING_SELFTEST
#include <linux/slab.h>
#include <linux/module.h>
static __init int memset16_selftest(void)
{
unsigned i, j, k;
u16 v, *p = kmalloc(256 * 2 * 2, GFP_KERNEL);
for (i = 0; i < 256; i++) {
for (j = 0; j < 256; j++) {
memset(p, 0xa1, 256 * 2 * sizeof(v));
memset16(p + i, 0xb1b2, j);
for (k = 0; k < 512; k++) {
v = p[k];
if (k < i) {
if (v != 0xa1a1)
goto fail;
} else if (k < i + j) {
if (v != 0xb1b2)
goto fail;
} else {
if (v != 0xa1a1)
goto fail;
}
}
}
}
fail:
kfree(p);
if (i < 256)
return (i << 24) | (j << 16) | k;
return 0;
}
static __init int memset32_selftest(void)
{
unsigned i, j, k;
u32 v, *p = kmalloc(256 * 2 * 4, GFP_KERNEL);
for (i = 0; i < 256; i++) {
for (j = 0; j < 256; j++) {
memset(p, 0xa1, 256 * 2 * sizeof(v));
memset32(p + i, 0xb1b2b3b4, j);
for (k = 0; k < 512; k++) {
v = p[k];
if (k < i) {
if (v != 0xa1a1a1a1)
goto fail;
} else if (k < i + j) {
if (v != 0xb1b2b3b4)
goto fail;
} else {
if (v != 0xa1a1a1a1)
goto fail;
}
}
}
}
fail:
kfree(p);
if (i < 256)
return (i << 24) | (j << 16) | k;
return 0;
}
static __init int memset64_selftest(void)
{
unsigned i, j, k;
u64 v, *p = kmalloc(256 * 2 * 8, GFP_KERNEL);
for (i = 0; i < 256; i++) {
for (j = 0; j < 256; j++) {
memset(p, 0xa1, 256 * 2 * sizeof(v));
memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);
for (k = 0; k < 512; k++) {
v = p[k];
if (k < i) {
if (v != 0xa1a1a1a1a1a1a1a1ULL)
goto fail;
} else if (k < i + j) {
if (v != 0xb1b2b3b4b5b6b7b8ULL)
goto fail;
} else {
if (v != 0xa1a1a1a1a1a1a1a1ULL)
goto fail;
}
}
}
}
fail:
kfree(p);
if (i < 256)
return (i << 24) | (j << 16) | k;
return 0;
}
static __init int string_selftest_init(void)
{
int test, subtest;
test = 1;
subtest = memset16_selftest();
if (subtest)
goto fail;
test = 2;
subtest = memset32_selftest();
if (subtest)
goto fail;
test = 3;
subtest = memset64_selftest();
if (subtest)
goto fail;
pr_info("String selftests succeeded\n");
return 0;
fail:
pr_crit("String selftest failure %d.%08x\n", test, subtest);
return 0;
}
module_init(string_selftest_init);
#endif /* CONFIG_STRING_SELFTEST */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment