summaryrefslogtreecommitdiff
path: root/vm/test/TestIndirectRefTable.c
blob: 25f1dd1479a52293991be1dadbde9ba4a79cb22d (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
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Test the indirect reference table implementation.
 */
#include "Dalvik.h"

#include <stdlib.h>

#ifndef NDEBUG

#define DBUG_MSG    LOGV

/*
 * Basic add/get/delete tests in an unsegmented table.
 */
static bool basicTest(void)
{
    static const int kTableMax = 20;
    IndirectRefTable irt;
    IndirectRef iref0, iref1, iref2, iref3;
    IndirectRef manyRefs[kTableMax];
    ClassObject* clazz = dvmFindClass("Ljava/lang/Object;", NULL);
    Object* obj0 = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
    Object* obj1 = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
    Object* obj2 = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
    Object* obj3 = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
    const u4 cookie = IRT_FIRST_SEGMENT;
    bool result = false;

    if (!dvmInitIndirectRefTable(&irt, kTableMax/2, kTableMax,
            kIndirectKindGlobal))
    {
        return false;
    }

    iref0 = (IndirectRef) 0x11110;
    if (dvmRemoveFromIndirectRefTable(&irt, cookie, iref0)) {
        LOGE("unexpectedly successful removal\n");
        goto bail;
    }

    /*
     * Add three, check, remove in the order in which they were added.
     */
    DBUG_MSG("+++ START fifo\n");
    iref0 = dvmAddToIndirectRefTable(&irt, cookie, obj0);
    iref1 = dvmAddToIndirectRefTable(&irt, cookie, obj1);
    iref2 = dvmAddToIndirectRefTable(&irt, cookie, obj2);
    if (iref0 == NULL || iref1 == NULL || iref2 == NULL) {
        LOGE("trivial add1 failed\n");
        goto bail;
    }

    if (dvmGetFromIndirectRefTable(&irt, iref0) != obj0 ||
        dvmGetFromIndirectRefTable(&irt, iref1) != obj1 ||
        dvmGetFromIndirectRefTable(&irt, iref2) != obj2)
    {
        LOGE("objects don't match expected values %p %p %p vs. %p %p %p\n",
            dvmGetFromIndirectRefTable(&irt, iref0),
            dvmGetFromIndirectRefTable(&irt, iref1),
            dvmGetFromIndirectRefTable(&irt, iref2),
            obj0, obj1, obj2);
        goto bail;
    } else {
        DBUG_MSG("+++ obj1=%p --> iref1=%p\n", obj1, iref1);
    }

    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref0) ||
        !dvmRemoveFromIndirectRefTable(&irt, cookie, iref1) ||
        !dvmRemoveFromIndirectRefTable(&irt, cookie, iref2))
    {
        LOGE("fifo deletion failed\n");
        goto bail;
    }

    /* table should be empty now */
    if (dvmIndirectRefTableEntries(&irt) != 0) {
        LOGE("fifo del not empty\n");
        goto bail;
    }

    /* get invalid entry (off the end of the list) */
    if (dvmGetFromIndirectRefTable(&irt, iref0) != NULL) {
        LOGE("stale entry get succeeded unexpectedly\n");
        goto bail;
    }

    /*
     * Add three, remove in the opposite order.
     */
    DBUG_MSG("+++ START lifo\n");
    iref0 = dvmAddToIndirectRefTable(&irt, cookie, obj0);
    iref1 = dvmAddToIndirectRefTable(&irt, cookie, obj1);
    iref2 = dvmAddToIndirectRefTable(&irt, cookie, obj2);
    if (iref0 == NULL || iref1 == NULL || iref2 == NULL) {
        LOGE("trivial add2 failed\n");
        goto bail;
    }

    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref2) ||
        !dvmRemoveFromIndirectRefTable(&irt, cookie, iref1) ||
        !dvmRemoveFromIndirectRefTable(&irt, cookie, iref0))
    {
        LOGE("lifo deletion failed\n");
        goto bail;
    }

    /* table should be empty now */
    if (dvmIndirectRefTableEntries(&irt) != 0) {
        LOGE("lifo del not empty\n");
        goto bail;
    }

    /*
     * Add three, remove middle / middle / bottom / top.  (Second attempt
     * to remove middle should fail.)
     */
    DBUG_MSG("+++ START unorder\n");
    iref0 = dvmAddToIndirectRefTable(&irt, cookie, obj0);
    iref1 = dvmAddToIndirectRefTable(&irt, cookie, obj1);
    iref2 = dvmAddToIndirectRefTable(&irt, cookie, obj2);
    if (iref0 == NULL || iref1 == NULL || iref2 == NULL) {
        LOGE("trivial add3 failed\n");
        goto bail;
    }

    if (dvmIndirectRefTableEntries(&irt) != 3) {
        LOGE("expected 3 entries, found %d\n",
            dvmIndirectRefTableEntries(&irt));
        goto bail;
    }

    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref1) ||
        dvmRemoveFromIndirectRefTable(&irt, cookie, iref1))
    {
        LOGE("unorder deletion1 failed\n");
        goto bail;
    }

    /* get invalid entry (from hole) */
    if (dvmGetFromIndirectRefTable(&irt, iref1) != NULL) {
        LOGE("hole get succeeded unexpectedly\n");
        goto bail;
    }

    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref2) ||
        !dvmRemoveFromIndirectRefTable(&irt, cookie, iref0))
    {
        LOGE("unorder deletion2 failed\n");
        goto bail;
    }

    /* table should be empty now */
    if (dvmIndirectRefTableEntries(&irt) != 0) {
        LOGE("unorder del not empty\n");
        goto bail;
    }

    /*
     * Add four entries.  Remove #1, add new entry, verify that table size
     * is still 4 (i.e. holes are getting filled).  Remove #1 and #3, verify
     * that we delete one and don't hole-compact the other.
     */
    DBUG_MSG("+++ START hole fill\n");
    iref0 = dvmAddToIndirectRefTable(&irt, cookie, obj0);
    iref1 = dvmAddToIndirectRefTable(&irt, cookie, obj1);
    iref2 = dvmAddToIndirectRefTable(&irt, cookie, obj2);
    iref3 = dvmAddToIndirectRefTable(&irt, cookie, obj3);
    if (iref0 == NULL || iref1 == NULL || iref2 == NULL || iref3 == NULL) {
        LOGE("trivial add4 failed\n");
        goto bail;
    }
    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref1)) {
        LOGE("remove 1 of 4 failed\n");
        goto bail;
    }
    iref1 = dvmAddToIndirectRefTable(&irt, cookie, obj1);
    if (dvmIndirectRefTableEntries(&irt) != 4) {
        LOGE("hole not filled\n");
        goto bail;
    }
    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref1) ||
        !dvmRemoveFromIndirectRefTable(&irt, cookie, iref3))
    {
        LOGE("remove 1/3 failed\n");
        goto bail;
    }
    if (dvmIndirectRefTableEntries(&irt) != 3) {
        LOGE("should be 3 after two deletions\n");
        goto bail;
    }
    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref2) ||
        !dvmRemoveFromIndirectRefTable(&irt, cookie, iref0))
    {
        LOGE("remove 2/0 failed\n");
        goto bail;
    }
    if (dvmIndirectRefTableEntries(&irt) != 0) {
        LOGE("not empty after split remove\n");
        goto bail;
    }

    /*
     * Add an entry, remove it, add a new entry, and try to use the original
     * iref.  They have the same slot number but are for different objects.
     * With the extended checks in place, this should fail.
     */
    DBUG_MSG("+++ START switched\n");
    iref0 = dvmAddToIndirectRefTable(&irt, cookie, obj0);
    dvmRemoveFromIndirectRefTable(&irt, cookie, iref0);
    iref1 = dvmAddToIndirectRefTable(&irt, cookie, obj1);
    if (dvmRemoveFromIndirectRefTable(&irt, cookie, iref0)) {
        LOGE("mismatched del succeeded (%p vs %p)\n", iref0, iref1);
        goto bail;
    }
    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref1)) {
        LOGE("switched del failed\n");
        goto bail;
    }
    if (dvmIndirectRefTableEntries(&irt) != 0) {
        LOGE("switching del not empty\n");
        goto bail;
    }

    /*
     * Same as above, but with the same object.  A more rigorous checker
     * (e.g. with slot serialization) will catch this.
     */
    iref0 = dvmAddToIndirectRefTable(&irt, cookie, obj0);
    dvmRemoveFromIndirectRefTable(&irt, cookie, iref0);
    iref1 = dvmAddToIndirectRefTable(&irt, cookie, obj0);
    if (iref0 != iref1) {
        /* try 0, should not work */
        if (dvmRemoveFromIndirectRefTable(&irt, cookie, iref0)) {
            LOGE("temporal del succeeded (%p vs %p)\n", iref0, iref1);
            goto bail;
        }
    }
    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref1)) {
        LOGE("temporal cleanup failed\n");
        goto bail;
    }
    if (dvmIndirectRefTableEntries(&irt) != 0) {
        LOGE("temporal del not empty\n");
        goto bail;
    }

    /*
     * Test table overflow.
     */
    DBUG_MSG("+++ START overflow\n");
    int i;
    for (i = 0; i < kTableMax; i++) {
        manyRefs[i] = dvmAddToIndirectRefTable(&irt, cookie, obj0);
        if (manyRefs[i] == NULL) {
            LOGE("Failed adding %d of %d\n", i, kTableMax);
            goto bail;
        }
    }
    if (dvmAddToIndirectRefTable(&irt, cookie, obj0) != NULL) {
        LOGE("Table overflow succeeded\n");
        goto bail;
    }
    if (dvmIndirectRefTableEntries(&irt) != (size_t)kTableMax) {
        LOGE("Expected %d entries, found %d\n",
            kTableMax, dvmIndirectRefTableEntries(&irt));
        goto bail;
    }
    for (i = 0; i < kTableMax-1; i++) {
        if (!dvmRemoveFromIndirectRefTable(&irt, cookie, manyRefs[i])) {
            LOGE("multi-remove failed at %d\n", i);
            goto bail;
        }
    }
    /* because of removal order, should have 20 entries, 19 of them holes */
    if (dvmIndirectRefTableEntries(&irt) != (size_t)kTableMax) {
        LOGE("Expected %d entries (with holes), found %d\n",
            kTableMax, dvmIndirectRefTableEntries(&irt));
        goto bail;
    }
    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, manyRefs[kTableMax-1])) {
        LOGE("multi-remove final failed\n");
        goto bail;
    }
    if (dvmIndirectRefTableEntries(&irt) != 0) {
        LOGE("multi-del not empty\n");
        goto bail;
    }

    DBUG_MSG("+++ basic test complete\n");
    result = true;

