Skip to content

Commit f8d22a3

Browse files
committed
Merge tag 'linux_kselftest-kunit-6.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest
Pull KUnit updates from Shuah Khan: - add vm_mmap() allocation resource manager - convert usercopy kselftest to KUnit - disable usercopy testing on !CONFIG_MMU - add MODULE_DESCRIPTION() to core, list, and usercopy tests - add tests for assertion formatting functions - assert.c - introduce KUNIT_ASSERT_MEMEQ and KUNIT_ASSERT_MEMNEQ macros - fix KUNIT_ASSERT_STRNEQ comments to make it clear that it is an assertion - rename KUNIT_ASSERT_FAILURE to KUNIT_FAIL_AND_ABORT * tag 'linux_kselftest-kunit-6.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: kunit: Introduce KUNIT_ASSERT_MEMEQ and KUNIT_ASSERT_MEMNEQ macros kunit: Rename KUNIT_ASSERT_FAILURE to KUNIT_FAIL_AND_ABORT for readability kunit: Fix the comment of KUNIT_ASSERT_STRNEQ as assertion kunit: executor: Simplify string allocation handling kunit/usercopy: Add missing MODULE_DESCRIPTION() kunit/usercopy: Disable testing on !CONFIG_MMU usercopy: Convert test_user_copy to KUnit test kunit: test: Add vm_mmap() allocation resource manager list: test: add the missing MODULE_DESCRIPTION() macro kunit: add missing MODULE_DESCRIPTION() macros to core modules list: test: remove unused struct 'klist_test_struct' kunit: Cover 'assert.c' with tests
2 parents 9de4ad3 + ebf51e4 commit f8d22a3

20 files changed

+978
-373
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12034,6 +12034,7 @@ F: arch/*/configs/hardening.config
1203412034
F: include/linux/overflow.h
1203512035
F: include/linux/randomize_kstack.h
1203612036
F: kernel/configs/hardening.config
12037+
F: lib/usercopy_kunit.c
1203712038
F: mm/usercopy.c
1203812039
K: \b(add|choose)_random_kstack_offset\b
1203912040
K: \b__check_(object_size|heap_object)\b

drivers/input/tests/input_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static int input_test_init(struct kunit *test)
3131
ret = input_register_device(input_dev);
3232
if (ret) {
3333
input_free_device(input_dev);
34-
KUNIT_ASSERT_FAILURE(test, "Register device failed: %d", ret);
34+
KUNIT_FAIL_AND_ABORT(test, "Register device failed: %d", ret);
3535
}
3636

3737
test->priv = input_dev;

include/kunit/assert.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void kunit_assert_prologue(const struct kunit_loc *loc,
6060
* struct kunit_fail_assert - Represents a plain fail expectation/assertion.
6161
* @assert: The parent of this type.
6262
*
63-
* Represents a simple KUNIT_FAIL/KUNIT_ASSERT_FAILURE that always fails.
63+
* Represents a simple KUNIT_FAIL/KUNIT_FAIL_AND_ABORT that always fails.
6464
*/
6565
struct kunit_fail_assert {
6666
struct kunit_assert assert;
@@ -218,4 +218,15 @@ void kunit_mem_assert_format(const struct kunit_assert *assert,
218218
const struct va_format *message,
219219
struct string_stream *stream);
220220

221+
#if IS_ENABLED(CONFIG_KUNIT)
222+
void kunit_assert_print_msg(const struct va_format *message,
223+
struct string_stream *stream);
224+
bool is_literal(const char *text, long long value);
225+
bool is_str_literal(const char *text, const char *value);
226+
void kunit_assert_hexdump(struct string_stream *stream,
227+
const void *buf,
228+
const void *compared_buf,
229+
const size_t len);
230+
#endif
231+
221232
#endif /* _KUNIT_ASSERT_H */

include/kunit/test.h

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,23 @@ static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp
480480
return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
481481
}
482482

483+
/**
484+
* kunit_vm_mmap() - Allocate KUnit-tracked vm_mmap() area
485+
* @test: The test context object.
486+
* @file: struct file pointer to map from, if any
487+
* @addr: desired address, if any
488+
* @len: how many bytes to allocate
489+
* @prot: mmap PROT_* bits
490+
* @flag: mmap flags
491+
* @offset: offset into @file to start mapping from.
492+
*
493+
* See vm_mmap() for more information.
494+
*/
495+
unsigned long kunit_vm_mmap(struct kunit *test, struct file *file,
496+
unsigned long addr, unsigned long len,
497+
unsigned long prot, unsigned long flag,
498+
unsigned long offset);
499+
483500
void kunit_cleanup(struct kunit *test);
484501

