示例#1
0
        def combine(path, tp_path):
            func_pattern = re.compile(r'int[ \t\n]+%s\(([^)]*)\);*' %
                                      func_name)
            fp = path
            print('-----------------------------')
            print(fp)
            with open(fp) as f:
                content = f.read()
            finds = func_pattern.findall(content)
            if len(finds) > 1:
                raise SyntaxError(repr(finds) + '. Duplicated definition!')
            elif len(finds) == 0:
                return None
            else:
                params = finds[0].strip()
                params_list = params_list_parser(params)
                vars_list = [_[1] for _ in params_list]
                params = ', '.join(vars_list)
                params_list_with_length = []
                comments_pattern = re.compile(r'//(.*)\n[ \t]*' +
                                              r'int[ \t\n]+%s\(([^)]*)\);*' %
                                              func_name)
                cmts = comments_pattern.findall(content)
                cmt = cmts[0] if len(cmts) > 0 else ('{}', )
                cmt_dict = json.loads(cmt[0])
                for var_type, var_name in params_list:
                    length = cmt_dict.get(var_name, {}).get('length', 0)
                    params_list_with_length.append(
                        (var_type, var_name, length))

            init_vars = dict(vp=params_list_with_length, params=params)
            tp = tpp.TemplateParser(tp_path)
            sruner = sr.ScriptRunner(init_vars)
            res = sruner.run(tp.parse()[0])
            res = '\n'.join(res[1])
            res = tp.replace([
                res,
            ])
            if echo:
                print(res)
            res = '\n'.join([content, res])
            return res
示例#2
0
 def __init__(self):
     super().__init__()
     self.init_ui()
     self.cursor_thread = CursorInfo()
     self.cursor_thread.cursor_trace.connect(self.receive_cursor)
     self.is_recording = False
     self.srunner = script_runner.ScriptRunner()
     self.srunner.current_command_changed.connect(self.receive_command_info)
     self.heal_check_timer = QTimer()
     self.heal_check_timer.setInterval(1000)
     self.heal_check_timer.timeout.connect(self.heal_check)
     self.song = AudioSegment.from_wav("alarm.wav")
     self.use_mp_mode = False
     self.fill_mp_mode = False
     self.on_hp_potion = False
     self.last_mp_start_time = datetime.now()
     self.boost_mode = False
     self.keep_current_boost = False
     self.keep_press_key = False
     self.key_tmp_rl = False
     self.against_mode = False
     self.guard_time = datetime.now()
     self.enemy_time = None
