Exemple #1
0
def convert_tlsf_or_acacia_to_acacia(spec_file_name: str,
                                     is_moore_=None) -> (str, str, bool):
    if spec_file_name.endswith('.ltl'):
        ltl_text, part_text = readfile(spec_file_name), readfile(
            spec_file_name.replace('.ltl', '.part'))
        return ltl_text, part_text, is_moore_

    rc, out, err = execute_shell('{syfco} -ins {spec_file_name}'.format(
        syfco=SYFCO_PATH, spec_file_name=spec_file_name))
    assert_exec_strict(rc, out, err)
    part_text = '.inputs ' + ' '.join(_parse_tuple_str(
        out.lower()))  # syfco lowers all signal names in props
    rc, out, err = execute_shell('{syfco} -outs {spec_file_name}'.format(
        syfco=SYFCO_PATH, spec_file_name=spec_file_name))
    assert_exec_strict(rc, out, err)
    part_text += '\n.outputs ' + ' '.join(_parse_tuple_str(
        out.lower()))  # syfco lowers all signal names in props
    # rc, out, err = execute_shell('{syfco} -f acacia -m fully -os Moore {spec_file_name}'
    #                              .format(syfco=SYFCO_PATH, spec_file_name=spec_file_name))
    # rc, out, err = execute_shell('{syfco} -f acacia -m fully {spec_file_name}'
    #                              .format(syfco=SYFCO_PATH, spec_file_name=spec_file_name))
    rc, out, err = execute_shell(
        '{syfco} -f lily -m fully -nr {spec_file_name}'.format(
            syfco=SYFCO_PATH, spec_file_name=spec_file_name))
    assert_exec_strict(rc, out, err)
    ltl_text = out

    return ltl_text, part_text, get_spec_type(spec_file_name)
Exemple #2
0
def _create_monitor_file(tlsf_file_name) -> str:
    rc, out, err = execute_shell('{syfco} -f smv {tlsf_file} -m fully'.format(
        syfco=SYFCO_PATH, tlsf_file=tlsf_file_name))
    assert_exec_strict(rc, out, err)

    rc, out, err = execute_shell('{smvtoaig} -a -L "{ltl2smv}"'.format(
        smvtoaig=SMVTOAIG_PATH, ltl2smv=LTL2SMV_PATH),
                                 input=out)
    assert rc == 0, rc_out_err_to_str(rc, out,
                                      err)  # it outputs the LTL into stderr

    return create_unique_file(out, suffix='.aag')
Exemple #3
0
def _create_monitor_file(tlsf_file_name) -> str:
    rc, out, err = execute_shell('{syfco} -f smv {tlsf_file} -m fully'.format(syfco=SYFCO_PATH,
                                                                              tlsf_file=tlsf_file_name))
    assert_exec_strict(rc, out, err)

    rc, out, err = execute_shell('{smvtoaig} -a'.format(smvtoaig=SMVTOAIG_PATH), input=out)
    assert rc == 0, rc_out_err_to_str(rc, out, err)   # it outputs the LTL into stderr

    (fd, aag_file_name) = tempfile.mkstemp(text=True, suffix='.aag')
    os.write(fd, bytes(out, encoding=sys.getdefaultencoding()))
    os.close(fd)

    return aag_file_name
Exemple #4
0
def _run_benchmark(solver:str, is_real:bool, bench_file_name:str, mc:bool) -> str or None:
    str_by_rc = {R_RC:'real_rc', U_RC:'unreal_rc', UNKN_RC:'unknown_rc'}

    rc, out, err = execute_shell('{solver} {bench}'.format(solver=solver, bench=bench_file_name))
    if rc not in (U_RC, R_RC, UNKN_RC):
        return rc_out_err_to_str(rc, out, err)

    exp_rc = (U_RC, R_RC)[is_real]
    if exp_rc != rc:
        return 'Realizability differs (expected vs. actual): %s vs. %s'\
               % (str_by_rc[exp_rc], str_by_rc[rc])

    out_lines = out.splitlines()
    if (is_real and out_lines[0] != R_STR) or (not is_real and out_lines[0] != U_STR):
        return 'stdout first line should be %s, got instead: %s'\
               % ((U_STR, R_STR)[is_real], out_lines[0])

    if not mc or not is_real:
        return None

    aiger_solution = '\n'.join(out_lines[1:])
    aiger_solution_file_name = create_unique_file(aiger_solution)
    is_correct = check_model.main(aiger_solution_file_name, bench_file_name, False)
    if not is_correct:
        return "the model is wrong"
    os.remove(aiger_solution_file_name)
    return None
