1 /*
 2  * Copyright (c) 2015, 2025, Oracle and/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 package jdk.jfr.event.oldobject;
24 
25 import java.util.List;
26 
27 import jdk.jfr.Recording;
28 import jdk.jfr.consumer.RecordedEvent;
29 import jdk.jfr.internal.test.WhiteBox;
30 import jdk.test.lib.jfr.EventNames;
31 import jdk.test.lib.jfr.Events;
32 
33 /**
34  * @test
35  * @requires vm.flagless
36  * @requires vm.hasJFR
37  * @requires !(vm.opt.final.UseCompactObjectHeaders == true | vm.opt.final.UseShenandoahGC == true)
38  * @library /test/lib /test/jdk
39  * @modules jdk.jfr/jdk.jfr.internal.test
40  * @run main/othervm -XX:TLABSize=2k jdk.jfr.event.oldobject.TestReferenceChainLimit
41  */
42 public class TestReferenceChainLimit {
43     private final static int TEST_CHAIN_LENGTH_FACTOR = 10; // scaling factor for the how long chain to be used in the test
44 
45     final static class ChainNode {
46         final ChainNode next;
47         byte[] leak;
48 
49         public ChainNode(ChainNode node) {
50             next = node;
51             leak = new byte[10_000];
52         }
53     }
54 
55     private static ChainNode createChain() {
56         ChainNode node = null;
57         final int testChainLength = OldObjects.MAX_CHAIN_LENGTH * TEST_CHAIN_LENGTH_FACTOR;
58         for (int i = 0; i < testChainLength; i++) {
59             node = new ChainNode(node);
60         }
61         return node;
62     }
63 
64     public static ChainNode leak;
65 
66     public static void main(String[] args) throws Exception {
67         WhiteBox.setWriteAllObjectSamples(true);
68 
69         try (Recording r = new Recording()) {
70             r.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
71             r.start();
72             leak = createChain();
73             List<RecordedEvent> events = Events.fromRecording(r);
74             if (OldObjects.countMatchingEvents(events, byte[].class, null, null, -1, "createChain") == 0) {
75                 throw new Exception("Could not find ChainNode");
76             }
77             for (RecordedEvent e : events) {
78                 OldObjects.validateReferenceChainLimit(e, OldObjects.MAX_CHAIN_LENGTH);
79             }
80         }
81     }
82 }