aboutsummaryrefslogtreecommitdiff
path: root/vm/alloc/TEST/HeapBitmapTest/main.c
blob: 10fa7f87641e58ea92b2a76158a9375795c57544 (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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#define __attribute(x) /* disable inlining */
#include "HeapBitmap.h"
#undef __attribute

#define PAGE_SIZE 4096
#define HEAP_BASE ((void *)0x10000)
#define HEAP_SIZE (5 * PAGE_SIZE + 888)

#define VERBOSE 1
#if VERBOSE
#define TRACE(...) printf(__VA_ARGS__)
#else
#define TRACE(...) /**/
#endif

void
test_init()
{
    HeapBitmap hb;
    bool ok;

    memset(&hb, 0x55, sizeof(hb));

    ok = dvmHeapBitmapInit(&hb, HEAP_BASE, HEAP_SIZE, "test");
    assert(ok);

    assert(hb.bits != NULL);
    assert(hb.bitsLen >= HB_OFFSET_TO_INDEX(HEAP_SIZE));
    assert(hb.base == (uintptr_t)HEAP_BASE);
    assert(hb.max < hb.base);

    /* Make sure hb.bits is mapped.
     */
    *hb.bits = 0x55;
    assert(*hb.bits = 0x55);
    *hb.bits = 0;

#define TEST_UNMAP 0
#if TEST_UNMAP
    /* Hold onto this to make sure it's unmapped later.
     */
    unsigned long int *bits = hb.bits;
#endif

    dvmHeapBitmapDelete(&hb);

    assert(hb.bits == NULL);
    assert(hb.bitsLen == 0);
    assert(hb.base == 0);
    assert(hb.max == 0);

#if TEST_UNMAP
    /* This pointer shouldn't be mapped anymore.
     */
    *bits = 0x55;
    assert(!"Should have segfaulted");
#endif
}

bool is_zeroed(const HeapBitmap *hb)
{
    int i;

    for (i = 0; i < hb->bitsLen / sizeof (*hb->bits); i++) {
        if (hb->bits[i] != 0L) {
            return false;
        }
    }
    return true;
}

void assert_empty(const HeapBitmap *hb)
{
    assert(hb->bits != NULL);
    assert(hb->bitsLen >= HB_OFFSET_TO_INDEX(HEAP_SIZE));
    assert(hb->base == (uintptr_t)HEAP_BASE);
    assert(hb->max < hb->base);

    assert(is_zeroed(hb));

    assert(!dvmHeapBitmapMayContainObject(hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapMayContainObject(hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapMayContainObject(hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapMayContainObject(hb,
            HEAP_BASE + HEAP_SIZE));

    assert(!dvmHeapBitmapIsObjectBitSet(hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapIsObjectBitSet(hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapIsObjectBitSet(hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));
}

void
test_bits()
{
    HeapBitmap hb;
    bool ok;

    ok = dvmHeapBitmapInit(&hb, HEAP_BASE, HEAP_SIZE, "test");
    assert(ok);

    assert_empty(&hb);

    /* Set the lowest address.
     */
    dvmHeapBitmapSetObjectBit(&hb, HEAP_BASE);
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE));

    assert(dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));

    /* Set the highest address.
     */
    dvmHeapBitmapSetObjectBit(&hb, HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT);
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE));
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE));

    assert(dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));

    /* Clear the lowest address.
     */
    dvmHeapBitmapClearObjectBit(&hb, HEAP_BASE);
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));
    assert(!is_zeroed(&hb));

    /* Clear the highest address.
     */
    dvmHeapBitmapClearObjectBit(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT);
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));
    assert(is_zeroed(&hb));

    /* Clean up.
     */
    dvmHeapBitmapDelete(&hb);
}

