Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8014229

Instrumentation.redefineClasses() for sun.plugin2.main.client.PluginMain

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P3 P3
    • 9
    • 7u21
    • core-svc

        FULL PRODUCT VERSION :
        java version " 1.7.0_21 "
        Java(TM) SE Runtime Environment (build 1.7.0_21-b12)
        Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

        FULL OS VERSION :
        Darwin Svens-MacBook-Pro-2.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64


        A DESCRIPTION OF THE PROBLEM :
        Calling Instrumentation.redefineClasses() for bytecode read from sun.plugin2.main.client.PluginMain.class produces the following exception:

        java.lang.InternalError
            at sun.instrument.InstrumentationImpl.redefineClasses0(Native Method)
            at sun.instrument.InstrumentationImpl.redefineClasses(InstrumentationImpl.java:170)
            at com.zeroturnaround.bugs.redefineClasses.RedefinerAgent.patch(RedefinerAgent:37)
            at com.zeroturnaround.bugs.redefineClasses.RedefinerAgent.premain(RedefinerAgent:28)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:601)

        THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: Did not try

        THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: Did not try

        REGRESSION. Last worked in version 6u45

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
          To reproduce the bug, build a redefiner-agent jar that tries to invoke Instrumentation.redefineClasses() with bytecode read from sun.plugin2.main.client.PluginMain.class. Build the javaagent jar and run any java application with plugin.jar and deploy.jar from JRE lib added to classpath by invoking:
        $ java -javaagent:/path/to/redefiner-agent.jar -cp /path/to/plugin.jar:/path/to/deploy.jar:. MyClass

        Please see " Source code for an executable test case " for a sample javaagent implementation.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        Expected result: Instrumentation.redefineClasses() passes without an error for unmodified bytecode read from the .class

        Actual result: an InternalError is thrown from a native Instrumentation.redefineClasses0() method.
        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        java.lang.InternalError
            at sun.instrument.InstrumentationImpl.redefineClasses0(Native Method)
            at sun.instrument.InstrumentationImpl.redefineClasses(InstrumentationImpl.java:170)
            at com.zeroturnaround.bugs.redefineClasses.RedefinerAgent.patch(RedefinerAgent:37)
            at com.zeroturnaround.bugs.redefineClasses.RedefinerAgent.premain(RedefinerAgent:28)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:601)

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package com.zeroturnaround.bugs.redefineClasses;

        import java.io.ByteArrayOutputStream;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.OutputStream;
        import java.lang.instrument.ClassDefinition;
        import java.lang.instrument.Instrumentation;

        /**
         * RedefinerAgent java agent showing an error with InstrumentationImpl.redefineClasses() when supplied
         * with bytecode read from sun.plugin2.main.client.PluginMain class.
         *
         * Reproducible on:
         * OSX version 10.7.5
         * java version " 1.7.0_21 "
         * Java(TM) SE Runtime Environment (build 1.7.0_21-b12)
         * Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)
         *
         * @author lanza
         *
         */
        public class RedefinerAgent {

        public static void premain(String agentArguments, Instrumentation instrumentation) {
        System.out.println( " Redefiner agent init! " );
        patch( " sun.plugin2.applet.Applet2ClassLoader " , instrumentation);
        patch( " sun.plugin2.main.client.PluginMain " , instrumentation);
        }

        private static void patch(String className, Instrumentation instrumentation) {
        try {
        System.out.println( " Start patching: " + className);

        Class<?> clazz = Class.forName(className, false, ClassLoader.getSystemClassLoader());
        byte[] bytes = Util.getBytes(clazz);
        instrumentation.redefineClasses(new ClassDefinition(clazz, bytes));

        System.out.println( " End patching: " + className);
        } catch (InternalError e) {
        System.err.println( " Internal error redefining class: " + className);
        e.printStackTrace();
        } catch (Exception e) {
        e.printStackTrace();
        }
        }
        }



        class Util {
        private static final int BUFFER_SIZE = 4096;

        public static byte[] getBytes(Class<?> clazz) throws IOException {
        String url = clazz.getName().replace( " . " , " / " ) + " .class " ;
        try (InputStream in = ClassLoader.getSystemResourceAsStream(url); ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        copy(in, out);
        return out.toByteArray();
        }
        }

        public static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE];
        int read;

        while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
        }
        }
        }

        ---------- END SOURCE ----------

              Unassigned Unassigned
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: