Esempio n. 1
0
    def __lines(self,
                analyzer1_info: AnalyzerInfo,
                analyzer2_info: AnalyzerInfo,
                used_comparison=None):

        result_comparison = Comparison()
        self.__subproc_comparison_init(result_comparison, analyzer1_info,
                                       analyzer2_info)

        if not result_comparison.check_both_FileInfo_format():
            return -1

        # FileInfo actions BEGIN

        for file_info_an1 in analyzer1_info.info:
            file_info_an2 = analyzer2_info.search_by_file(file_info_an1.file)

            if isinstance(file_info_an2, int) and file_info_an2 == -1:
                continue

            for error_info_an1_ind in range(len(file_info_an1.errors)):
                for error_info_an2_ind in range(len(file_info_an2.errors)):
                    if self.__subproc_lines_check_intersect(
                            file_info_an1[error_info_an1_ind].lines,
                            file_info_an2[error_info_an2_ind].lines,
                            self.heuristic_params["distance"]):
                        self.__subproc_result_comparison_binding_create(
                            result_comparison, file_info_an1, file_info_an2,
                            error_info_an1_ind, error_info_an2_ind)

        # FileInfo actions END
        result_comparison.stat_matrix_fill_by_bindings()
        return result_comparison
Esempio n. 2
0
    def __reaching_defs(self,
                        analyzer1_info: AnalyzerInfo,
                        analyzer2_info: AnalyzerInfo,
                        used_comparison=None):

        result_comparison = Comparison()
        self.__subproc_comparison_init(result_comparison, analyzer1_info,
                                       analyzer2_info)

        if not result_comparison.check_both_FileInfo_format():
            return -1

        for file_info_an1 in analyzer1_info:

            file_info_an2 = analyzer2_info.search_by_file(file_info_an1.file)
            if isinstance(file_info_an2, int) and file_info_an2 == -1:
                continue

            cursor = create_cursor(file_info_an1.file)

            for error_an1_ind, error_an1 in enumerate(file_info_an1):
                var_er_an1 = self.__subproc_get_msg_leak_var(
                    error_an1, analyzer1_info.analyzer_name)

                if var_er_an1 is None:
                    continue

                line_er_an1 = error_an1.main_line
                cursor_er_an1 = reaching_var_def_by_line(
                    cursor, var_er_an1, line_er_an1)
                for error_an2_ind, error_an2 in enumerate(file_info_an2):
                    var_er_an2 = self.__subproc_get_msg_leak_var(
                        error_an2, analyzer2_info.analyzer_name)

                    if var_er_an2 is None:
                        continue

                    line_er_an2 = error_an2.main_line
                    cursor_er_an2 = reaching_var_def_by_line(
                        cursor, var_er_an2, line_er_an2)

                    if cursor_er_an1 is None or cursor_er_an2 is None:
                        continue

                    if cursor_er_an2 == cursor_er_an1:
                        self.__subproc_result_comparison_binding_create(
                            result_comparison, file_info_an1, file_info_an2,
                            error_an1_ind, error_an2_ind)

        result_comparison.stat_matrix_fill_by_bindings()
        return result_comparison
Esempio n. 3
0
    def __funcs(self,
                analyzer1_info: AnalyzerInfo,
                analyzer2_info: AnalyzerInfo,
                used_comparison=None):
        # TODO: fully implement and debug

        def __subproc_same_func_in_file_check(cursor: cl.Cursor, error_an1,
                                              error_an2):

            for child in cursor.get_children():
                if child.kind == cl.CursorKind.FUNCTION_DECL:
                    if child.extent.start.line <= error_an1.main_line <= child.extent.end.line and \
                            child.extent.start.line <= error_an2.main_line <= child.extent.end.line:
                        return True

        result_comparison = Comparison()
        self.__subproc_comparison_init(result_comparison, analyzer1_info,
                                       analyzer2_info)

        if not result_comparison.check_both_FileInfo_format():
            return -1

        for file_info_an1 in analyzer1_info:

            file_info_an2 = analyzer2_info.search_by_file(file_info_an1.file)
            if isinstance(file_info_an2, int) and file_info_an2 == -1:
                continue

            cursor = create_cursor(file_info_an1.file)

            for error_info_an1_ind, error_an1 in enumerate(file_info_an1):
                for error_info_an2_ind, error_an2 in enumerate(file_info_an2):
                    if __subproc_same_func_in_file_check(
                            cursor, error_an1, error_an2):
                        self.__subproc_result_comparison_binding_create(
                            result_comparison, file_info_an1, file_info_an2,
                            error_info_an1_ind, error_info_an2_ind)

        result_comparison.stat_matrix_fill_by_bindings()
        return result_comparison