Exemple #5
0
def get_spec_type(spec_file_name) -> bool:
    rc, out, err = execute_shell("{syfco} -g {spec_file_name}".format(syfco=SYFCO_PATH, spec_file_name=spec_file_name))
    assert_exec_strict(rc, out, err)
    out_stripped = out.strip().lower()
    assert out_stripped in ["moore", "mealy"], out_stripped
    is_moore = out_stripped == "moore"
    return is_moore
Exemple #6
0
def _create_combined(model_file_name, monitor_aiger_file_name) -> str:
    rc, out, err = execute_shell('{combine} {spec_aiger} {model_aiger}'.format(
        combine=COMBINEAIGER_PATH,
        model_aiger=model_file_name,
        spec_aiger=monitor_aiger_file_name))
    assert_exec_strict(rc, out, err)
    return create_unique_file(out, suffix='.aag')
Exemple #7
0
def synthesize(aiger_spec: str, is_moore: bool,
               bad_out_name: str) -> str or None:
    aiger_file = create_unique_file(aiger_spec)
    logging.debug("synthesize: using %s to store aiger_spec" % aiger_file)

    logging.info('executing sdf...')

    rc, out, err = execute_shell('{sdf} {aiger_file} -f {is_moore}'.format(
        sdf=SDF_PATH,
        aiger_file=aiger_file,
        is_moore='-moore' if is_moore else ''))
    assert is_empty_str(err), rc_out_err_to_str(rc, out, err)
    assert rc in (REALIZABLE_RC,
                  UNREALIZABLE_RC), rc_out_err_to_str(rc, out, err)

    logging.info('sdf completed')
    logging.debug('sdf returned:\n' + out)

    out_split = out.splitlines()
    status = out_split[0]
    if status == REALIZABLE_STR:
        full_model = convert_aiger_model_to_tlsf_model(
            '\n'.join(out_split[1:]), bad_out_name)
        os.remove(aiger_file)
        return full_model

    os.remove(aiger_file)
    return None
Exemple #8
0
def run_benchmark(python_script_relative_path, benchmark, rc_expected, size_expected) -> bool:
    cmd_args = BENCHMARKS_DIR + benchmark
    exec_cmd = '{python3} {program} {args}'.format(python3=sys.executable,
                                                   program=get_root_dir() + python_script_relative_path,
                                                   args=cmd_args)
    result, out, err = execute_shell(exec_cmd)

    if not is_empty_str(err):
        _print_failed('error while executing the command', exec_cmd, result, out, err)
        return False
    else:
        size_actual = _extract_model_size(out)
        if result == rc_expected and (not size_expected or size_expected == size_actual):
            _print_ok(cmd_args)
            return True
        else:
            _print_failed('invalid exit status or model size: \n'\
                          '  rc_actual vs rc_expected: {rc_act} vs. {rc_exp}\n'\
                          '  size_actual vs size_expected: {size_act} vs. {size_exp}'.
                          format(rc_act=result,
                                 rc_exp=rc_expected,
                                 size_act=size_actual,
                                 size_exp=size_expected),
                          exec_cmd, result, out, err)
            return False