485502
void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...);
@@ -1211,7 +1228,18 @@ do { \
12111228
fmt, \
12121229
##__VA_ARGS__)
12131230

1214-
#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1231+
/**
1232+
* KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated.
1233+
* @test: The test context object.
1234+
* @fmt: an informational message to be printed when the assertion is made.
1235+
* @...: string format arguments.
1236+
*
1237+
* The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In
1238+
* other words, it always results in a failed assertion, and consequently
1239+
* always causes the test case to fail and abort when evaluated.
1240+
* See KUNIT_ASSERT_TRUE() for more information.
1241+
*/
1242+
#define KUNIT_FAIL_AND_ABORT(test, fmt, ...) \
12151243
KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
12161244

12171245
/**
@@ -1438,12 +1466,12 @@ do { \
14381466
##__VA_ARGS__)
14391467

14401468
/**
1441-
* KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1469+
* KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal.
14421470
* @test: The test context object.
14431471
* @left: an arbitrary expression that evaluates to a null terminated string.
14441472
* @right: an arbitrary expression that evaluates to a null terminated string.
14451473
*
1446-
* Sets an expectation that the values that @left and @right evaluate to are
1474+
* Sets an assertion that the values that @left and @right evaluate to are
14471475
* not equal. This is semantically equivalent to
14481476
* KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
14491477
* for more information.
@@ -1458,6 +1486,60 @@ do { \
14581486
fmt, \
14591487
##__VA_ARGS__)
14601488

1489+
/**
1490+
* KUNIT_ASSERT_MEMEQ() - Asserts that the first @size bytes of @left and @right are equal.
1491+
* @test: The test context object.
1492+
* @left: An arbitrary expression that evaluates to the specified size.
1493+
* @right: An arbitrary expression that evaluates to the specified size.
1494+
* @size: Number of bytes compared.
1495+
*
1496+
* Sets an assertion that the values that @left and @right evaluate to are
1497+
* equal. This is semantically equivalent to
1498+
* KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1499+
* KUNIT_ASSERT_TRUE() for more information.
1500+
*
1501+
* Although this assertion works for any memory block, it is not recommended
1502+
* for comparing more structured data, such as structs. This assertion is
1503+
* recommended for comparing, for example, data arrays.
1504+
*/
1505+
#define KUNIT_ASSERT_MEMEQ(test, left, right, size) \
1506+
KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL)
1507+
1508+
#define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1509+
KUNIT_MEM_ASSERTION(test, \
1510+
KUNIT_ASSERTION, \
1511+
left, ==, right, \
1512+
size, \
1513+
fmt, \
1514+
##__VA_ARGS__)
1515+
1516+
/**
1517+
* KUNIT_ASSERT_MEMNEQ() - Asserts that the first @size bytes of @left and @right are not equal.
1518+
* @test: The test context object.
1519+
* @left: An arbitrary expression that evaluates to the specified size.
1520+
* @right: An arbitrary expression that evaluates to the specified size.
1521+
* @size: Number of bytes compared.
1522+
*
1523+
* Sets an assertion that the values that @left and @right evaluate to are
1524+
* not equal. This is semantically equivalent to
1525+
* KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1526+
* KUNIT_ASSERT_TRUE() for more information.
1527+
*
1528+
* Although this assertion works for any memory block, it is not recommended
1529+
* for comparing more structured data, such as structs. This assertion is
1530+
* recommended for comparing, for example, data arrays.
1531+
*/
1532+
#define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \
1533+
KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL)
1534+
1535+
#define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1536+
KUNIT_MEM_ASSERTION(test, \
1537+
KUNIT_ASSERTION, \
1538+
left, !=, right, \
1539+
size, \
1540+
fmt, \
1541+
##__VA_ARGS__)
1542+
14611543
/**
14621544
* KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
14631545
* @test: The test context object.

kernel/fork.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@
115115
#define CREATE_TRACE_POINTS
116116
#include <trace/events/task.h>
117117

118+
#include <kunit/visibility.h>
119+
118120
/*
119121
* Minimum number of threads to boot the kernel
120122
*/
@@ -1328,6 +1330,7 @@ struct mm_struct *mm_alloc(void)
13281330
memset(mm, 0, sizeof(*mm));
13291331
return mm_init(mm, current, current_user_ns());
13301332
}
1333+
EXPORT_SYMBOL_IF_KUNIT(mm_alloc);
13311334

