1#include "mem/slab/tests/test_internal.h"
2
3TEST_DECLARE_UNIT(slab, random_free_stress, TEST_INTENSITY(256, 2048, 32768)) {
4 ABORT_IF_RAM_LOW();
5
6 size_t n = ctx->intensity_val ? ctx->intensity_val : 2048;
7 void **stress_alloc_free_ptrs =
8 kmalloc(sizeof(void *) * n, ALLOC_FLAGS_ZERO);
9 TEST_ASSERT_NONNULL(stress_alloc_free_ptrs);
10
11 for (size_t i = 0; i < n; i++) {
12 stress_alloc_free_ptrs[i] = kmalloc(64);
13 TEST_ASSERT_NONNULL(stress_alloc_free_ptrs[i]);
14 }
15
16 for (size_t i = 0; i < n; i++) {
17 uint64_t idx = prng_next() % n;
18 if (stress_alloc_free_ptrs[idx]) {
19 kfree(stress_alloc_free_ptrs[idx]);
20 stress_alloc_free_ptrs[idx] = NULL;
21 }
22 }
23
24 for (size_t i = 0; i < n; i++) {
25 if (stress_alloc_free_ptrs[i]) {
26 kfree(stress_alloc_free_ptrs[i]);
27 }
28 }
29
30 kfree(stress_alloc_free_ptrs);
31 return TEST_SUCCESS;
32}
33
34TEST_DECLARE_UNIT(slab, bulk_alloc_free_stress,
35 TEST_INTENSITY(256, 2048, 16384)) {
36 ABORT_IF_RAM_LOW();
37
38 size_t n = ctx->intensity_val ? ctx->intensity_val : 2048;
39 void **mixed_stress_test_ptrs = kmalloc(sizeof(void *) * n);
40 TEST_ASSERT_NONNULL(mixed_stress_test_ptrs);
41
42 for (size_t i = 0; i < n; i++) {
43 mixed_stress_test_ptrs[i] = kmalloc(128);
44 TEST_ASSERT_NONNULL(mixed_stress_test_ptrs[i]);
45 }
46
47 for (size_t i = 0; i < n; i++) {
48 kfree(mixed_stress_test_ptrs[i]);
49 }
50
51 kfree(mixed_stress_test_ptrs);
52 return TEST_SUCCESS;
53}
54
55TEST_DECLARE_UNIT(slab, atomic_behavior_flags) {
56 /* ALLOC_BEHAVIOR_ATOMIC should require nonpageable/nonmovable - allocator
57 or sanitizers might coerce flags. This test ensures allocation doesn't
58 return NULL for such a request. */
59 uint16_t f = ALLOC_FLAG_NONPAGEABLE | ALLOC_FLAG_NONMOVABLE |
60 ALLOC_FLAG_NO_CACHE_ALIGN;
61 void *p = kmalloc_new(size: 256, flags: f, behavior: ALLOC_BEHAVIOR_ATOMIC);
62 if (!p) {
63 test_info("kmalloc_new failed for ATOMIC nonpageable request");
64 return TEST_FAIL(NULL);
65 }
66 /* Do a quick write */
67 volatile uint8_t *b = p;
68 b[0] = 0x7E;
69 if (b[0] != 0x7E) {
70 test_info("atomic allocation memory check failed");
71 kfree_new(ptr: p, behavior: ALLOC_BEHAVIOR_NORMAL);
72 return TEST_FAIL(NULL);
73 }
74 kfree_new(ptr: p, behavior: ALLOC_BEHAVIOR_NORMAL);
75 test_info("behavior (ATOMIC) allocation passed");
76 return TEST_SUCCESS;
77}
78
79TEST_DECLARE(slab, map_new) {
80 return TEST_SUCCESS;
81}
82