1 /*
2 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package gc.stress.ihash;
25
26 /*
27 * @test id=G1
28 * @bug 8387335
29 * @summary Stress test: does humongous object compaction corrupt hash-code or other objects?
30 * @requires vm.gc.G1
31 * @key stress
32 * @run main/othervm/timeout=300
33 * -XX:+UseCompactObjectHeaders -XX:+UseG1GC
34 * -XX:+UnlockDiagnosticVMOptions -XX:+VerifyDuringGC
35 * -XX:-ExplicitGCInvokesConcurrent
36 * -Xmx512m -XX:G1HeapRegionSize=1M
37 * gc.stress.ihash.TestHumongousHash
38 */
39
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.List;
43
44 public class TestHumongousHash {
45 public static void main(String[] args) {
46 // For G1, 50% of region size. For Shenandoah, 100%. We want to stress objects over the
47 // threshold, but also particularly objects near a region boundary (hash-code expansion)
48 // could see an object expand from below threshold to above threshold, or from fitting in
49 // N regions to requiring N+1 regions.
50 int humongousThreshold = 512 * 1024;
51
52 for (int i = 1; i < 10; i++) {
53 test(humongousThreshold * i);
54 }
55 }
56
57 static void test(int objectSize) {
58 List<byte[]> largeObjects = new ArrayList<>();
59 List<Integer> hashes = new ArrayList<>();
60
61 int numObjects = 0;
62 try {
63 while (true) {
64 // Stress various sizes near the threshold, below and above.
65 byte[] largeObject = new byte[objectSize + numObjects - 32];
66 Arrays.fill(largeObject, (byte) numObjects);
67 largeObjects.add(largeObject);
68 hashes.add(System.identityHashCode(largeObject));
69 numObjects++;
70 }
71 } catch (OutOfMemoryError e) {
72 // That's enough.
73 }
74
75 // Fragment so that GC can compact.
76 for (int i = 0; i < numObjects; i++) {
77 if (i % 2 == 0) {
78 largeObjects.set(i, null);
79 }
80 }
81
82 // Trigger compaction. System.gc() doesn't trigger it in all cases (e.g. G1), so we fill up
83 // the heap to trigger OOM, too.
84 System.gc();
85 ArrayList<byte[]> boom = new ArrayList<>();
86 try {
87 while (true) {
88 boom.add(new byte[100_000_000]);
89 }
90 } catch (OutOfMemoryError e) {
91 boom = null;
92 }
93
94 // Check for corruption in hash-code, length or data.
95 boolean fail = false;
96 for (int i = 0; i < numObjects; i++) {
97 if (i % 2 == 0) {
98 continue;
99 }
100 byte[] largeObject = largeObjects.get(i);
101 if (System.identityHashCode(largeObject) != hashes.get(i)) {
102 System.out.println("hash mismatch at " + i);
103 fail = true;
104 }
105 if (largeObject.length != objectSize - 32 + i) {
106 System.out.println("length corruption at " + i);
107 fail = true;
108 }
109 for (int x = 0; x < largeObject.length; x++) {
110 if (largeObject[x] != (byte) i) {
111 System.out.println("corruption at [" + i + "][" + x + "]");
112 fail = true;
113 }
114 }
115 }
116 if (fail) {
117 throw new RuntimeException("Identity hash-code, length or data corruption detected.");
118 }
119 }
120 }