Package soot

Examples of soot.Hierarchy


        // FIXME: This would be nice to do by inlining instead of
        // special casing.
        ModelTransformer.implementExecutableInterface(entityInstanceClass);

        // Reinitialize the hierarchy, since we've added classes.
        Scene.v().setActiveHierarchy(new Hierarchy());
        Scene.v().setFastHierarchy(new FastHierarchy());

        // Inline all methods in the class that are called from
        // within the class.
        ModelTransformer.inlineLocalCalls(entityInstanceClass);
View Full Code Here


    private void _replaceComplexParametersIn(SootClass modelClass,
            CompositeActor model) {
        copyAttributesOtherThanVariable(model);

        // Reinitialize the hierarchy, since we've added classes.
        Scene.v().setActiveHierarchy(new Hierarchy());
        Scene.v().setFastHierarchy(new FastHierarchy());
    }
View Full Code Here

                }
            }
        }

        // Reset the hierarchy, since we've changed superclasses and such.
        Scene.v().setActiveHierarchy(new Hierarchy());
        Scene.v().setFastHierarchy(new FastHierarchy());

        // Fix the specialInvokes.
        for (Iterator i = Scene.v().getApplicationClasses().iterator(); i
                .hasNext();) {
View Full Code Here

                        if (debug) {
                            System.out.println("value = " + value);
                        }

                        Scene.v().setActiveHierarchy(new Hierarchy());
                        Scene.v().setFastHierarchy(new FastHierarchy());

                        boolean flag = _inlineTokenMethodsIn(method, body,
                                stmt, box, localDefs, localUses, typeAnalysis,
                                depth, unsafeLocalSet, debug);
View Full Code Here

    public boolean _inlineTokenMethodsIn(SootMethod method, JimpleBody body,
            Unit unit, ValueBox box, SimpleLocalDefs localDefs,
            SimpleLocalUses localUses, TypeSpecializerAnalysis typeAnalysis,
            int depth, Set unsafeLocalSet, boolean debug) {
        Hierarchy hierarchy = Scene.v().getActiveHierarchy();

        boolean doneSomething = false;
        Value value = box.getValue();

        // If we have a call to evaluateParseTree, then we can't
        // inline any of those locals.  This sucks, but I can't think
        // of a better way to statically determine the value of the
        // token.  In some cases, this doesn't work because things are
        // inlined on an unsafe local before we know it is unsafe!
        if (value instanceof InvokeExpr) {
            InvokeExpr r = (InvokeExpr) value;

            if (unit instanceof AssignStmt
                    && r.getMethod().getName().equals("evaluateParseTree")) {
                AssignStmt stmt = (AssignStmt) unit;
                unsafeLocalSet.add(stmt.getLeftOp());
                unsafeLocalSet.addAll(_computeTokenLocalsDefinedFrom(localUses,
                        stmt));

                if (debug) {
                    System.out.println("unsafeLocalSet = " + unsafeLocalSet);
                }
            }
        }

        if (value instanceof VirtualInvokeExpr
                || value instanceof InterfaceInvokeExpr
                || value instanceof SpecialInvokeExpr) {
            InstanceInvokeExpr r = (InstanceInvokeExpr) value;

            Local local = (Local) r.getBase();
            Type baseType = local.getType();

            if (baseType instanceof NullType) {
                // replace with NullPointerException..
                if (debug) {
                    System.out.println("Invoke base is null, replacing with "
                            + "NullPointerException");
                }

                Local exception = SootUtilities.createRuntimeException(body,
                        unit, "NullPointerException");
                body.getUnits().insertBefore(
                        Jimple.v().newThrowStmt(exception), unit);
                body.getUnits().remove(unit);
            }

            boolean isInlineableTokenMethod = _isLocalTokenTypeWithDepth(local,
                    typeAnalysis, unsafeLocalSet, depth, debug);

            if (debug) {
                System.out.println("checking inline for " + r);
            }

            // Check if token arguments are being used.  This makes
            // sure we get methods like Scale._scaleOnRight and
            // DB._doFunction inlined.  It would be better if the
            // token inliner modified the functions, but it doesn't.
            if (baseType instanceof RefType
                    && Scene.v().getApplicationClasses().contains(
                            ((RefType) baseType).getSootClass())) {
                Type returnType = r.getMethod().getReturnType();
                isInlineableTokenMethod |= _isInlineableTokenType(returnType);

                for (Iterator args = r.getArgs().iterator(); args.hasNext()
                        && !isInlineableTokenMethod;) {
                    Object arg = args.next();

                    if (arg instanceof Local) {
                        Local argLocal = (Local) arg;

                        if (debug) {
                            System.out.println("argtype = "
                                    + argLocal.getType());
                        }

                        isInlineableTokenMethod = _isLocalTokenTypeWithDepth(
                                argLocal, typeAnalysis, unsafeLocalSet, depth,
                                debug);

                        if (debug) {
                            System.out.println("isInlineableTokenMethod = "
                                    + isInlineableTokenMethod);
                        }
                    }
                }
            }

            if (!isInlineableTokenMethod) {
                return false;
            }

            // System.out.println("baseType = " + baseType);
            RefType type = (RefType) typeAnalysis.getSpecializedSootType(local);

            // System.out.println("specializedType = " + type);
            // Then determine the method that was
            // actually invoked.
            List methodList;

            if (value instanceof SpecialInvokeExpr) {
                SootMethod targetMethod = hierarchy.resolveSpecialDispatch(
                        (SpecialInvokeExpr) r, method);
                methodList = new LinkedList();
                methodList.add(targetMethod);
            } else {
                methodList =
                //      invokeGraph.getTargetsOf((Stmt)unit);
                hierarchy.resolveAbstractDispatch(type.getSootClass(), r
                        .getMethod());
            }

            // If there was only one possible method...
            if (methodList.size() == 1) {
                // Then inline its code
                SootMethod inlinee = (SootMethod) methodList.get(0);

                if (inlinee.getName().equals("getClass")) {
                    SootClass typeClass = type.getSootClass();
                    int subclasses = hierarchy.getSubclassesOf(typeClass)
                            .size();

                    if (subclasses == 0) {
                        // FIXME: do something better here.
                        SootMethod newGetClassMethod = Scene.v().getMethod(
                                "<java.lang.Class: java.lang.Class "
                                        + "forName(java.lang.String)>");
                        box.setValue(Jimple.v().newStaticInvokeExpr(
                                newGetClassMethod.makeRef(),
                                StringConstant.v(typeClass.getName())));
                        doneSomething = true;
                    }
                } else if (inlinee.getName().equals("isNil")) {
                    box.setValue(IntConstant.v(0));
                    //                 } else if (inlinee.getSignature().equals(PtolemyUtilities.arrayTokenConstructor.getSignature())) {
                    //                     System.out.println("method = " + method);
                    //                     System.out.println("inlinee = " + inlinee.getSignature());
                    //                     throw new RuntimeException("Cannot inline arrayTokens that do not have a type explicitly specified.");
                } else {
                    SootClass declaringClass = inlinee.getDeclaringClass();

                    if (!declaringClass.isApplicationClass()) {
                        declaringClass.setLibraryClass();
                    }

                    if (!inlinee.isAbstract() && !inlinee.isNative()) {
                        // FIXME: only inline things where we are
                        // also inlining the constructor???
                        if (debug) {
                            System.out.println("inlining " + inlinee);
                        }

                        inlinee.retrieveActiveBody();

                        // Then we know exactly what method will
                        // be called, so inline it.
                        SiteInliner.inlineSite(inlinee, (Stmt) unit, method);
                        doneSomething = true;
                    } else {
                        throw new RuntimeException("inlinee is not concrete!: "
                                + inlinee);
                    }
                }
            } else {
                if (debug) {
                    System.out.println("uninlinable method invocation = " + r);

                    for (Iterator j = methodList.iterator(); j.hasNext();) {
                        System.out.println("method = " + j.next());
                    }
                }
            }
        } else if (value instanceof SpecialInvokeExpr) {
            SpecialInvokeExpr r = (SpecialInvokeExpr) value;

            if (debug) {
                System.out.println("special invoking = " + r.getMethod());
            }

            Type baseType = typeAnalysis.getSpecializedSootType((Local) r
                    .getBase());

            if (baseType instanceof RefType) {
                RefType type = (RefType) baseType;

                boolean isInlineableTokenMethod = SootUtilities.derivesFrom(
                        type.getSootClass(), PtolemyUtilities.tokenClass);

                // If it is a token, then check to
                // make sure that it has the
                // appropriate type
                if (isInlineableTokenMethod) {
                    type = (RefType) typeAnalysis
                            .getSpecializedSootType((Local) r.getBase());

                    if (PtolemyUtilities.getTypeDepth(typeAnalysis
                            .getSpecializedType((Local) r.getBase())) != depth) {
                        if (debug) {
                            System.out
                                    .println("skipping, type depth = "
                                            + PtolemyUtilities
                                                    .getTypeDepth(typeAnalysis
                                                            .getSpecializedType((Local) r
                                                                    .getBase()))
                                            + ", but only inlining depth "
                                            + depth);
                        }

                        return false;

                        //continue;
                    }
                }

                if (isInlineableTokenMethod) {
                    SootMethod inlinee = hierarchy.resolveSpecialDispatch(r,
                            method);
                    SootClass declaringClass = inlinee.getDeclaringClass();

                    if (!declaringClass.isApplicationClass()) {
                        declaringClass.setLibraryClass();
View Full Code Here

    public boolean _inlineTypeMethodsIn(SootMethod method, JimpleBody body,
            Unit unit, ValueBox box, SimpleLocalDefs localDefs,
            SimpleLocalUses localUses, TypeSpecializerAnalysis typeAnalysis,
            int depth, Set unsafeLocalSet, boolean debug) {
        boolean doneSomething = false;
        Hierarchy hierarchy = Scene.v().getActiveHierarchy();
        Value value = box.getValue();

        if (value instanceof VirtualInvokeExpr
                || value instanceof InterfaceInvokeExpr) {
            InstanceInvokeExpr r = (InstanceInvokeExpr) value;

            Type baseType = r.getBase().getType();

            // If we are invoking a method on a Token or Type, and
            // the token is not unsafe.
            if (baseType instanceof RefType
                    && !unsafeLocalSet.contains(r.getBase())) {
                RefType type = (RefType) baseType;

                boolean isInlineableTypeMethod = SootUtilities.derivesFrom(type
                        .getSootClass(), PtolemyUtilities.typeClass)
                        && (r.getMethod().getName().equals("convert")
                                || r.getMethod().getName().equals("equals")
                                || r.getMethod().getName().equals(
                                        "getTokenClass") || r.getMethod()
                                .getName().equals("getElementType"));

                if (debug && isInlineableTypeMethod) {
                    System.out.println("Inlineable type method = "
                            + r.getMethod());
                }

                if (isInlineableTypeMethod) {
                    ptolemy.data.type.Type baseTokenType = typeAnalysis
                            .getSpecializedType((Local) r.getBase());
                    SootClass typeClass = Scene.v().loadClassAndSupport(
                            baseTokenType.getClass().getName());

                    // Only instantiable types can be inlined..
                    if (!baseTokenType.isInstantiable()) {
                        return doneSomething;
                    }

                    // Then determine the method that was
                    // actually invoked.
                    List methodList = hierarchy.resolveAbstractDispatch(
                            typeClass, r.getMethod());

                    // If there was only one possible method...
                    if (methodList.size() == 1) {
                        // Then inline its code
View Full Code Here

        Iterator classes = classSet.iterator();

        while (classes.hasNext()) {
            SootClass source = (SootClass) classes.next();
            Iterator methods = methodSet.iterator();
            Hierarchy hierarchy = new Hierarchy();

            // Candidates for I. All superclasses and all implemented
            // interfaces.
            Collection allParents = AnalysisUtilities
                    .getAllInterfacesOf(source);

            if (!source.isInterface()) {
                allParents.addAll(hierarchy.getSuperclassesOf(source));
            }

            while (methods.hasNext()) {
                SootMethod method = (SootMethod) methods.next();
                String subSignature = method.getSubSignature();
View Full Code Here

        }
    }

    private void _processClass(SootClass theClass) {
        //  System.out.println("processing class " + theClass);
        Hierarchy hierarchy = Scene.v().getActiveHierarchy();

        // Make the method bodies analyzeable.
        if (!theClass.isApplicationClass()) {
            theClass.setLibraryClass();
        }

        _addClasses(hierarchy.getSuperclassesOfIncluding(theClass));

        // FIXME: what about super interfaces
        _addClasses(theClass.getInterfaces());

        // Grab the types of all fields.
View Full Code Here

        eliminateCastsAndInstanceOf(body, phaseName, new HashSet(), debug);
    }

    public static void eliminateCastsAndInstanceOf(Body body, String phaseName,
            Set unsafeLocalSet, boolean debug) {
        Scene.v().setActiveHierarchy(new Hierarchy());
        Scene.v().setFastHierarchy(new FastHierarchy());

        for (Iterator units = body.getUnits().iterator(); units.hasNext();) {
            Unit unit = (Unit) units.next();

            for (Iterator boxes = unit.getUseBoxes().iterator(); boxes
                    .hasNext();) {
                ValueBox box = (ValueBox) boxes.next();
                Value value = box.getValue();

                // This assumes that the types of local
                // variables are verifiable types.
                // This is not ensured by Soot, unless
                // you run the TypeAssigner before this
                // transformation.
                if (value instanceof CastExpr) {
                    // If the cast is to the same type as the
                    // operand already is, then replace with
                    // simple assignment.
                    CastExpr expr = (CastExpr) value;
                    Type castType = expr.getCastType();
                    Value op = expr.getOp();
                    Type opType = op.getType();

                    //                     // Skip locals that are unsafe.
                    //                     if (castType.equals(opType) &&
                    //                             !unsafeLocalSet.contains(op)) {
                    //                         box.setValue(op);
                    //                     }
                    if (unsafeLocalSet.contains(op)) {
                        continue;
                    }

                    Hierarchy hierarchy = Scene.v().getActiveHierarchy();

                    //   if (debug) System.out.println("checking cast in " + unit);
                    //                     if (debug) System.out.println("op = " + op);
                    //                     if (debug) System.out.println("opType = " + opType);
                    replaceCast(box, hierarchy, castType, op, opType, debug);
                } else if (value instanceof InstanceOfExpr) {
                    // If the operand of the expression is
                    // declared to be of a type that implies
                    // the instanceof is true, then replace
                    // with true.
                    InstanceOfExpr expr = (InstanceOfExpr) value;
                    Type checkType = expr.getCheckType();
                    Value op = expr.getOp();
                    Type opType = op.getType();

                    // Skip locals that are unsafe.
                    if (unsafeLocalSet.contains(op)) {
                        continue;
                    }

                    Hierarchy hierarchy = Scene.v().getActiveHierarchy();

                    if (debug) {
                        System.out.println("checking instanceof in " + unit);
                    }
View Full Code Here

        ReachableMethods reachableMethods = new ReachableMethods(callGraph,
                EntryPoints.v().application());

        reachableMethods.update();

        Hierarchy hierarchy = Scene.v().getActiveHierarchy();

        final Set createableClasses = new HashSet();

        for (Iterator reachables = reachableMethods.listener(); reachables
                .hasNext();) {
            SootMethod method = (SootMethod) reachables.next();
            //String methodName = method.getSignature();

            if (method.getName().equals("<init>")
                    && !method.getDeclaringClass().getName().startsWith("java")) {
                createableClasses
                        .addAll(hierarchy.getSuperclassesOfIncluding(method
                                .getDeclaringClass()));
                _addAllInterfaces(createableClasses, method.getDeclaringClass());
            }
        }

        System.out.println("createableClasses = " + createableClasses);

        // Now create a new set of reachable methods that only
        // includes methods that are static or are declared in classes
        // that can are created.
        Filter filter = new Filter(new EdgePredicate() {
            public boolean want(Edge e) {
                SootMethod target = e.tgt();
                return e.isExplicit()
                        && (target.isStatic() || createableClasses
                                .contains(target.getDeclaringClass()));
            }
        });
        Set necessaryClasses = new HashSet();
        ReachableMethods RTAReachableMethods = new ReachableMethods(callGraph,
                EntryPoints.v().application().iterator(), filter);
        RTAReachableMethods.update();

        List list = new LinkedList();

        for (Iterator reachables = RTAReachableMethods.listener(); reachables
                .hasNext();) {
            SootMethod method = (SootMethod) reachables.next();
            String methodName = method.getSignature();
            list.add(methodName);

            SootClass declaringClass = method.getDeclaringClass();

            if (!declaringClass.getName().startsWith("java")) {
                necessaryClasses.add(declaringClass);
            }

            if (method.isConcrete()) {
                for (Iterator units = method.retrieveActiveBody().getUnits()
                        .iterator(); units.hasNext();) {
                    Unit unit = (Unit) units.next();

                    for (Iterator boxes = unit.getUseBoxes().iterator(); boxes
                            .hasNext();) {
                        ValueBox box = (ValueBox) boxes.next();
                        Value value = box.getValue();

                        if (value instanceof CastExpr) {
                            CastExpr expr = (CastExpr) value;
                            Type castType = expr.getCastType();

                            if (castType instanceof RefType) {
                                SootClass castClass = ((RefType) castType)
                                        .getSootClass();

                                if (castClass.isInterface()) {
                                    necessaryClasses.add(castClass);
                                } else {
                                    necessaryClasses
                                            .addAll(hierarchy
                                                    .getSuperclassesOfIncluding(castClass));
                                }

                                _addAllInterfaces(necessaryClasses, castClass);
                            }
                        } else if (value instanceof InstanceOfExpr) {
                            InstanceOfExpr expr = (InstanceOfExpr) value;
                            Type checkType = expr.getCheckType();

                            if (checkType instanceof RefType) {
                                SootClass checkClass = ((RefType) checkType)
                                        .getSootClass();

                                if (!checkClass.isInterface()) {
                                    necessaryClasses
                                            .addAll(hierarchy
                                                    .getSuperclassesOfIncluding(checkClass));
                                }

                                _addAllInterfaces(necessaryClasses, checkClass);
                            }
View Full Code Here

TOP

Related Classes of soot.Hierarchy

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.