Lines Matching refs:test
36 #define KFENCE_TEST_REQUIRES(test, cond) do { \ argument
38 kunit_skip((test), "Test requires: " #cond); \
186 static size_t setup_test_cache(struct kunit *test, size_t size, slab_flags_t flags, in setup_test_cache() argument
189 if (test->priv != TEST_PRIV_WANT_MEMCACHE) in setup_test_cache()
192 kunit_info(test, "%s: size=%zu, ctor=%ps\n", __func__, size, ctor); in setup_test_cache()
200 KUNIT_ASSERT_TRUE_MSG(test, test_cache, "could not create cache"); in setup_test_cache()
245 static void *test_alloc(struct kunit *test, size_t size, gfp_t gfp, enum allocation_policy policy) in test_alloc() argument
266 kunit_info(test, "%s: size=%zu, gfp=%x, policy=%s, cache=%i\n", __func__, size, gfp, in test_alloc()
297 KUNIT_EXPECT_EQ(test, obj_to_index(s, slab, alloc), 0U); in test_alloc()
298 KUNIT_EXPECT_EQ(test, objs_per_slab(s, slab), 1); in test_alloc()
315 KUNIT_ASSERT_TRUE_MSG(test, false, "failed to allocate from KFENCE"); in test_alloc()
319 static void test_out_of_bounds_read(struct kunit *test) in test_out_of_bounds_read() argument
329 setup_test_cache(test, size, 0, NULL); in test_out_of_bounds_read()
340 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT); in test_out_of_bounds_read()
343 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_out_of_bounds_read()
346 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_out_of_bounds_read()
349 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_out_of_bounds_read()
353 static void test_out_of_bounds_write(struct kunit *test) in test_out_of_bounds_write() argument
363 setup_test_cache(test, size, 0, NULL); in test_out_of_bounds_write()
364 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT); in test_out_of_bounds_write()
367 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_out_of_bounds_write()
371 static void test_use_after_free_read(struct kunit *test) in test_use_after_free_read() argument
380 setup_test_cache(test, size, 0, NULL); in test_use_after_free_read()
381 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_use_after_free_read()
384 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_use_after_free_read()
387 static void test_use_after_free_read_nofault(struct kunit *test) in test_use_after_free_read_nofault() argument
394 setup_test_cache(test, size, 0, NULL); in test_use_after_free_read_nofault()
395 addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_use_after_free_read_nofault()
399 KUNIT_EXPECT_EQ(test, ret, -EFAULT); in test_use_after_free_read_nofault()
400 KUNIT_EXPECT_FALSE(test, report_available()); in test_use_after_free_read_nofault()
403 static void test_double_free(struct kunit *test) in test_double_free() argument
411 setup_test_cache(test, size, 0, NULL); in test_double_free()
412 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_double_free()
415 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_double_free()
418 static void test_invalid_addr_free(struct kunit *test) in test_invalid_addr_free() argument
427 setup_test_cache(test, size, 0, NULL); in test_invalid_addr_free()
428 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_invalid_addr_free()
432 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_invalid_addr_free()
435 static void test_corruption(struct kunit *test) in test_corruption() argument
444 setup_test_cache(test, size, 0, NULL); in test_corruption()
448 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT); in test_corruption()
452 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_corruption()
454 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_corruption()
458 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_corruption()
471 static void test_kmalloc_aligned_oob_read(struct kunit *test) in test_kmalloc_aligned_oob_read() argument
482 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_kmalloc_aligned_oob_read()
489 KUNIT_EXPECT_FALSE(test, report_available()); in test_kmalloc_aligned_oob_read()
496 KUNIT_EXPECT_FALSE(test, report_available()); in test_kmalloc_aligned_oob_read()
501 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_kmalloc_aligned_oob_read()
506 static void test_kmalloc_aligned_oob_write(struct kunit *test) in test_kmalloc_aligned_oob_write() argument
515 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_kmalloc_aligned_oob_write()
522 KUNIT_EXPECT_FALSE(test, report_available()); in test_kmalloc_aligned_oob_write()
524 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_kmalloc_aligned_oob_write()
528 static void test_shrink_memcache(struct kunit *test) in test_shrink_memcache() argument
533 setup_test_cache(test, size, 0, NULL); in test_shrink_memcache()
534 KUNIT_EXPECT_TRUE(test, test_cache); in test_shrink_memcache()
535 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_shrink_memcache()
539 KUNIT_EXPECT_FALSE(test, report_available()); in test_shrink_memcache()
549 static void test_free_bulk(struct kunit *test) in test_free_bulk() argument
554 const size_t size = setup_test_cache(test, get_random_u32_inclusive(8, 307), in test_free_bulk()
557 test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT), in test_free_bulk()
558 test_alloc(test, size, GFP_KERNEL, ALLOCATE_NONE), in test_free_bulk()
559 test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT), in test_free_bulk()
560 test_alloc(test, size, GFP_KERNEL, ALLOCATE_NONE), in test_free_bulk()
561 test_alloc(test, size, GFP_KERNEL, ALLOCATE_NONE), in test_free_bulk()
565 KUNIT_ASSERT_FALSE(test, report_available()); in test_free_bulk()
571 static void test_init_on_free(struct kunit *test) in test_init_on_free() argument
581 KFENCE_TEST_REQUIRES(test, IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON)); in test_init_on_free()
584 setup_test_cache(test, size, 0, NULL); in test_init_on_free()
585 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_init_on_free()
596 KUNIT_EXPECT_EQ(test, expect.addr[i], (char)0); in test_init_on_free()
599 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_init_on_free()
604 static void test_memcache_ctor(struct kunit *test) in test_memcache_ctor() argument
610 setup_test_cache(test, size, 0, ctor_set_x); in test_memcache_ctor()
611 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_memcache_ctor()
614 KUNIT_EXPECT_EQ(test, buf[i], (char)'x'); in test_memcache_ctor()
618 KUNIT_EXPECT_FALSE(test, report_available()); in test_memcache_ctor()
622 static void test_gfpzero(struct kunit *test) in test_gfpzero() argument
629 KFENCE_TEST_REQUIRES(test, kfence_sample_interval <= 100); in test_gfpzero()
631 setup_test_cache(test, size, 0, NULL); in test_gfpzero()
632 buf1 = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_gfpzero()
639 buf2 = test_alloc(test, size, GFP_KERNEL | __GFP_ZERO, ALLOCATE_ANY); in test_gfpzero()
645 kunit_warn(test, "giving up ... cannot get same object back\n"); in test_gfpzero()
652 KUNIT_EXPECT_EQ(test, buf2[i], (char)0); in test_gfpzero()
656 KUNIT_EXPECT_FALSE(test, report_available()); in test_gfpzero()
659 static void test_invalid_access(struct kunit *test) in test_invalid_access() argument
669 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_invalid_access()
673 static void test_memcache_typesafe_by_rcu(struct kunit *test) in test_memcache_typesafe_by_rcu() argument
682 setup_test_cache(test, size, SLAB_TYPESAFE_BY_RCU, NULL); in test_memcache_typesafe_by_rcu()
683 KUNIT_EXPECT_TRUE(test, test_cache); /* Want memcache. */ in test_memcache_typesafe_by_rcu()
685 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_memcache_typesafe_by_rcu()
690 KUNIT_EXPECT_EQ(test, *expect.addr, (char)42); in test_memcache_typesafe_by_rcu()
698 KUNIT_EXPECT_FALSE(test, report_available()); in test_memcache_typesafe_by_rcu()
704 KUNIT_EXPECT_EQ(test, *expect.addr, (char)42); in test_memcache_typesafe_by_rcu()
705 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_memcache_typesafe_by_rcu()
709 static void test_krealloc(struct kunit *test) in test_krealloc() argument
715 .addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY), in test_krealloc()
721 KUNIT_EXPECT_FALSE(test, test_cache); in test_krealloc()
722 KUNIT_EXPECT_EQ(test, ksize(buf), size); /* Precise size match after KFENCE alloc. */ in test_krealloc()
729 KUNIT_EXPECT_GE(test, ksize(buf), size * 3); in test_krealloc()
731 KUNIT_EXPECT_EQ(test, buf[i], (char)(i + 1)); in test_krealloc()
736 KUNIT_EXPECT_GE(test, ksize(buf), size * 2); in test_krealloc()
738 KUNIT_EXPECT_EQ(test, buf[i], (char)(i + 1)); in test_krealloc()
741 KUNIT_EXPECT_EQ(test, (unsigned long)buf, (unsigned long)ZERO_SIZE_PTR); in test_krealloc()
742 KUNIT_ASSERT_FALSE(test, report_available()); /* No reports yet! */ in test_krealloc()
745 KUNIT_ASSERT_TRUE(test, report_matches(&expect)); in test_krealloc()
749 static void test_memcache_alloc_bulk(struct kunit *test) in test_memcache_alloc_bulk() argument
755 setup_test_cache(test, size, 0, NULL); in test_memcache_alloc_bulk()
756 KUNIT_EXPECT_TRUE(test, test_cache); /* Want memcache. */ in test_memcache_alloc_bulk()
783 KUNIT_EXPECT_TRUE(test, pass); in test_memcache_alloc_bulk()
784 KUNIT_EXPECT_FALSE(test, report_available()); in test_memcache_alloc_bulk()
820 static int test_init(struct kunit *test) in test_init() argument
835 if (strstr(test->name, "memcache")) in test_init()
836 test->priv = TEST_PRIV_WANT_MEMCACHE; in test_init()
838 test->priv = NULL; in test_init()
843 static void test_exit(struct kunit *test) in test_exit() argument