Exemple #9
0
    def convert(self, expr: Expr) -> Automaton:
        # self._logger.info('Ltl2UCW: converting..\n' + ConverterToWringVisitor().dispatch(expr))

        #make sure we don't have Weak Until since ltl2ba does not accept it..
        expr = WeakToUntilConverterVisitor().dispatch(expr)

        self._logger.info('Ltl2UCW: converting..(non-negated version)\n' +
                          ConverterToWringVisitor().dispatch(expr))

        format_converter = ConverterToLtl2BaFormatVisitor()
        property_in_ltl2ba_format = format_converter.dispatch(_negate(expr))

        rc, ba, err = execute_shell('{0} "{1}"'.format(
            self._execute_cmd, property_in_ltl2ba_format))
        assert rc == 0, str(rc) + ', err: ' + str(err) + ', out: ' + str(ba)
        assert (err == '') or err is None, err
        self._logger.debug(ba)

        initial_nodes, rejecting_nodes, nodes = parse_ltl2ba_ba(
            ba, format_converter.signal_by_name)

        _assert_are_signals_in_labels(
            list(chain(*initial_nodes)) + rejecting_nodes + nodes)

        automaton = Automaton(initial_nodes,
                              rejecting_nodes,
                              nodes,
                              name=str(property_in_ltl2ba_format))

        self._logger.debug(to_dot(automaton))

        return automaton
Exemple #10
0
def run_benchmark(python_script_relative_path, benchmark,
                  is_realizable) -> bool:
    cmd_args = _BENCHMARKS_DIR + benchmark
    exec_cmd = '{python3} {program} {args}'.format(python3=sys.executable,
                                                   program=get_root_dir() +
                                                   python_script_relative_path,
                                                   args=cmd_args)
    result, out, err = execute_shell(exec_cmd)

    if (result != 0 and result != 1) or not is_empty_str(err):
        _failed('error while executing the command', exec_cmd, result, out,
                err)
        return False

    else:
        actual_is_realizable = result is 0

        if is_realizable == actual_is_realizable:
            _ok(cmd_args)
            return True
        else:
            _failed(
                'invalid realizability status(should be {status})'.format(
                    status=['unrealizable', 'realizable'][is_realizable]),
                exec_cmd, result, out, err)
            return False
Exemple #11
0
def get_spec_type(spec_file_name) -> bool:
    rc, out, err = execute_shell('{syfco} -g {spec_file_name}'.format(
        syfco=SYFCO_PATH, spec_file_name=spec_file_name))
    assert_exec_strict(rc, out, err)
    out_stripped = out.strip().lower()
    assert out_stripped in ['moore', 'mealy'], out_stripped
    return out_stripped == 'moore'
Exemple #12
0
    def convert(self, expr:Expr) -> Automaton:
        # self._logger.info('Ltl2UCW: converting..\n' + ConverterToWringVisitor().dispatch(expr))

        #make sure we don't have Weak Until since ltl2ba does not accept it..
        expr = WeakToUntilConverterVisitor().dispatch(expr)

        self._logger.info('Ltl2UCW: converting..(non-negated version)\n' + ConverterToWringVisitor().dispatch(expr))

        format_converter = ConverterToLtl2BaFormatVisitor()
        property_in_ltl2ba_format = format_converter.dispatch(_negate(expr))

        rc, ba, err = execute_shell('{0} "{1}"'.format(self._execute_cmd, property_in_ltl2ba_format))
        assert rc == 0, str(rc) + ', err: ' + str(err) + ', out: ' + str(ba)
        assert (err == '') or err is None, err
        self._logger.debug(ba)

        initial_nodes, rejecting_nodes, nodes = parse_ltl2ba_ba(ba, format_converter.signal_by_name)

        _assert_are_signals_in_labels(list(chain(*initial_nodes)) + rejecting_nodes + nodes)

        automaton = Automaton(initial_nodes, rejecting_nodes, nodes, name=str(property_in_ltl2ba_format))

        self._logger.debug(to_dot(automaton))

        return automaton