void
test_clear()
{
    HeapBitmap hb;
    bool ok;

    ok = dvmHeapBitmapInit(&hb, HEAP_BASE, HEAP_SIZE, "test");
    assert(ok);
    assert_empty(&hb);

    /* Set the highest address.
     */
    dvmHeapBitmapSetObjectBit(&hb, HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT);
    assert(!is_zeroed(&hb));

    /* Clear the bitmap.
     */
    dvmHeapBitmapZero(&hb);
    assert_empty(&hb);

    /* Clean up.
     */
    dvmHeapBitmapDelete(&hb);
}

void
test_modify()
{
    HeapBitmap hb;
    bool ok;
    unsigned long bit;

    ok = dvmHeapBitmapInit(&hb, HEAP_BASE, HEAP_SIZE, "test");
    assert(ok);
    assert_empty(&hb);

    /* Set the lowest address.
     */
    bit = dvmHeapBitmapSetAndReturnObjectBit(&hb, HEAP_BASE);
    assert(bit == 0);
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE));

    assert(dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));

    /* Set the lowest address again.
     */
    bit = dvmHeapBitmapSetAndReturnObjectBit(&hb, HEAP_BASE);
    assert(bit != 0);
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE));

    assert(dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));

    /* Set the highest address.
     */
    bit = dvmHeapBitmapSetAndReturnObjectBit(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT);
    assert(bit == 0);
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE));
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE));

    assert(dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));

    /* Set the highest address again.
     */
    bit = dvmHeapBitmapSetAndReturnObjectBit(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT);
    assert(bit != 0);
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE));
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));
    assert(!dvmHeapBitmapMayContainObject(&hb,
            HEAP_BASE + HEAP_SIZE));

    assert(dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE));
    assert(!dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HB_OBJECT_ALIGNMENT));
    assert(dvmHeapBitmapIsObjectBitSet(&hb,
            HEAP_BASE + HEAP_SIZE - HB_OBJECT_ALIGNMENT));

    /* Clean up.
     */
    dvmHeapBitmapDelete(&hb);
}

/*
 * xor test support functions
 */

static void *gCallbackArg = NULL;

#define NUM_XOR_PTRS  128
static size_t gNumPtrs;
static void *gXorPtrs[NUM_XOR_PTRS];
static bool gClearedPtrs[NUM_XOR_PTRS];
static bool gSeenPtrs[NUM_XOR_PTRS];

bool
xorCallback(size_t numPtrs, void **ptrs, const void *finger, void *arg)
{
    assert(numPtrs > 0);
    assert(ptrs != NULL);
    assert(arg == gCallbackArg);

size_t i;
    for (i = 0; i < numPtrs; i++) {
        assert(ptrs[i] < finger);
        printf("callback: 0x%08x ( < 0x%08x )\n",
                (uintptr_t)ptrs[i], (uintptr_t)finger);
    }

    return true;
}

bool
seenAndClearedMatch()
{
    size_t i;
    for (i = 0; i < gNumPtrs; i++) {
        if (gClearedPtrs[i] != gSeenPtrs[i]) {
            return false;
        }
    }
    return true;
}

