1 /*
   2  * Copyright (c) 2018, 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.api.metadata.eventtype;
  27 
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 
  34 import jdk.jfr.Event;
  35 import jdk.jfr.EventType;
  36 import jdk.jfr.FlightRecorder;
  37 import jdk.jfr.Recording;
  38 import jdk.jfr.consumer.RecordingFile;
  39 import jdk.jfr.internal.JVM;
  40 
  41 /*
  42  * @test
  43  * @key jfr
  44  * @summary Test that verifies event metadata is removed when an event class is unloaded.
  45  *
  46  * @library /lib/testlibrary
  47  * @modules jdk.jfr/jdk.jfr.internal
  48  *          java.base/jdk.internal.misc
  49  *
  50  * @run main/othervm jdk.jfr.api.metadata.eventtype.TestUnloadingEventClass
  51  */
  52 public class TestUnloadingEventClass {
  53 
  54     private static final String EVENT_NAME = "jdk.jfr.api.metadata.eventtype.TestUnloadingEventClass$ToBeUnloaded";
  55 
  56     public static class ToBeUnloaded extends Event {
  57     }
  58 
  59     static public class MyClassLoader extends ClassLoader {
  60         public MyClassLoader() {
  61             super( null);
  62         }
  63 
  64         public final Class<?> defineClass(String name, byte[] b) {
  65             return super.defineClass(name, b, 0, b.length);
  66         }
  67     }
  68 
  69     private static final JVM jvm = JVM.getJVM();
  70     public static ClassLoader myClassLoader;
  71 
  72     public static void main(String[] args) throws Throwable {
  73         assertEventTypeNotAvailable();
  74         myClassLoader = createClassLoaderWithEventClass();
  75 
  76         try (Recording r0 = new Recording()) {
  77             r0.start();
  78             r0.stop();
  79             if (getEventType(r0, 0, EVENT_NAME) == null) {
  80                 throw new Exception("Expected event class to have corresponding event type");
  81             }
  82         }
  83 
  84         try (Recording r1 = new Recording(); Recording r2 = new Recording(); Recording r3 = new Recording()) {
  85             r1.start();
  86             r2.start();
  87             unLoadEventClass();
  88             r3.start();
  89 
  90             assertEventTypeNotAvailable();
  91             r3.stop();
  92             r2.stop();
  93             r1.stop();
  94 
  95             if (getEventType(r1, 1, EVENT_NAME) == null) {
  96                 throw new Exception("Expected event class to have corresponding event type in recording with all chunks");
  97             }
  98             if (getEventType(r2, 2, EVENT_NAME) == null) {
  99                 throw new Exception("Expected event class to have corresponding event type in recording where event class was unloaded");
 100             }
 101             if (getEventType(r3, 3, EVENT_NAME) != null) {
 102                 throw new Exception("Unexpected metadata found for event class tha has been unloaded.");
 103             }
 104         }
 105     }
 106 
 107     private static MyClassLoader createClassLoaderWithEventClass() throws Exception {
 108         String resourceName = EVENT_NAME.replace('.', '/') + ".class";
 109         try (InputStream is = TestUnloadingEventClass.class.getClassLoader().getResourceAsStream(resourceName)) {
 110             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 111             byte[] buffer = new byte[4096];
 112             int byteValue = 0;
 113             while ((byteValue = is.read(buffer, 0, buffer.length)) != -1) {
 114                 baos.write(buffer, 0, byteValue);
 115             }
 116             baos.flush();
 117             MyClassLoader myClassLoader = new MyClassLoader();
 118             Class<?> eventClass = myClassLoader.defineClass(EVENT_NAME, baos.toByteArray());
 119             if (eventClass == null) {
 120                 throw new Exception("Could not define test class");
 121             }
 122             if (eventClass.getSuperclass() != Event.class) {
 123                 throw new Exception("Superclass should be jdk.jfr.Event");
 124             }
 125             if (eventClass.getSuperclass().getClassLoader() != null) {
 126                 throw new Exception("Class loader of jdk.jfr.Event should be null");
 127             }
 128             if (eventClass.getClassLoader() != myClassLoader) {
 129                 throw new Exception("Incorrect class loader for event class");
 130             }
 131             eventClass.newInstance(); // force <clinit>
 132             return myClassLoader;
 133         }
 134     }
 135 
 136     private static void unLoadEventClass() throws Exception {
 137         long firstCount = jvm.getUnloadedEventClassCount();
 138         System.out.println("Initial unloaded count: " + firstCount);
 139         myClassLoader = null;
 140         System.out.println("Cleared reference to MyClassLoader");
 141         long newCount = 0;
 142         do {
 143             System.out.println("GC triggered");
 144             System.gc();
 145             Thread.sleep(1000);
 146             newCount = jvm.getUnloadedEventClassCount();
 147             System.out.println("Unloaded count: " + newCount);
 148         } while (firstCount + 1 != newCount);
 149         System.out.println("Event class unloaded!");
 150         System.out.println("Event classes currently on the heap:");
 151         for (Class<?> eventClass : JVM.getJVM().getAllEventClasses()) {
 152         }
 153 
 154     }
 155 
 156     private static void assertEventTypeNotAvailable() throws Exception {
 157         for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) {
 158             if (type.getName().equals(EVENT_NAME)) {
 159                 throw new Exception("Event type should not be available");
 160             }
 161         }
 162     }
 163 
 164     private static Object getEventType(Recording r, long id, String eventName) throws IOException {
 165         Path p = Files.createTempFile("recording-" + id + "_", ".jfr");
 166         r.dump(p);
 167         try (RecordingFile rf = new RecordingFile(p)) {
 168             for (EventType et : rf.readEventTypes()) {
 169                 if (et.getName().equals(eventName)) {
 170                     return et;
 171                 }
 172             }
 173 
 174         }
 175         return null;
 176     }
 177 }
 178