aboutsummaryrefslogtreecommitdiff
path: root/tests/test_memory.c
blob: 407aa3de916f2a84c76bca4989c2a7cd501dce34 (plain) (blame)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#ifndef FOR_TESTING
#define FOR_TESTING
#endif

#include <stdlib.h>

#include "arch.h"
#include "kern/common.h"
#include "kern/mem.h"
#include "test_harness.c"

struct TEST_STRUCT {
  uint32_t array[3];
};

struct TEST_STRUCT2 {
  uint32_t array[16];
};

/* Copy of the node structure. */
// The sizes will count the number of WORDS allocated.
// Since there's a max size of 16k, only 12 bits will be
// needed for this.
typedef struct KALLOC_NODE {
  uint16_t size_words;
  uint16_t prev;
  /*
   * LSB is whether this block is used.
   *
   * Rest of the bits must equal (0xdeadbeee >> 1)
   */
  uint32_t used_and_canary;

  uint8_t mem[]; /* The memory to use. */
} PACKED kalloc_node_t;

extern kalloc_node_t* kalloc_start;

static void wipeout_kalloc()
{
  memset(kalloc_start, 0, 1024);
  kalloc_start = NULL;
}

static struct TEST_STRUCT* new_test_struct()
{
  struct TEST_STRUCT* ret = kalloc(sizeof(struct TEST_STRUCT));

  ret->array[0] = 1;
  ret->array[1] = 2;
  ret->array[2] = 3;

  return ret;
}

static struct TEST_STRUCT2* new_test_struct2()
{
  struct TEST_STRUCT2* ret = kalloc(sizeof(struct TEST_STRUCT2));

  for (int i = 0; i < 10; ++i) {
    ret->array[i] = i;
  }

  return ret;
}

#define ASSERT_CHAIN(t1, t2) \
  ASSERT_EQ(V(t1) + sizeof(*t1) + sizeof(kalloc_node_t), V(t2))

TEST(memory, kalloc)
{
#define V(x) ((void*)(x))
  struct TEST_STRUCT* test1 = new_test_struct();
  struct TEST_STRUCT2* test2 = new_test_struct2();
  struct TEST_STRUCT* test3 = new_test_struct();
  struct TEST_STRUCT2* test4 = new_test_struct2();
  struct TEST_STRUCT2* test5 = new_test_struct2();

  ASSERT_TRUE(V(test1) != V(test2));
  ASSERT_TRUE(V(test2) != V(test3));
  ASSERT_TRUE(V(test3) != V(test1));
  ASSERT_TRUE(V(test2) != V(test5));
  ASSERT_TRUE(V(test4) != V(test5));

  ASSERT_CHAIN(test1, test2);
  ASSERT_CHAIN(test2, test3);
  ASSERT_CHAIN(test3, test4);
  ASSERT_CHAIN(test4, test5);

  char buf[1024];
  if (debug_kalloc_assert_consistency(buf, 1024)) {
    fprintf(stderr, "Consistency check failed. (%s:%d)\n", __FILE__, __LINE__);
    fprintf(stderr, "%s", buf);
    ASSERT_TRUE(false);
  }

  wipeout_kalloc();

  return 0;
}

struct UNEVEN_STRUCT {
  uint8_t arr[5];
};

struct UNEVEN_STRUCT* new_uneven_struct()
{
  struct UNEVEN_STRUCT* ret = kalloc(sizeof(struct UNEVEN_STRUCT));

  ret->arr[0] = 1;
  ret->arr[1] = 2;
  ret->arr[2] = 3;
  ret->arr[3] = 4;
  ret->arr[4] = 5;

  return ret;
}

#define size_for(n) (((n) / 4) + ((n) % 4 != 0))

TEST(memory, uneven_kalloc)
{
  if (kalloc_start) {
    wipeout_kalloc();
  }

  struct UNEVEN_STRUCT* test1 = new_uneven_struct();
  struct UNEVEN_STRUCT* test2 = new_uneven_struct();

  ASSERT_EQ(V(test1) + 8 + sizeof(kalloc_node_t), test2);

  wipeout_kalloc();

  return 0;
}

#define ASSERT_KALLOC_EMPTY()                                                                                \
  ASSERT_EQ(                                                                                                 \
      (void*)((uint8_t*)kalloc_start + kalloc_start->size_words * sizeof(uint32_t) + sizeof(kalloc_node_t)), \
      (void*)&HEAP_STOP)

TEST(memory, kalloc_free)
{
  if (kalloc_start) {
    wipeout_kalloc();
  }

  kalloc_init();

  ASSERT_KALLOC_EMPTY();

  struct TEST_STRUCT* test1 = new_test_struct();
  struct TEST_STRUCT2* test2 = new_test_struct2();
  struct TEST_STRUCT* test3 = new_test_struct();
  struct TEST_STRUCT2* test4 = new_test_struct2();
  struct TEST_STRUCT2* test5 = new_test_struct2();

  kfree(test2);
  kfree(test4);
  kfree(test3);
  kfree(test1);
  kfree(test5);

  ASSERT_KALLOC_EMPTY();

  test1 = new_test_struct();
  test2 = new_test_struct2();
  test3 = new_test_struct();
  test4 = new_test_struct2();
  test5 = new_test_struct2();

  kfree(test1);
  kfree(test3);
  kfree(test2);
  kfree(test4);
  kfree(test5);

  ASSERT_KALLOC_EMPTY();

  test1 = new_test_struct();
  test2 = new_test_struct2();
  test3 = new_test_struct();
  test4 = new_test_struct2();
  test5 = new_test_struct2();

  kfree(test4);
  kfree(test3);
  kfree(test1);
  kfree(test2);
  kfree(test5);

  ASSERT_KALLOC_EMPTY();

  wipeout_kalloc();

  return 0;
}

