private IMethod makeNullaryValueConstructor(IClass cls, Object value) {
    JSInstructionFactory insts =
        (JSInstructionFactory) cls.getClassLoader().getInstructionFactory();
    MethodReference ref = JavaScriptMethods.makeCtorReference(cls.getReference());
    JavaScriptSummary S = new JavaScriptSummary(ref, 1);

    S.addStatement(insts.GetInstruction(4, 1, "prototype"));
    S.getNextProgramCounter();

    S.addStatement(
        insts.NewInstruction(
            5, NewSiteReference.make(S.getNextProgramCounter(), cls.getReference())));

    S.addStatement(insts.SetPrototype(5, 4));
    // S.addStatement(insts.PutInstruction(5, 4, "__proto__"));
    S.getNextProgramCounter();

    S.addConstant(new Integer(8), new ConstantValue(value));
    S.addStatement(insts.PutInstruction(5, 8, "$value"));
    S.getNextProgramCounter();

    S.addStatement(insts.ReturnInstruction(5, false));
    S.getNextProgramCounter();

    // S.addConstant(9, new ConstantValue("__proto__"));

    return new JavaScriptConstructor(ref, S, cls);
  }
  private IMethod makeArrayLengthConstructor(IClass cls) {
    JSInstructionFactory insts =
        (JSInstructionFactory) cls.getClassLoader().getInstructionFactory();
    MethodReference ref = JavaScriptMethods.makeCtorReference(JavaScriptTypes.Array);
    JavaScriptSummary S = new JavaScriptSummary(ref, 2);

    S.addStatement(insts.GetInstruction(5, 1, "prototype"));
    S.getNextProgramCounter();

    S.addStatement(
        insts.NewInstruction(
            6, NewSiteReference.make(S.getNextProgramCounter(), JavaScriptTypes.Array)));

    S.addStatement(insts.SetPrototype(6, 5));
    // S.addStatement(insts.PutInstruction(6, 5, "__proto__"));
    S.getNextProgramCounter();

    S.addStatement(insts.PutInstruction(6, 2, "length"));
    S.getNextProgramCounter();

    S.addStatement(insts.ReturnInstruction(6, false));
    S.getNextProgramCounter();

    // S.addConstant(7, new ConstantValue("__proto__"));

    return new JavaScriptConstructor(ref, S, cls);
  }
  private IMethod makeFunctionObjectConstructor(IClass cls, int nargs) {
    JSInstructionFactory insts =
        (JSInstructionFactory) cls.getClassLoader().getInstructionFactory();
    Object key = Pair.make(cls, new Integer(nargs));
    if (constructors.containsKey(key)) return constructors.get(key);

    MethodReference ref = JavaScriptMethods.makeCtorReference(cls.getReference());
    JavaScriptSummary S = new JavaScriptSummary(ref, nargs + 1);
    S.addStatement(insts.GetInstruction(nargs + 4, 1, "prototype"));
    S.getNextProgramCounter();

    S.addStatement(
        insts.NewInstruction(
            nargs + 5, NewSiteReference.make(S.getNextProgramCounter(), JavaScriptTypes.Object)));

    S.addStatement(insts.SetPrototype(nargs + 5, nargs + 4));
    // S.addStatement(insts.PutInstruction(nargs + 5, nargs + 4, "__proto__"));
    S.getNextProgramCounter();

    CallSiteReference cs = new JSCallSiteReference(S.getNextProgramCounter());
    int[] args = new int[nargs + 1];
    args[0] = nargs + 5;
    for (int i = 0; i < nargs; i++) args[i + 1] = i + 2;
    S.addStatement(insts.Invoke(1, nargs + 7, args, nargs + 8, cs));

    S.addStatement(insts.ReturnInstruction(nargs + 7, false));
    S.getNextProgramCounter();

    S.addStatement(insts.ReturnInstruction(nargs + 5, false));
    S.getNextProgramCounter();

    // S.addConstant(nargs + 9, new ConstantValue("__proto__"));

    return record(key, new JavaScriptConstructor(ref, S, cls));
  }
  private IMethod makeUnaryObjectConstructor(IClass cls) {
    JSInstructionFactory insts =
        (JSInstructionFactory) cls.getClassLoader().getInstructionFactory();
    MethodReference ref = JavaScriptMethods.makeCtorReference(JavaScriptTypes.Object);
    JavaScriptSummary S = new JavaScriptSummary(ref, 2);

    S.addStatement(insts.ReturnInstruction(2, false));
    S.getNextProgramCounter();

    return new JavaScriptConstructor(ref, S, cls);
  }
  private IMethod makeNullaryNumberCall(IClass cls) {
    JSInstructionFactory insts =
        (JSInstructionFactory) cls.getClassLoader().getInstructionFactory();
    MethodReference ref = AstMethodReference.fnReference(JavaScriptTypes.Number);
    JavaScriptSummary S = new JavaScriptSummary(ref, 1);

    S.addConstant(new Integer(2), new ConstantValue(0.0));
    S.addStatement(insts.ReturnInstruction(2, false));
    S.getNextProgramCounter();

    return new JavaScriptConstructor(ref, S, cls);
  }
  private IMethod makeUnaryNumberCall(IClass cls) {
    JSInstructionFactory insts =
        (JSInstructionFactory) cls.getClassLoader().getInstructionFactory();
    MethodReference ref = AstMethodReference.fnReference(JavaScriptTypes.Number);
    JavaScriptSummary S = new JavaScriptSummary(ref, 2);

    S.addStatement(insts.GetInstruction(4, 2, "toNumber"));
    S.getNextProgramCounter();

    CallSiteReference cs = new JSCallSiteReference(S.getNextProgramCounter());
    S.addStatement(insts.Invoke(4, 5, new int[] {2}, 6, cs));

    S.addStatement(insts.ReturnInstruction(5, false));
    S.getNextProgramCounter();

    return new JavaScriptConstructor(ref, S, cls);
  }
  private IMethod makeFunctionConstructor(IClass receiver, IClass cls) {
    JSInstructionFactory insts =
        (JSInstructionFactory) cls.getClassLoader().getInstructionFactory();
    Pair<IClass, IClass> tableKey = Pair.make(receiver, cls);
    if (constructors.containsKey(tableKey)) return constructors.get(tableKey);

    MethodReference ref = JavaScriptMethods.makeCtorReference(receiver.getReference());
    JavaScriptSummary S = new JavaScriptSummary(ref, 1);

    S.addStatement(insts.GetInstruction(4, 1, "prototype"));
    S.getNextProgramCounter();

    S.addStatement(
        insts.NewInstruction(
            5, NewSiteReference.make(S.getNextProgramCounter(), cls.getReference())));

    S.addStatement(
        insts.NewInstruction(
            7, NewSiteReference.make(S.getNextProgramCounter(), JavaScriptTypes.Object)));

    S.addStatement(insts.SetPrototype(5, 4));
    // S.addStatement(insts.PutInstruction(5, 4, "__proto__"));
    S.getNextProgramCounter();

    S.addStatement(insts.PutInstruction(5, 7, "prototype"));
    S.getNextProgramCounter();

    S.addStatement(insts.PutInstruction(7, 5, "constructor"));
    S.getNextProgramCounter();

    // TODO we need to set v7.__proto__ to Object.prototype
    S.addStatement(insts.ReturnInstruction(5, false));
    S.getNextProgramCounter();

    // S.addConstant(8, new ConstantValue("__proto__"));

    if (receiver != cls)
      return record(
          tableKey,
          new JavaScriptConstructor(ref, S, receiver, "(" + cls.getReference().getName() + ")"));
    else return record(tableKey, new JavaScriptConstructor(ref, S, receiver));
  }
  private IMethod makeArrayContentsConstructor(IClass cls, int nargs) {
    JSInstructionFactory insts =
        (JSInstructionFactory) cls.getClassLoader().getInstructionFactory();
    MethodReference ref = JavaScriptMethods.makeCtorReference(JavaScriptTypes.Array);
    JavaScriptSummary S = new JavaScriptSummary(ref, nargs + 1);

    S.addConstant(new Integer(nargs + 3), new ConstantValue("prototype"));
    S.addStatement(insts.PropertyRead(nargs + 4, 1, nargs + 3));
    S.getNextProgramCounter();

    S.addStatement(
        insts.NewInstruction(
            nargs + 5, NewSiteReference.make(S.getNextProgramCounter(), JavaScriptTypes.Array)));

    S.addStatement(insts.SetPrototype(nargs + 5, nargs + 4));
    // S.addStatement(insts.PutInstruction(nargs + 5, nargs + 4, "__proto__"));
    S.getNextProgramCounter();

    S.addConstant(new Integer(nargs + 7), new ConstantValue(nargs));
    S.addStatement(insts.PutInstruction(nargs + 5, nargs + 7, "length"));
    S.getNextProgramCounter();

    int vn = nargs + 9;
    for (int i = 0; i < nargs; i++, vn += 2) {
      S.addConstant(new Integer(vn), new ConstantValue(i));
      S.addStatement(insts.PropertyWrite(nargs + 5, vn, i + 1));
      S.getNextProgramCounter();
    }

    S.addStatement(insts.ReturnInstruction(5, false));
    S.getNextProgramCounter();

    // S.addConstant(vn, new ConstantValue("__proto__"));

    return new JavaScriptConstructor(ref, S, cls);
  }
