Ejemplo n.º 1
0
	void RuleSet:: apply_to_list (const Nodes_t & s) {
		if (s.empty()) return;
		
		for (auto i : s) {
			apply_to (*i);
		}
	}
Ejemplo n.º 2
0
	void RuleSet:: apply_to_children (const Selection & s) {
		if (s.nodes.empty()) return;
		
		for (auto i : s.nodes) {
			for (auto j : i->children()) {
				apply_to (*j);
			}
		}
	}
Ejemplo n.º 3
0
	void RuleSet:: apply_to_children (const Node& n) {
		for (auto j : n.children()) {
			apply_to (*j);
		}
	}
Ejemplo n.º 4
0
	void RuleSet:: apply_to_selection (const Selection & s) {
		if (s.nodes.empty()) return;
		
		for (auto i : s.nodes) { apply_to (*i); }
	}
Ejemplo n.º 5
0
        /*! 
            @brief This function determines if a ctree item is legitimate; it
                   marks variables legitimate via the afVariableIsLegit array
                   and saves legitimate items in the vectorLegitItems vector

            @param[in] pItem The visited ctree item
            @return Returns 0 to continue the traversal, returns 1 to stop
                    the traversal
        */
        int
        visit_item (
            citem_t* pItem
            )
        {
            cexpr_t* pExpression;
            char szType[16];

            //
            // Ensure that we're initialized
            //
            if (!fInitialized)
            {
                if (!Initialize())
                {
                    return 1;
                }
            }

            //
            // If we're traversing the graph solely to mark descendants as
            // legitimate...
            //
            if (fMarkingDescendantsLegit)
            {
                //
                // Don't descend through items through which we've already
                //   descended
                //
                if (vectorDescendantsMarkedLegit.has(pItem))
                {
                    return 0;
                }

                //
                // If this is a variable, mark the variable legitimate
                //
                if (pItem->op == cot_var)
                {
                    afVariableIsLegit[((cexpr_t*)pItem)->v.idx] = true;
                }

                //
                // Mark the item itself legitimate
                //
                if (!vectorLegitItems.has(pItem))
                {
                    vectorLegitItems.push_back(
                        pItem);
                    fNewLegitItemFound = true;
                }

                //
                // Remember that we've now descended through this item
                //
                vectorDescendantsMarkedLegit.push_back(
                    pItem);

                //
                // Continue marking other descendant items as legitimate
                //
                return 0;
            }

            //
            // If this item was already marked as legititmate...
            //
            if (vectorLegitItems.has(pItem))
            {
                //
                // If we have a legitimate item that's an if/for/while/do/
                // return statement then mark the expression part of that node
                // (for example, the "x" in "if(x)") as legitimate as well
                //
                pExpression = NULL;
                switch (pItem->op)
                {
                case cit_if:
                    pExpression = &((cinsn_t*)pItem)->cif->expr; break;
                case cit_for:
                    pExpression = &((cinsn_t*)pItem)->cfor->expr; break;
                case cit_while:
                    pExpression = &((cinsn_t*)pItem)->cwhile->expr; break;
                case cit_do:
                    pExpression = &((cinsn_t*)pItem)->cdo->expr; break;
                case cit_return:
                    pExpression = &((cinsn_t*)pItem)->creturn->expr; break;
                default:
                    break;
                }
                if (pExpression == NULL)
                {
                    return 0;
                }

                //
                // If the expression hasn't already been marked as legitimate
                // then mark it so and mark all of its descendants as
                // legitimate as well
                //
                if (!vectorDescendantsMarkedLegit.has(pExpression))
                {
                    //
                    // Mark all items under this expression/call as legitimate
                    //
                    fMarkingDescendantsLegit = true;
                    apply_to(
                        pExpression,
                        NULL);
                    fMarkingDescendantsLegit = false;

                    //
                    // cit_for statements require us to also process the
                    // for-loop initialization and step expressions
                    //
                    if (pItem->op == cit_for)
                    {
                        //
                        // Process the for-loop's initialization expression
                        //
                        pExpression = &((cinsn_t*)pItem)->cfor->init;
                        if (!vectorDescendantsMarkedLegit.has(pExpression))
                        {
                            //
                            // Mark all items under this expression as legit
                            //
                            fMarkingDescendantsLegit = true;
                            apply_to(
                                pExpression,
                                NULL);
                            fMarkingDescendantsLegit = false;
                        }

                        //
                        // Process the for-loop's step expression
                        //
                        pExpression = &((cinsn_t*)pItem)->cfor->step;
                        if (!vectorDescendantsMarkedLegit.has(pExpression))
                        {
                            //
                            // Mark all items under this expression as legit
                            //
                            fMarkingDescendantsLegit = true;
                            apply_to(
                                pExpression,
                                NULL);
                            fMarkingDescendantsLegit = false;
                        }
                    }
                }

                return 0;
            }

            //
            // If this item is a legitimate variable and/or a CPPEH_RECORD
            // variable, or a function, global variable, legit macro, goto,
            // break, continue, return, or asm-statement then mark the ancestor
            // expressions as legitimate
            //
            if (pItem->op == cot_var)
            {
                if (!afVariableIsLegit[((cexpr_t*)pItem)->v.idx])
                {
                    if (T_NORMAL != print_type_to_one_line(
                        szType,
                        _countof(szType),
                        idati,
                        ((cexpr_t*)pItem)->type.u_str()))
                    {
                        return 0;
                    }
                    if (0 != strcmp(szType, "CPPEH_RECORD"))
                    {
                        return 0;
                    }
                }
            }
            else if (!((pItem->op == cot_obj) ||
                ((pItem->op == cot_call) &&
                    IsLegitimateCall((cexpr_t*)pItem)) ||
                (pItem->op == cit_goto) ||
                (pItem->op == cit_break) ||
                (pItem->op == cit_continue) ||
                (pItem->op == cit_return)) ||
                (pItem->op == cit_asm))
            {
                return 0;
            }

            //
            // Iterate through all ancestors (assumes that the decompilation
            // graph is a tree and that no item has more than one parent)
            //
            for(citem_t* pCurrentItem = pItem;
                pCurrentItem != NULL;
                pCurrentItem = pFunction->body.find_parent_of(pCurrentItem))
            {
                if (!vectorLegitItems.has(pCurrentItem))
                {
                    vectorLegitItems.push_back(
                        pCurrentItem);
                    fNewLegitItemFound = true;
                }

                if ((pCurrentItem->op == cit_expr) ||
                    ((pCurrentItem->op == cot_call) &&
                        IsLegitimateCall((cexpr_t*)pCurrentItem)) ||
                    (pCurrentItem->op == cit_return))
                {
                    //
                    // This is a cit_expr statement node or cot_call
                    // expression
                    //

                    if (!vectorDescendantsMarkedLegit.has(pCurrentItem))
                    {
                        //
                        // Mark all items under this expression/call as legit
                        //
                        fMarkingDescendantsLegit = true;
                        apply_to(
                            pCurrentItem,
                            NULL);
                        fMarkingDescendantsLegit = false;
                    }
                }
            }

            return 0;
        }