1 /*
  2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  3  *
  4  * This code is free software; you can redistribute it and/or modify it
  5  * under the terms of the GNU General Public License version 2 only, as
  6  * published by the Free Software Foundation.  Oracle designates this
  7  * particular file as subject to the "Classpath" exception as provided
  8  * by Oracle in the LICENSE file that accompanied this code.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /*
 26  * This file is available under and governed by the GNU General Public
 27  * License version 2 only, as published by the Free Software Foundation.
 28  * However, the following notice accompanied the original version of this
 29  * file:
 30  *
 31  * ASM: a very small and fast Java bytecode manipulation framework
 32  * Copyright (c) 2000-2011 INRIA, France Telecom
 33  * All rights reserved.
 34  *
 35  * Redistribution and use in source and binary forms, with or without
 36  * modification, are permitted provided that the following conditions
 37  * are met:
 38  * 1. Redistributions of source code must retain the above copyright
 39  *    notice, this list of conditions and the following disclaimer.
 40  * 2. Redistributions in binary form must reproduce the above copyright
 41  *    notice, this list of conditions and the following disclaimer in the
 42  *    documentation and/or other materials provided with the distribution.
 43  * 3. Neither the name of the copyright holders nor the names of its
 44  *    contributors may be used to endorse or promote products derived from
 45  *    this software without specific prior written permission.
 46  *
 47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 57  * THE POSSIBILITY OF SUCH DAMAGE.
 58  */
 59 
 60 package jdk.internal.org.objectweb.asm;
 61 
 62 import java.io.DataInputStream;
 63 import java.io.IOException;
 64 import java.io.InputStream;
 65 import java.util.regex.Pattern;
 66 
 67 /**
 68  * Defines additional JVM opcodes, access flags and constants which are not part of the ASM public
 69  * API.
 70  *
 71  * @see <a href="https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-6.html">JVMS 6</a>
 72  * @author Eric Bruneton
 73  */
 74 final class Constants {
 75 
 76     // The ClassFile attribute names, in the order they are defined in
 77     // https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-4.html#jvms-4.7-300.
 78 
 79     static final String CONSTANT_VALUE = "ConstantValue";
 80     static final String CODE = "Code";
 81     static final String STACK_MAP_TABLE = "StackMapTable";
 82     static final String EXCEPTIONS = "Exceptions";
 83     static final String INNER_CLASSES = "InnerClasses";
 84     static final String ENCLOSING_METHOD = "EnclosingMethod";
 85     static final String SYNTHETIC = "Synthetic";
 86     static final String SIGNATURE = "Signature";
 87     static final String SOURCE_FILE = "SourceFile";
 88     static final String SOURCE_DEBUG_EXTENSION = "SourceDebugExtension";
 89     static final String LINE_NUMBER_TABLE = "LineNumberTable";
 90     static final String LOCAL_VARIABLE_TABLE = "LocalVariableTable";
 91     static final String LOCAL_VARIABLE_TYPE_TABLE = "LocalVariableTypeTable";
 92     static final String DEPRECATED = "Deprecated";
 93     static final String RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations";
 94     static final String RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations";
 95     static final String RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS = "RuntimeVisibleParameterAnnotations";
 96     static final String RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS =
 97             "RuntimeInvisibleParameterAnnotations";
 98     static final String RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations";
 99     static final String RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations";
100     static final String ANNOTATION_DEFAULT = "AnnotationDefault";
101     static final String BOOTSTRAP_METHODS = "BootstrapMethods";
102     static final String METHOD_PARAMETERS = "MethodParameters";
103     static final String MODULE = "Module";
104     static final String MODULE_PACKAGES = "ModulePackages";
105     static final String MODULE_MAIN_CLASS = "ModuleMainClass";
106     static final String NEST_HOST = "NestHost";
107     static final String NEST_MEMBERS = "NestMembers";
108     static final String PERMITTED_SUBCLASSES = "PermittedSubclasses";
109     static final String RECORD = "Record";
110 
111     // ASM specific access flags.
112     // WARNING: the 16 least significant bits must NOT be used, to avoid conflicts with standard
113     // access flags, and also to make sure that these flags are automatically filtered out when
114     // written in class files (because access flags are stored using 16 bits only).
115 
116     static final int ACC_CONSTRUCTOR = 0x40000; // method access flag.
117 
118     // ASM specific stack map frame types, used in {@link ClassVisitor#visitFrame}.
119 
120     /**
121       * A frame inserted between already existing frames. This internal stack map frame type (in
122       * addition to the ones declared in {@link Opcodes}) can only be used if the frame content can be
123       * computed from the previous existing frame and from the instructions between this existing frame
124       * and the inserted one, without any knowledge of the type hierarchy. This kind of frame is only
125       * used when an unconditional jump is inserted in a method while expanding an ASM specific
126       * instruction. Keep in sync with Opcodes.java.
127       */
128     static final int F_INSERT = 256;
129 
130     // The JVM opcode values which are not part of the ASM public API.
131     // See https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-6.html.
132 
133     static final int LDC_W = 19;
134     static final int LDC2_W = 20;
135     static final int ILOAD_0 = 26;
136     static final int ILOAD_1 = 27;
137     static final int ILOAD_2 = 28;
138     static final int ILOAD_3 = 29;
139     static final int LLOAD_0 = 30;
140     static final int LLOAD_1 = 31;
141     static final int LLOAD_2 = 32;
142     static final int LLOAD_3 = 33;
143     static final int FLOAD_0 = 34;
144     static final int FLOAD_1 = 35;
145     static final int FLOAD_2 = 36;
146     static final int FLOAD_3 = 37;
147     static final int DLOAD_0 = 38;
148     static final int DLOAD_1 = 39;
149     static final int DLOAD_2 = 40;
150     static final int DLOAD_3 = 41;
151     static final int ALOAD_0 = 42;
152     static final int ALOAD_1 = 43;
153     static final int ALOAD_2 = 44;
154     static final int ALOAD_3 = 45;
155     static final int ISTORE_0 = 59;
156     static final int ISTORE_1 = 60;
157     static final int ISTORE_2 = 61;
158     static final int ISTORE_3 = 62;
159     static final int LSTORE_0 = 63;
160     static final int LSTORE_1 = 64;
161     static final int LSTORE_2 = 65;
162     static final int LSTORE_3 = 66;
163     static final int FSTORE_0 = 67;
164     static final int FSTORE_1 = 68;
165     static final int FSTORE_2 = 69;
166     static final int FSTORE_3 = 70;
167     static final int DSTORE_0 = 71;
168     static final int DSTORE_1 = 72;
169     static final int DSTORE_2 = 73;
170     static final int DSTORE_3 = 74;
171     static final int ASTORE_0 = 75;
172     static final int ASTORE_1 = 76;
173     static final int ASTORE_2 = 77;
174     static final int ASTORE_3 = 78;
175     static final int WIDE = 196;
176     static final int GOTO_W = 200;
177     static final int JSR_W = 201;
178 
179     // Constants to convert between normal and wide jump instructions.
180 
181     // The delta between the GOTO_W and JSR_W opcodes and GOTO and JUMP.
182     static final int WIDE_JUMP_OPCODE_DELTA = GOTO_W - Opcodes.GOTO;
183 
184     // Constants to convert JVM opcodes to the equivalent ASM specific opcodes, and vice versa.
185 
186     // The delta between the ASM_IFEQ, ..., ASM_IF_ACMPNE, ASM_GOTO and ASM_JSR opcodes
187     // and IFEQ, ..., IF_ACMPNE, GOTO and JSR.
188     static final int ASM_OPCODE_DELTA = 49;
189 
190     // The delta between the ASM_IFNULL and ASM_IFNONNULL opcodes and IFNULL and IFNONNULL.
191     static final int ASM_IFNULL_OPCODE_DELTA = 20;
192 
193     // ASM specific opcodes, used for long forward jump instructions.
194 
195     static final int ASM_IFEQ = Opcodes.IFEQ + ASM_OPCODE_DELTA;
196     static final int ASM_IFNE = Opcodes.IFNE + ASM_OPCODE_DELTA;
197     static final int ASM_IFLT = Opcodes.IFLT + ASM_OPCODE_DELTA;
198     static final int ASM_IFGE = Opcodes.IFGE + ASM_OPCODE_DELTA;
199     static final int ASM_IFGT = Opcodes.IFGT + ASM_OPCODE_DELTA;
200     static final int ASM_IFLE = Opcodes.IFLE + ASM_OPCODE_DELTA;
201     static final int ASM_IF_ICMPEQ = Opcodes.IF_ICMPEQ + ASM_OPCODE_DELTA;
202     static final int ASM_IF_ICMPNE = Opcodes.IF_ICMPNE + ASM_OPCODE_DELTA;
203     static final int ASM_IF_ICMPLT = Opcodes.IF_ICMPLT + ASM_OPCODE_DELTA;
204     static final int ASM_IF_ICMPGE = Opcodes.IF_ICMPGE + ASM_OPCODE_DELTA;
205     static final int ASM_IF_ICMPGT = Opcodes.IF_ICMPGT + ASM_OPCODE_DELTA;
206     static final int ASM_IF_ICMPLE = Opcodes.IF_ICMPLE + ASM_OPCODE_DELTA;
207     static final int ASM_IF_ACMPEQ = Opcodes.IF_ACMPEQ + ASM_OPCODE_DELTA;
208     static final int ASM_IF_ACMPNE = Opcodes.IF_ACMPNE + ASM_OPCODE_DELTA;
209     static final int ASM_GOTO = Opcodes.GOTO + ASM_OPCODE_DELTA;
210     static final int ASM_JSR = Opcodes.JSR + ASM_OPCODE_DELTA;
211     static final int ASM_IFNULL = Opcodes.IFNULL + ASM_IFNULL_OPCODE_DELTA;
212     static final int ASM_IFNONNULL = Opcodes.IFNONNULL + ASM_IFNULL_OPCODE_DELTA;
213     static final int ASM_GOTO_W = 220;
214 
215     private Constants() {}
216 
217     static void checkAsmExperimental(final Object caller) {
218         Class<?> callerClass = caller.getClass();
219         String internalName = callerClass.getName().replace('.', '/');
220         if (!isWhitelisted(internalName)) {
221             checkIsPreview(callerClass.getClassLoader().getResourceAsStream(internalName + ".class"));
222         }
223     }
224 
225     static boolean isWhitelisted(final String internalName) {
226         if (!internalName.startsWith("jdk/internal/org/objectweb/asm/")) {
227             return false;
228         }
229         String member = "(Annotation|Class|Field|Method|Module|RecordComponent|Signature)";
230         return internalName.contains("Test$")
231                 || Pattern.matches(
232                         "jdk/internal/org/objectweb/asm/util/Trace" + member + "Visitor(\\$.*)?", internalName)
233                 || Pattern.matches(
234                         "jdk/internal/org/objectweb/asm/util/Check" + member + "Adapter(\\$.*)?", internalName);
235     }
236 
237     static void checkIsPreview(final InputStream classInputStream) {
238         if (classInputStream == null) {
239             throw new IllegalStateException("Bytecode not available, can't check class version");
240         }
241         int minorVersion;
242         try (DataInputStream callerClassStream = new DataInputStream(classInputStream); ) {
243             callerClassStream.readInt();
244             minorVersion = callerClassStream.readUnsignedShort();
245         } catch (IOException ioe) {
246             throw new IllegalStateException("I/O error, can't check class version", ioe);
247         }
248         if (minorVersion != 0xFFFF) {
249             throw new IllegalStateException(
250                     "ASM9_EXPERIMENTAL can only be used by classes compiled with --enable-preview");
251         }
252     }
253 }
254