void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception {
   ClassFile classFile = ClassFile.read(cfile);
   boolean methodFound = false;
   for (Method method : classFile.methods) {
     if (method.getName(classFile.constant_pool).equals(methodToFind)) {
       methodFound = true;
       Code_attribute code = (Code_attribute) method.attributes.get("Code");
       LineNumberTable_attribute lnt =
           (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
       Assert.check(
           lnt.line_number_table_length == expectedLNT.length,
           "The LineNumberTable found has a length different to the expected one");
       int i = 0;
       for (LineNumberTable_attribute.Entry entry : lnt.line_number_table) {
         Assert.check(
             entry.line_number == expectedLNT[i][0] && entry.start_pc == expectedLNT[i][1],
             "LNT entry at pos "
                 + i
                 + " differ from expected."
                 + "Found "
                 + entry.line_number
                 + ":"
                 + entry.start_pc
                 + ". Expected "
                 + expectedLNT[i][0]
                 + ":"
                 + expectedLNT[i][1]);
         i++;
       }
     }
   }
   Assert.check(methodFound, "The seek method was not found");
 }
Example #2
0
 String getName(Method m) {
   try {
     return m.getName(constant_pool);
   } catch (ConstantPoolException e) {
     return report(e);
   }
 }
 void checkClassFile(final Path path) throws Exception {
   ClassFile classFile = ClassFile.read(new BufferedInputStream(Files.newInputStream(path)));
   constantPool = classFile.constant_pool;
   utf8Index = constantPool.getUTF8Index("STR_TO_LOOK_FOR");
   for (Method method : classFile.methods) {
     if (method.getName(constantPool).equals("methodToLookFor")) {
       Code_attribute codeAtt = (Code_attribute) method.attributes.get(Attribute.Code);
       for (Instruction inst : codeAtt.getInstructions()) {
         inst.accept(codeVisitor, null);
       }
     }
   }
   Assert.check(
       numberOfRefToStr == 1,
       "There should only be one reference to a CONSTANT_String_info structure in the generated code");
 }
Example #4
0
  /**
   * This constructor takes a ClassFile and the MethodInfo structure representing the method and
   * produces an M6Method object.
   *
   * @param c The ClassFile representing the class
   * @param m The MethodInfo structure representing the method to analyze.
   */
  public M6Method(M6Class cls, Method m) throws ConstantPoolException {
    this.cls = cls;
    cf = cls.cf;
    this.m = m;
    name = m.getName(cf.constant_pool);
    desc = m.descriptor.getValue(cf.constant_pool);
    String[] ss = splitMethodDesc(desc);
    for (int i = 1; i < ss.length; i++) {
      params.add(ss[i]);
    }
    returntype = ss[0];
    cai = (Code_attribute) m.attributes.get(Attribute.Code);
    if (cai != null) {
      max_stack = cai.max_stack;
      max_locals = cai.max_locals;
      code_length = cai.code_length;
      sm = (StackMap_attribute) cai.attributes.get(Attribute.StackMap);
      lnt = (LineNumberTable_attribute) cai.attributes.get(Attribute.LineNumberTable);
      lvt = (LocalVariableTable_attribute) cai.attributes.get(Attribute.LocalVariableTable);
    } else {
      max_stack = max_locals = code_length = 0;
      sm = null;
      lnt = null;
      lvt = null;
    }

    tagTable = new Hashtable();
    //        debug = true;
    processed = false;
  }
  void verifyBytecode(VarargsMethod selected, JavaSource source, int id) {
    bytecodeCheckCount.incrementAndGet();
    File compiledTest = new File(String.format("Test%d.class", id));
    try {
      ClassFile cf = ClassFile.read(compiledTest);
      Method testMethod = null;
      for (Method m : cf.methods) {
        if (m.getName(cf.constant_pool).equals("test")) {
          testMethod = m;
          break;
        }
      }
      if (testMethod == null) {
        throw new Error("Test method not found");
      }
      Code_attribute ea = (Code_attribute) testMethod.attributes.get(Attribute.Code);
      if (testMethod == null) {
        throw new Error("Code attribute for test() method not found");
      }

      for (Instruction i : ea.getInstructions()) {
        if (i.getMnemonic().equals("invokevirtual")) {
          int cp_entry = i.getUnsignedShort(1);
          CONSTANT_Methodref_info methRef =
              (CONSTANT_Methodref_info) cf.constant_pool.get(cp_entry);
          String type = methRef.getNameAndTypeInfo().getType();
          String sig = selected.parameterTypes.bytecodeSigStr;
          if (!type.contains(sig)) {
            throw new Error(
                "Unexpected type method call: "
                    + type
                    + ""
                    + "\nfound: "
                    + sig
                    + "\n"
                    + source.getCharContent(true));
          }
          break;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw new Error("error reading " + compiledTest + ": " + e);
    }
  }
Example #6
0
  void verifyBytecode(VarargsMethod selected) {
    bytecodeCheckCount++;
    File compiledTest = new File("Test.class");
    try {
      ClassFile cf = ClassFile.read(compiledTest);
      Method testMethod = null;
      for (Method m : cf.methods) {
        if (m.getName(cf.constant_pool).equals("test")) {
          testMethod = m;
          break;
        }
      }
      if (testMethod == null) {
        throw new Error("Test method not found");
      }
      Code_attribute ea = (Code_attribute) testMethod.attributes.get(Attribute.Code);
      if (testMethod == null) {
        throw new Error("Code attribute for test() method not found");
      }

      for (Instruction i : ea.getInstructions()) {
        if (i.getMnemonic().equals("invokevirtual")) {
          int cp_entry = i.getUnsignedShort(1);
          CONSTANT_Methodref_info methRef =
              (CONSTANT_Methodref_info) cf.constant_pool.get(cp_entry);
          String type = methRef.getNameAndTypeInfo().getType();
          if (!type.contains(selected.varargsElement.bytecodeString)) {
            throw new Error("Unexpected type method call: " + type);
          }
          break;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw new Error("error reading " + compiledTest + ": " + e);
    }
  }
Example #7
0
  void verifyBytecode(JavaSource source, int id) {
    boolean lastInlined = false;
    boolean hasCode = false;
    int gapsCount = 0;
    for (int i = 0; i < stmts.length; i++) {
      lastInlined = stmts[i].canInline;
      hasCode = hasCode || !stmts[i].empty;
      if (lastInlined && hasCode) {
        hasCode = false;
        gapsCount++;
      }
    }
    if (!lastInlined) {
      gapsCount++;
    }

    File compiledTest = new File(String.format("Test%s.class", id));
    try {
      ClassFile cf = ClassFile.read(compiledTest);
      if (cf == null) {
        throw new Error("Classfile not found: " + compiledTest.getName());
      }

      Method test_method = null;
      for (Method m : cf.methods) {
        if (m.getName(cf.constant_pool).equals("test")) {
          test_method = m;
          break;
        }
      }

      if (test_method == null) {
        throw new Error("Method test() not found in class Test");
      }

      Code_attribute code = null;
      for (Attribute a : test_method.attributes) {
        if (a.getName(cf.constant_pool).equals(Attribute.Code)) {
          code = (Code_attribute) a;
          break;
        }
      }

      if (code == null) {
        throw new Error("Code attribute not found in method test()");
      }

      int actualGapsCount = 0;
      for (int i = 0; i < code.exception_table_langth; i++) {
        int catchType = code.exception_table[i].catch_type;
        if (catchType == 0) { // any
          actualGapsCount++;
        }
      }

      if (actualGapsCount != gapsCount) {
        throw new Error(
            "Bad exception table for test()\n"
                + "expected gaps: "
                + gapsCount
                + "\n"
                + "found gaps: "
                + actualGapsCount
                + "\n"
                + source);
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw new Error("error reading " + compiledTest + ": " + e);
    }
  }