Lines Matching full:test

3  * Test cases for KFENCE memory safety error detector. Since the interface with
5 * should verify. For each test case checks the presence (or absence) of
14 #include <kunit/test.h>
55 * KFENCE report and related to the test. in probe_console()
70 /* Check if a report related to the test exists. */
173 /* ===== Test cases ===== */
180 static size_t setup_test_cache(struct kunit *test, size_t size, slab_flags_t flags, in setup_test_cache() argument
183 if (test->priv != TEST_PRIV_WANT_MEMCACHE) in setup_test_cache()
186 kunit_info(test, "%s: size=%zu, ctor=%ps\n", __func__, size, ctor); in setup_test_cache()
194 test_cache = kmem_cache_create("test", size, 1, flags, ctor); in setup_test_cache()
195 KUNIT_ASSERT_TRUE_MSG(test, test_cache, "could not create cache"); in setup_test_cache()
238 static void *test_alloc(struct kunit *test, size_t size, gfp_t gfp, enum allocation_policy policy) in test_alloc() argument
259 kunit_info(test, "%s: size=%zu, gfp=%x, policy=%s, cache=%i\n", __func__, size, gfp, in test_alloc()
289 KUNIT_EXPECT_EQ(test, obj_to_index(s, page, alloc), 0U); in test_alloc()
290 KUNIT_EXPECT_EQ(test, objs_per_slab_page(s, page), 1); in test_alloc()
308 KUNIT_ASSERT_TRUE_MSG(test, false, "failed to allocate from KFENCE"); in test_alloc()
312 static void test_out_of_bounds_read(struct kunit *test) in test_out_of_bounds_read() argument
322 setup_test_cache(test, size, 0, NULL); in test_out_of_bounds_read()
331 /* Test both sides. */ in test_out_of_bounds_read()
333 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT); in test_out_of_bounds_read()
336 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_out_of_bounds_read()
339 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_out_of_bounds_read()
342 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_out_of_bounds_read()
346 static void test_out_of_bounds_write(struct kunit *test) in test_out_of_bounds_write() argument
356 setup_test_cache(test, size, 0, NULL); in test_out_of_bounds_write()
357 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT); in test_out_of_bounds_write()
360 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_out_of_bounds_write()
364 static void test_use_after_free_read(struct kunit *test) in test_use_after_free_read() argument
373 setup_test_cache(test, size, 0, NULL); in test_use_after_free_read()
374 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_use_after_free_read()
377 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_use_after_free_read()
380 static void test_double_free(struct kunit *test) in test_double_free() argument
388 setup_test_cache(test, size, 0, NULL); in test_double_free()
389 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_double_free()
392 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_double_free()
395 static void test_invalid_addr_free(struct kunit *test) in test_invalid_addr_free() argument
404 setup_test_cache(test, size, 0, NULL); in test_invalid_addr_free()
405 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_invalid_addr_free()
409 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_invalid_addr_free()
412 static void test_corruption(struct kunit *test) in test_corruption() argument
421 setup_test_cache(test, size, 0, NULL); in test_corruption()
423 /* Test both sides. */ in test_corruption()
425 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT); in test_corruption()
429 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_corruption()
431 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_corruption()
435 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_corruption()
445 * However, we test that an access to memory beyond the gap results in KFENCE
448 static void test_kmalloc_aligned_oob_read(struct kunit *test) in test_kmalloc_aligned_oob_read() argument
459 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_kmalloc_aligned_oob_read()
466 KUNIT_EXPECT_FALSE(test, report_available()); in test_kmalloc_aligned_oob_read()
473 KUNIT_EXPECT_FALSE(test, report_available()); in test_kmalloc_aligned_oob_read()
478 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_kmalloc_aligned_oob_read()
483 static void test_kmalloc_aligned_oob_write(struct kunit *test) in test_kmalloc_aligned_oob_write() argument
492 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT); in test_kmalloc_aligned_oob_write()
499 KUNIT_EXPECT_FALSE(test, report_available()); in test_kmalloc_aligned_oob_write()
501 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_kmalloc_aligned_oob_write()
504 /* Test cache shrinking and destroying with KFENCE. */
505 static void test_shrink_memcache(struct kunit *test) in test_shrink_memcache() argument
510 setup_test_cache(test, size, 0, NULL); in test_shrink_memcache()
511 KUNIT_EXPECT_TRUE(test, test_cache); in test_shrink_memcache()
512 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_shrink_memcache()
516 KUNIT_EXPECT_FALSE(test, report_available()); in test_shrink_memcache()
526 static void test_free_bulk(struct kunit *test) in test_free_bulk() argument
531 const size_t size = setup_test_cache(test, 8 + prandom_u32_max(300), 0, in test_free_bulk()
534 test_alloc(test, size, GFP_KERNEL, ALLOCATE_RIGHT), in test_free_bulk()
535 test_alloc(test, size, GFP_KERNEL, ALLOCATE_NONE), in test_free_bulk()
536 test_alloc(test, size, GFP_KERNEL, ALLOCATE_LEFT), in test_free_bulk()
537 test_alloc(test, size, GFP_KERNEL, ALLOCATE_NONE), in test_free_bulk()
538 test_alloc(test, size, GFP_KERNEL, ALLOCATE_NONE), in test_free_bulk()
542 KUNIT_ASSERT_FALSE(test, report_available()); in test_free_bulk()
547 /* Test init-on-free works. */
548 static void test_init_on_free(struct kunit *test) in test_init_on_free() argument
562 setup_test_cache(test, size, 0, NULL); in test_init_on_free()
563 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_init_on_free()
574 KUNIT_EXPECT_EQ(test, expect.addr[i], (char)0); in test_init_on_free()
576 if (!i) /* Only check first access to not fail test if page is ever re-protected. */ in test_init_on_free()
577 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_init_on_free()
582 static void test_memcache_ctor(struct kunit *test) in test_memcache_ctor() argument
588 setup_test_cache(test, size, 0, ctor_set_x); in test_memcache_ctor()
589 buf = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_memcache_ctor()
592 KUNIT_EXPECT_EQ(test, buf[i], (char)'x'); in test_memcache_ctor()
596 KUNIT_EXPECT_FALSE(test, report_available()); in test_memcache_ctor()
599 /* Test that memory is zeroed if requested. */
600 static void test_gfpzero(struct kunit *test) in test_gfpzero() argument
607 kunit_warn(test, "skipping ... would take too long\n"); in test_gfpzero()
611 setup_test_cache(test, size, 0, NULL); in test_gfpzero()
612 buf1 = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_gfpzero()
619 buf2 = test_alloc(test, size, GFP_KERNEL | __GFP_ZERO, ALLOCATE_ANY); in test_gfpzero()
625 kunit_warn(test, "giving up ... cannot get same object back\n"); in test_gfpzero()
631 KUNIT_EXPECT_EQ(test, buf2[i], (char)0); in test_gfpzero()
635 KUNIT_EXPECT_FALSE(test, report_available()); in test_gfpzero()
638 static void test_invalid_access(struct kunit *test) in test_invalid_access() argument
648 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_invalid_access()
651 /* Test SLAB_TYPESAFE_BY_RCU works. */
652 static void test_memcache_typesafe_by_rcu(struct kunit *test) in test_memcache_typesafe_by_rcu() argument
661 setup_test_cache(test, size, SLAB_TYPESAFE_BY_RCU, NULL); in test_memcache_typesafe_by_rcu()
662 KUNIT_EXPECT_TRUE(test, test_cache); /* Want memcache. */ in test_memcache_typesafe_by_rcu()
664 expect.addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY); in test_memcache_typesafe_by_rcu()
669 KUNIT_EXPECT_EQ(test, *expect.addr, (char)42); in test_memcache_typesafe_by_rcu()
677 KUNIT_EXPECT_FALSE(test, report_available()); in test_memcache_typesafe_by_rcu()
683 KUNIT_EXPECT_EQ(test, *expect.addr, (char)42); in test_memcache_typesafe_by_rcu()
684 KUNIT_EXPECT_TRUE(test, report_matches(&expect)); in test_memcache_typesafe_by_rcu()
687 /* Test krealloc(). */
688 static void test_krealloc(struct kunit *test) in test_krealloc() argument
694 .addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY), in test_krealloc()
700 KUNIT_EXPECT_FALSE(test, test_cache); in test_krealloc()
701 KUNIT_EXPECT_EQ(test, ksize(buf), size); /* Precise size match after KFENCE alloc. */ in test_krealloc()
708 KUNIT_EXPECT_GE(test, ksize(buf), size * 3); in test_krealloc()
710 KUNIT_EXPECT_EQ(test, buf[i], (char)(i + 1)); in test_krealloc()
715 KUNIT_EXPECT_GE(test, ksize(buf), size * 2); in test_krealloc()
717 KUNIT_EXPECT_EQ(test, buf[i], (char)(i + 1)); in test_krealloc()
720 KUNIT_EXPECT_EQ(test, (unsigned long)buf, (unsigned long)ZERO_SIZE_PTR); in test_krealloc()
721 KUNIT_ASSERT_FALSE(test, report_available()); /* No reports yet! */ in test_krealloc()
724 KUNIT_ASSERT_TRUE(test, report_matches(&expect)); in test_krealloc()
727 /* Test that some objects from a bulk allocation belong to KFENCE pool. */
728 static void test_memcache_alloc_bulk(struct kunit *test) in test_memcache_alloc_bulk() argument
734 setup_test_cache(test, size, 0, NULL); in test_memcache_alloc_bulk()
735 KUNIT_EXPECT_TRUE(test, test_cache); /* Want memcache. */ in test_memcache_alloc_bulk()
762 KUNIT_EXPECT_TRUE(test, pass); in test_memcache_alloc_bulk()
763 KUNIT_EXPECT_FALSE(test, report_available()); in test_memcache_alloc_bulk()
768 * additional info in the name. Set up 2 tests per test case, one using the
796 /* ===== End test cases ===== */
798 static int test_init(struct kunit *test) in test_init() argument
812 /* Any test with 'memcache' in its name will want a memcache. */ in test_init()
813 if (strstr(test->name, "memcache")) in test_init()
814 test->priv = TEST_PRIV_WANT_MEMCACHE; in test_init()
816 test->priv = NULL; in test_init()
821 static void test_exit(struct kunit *test) in test_exit() argument
854 * Because we want to be able to build the test as a module, we need to in kfence_test_init()