示例#3
0
def ATKrun(target,
           func_name='logic_bomb',
           default_stdin_len=10,
           maxtime=60,
           source=None,
           skip=False,
           folder=None):
    def params_list_parser(params):
        if len(params.strip()) == 0:
            return []
        else:
            params = params.split(',')
            params = list(map(str.strip, params))
            res = []
            var_pattern = re.compile(r'([a-zA-Z_][a-zA-Z0-9_*]*|\*+)')
            for param in params:
                tmp = var_pattern.findall(param)
                if len(tmp) < 2:
                    raise SyntaxError(', '.join(params))
                var_name = tmp[-1]
                var_type = ' '.join(tmp[:-1])
                var_type = re.sub(r'[ \t\n]*\*', '*', var_type)
                res.append((var_type, var_name))
            return res

    cmds_tp, tp_path, prefix, src_dirs = target
    # insert perf string
    perf = 'perf stat -I 400 -o %s.%s -x, -e cycles -e instructions -e branch-misses -e L1-dcache-load-misses -e l2_rqsts.miss '
    cmds_tp[1] = perf + cmds_tp[1]

    if folder:
        src_dirs = (folder, )
    if not os.path.exists(prefix):
        os.mkdir(prefix)

    if source and not os.path.exists(source):
        os.mkdir(source)

    ERROR = 0
    CORRECT = 1
    COMPILE_ERROR = 3
    TLE = 4
    RUNTIME_ERROR = 255

    MAX_TIME = maxtime
    test_results = {}

    func_pattern = re.compile(r'int[ \t\n]+%s\(([^)]*)\);*' % func_name)

    for src_dir in src_dirs:
        print('===========')
        print('In dir ' + src_dir)
        for root, dirs, files in sorted(os.walk(src_dir)):
            for file in sorted(files):
                cmds = []
                fp = os.path.join(root, file)
                print('-----------------------------')
                print(fp)
                with open(fp) as f:
                    content = f.read()
                finds = func_pattern.findall(content)
                if len(finds) > 1:
                    raise SyntaxError(repr(finds) + '. Duplicated definition!')
                elif len(finds) == 0:
                    continue
                else:
                    params = finds[0].strip()
                    params_list = params_list_parser(params)
                    vars_list = [_[1] for _ in params_list]
                    params = ', '.join(vars_list)
                    params_list_with_length = []
                    comments_pattern = re.compile(
                        r'//(.*)\n[ \t]*' +
                        r'int[ \t\n]+%s\(([^)]*)\);*' % func_name)
                    cmts = comments_pattern.findall(content)
                    cmt = cmts[0] if len(cmts) > 0 else ('{}', )
                    cmt_dict = json.loads(cmt[0])
                    for var_type, var_name in params_list:
                        length = cmt_dict.get(var_name, {}).get('length', 0)
                        params_list_with_length.append(
                            (var_type, var_name, length))

                init_vars = dict(vp=params_list_with_length,
                                 params=params,
                                 func_name=FUNC_NAME)
                tp = tpp.TemplateParser(tp_path)
                sruner = sr.ScriptRunner(init_vars)
                res = sruner.run(tp.parse()[0])
                res = '\n'.join(res[1])
                res = tp.replace([
                    res,
                ])
                print(res)
                res = '\n'.join([content, res])
                outname = file if len(
                    file.split('.')) == 1 else file.split('.')[0]
                if source:
                    with open(os.path.join(source, file), 'w') as f:
                        f.write(res)
                    if skip:
                        continue
                if prefix == 'angr':
                    cmds.append(cmds_tp[0] % outname)
                    cmds.append(cmds_tp[1] %
                                (outname, prefix, default_stdin_len, outname))

                    # Compile
                    p = subprocess.Popen(cmds[0].split(' '),
                                         stdin=subprocess.PIPE)
                    p.communicate(res.encode('utf8'))
                    cp_value = p.wait()
                    if cp_value:
                        test_results[fp] = COMPILE_ERROR
                        print('========= Compile Error! ==========')
                        continue
                    # Run test
                    print(cmds_tp[1])
                    print(cmds[1].split(' '))
                    p = subprocess.Popen(cmds[1].split(' '))
                    print(p.pid)
                    try:
                        rt_vale = p.wait(timeout=MAX_TIME)
                        test_results[fp] = rt_vale
                    except subprocess.TimeoutExpired:
                        test_results[fp] = TLE
                        kill_all(p)

                if prefix == 'mcore':
                    cmds.append(cmds_tp[0] % outname)
                    cmds.append(cmds_tp[1] % (outname, prefix, MAX_TIME - 30,
                                              default_stdin_len, outname))

                    # Compile
                    p = subprocess.Popen(cmds[0].split(' '),
                                         stdin=subprocess.PIPE)
                    p.communicate(res.encode('utf8'))
                    cp_value = p.wait()
                    if cp_value:
                        test_results[fp] = COMPILE_ERROR
                        print('========= Compile Error! ==========')
                        continue
                    # Run test
                    p = subprocess.Popen(cmds[1].split(' '))
                    print(p.pid)
                    try:
                        rt_vale = p.wait(timeout=MAX_TIME)
                        test_results[fp] = rt_vale
                    except:
                        test_results[fp] = TLE
                        #kill_all(p)
                        p.kill()

                elif prefix == 'klee':
                    if not os.path.exists('klee'):
                        os.mkdir('klee')

                    with open('klee/a.c', 'w') as f:
                        f.write(res)

                    cmds.append(cmds_tp[0] % outname)
                    cmds.append(cmds_tp[1] % outname)
                    cmds.append(cmds_tp[2] % (2, outname))
                    p = subprocess.Popen(cmds[0].split(' '),
                                         stdin=subprocess.PIPE)
                    p.communicate(res.encode('utf8'))
                    cp_value = p.wait()
                    if cp_value:
                        test_results[fp] = COMPILE_ERROR
                        print('========= Compile Error! ==========')
                        continue
                    try:
                        p = subprocess.Popen(cmds[1].split(' '),
                                             stdout=subprocess.PIPE,
                                             stderr=subprocess.PIPE)
                        errored = False
                        out, err = p.communicate(timeout=MAX_TIME)
                        rt_vale = p.wait(timeout=MAX_TIME)
                    except subprocess.TimeoutExpired:
                        test_results[fp] = TLE
                        kill_all(p)
                        continue

                    p = subprocess.Popen(cmds[2].split(' '))
                    try:
                        rt_vale = p.wait(timeout=MAX_TIME)
                        test_results[fp] = rt_vale
                    except subprocess.TimeoutExpired:
                        test_results[fp] = TLE
                        kill_all(p)
                    shutil.rmtree('klee')
                elif prefix == 'triton':
                    cmds.append(cmds_tp[0] % outname)
                    cmds.append(cmds_tp[1] %
                                (default_stdin_len, MAX_TIME, FUNC_NAME,
                                 TRITON_INSTALLATION_PATH, outname))

                    # Compile
                    p = subprocess.Popen(cmds[0].split(' '),
                                         stdin=subprocess.PIPE)
                    p.communicate(res.encode('utf8'))
                    cp_value = p.wait()
                    if cp_value:
                        test_results[fp] = COMPILE_ERROR
                        print('========= Compile Error! ==========')
                        continue

                    # Run test
                    print("=== Run test!", outname, "===")
                    p = subprocess.Popen(cmds[1].split(' '))
                    rt_vale = p.wait()
                    test_results[fp] = rt_vale

    return test_results