Verifier.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.verifier;

  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;

  22. import org.apache.bcel.classfile.JavaClass;
  23. import org.apache.bcel.classfile.Utility;
  24. import org.apache.bcel.verifier.statics.Pass1Verifier;
  25. import org.apache.bcel.verifier.statics.Pass2Verifier;
  26. import org.apache.bcel.verifier.statics.Pass3aVerifier;
  27. import org.apache.bcel.verifier.structurals.Pass3bVerifier;
  28. import org.apache.commons.lang3.ArrayUtils;

  29. /**
  30.  * A Verifier instance is there to verify a class file according to The Java Virtual Machine Specification, 2nd Edition.
  31.  *
  32.  * Pass-3b-verification includes pass-3a-verification; pass-3a-verification includes pass-2-verification;
  33.  * pass-2-verification includes pass-1-verification.
  34.  *
  35.  * A Verifier creates PassVerifier instances to perform the actual verification. Verifier instances are usually
  36.  * generated by the VerifierFactory.
  37.  *
  38.  * @see VerifierFactory
  39.  * @see PassVerifier
  40.  */
  41. public class Verifier {

  42.     static final String NAME = "Apache Commons BCEL";
  43.     static final String BANNER = NAME + "\nhttps://commons.apache.org/bcel\n";

  44.     static final Verifier[] EMPTY_ARRAY = {};

  45.     /**
  46.      * Verifies class files. This is a simple demonstration of how the API of BCEL's class file verifier "JustIce" may be
  47.      * used. You should supply command-line arguments which are fully qualified namea of the classes to verify. These class
  48.      * files must be somewhere in your CLASSPATH (refer to Sun's documentation for questions about this) or you must have
  49.      * put the classes into the BCEL Repository yourself (via 'addClass(JavaClass)').
  50.      */
  51.     public static void main(final String[] args) {
  52.         System.out.println(BANNER);
  53.         for (int index = 0; index < args.length; index++) {
  54.             try {
  55.                 if (args[index].endsWith(JavaClass.EXTENSION)) {
  56.                     final int dotclasspos = args[index].lastIndexOf(JavaClass.EXTENSION);
  57.                     if (dotclasspos != -1) {
  58.                         args[index] = args[index].substring(0, dotclasspos);
  59.                     }
  60.                 }
  61.                 args[index] = Utility.pathToPackage(args[index]);
  62.                 System.out.println("Now verifying: " + args[index] + "\n");
  63.                 verifyType(args[index]);
  64.                 org.apache.bcel.Repository.clearCache();
  65.                 System.gc();
  66.             } catch (final ClassNotFoundException e) {
  67.                 e.printStackTrace();
  68.             }
  69.         }
  70.     }

  71.     static void verifyType(final String fullyQualifiedClassName) throws ClassNotFoundException {
  72.         final Verifier verifier = VerifierFactory.getVerifier(fullyQualifiedClassName);
  73.         VerificationResult verificationResult;
  74.         verificationResult = verifier.doPass1();
  75.         System.out.println("Pass 1:\n" + verificationResult);
  76.         verificationResult = verifier.doPass2();
  77.         System.out.println("Pass 2:\n" + verificationResult);
  78.         if (verificationResult == VerificationResult.VR_OK) {
  79.             final JavaClass jc = org.apache.bcel.Repository.lookupClass(fullyQualifiedClassName);
  80.             for (int i = 0; i < jc.getMethods().length; i++) {
  81.                 verificationResult = verifier.doPass3a(i);
  82.                 System.out.println("Pass 3a, method number " + i + " ['" + jc.getMethods()[i] + "']:\n" + verificationResult);
  83.                 verificationResult = verifier.doPass3b(i);
  84.                 System.out.println("Pass 3b, method number " + i + " ['" + jc.getMethods()[i] + "']:\n" + verificationResult);
  85.             }
  86.         }
  87.         System.out.println("Warnings:");
  88.         final String[] warnings = verifier.getMessages();
  89.         if (warnings.length == 0) {
  90.             System.out.println("<none>");
  91.         }
  92.         for (final String warning : warnings) {
  93.             System.out.println(warning);
  94.         }
  95.         System.out.println("\n");
  96.         // avoid swapping.
  97.         verifier.flush();
  98.     }

  99.     /**
  100.      * The name of the class this verifier operates on.
  101.      */
  102.     private final String className;

  103.     /** A Pass1Verifier for this Verifier instance. */
  104.     private Pass1Verifier p1v;

  105.     /** A Pass2Verifier for this Verifier instance. */
  106.     private Pass2Verifier p2v;

  107.     /** The Pass3aVerifiers for this Verifier instance. Key: Interned string specifying the method number. */
  108.     private final Map<String, Pass3aVerifier> p3avs = new HashMap<>();

  109.     /** The Pass3bVerifiers for this Verifier instance. Key: Interned string specifying the method number. */
  110.     private final Map<String, Pass3bVerifier> p3bvs = new HashMap<>();

  111.     /**
  112.      * Instantiation is done by the VerifierFactory.
  113.      *
  114.      * @see VerifierFactory
  115.      */
  116.     Verifier(final String fullyQualifiedClassName) {
  117.         className = fullyQualifiedClassName;
  118.     }

  119.     /** Returns the VerificationResult for the given pass. */
  120.     public VerificationResult doPass1() {
  121.         if (p1v == null) {
  122.             p1v = new Pass1Verifier(this);
  123.         }
  124.         return p1v.verify();
  125.     }

  126.     /** Returns the VerificationResult for the given pass. */
  127.     public VerificationResult doPass2() {
  128.         if (p2v == null) {
  129.             p2v = new Pass2Verifier(this);
  130.         }
  131.         return p2v.verify();
  132.     }

  133.     /**
  134.      * Returns the VerificationResult for the given pass.
  135.      *
  136.      * @param methodNo The method to verify
  137.      * @return the VerificationResult
  138.      */
  139.     public VerificationResult doPass3a(final int methodNo) {
  140.         return p3avs.computeIfAbsent(Integer.toString(methodNo), k -> new Pass3aVerifier(this, methodNo)).verify();
  141.     }

  142.     /**
  143.      * Returns the VerificationResult for the given pass.
  144.      *
  145.      * @param methodNo The method to verify
  146.      * @return the VerificationResult
  147.      */
  148.     public VerificationResult doPass3b(final int methodNo) {
  149.         return p3bvs.computeIfAbsent(Integer.toString(methodNo), k -> new Pass3bVerifier(this, methodNo)).verify();
  150.     }

  151.     /**
  152.      * Forget everything known about the class file; that means, really start a new verification of a possibly different
  153.      * class file from BCEL's repository.
  154.      */
  155.     public void flush() {
  156.         p1v = null;
  157.         p2v = null;
  158.         p3avs.clear();
  159.         p3bvs.clear();
  160.     }

  161.     /**
  162.      * Returns the name of the class this verifier operates on. This is particularly interesting when this verifier was
  163.      * created recursively by another Verifier and you got a reference to this Verifier by the getVerifiers() method of the
  164.      * VerifierFactory.
  165.      *
  166.      * @see VerifierFactory
  167.      */
  168.     public final String getClassName() {
  169.         return className;
  170.     }

  171.     /**
  172.      * This returns all the (warning) messages collected during verification. A prefix shows from which verifying pass a
  173.      * message originates.
  174.      *
  175.      * @throws ClassNotFoundException if this class can't be found.
  176.      */
  177.     public String[] getMessages() throws ClassNotFoundException {
  178.         final List<String> messages = new ArrayList<>();
  179.         if (p1v != null) {
  180.             p1v.getMessagesList().forEach(element -> messages.add("Pass 1: " + element));
  181.         }
  182.         if (p2v != null) {
  183.             p2v.getMessagesList().forEach(element -> messages.add("Pass 2: " + element));
  184.         }
  185.         for (final Pass3aVerifier pv : p3avs.values()) {
  186.             final int meth = pv.getMethodNo();
  187.             for (final String element : pv.getMessages()) {
  188.                 messages.add("Pass 3a, method " + meth + " ('" + org.apache.bcel.Repository.lookupClass(className).getMethods()[meth] + "'): " + element);
  189.             }
  190.         }
  191.         for (final Pass3bVerifier pv : p3bvs.values()) {
  192.             final int meth = pv.getMethodNo();
  193.             for (final String element : pv.getMessages()) {
  194.                 messages.add("Pass 3b, method " + meth + " ('" + org.apache.bcel.Repository.lookupClass(className).getMethods()[meth] + "'): " + element);
  195.             }
  196.         }

  197.         return messages.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
  198.     }
  199. }