hotspot/test/runtime/Metaspace/DefineClass.java
author simonis
Thu, 16 Feb 2017 09:40:51 +0100
changeset 43968 bf02bf2ff560
parent 43948 26f3c55e246e
child 44993 f61bcd80ec1f
permissions -rw-r--r--
8174856: [TESTBUG] Missing DefineClass instances Reviewed-by: dholmes, ddmitriev
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43948
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
     1
/*
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
     2
 * Copyright (c) 2017 SAP SE. All rights reserved.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
     4
 *
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
     7
 * published by the Free Software Foundation.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
     8
 *
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    13
 * accompanied this code).
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    14
 *
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    18
 *
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    21
 * questions.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    22
 */
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    23
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    24
/**
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    25
 * @test
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    26
 * @bug 8173743
43968
bf02bf2ff560 8174856: [TESTBUG] Missing DefineClass instances
simonis
parents: 43948
diff changeset
    27
 * @requires vm.compMode != "Xcomp"
43948
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    28
 * @summary Failures during class definition can lead to memory leaks in metaspace
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    29
 * @library /test/lib
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    30
 * @run main/othervm test.DefineClass defineClass
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    31
 * @run main/othervm test.DefineClass defineSystemClass
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    32
 * @run main/othervm -XX:+UnlockDiagnosticVMOptions
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    33
                     -XX:+UnsyncloadClass -XX:+AllowParallelDefineClass
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    34
                     test.DefineClass defineClassParallel
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    35
 * @run main/othervm -XX:+UnlockDiagnosticVMOptions
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    36
                     -XX:+UnsyncloadClass -XX:-AllowParallelDefineClass
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    37
                     test.DefineClass defineClassParallel
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    38
 * @run main/othervm -XX:+UnlockDiagnosticVMOptions
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    39
                     -XX:-UnsyncloadClass -XX:+AllowParallelDefineClass
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    40
                     test.DefineClass defineClassParallel
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    41
 * @run main/othervm -XX:+UnlockDiagnosticVMOptions
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    42
                     -XX:-UnsyncloadClass -XX:-AllowParallelDefineClass
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    43
                     test.DefineClass defineClassParallel
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    44
 * @run main/othervm test.DefineClass redefineClass
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    45
 * @run main/othervm test.DefineClass redefineClassWithError
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    46
 * @author volker.simonis@gmail.com
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    47
 */
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    48
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    49
package test;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    50
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    51
import java.io.ByteArrayOutputStream;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    52
import java.io.File;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    53
import java.io.FileOutputStream;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    54
import java.io.InputStream;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    55
import java.lang.instrument.ClassDefinition;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    56
import java.lang.instrument.Instrumentation;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    57
import java.lang.management.ManagementFactory;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    58
import java.util.Scanner;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    59
import java.util.concurrent.CountDownLatch;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    60
import java.util.jar.Attributes;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    61
import java.util.jar.JarEntry;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    62
import java.util.jar.JarOutputStream;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    63
import java.util.jar.Manifest;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    64
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    65
import javax.management.MBeanServer;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    66
import javax.management.ObjectName;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    67
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    68
import com.sun.tools.attach.VirtualMachine;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    69
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    70
import jdk.test.lib.process.ProcessTools;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    71
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    72
public class DefineClass {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    73
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    74
    private static Instrumentation instrumentation;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    75
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    76
    public void getID(CountDownLatch start, CountDownLatch stop) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    77
        String id = "AAAAAAAA";
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    78
        System.out.println(id);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    79
        try {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    80
            // Signal that we've entered the activation..
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    81
            start.countDown();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    82
            //..and wait until we can leave it.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    83
            stop.await();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    84
        } catch (InterruptedException e) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    85
            e.printStackTrace();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    86
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    87
        System.out.println(id);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    88
        return;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    89
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    90
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    91
    private static class MyThread extends Thread {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    92
        private DefineClass dc;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    93
        private CountDownLatch start, stop;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    94
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    95
        public MyThread(DefineClass dc, CountDownLatch start, CountDownLatch stop) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    96
            this.dc = dc;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    97
            this.start = start;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    98
            this.stop = stop;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
    99
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   100
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   101
        public void run() {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   102
            dc.getID(start, stop);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   103
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   104
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   105
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   106
    private static class ParallelLoadingThread extends Thread {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   107
        private MyParallelClassLoader pcl;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   108
        private CountDownLatch stop;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   109
        private byte[] buf;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   110
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   111
        public ParallelLoadingThread(MyParallelClassLoader pcl, byte[] buf, CountDownLatch stop) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   112
            this.pcl = pcl;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   113
            this.stop = stop;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   114
            this.buf = buf;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   115
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   116
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   117
        public void run() {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   118
            try {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   119
                stop.await();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   120
            } catch (InterruptedException e) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   121
                e.printStackTrace();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   122
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   123
            try {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   124
                @SuppressWarnings("unchecked")
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   125
                Class<DefineClass> dc = (Class<DefineClass>) pcl.myDefineClass(DefineClass.class.getName(), buf, 0, buf.length);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   126
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   127
            catch (LinkageError jle) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   128
                // Expected with a parallel capable class loader and
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   129
                // -XX:+UnsyncloadClass or -XX:+AllowParallelDefineClass
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   130
                pcl.incrementLinkageErrors();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   131
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   132
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   133
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   134
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   135
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   136
    static private class MyClassLoader extends ClassLoader {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   137
        public Class<?> myDefineClass(String name, byte[] b, int off, int len) throws ClassFormatError {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   138
            return defineClass(name, b, off, len, null);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   139
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   140
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   141
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   142
    static private class MyParallelClassLoader extends ClassLoader {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   143
        static {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   144
            System.out.println("parallelCapable : " + registerAsParallelCapable());
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   145
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   146
        public Class<?> myDefineClass(String name, byte[] b, int off, int len) throws ClassFormatError {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   147
            return defineClass(name, b, off, len, null);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   148
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   149
        public synchronized void incrementLinkageErrors() {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   150
            linkageErrors++;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   151
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   152
        public int getLinkageErrors() {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   153
            return linkageErrors;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   154
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   155
        private volatile int linkageErrors;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   156
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   157
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   158
    public static void agentmain(String args, Instrumentation inst) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   159
        System.out.println("Loading Java Agent.");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   160
        instrumentation = inst;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   161
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   162
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   163
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   164
    private static void loadInstrumentationAgent(String myName, byte[] buf) throws Exception {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   165
        // Create agent jar file on the fly
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   166
        Manifest m = new Manifest();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   167
        m.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   168
        m.getMainAttributes().put(new Attributes.Name("Agent-Class"), myName);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   169
        m.getMainAttributes().put(new Attributes.Name("Can-Redefine-Classes"), "true");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   170
        File jarFile = File.createTempFile("agent", ".jar");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   171
        jarFile.deleteOnExit();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   172
        JarOutputStream jar = new JarOutputStream(new FileOutputStream(jarFile), m);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   173
        jar.putNextEntry(new JarEntry(myName.replace('.', '/') + ".class"));
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   174
        jar.write(buf);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   175
        jar.close();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   176
        String pid = Long.toString(ProcessTools.getProcessId());
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   177
        System.out.println("Our pid is = " + pid);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   178
        VirtualMachine vm = VirtualMachine.attach(pid);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   179
        vm.loadAgent(jarFile.getAbsolutePath());
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   180
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   181
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   182
    private static byte[] getBytecodes(String myName) throws Exception {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   183
        InputStream is = DefineClass.class.getResourceAsStream(myName + ".class");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   184
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   185
        byte[] buf = new byte[4096];
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   186
        int len;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   187
        while ((len = is.read(buf)) != -1) baos.write(buf, 0, len);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   188
        buf = baos.toByteArray();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   189
        System.out.println("sizeof(" + myName + ".class) == " + buf.length);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   190
        return buf;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   191
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   192
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   193
    private static int getStringIndex(String needle, byte[] buf) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   194
        return getStringIndex(needle, buf, 0);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   195
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   196
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   197
    private static int getStringIndex(String needle, byte[] buf, int offset) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   198
        outer:
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   199
        for (int i = offset; i < buf.length - offset - needle.length(); i++) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   200
            for (int j = 0; j < needle.length(); j++) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   201
                if (buf[i + j] != (byte)needle.charAt(j)) continue outer;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   202
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   203
            return i;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   204
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   205
        return 0;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   206
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   207
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   208
    private static void replaceString(byte[] buf, String name, int index) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   209
        for (int i = index; i < index + name.length(); i++) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   210
            buf[i] = (byte)name.charAt(i - index);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   211
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   212
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   213
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   214
    private static MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   215
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   216
    private static int getClassStats(String pattern) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   217
        try {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   218
            ObjectName diagCmd = new ObjectName("com.sun.management:type=DiagnosticCommand");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   219
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   220
            String result = (String)mbserver.invoke(diagCmd , "gcClassStats" , new Object[] { null }, new String[] {String[].class.getName()});
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   221
            int count = 0;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   222
            try (Scanner s = new Scanner(result)) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   223
                if (s.hasNextLine()) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   224
                    System.out.println(s.nextLine());
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   225
                }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   226
                while (s.hasNextLine()) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   227
                    String l = s.nextLine();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   228
                    if (l.endsWith(pattern)) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   229
                        count++;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   230
                        System.out.println(l);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   231
                    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   232
                }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   233
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   234
            return count;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   235
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   236
        catch (Exception e) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   237
            throw new RuntimeException("Test failed because we can't read the class statistics!", e);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   238
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   239
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   240
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   241
    private static void printClassStats(int expectedCount, boolean reportError) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   242
        int count = getClassStats("DefineClass");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   243
        String res = "Should have " + expectedCount +
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   244
                     " DefineClass instances and we have: " + count;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   245
        System.out.println(res);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   246
        if (reportError && count != expectedCount) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   247
            throw new RuntimeException(res);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   248
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   249
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   250
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   251
    public static final int ITERATIONS = 10;
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   252
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   253
    public static void main(String[] args) throws Exception {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   254
        String myName = DefineClass.class.getName();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   255
        byte[] buf = getBytecodes(myName.substring(myName.lastIndexOf(".") + 1));
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   256
        int iterations = (args.length > 1 ? Integer.parseInt(args[1]) : ITERATIONS);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   257
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   258
        if (args.length == 0 || "defineClass".equals(args[0])) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   259
            MyClassLoader cl = new MyClassLoader();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   260
            for (int i = 0; i < iterations; i++) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   261
                try {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   262
                    @SuppressWarnings("unchecked")
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   263
                    Class<DefineClass> dc = (Class<DefineClass>) cl.myDefineClass(myName, buf, 0, buf.length);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   264
                    System.out.println(dc);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   265
                }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   266
                catch (LinkageError jle) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   267
                    // Can only define once!
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   268
                    if (i == 0) throw new Exception("Should succeed the first time.");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   269
                }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   270
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   271
            // We expect to have two instances of DefineClass here: the initial version in which we are
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   272
            // executing and another version which was loaded into our own classloader 'MyClassLoader'.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   273
            // All the subsequent attempts to reload DefineClass into our 'MyClassLoader' should have failed.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   274
            printClassStats(2, false);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   275
            System.gc();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   276
            System.out.println("System.gc()");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   277
            // At least after System.gc() the failed loading attempts should leave no instances around!
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   278
            printClassStats(2, true);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   279
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   280
        else if ("defineSystemClass".equals(args[0])) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   281
            MyClassLoader cl = new MyClassLoader();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   282
            int index = getStringIndex("test/DefineClass", buf);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   283
            replaceString(buf, "java/DefineClass", index);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   284
            while ((index = getStringIndex("Ltest/DefineClass;", buf, index + 1)) != 0) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   285
                replaceString(buf, "Ljava/DefineClass;", index);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   286
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   287
            index = getStringIndex("test.DefineClass", buf);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   288
            replaceString(buf, "java.DefineClass", index);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   289
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   290
            for (int i = 0; i < iterations; i++) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   291
                try {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   292
                    @SuppressWarnings("unchecked")
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   293
                    Class<DefineClass> dc = (Class<DefineClass>) cl.myDefineClass(null, buf, 0, buf.length);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   294
                    throw new RuntimeException("Defining a class in the 'java' package should fail!");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   295
                }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   296
                catch (java.lang.SecurityException jlse) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   297
                    // Expected, because we're not allowed to define a class in the 'java' package
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   298
                }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   299
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   300
            // We expect to stay with one (the initial) instances of DefineClass.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   301
            // All the subsequent attempts to reload DefineClass into the 'java' package should have failed.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   302
            printClassStats(1, false);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   303
            System.gc();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   304
            System.out.println("System.gc()");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   305
            // At least after System.gc() the failed loading attempts should leave no instances around!
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   306
            printClassStats(1, true);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   307
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   308
        else if ("defineClassParallel".equals(args[0])) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   309
            MyParallelClassLoader pcl = new MyParallelClassLoader();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   310
            CountDownLatch stop = new CountDownLatch(1);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   311
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   312
            Thread[] threads = new Thread[iterations];
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   313
            for (int i = 0; i < iterations; i++) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   314
                (threads[i] = new ParallelLoadingThread(pcl, buf, stop)).start();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   315
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   316
            stop.countDown(); // start parallel class loading..
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   317
            // ..and wait until all threads loaded the class
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   318
            for (int i = 0; i < iterations; i++) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   319
                threads[i].join();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   320
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   321
            System.out.print("Counted " + pcl.getLinkageErrors() + " LinkageErrors ");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   322
            System.out.println(pcl.getLinkageErrors() == 0 ?
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   323
                    "" : "(use -XX:+UnsyncloadClass and/or -XX:+AllowParallelDefineClass to avoid this)");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   324
            System.gc();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   325
            System.out.println("System.gc()");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   326
            // After System.gc() we expect to remain with two instances: one is the initial version which is
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   327
            // kept alive by this main method and another one in the parallel class loader.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   328
            printClassStats(2, true);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   329
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   330
        else if ("redefineClass".equals(args[0])) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   331
            loadInstrumentationAgent(myName, buf);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   332
            int index = getStringIndex("AAAAAAAA", buf);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   333
            CountDownLatch stop = new CountDownLatch(1);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   334
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   335
            Thread[] threads = new Thread[iterations];
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   336
            for (int i = 0; i < iterations; i++) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   337
                buf[index] = (byte) ('A' + i + 1); // Change string constant in getID() which is legal in redefinition
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   338
                instrumentation.redefineClasses(new ClassDefinition(DefineClass.class, buf));
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   339
                DefineClass dc = DefineClass.class.newInstance();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   340
                CountDownLatch start = new CountDownLatch(1);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   341
                (threads[i] = new MyThread(dc, start, stop)).start();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   342
                start.await(); // Wait until the new thread entered the getID() method
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   343
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   344
            // We expect to have one instance for each redefinition because they are all kept alive by an activation
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   345
            // plus the initial version which is kept active by this main method.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   346
            printClassStats(iterations + 1, false);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   347
            stop.countDown(); // Let all threads leave the DefineClass.getID() activation..
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   348
            // ..and wait until really all of them returned from DefineClass.getID()
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   349
            for (int i = 0; i < iterations; i++) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   350
                threads[i].join();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   351
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   352
            System.gc();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   353
            System.out.println("System.gc()");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   354
            // After System.gc() we expect to remain with two instances: one is the initial version which is
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   355
            // kept alive by this main method and another one which is the latest redefined version.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   356
            printClassStats(2, true);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   357
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   358
        else if ("redefineClassWithError".equals(args[0])) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   359
            loadInstrumentationAgent(myName, buf);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   360
            int index = getStringIndex("getID", buf);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   361
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   362
            for (int i = 0; i < iterations; i++) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   363
                buf[index] = (byte) 'X'; // Change getID() to XetID() which is illegal in redefinition
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   364
                try {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   365
                    instrumentation.redefineClasses(new ClassDefinition(DefineClass.class, buf));
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   366
                    throw new RuntimeException("Class redefinition isn't allowed to change method names!");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   367
                }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   368
                catch (UnsupportedOperationException uoe) {
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   369
                    // Expected because redefinition can't change the name of methods
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   370
                }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   371
            }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   372
            // We expect just a single DefineClass instance because failed redefinitions should
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   373
            // leave no garbage around.
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   374
            printClassStats(1, false);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   375
            System.gc();
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   376
            System.out.println("System.gc()");
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   377
            // At least after a System.gc() we should definitely stay with a single instance!
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   378
            printClassStats(1, true);
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   379
        }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   380
    }
26f3c55e246e 8173743: Failures during class definition can lead to memory leaks in metaspace
simonis
parents:
diff changeset
   381
}