< prev index next >

src/java.base/share/classes/java/lang/invoke/ClassSpecializer.java

Print this page
rev 54607 : 8222895: StackOverflowError in custom security manager that relies on ClassSpecializer
Reviewed-by: TBD


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 

  28 import jdk.internal.loader.BootLoader;
  29 import jdk.internal.org.objectweb.asm.ClassWriter;
  30 import jdk.internal.org.objectweb.asm.FieldVisitor;
  31 import jdk.internal.org.objectweb.asm.MethodVisitor;
  32 import jdk.internal.vm.annotation.Stable;
  33 import sun.invoke.util.BytecodeName;
  34 
  35 import java.lang.reflect.Constructor;
  36 import java.lang.reflect.Field;
  37 import java.lang.reflect.Modifier;
  38 import java.security.AccessController;
  39 import java.security.PrivilegedAction;

  40 import java.util.ArrayList;
  41 import java.util.Collections;
  42 import java.util.List;
  43 import java.util.Objects;
  44 import java.util.concurrent.ConcurrentHashMap;
  45 import java.util.function.Function;
  46 
  47 import static java.lang.invoke.LambdaForm.*;
  48 import static java.lang.invoke.MethodHandleNatives.Constants.REF_getStatic;
  49 import static java.lang.invoke.MethodHandleNatives.Constants.REF_putStatic;
  50 import static java.lang.invoke.MethodHandleStatics.*;
  51 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  52 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  53 
  54 /**
  55  * Class specialization code.
  56  * @param <T> top class under which species classes are created.
  57  * @param <K> key which identifies individual specializations.
  58  * @param <S> species data type.
  59  */


 558          *     }
 559          *     // two transforms, for the sake of illustration:
 560          *     final TopClass copyWithExtendL(CT ctarg, ..., Object narg) {
 561          *         return BMH_SPECIES.transform(L_TYPE).invokeBasic(ctarg, ..., argL0, argL1, argI2, narg);
 562          *     }
 563          *     final TopClass copyWithExtendI(CT ctarg, ..., int narg) {
 564          *         return BMH_SPECIES.transform(I_TYPE).invokeBasic(ctarg, ..., argL0, argL1, argI2, narg);
 565          *     }
 566          * }
 567          * </pre>
 568          *
 569          * @param className of the species
 570          * @param speciesData what species we are generating
 571          * @return the generated concrete TopClass class
 572          */
 573         Class<? extends T> generateConcreteSpeciesCode(String className, ClassSpecializer<T,K,S>.SpeciesData speciesData) {
 574             byte[] classFile = generateConcreteSpeciesCodeFile(className, speciesData);
 575 
 576             // load class
 577             InvokerBytecodeGenerator.maybeDump(classBCName(className), classFile);
 578             Class<?> speciesCode;
 579 
 580             MethodHandles.Lookup lookup = IMPL_LOOKUP.in(topClass());
 581             speciesCode = AccessController.doPrivileged(new PrivilegedAction<>() {

 582                 @Override
 583                 public Class<?> run() {
 584                     try {
 585                         return lookup.defineClass(classFile);
 586                     } catch (Exception ex) {
 587                         throw newInternalError(ex);
 588                     }
 589                 }
 590             });



 591             return speciesCode.asSubclass(topClass());
 592         }
 593 
 594         // These are named like constants because there is only one per specialization scheme:
 595         private final String SPECIES_DATA = classBCName(metaType);
 596         private final String SPECIES_DATA_SIG = classSig(SPECIES_DATA);
 597         private final String SPECIES_DATA_NAME = sdAccessor.getName();
 598         private final int SPECIES_DATA_MODS = sdAccessor.getModifiers();
 599         private final List<String> TRANSFORM_NAMES;  // derived from transformMethods
 600         private final List<MethodType> TRANSFORM_TYPES;
 601         private final List<Integer> TRANSFORM_MODS;
 602         {
 603             // Tear apart transformMethods to get the names, types, and modifiers.
 604             List<String> tns = new ArrayList<>();
 605             List<MethodType> tts = new ArrayList<>();
 606             List<Integer> tms = new ArrayList<>();
 607             for (int i = 0; i < transformMethods.size(); i++) {
 608                 MemberName tm = transformMethods.get(i);
 609                 tns.add(tm.getName());
 610                 final MethodType tt = tm.getMethodType();




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import jdk.internal.access.SharedSecrets;
  29 import jdk.internal.loader.BootLoader;
  30 import jdk.internal.org.objectweb.asm.ClassWriter;
  31 import jdk.internal.org.objectweb.asm.FieldVisitor;
  32 import jdk.internal.org.objectweb.asm.MethodVisitor;
  33 import jdk.internal.vm.annotation.Stable;
  34 import sun.invoke.util.BytecodeName;
  35 
  36 import java.lang.reflect.Constructor;
  37 import java.lang.reflect.Field;
  38 import java.lang.reflect.Modifier;
  39 import java.security.AccessController;
  40 import java.security.PrivilegedAction;
  41 import java.security.ProtectionDomain;
  42 import java.util.ArrayList;
  43 import java.util.Collections;
  44 import java.util.List;
  45 import java.util.Objects;
  46 import java.util.concurrent.ConcurrentHashMap;
  47 import java.util.function.Function;
  48 
  49 import static java.lang.invoke.LambdaForm.*;
  50 import static java.lang.invoke.MethodHandleNatives.Constants.REF_getStatic;
  51 import static java.lang.invoke.MethodHandleNatives.Constants.REF_putStatic;
  52 import static java.lang.invoke.MethodHandleStatics.*;
  53 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  54 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  55 
  56 /**
  57  * Class specialization code.
  58  * @param <T> top class under which species classes are created.
  59  * @param <K> key which identifies individual specializations.
  60  * @param <S> species data type.
  61  */


 560          *     }
 561          *     // two transforms, for the sake of illustration:
 562          *     final TopClass copyWithExtendL(CT ctarg, ..., Object narg) {
 563          *         return BMH_SPECIES.transform(L_TYPE).invokeBasic(ctarg, ..., argL0, argL1, argI2, narg);
 564          *     }
 565          *     final TopClass copyWithExtendI(CT ctarg, ..., int narg) {
 566          *         return BMH_SPECIES.transform(I_TYPE).invokeBasic(ctarg, ..., argL0, argL1, argI2, narg);
 567          *     }
 568          * }
 569          * </pre>
 570          *
 571          * @param className of the species
 572          * @param speciesData what species we are generating
 573          * @return the generated concrete TopClass class
 574          */
 575         Class<? extends T> generateConcreteSpeciesCode(String className, ClassSpecializer<T,K,S>.SpeciesData speciesData) {
 576             byte[] classFile = generateConcreteSpeciesCodeFile(className, speciesData);
 577 
 578             // load class
 579             InvokerBytecodeGenerator.maybeDump(classBCName(className), classFile);
 580             ClassLoader cl = topClass.getClassLoader();
 581             ProtectionDomain pd = null;
 582             if (cl != null) {
 583                 pd = AccessController.doPrivileged(
 584                         new PrivilegedAction<>() {
 585                             @Override
 586                             public ProtectionDomain run() {
 587                                 return topClass().getProtectionDomain();




 588                             }
 589                         });
 590             }
 591             Class<?> speciesCode = SharedSecrets.getJavaLangAccess()
 592                     .defineClass(cl, className, classFile, pd, "_ClassSpecializer_generateConcreteSpeciesCode");
 593             return speciesCode.asSubclass(topClass());
 594         }
 595 
 596         // These are named like constants because there is only one per specialization scheme:
 597         private final String SPECIES_DATA = classBCName(metaType);
 598         private final String SPECIES_DATA_SIG = classSig(SPECIES_DATA);
 599         private final String SPECIES_DATA_NAME = sdAccessor.getName();
 600         private final int SPECIES_DATA_MODS = sdAccessor.getModifiers();
 601         private final List<String> TRANSFORM_NAMES;  // derived from transformMethods
 602         private final List<MethodType> TRANSFORM_TYPES;
 603         private final List<Integer> TRANSFORM_MODS;
 604         {
 605             // Tear apart transformMethods to get the names, types, and modifiers.
 606             List<String> tns = new ArrayList<>();
 607             List<MethodType> tts = new ArrayList<>();
 608             List<Integer> tms = new ArrayList<>();
 609             for (int i = 0; i < transformMethods.size(); i++) {
 610                 MemberName tm = transformMethods.get(i);
 611                 tns.add(tm.getName());
 612                 final MethodType tt = tm.getMethodType();


< prev index next >