bail:
    dvmClearIndirectRefTable(&irt);
    return result;
}

/*
 * Test operations on a segmented table.
 */
static bool segmentTest(void)
{
    static const int kTableMax = 20;
    IndirectRefTable irt;
    IndirectRef iref0, iref1, iref2, iref3;
    ClassObject* clazz = dvmFindClass("Ljava/lang/Object;", NULL);
    Object* obj0 = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
    Object* obj1 = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
    Object* obj2 = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
    Object* obj3 = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
    u4 cookie;
    u4 segmentState[4];
    bool result = false;

    if (!dvmInitIndirectRefTable(&irt, kTableMax, kTableMax,
            kIndirectKindLocal))
    {
        return false;
    }
    cookie = segmentState[0] = IRT_FIRST_SEGMENT;
    DBUG_MSG("+++ objs %p %p %p %p\n", obj0, obj1, obj2, obj3);

    /*
     * Push two, create new segment, push two more, try to get all four,
     * try to delete all 4.  All four should be accessible, but only the
     * last two should be deletable.
     */
    DBUG_MSG("+++ START basic segment\n");
    iref0 = dvmAddToIndirectRefTable(&irt, cookie, obj0);
    iref1 = dvmAddToIndirectRefTable(&irt, cookie, obj1);
    cookie = segmentState[1] = dvmPushIndirectRefTableSegment(&irt);
    DBUG_MSG("+++ pushed, cookie is 0x%08x\n", cookie);
    iref2 = dvmAddToIndirectRefTable(&irt, cookie, obj2);
    iref3 = dvmAddToIndirectRefTable(&irt, cookie, obj3);

    if (dvmRemoveFromIndirectRefTable(&irt, cookie, iref0) ||
        dvmRemoveFromIndirectRefTable(&irt, cookie, iref1))
    {
        LOGE("removed values from earlier segment\n");
        goto bail;
    }
    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref2) ||
        !dvmRemoveFromIndirectRefTable(&irt, cookie, iref3))
    {
        LOGE("unable to remove values from current segment\n");
        goto bail;
    }
    if (dvmIndirectRefTableEntries(&irt) != 2) {
        LOGE("wrong total entries\n");
        goto bail;
    }
    dvmPopIndirectRefTableSegment(&irt, segmentState[1]);
    cookie = segmentState[0];
    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref0) ||
        !dvmRemoveFromIndirectRefTable(&irt, cookie, iref1))
    {
        LOGE("unable to remove values from first segment\n");
        goto bail;
    }
    if (dvmIndirectRefTableEntries(&irt) != 0) {
        LOGE("basic push/pop not empty\n");
        goto bail;
    }

    /*
     * Push two, delete first, segment, push two more, pop segment, verify
     * the last two are no longer present and hole count is right.  The
     * adds after the segment pop should not be filling in the hole.
     */
    DBUG_MSG("+++ START segment pop\n");
    iref0 = dvmAddToIndirectRefTable(&irt, cookie, obj0);
    iref1 = dvmAddToIndirectRefTable(&irt, cookie, obj1);
    dvmRemoveFromIndirectRefTable(&irt, cookie, iref0);
    cookie = segmentState[1] = dvmPushIndirectRefTableSegment(&irt);
    iref2 = dvmAddToIndirectRefTable(&irt, cookie, obj2);
    iref3 = dvmAddToIndirectRefTable(&irt, cookie, obj3);
    dvmPopIndirectRefTableSegment(&irt, segmentState[1]);
    cookie = segmentState[0];
    if (dvmIndirectRefTableEntries(&irt) != 2) {
        LOGE("wrong total entries after pop\n");
        goto bail;
    }
    dvmRemoveFromIndirectRefTable(&irt, cookie, iref1);
    if (dvmIndirectRefTableEntries(&irt) != 0) {
        LOGE("not back to zero after pop + del\n");
        goto bail;
    }

    /*
     * Multiple segments, some empty.
     */
    DBUG_MSG("+++ START multiseg\n");
    iref0 = dvmAppendToIndirectRefTable(&irt, cookie, obj0);
    iref1 = dvmAppendToIndirectRefTable(&irt, cookie, obj1);
    cookie = segmentState[1] = dvmPushIndirectRefTableSegment(&irt);
    cookie = segmentState[2] = dvmPushIndirectRefTableSegment(&irt);
    iref3 = dvmAppendToIndirectRefTable(&irt, cookie, obj3);
    iref2 = dvmAppendToIndirectRefTable(&irt, cookie, obj2);
    dvmRemoveFromIndirectRefTable(&irt, cookie, iref3);
    cookie = segmentState[3] = dvmPushIndirectRefTableSegment(&irt);
    iref3 = dvmAppendToIndirectRefTable(&irt, cookie, obj3);

    if (dvmGetFromIndirectRefTable(&irt, iref0) != obj0 ||
        dvmGetFromIndirectRefTable(&irt, iref1) != obj1 ||
        dvmGetFromIndirectRefTable(&irt, iref2) != obj2 ||
        dvmGetFromIndirectRefTable(&irt, iref3) != obj3)
    {
        LOGE("Unable to retrieve all multiseg objects\n");
        goto bail;
    }

    dvmDumpIndirectRefTable(&irt, "test");

    //int i;
    //for (i = 0; i < sizeof(segmentState) / sizeof(segmentState[0]); i++) {
    //    DBUG_MSG("+++  segment %d = 0x%08x\n", i, segmentState[i]);
    //}

    dvmRemoveFromIndirectRefTable(&irt, cookie, iref3);
    if (dvmRemoveFromIndirectRefTable(&irt, cookie, iref2)) {
        LOGE("multiseg del2 worked\n");
        goto bail;
    }
    dvmPopIndirectRefTableSegment(&irt, segmentState[3]);
    cookie = segmentState[2];
    if (!dvmRemoveFromIndirectRefTable(&irt, cookie, iref2)) {
        LOGE("multiseg del2b failed (cookie=0x%08x ref=%p)\n", cookie, iref2);
        goto bail;
    }
    iref2 = dvmAddToIndirectRefTable(&irt, cookie, obj2);

    /* pop two off at once */
    dvmPopIndirectRefTableSegment(&irt, segmentState[1]);
    cookie = segmentState[0];

    if (dvmIndirectRefTableEntries(&irt) != 2) {
        LOGE("Unexpected entry count in multiseg\n");
        goto bail;
    }
    dvmRemoveFromIndirectRefTable(&irt, cookie, iref0);
    dvmRemoveFromIndirectRefTable(&irt, cookie, iref1);
    if (dvmIndirectRefTableEntries(&irt) != 0) {
        LOGE("Unexpected entry count at multiseg end\n");
        goto bail;
    }

    DBUG_MSG("+++ segment test complete\n");
    result = true;

bail:
    dvmClearIndirectRefTable(&irt);
    return result;
}


/*
 * Some quick tests.
 */
bool dvmTestIndirectRefTable(void)
{
    if (!basicTest()) {
        LOGE("IRT basic test failed\n");
        return false;
    }
    if (!segmentTest()) {
        LOGE("IRT segment test failed\n");
        return false;
    }

    return true;
}

#endif /*NDEBUG*/