void
run_xor(ssize_t offset, size_t step)
{
    assert(step != 0);
    assert(step < HEAP_SIZE);

    /* Figure out the range.
     */
uintptr_t base;
uintptr_t top;
    if (offset >= 0) {
        base = (uintptr_t)HEAP_BASE + offset;
    } else {
        base = (uintptr_t)HEAP_BASE + (uintptr_t)HEAP_SIZE + offset;
    }
    if (base < (uintptr_t)HEAP_BASE) {
        base = (uintptr_t)HEAP_BASE;
    } else if (base > (uintptr_t)(HEAP_BASE + HEAP_SIZE)) {
        base = (uintptr_t)(HEAP_BASE + HEAP_SIZE);
    } else {
        base = (base + HB_OBJECT_ALIGNMENT - 1) & ~(HB_OBJECT_ALIGNMENT - 1);
    }
    step *= HB_OBJECT_ALIGNMENT;
    top = base + step * NUM_XOR_PTRS;
    if (top > (uintptr_t)(HEAP_BASE + HEAP_SIZE)) {
        top = (uintptr_t)(HEAP_BASE + HEAP_SIZE);
    }

    /* Create the pointers.
     */
    gNumPtrs = 0;
    memset(gXorPtrs, 0, sizeof(gXorPtrs));
    memset(gClearedPtrs, 0, sizeof(gClearedPtrs));
    memset(gSeenPtrs, 0, sizeof(gSeenPtrs));

uintptr_t addr;
void **p = gXorPtrs;
    for (addr = base; addr < top; addr += step) {
        *p++ = (void *)addr;
        gNumPtrs++;
    }
    assert(seenAndClearedMatch());

    /* Set up the bitmaps.
     */
HeapBitmap hb1, hb2;
bool ok;

    ok = dvmHeapBitmapInit(&hb1, HEAP_BASE, HEAP_SIZE, "test1");
    assert(ok);
    ok = dvmHeapBitmapInitFromTemplate(&hb2, &hb1, "test2");
    assert(ok);

    /* Walk two empty bitmaps.
     */
TRACE("walk 0\n");
    ok = dvmHeapBitmapXorWalk(&hb1, &hb2, xorCallback, gCallbackArg);
    assert(ok);
    assert(seenAndClearedMatch());

    /* Walk one empty bitmap.
     */
TRACE("walk 1\n");
    dvmHeapBitmapSetObjectBit(&hb1, (void *)base);
    ok = dvmHeapBitmapXorWalk(&hb1, &hb2, xorCallback, gCallbackArg);
    assert(ok);

    /* Make the bitmaps match.
     */
TRACE("walk 2\n");
    dvmHeapBitmapSetObjectBit(&hb2, (void *)base);
    ok = dvmHeapBitmapXorWalk(&hb1, &hb2, xorCallback, gCallbackArg);
    assert(ok);

    /* Clear the bitmaps.
     */
    dvmHeapBitmapZero(&hb1);
    assert_empty(&hb1);
    dvmHeapBitmapZero(&hb2);
    assert_empty(&hb2);

    /* Set the pointers we created in one of the bitmaps,
     * then visit them.
     */
size_t i;
    for (i = 0; i < gNumPtrs; i++) {
        dvmHeapBitmapSetObjectBit(&hb1, gXorPtrs[i]);
    }
TRACE("walk 3\n");
    ok = dvmHeapBitmapXorWalk(&hb1, &hb2, xorCallback, gCallbackArg);
    assert(ok);

    /* Set every third pointer in the other bitmap, and visit again.
     */
    for (i = 0; i < gNumPtrs; i += 3) {
        dvmHeapBitmapSetObjectBit(&hb2, gXorPtrs[i]);
    }
TRACE("walk 4\n");
    ok = dvmHeapBitmapXorWalk(&hb1, &hb2, xorCallback, gCallbackArg);
    assert(ok);

    /* Set every other pointer in the other bitmap, and visit again.
     */
    for (i = 0; i < gNumPtrs; i += 2) {
        dvmHeapBitmapSetObjectBit(&hb2, gXorPtrs[i]);
    }
TRACE("walk 5\n");
    ok = dvmHeapBitmapXorWalk(&hb1, &hb2, xorCallback, gCallbackArg);
    assert(ok);

    /* Walk just one bitmap.
     */
TRACE("walk 6\n");
    ok = dvmHeapBitmapWalk(&hb2, xorCallback, gCallbackArg);
    assert(ok);

//xxx build an expect list for the callback
//xxx test where max points to beginning, middle, and end of a word

    /* Clean up.
     */
    dvmHeapBitmapDelete(&hb1);
    dvmHeapBitmapDelete(&hb2);
}

void
test_xor()
{
    run_xor(0, 1);
    run_xor(100, 34);
}

int main(int argc, char *argv[])
{
    printf("test_init...\n");
    test_init();

    printf("test_bits...\n");
    test_bits();

    printf("test_clear...\n");
    test_clear();

    printf("test_modify...\n");
    test_modify();

    printf("test_xor...\n");
    test_xor();

    printf("done.\n");
    return 0;
}