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=Shenandoah
 28  * @bug 8387285
 29  * @summary Full GC must not underflow the humongous compaction window when an
 30  *          identity-hashed, exact-region-multiple humongous object needs one more
 31  *          region after hash expansion.
 32  * @requires vm.gc.Shenandoah
 33  * @run main/othervm
 34  *      -XX:+UseCompactObjectHeaders -XX:+UseShenandoahGC
 35  *      -XX:+UnlockDiagnosticVMOptions -XX:+VerifyDuringGC -XX:+ShenandoahVerify
 36  *      -XX:-ExplicitGCInvokesConcurrent
 37  *      -Xms16m -Xmx16m
 38  *      -XX:+UnlockExperimentalVMOptions -XX:ShenandoahRegionSize=256K
 39  *      gc.stress.ihash.TestHumongousHashFullGC
 40  */
 41 
 42 /*
 43  * @test id=Shenandoah-aggressive
 44  * @bug 8387285
 45  * @summary Full GC must not underflow the humongous compaction window when an
 46  *          identity-hashed, exact-region-multiple humongous object needs one more
 47  *          region after hash expansion.
 48  * @requires vm.gc.Shenandoah
 49  * @run main/othervm
 50  *      -XX:+UseCompactObjectHeaders -XX:+UseShenandoahGC
 51  *      -XX:+UnlockDiagnosticVMOptions -XX:+VerifyDuringGC -XX:+ShenandoahVerify
 52  *      -XX:-ExplicitGCInvokesConcurrent
 53  *      -XX:ShenandoahGCHeuristics=aggressive
 54  *      -Xms16m -Xmx16m
 55  *      -XX:+UnlockExperimentalVMOptions -XX:ShenandoahRegionSize=256K
 56  *      gc.stress.ihash.TestHumongousHashFullGC
 57  */
 58 
 59 /**
 60  * Regression test for the Full-GC half of JDK-8387285.
 61  *
 62  * ShenandoahFullGC::calculate_target_humongous_objects() slides movable humongous
 63  * objects toward the end of the heap within a window [to_begin, to_end). With
 64  * compact object headers an identity-hashed humongous object grows by one word when
 65  * relocated, so an object whose size is an exact multiple of the region size needs
 66  * one extra region after expansion (k regions -> k+1).
 67  *
 68  * The bug: the fit test computed "start = to_end - num_regions" before checking it
 69  * fits. When the expanded num_regions exceeds the remaining window, the unsigned
 70  * subtraction underflows to ~SIZE_MAX, the guards pass, and the code dereferences a
 71  * bogus region -> SIGSEGV during STW Full GC.
 72  *
 73  * Reproduction (verified to SIGSEGV on the unfixed VM, exit cleanly on the fix):
 74  * a small heap with the minimum (256K) region size keeps the humongous objects at
 75  * the bottom of the heap, so the backward compaction scan reaches a movable
 76  * exact-2-region humongous start while the remaining window is smaller than its
 77  * expanded (3-region) size. The arrays are identity-hashed so relocation must
 78  * expand them, and -XX:-ExplicitGCInvokesConcurrent makes System.gc() a STW Full GC.
 79  */
 80 public class TestHumongousHashFullGC {
 81 
 82     static final int REGION_SIZE = 256 * 1024;
 83 
 84     // byte[] allocation size (with compact headers) = align_up(8 + length, 8).
 85     // length = k * REGION_SIZE - 8 yields an allocation of exactly k regions.
 86     static byte[] exactRegions(int k) {
 87         return new byte[k * REGION_SIZE - 8];
 88     }
 89 
 90     static final int COUNT = 6;
 91 
 92     static Object[] keep;
 93 
 94     public static void main(String[] args) {
 95         // Allocate exact-2-region humongous arrays at the bottom of the small heap
 96         // and identity-hash each so a relocating GC must expand it by one word.
 97         keep = new Object[COUNT];
 98         int[] hashes = new int[COUNT];
 99         for (int i = 0; i < COUNT; i++) {
100             byte[] o = exactRegions(2);
101             hashes[i] = System.identityHashCode(o);
102             keep[i] = o;
103         }
104 
105         // Fragment: drop a couple of arrays low in the heap so the backward Full GC
106         // compaction scan reaches a movable exact-2-region humongous start while the
107         // remaining window is smaller than its expanded (3-region) size.
108         keep[1] = null;
109         keep[3] = null;
110 
111         // STW Full GC compaction. With the bug this SIGSEGVs in
112         // calculate_target_humongous_objects(); with the fix it slides cleanly.
113         System.gc();
114 
115         // Validate surviving objects are intact across expansion/relocation.
116         for (int i = 0; i < COUNT; i++) {
117             byte[] o = (byte[]) keep[i];
118             if (o == null) {
119                 continue;
120             }
121             if (System.identityHashCode(o) != hashes[i]) {
122                 throw new RuntimeException("hash mismatch at " + i);
123             }
124         }
125         System.out.println("OK");
126     }
127 }