Exemple #9
0
  private static SDGNode convertNode(SDGBuilder sdg, PDGNode node) {
    Operation op = null;
    Kind kind = null;
    int[] allocNodes = null;

    switch (node.getKind()) {
      case ACTUAL_IN:
        op = Operation.ACTUAL_IN;
        kind = Kind.ACTUAL_IN;
        break;
      case ACTUAL_OUT:
        op = Operation.ACTUAL_OUT;
        kind = Kind.ACTUAL_OUT;
        break;
      case CALL:
        op = Operation.CALL;
        kind = Kind.CALL;
        if (sdg.cfg.computeInterference) {
          TIntSet allocNodesAsSet = sdg.getAllocationNodes(node);
          if (allocNodesAsSet != null) {
            allocNodes = allocNodesAsSet.toArray();
          }
        }
        break;
      case ENTRY:
        op = Operation.ENTRY;
        kind = Kind.ENTRY;
        break;
      case EXIT:
        op = Operation.EXIT;
        kind = Kind.EXIT;
        break;
      case EXPRESSION:
        op = Operation.ASSIGN;
        kind = Kind.EXPRESSION;
        break;
      case FOLDED:
        op = Operation.COMPOUND;
        kind = Kind.FOLDED;
        break;
      case FORMAL_IN:
        op = Operation.FORMAL_IN;
        kind = Kind.FORMAL_IN;
        break;
      case FORMAL_OUT:
        op = Operation.FORMAL_OUT;
        kind = Kind.FORMAL_OUT;
        break;
      case HREAD:
        op = Operation.REFERENCE;
        kind = Kind.EXPRESSION;
        break;
      case HWRITE:
        op = Operation.MODIFY;
        kind = Kind.EXPRESSION;
        break;
      case JOIN:
        op = Operation.COMPOUND;
        kind = Kind.JOIN;
        break;
      case NEW:
        op = Operation.DECLARATION;
        kind = Kind.NORMAL;
        break;
      case NORMAL:
        op = Operation.COMPOUND;
        kind = Kind.NORMAL;
        break;
      case PHI:
        op = Operation.ASSIGN;
        kind = Kind.EXPRESSION;
        break;
      case PREDICATE:
        op = Operation.IF;
        kind = Kind.PREDICATE;
        break;
      case SYNCHRONIZATION:
        op = Operation.MONITOR;
        kind = Kind.SYNCHRONIZATION;
        break;
      default:
        throw new IllegalStateException("Unknown node kind: " + node.getKind().name());
    }
    SourceLocation sloc = node.getSourceLocation();

    SDGNode sn =
        new SecurityNode(
            node.getId(),
            op,
            node.getLabel(),
            node.getPdgId(),
            node.getType(),
            sloc.getSourceFile(),
            sloc.getStartRow(),
            sloc.getStartColumn(),
            sloc.getEndRow(),
            sloc.getEndColumn(),
            node.getBytecodeName(),
            node.getBytecodeIndex());

    if (node.getKind() == PDGNode.Kind.ENTRY) {
      PDG pdg = sdg.getPDGforId(node.getPdgId());
      IMethod im = pdg.getMethod();

      if (im != null) {
        IClass cls = im.getDeclaringClass();

        if (cls != null) {
          String clsLoader = cls.getClassLoader().toString();
          sn.setClassLoader(clsLoader);
        }
      }
    }

    if (allocNodes != null) {
      sn.setAllocationSites(allocNodes);
    }

    if (node.getAliasDataSources() != null) {
      sn.setAliasDataSources(node.getAliasDataSources());
    }

    assert sn.getKind() == kind;

    return sn;
  }