Beispiel #1
0
 def handle_segment(self, doc, segment):
     if not segment: return
     segment_text = ''
     for node in segment:
         segment_text += domutil.get_text_content(node) + '\n'
     if config.get('max_segment_size'):
         if segment_text.count('\n') > int(config.get('max_segment_size')):
             return
     if config.get('show_segment_size') == 'true':
         print('segment size: '+str(segment_text.count('\n')))
     if config.get('show_parent_function_size') == 'true':
         function_node = domutil.get_parent_by_tagname(segment[0], 'function')
         function_size = 0
         if function_node:
             text = domutil.get_text_content(function_node)
             function_size = text.count('\n')
         print('parent function size: '+str(function_size))
     if config.get('show_segment') == 'true':
         print(segment_text)
     instrument_segment(doc, segment)
     max_linear_value = int(config.get('max_linear_value'))
     stop_criteria = StopCriteria()
     for i in range(max_linear_value+1):
         logger.info('context search: ' + str(i))
         if config.get('show_context_search_value') == 'true':
             print('context search value: '+str(i))
         context, functions = get_context(doc, segment, i)
         if not context: break
         if self.builder.build(doc, context, functions):
             if config.get('run_test') == 'true':
                 result = self.tester.test()
                 if self.analyzer == 'recursive':
                     if result == True:
                         green('found equivalent loops for recursive call')
                         print_nodes(segment)
                 elif self.analyzer == 'invariant':
                     if config.get('show_analyze_result') == 'true':
                         print(result)
                     stop_criteria.add(result)
                     stable_result = stop_criteria.get_stable()
                     if stable_result:
                         green(stable_result)
                 if config.get('interact_after_test') == 'true':
                     input('Enter to continue ...')
     remove_instrument(doc)
Beispiel #2
0
 def build(self, doc, context, functions):
     """
     :param doc: document of the context
     :param context: context nodes
     :param functions: functions because of the linear search up above function.
     functions = {call_name: node, ...}
     """
     self.doc = doc
     self.context = context
     self.functions = functions
     self.inputs = io.resolve_undefined_vars(context, set())
     self.resolved = {}
     self.unresolved = set()
     self.generate()
     self.resolve()
     self.generate_support()
     Instrumenter().instrument()
     compile_success = self.compile()
     if config.get("show_compile_result") == "true":
         if compile_success:
             green("compile success")
         else:
             red("compile error")
     return compile_success
Beispiel #3
0
    def run(self):
        fout = open(self.output_folder + "/output.csv", "w")
        fin = open(self.output_folder + "/input.csv", "w")
        var_list = [var for _, var in self.output_spec]
        fout.write(",".join(var_list))
        output_number = len(var_list)
        fout.write("\n")
        input_var_list = [var for _, var in self.input_spec]
        fin.write(",".join(input_var_list))
        fin.write("\n")
        logger.info("Running " + str(len(self.tests)) + " tests")
        run_success = False
        for test in self.tests:
            if config.get("show_test_data") == "true":
                logger.debug("input:" + repr(test))
            fin.write(",".join(test.split()))
            fin.write("\n")
            p = Popen(self.output_folder + "/a.out", shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
            # print(test)
            if not test.strip():
                continue
            timeout = int(config.get("timeout"))
            try:
                stdout, _ = p.communicate(input=test.encode("utf8"), timeout=timeout / 1000)
                stdout = stdout.decode("utf8")
            except TimeoutExpired:
                logger.warning("testing timeout")
                p.kill()
                stdout = ""
            except UnicodeDecodeError:
                logger.warning("UnicodeDecodeError")
                stdout = ""
            # stderr = stderr.decode('utf8')
            # -6: 134: assertion fail
            # -11: 139: segment fault
            if p.returncode == 0:
                run_success = True

            if config.get("show_returncode") == "true":
                print("return code: " + str(p.returncode))
            if p.returncode != 0:
                # stdout = ''
                pass
            if config.get("show_test_data") == "true":
                logger.debug("output:" + stdout)
            output = stdout.split()
            if not output:
                fout.write(",".join(["NA" for _ in range(output_number)]))
                fout.write("\n")
            else:
                if config.get("instrument_type") == "count":
                    # fout.write(str(len(output)))
                    fout.write(str(stdout.count("5678")))
                    if config.get("instrument_loop_parameter") == "true":
                        output = stdout.replace("5678", "").split()
                        for _ in range(output_number - len(output) - 1):
                            output.append("NA")
                        fout.write(",")
                        fout.write(",".join(output))
                    fout.write("\n")
                else:
                    for _ in range(output_number - len(output)):
                        output.append("NA")
                    fout.write(",".join(output))
                    fout.write("\n")
        logger.info("done running testing")
        if run_success and config.get("show_run_success") == "true":
            green("run success")
        fout.close()
        fin.close()