TEST(memory, kalloc_free_alloc2)
{
  if (kalloc_start) {
    wipeout_kalloc();
  }

  struct TEST_STRUCT2* test1 = new_test_struct2();
  struct TEST_STRUCT2* test2 = new_test_struct2();

  kfree(test1);

  struct TEST_STRUCT* test3 = new_test_struct();
  struct TEST_STRUCT* test4 = new_test_struct();

  ASSERT_EQ(debug_kalloc_get_next_ptr(test3), V(test4));

  ASSERT_EQ(
      // There should be a free block after test4.
      debug_kalloc_get_next_ptr(debug_kalloc_get_next_ptr(test4)),
      V(test2));

  ASSERT_EQ(
      // There should be a free block after test4.
      debug_kalloc_get_prev_ptr(debug_kalloc_get_prev_ptr(test2)),
      V(test4));

  char buf[1024];
  if (debug_kalloc_assert_consistency(buf, 1024)) {
    fprintf(stderr, "Consistency check failed.\n");
    fprintf(stderr, "%s", buf);
    ASSERT_TRUE(false);
  }

  return 0;
}

TEST(memory, relink_backref_after_free)
{
  if (kalloc_start) {
    wipeout_kalloc();
  }

  struct TEST_STRUCT* test2 = new_test_struct();
  struct TEST_STRUCT* test3 = new_test_struct();

  kfree(test2);
  kfree(test3);

  char buf[1024];
  if (debug_kalloc_assert_consistency(buf, 1024)) {
    fprintf(stderr, "Consistency check failed.\n");
    fprintf(stderr, "%s", buf);
    ASSERT_TRUE(false);
  }

  return 0;
}

TEST(memory, alloc1)
{
  char buf[1024];

  kalloc(5);
  kalloc(1);

  if (debug_kalloc_assert_consistency(buf, 1024)) {
    fprintf(stderr, "Consistency check failed. (%s:%d)\n", __FILE__, __LINE__);
    fprintf(stderr, "%s", buf);
    ASSERT_TRUE(false);
  }

  return 0;
}

TEST(memory, alloc0)
{
  char buf[1024];

  kalloc(5);
  kalloc(0);

  if (debug_kalloc_assert_consistency(buf, 1024)) {
    fprintf(stderr, "Consistency check failed. (%s:%d)\n", __FILE__, __LINE__);
    fprintf(stderr, "%s", buf);
    ASSERT_TRUE(false);
  }

  return 0;
}

TEST(memory, consistency_stress)
{
#define NRUNS 500
  if (kalloc_start) {
    wipeout_kalloc();
  }

  int i;
  void* allocd[NRUNS] = {0};
  char buf[1024];
  void* tofree;

  for (i = 0; i < NRUNS; ++i) {
    size_t nalloc = rand() % 20;
    allocd[i] = kalloc(nalloc);

    if (debug_kalloc_assert_consistency(buf, 1024)) {
      fprintf(
          stderr,
          "Consistency check failed. (At index=%d, %s:%d, nalloc=%ld)\n",
          i,
          __FILE__,
          __LINE__,
          nalloc);
      fprintf(stderr, "%s", buf);
      ASSERT_TRUE(false);
    }

    ASSERT_TRUE(allocd[i]);

    memset(allocd[i], 0xFF, nalloc);
    size_t idx = rand() % NRUNS;

    if (debug_kalloc_assert_consistency(buf, 1024)) {
      fprintf(
          stderr,
          "Consistency check failed. (At index=%d, %s:%d)\n",
          i,
          __FILE__,
          __LINE__);
      fprintf(stderr, "%s", buf);
      ASSERT_TRUE(false);
    }

    tofree = allocd[idx];
    kfree(tofree);
    allocd[idx] = NULL;

    if (debug_kalloc_assert_consistency(buf, 1024)) {
      fprintf(
          stderr,
          "Consistency check failed. (At index=%d, %s:%d)\n",
          i,
          __FILE__,
          __LINE__);
      fprintf(stderr, "%s", buf);
      ASSERT_TRUE(false);
    }

    idx = rand() % NRUNS;
    tofree = allocd[idx];
    kfree(tofree);
    allocd[idx] = NULL;

    if (debug_kalloc_assert_consistency(buf, 1024)) {
      fprintf(
          stderr,
          "Consistency check failed. (At index=%d, %s:%d)\n",
          i,
          __FILE__,
          __LINE__);
      fprintf(stderr, "%s", buf);
      ASSERT_TRUE(false);
    }
  }

  for (i = 0; i < NRUNS; ++i) {
    if (allocd[i]) {
      kfree(allocd[i]);
    }

    if (debug_kalloc_assert_consistency(buf, 1024)) {
      fprintf(
          stderr,
          "Consistency check failed. (At index=%d, %s:%d)\n",
          i,
          __FILE__,
          __LINE__);
      fprintf(stderr, "%s", buf);
      ASSERT_TRUE(false);
    }
  }

  ASSERT_KALLOC_EMPTY();

  return 0;
}

TEST(memory, kalloc_free_alloc)
{
  if (kalloc_start) {
    wipeout_kalloc();
  }

  new_test_struct();
  struct TEST_STRUCT2* test2 = new_test_struct2();
  new_test_struct();
  struct TEST_STRUCT2* test4 = new_test_struct2();
  new_test_struct2();

  kfree(test4);

  struct TEST_STRUCT2* test6 = new_test_struct2();

  // test_6 should have been allocated in test_4's spot.
  ASSERT_EQ(test6, test4);

  kfree(test2);
  struct TEST_STRUCT* test7 = new_test_struct();

  ASSERT_EQ(V(test7), V(test2));
  return 0;
}