Example #1
1
  private String parseInst(Instruction inst, int offset, Map<Integer, Integer> labels)
      throws ConstantPoolException {
    StringBuffer tmp = new StringBuffer();
    StringBuilder tmp2 = new StringBuilder();

    ParseVisitor parseVisitor = new ParseVisitorM6(labels, tmp2);
    //        System.out.println(inst.toString());

    tmp.append("(" + offset + " (");
    if (labels.containsKey(inst.getPC())) {
      String label = "TAG_" + labels.get(inst.getPC());
      tagTable.put(label, new Integer(offset));
      tmp2.append(";;at " + label);
    }
    tmp.append(inst.accept(parseVisitor, null)).append("))");
    // round the line length to multiple of 8.
    if (!tmp2.equals("")) {
      int pos = 20;
      while (pos <= tmp.length()) {
        pos += 20;
      }
      int bc = pos - tmp.length();
      for (int j = 0; j < bc; j++) {
        tmp.append(" ");
      }
      tmp.append(tmp2);
    }
    return tmp.toString().trim();
  }
Example #2
0
 @Override
 public String visitConstantPoolRef(Instruction instr, int index, Void p) {
   StringBuilder sb = new StringBuilder();
   sb.append(instr.getMnemonic());
   cpRef(sb, instr.getOpcode(), cls.constantPool[index]);
   return sb.toString();
 }
Example #3
0
 @Override
 public String visitBranch(Instruction instr, int offset, Void p) {
   StringBuilder sb = new StringBuilder();
   return sb.append(instr.getMnemonic())
       .append(' ')
       .append(label(instr.getPC(), offset))
       .toString();
 }
 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");
 }
  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
 @Override
 public String visitTableSwitch(
     Instruction instr, int default_, int low, int high, int[] offsets, Void p) {
   StringBuilder sb = new StringBuilder();
   sb.append(instr.getMnemonic()) // the opcode
       .append(" (tableswitchinfo ")
       .append(label(instr.getPC(), default_)) // the default target
       .append(" (")
       .append(low)
       .append(" . ")
       .append(high)
       .append(") (");
   for (int i = 0; i < high - low + 1; i++) {
     sb.append(label(instr.getPC(), offsets[i])).append(' ');
   }
   sb.setCharAt(sb.length() - 1, ')');
   return sb.append(")").toString();
 }
Example #7
0
 @Override
 public String visitLocalAndValue(Instruction instr, int index, int value, Void p) {
   StringBuilder sb = new StringBuilder();
   return sb.append(instr.getMnemonic())
       .append(' ')
       .append(index)
       .append(' ')
       .append(value)
       .toString();
 }
Example #8
0
 @Override
 public String visitLookupSwitch(
     Instruction instr, int default_, int npairs, int[] matches, int[] offsets, Void p) {
   StringBuilder sb = new StringBuilder();
   sb.append(instr.getMnemonic()) // the opcode
       .append(" (lookupswitchinfo ")
       .append(label(instr.getPC(), default_)) // the default target
       .append(' ')
       .append(npairs) // the pair count
       .append(" (");
   for (int i = 0; i < npairs; i++) {
     sb.append('(')
         .append(matches[i])
         .append(" . ")
         .append(label(instr.getPC(), offsets[i]))
         .append(") ");
   }
   sb.setCharAt(sb.length() - 1, ')');
   return sb.append(")").toString();
 }
Example #9
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 #10
0
  /**
   * This method processes the MethodInfo given in the constructor. It is necessary to call this
   * method before querying this method.
   *
   * @param constant_pool The constant pool element from the M6Class object representing the outer
   *     class from which this method is taken from.
   */
  public void processMethod() throws ConstantPoolException, DescriptorException {
    /* The name of the method */
    if (debug) {
      System.out.println("\nProcessing method: " + name + "Parameters: " + params);
    }
    /* Process the code attribute */
    if (cai != null && !no_method_body) {
      LinkedHashMap<Integer, Integer> labels = new LinkedHashMap<>();
      LabelsVisitor labelsVisitor = new LabelsVisitor();
      for (Instruction inst : cai.getInstructions()) {
        for (int offset : inst.accept(labelsVisitor, null)) {
          int label = inst.getPC() + offset;
          if (!labels.containsKey(label)) {
            labels.put(label, labels.size());
          }
        }
      }
      for (Code_attribute.Exception_data e : cai.exception_table) {
        if (!labels.containsKey(e.start_pc)) {
          labels.put(e.start_pc, labels.size());
        }
        if (!labels.containsKey(e.end_pc)) {
          labels.put(e.end_pc, labels.size());
        }
        if (!labels.containsKey(e.handler_pc)) {
          labels.put(e.handler_pc, labels.size());
        }
      }

      int offset = 0;
      int li = 0; // index in linenumber table
      // get the parsed code part
      for (Instruction instr : cai.getInstructions()) {
        if (lnt != null
            && li < lnt.line_number_table.length
            && lnt.line_number_table[li].start_pc == instr.getPC()) {
          m6instructions.add("; " + "line_number #" + lnt.line_number_table[li].line_number);
          li++;
        }
        String resultStr = parseInst(instr, offset, labels);
        if (resultStr != null) {
          m6instructions.add(resultStr);
          offset = offset + instr.length();
        }
      }

      // resolve any tag to absolute offsets.
      resolveTagInCode();

      // add an end marker to the code.
      m6instructions.add("(endofcode " + code_length + ")");
    }
    processed = true;
  }
Example #11
0
 @Override
 public String visitUnknown(Instruction instr, Void p) {
   return "bytecode " + instr.getOpcode();
 }
Example #12
0
 @Override
 public String visitArrayType(Instruction instr, Instruction.TypeKind tk, Void p) {
   StringBuilder sb = new StringBuilder();
   return sb.append(instr.getMnemonic()).append(' ').append(typeKind(tk)).toString();
 }
Example #13
0
 @Override
 public String visitNoOperands(Instruction instr, Void p) {
   StringBuilder sb = new StringBuilder();
   return sb.append(instr.getMnemonic()).toString();
 }