Exemple #13
0
def verilog_to_aiger(verilog:str) -> str:
    verilog_file_name = create_unique_file(verilog, suffix='.v')
    aiger_file_name = create_unique_file(suffix='.aag')

    script = """
    read_verilog {verilog_file}
    synth -flatten -auto-top
    abc -g AND
    write_aiger -ascii -symbols -zinit {aiger_file}""".format(aiger_file=aiger_file_name,
                                                              verilog_file=verilog_file_name)
    script_file_name = create_unique_file(text=script, suffix='.ys')

    files_to_remove = (aiger_file_name,
                       verilog_file_name,
                       script_file_name)  # tmp files stay if smth goes wrong

    # on some examples yosys fails with a standard stack limit, so we raise it
    hard_limit = resource.getrlimit(resource.RLIMIT_STACK)[1]
    resource.setrlimit(resource.RLIMIT_STACK, (hard_limit, hard_limit))
    rc, out, err = execute_shell('{yosys} -Q -s {script}'.format(
        yosys=YOSYS_PATH, script=script_file_name))
    assert_exec_strict(rc, out, err)
    logging.debug('yosys stdout:\n' + out)

    res = readfile(aiger_file_name)

    [os.remove(f) for f in files_to_remove]
    return res
Exemple #14
0
def verilog_to_aiger(verilog:str) -> str:
    input_verilog_file = create_tmp_file(verilog, suffix='v')
    file_blif_mv = create_tmp_file(suffix='.mv')
    file_aiger_tmp = create_tmp_file(suffix='.aag')
    file_output_aiger = create_tmp_file(suffix='.aag')

    files_to_remove = (input_verilog_file,
                       file_blif_mv,
                       file_aiger_tmp,
                       file_output_aiger)  # tmp files stay if smth goes wrong

    # vl2mv
    # on some examples vl2mv fails with a standard stack limit, so we raise it
    hard_limit = resource.getrlimit(resource.RLIMIT_STACK)[1]
    resource.setrlimit(resource.RLIMIT_STACK, (hard_limit, hard_limit))
    rc, out, err = execute_shell('{vl2mv} {file_input_verilog} -o {file_blif_mv}'.format(
        vl2mv=VL2MV_PATH,
        file_input_verilog=input_verilog_file,
        file_blif_mv=file_blif_mv))
    if rc == -11:
        logging.warning('vl2mv caught SIGSEGV: ha-ha! Re-run me on this example, or manually convert into aiger')
        logging.debug('verilog was: ' + readfile(input_verilog_file))
    assert rc == 0, rc_out_err_to_str(rc, out, err)   # no check that stderr='' because vl2mv outputs the input file name

    # abc
    rc, out, err = execute_shell('{abc} -c '
                                 '"read_blif_mv {file_blif_mv}; '
                                  'strash; refactor; rewrite; dfraig; rewrite; dfraig; '
                                  'write_aiger -s {file_aiger_tmp}"'.format(
        file_blif_mv=file_blif_mv,
        abc=ABC_PATH,
        file_aiger_tmp=file_aiger_tmp))
    assert_exec_strict(rc, out, err)

    # aigtoaig
    rc, out, err = execute_shell('{aigtoaig} {file_aiger_tmp} {file_output_aiger}'.format(
        aigtoaig=AIGTOAIG_PATH,
        file_aiger_tmp=file_aiger_tmp,
        file_output_aiger=file_output_aiger))
    assert_exec_strict(rc, out, err)

    res = readfile(file_output_aiger)

    [os.remove(f) for f in files_to_remove]
    return res
Exemple #15
0
def convert_tlsf_to_acacia(spec_file_name) -> (str, str):
    rc, out, err = execute_shell(
        "{syfco} -ins {spec_file_name}".format(syfco=SYFCO_PATH, spec_file_name=spec_file_name)
    )
    assert_exec_strict(rc, out, err)
    part_text = ".inputs " + " ".join(_parse_tuple_str(out.lower()))  # syfco lowers all signal names in props
    rc, out, err = execute_shell(
        "{syfco} -outs {spec_file_name}".format(syfco=SYFCO_PATH, spec_file_name=spec_file_name)
    )
    assert_exec_strict(rc, out, err)
    part_text += "\n.outputs " + " ".join(_parse_tuple_str(out.lower()))  # syfco lowers all signal names in props
    rc, out, err = execute_shell(
        "{syfco} -f acacia -m fully {spec_file_name}".format(syfco=SYFCO_PATH, spec_file_name=spec_file_name)
    )
    assert_exec_strict(rc, out, err)
    ltl_text = out

    return ltl_text, part_text
