test/hotspot/jtreg/vmTestbase/nsk/jvmti/RedefineClasses/StressRedefine.java
changeset 51221 b65916c52e3c
parent 50260 46c67f5e27c2
child 51269 b53d1f96b8c4
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/RedefineClasses/StressRedefine.java	Fri Jul 20 14:34:51 2018 -0700
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/RedefineClasses/StressRedefine.java	Fri Jul 20 18:03:23 2018 -0400
@@ -61,7 +61,10 @@
     private static ExecutionController stresser;
     private static String[] args;
 
-    // This is random generator used for generating seeds for other Randoms. Setting seed from command line sets seed for this random.
+    private static byte[] bytecode;
+
+    // This is random generator used for generating seeds for other Randoms. Setting seed
+    // from command line sets seed for this random.
     static Random seedGenerator;
 
     static {
@@ -82,47 +85,53 @@
         Tests.runTest(new StressRedefine(), args);
     }
 
-        @Override
-        public void run() {
-                seedGenerator = new Random(runParams.getSeed());
-                GenerateSourceHelper.setRandom(new Random(seedGenerator.nextLong()));
+    @Override
+    public void run() {
+        seedGenerator = new Random(runParams.getSeed());
+        GenerateSourceHelper.setRandom(new Random(seedGenerator.nextLong()));
         stresser = new Stresser(args);
 
         for (int i = 0; i < args.length; i++ ) {
-                if ("-staticMethodCallersNumber".equals(args[i])) {
-                        staticMethodCallersNumber = Integer.parseInt(args[i + 1]);
-                } else if ("-nonstaticMethodCallersNumber".equals(args[i])) {
-                        nonstaticMethodCallersNumber = Integer.parseInt(args[i + 1]);
-                } else if ("-redefiningThreadsNumber".equals(args[i])) {
-                        redefiningThreadsNumber = Integer.parseInt(args[i + 1]);
-                } else if ("-corruptingBytecodeProbability".equals(args[i])) {
-                        corruptingBytecodeProbability = Double.parseDouble(args[i + 1]);
-                }
+            if ("-staticMethodCallersNumber".equals(args[i])) {
+                staticMethodCallersNumber = Integer.parseInt(args[i + 1]);
+            } else if ("-nonstaticMethodCallersNumber".equals(args[i])) {
+                nonstaticMethodCallersNumber = Integer.parseInt(args[i + 1]);
+            } else if ("-redefiningThreadsNumber".equals(args[i])) {
+                redefiningThreadsNumber = Integer.parseInt(args[i + 1]);
+            } else if ("-corruptingBytecodeProbability".equals(args[i])) {
+                corruptingBytecodeProbability = Double.parseDouble(args[i + 1]);
+            }
         }
 
         //Dynamic attach if required
         nsk.share.jvmti.JVMTITest.commonInit(args);
 
         new StressRedefine().runIt();
+    }
+
+    private static void runMethod(Random random, String name) {
+        while (stresser.continueExecution()) {
+            try {
+                // Just for fun we transfer parameters to method
+                Object res = myClass.getMethod(name, double.class, int.class, Object.class)
+                                         .invoke(null, random.nextDouble(), random.nextInt(), new Object());
+             } catch (IllegalArgumentException | InvocationTargetException
+                     | IllegalAccessException | NoSuchMethodException e) {
+                 // It's okay to get exception here since we are corrupting bytecode and can't expect
+                 // class to work properly.
+                 System.out.println("Got expected exception: " + e.toString());
+             }
         }
+    }
 
     private static class StaticMethodCaller implements Runnable {
         private Random random;
         public StaticMethodCaller() {random = new Random(seedGenerator.nextLong());}
 
-                @Override
-                public void run() {
-                        while (stresser.continueExecution()) {
-                                try {
-                                        Object res = myClass.getMethod(GenerateSourceHelper.STATIC_METHOD_NAME, double.class, int.class, Object.class).invoke(
-                                                        null, random.nextDouble(), random.nextInt(), new Object()); // Just for fun we transfer parameters to method
-                                } catch (IllegalArgumentException | InvocationTargetException
-                                                | IllegalAccessException | NoSuchMethodException e) {
-                                        //It's okay to get exception here since we are corrupting bytecode and can't expect class to work properly.
-                                        System.out.println("Got expected exception: " + e.toString());
-                                }
-                        }
-                }
+        @Override
+        public void run() {
+            runMethod(random, GenerateSourceHelper.STATIC_METHOD_NAME);
+        }
     }
 
     private static class NonstaticMethodCaller implements Runnable {
@@ -130,59 +139,54 @@
         public NonstaticMethodCaller() {random = new Random(seedGenerator.nextLong());}
 
         @Override
-                public void run() {
-                        while (stresser.continueExecution()) {
-                                try {
-                                        Object res = myClass.getMethod(GenerateSourceHelper.NONSTATIC_METHOD_NAME, double.class, int.class, Object.class).
-                                                        invoke(myClass.newInstance(), random.nextDouble(), random.nextInt(), new Object()); // Just for fun we transfer parameters to method
-                                } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) {
-                                        //It's okay to get exception here since we are corrupting bytecode and can't expect class to work properly.
-                                        System.out.println("Got expected exception: " + e.toString());
-                                }
-                        }
-                }
+        public void run() {
+            runMethod(random, GenerateSourceHelper.NONSTATIC_METHOD_NAME);
+        }
     }
 
     private static class Worker implements Runnable {
         private Random random;
         public Worker() {random = new Random(seedGenerator.nextLong());}
 
-                @Override
-                public void run() {
-                        while (stresser.continueExecution()) {
-                                byte[] bytecode = generateAndCompile();
-                                if (random.nextDouble() < corruptingBytecodeProbability) {
-                                        bytecode[random.nextInt(bytecode.length)] = 42;
-                                }
-                                makeRedefinition(2, myClass, bytecode);
-                        }
+        @Override
+        public void run() {
+            while (stresser.continueExecution()) {
+                byte[] badBytecode = bytecode.clone();
+                if (random.nextDouble() < corruptingBytecodeProbability) {
+                    badBytecode[random.nextInt(bytecode.length)] = 42;
                 }
+                makeRedefinition(2, myClass, badBytecode);
+            }
+        }
     }
 
     private void runIt() {
         myClass = new DefiningClassLoader().defineClass(generateAndCompile());
         stresser.start(0);
 
+        // Generate some bytecode.
+        bytecode = generateAndCompile();
+
         List<Thread> threads = new LinkedList<Thread>();
         for (int i = 0; i < staticMethodCallersNumber; i++) {
-                threads.add(new Thread(new StaticMethodCaller()));
+            threads.add(new Thread(new StaticMethodCaller()));
         }
         for (int i = 0; i < nonstaticMethodCallersNumber; i++) {
-                threads.add(new Thread(new NonstaticMethodCaller()));
+            threads.add(new Thread(new NonstaticMethodCaller()));
         }
         for (int i = 0; i < redefiningThreadsNumber; i++) {
-                threads.add(new Thread(new Worker()));
+            threads.add(new Thread(new Worker()));
         }
 
         for (Thread thread : threads) {
-                thread.start();
+            thread.start();
         }
         for (Thread thread : threads) {
-                try {
-                        thread.join();
-                } catch (InterruptedException e) {
-                        throw new TestFailure("Thread " + Thread.currentThread() + " was interrupted:", e);
-                }
+            try {
+                thread.join();
+            } catch (InterruptedException e) {
+                throw new TestFailure("Thread " + Thread.currentThread() + " was interrupted:", e);
+            }
         }
     }
 
@@ -192,15 +196,14 @@
         return InMemoryJavaCompiler.compile(sources).values().iterator().next();
     }
 
-        // Auxiliary classloader. Used only once at the beginning.
-        private static class DefiningClassLoader extends URLClassLoader {
-                public DefiningClassLoader() {
-                        super(new URL[0]);
-                }
-
-                Class<?> defineClass(byte[] bytecode) {
-                        return defineClass(null, bytecode, 0, bytecode.length);
-                }
+    // Auxiliary classloader. Used only once at the beginning.
+    private static class DefiningClassLoader extends URLClassLoader {
+        public DefiningClassLoader() {
+            super(new URL[0]);
         }
 
+        Class<?> defineClass(byte[] bytecode) {
+            return defineClass(null, bytecode, 0, bytecode.length);
+        }
+    }
 }