13321335
static inline void __mmput(struct mm_struct *mm)
13331336
{

lib/Kconfig.debug

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,18 +2505,6 @@ config TEST_VMALLOC
25052505

25062506
If unsure, say N.
25072507

2508-
config TEST_USER_COPY
2509-
tristate "Test user/kernel boundary protections"
2510-
depends on m
2511-
help
2512-
This builds the "test_user_copy" module that runs sanity checks
2513-
on the copy_to/from_user infrastructure, making sure basic
2514-
user/kernel boundary testing is working. If it fails to load,
2515-
a regression has been detected in the user/kernel memory boundary
2516-
protections.
2517-
2518-
If unsure, say N.
2519-
25202508
config TEST_BPF
25212509
tristate "Test BPF filter functionality"
25222510
depends on m && NET
@@ -2814,6 +2802,15 @@ config SIPHASH_KUNIT_TEST
28142802
This is intended to help people writing architecture-specific
28152803
optimized versions. If unsure, say N.
28162804

2805+
config USERCOPY_KUNIT_TEST
2806+
tristate "KUnit Test for user/kernel boundary protections"
2807+
depends on KUNIT
2808+
default KUNIT_ALL_TESTS
2809+
help
2810+
This builds the "usercopy_kunit" module that runs sanity checks
2811+
on the copy_to/from_user infrastructure, making sure basic
2812+
user/kernel boundary testing is working.
2813+
28172814
config TEST_UDELAY
28182815
tristate "udelay test driver"
28192816
help

lib/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ obj-$(CONFIG_TEST_LKM) += test_module.o
7878
obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o
7979
obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
8080
obj-$(CONFIG_TEST_SORT) += test_sort.o
81-
obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
8281
obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
8382
obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
8483
obj-$(CONFIG_TEST_DYNAMIC_DEBUG) += test_dynamic_debug.o
@@ -388,6 +387,7 @@ CFLAGS_fortify_kunit.o += $(call cc-disable-warning, stringop-truncation)
388387
CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN)
389388
obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
390389
obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o
390+
obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o
391391

392392
obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
393393

lib/kunit/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ obj-$(CONFIG_KUNIT) += kunit.o
22

33
kunit-objs += test.o \
44
resource.o \
5+
user_alloc.o \
56
static_stub.o \
67
string-stream.o \
78
assert.o \
@@ -22,6 +23,7 @@ obj-$(CONFIG_KUNIT_TEST) += kunit-test.o
2223
# string-stream-test compiles built-in only.
2324
ifeq ($(CONFIG_KUNIT_TEST),y)
2425
obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o
26+
obj-$(CONFIG_KUNIT_TEST) += assert_test.o
2527
endif
2628

2729
obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o

lib/kunit/assert.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
#include <kunit/assert.h>
99
#include <kunit/test.h>
10+
#include <kunit/visibility.h>
1011

1112
#include "string-stream.h"
1213

@@ -30,8 +31,9 @@ void kunit_assert_prologue(const struct kunit_loc *loc,
3031
}
3132
EXPORT_SYMBOL_GPL(kunit_assert_prologue);
3233

33-
static void kunit_assert_print_msg(const struct va_format *message,
34-
struct string_stream *stream)
34+
VISIBLE_IF_KUNIT
35+
void kunit_assert_print_msg(const struct va_format *message,
36+
struct string_stream *stream)
3537
{
3638
if (message->fmt)
3739
string_stream_add(stream, "\n%pV", message);
@@ -89,7 +91,7 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
8991
EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
9092

9193
/* Checks if `text` is a literal representing `value`, e.g. "5" and 5 */
92-
static bool is_literal(const char *text, long long value)
94+
VISIBLE_IF_KUNIT bool is_literal(const char *text, long long value)
9395
{
9496
char *buffer;
9597
int len;
@@ -166,7 +168,7 @@ EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
166168
/* Checks if KUNIT_EXPECT_STREQ() args were string literals.
167169
* Note: `text` will have ""s where as `value` will not.
168170
*/
169-
static bool is_str_literal(const char *text, const char *value)
171+
VISIBLE_IF_KUNIT bool is_str_literal(const char *text, const char *value)
170172
{
171173
int len;
172174

@@ -208,10 +210,11 @@ EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
208210
/* Adds a hexdump of a buffer to a string_stream comparing it with
209211
* a second buffer. The different bytes are marked with <>.
210212
*/
211-
static void kunit_assert_hexdump(struct string_stream *stream,
212-
const void *buf,
213-
const void *compared_buf,
214-
const size_t len)
213+
VISIBLE_IF_KUNIT
214+
void kunit_assert_hexdump(struct string_stream *stream,
215+
const void *buf,
216+
const void *compared_buf,
217+
const size_t len)
215218
{
216219
size_t i;
217220
const u8 *buf1 = buf;

0 commit comments

Comments
 (0)