Exemple #16
0
def _create_combined(model_file_name, monitor_aiger_file_name) -> str:
    rc, out, err = execute_shell('{combine} {spec_aiger} {model_aiger}'.format(
        combine=COMBINEAIGER_PATH,
        model_aiger=model_file_name,
        spec_aiger=monitor_aiger_file_name))
    assert_exec_strict(rc, out, err)

    (fd, aag_file_name) = tempfile.mkstemp(text=True, suffix='.aag')
    os.write(fd, bytes(out, encoding=sys.getdefaultencoding()))
    os.close(fd)

    return aag_file_name
Exemple #17
0
def _model_check(combined_aiger_file: str) -> int:
    """ :return: 0 if correct, 1 if wrong """
    for pi in range(get_nof_properties(combined_aiger_file)):
        logging.debug('checking property ' + str(pi))
        rc, out, err = execute_shell('{IIMC} {aiger_file} --pi {pi}'.format(
            IIMC=IIMC_PATH, aiger_file=combined_aiger_file, pi=pi))
        assert rc == 0, 'model checking call failed: \n' + rc_out_err_to_str(
            rc, out, err)
        is_correct = out.splitlines()[0].strip() == '0'
        if not is_correct:
            return 1
    # end of for
    return 0
Exemple #18
0
def _model_check(combined_aiger_file:str) -> int:
    """ :return: 0 if correct, 1 if wrong """
    for pi in range(get_nof_properties(combined_aiger_file)):
        logging.debug('checking property ' + str(pi))
        rc, out, err = execute_shell('{IIMC} {aiger_file} --pi {pi}'.format(
            IIMC=IIMC_PATH,
            aiger_file=combined_aiger_file,
            pi=pi))
        assert rc == 0, 'model checking call failed: \n' + rc_out_err_to_str(rc, out, err)
        is_correct = out.splitlines()[0].strip() == '0'
        if not is_correct:
            return 1

    return 0
Exemple #19
0
    def _convert_raw(property_: str,
                     signal_by_name: Dict[str, Signal],
                     states_prefix,
                     timeout=None) -> Automaton:
        """ :param property_: in the LTL2BA format (we do NOT negate it!) """

        rc, ba, err = execute_shell('{0} "{1}"'.format(
            LTLToAtmViaLTL3BA._execute_cmd, property_),
                                    timeout=timeout)
        assert rc == 0, str(rc) + ', err: ' + str(err) + ', out: ' + str(ba)
        assert is_empty_str(err), err
        logging.debug(ba)

        aut = spot.automaton(ba + '\n')  # type: spot.twa
        # (when SPOT sees `\n` it treats input as the string)
        atm = spotAtm_to_automaton(aut, states_prefix, signal_by_name,
                                   property_)
        atm.name = property_
        return atm
Exemple #20
0
    def convert_raw(self, property:str, signal_by_name:dict, states_prefix, timeout=None) -> Automaton:
        """
        :param property: in the LTL2BA format (we do NOT negate it!)
        """

        rc, ba, err = execute_shell('{0} "{1}"'.format(self._execute_cmd, property), timeout=timeout)
        assert rc == 0, str(rc) + ', err: ' + str(err) + ', out: ' + str(ba)
        assert (err == '') or err is None, err
        logging.debug(ba)

        initial_nodes, rejecting_nodes, nodes = parse_ltl2ba_ba(ba, signal_by_name, states_prefix)

        assert set(rejecting_nodes).issubset(set(nodes)) and set(initial_nodes).issubset(set(nodes))

        _assert_are_signals_in_labels(nodes)

        automaton = Automaton(initial_nodes, rejecting_nodes, nodes, name=str(property))

        return automaton