Esempio n. 4
0
    def __files(self,
                analyzer1_info: AnalyzerInfo,
                analyzer2_info: AnalyzerInfo,
                used_comparison=None):

        result_comparison = Comparison()
        self.__subproc_comparison_init(result_comparison, analyzer1_info,
                                       analyzer2_info)

        if not result_comparison.check_both_FileInfo_format():
            return -1

        for file_info_an1 in analyzer1_info:

            #if file_info_an1.file == "/home/nick/C/testcases/CWE401_Memory_Leak/s01/CWE401_Memory_Leak__char_calloc_21.c":
            #    print("HEH")

            file_info_an2 = analyzer2_info.search_by_file(file_info_an1.file)
            if isinstance(file_info_an2, int) and file_info_an2 == -1:
                continue

            for error_an1_ind, error_an1 in enumerate(file_info_an1):
                var_er_an1 = self.__subproc_get_msg_leak_var(
                    error_an1, analyzer1_info.analyzer_name)

                for error_an2_ind, error_an2 in enumerate(file_info_an2):
                    var_er_an2 = self.__subproc_get_msg_leak_var(
                        error_an2, analyzer2_info.analyzer_name)

                    if var_er_an1 == var_er_an2:
                        self.__subproc_result_comparison_binding_create(
                            result_comparison, file_info_an1, file_info_an2,
                            error_an1_ind, error_an2_ind)

        result_comparison.stat_matrix_fill_by_bindings()
        return result_comparison
Esempio n. 5
0
    def __syntax_construct(self,
                           analyzer1_info: AnalyzerInfo,
                           analyzer2_info: AnalyzerInfo,
                           used_comparison=None):

        # SUBPROCEDURES BEGIN

        def __subfunc_search(node: clang.cindex.Cursor, lst: list):

            if node.kind in self.heuristic_params["statement_list"]:
                trace_unit = []
                trace_unit.append(node.location.line)
                sub_stmts = list(node.get_children())
                for el in sub_stmts:
                    trace_unit.append(el.location.line)
                lst.append(trace_unit)

            for c in node.get_children():
                __subfunc_search(c, lst)
            return

        # SUBPROCEDURES END

        result_comparison = Comparison()
        self.__subproc_comparison_init(result_comparison, analyzer1_info,
                                       analyzer2_info)

        if not result_comparison.check_both_FileInfo_format():
            return -1

        # FileInfo actions BEGIN

        for file_info_an1 in analyzer1_info:

            file_info_an2 = analyzer2_info.search_by_file(file_info_an1.file)
            if isinstance(file_info_an2, int) and file_info_an2 == -1:
                continue

            index = clang.cindex.Index.create()
            translation_unit = index.parse(
                file_info_an1.file,
                args=[self.heuristic_params["c++_version"]])
            cursor = translation_unit.cursor

            syntax_constr_line_list = []
            __subfunc_search(cursor, syntax_constr_line_list)

            for error_info_an1_ind in range(len(file_info_an1.errors)):
                for error_info_an2_ind in range(len(file_info_an2.errors)):
                    if file_info_an1[error_info_an1_ind].type in self.heuristic_params["analyzer1_warn_types_list"] and \
                       file_info_an2[error_info_an2_ind].type in self.heuristic_params["analyzer2_warn_types_list"]:

                        for statement_lines in syntax_constr_line_list:
                            if self.__subproc_lines_check_intersect(file_info_an1[error_info_an1_ind].lines,
                                                                    statement_lines, 0) and \
                                self.__subproc_lines_check_intersect(file_info_an2[error_info_an2_ind].lines,
                                                                     statement_lines, 0):
                                self.__subproc_result_comparison_binding_create(
                                    result_comparison, file_info_an1,
                                    file_info_an2, error_info_an1_ind,
                                    error_info_an2_ind)

        # FileInfo actions END
        result_comparison.stat_matrix_fill_by_bindings()
        return result_comparison