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