summaryrefslogtreecommitdiff
path: root/test/test_slab.cc
blob: 8a448b49c6b8a153a023c4989e59a70629757865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

/*
 * Boot Self-Test: Slab Allocator (kmalloc/kfree)
 *
 * Tests:
 *   1. Small allocation (8 bytes) and free
 *   2. Multiple size classes
 *   3. No aliasing between consecutive allocs
 *   4. Alignment check (minimum 8-byte aligned)
 *   5. Bulk alloc/free stress
 *   6. operator new / operator delete
 *   7. Large allocation (> 2048, falls back to PMM)
 *   8. Zero-size and null-free edge cases
 */

#include <lume/selftest.h>
#include <lume/new.h>
#include <lume/pmm.h>
#include <lume/addr.h>

/* Forward declarations — defined in slab.cc */
void* kmalloc(uint32 size);
void kfree(void* ptr);

void selftest_slab()
{
    st_begin("slab: small alloc (8 bytes)");
    {
        void* p = kmalloc(8);
        ST_ASSERT(p != nullptr);
        // Write and read back to verify the memory is usable
        *static_cast<uint64*>(p) = 0xDEADBEEFCAFEBABEULL;
        ST_ASSERT_EQ(*static_cast<uint64*>(p), 0xDEADBEEFCAFEBABEULL);
        kfree(p);
    }
    st_pass();

    st_begin("slab: multiple size classes");
    {
        uint32 sizes[] = {8, 16, 32, 64, 128, 256, 512, 1024, 2048};
        void* ptrs[9];
        for (int i = 0; i < 9; i++) {
            ptrs[i] = kmalloc(sizes[i]);
            ST_ASSERT(ptrs[i] != nullptr);
            // Write a marker
            *static_cast<uint8*>(ptrs[i]) = static_cast<uint8>(i);
        }
        // Verify markers (no cross-contamination)
        for (int i = 0; i < 9; i++) {
            ST_ASSERT_EQ(*static_cast<uint8*>(ptrs[i]), static_cast<uint8>(i));
        }
        for (int i = 0; i < 9; i++) {
            kfree(ptrs[i]);
        }
    }
    st_pass();

    st_begin("slab: no aliasing");
    {
        void* a = kmalloc(32);
        void* b = kmalloc(32);
        ST_ASSERT(a != nullptr);
        ST_ASSERT(b != nullptr);
        ST_ASSERT_NE(a, b);
        kfree(b);
        kfree(a);
    }
    st_pass();

    st_begin("slab: alignment (8-byte minimum)");
    {
        for (int i = 0; i < 16; i++) {
            void* p = kmalloc(7);  // odd size, should still be 8-aligned
            ST_ASSERT(p != nullptr);
            ST_ASSERT_EQ(reinterpret_cast<uint64>(p) % 8, 0ULL);
            kfree(p);
        }
    }
    st_pass();

    st_begin("slab: bulk alloc/free stress (128 x 64B)");
    {
        constexpr int N = 128;
        void* ptrs[N];
        for (int i = 0; i < N; i++) {
            ptrs[i] = kmalloc(64);
            ST_ASSERT(ptrs[i] != nullptr);
        }
        // Free in reverse order
        for (int i = N - 1; i >= 0; i--) {
            kfree(ptrs[i]);
        }
    }
    st_pass();

    st_begin("slab: operator new/delete");
    {
        struct TestObj {
            uint64 a;
            uint64 b;
        };
        TestObj* obj = new TestObj{42, 99};
        ST_ASSERT(obj != nullptr);
        ST_ASSERT_EQ(obj->a, 42ULL);
        ST_ASSERT_EQ(obj->b, 99ULL);
        delete obj;
    }
    st_pass();

    st_begin("slab: large alloc (4096 bytes, PMM fallback)");
    {
        void* p = kmalloc(4096);
        ST_ASSERT(p != nullptr);
        ST_ASSERT_EQ(reinterpret_cast<uint64>(p) % kPageSize, 0ULL);
        kfree(p);
    }
    st_pass();

    st_begin("slab: edge cases (zero size, null free)");
    {
        void* p = kmalloc(0);
        ST_ASSERT(p == nullptr);
        kfree(nullptr);  // should not crash
    }
    st_pass();
    st_begin("slab: LIFO freelist reallocation (same address)");
    {
        void* a = kmalloc(32);
        kfree(a);
        void* b = kmalloc(32);
        ST_ASSERT_EQ(a, b);
        kfree(b);
    }
    st_pass();

    st_begin("slab: boundary conditions (2048 bytes size class)");
    {
        // 2048 bytes should still use slab (2 objects per page)
        // We allocate 3 to guarantee at least two fall in the exact same slab page
        void* p1 = kmalloc(2048);
        void* p2 = kmalloc(2048);
        void* p3 = kmalloc(2048);
        
        auto diff = [](void* a, void* b) -> uint64 {
            uint64 ua = reinterpret_cast<uint64>(a);
            uint64 ub = reinterpret_cast<uint64>(b);
            return ua > ub ? ua - ub : ub - ua;
        };
        
        // At least one adjacent pair MUST be exactly separated by 2048 bytes
        bool found_2048 = (diff(p1, p2) == 2048ULL) || (diff(p2, p3) == 2048ULL);
        ST_ASSERT(found_2048);
        
        kfree(p1);
        kfree(p2);
        kfree(p3);
    }
    st_pass();

    st_begin("slab: cross-page allocation (> 1 page equivalent objects)");
    {
        // Allocating more than one page worth of 32-byte objects (4096 / 32 = 128)
        constexpr int NUM = 150;
        void* ptrs[NUM];
        for (int i=0; i<NUM; i++) {
            ptrs[i] = kmalloc(32);
            ST_ASSERT(ptrs[i] != nullptr);
        }
        for (int i=0; i<NUM; i++) {
            kfree(ptrs[i]);
        }
    }
    st_pass();

    st_begin("slab: empty page reclamation (verifiable by page availability)");
    {
        // Allocate 128-byte objects until we hit the start of a completely new page.
        void* base_ptr = nullptr;
        void* drain[64];
        int drain_count = 0;
        
        while (true) {
            void* p = kmalloc(128);
            // Because 128 divides 4096 evenly, a new page starts at an exact multiple of kPageSize.
            if (reinterpret_cast<uint64>(p) % kPageSize == 0) {
                base_ptr = p;
                break;
            }
            drain[drain_count++] = p;
        }
        
        // base_ptr is the very first object in a fresh slab page!
        // The page holds exactly 32 objects (4096 / 128 = 32). We already hold 1 (base_ptr).
        void* ptrs[32];
        ptrs[0] = base_ptr;
        for (int i = 1; i < 32; i++) {
            ptrs[i] = kmalloc(128);
        }
        
        // At this point, we completely own all 32 objects of this slab page.
        uint64 page_pa = va_to_pa(virt_addr(reinterpret_cast<uint64>(base_ptr))).raw;
        Page* page = pa_to_page(phys_addr(page_pa));
        
        ST_ASSERT(page->type == PageType::Slab);
        ST_ASSERT(page->slab.obj_count == 32);

        // Free all items in this isolated slab page
        for (int i = 0; i < 32; i++) {
            kfree(ptrs[i]);
        }
        
        // The page MUST have been returned to the PMM (either global Free or Per-CPU Cached).
        ST_ASSERT(page->type == PageType::Free || page->type == PageType::PcpCached);
        
        // Clean up the drain array
        for (int i = 0; i < drain_count; i++) {
            kfree(drain[i]);
        }
    }
    st_pass();

    st_begin("slab: continuous operator new/delete memory leak loop");
    {
        struct BigObj { uint8 data[512]; };
        BigObj* ptr = nullptr;
        // 10K allocations to ensure no memory leak causes OOM
        for (int i = 0; i < 10000; i++) {
            ptr = new BigObj;
            ST_ASSERT(ptr != nullptr);
            delete ptr;
        }
    }
    st_pass();

    st_begin("slab: allocation pressure (mixed sizes)");
    {
        constexpr int MAX_ITEMS = 100;
        void* ptrs[MAX_ITEMS];
        uint32 sizes[MAX_ITEMS];
        
        for (int i = 0; i < MAX_ITEMS; i++) {
            sizes[i] = (i * 17) % 2000 + 8; // Pseudo-random sizes between 8 and 2008
            ptrs[i] = kmalloc(sizes[i]);
            ST_ASSERT(ptrs[i] != nullptr);
            ST_ASSERT(reinterpret_cast<uintptr>(ptrs[i]) % 8 == 0);
        }
        
        for (int i = 0; i < MAX_ITEMS; i++) {
            kfree(ptrs[i]);
        }
    }
    st_pass();
}