test/hotspot/jtreg/vmTestbase/nsk/jvmti/RedefineClasses/StressRedefine.java
author iignatyev
Thu, 24 May 2018 17:12:15 -0700
changeset 50260 46c67f5e27c2
child 51221 b65916c52e3c
permissions -rw-r--r--
8199383: [TESTBUG] Open source VM testbase JVMTI tests Reviewed-by: sspitsyn, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50260
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
     1
/*
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
     2
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
     4
 *
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
     7
 * published by the Free Software Foundation.
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
     8
 *
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    13
 * accompanied this code).
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    14
 *
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    18
 *
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    21
 * questions.
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    22
 */
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    23
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    24
//package nsk.jvmti.RedefineClasses.StressRedefine;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    25
package nsk.jvmti.RedefineClasses;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    26
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    27
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    28
import java.lang.reflect.InvocationTargetException;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    29
import java.lang.reflect.Method;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    30
import java.net.URL;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    31
import java.net.URLClassLoader;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    32
import java.util.HashMap;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    33
import java.util.LinkedList;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    34
import java.util.List;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    35
import java.util.Map;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    36
import java.util.Random;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    37
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    38
import nsk.share.TestFailure;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    39
import nsk.share.gc.GCTestBase;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    40
import nsk.share.test.ExecutionController;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    41
import nsk.share.test.Stresser;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    42
import nsk.share.test.Tests;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    43
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    44
import vm.share.InMemoryJavaCompiler;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    45
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    46
/**
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    47
 * There is a data structure named "dictionary" in class BlockFreelist. It stores
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    48
 * information about free memory blocks for further reusing. Allocation of new block goes
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    49
 * from dictionary only if dictionary is fat enough. (At the moment of test creation this limit is 64K.)
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    50
 *
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    51
 * This tests stresses dictionary as other test metaspace/StressDictionary does, but instead of
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    52
 * failing classloading this test leverages redefineClass method from jvmti.
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    53
 */
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    54
public class StressRedefine extends GCTestBase {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    55
    private static int staticMethodCallersNumber = 10;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    56
    private static int nonstaticMethodCallersNumber = 10;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    57
    private static int redefiningThreadsNumber = 40;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    58
    private static double corruptingBytecodeProbability = .75;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    59
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    60
    private static volatile Class<?> myClass;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    61
    private static ExecutionController stresser;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    62
    private static String[] args;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    63
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    64
    // This is random generator used for generating seeds for other Randoms. Setting seed from command line sets seed for this random.
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    65
    static Random seedGenerator;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    66
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    67
    static {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    68
        try {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    69
            System.loadLibrary("stressRedefine");
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    70
        } catch (UnsatisfiedLinkError e) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    71
            System.err.println("Could not load stressRedefine library");
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    72
            System.err.println("java.library.path:" +
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    73
                System.getProperty("java.library.path"));
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    74
            throw e;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    75
        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    76
    }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    77
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    78
    native static int makeRedefinition(int verbose, Class<?> redefClass, byte[] classBytes);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    79
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    80
    public static void main(String[] args) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    81
        StressRedefine.args = args;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    82
        Tests.runTest(new StressRedefine(), args);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    83
    }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    84
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    85
        @Override
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    86
        public void run() {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    87
                seedGenerator = new Random(runParams.getSeed());
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    88
                GenerateSourceHelper.setRandom(new Random(seedGenerator.nextLong()));
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    89
        stresser = new Stresser(args);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    90
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    91
        for (int i = 0; i < args.length; i++ ) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    92
                if ("-staticMethodCallersNumber".equals(args[i])) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    93
                        staticMethodCallersNumber = Integer.parseInt(args[i + 1]);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    94
                } else if ("-nonstaticMethodCallersNumber".equals(args[i])) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    95
                        nonstaticMethodCallersNumber = Integer.parseInt(args[i + 1]);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    96
                } else if ("-redefiningThreadsNumber".equals(args[i])) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    97
                        redefiningThreadsNumber = Integer.parseInt(args[i + 1]);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    98
                } else if ("-corruptingBytecodeProbability".equals(args[i])) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
    99
                        corruptingBytecodeProbability = Double.parseDouble(args[i + 1]);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   100
                }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   101
        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   102
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   103
        //Dynamic attach if required
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   104
        nsk.share.jvmti.JVMTITest.commonInit(args);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   105
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   106
        new StressRedefine().runIt();
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   107
        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   108
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   109
    private static class StaticMethodCaller implements Runnable {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   110
        private Random random;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   111
        public StaticMethodCaller() {random = new Random(seedGenerator.nextLong());}
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   112
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   113
                @Override
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   114
                public void run() {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   115
                        while (stresser.continueExecution()) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   116
                                try {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   117
                                        Object res = myClass.getMethod(GenerateSourceHelper.STATIC_METHOD_NAME, double.class, int.class, Object.class).invoke(
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   118
                                                        null, random.nextDouble(), random.nextInt(), new Object()); // Just for fun we transfer parameters to method
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   119
                                } catch (IllegalArgumentException | InvocationTargetException
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   120
                                                | IllegalAccessException | NoSuchMethodException e) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   121
                                        //It's okay to get exception here since we are corrupting bytecode and can't expect class to work properly.
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   122
                                        System.out.println("Got expected exception: " + e.toString());
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   123
                                }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   124
                        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   125
                }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   126
    }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   127
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   128
    private static class NonstaticMethodCaller implements Runnable {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   129
        private Random random;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   130
        public NonstaticMethodCaller() {random = new Random(seedGenerator.nextLong());}
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   131
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   132
        @Override
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   133
                public void run() {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   134
                        while (stresser.continueExecution()) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   135
                                try {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   136
                                        Object res = myClass.getMethod(GenerateSourceHelper.NONSTATIC_METHOD_NAME, double.class, int.class, Object.class).
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   137
                                                        invoke(myClass.newInstance(), random.nextDouble(), random.nextInt(), new Object()); // Just for fun we transfer parameters to method
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   138
                                } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   139
                                        //It's okay to get exception here since we are corrupting bytecode and can't expect class to work properly.
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   140
                                        System.out.println("Got expected exception: " + e.toString());
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   141
                                }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   142
                        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   143
                }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   144
    }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   145
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   146
    private static class Worker implements Runnable {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   147
        private Random random;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   148
        public Worker() {random = new Random(seedGenerator.nextLong());}
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   149
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   150
                @Override
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   151
                public void run() {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   152
                        while (stresser.continueExecution()) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   153
                                byte[] bytecode = generateAndCompile();
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   154
                                if (random.nextDouble() < corruptingBytecodeProbability) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   155
                                        bytecode[random.nextInt(bytecode.length)] = 42;
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   156
                                }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   157
                                makeRedefinition(2, myClass, bytecode);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   158
                        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   159
                }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   160
    }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   161
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   162
    private void runIt() {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   163
        myClass = new DefiningClassLoader().defineClass(generateAndCompile());
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   164
        stresser.start(0);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   165
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   166
        List<Thread> threads = new LinkedList<Thread>();
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   167
        for (int i = 0; i < staticMethodCallersNumber; i++) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   168
                threads.add(new Thread(new StaticMethodCaller()));
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   169
        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   170
        for (int i = 0; i < nonstaticMethodCallersNumber; i++) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   171
                threads.add(new Thread(new NonstaticMethodCaller()));
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   172
        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   173
        for (int i = 0; i < redefiningThreadsNumber; i++) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   174
                threads.add(new Thread(new Worker()));
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   175
        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   176
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   177
        for (Thread thread : threads) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   178
                thread.start();
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   179
        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   180
        for (Thread thread : threads) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   181
                try {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   182
                        thread.join();
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   183
                } catch (InterruptedException e) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   184
                        throw new TestFailure("Thread " + Thread.currentThread() + " was interrupted:", e);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   185
                }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   186
        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   187
    }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   188
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   189
    private static byte[] generateAndCompile() {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   190
        Map<String, CharSequence> sources = new HashMap<String, CharSequence>();
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   191
        sources.put(GenerateSourceHelper.CLASS_NAME, GenerateSourceHelper.generateSource());
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   192
        return InMemoryJavaCompiler.compile(sources).values().iterator().next();
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   193
    }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   194
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   195
        // Auxiliary classloader. Used only once at the beginning.
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   196
        private static class DefiningClassLoader extends URLClassLoader {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   197
                public DefiningClassLoader() {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   198
                        super(new URL[0]);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   199
                }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   200
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   201
                Class<?> defineClass(byte[] bytecode) {
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   202
                        return defineClass(null, bytecode, 0, bytecode.length);
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   203
                }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   204
        }
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   205
46c67f5e27c2 8199383: [TESTBUG] Open source VM testbase JVMTI tests
iignatyev
parents:
diff changeset
   206
}