bfs又写错了,感觉把dfs中的visited,所有容易带偏

//error
 Queue<SootMethod> sootMethodQueue = new LinkedList<>();
        sootMethodQueue.offer(entryPoint);


        int count = 0;
        //不需要使用isChanged, dummymain可以到达所有组件,这些组件包含了app的所有代码

        Set<SootMethod> visited = new HashSet<>();
        while (!sootMethodQueue.isEmpty()) {

            SootMethod firstSootMethod = sootMethodQueue.poll();
            visited.add(firstSootMethod);//---------------------------error 
            

            if (!firstSootMethod.getDeclaringClass().getName().startsWith("android.")) {//不分析android开头的
                if (Util.isApplicationMethod(firstSootMethod)) {

                    if (firstSootMethod.hasActiveBody()) {
                        Body body = firstSootMethod.getActiveBody();
                        List<UnitPair> insertUnitList = new ArrayList<>();
                        for (Unit eachUnit : body.getUnits()) {
                            Stmt iccStmt = (Stmt) eachUnit;
                            SootMethod calleeSootMethod = Util.getCalleeSootMethodAt(eachUnit);
                            if (calleeSootMethod != null) {
                                if (IntentImplicitUse.iccMethods.contains(calleeSootMethod.getName())) {
                                    InvokeExpr invokeExpr = iccStmt.getInvokeExpr();
                                    for (Value valueIntentArg : invokeExpr.getArgs()) {
                                        if (valueIntentArg.getType().toString().equals("android.content.Intent")) {
                                            count++;
                                            long beforeGetTargetComponent = System.nanoTime();
                                            TargetComponent targetComponent = intentImplicitUse.doAnalysisIntentGetTargetComponent(valueIntentArg, iccStmt, iccStmt, firstSootMethod);
                                            long afterGetTargetComponent = System.nanoTime();
                                            appLogger.info("getGetTargetComponent时间:" + ((afterGetTargetComponent - beforeGetTargetComponent) / 1E9) + " seconds");
                                            if (targetComponent != null && targetComponent.isHasInfo()) {

                                                addTargetComponent(intentImplicitUse, insertUnitList, iccStmt, targetComponent);


                                            }

                                        }
                                    }
                                }
                            }


                        }

                        Set<UnitPair> unitPairSet=new HashSet<>(insertUnitList);
                        if(unitPairSet.size()!=insertUnitList.size())
                        {
                            System.out.println("");
                        }
                        for (UnitPair unitPair : insertUnitList) {
                            body.getUnits().insertAfter(unitPair.insertUnit, unitPair.point);
                            body.validate();

                            appLogger.info(firstSootMethod + "的" + unitPair.point + "解析成功!" + unitPair.className);
                        }
                    }


                }
                for (Iterator<Edge> edgeIterator = cg.edgesOutOf(firstSootMethod); edgeIterator.hasNext(); ) {
                    Edge outEdge = edgeIterator.next();
                    SootMethod outSootMethod = outEdge.tgt();
                    if (!visited.contains(outSootMethod)) {
                        
                        sootMethodQueue.offer(outSootMethod);
                    }

                }
            }


        }

例如:
1(2,3,4)
2(3,4)
1
2 3 4
3 4 3 4
出现访问重复
换成算法导论上的。

Map<SootMethod,String> sootMethodColor = new HashMap<>();//存储节点颜色
        Queue<SootMethod> sootMethodQueue = new LinkedList<>();
        sootMethodColor.put(entryPoint,"gray");//节点颜色为null的表示还未发现,为gray表示在队列里,节点为黑色,表示所有孩子都被发现了
        sootMethodQueue.offer(entryPoint);
        int count = 0;
        //不需要使用isChanged, dummymain可以到达所有组件,这些组件包含了app的所有代码

        while (!sootMethodQueue.isEmpty()) {

            SootMethod firstSootMethod = sootMethodQueue.poll();

            if (!firstSootMethod.getDeclaringClass().getName().startsWith("android.")) {//不分析android开头的
                if (Util.isApplicationMethod(firstSootMethod)) {

                    if (firstSootMethod.hasActiveBody()) {
                        Body body = firstSootMethod.getActiveBody();
                        List<UnitPair> insertUnitList = new ArrayList<>();
                        for (Unit eachUnit : body.getUnits()) {
                            Stmt iccStmt = (Stmt) eachUnit;
                            SootMethod calleeSootMethod = Util.getCalleeSootMethodAt(eachUnit);
                            if (calleeSootMethod != null) {
                                if (IntentImplicitUse.iccMethods.contains(calleeSootMethod.getName())) {
                                    InvokeExpr invokeExpr = iccStmt.getInvokeExpr();
                                    for (Value valueIntentArg : invokeExpr.getArgs()) {
                                        if (valueIntentArg.getType().toString().equals("android.content.Intent")) {
                                            count++;
                                            long beforeGetTargetComponent = System.nanoTime();
                                            TargetComponent targetComponent = intentImplicitUse.doAnalysisIntentGetTargetComponent(valueIntentArg, iccStmt, iccStmt, firstSootMethod);
                                            long afterGetTargetComponent = System.nanoTime();
                                            appLogger.info("getGetTargetComponent时间:" + ((afterGetTargetComponent - beforeGetTargetComponent) / 1E9) + " seconds");
                                            if (targetComponent != null && targetComponent.isHasInfo()) {

                                                addTargetComponent(intentImplicitUse, insertUnitList, iccStmt, targetComponent);


                                            }

                                        }
                                    }
                                }
                            }


                        }


                        for (UnitPair unitPair : insertUnitList) {
                            body.getUnits().insertAfter(unitPair.insertUnit, unitPair.point);
                            body.validate();

                            appLogger.info(firstSootMethod + "的" + unitPair.point + "解析成功!" + unitPair.className);
                        }
                    }


                }
                for (Iterator<Edge> edgeIterator = cg.edgesOutOf(firstSootMethod); edgeIterator.hasNext(); ) {
                    Edge outEdge = edgeIterator.next();
                    SootMethod outSootMethod = outEdge.tgt();
                    if (sootMethodColor.get(outSootMethod)==null) {

                        sootMethodColor.put(outSootMethod,"gray");

                        sootMethodQueue.offer(outSootMethod);
                    }

                }

                sootMethodColor.put(firstSootMethod,"black");
            }


        }
发布了184 篇原创文章 · 获赞 19 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/zhoumingsong123/article/details/85177670
今日推荐