Ejemplo n.º 1
0
    public void visitJavaClass(JavaClass clazz) {

      super.visitJavaClass(clazz);
      JopClassInfo cli = (JopClassInfo) this.cli;

      cpool = clazz.getConstantPool();

      if (clazz.isInterface()) {
        cli.interfaceID = ++cli.interfaceCnt;
        cli.interfaceList.add(cli.interfaceID - 1, clazz.getClassName());
      }
    }
Ejemplo n.º 2
0
 private boolean implementsInterface(String ifname) {
   OldClassInfo cli = this;
   do {
     String[] interfaces = cli.clazz.getInterfaceNames();
     for (int i = 0; i < interfaces.length; i++) {
       if (ifname.equals(interfaces[i])) {
         return true;
       } else {
         // an interface may have a super-interface
         JopClassInfo superCli = (JopClassInfo) appInfo.cliMap.get(interfaces[i]);
         boolean match = superCli.implementsInterface(ifname);
         if (match) {
           return true;
         }
       }
     }
     cli = (OldClassInfo) cli.superClass;
   } while (cli != null);
   return false;
 }
Ejemplo n.º 3
0
  /** @param cp */
  public void resolveCPool(ConstantPool cp) {

    Constant[] ca = cp.getConstantPool();
    cpoolArry = new int[cpoolUsed.size()];
    cpoolComments = new String[ca.length];

    // System.out.println(clazz.getClassName()+" cpool "+cpoolUsed);

    for (int i = 0; i < ca.length; ++i) {
      Constant co = ca[i];
      Integer idx = new Integer(i);
      // pos is the new position in the reduced constant pool
      // idx is the position in the 'original' unresolved cpool
      int pos = cpoolUsed.indexOf(idx);
      if (pos != -1) {
        boolean isInterface = false;
        // System.out.println("cpool@"+pos+" = orig_cp@"+i+" "+co);
        switch (co.getTag()) {
          case Constants.CONSTANT_Integer:
            cpoolArry[pos] = ((ConstantInteger) co).getBytes();
            cpoolComments[pos] = "Integer";
            break;
          case Constants.CONSTANT_Long:
            long lval = ((ConstantLong) co).getBytes();
            // store LOW, HIGH words in this order
            int loW = (new Long(0xFFFFFFFF & lval)).intValue();
            int hiW = (new Long(lval >>> 32)).intValue();
            cpoolArry[pos] = hiW;
            cpoolArry[pos + 1] = loW;
            cpoolComments[pos] = "Long: " + lval;
            cpoolComments[pos + 1] = "";
            break;
          case Constants.CONSTANT_Float:
            float fval = ((ConstantFloat) co).getBytes();
            cpoolArry[pos] = Float.floatToRawIntBits(fval);
            cpoolComments[pos] = "Float: " + fval;
            break;
          case Constants.CONSTANT_Double:
            double dval = ((ConstantDouble) co).getBytes();
            long d_lval = Double.doubleToRawLongBits(dval);
            // store LOW, HIGH words in this order
            int d_loW = (new Long(0xFFFFFFFF & d_lval)).intValue();
            int d_hiW = (new Long(d_lval >>> 32)).intValue();
            cpoolArry[pos] = d_hiW;
            cpoolArry[pos + 1] = d_loW;
            cpoolComments[pos] = "Double: " + dval;
            cpoolComments[pos + 1] = "";
            break;
          case Constants.CONSTANT_String:
            String str = ((ConstantString) co).getBytes(cp);
            StringInfo si = StringInfo.getStringInfo(str);
            cpoolArry[pos] = StringInfo.stringTableAddress + si.getAddress();
            cpoolComments[pos] = "String: " + si.getSaveString();
            break;
          case Constants.CONSTANT_Class:
            String clname = ((ConstantClass) co).getBytes(cp).replace('/', '.');
            JopClassInfo clinfo = (JopClassInfo) appInfo.cliMap.get(clname);
            if (clinfo == null) {
              cpoolComments[pos] = "Problem with class: " + clname;
              String type = clname.substring(clname.length() - 2);
              if (type.charAt(0) == '[') {
                switch (type.charAt(1)) {
                  case 'Z':
                    cpoolArry[pos] = 4;
                    break;
                  case 'C':
                    cpoolArry[pos] = 5;
                    break;
                  case 'F':
                    cpoolArry[pos] = 6;
                    break;
                  case 'D':
                    cpoolArry[pos] = 7;
                    break;
                  case 'B':
                    cpoolArry[pos] = 8;
                    break;
                  case 'S':
                    cpoolArry[pos] = 9;
                    break;
                  case 'I':
                    cpoolArry[pos] = 10;
                    break;
                  case 'J':
                    cpoolArry[pos] = 11;
                    break;
                  default:; // all other types are missing...
                }
              }
              // System.out.println(cpoolComments[pos]+" "+type+" "+cpoolArry[pos]);
              continue;
            }
            cpoolArry[pos] = clinfo.classRefAddress;
            cpoolComments[pos] = "Class: " + clname;
            break;
          case Constants.CONSTANT_InterfaceMethodref:
            isInterface = true;
          case Constants.CONSTANT_Methodref:
            // find the class for this method
            int mclidx;
            if (isInterface) {
              mclidx = ((ConstantInterfaceMethodref) co).getClassIndex();
            } else {
              mclidx = ((ConstantMethodref) co).getClassIndex();
            }
            ConstantClass mcl = (ConstantClass) cp.getConstant(mclidx);
            // the method has "/" instead of ".", fix that
            // now get the signature too...
            String mclname = mcl.getBytes(cp).replace('/', '.');
            int sigidx;
            if (isInterface) {
              sigidx = ((ConstantInterfaceMethodref) co).getNameAndTypeIndex();
            } else {
              sigidx = ((ConstantMethodref) co).getNameAndTypeIndex();
            }
            ConstantNameAndType signt = (ConstantNameAndType) cp.getConstant(sigidx);
            String sigstr = signt.getName(cp) + signt.getSignature(cp);
            // now find the address of the method struct!
            JopClassInfo clinf = (JopClassInfo) appInfo.cliMap.get(mclname);
            if (clinf == null) {
              // probably a reference to Native - a class that
              // is NOT present in the application.
              // we could avoid this by not adding method refs to
              // Native in our reduced cpool.
              cpoolArry[pos] = 0;
              cpoolComments[pos] = "static " + mclname + "." + sigstr;
              break;
            }
            JopMethodInfo minf;
            if (isInterface) {
              minf = clinf.getITMethodInfo(sigstr);
            } else {
              minf = clinf.getVTMethodInfo(sigstr);
            }
            if (minf == null) {
              System.out.println(
                  "Error: Method " + clinf.clazz.getClassName() + '.' + sigstr + " not found.");
              System.out.println("Invoked by " + clazz.getClassName());
              for (int xxx = 0; xxx < clinf.clvt.len; ++xxx) {
                System.out.println(clinf.clvt.key[xxx]);
              }
              System.exit(1);
            }
            if (minf.getMethod().isStatic()
                ||
                // <init> and privat methods are called with invokespecial
                // which mapps in jvm.asm to invokestatic
                minf.getMethod().isPrivate()
                || sigstr.charAt(0) == '<') {
              // for static methods a direct pointer to the
              // method struct
              cpoolArry[pos] = minf.structAddress;
              cpoolComments[pos] =
                  "static, special or private " + clinf.clazz.getClassName() + "." + minf.methodId;
            } else {
              // as Flavius correctly comments:
              // TODO: CHANGE THIS TO A MORE CONSISTENT FORMAT...
              // extract the objref! for some reason the microcode
              // needs -1 here...weird

              // that's for simple virtual methods
              int vpos = minf.vtindex;
              String comment = "virtual";

              // TODO: is kind of redundant search as we've already
              // searched the IT table with getVTMethodInfo()
              // TODO: do we handle different interfaces with same
              // method id correct? (see buildIT)
              if (isInterface) {
                comment = "interface";
                for (int j = 0; j < listIT.size(); ++j) {
                  IT it = (IT) listIT.get(j);
                  if (it.key.equals(minf.methodId)) {
                    vpos = j;
                    break;
                  }
                }
                // offest in interface table
                // index plus number of arguments (without this!)
                cpoolArry[pos] = (vpos << 8) + (minf.margs - 1);
              } else {
                // offest in method table
                // (index*2) plus number of arguments (without
                // this!)
                cpoolArry[pos] = (vpos * ClassStructConstants.METH_STR << 8) + (minf.margs - 1);
              }
              cpoolComments[pos] =
                  comment
                      + " index: "
                      + vpos
                      + " args: "
                      + minf.margs
                      + " "
                      + clinf.clazz.getClassName()
                      + "."
                      + minf.methodId;
            }
            break;
          case Constants.CONSTANT_Fieldref:
            throw new Error("Fieldref should not be used anymore");
            //					int fidx = ((ConstantFieldref) co).getClassIndex();
            //					ConstantClass fcl = (ConstantClass) cp.getConstant(fidx);
            //					String fclname = fcl.getBytes(cp).replace('/', '.');
            //					// got the class name
            //					sigidx = ((ConstantFieldref) co).getNameAndTypeIndex();
            //					signt = (ConstantNameAndType) cp.getConstant(sigidx);
            //					sigstr = signt.getName(cp) + signt.getSignature(cp);
            //					clinf = (JopClassInfo) appInfo.cliMap.get(fclname);
            //					int j;
            //					String comment = "";
            //					boolean found = false;
            //					while (!found) {
            //						for (j = 0; j < clinf.clft.len; ++j) {
            //							if (clinf.clft.key[j].equals(sigstr)) {
            //								found = true;
            //								if (clinf.clft.isStatic[j]) {
            //									comment = "static ";
            //								}
            //								// for static fields a direct pointer to the
            //								// static field
            //								cpoolArry[pos] = clinf.clft.idx[j];
            //								cpoolComments[pos] = comment
            //										+ clinf.clazz.getClassName() + "."
            //										+ sigstr;
            //								break;
            //							}
            //						}
            //						if (!found) {
            //							clinf = (JopClassInfo) clinf.superClass;
            //							if (clinf == null) {
            //								System.out.println("Error: field " + fclname
            //										+ "." + sigstr + " not found!");
            //								break;
            //							}
            //						}
            //					}
            //					break;
          default:
            System.out.println("TODO: cpool@" + pos + " = orig_cp@" + i + " " + co);
            cpoolComments[pos] = "Problem with: " + co;
        }
      }
    }
  }