Lines Matching refs:test

35 #define KFENCE_TEST_REQUIRES(test, cond) do {			\  argument
37 kunit_skip((test), "Test requires: " #cond); \
185 static size_t setup_test_cache(struct kunit *test, size_t size, slab_flags_t flags, in setup_test_cache() argument
188 if (test->priv != TEST_PRIV_WANT_MEMCACHE) in setup_test_cache()
191 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()
243 static void *test_alloc(struct kunit *test, size_t size, gfp_t gfp, enum allocation_policy policy) in test_alloc() argument
264 kunit_info(test, "%s: size=%zu, gfp=%x, policy=%s, cache=%i\n", __func__, size, gfp, in test_alloc()
294 KUNIT_EXPECT_EQ(test, obj_to_index(s, page, alloc), 0U); in test_alloc()
295 KUNIT_EXPECT_EQ(test, objs_per_slab_page(s, page), 1); in test_alloc()
313 KUNIT_ASSERT_TRUE_MSG(test, false, "failed to allocate from KFENCE"); in test_alloc()
317 static void test_out_of_bounds_read(struct kunit *test) in test_out_of_bounds_read() argument
327 setup_test_cache(test, size, 0, NULL); in test_out_of_bounds_read()
338 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT); in test_out_of_bounds_read()
341 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_out_of_bounds_read()
344 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_out_of_bounds_read()
347 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_out_of_bounds_read()
351 static void test_out_of_bounds_write(struct kunit *test) in test_out_of_bounds_write() argument
361 setup_test_cache(test, size, 0, NULL); in test_out_of_bounds_write()
362 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT); in test_out_of_bounds_write()
365 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_out_of_bounds_write()
369 static void test_use_after_free_read(struct kunit *test) in test_use_after_free_read() argument
378 setup_test_cache(test, size, 0, NULL); in test_use_after_free_read()
379 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_use_after_free_read()
382 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_use_after_free_read()
385 static void test_double_free(struct kunit *test) in test_double_free() argument
393 setup_test_cache(test, size, 0, NULL); in test_double_free()
394 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_double_free()
397 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_double_free()
400 static void test_invalid_addr_free(struct kunit *test) in test_invalid_addr_free() argument
409 setup_test_cache(test, size, 0, NULL); in test_invalid_addr_free()
410 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_invalid_addr_free()
414 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_invalid_addr_free()
417 static void test_corruption(struct kunit *test) in test_corruption() argument
426 setup_test_cache(test, size, 0, NULL); in test_corruption()
430 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT); in test_corruption()
434 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_corruption()
436 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_corruption()
440 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_corruption()
453 static void test_kmalloc_aligned_oob_read(struct kunit *test) in test_kmalloc_aligned_oob_read() argument
464 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_kmalloc_aligned_oob_read()
471 KUNIT_EXPECT_FALSE(test, report_available()); in test_kmalloc_aligned_oob_read()
478 KUNIT_EXPECT_FALSE(test, report_available()); in test_kmalloc_aligned_oob_read()
483 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_kmalloc_aligned_oob_read()
488 static void test_kmalloc_aligned_oob_write(struct kunit *test) in test_kmalloc_aligned_oob_write() argument
497 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_kmalloc_aligned_oob_write()
504 KUNIT_EXPECT_FALSE(test, report_available()); in test_kmalloc_aligned_oob_write()
506 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_kmalloc_aligned_oob_write()
510 static void test_shrink_memcache(struct kunit *test) in test_shrink_memcache() argument
515 setup_test_cache(test, size, 0, NULL); in test_shrink_memcache()
516 KUNIT_EXPECT_TRUE(test, test_cache); in test_shrink_memcache()
517 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_shrink_memcache()
521 KUNIT_EXPECT_FALSE(test, report_available()); in test_shrink_memcache()
531 static void test_free_bulk(struct kunit *test) in test_free_bulk() argument
536 const size_t size = setup_test_cache(test, 8 + prandom_u32_max(300), 0, in test_free_bulk()
539 test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT), in test_free_bulk()
540 test_alloc(test, size, GFP_KERNEL, ALLOCATE_NONE), in test_free_bulk()
541 test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT), in test_free_bulk()
542 test_alloc(test, size, GFP_KERNEL, ALLOCATE_NONE), in test_free_bulk()
543 test_alloc(test, size, GFP_KERNEL, ALLOCATE_NONE), in test_free_bulk()
547 KUNIT_ASSERT_FALSE(test, report_available()); in test_free_bulk()
553 static void test_init_on_free(struct kunit *test) in test_init_on_free() argument
563 KFENCE_TEST_REQUIRES(test, IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON)); in test_init_on_free()
566 setup_test_cache(test, size, 0, NULL); in test_init_on_free()
567 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_init_on_free()
578 KUNIT_EXPECT_EQ(test, expect.addr[i], (char)0); in test_init_on_free()
581 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_init_on_free()
586 static void test_memcache_ctor(struct kunit *test) in test_memcache_ctor() argument
592 setup_test_cache(test, size, 0, ctor_set_x); in test_memcache_ctor()
593 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_memcache_ctor()
596 KUNIT_EXPECT_EQ(test, buf[i], (char)'x'); in test_memcache_ctor()
600 KUNIT_EXPECT_FALSE(test, report_available()); in test_memcache_ctor()
604 static void test_gfpzero(struct kunit *test) in test_gfpzero() argument
611 KFENCE_TEST_REQUIRES(test, CONFIG_KFENCE_SAMPLE_INTERVAL <= 100); in test_gfpzero()
613 setup_test_cache(test, size, 0, NULL); in test_gfpzero()
614 buf1 = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_gfpzero()
621 buf2 = test_alloc(test, size, GFP_KERNEL | __GFP_ZERO, ALLOCATE_ANY); in test_gfpzero()
627 kunit_warn(test, "giving up ... cannot get same object back\n"); in test_gfpzero()
633 KUNIT_EXPECT_EQ(test, buf2[i], (char)0); in test_gfpzero()
637 KUNIT_EXPECT_FALSE(test, report_available()); in test_gfpzero()
640 static void test_invalid_access(struct kunit *test) in test_invalid_access() argument
650 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_invalid_access()
654 static void test_memcache_typesafe_by_rcu(struct kunit *test) in test_memcache_typesafe_by_rcu() argument
663 setup_test_cache(test, size, SLAB_TYPESAFE_BY_RCU, NULL); in test_memcache_typesafe_by_rcu()
664 KUNIT_EXPECT_TRUE(test, test_cache); /* Want memcache. */ in test_memcache_typesafe_by_rcu()
666 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_memcache_typesafe_by_rcu()
671 KUNIT_EXPECT_EQ(test, *expect.addr, (char)42); in test_memcache_typesafe_by_rcu()
679 KUNIT_EXPECT_FALSE(test, report_available()); in test_memcache_typesafe_by_rcu()
685 KUNIT_EXPECT_EQ(test, *expect.addr, (char)42); in test_memcache_typesafe_by_rcu()
686 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_memcache_typesafe_by_rcu()
690 static void test_krealloc(struct kunit *test) in test_krealloc() argument
696 .addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY), in test_krealloc()
702 KUNIT_EXPECT_FALSE(test, test_cache); in test_krealloc()
703 KUNIT_EXPECT_EQ(test, ksize(buf), size); /* Precise size match after KFENCE alloc. */ in test_krealloc()
710 KUNIT_EXPECT_GE(test, ksize(buf), size * 3); in test_krealloc()
712 KUNIT_EXPECT_EQ(test, buf[i], (char)(i + 1)); in test_krealloc()
717 KUNIT_EXPECT_GE(test, ksize(buf), size * 2); in test_krealloc()
719 KUNIT_EXPECT_EQ(test, buf[i], (char)(i + 1)); in test_krealloc()
722 KUNIT_EXPECT_EQ(test, (unsigned long)buf, (unsigned long)ZERO_SIZE_PTR); in test_krealloc()
723 KUNIT_ASSERT_FALSE(test, report_available()); /* No reports yet! */ in test_krealloc()
726 KUNIT_ASSERT_TRUE(test, report_matches(&expect)); in test_krealloc()
730 static void test_memcache_alloc_bulk(struct kunit *test) in test_memcache_alloc_bulk() argument
736 setup_test_cache(test, size, 0, NULL); in test_memcache_alloc_bulk()
737 KUNIT_EXPECT_TRUE(test, test_cache); /* Want memcache. */ in test_memcache_alloc_bulk()
764 KUNIT_EXPECT_TRUE(test, pass); in test_memcache_alloc_bulk()
765 KUNIT_EXPECT_FALSE(test, report_available()); in test_memcache_alloc_bulk()
800 static int test_init(struct kunit *test) in test_init() argument
815 if (strstr(test->name, "memcache")) in test_init()
816 test->priv = TEST_PRIV_WANT_MEMCACHE; in test_init()
818 test->priv = NULL; in test_init()
823 static void test_exit(struct kunit *test) in test_exit() argument