1 /*
  2  * Copyright (c) 2018, 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 
 24 package jdk.jfr.event.oldobject;
 25 
 26 import java.lang.reflect.Modifier;
 27 import java.util.HashSet;
 28 import java.util.List;
 29 import java.util.Set;
 30 
 31 import jdk.jfr.Recording;
 32 import jdk.jfr.consumer.RecordedEvent;
 33 import jdk.jfr.consumer.RecordedObject;
 34 import jdk.jfr.internal.test.WhiteBox;
 35 import jdk.test.lib.Asserts;
 36 import jdk.test.lib.jfr.EventNames;
 37 import jdk.test.lib.jfr.Events;
 38 
 39 /**
 40  * @test
 41  * @requires vm.flagless
 42  * @requires vm.hasJFR
 43  * @requires !(vm.opt.final.UseCompactObjectHeaders == true | vm.opt.final.UseShenandoahGC == true)
 44  * @library /test/lib /test/jdk
 45  * @modules jdk.jfr/jdk.jfr.internal.test
 46  * @run main/othervm -XX:TLABSize=2k -Xlog:gc+tlab=trace jdk.jfr.event.oldobject.TestFieldInformation
 47  */
 48 public class TestFieldInformation {
 49 
 50     public static final Object[] testField = new Object[50];
 51 
 52     public static void main(String[] args) throws Exception {
 53         WhiteBox.setWriteAllObjectSamples(true);
 54 
 55         try (Recording recording = new Recording()) {
 56             recording.enable(EventNames.OldObjectSample).withoutStackTrace().with("cutoff", "infinity");
 57             recording.start();
 58 
 59             addToTestField();
 60 
 61             recording.stop();
 62 
 63             List<RecordedEvent> events = Events.fromRecording(recording);
 64             Events.hasEvents(events);
 65             for (RecordedEvent e : events) {
 66                 if (hasValidField(e)) {
 67                     return;
 68                 }
 69             }
 70             System.out.println(events);
 71             Asserts.fail("Could not find old object with field 'testField'");
 72         }
 73     }
 74 
 75     private static boolean hasValidField(RecordedEvent e) throws Exception {
 76         RecordedObject object = e.getValue("object");
 77         Set<Long> visited = new HashSet<>();
 78         while (object != null) {
 79             Long address = object.getValue("address");
 80             if (visited.contains(address)) {
 81                 return false;
 82             }
 83             visited.add(address);
 84             RecordedObject referrer = object.getValue("referrer");
 85             RecordedObject fieldObject = referrer != null ? referrer.getValue("field") : null;
 86             if (fieldObject != null) {
 87                 String name = fieldObject.getValue("name");
 88                 if (name != null && name.equals("testField")) {
 89                     int modifiers = (short) fieldObject.getValue("modifiers");
 90                     if (!Modifier.isStatic(modifiers)) {
 91                         throw new Exception("Field should be static");
 92                     }
 93                     if (!Modifier.isPublic(modifiers)) {
 94                         throw new Exception("Field should be private");
 95                     }
 96                     if (!Modifier.isFinal(modifiers)) {
 97                         throw new Exception("Field should be final");
 98                     }
 99                     if (Modifier.isTransient(modifiers)) {
100                         throw new Exception("Field should not be transient");
101                     }
102                     if (Modifier.isVolatile(modifiers)) {
103                         throw new Exception("Field should not be volatile");
104                     }
105                     return true;
106                 }
107             }
108             object = referrer != null ? referrer.getValue("object") : null;
109         }
110         return false;
111     }
112 
113     private static void addToTestField() {
114         for (int i = 0; i < testField.length; i++) {
115             // Allocate array to trigger sampling code path for interpreter / c1
116             testField[i] = new Object[1_000_000];
117         }
118     }
119 }