1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.util.Random;
18
19public class Main {
20    private static final int buckets = 16 * 1024;
21    private static final int bufferSize = 1024;
22
23    static class ByteContainer {
24        public byte[] bytes;
25    }
26
27    public static void main(String[] args) throws Exception {
28        ByteContainer[] l = new ByteContainer[buckets];
29
30        for (int i = 0; i < buckets; ++i) {
31            l[i] = new ByteContainer();
32        }
33
34        Random rnd = new Random(123456);
35        for (int i = 0; i < buckets / 256; ++i) {
36            int index = rnd.nextInt(buckets);
37            l[index].bytes = new byte[bufferSize];
38
39            // Try to get GC to run if we can
40            Runtime.getRuntime().gc();
41
42            // Shuffle the array to try cause the lost object problem:
43            // This problem occurs when an object is white, it may be
44            // only referenced from a white or grey object. If the white
45            // object is moved during a CMS to be a black object's field, it
46            // causes the moved object to not get marked. This can result in
47            // heap corruption. A typical way to address this issue is by
48            // having a card table.
49            // This aspect of the test is meant to ensure that card
50            // dirtying works and that we check the marked cards after
51            // marking.
52            // If these operations are not done, a segfault / failed assert
53            // should occur.
54            for (int j = 0; j < l.length; ++j) {
55                int a = l.length - i - 1;
56                int b = rnd.nextInt(a);
57                byte[] temp = l[a].bytes;
58                l[a].bytes = l[b].bytes;
59                l[b].bytes = temp;
60            }
61        }
62        System.out.println("Test complete");
63    }
64}
65