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.util;
60 
61 import java.util.HashSet;
62 import jdk.internal.org.objectweb.asm.ModuleVisitor;
63 import jdk.internal.org.objectweb.asm.Opcodes;
64 
65 /**
66  * A {@link ModuleVisitor} that checks that its methods are properly used.
67  *
68  * @author Remi Forax
69  */
70 public class CheckModuleAdapter extends ModuleVisitor {
71     /** Whether the visited module is open. */
72     private final boolean isOpen;
73 
74     /** The fully qualified names of the dependencies of the visited module. */
75     private final NameSet requiredModules = new NameSet("Modules requires");
76 
77     /** The internal names of the packages exported by the visited module. */
78     private final NameSet exportedPackages = new NameSet("Module exports");
79 
80     /** The internal names of the packages opened by the visited module. */
81     private final NameSet openedPackages = new NameSet("Module opens");
82 
83     /** The internal names of the services used by the visited module. */
84     private final NameSet usedServices = new NameSet("Module uses");
85 
86     /** The internal names of the services provided by the visited module. */
87     private final NameSet providedServices = new NameSet("Module provides");
88 
89     /** The class version number. */
90     int classVersion;
91 
92     /** Whether the {@link #visitEnd} method has been called. */
93     private boolean visitEndCalled;
94 
95     /**
96       * Constructs a new {@link CheckModuleAdapter}. <i>Subclasses must not use this constructor</i>.
97       * Instead, they must use the {@link #CheckModuleAdapter(int, ModuleVisitor, boolean)} version.
98       *
99       * @param moduleVisitor the module visitor to which this adapter must delegate calls.
100       * @param isOpen whether the visited module is open. Open modules have their {@link
101       *     Opcodes#ACC_OPEN} access flag set in {@link jdk.internal.org.objectweb.asm.ClassVisitor#visitModule}.
102       * @throws IllegalStateException If a subclass calls this constructor.
103       */
CheckModuleAdapter(final ModuleVisitor moduleVisitor, final boolean isOpen)104     public CheckModuleAdapter(final ModuleVisitor moduleVisitor, final boolean isOpen) {
105         this(/* latest api = */ Opcodes.ASM8, moduleVisitor, isOpen);
106         if (getClass() != CheckModuleAdapter.class) {
107             throw new IllegalStateException();
108         }
109     }
110 
111     /**
112       * Constructs a new {@link CheckModuleAdapter}.
113       *
114       * @param api the ASM API version implemented by this visitor. Must be one of {@link
115       *     Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6}, {@link Opcodes#ASM7} or {@link
116       *     Opcodes#ASM8}.
117       * @param moduleVisitor the module visitor to which this adapter must delegate calls.
118       * @param isOpen whether the visited module is open. Open modules have their {@link
119       *     Opcodes#ACC_OPEN} access flag set in {@link jdk.internal.org.objectweb.asm.ClassVisitor#visitModule}.
120       */
CheckModuleAdapter( final int api, final ModuleVisitor moduleVisitor, final boolean isOpen)121     protected CheckModuleAdapter(
122             final int api, final ModuleVisitor moduleVisitor, final boolean isOpen) {
123         super(api, moduleVisitor);
124         this.isOpen = isOpen;
125     }
126 
127     @Override
visitMainClass(final String mainClass)128     public void visitMainClass(final String mainClass) {
129         // Modules can only appear in V9 or more classes.
130         CheckMethodAdapter.checkInternalName(Opcodes.V9, mainClass, "module main class");
131         super.visitMainClass(mainClass);
132     }
133 
134     @Override
visitPackage(final String packaze)135     public void visitPackage(final String packaze) {
136         CheckMethodAdapter.checkInternalName(Opcodes.V9, packaze, "module package");
137         super.visitPackage(packaze);
138     }
139 
140     @Override
visitRequire(final String module, final int access, final String version)141     public void visitRequire(final String module, final int access, final String version) {
142         checkVisitEndNotCalled();
143         CheckClassAdapter.checkFullyQualifiedName(Opcodes.V9, module, "required module");
144         requiredModules.checkNameNotAlreadyDeclared(module);
145         CheckClassAdapter.checkAccess(
146                 access,
147                 Opcodes.ACC_STATIC_PHASE
148                         | Opcodes.ACC_TRANSITIVE
149                         | Opcodes.ACC_SYNTHETIC
150                         | Opcodes.ACC_MANDATED);
151         if (classVersion >= Opcodes.V10
152                 && module.equals("java.base")
153                 && (access & (Opcodes.ACC_STATIC_PHASE | Opcodes.ACC_TRANSITIVE)) != 0) {
154             throw new IllegalArgumentException(
155                     "Invalid access flags: "
156                             + access
157                             + " java.base can not be declared ACC_TRANSITIVE or ACC_STATIC_PHASE");
158         }
159         super.visitRequire(module, access, version);
160     }
161 
162     @Override
visitExport(final String packaze, final int access, final String... modules)163     public void visitExport(final String packaze, final int access, final String... modules) {
164         checkVisitEndNotCalled();
165         CheckMethodAdapter.checkInternalName(Opcodes.V9, packaze, "package name");
166         exportedPackages.checkNameNotAlreadyDeclared(packaze);
167         CheckClassAdapter.checkAccess(access, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_MANDATED);
168         if (modules != null) {
169             for (String module : modules) {
170                 CheckClassAdapter.checkFullyQualifiedName(Opcodes.V9, module, "module export to");
171             }
172         }
173         super.visitExport(packaze, access, modules);
174     }
175 
176     @Override
visitOpen(final String packaze, final int access, final String... modules)177     public void visitOpen(final String packaze, final int access, final String... modules) {
178         checkVisitEndNotCalled();
179         if (isOpen) {
180             throw new UnsupportedOperationException("An open module can not use open directive");
181         }
182         CheckMethodAdapter.checkInternalName(Opcodes.V9, packaze, "package name");
183         openedPackages.checkNameNotAlreadyDeclared(packaze);
184         CheckClassAdapter.checkAccess(access, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_MANDATED);
185         if (modules != null) {
186             for (String module : modules) {
187                 CheckClassAdapter.checkFullyQualifiedName(Opcodes.V9, module, "module open to");
188             }
189         }
190         super.visitOpen(packaze, access, modules);
191     }
192 
193     @Override
visitUse(final String service)194     public void visitUse(final String service) {
195         checkVisitEndNotCalled();
196         CheckMethodAdapter.checkInternalName(Opcodes.V9, service, "service");
197         usedServices.checkNameNotAlreadyDeclared(service);
198         super.visitUse(service);
199     }
200 
201     @Override
visitProvide(final String service, final String... providers)202     public void visitProvide(final String service, final String... providers) {
203         checkVisitEndNotCalled();
204         CheckMethodAdapter.checkInternalName(Opcodes.V9, service, "service");
205         providedServices.checkNameNotAlreadyDeclared(service);
206         if (providers == null || providers.length == 0) {
207             throw new IllegalArgumentException("Providers cannot be null or empty");
208         }
209         for (String provider : providers) {
210             CheckMethodAdapter.checkInternalName(Opcodes.V9, provider, "provider");
211         }
212         super.visitProvide(service, providers);
213     }
214 
215     @Override
visitEnd()216     public void visitEnd() {
217         checkVisitEndNotCalled();
218         visitEndCalled = true;
219         super.visitEnd();
220     }
221 
checkVisitEndNotCalled()222     private void checkVisitEndNotCalled() {
223         if (visitEndCalled) {
224             throw new IllegalStateException("Cannot call a visit method after visitEnd has been called");
225         }
226     }
227 
228     private static class NameSet {
229 
230         private final String type;
231         private final HashSet<String> names;
232 
NameSet(final String type)233         NameSet(final String type) {
234             this.type = type;
235             this.names = new HashSet<>();
236         }
237 
checkNameNotAlreadyDeclared(final String name)238         void checkNameNotAlreadyDeclared(final String name) {
239             if (!names.add(name)) {
240                 throw new IllegalArgumentException(type + " '" + name + "' already declared");
241             }
242         }
243     }
244 }
245