Exemple #21
0
def run_benchmark(python_script_relative_path, benchmark, rc_expected) -> bool:
    cmd_args = _BENCHMARKS_DIR + benchmark
    exec_cmd = '{python3} {program} {args}'.format(python3=sys.executable,
                                                   program=get_root_dir() + python_script_relative_path,
                                                   args=cmd_args)
    result, out, err = execute_shell(exec_cmd)

    if not is_empty_str(err):
        _print_failed('error while executing the command', exec_cmd, result, out, err)
        return False

    else:
        if result == rc_expected:
            _print_ok(cmd_args)
            return True
        else:
            _print_failed('invalid exit status (actual != expected: {act} != {exp})'.
                          format(act=result, exp=rc_expected),
                          exec_cmd, result, out, err)
            return False
    def convert(self, expr:Expr) -> Automaton:
        self._logger.debug('Ltl2UCW: converting..')

        format_converter = ConverterToLtl2BaFormatVisitor()
        property_in_ltl2ba_format = format_converter.dispatch(_negate(expr))
	
        self._logger.debug("------------------------------------------")
        self._logger.debug(property_in_ltl2ba_format)
        self._logger.debug("------------------------------------------")


        rc, ba, err = execute_shell('{0} "{1}"'.format(self._execute_cmd, property_in_ltl2ba_format))
        assert rc == 0, str(rc) + ', err: ' + str(err) + ', out: ' + str(ba)
        assert (err == '') or err is None, err
        self._logger.debug(ba)

        initial_nodes, rejecting_nodes, nodes = parse_ltl2ba_ba(ba, format_converter.signal_by_name)

        _assert_are_signals_in_labels(list(chain(*initial_nodes)) + rejecting_nodes + nodes)

        automaton = Automaton(initial_nodes, rejecting_nodes, nodes, name=str(property_in_ltl2ba_format))
        return automaton
Exemple #23
0
    def solve(self) -> List[str] or None:
        logging.info('solving ' + self._file_name)

        self._query_storage += smt_format.make_exit()
        self._query_storage.flush()
        #change the name of file and z3_cmd if necessary
        ret, out, err = execute_shell(self._z3_cmd)

        logging.debug('solver returned: \n' + out)

        out_lines = [s.strip() for s in out.splitlines() if s]
        if ret == 1 and out_lines[0].strip() != 'unsat':
            assert 0, 'error while executing z3: ret: {ret}\n' \
                      'out:{out}\n' \
                      'err:{err}\n'.format(ret=str(ret),
                                           out=out,
                                           err=err)

        if out_lines[0] == 'sat':
            return out_lines[1:]
        else:
            return None
Exemple #24
0
def run_benchmark(python_script_relative_path, benchmark, is_realizable) -> bool:
    cmd_args = _BENCHMARKS_DIR + benchmark
    exec_cmd = '{python3} {program} {args}'.format(python3=sys.executable,
                                                   program=get_root_dir() + python_script_relative_path,
                                                   args=cmd_args)
    result, out, err = execute_shell(exec_cmd)

    if (result != 0 and result != 1) or not is_empty_str(err):
        _failed('error while executing the command', exec_cmd, result, out, err)
        return False

    else:
        actual_is_realizable = result is 0

        if is_realizable == actual_is_realizable:
            _ok(cmd_args)
            return True
        else:
            _failed('invalid realizability status(should be {status})'.
                    format(status=['unrealizable', 'realizable'][is_realizable]),
                    exec_cmd, result, out, err)
            return False
Exemple #25
0
    def solve(self):
        self._logger.info('solving ' + self._file_name)

        self._query_storage += smt_helper.make_exit()
        self._query_storage.flush()  # i know query_storage.flush() exists
        #change the name of file and z3_cmd if necessary
        ret, out, err = execute_shell(self._z3_cmd)

        self._logger.debug('solver returned: \n' + out)

        out_lines = [s.strip() for s in out.splitlines() if s]
        if ret == 1 and out_lines[0].strip() != 'unsat':
            assert 0, 'error while executing z3: ret: {ret}\n' \
                      'out:{out}\n' \
                      'err:{err}\n'.format(ret=str(ret),
                                           out=out,
                                           err=err)

        if out_lines[0] == 'sat':
            return out_lines[1:]
        else:
            return None