Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "../tools/testing/selftests/kselftest_module.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Kernel module for testing 'strscpy' family of functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) KSTM_MODULE_GLOBALS();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * tc() - Run a specific test case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * @src: Source string, argument to strscpy_pad()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * @count: Size of destination buffer, argument to strscpy_pad()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * @expected: Expected return value from call to strscpy_pad()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * @terminator: 1 if there should be a terminating null byte 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * @chars: Number of characters from the src string expected to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *         written to the dst buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * @pad: Number of pad characters expected (in the tail of dst buffer).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *       (@pad does not include the null terminator byte.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * Calls strscpy_pad() and verifies the return value and state of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * destination buffer after the call returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static int __init tc(char *src, int count, int expected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		     int chars, int terminator, int pad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	int nr_bytes_poison;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	int max_expected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	int max_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	char buf[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	int index, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	const char POISON = 'z';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	total_tests++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (!src) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		pr_err("null source string not supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	memset(buf, POISON, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	/* Future proofing test suite, validate args */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	max_expected = count - 1;    /* Space for the null */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (count > max_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		pr_err("count (%d) is too big (%d) ... aborting", count, max_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (expected > max_expected) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		pr_warn("expected (%d) is bigger than can possibly be returned (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			expected, max_expected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	written = strscpy_pad(buf, src, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if ((written) != (expected)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		pr_err("%d != %d (written, expected)\n", written, expected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (count && written == -E2BIG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		if (strncmp(buf, src, count - 1) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			pr_err("buffer state invalid for -E2BIG\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		if (buf[count - 1] != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			pr_err("too big string is not null terminated correctly\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	for (i = 0; i < chars; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		if (buf[i] != src[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			pr_err("buf[i]==%c != src[i]==%c\n", buf[i], src[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (terminator) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		if (buf[count - 1] != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			pr_err("string is not null terminated correctly\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	for (i = 0; i < pad; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		index = chars + terminator + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		if (buf[index] != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			pr_err("padding missing at index: %d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	nr_bytes_poison = sizeof(buf) - chars - terminator - pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	for (i = 0; i < nr_bytes_poison; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		index = sizeof(buf) - 1 - i; /* Check from the end back */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		if (buf[index] != POISON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			pr_err("poison value missing at index: %d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	failed_tests++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static void __init selftest(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	 * tc() uses a destination buffer of size 6 and needs at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	 * least 2 characters spare (one for null and one to check for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	 * overflow).  This means we should only call tc() with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	 * strings up to a maximum of 4 characters long and 'count'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	 * should not exceed 4.  To test with longer strings increase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	 * the buffer size in tc().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	/* tc(src, count, expected, chars, terminator, pad) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	KSTM_CHECK_ZERO(tc("a", 0, -E2BIG, 0, 0, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	KSTM_CHECK_ZERO(tc("", 0, -E2BIG, 0, 0, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	KSTM_CHECK_ZERO(tc("a", 1, -E2BIG, 0, 1, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	KSTM_CHECK_ZERO(tc("", 1, 0, 0, 1, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	KSTM_CHECK_ZERO(tc("ab", 2, -E2BIG, 1, 1, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	KSTM_CHECK_ZERO(tc("a", 2, 1, 1, 1, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	KSTM_CHECK_ZERO(tc("", 2, 0, 0, 1, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	KSTM_CHECK_ZERO(tc("abc", 3, -E2BIG, 2, 1, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	KSTM_CHECK_ZERO(tc("ab", 3, 2, 2, 1, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	KSTM_CHECK_ZERO(tc("a", 3, 1, 1, 1, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	KSTM_CHECK_ZERO(tc("", 3, 0, 0, 1, 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	KSTM_CHECK_ZERO(tc("abcd", 4, -E2BIG, 3, 1, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	KSTM_CHECK_ZERO(tc("abc", 4, 3, 3, 1, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	KSTM_CHECK_ZERO(tc("ab", 4, 2, 2, 1, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	KSTM_CHECK_ZERO(tc("a", 4, 1, 1, 1, 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	KSTM_CHECK_ZERO(tc("", 4, 0, 0, 1, 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) KSTM_MODULE_LOADERS(test_strscpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) MODULE_AUTHOR("Tobin C. Harding <tobin@kernel.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) MODULE_LICENSE("GPL");