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 package jdk.internal.org.objectweb.asm;
 60 
 61 import java.io.DataInputStream;
 62 import java.io.IOException;
 63 import java.io.InputStream;
 64 import java.util.regex.Pattern;
 65 
 66 /**
 67  * Defines additional JVM opcodes, access flags and constants which are not part of the ASM public
 68  * API.
 69  *
 70  * @see <a href="https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-6.html">JVMS 6</a>
 71  * @author Eric Bruneton
 72  */
 73 final class Constants {
 74 
 75     // The ClassFile attribute names, in the order they are defined in
 76     // https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-4.html#jvms-4.7-300.
 77 
 78     static final String CONSTANT_VALUE = "ConstantValue";
 79     static final String CODE = "Code";
 80     static final String STACK_MAP_TABLE = "StackMapTable";
 81     static final String EXCEPTIONS = "Exceptions";
 82     static final String INNER_CLASSES = "InnerClasses";
 83     static final String ENCLOSING_METHOD = "EnclosingMethod";
 84     static final String SYNTHETIC = "Synthetic";
 85     static final String SIGNATURE = "Signature";
 86     static final String SOURCE_FILE = "SourceFile";
 87     static final String SOURCE_DEBUG_EXTENSION = "SourceDebugExtension";
 88     static final String LINE_NUMBER_TABLE = "LineNumberTable";
 89     static final String LOCAL_VARIABLE_TABLE = "LocalVariableTable";
 90     static final String LOCAL_VARIABLE_TYPE_TABLE = "LocalVariableTypeTable";
 91     static final String DEPRECATED = "Deprecated";
 92     static final String RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations";
 93     static final String RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations";
 94     static final String RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS = "RuntimeVisibleParameterAnnotations";
 95     static final String RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS =
 96             "RuntimeInvisibleParameterAnnotations";
 97     static final String RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations";
 98     static final String RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations";
 99     static final String ANNOTATION_DEFAULT = "AnnotationDefault";
100     static final String BOOTSTRAP_METHODS = "BootstrapMethods";
101     static final String METHOD_PARAMETERS = "MethodParameters";
102     static final String MODULE = "Module";
103     static final String MODULE_PACKAGES = "ModulePackages";
104     static final String MODULE_MAIN_CLASS = "ModuleMainClass";
105     static final String NEST_HOST = "NestHost";
106     static final String NEST_MEMBERS = "NestMembers";
107     static final String PERMITTED_SUBCLASSES = "PermittedSubclasses";
108     static final String RECORD = "Record";
109 
110     // ASM specific access flags.
111     // WARNING: the 16 least significant bits must NOT be used, to avoid conflicts with standard
112     // access flags, and also to make sure that these flags are automatically filtered out when
113     // written in class files (because access flags are stored using 16 bits only).
114 
115     static final int ACC_CONSTRUCTOR = 0x40000; // method access flag.
116 
117     // ASM specific stack map frame types, used in {@link ClassVisitor#visitFrame}.
118 
119     /**
120       * A frame inserted between already existing frames. This internal stack map frame type (in
121       * addition to the ones declared in {@link Opcodes}) can only be used if the frame content can be
122       * computed from the previous existing frame and from the instructions between this existing frame
123       * and the inserted one, without any knowledge of the type hierarchy. This kind of frame is only
124       * used when an unconditional jump is inserted in a method while expanding an ASM specific
125       * instruction. Keep in sync with Opcodes.java.
126       */
127     static final int F_INSERT = 256;
128 
129     // The JVM opcode values which are not part of the ASM public API.
130     // See https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-6.html.
131 
132     static final int LDC_W = 19;
133     static final int LDC2_W = 20;
134     static final int ILOAD_0 = 26;
135     static final int ILOAD_1 = 27;
136     static final int ILOAD_2 = 28;
137     static final int ILOAD_3 = 29;
138     static final int LLOAD_0 = 30;
139     static final int LLOAD_1 = 31;
140     static final int LLOAD_2 = 32;
141     static final int LLOAD_3 = 33;
142     static final int FLOAD_0 = 34;
143     static final int FLOAD_1 = 35;
144     static final int FLOAD_2 = 36;
145     static final int FLOAD_3 = 37;
146     static final int DLOAD_0 = 38;
147     static final int DLOAD_1 = 39;
148     static final int DLOAD_2 = 40;
149     static final int DLOAD_3 = 41;
150     static final int ALOAD_0 = 42;
151     static final int ALOAD_1 = 43;
152     static final int ALOAD_2 = 44;
153     static final int ALOAD_3 = 45;
154     static final int ISTORE_0 = 59;
155     static final int ISTORE_1 = 60;
156     static final int ISTORE_2 = 61;
157     static final int ISTORE_3 = 62;
158     static final int LSTORE_0 = 63;
159     static final int LSTORE_1 = 64;
160     static final int LSTORE_2 = 65;
161     static final int LSTORE_3 = 66;
162     static final int FSTORE_0 = 67;
163     static final int FSTORE_1 = 68;
164     static final int FSTORE_2 = 69;
165     static final int FSTORE_3 = 70;
166     static final int DSTORE_0 = 71;
167     static final int DSTORE_1 = 72;
168     static final int DSTORE_2 = 73;
169     static final int DSTORE_3 = 74;
170     static final int ASTORE_0 = 75;
171     static final int ASTORE_1 = 76;
172     static final int ASTORE_2 = 77;
173     static final int ASTORE_3 = 78;
174     static final int WIDE = 196;
175     static final int GOTO_W = 200;
176     static final int JSR_W = 201;
177 
178     // Constants to convert between normal and wide jump instructions.
179 
180     // The delta between the GOTO_W and JSR_W opcodes and GOTO and JUMP.
181     static final int WIDE_JUMP_OPCODE_DELTA = GOTO_W - Opcodes.GOTO;
182 
183     // Constants to convert JVM opcodes to the equivalent ASM specific opcodes, and vice versa.
184 
185     // The delta between the ASM_IFEQ, ..., ASM_IF_ACMPNE, ASM_GOTO and ASM_JSR opcodes
186     // and IFEQ, ..., IF_ACMPNE, GOTO and JSR.
187     static final int ASM_OPCODE_DELTA = 49;
188 
189     // The delta between the ASM_IFNULL and ASM_IFNONNULL opcodes and IFNULL and IFNONNULL.
190     static final int ASM_IFNULL_OPCODE_DELTA = 20;
191 
192     // ASM specific opcodes, used for long forward jump instructions.
193 
194     static final int ASM_IFEQ = Opcodes.IFEQ + ASM_OPCODE_DELTA;
195     static final int ASM_IFNE = Opcodes.IFNE + ASM_OPCODE_DELTA;
196     static final int ASM_IFLT = Opcodes.IFLT + ASM_OPCODE_DELTA;
197     static final int ASM_IFGE = Opcodes.IFGE + ASM_OPCODE_DELTA;
198     static final int ASM_IFGT = Opcodes.IFGT + ASM_OPCODE_DELTA;
199     static final int ASM_IFLE = Opcodes.IFLE + ASM_OPCODE_DELTA;
200     static final int ASM_IF_ICMPEQ = Opcodes.IF_ICMPEQ + ASM_OPCODE_DELTA;
201     static final int ASM_IF_ICMPNE = Opcodes.IF_ICMPNE + ASM_OPCODE_DELTA;
202     static final int ASM_IF_ICMPLT = Opcodes.IF_ICMPLT + ASM_OPCODE_DELTA;
203     static final int ASM_IF_ICMPGE = Opcodes.IF_ICMPGE + ASM_OPCODE_DELTA;
204     static final int ASM_IF_ICMPGT = Opcodes.IF_ICMPGT + ASM_OPCODE_DELTA;
205     static final int ASM_IF_ICMPLE = Opcodes.IF_ICMPLE + ASM_OPCODE_DELTA;
206     static final int ASM_IF_ACMPEQ = Opcodes.IF_ACMPEQ + ASM_OPCODE_DELTA;
207     static final int ASM_IF_ACMPNE = Opcodes.IF_ACMPNE + ASM_OPCODE_DELTA;
208     static final int ASM_GOTO = Opcodes.GOTO + ASM_OPCODE_DELTA;
209     static final int ASM_JSR = Opcodes.JSR + ASM_OPCODE_DELTA;
210     static final int ASM_IFNULL = Opcodes.IFNULL + ASM_IFNULL_OPCODE_DELTA;
211     static final int ASM_IFNONNULL = Opcodes.IFNONNULL + ASM_IFNULL_OPCODE_DELTA;
212     static final int ASM_GOTO_W = 220;
213 
214     private Constants() {}
215 
216     static void checkAsmExperimental(final Object caller) {
217         Class<?> callerClass = caller.getClass();
218         String internalName = callerClass.getName().replace('.', '/');
219         if (!isWhitelisted(internalName)) {
220             checkIsPreview(callerClass.getClassLoader().getResourceAsStream(internalName + ".class"));
221         }
222     }
223 
224     static boolean isWhitelisted(final String internalName) {
225         if (!internalName.startsWith("jdk/internal/org/objectweb/asm/")) {
226             return false;
227         }
228         String member = "(Annotation|Class|Field|Method|Module|RecordComponent|Signature)";
229         return internalName.contains("Test$")
230                 || Pattern.matches(
231                         "jdk/internal/org/objectweb/asm/util/Trace" + member + "Visitor(\\$.*)?", internalName)
232                 || Pattern.matches(
233                         "jdk/internal/org/objectweb/asm/util/Check" + member + "Adapter(\\$.*)?", internalName);
234     }
235 
236     static void checkIsPreview(final InputStream classInputStream) {
237         if (classInputStream == null) {
238             throw new IllegalStateException("Bytecode not available, can't check class version");
239         }
240         int minorVersion;
241         try (DataInputStream callerClassStream = new DataInputStream(classInputStream); ) {
242             callerClassStream.readInt();
243             minorVersion = callerClassStream.readUnsignedShort();
244         } catch (IOException ioe) {
245             throw new IllegalStateException("I/O error, can't check class version", ioe);
246         }
247         if (minorVersion != 0xFFFF) {
248             throw new IllegalStateException(
249                     "ASM9_EXPERIMENTAL can only be used by classes compiled with --enable-preview");
250         }
251     }
252 }