def solve_problems(self, problems, config): if len(problems.problems) == 0: Logger.error("No problems defined") if VerificationType.LTL in [ problem.verification for problem in problems.problems ]: ltl_reset_env() # generate systems for each problem configuration systems = {} for si in problems.symbolic_inits: (systems[('hts', si)], _, _) = self.parse_model(problems.relative_path, \ problems.model_file, \ problems.abstract_clock, si, "System 1", \ boolean=problems.boolean, \ no_clock=problems.no_clock, \ run_passes=problems.run_coreir_passes) if problems.equivalence is not None: (systems[('hts2', si)], _, _) = self.parse_model(problems.relative_path, \ problems.equivalence, \ problems.abstract_clock, si, "System 2", \ boolean=problems.boolean, \ no_clock=problems.no_clock, \ run_passes=problems.run_coreir_passes) else: systems[('hts2', si)] = None for problem in problems.problems: problem.hts = systems[('hts', problem.symbolic_init)] problem.hts2 = systems[('hts2', problem.symbolic_init)] problem.abstract_clock = problems.abstract_clock problem.no_clock = problems.no_clock problem.run_coreir_passes = problems.run_coreir_passes problem.relative_path = problems.relative_path if config.time or problems.time: timer_solve = Logger.start_timer("Problem %s" % problem.name, False) try: self.solve_problem(problem, config) Logger.msg(" %s\n" % problem.status, 0, not (Logger.level(1))) if config.time or problems.time: problem.time = Logger.get_timer(timer_solve, False) except KeyboardInterrupt as e: Logger.msg("\b\b Skipped!\n", 0)
def _solve(self, solver): self._write_smt2_log(solver, "(check-sat)") self._write_smt2_log(solver, "") if self.config.skip_solving: return None if Logger.level(2): timer = Logger.start_timer("Solve") r = solver.solver.solve() if Logger.level(2): self.total_time += Logger.get_timer(timer) Logger.log("Total time solve: %.2f sec" % self.total_time, 1) return r
def solve_liveness_inc_fwd(self, hts, prop, k, k_min, eventually=False): self._reset_assertions(self.solver) init = hts.single_init() trans = hts.single_trans() invar = hts.single_invar() if self.config.simplify: Logger.log("Simplifying the Transition System", 1) if Logger.level(1): timer = Logger.start_timer("Simplify") init = simplify(init) trans = simplify(trans) invar = simplify(invar) if Logger.level(1): Logger.get_timer(timer) heqvar = None if not eventually: heqvar = Symbol(HEQVAR, BOOL) self._init_at_time(hts.vars.union(set([heqvar])), k) if self.config.prove: self.solver_klive = self.solver.copy("klive") self._reset_assertions(self.solver_klive) self._add_assertion(self.solver_klive, self.at_time(invar, 0)) if eventually: self._add_assertion(self.solver_klive, self.at_time(init, 0)) propt = FALSE() formula = And(init, invar) formula = self.at_time(formula, 0) Logger.log("Add init and invar", 2) self._add_assertion(self.solver, formula) next_prop = TS.has_next(prop) if next_prop: if k < 1: Logger.error( "Liveness checking with next variables requires at least k=1" ) k_min = 1 t = 0 while (t < k + 1): self._push(self.solver) loopback = FALSE() if t > 0: loopback = self.all_loopbacks(self.hts.vars, t, heqvar) Logger.log("Add loopbacks at time %d" % t, 2) self._add_assertion(self.solver, loopback) if t >= k_min: self._write_smt2_comment(self.solver, "Solving for k=%s" % (t)) Logger.log("\nSolving for k=%s" % (t), 1) if self._solve(self.solver): Logger.log("Counterexample found with k=%s" % (t), 1) model = self._get_model(self.solver) return (t, model) else: Logger.log("No counterexample found with k=%s" % (t), 1) Logger.msg(".", 0, not (Logger.level(1))) else: Logger.log("Skipping solving for k=%s (k_min=%s)" % (t, k_min), 1) Logger.msg(".", 0, not (Logger.level(1))) self._pop(self.solver) n_prop = Not(prop) if not eventually: n_prop = Or(n_prop, Not(heqvar)) if next_prop: if t > 0: propt = self.at_time(n_prop, t - 1) else: propt = self.at_time(n_prop, t) self._add_assertion(self.solver, propt) if self.config.prove: if t > 0: self._add_assertion(self.solver_klive, trans_t) self._write_smt2_comment(self.solver_klive, "Solving for k=%s" % (t)) if next_prop: if t > 0: propt = self.at_time(Not(prop), t - 1) else: propt = self.at_time(Not(prop), t) self._add_assertion(self.solver_klive, propt) if t >= k_min: if self._solve(self.solver_klive): Logger.log("K-Liveness failed with k=%s" % (t), 1) else: Logger.log("K-Liveness holds with k=%s" % (t), 1) return (t, True) else: self._add_assertion(self.solver_klive, self.at_time(Not(prop), 0)) # self._push(self.solver_klive) # self._add_assertion(self.solver_klive, self.at_time(prop, 0)) # res = self._solve(self.solver_klive) # self._pop(self.solver_klive) # if res: # self._add_assertion(self.solver_klive, self.at_time(prop, 0)) # else: # self._add_assertion(self.solver_klive, self.at_time(Not(prop), 0)) trans_t = self.unroll(trans, invar, t + 1, t) self._add_assertion(self.solver, trans_t) if self.assert_property: prop_t = self.unroll(TRUE(), prop, t, t - 1) self._add_assertion(self.solver, prop_t) Logger.log("Add property at time %d" % t, 2) t += 1 return (t - 1, None)
def solve_problems(self, problems, config): encoder_config = self.problems2encoder_config(config, problems) self.sparser = StringParser(encoder_config) self.lparser = LTLParser() self.coi = ConeOfInfluence() invar_props = [] ltl_props = [] si = False if len(problems.symbolic_inits) == 0: problems.symbolic_inits.add(si) HTSM = 0 HTS2 = 1 HTSD = (HTSM, si) model_extension = config.model_extension if problems.model_extension is None else problems.model_extension assume_if_true = config.assume_if_true or problems.assume_if_true cache_files = config.cache_files or problems.cache_files clean_cache = config.clean_cache modifier = None if model_extension is not None: modifier = lambda hts: ModelExtension.extend( hts, ModelModifiersFactory.modifier_by_name(model_extension)) # generate systems for each problem configuration systems = {} for si in problems.symbolic_inits: encoder_config.symbolic_init = si (systems[(HTSM, si)], invar_props, ltl_props) = self.parse_model(problems.relative_path, \ problems.model_file, \ encoder_config, \ "System 1", \ modifier, \ cache_files=cache_files, \ clean_cache=clean_cache) if problems.equivalence is not None: (systems[(HTS2, si)], _, _) = self.parse_model(problems.relative_path, \ problems.equivalence, \ encoder_config, \ "System 2", \ cache_files=cache_files, \ clean_cache=clean_cache) else: systems[(HTS2, si)] = None if config.safety or config.problems: for invar_prop in invar_props: inv_prob = problems.new_problem() inv_prob.verification = VerificationType.SAFETY inv_prob.name = invar_prop[0] inv_prob.description = invar_prop[1] inv_prob.formula = invar_prop[2] problems.add_problem(inv_prob) if config.ltl or config.problems: for ltl_prop in ltl_props: ltl_prob = problems.new_problem() ltl_prob.verification = VerificationType.LTL ltl_prob.name = ltl_prop[0] ltl_prob.description = ltl_prop[1] ltl_prob.formula = ltl_prop[2] problems.add_problem(ltl_prob) if HTSD in systems: problems._hts = systems[HTSD] for problem in problems.problems: problem.hts = systems[(HTSM, problem.symbolic_init)] if problems._hts is None: problems._hts = problem.hts problem.hts2 = systems[(HTS2, problem.symbolic_init)] if problems._hts2 is None: problems._hts2 = problem.hts2 problem.vcd = problems.vcd or config.vcd or problem.vcd problem.abstract_clock = problems.abstract_clock or config.abstract_clock problem.add_clock = problems.add_clock or config.add_clock problem.coi = problems.coi or config.coi problem.run_coreir_passes = problems.run_coreir_passes problem.relative_path = problems.relative_path problem.cardinality = max(problems.cardinality, config.cardinality) if not problem.full_trace: problem.full_trace = problems.full_trace if not problem.trace_vars_change: problem.trace_vars_change = problems.trace_vars_change if not problem.trace_all_vars: problem.trace_all_vars = problems.trace_all_vars if not problem.clock_behaviors: clk_bhvs = [ p for p in [problems.clock_behaviors, config.clock_behaviors] if p is not None ] if len(clk_bhvs) > 0: problem.clock_behaviors = ";".join(clk_bhvs) if not problem.generators: problem.generators = config.generators Logger.log( "Solving with abstract_clock=%s, add_clock=%s" % (problem.abstract_clock, problem.add_clock), 2) if problem.trace_prefix is not None: problem.trace_prefix = "".join( [problem.relative_path, problem.trace_prefix]) if config.time or problems.time: timer_solve = Logger.start_timer("Problem %s" % problem.name, False) try: self.__solve_problem(problem, config) if problem.verification is None: Logger.log("Unset verification", 2) continue Logger.msg(" %s\n" % problem.status, 0, not (Logger.level(1))) if (assume_if_true) and \ (problem.status == VerificationStatus.TRUE) and \ (problem.assumptions == None) and \ (problem.verification == VerificationType.SAFETY): ass_ts = TS("Previous assumption from property") if TS.has_next(problem.formula): ass_ts.trans = problem.formula else: ass_ts.invar = problem.formula problem.hts.reset_formulae() problem.hts.add_ts(ass_ts) if config.time or problems.time: problem.time = Logger.get_timer(timer_solve, False) except KeyboardInterrupt as e: Logger.msg("\b\b Skipped!\n", 0)
def __solve_problem(self, problem, config): if problem.name is not None: Logger.log("\n*** Analyzing problem \"%s\" ***" % (problem), 1) Logger.msg("Solving \"%s\" " % problem.name, 0, not (Logger.level(1))) parsing_defs = [problem.formula, problem.lemmas, problem.assumptions] for i in range(len(parsing_defs)): if parsing_defs[i] is not None: pdef_file = problem.relative_path + parsing_defs[i] if os.path.isfile(pdef_file): with open(pdef_file) as f: parsing_defs[i] = [ p.strip() for p in f.read().strip().split("\n") ] else: parsing_defs[i] = [ p.strip() for p in parsing_defs[i].split(MODEL_SP) ] else: parsing_defs[i] = None [formulae, problem.lemmas, problem.assumptions] = parsing_defs ParametricBehavior.apply_to_problem(problem, self.model_info) assumps = None lemmas = None trace = None traces = None if formulae is None: if problem.verification == VerificationType.SIMULATION: formulae = ["True"] elif (problem.verification is not None) and ( problem.verification != VerificationType.EQUIVALENCE): Logger.error("Property not provided") accepted_ver = False if formulae is not None: problem.formula = formulae[0] precondition = config.precondition if config.precondition is not None else problem.precondition if precondition and problem.verification == VerificationType.SAFETY: problem.formula = "(%s) -> (%s)" % (precondition, problem.formula) if (problem.verification != VerificationType.EQUIVALENCE) and ( problem.formula is not None): assumps = [ t[1] for t in self.sparser.parse_formulae(problem.assumptions) ] lemmas = [ t[1] for t in self.sparser.parse_formulae(problem.lemmas) ] for ass in assumps: problem.hts.add_assumption(ass) for lemma in lemmas: problem.hts.add_lemma(lemma) if problem.verification != VerificationType.LTL: (strprop, prop, types) = self.sparser.parse_formulae([problem.formula])[0] else: (strprop, prop, types) = self.lparser.parse_formulae([problem.formula])[0] problem.formula = prop if problem.verification is None: return problem if problem.coi: if Logger.level(2): timer = Logger.start_timer("COI") problem.hts = self.coi.compute(problem.hts, problem.formula) if Logger.level(2): Logger.get_timer(timer) mc_config = self.problem2mc_config(problem, config) bmc_safety = BMCSafety(problem.hts, mc_config) bmc_parametric = BMCParametric(problem.hts, mc_config) bmc_ltl = BMCLTL(problem.hts, mc_config) res = VerificationStatus.UNC bmc_length = max(problem.bmc_length, config.bmc_length) bmc_length_min = max(problem.bmc_length_min, config.bmc_length_min) if problem.verification == VerificationType.SAFETY: accepted_ver = True Logger.log("Property: %s" % (prop.serialize(threshold=100)), 2) res, trace, _ = bmc_safety.safety(prop, bmc_length, bmc_length_min, config.processes) if problem.verification == VerificationType.LTL: accepted_ver = True res, trace, _ = bmc_ltl.ltl(prop, bmc_length, bmc_length_min) if problem.verification == VerificationType.SIMULATION: accepted_ver = True res, trace = bmc_safety.simulate(prop, bmc_length) if problem.verification == VerificationType.PARAMETRIC: accepted_ver = True Logger.log("Property: %s" % (prop.serialize(threshold=100)), 2) res, traces, problem.region = bmc_parametric.parametric_safety( prop, bmc_length, bmc_length_min, ModelExtension.get_parameters(problem.hts), at_most=problem.cardinality) hts = problem.hts if problem.verification == VerificationType.EQUIVALENCE: accepted_ver = True htseq, miter_out = Miter.combine_systems(problem.hts, \ problem.hts2, \ bmc_length, \ problem.symbolic_init, \ problem.formula, \ True) if problem.assumptions is not None: assumps = [ t[1] for t in self.sparser.parse_formulae(problem.assumptions) ] if problem.lemmas is not None: lemmas = [ t[1] for t in self.sparser.parse_formulae(problem.lemmas) ] if assumps is not None: for assumption in assumps: htseq.add_assumption(assumption) if lemmas is not None: for lemma in lemmas: htseq.add_lemma(lemma) bmcseq = BMCSafety(htseq, mc_config) hts = htseq res, trace, t = bmcseq.safety(miter_out, bmc_length, bmc_length_min) if not accepted_ver: Logger.error("Invalid verification type") problem.status = res if trace is not None: problem.traces = self.__process_trace(hts, trace, config, problem) if traces is not None: problem.traces = [] for trace in traces: problem.traces += self.__process_trace(hts, trace, config, problem) if problem.assumptions is not None: problem.hts.assumptions = None Logger.log("\n*** Problem \"%s\" is %s ***" % (problem, res), 1)
def solve_safety_inc_fwd(self, hts, prop, k, k_min, \ all_vars=False, generalize=None, prove=None): add_unsat_cons = False prove = self.config.prove if prove is None else prove solver_name = "inc_fwd%s"%("_prove" if prove else "") solver = self.solver.copy(solver_name) self._reset_assertions(solver) if prove: solver_ind = self.solver.copy("%s_ind"%solver_name) self._reset_assertions(solver_ind) if all_vars: relevant_vars = hts.vars else: relevant_vars = hts.state_vars | hts.output_vars init = hts.single_init() trans = hts.single_trans() invar = hts.single_invar() acc_init = TRUE() acc_prop = TRUE() acc_loop_free = TRUE() trans_t = TRUE() if self.config.simplify: Logger.log("Simplifying the Transition System", 1) if Logger.level(2): timer = Logger.start_timer("Simplify") init = simplify(init) trans = simplify(trans) invar = simplify(invar) if Logger.level(2): Logger.get_timer(timer) n_prop_t = FALSE() init_0 = self.at_time(And(init, invar), 0) Logger.log("Add init and invar", 2) self._add_assertion(solver, init_0) if prove: # add invariants at time 0, but not init self._add_assertion(solver_ind, self.at_time(invar, 0), "invar") next_prop = TS.has_next(prop) if next_prop: if k < 1: Logger.error("Invariant checking with next variables requires at least k=1") k_min = 1 skip_push = False constraints = TRUE() t = k_min for i in range(t): trans_t = self.unroll(trans, invar, i+1, i) self._add_assertion(solver, trans_t) Logger.msg("Unroll and call check-sat without property", 2) # Note: Seems to help a lot to call check-sat here # without this some solvers will run out of memory on large problems # it likely lets them do some internal clean up, and learn some things # about the model self._solve(solver) Logger.msg("_", 0, not(Logger.level(1))) while (t < k+1): if not skip_push: self._push(solver) skip_push = False t_prop = t-1 if next_prop else t if k_min > 0: if (not next_prop) or (next_prop and t>0): if n_prop_t == FALSE(): n_prop_t = self.at_time(Not(prop), t_prop) else: n_prop_t = Or(n_prop_t, self.at_time(Not(prop), t_prop)) else: n_prop_t = self.at_time(Not(prop), t) Logger.log("Add not property at time %d"%t, 2) if not skip_push: self._add_assertion(solver, n_prop_t, "Property") if constraints != TRUE(): self._add_assertion(solver, self.at_time(constraints, t), "Addditional Constraints") if t >= k_min: Logger.log("\nSolving for k=%s"%(t), 1) if self.preferred is not None: try: for (var, val) in self.preferred: for j in range(t+1): solver.solver.set_preferred_var(TS.get_timed(var, j), val) except: Logger.warning("Current solver does not support preferred variables") self.preferred = None if self._solve(solver): Logger.log("Counterexample found with k=%s"%(t), 1) model = self._get_model(solver) if generalize is not None: constr, res = generalize(model, t) if res: return (t, model) constraints = And(constraints, Not(constr)) skip_push = True continue else: return (t, model) else: Logger.log("No counterexample found with k=%s"%(t), 1) Logger.msg(".", 0, not(Logger.level(1))) if add_unsat_cons and prove: self._add_assertion(solver, Implies(self.at_time(And(init, invar), 1), self.at_time(Not(prop), t_prop+1))) self._add_assertion(solver, Not(n_prop_t)) else: Logger.log("\nSkipping solving for k=%s (k_min=%s)"%(t,k_min), 1) Logger.msg("_", 0, not(Logger.level(1))) self._pop(solver) skip_push = False if prove: if t > k_min: loop_free = self.loop_free(relevant_vars, t, t-1) # Checking I & T & loopFree acc_init = And(acc_init, self.at_time(Not(init), t)) acc_loop_free = And(acc_loop_free, loop_free) self._push(solver) self._add_assertion(solver, acc_init) self._add_assertion(solver, acc_loop_free) if self._solve(solver): Logger.log("Induction (I & lF) failed with k=%s"%(t), 1) else: Logger.log("Induction (I & lF) holds with k=%s"%(t), 1) return (t, True) self._pop(solver) # Checking T & loopFree & !P self._add_assertion(solver_ind, trans_t, comment="trans") self._add_assertion(solver_ind, loop_free, comment="loop_free") self._push(solver_ind) self._add_assertion(solver_ind, self.at_time(Not(prop), t_prop)) if self._solve(solver_ind): Logger.log("Induction (lF & !P) failed with k=%s"%(t), 1) else: Logger.log("Induction (lF & !P) holds with k=%s"%(t), 1) return (t, True) self._pop(solver_ind) self._add_assertion(solver_ind, self.at_time(prop, t_prop), "prop") else: if not next_prop: self._add_assertion(solver_ind, self.at_time(prop, t_prop), "prop") else: # add skipped transition self._add_assertion(solver_ind, trans_t, comment="trans") trans_t = self.unroll(trans, invar, t+1, t) self._add_assertion(solver, trans_t) t += 1 return (t-1, None)
def solve_safety_inc_fwd(self, hts, prop, k, k_min, all_vars=False): self._reset_assertions(self.solver) if self.config.prove: self.solver_ind = self.solver.copy("ind") self._reset_assertions(self.solver_ind) if all_vars: relevant_vars = hts.vars else: relevant_vars = hts.state_vars | hts.input_vars | hts.output_vars init = hts.single_init() trans = hts.single_trans() invar = hts.single_invar() acc_init = TRUE() acc_prop = TRUE() acc_loop_free = TRUE() trans_t = TRUE() if self.config.simplify: Logger.log("Simplifying the Transition System", 1) if Logger.level(2): timer = Logger.start_timer("Simplify") init = simplify(init) trans = simplify(trans) invar = simplify(invar) if Logger.level(2): Logger.get_timer(timer) n_prop_t = FALSE() init_0 = self.at_time(And(init, invar), 0) Logger.log("Add init and invar", 2) self._add_assertion(self.solver, init_0) if self.config.prove: # add invariants at time 0, but not init self._add_assertion(self.solver_ind, self.at_time(invar, 0), "invar") next_prop = TS.has_next(prop) if next_prop: if k < 1: Logger.error( "Invariant checking with next variables requires at least k=1" ) k_min = 1 t = 0 while (t < k + 1): self._push(self.solver) t_prop = t - 1 if next_prop else t if k_min > 0: if (not next_prop) or (next_prop and t > 0): n_prop_t = Or(n_prop_t, self.at_time(Not(prop), t_prop)) else: n_prop_t = self.at_time(Not(prop), t) Logger.log("Add not property at time %d" % t, 2) self._add_assertion(self.solver, n_prop_t) if t >= k_min: Logger.log("\nSolving for k=%s" % (t), 1) if self._solve(self.solver): Logger.log("Counterexample found with k=%s" % (t), 1) model = self._get_model(self.solver) return (t, model) else: Logger.log("No counterexample found with k=%s" % (t), 1) Logger.msg(".", 0, not (Logger.level(1))) #self._add_assertion(self.solver, Not(n_prop_t)) else: Logger.log( "\nSkipping solving for k=%s (k_min=%s)" % (t, k_min), 1) Logger.msg(".", 0, not (Logger.level(1))) self._pop(self.solver) if self.config.prove: if t > k_min: loop_free = self.loop_free(relevant_vars, t, t - 1) # Checking I & T & loopFree acc_init = And(acc_init, self.at_time(Not(init), t)) acc_loop_free = And(acc_loop_free, loop_free) self._push(self.solver) self._add_assertion(self.solver, acc_init) self._add_assertion(self.solver, acc_loop_free) if self._solve(self.solver): Logger.log("Induction (I & lF) failed with k=%s" % (t), 1) else: Logger.log("Induction (I & lF) holds with k=%s" % (t), 1) return (t, True) self._pop(self.solver) # Checking T & loopFree & !P self._add_assertion(self.solver_ind, trans_t, comment="trans") self._add_assertion(self.solver_ind, loop_free, comment="loop_free") self._push(self.solver_ind) self._add_assertion(self.solver_ind, self.at_time(Not(prop), t_prop)) if self._solve(self.solver_ind): Logger.log( "Induction (lF & !P) failed with k=%s" % (t), 1) else: Logger.log("Induction (lF & !P) holds with k=%s" % (t), 1) return (t, True) self._pop(self.solver_ind) self._add_assertion(self.solver_ind, self.at_time(prop, t_prop), "prop") else: if not next_prop: self._add_assertion(self.solver_ind, self.at_time(prop, t_prop), "prop") trans_t = self.unroll(trans, invar, t + 1, t) self._add_assertion(self.solver, trans_t) t += 1 return (t - 1, None)
def solve_problems(self, problems_config: ProblemsManager) -> None: general_config = problems_config.general_config model_extension = general_config.model_extension assume_if_true = general_config.assume_if_true self.sparser = StringParser(general_config) self.lparser = LTLParser() self.coi = ConeOfInfluence() modifier = None if general_config.model_extension is not None: modifier = lambda hts: ModelExtension.extend( hts, ModelModifiersFactory.modifier_by_name(general_config. model_extension)) # generate main system system hts, invar_props, ltl_props = self.parse_model( general_config.model_files, problems_config.relative_path, general_config, "System 1", modifier) # Generate second models if any are necessary for problem in problems_config.problems: if problem.verification == VerificationType.EQUIVALENCE: if problem.equal_to is None: raise RuntimeError( "No second model for equivalence " "checking provided for problem {}".format( problem.name)) hts2, _, _ = self.parse_model(problem.equal_to, problems_config.relative_path, general_config, "System 2", modifier) problems_config.add_second_model(problem, hts2) # TODO : contain these types of passes in functions # they should be registered as passes if general_config.init is not None: iparser = InitParser() init_hts, inv_a, ltl_a = iparser.parse_file( general_config.init, general_config) assert inv_a is None and ltl_a is None, "Not expecting assertions from init state file" # remove old inits for ts in hts.tss: ts.init = TRUE() hts.combine(init_hts) hts.single_init(rebuild=True) # set default bit-wise initial values (0 or 1) if general_config.default_initial_value is not None: def_init_val = int(general_config.default_initial_value) try: if int(def_init_val) not in {0, 1}: raise RuntimeError except: raise RuntimeError( "Expecting 0 or 1 for default_initial_value," "but received {}".format(def_init_val)) def_init_ts = TS("Default initial values") new_init = [] initialized_vars = get_free_variables(hts.single_init()) state_vars = hts.state_vars num_def_init_vars = 0 num_state_vars = len(state_vars) const_arr_supported = True if hts.logic == L_ABV: for p in problems_config.problems: if p.solver_name not in CONST_ARRAYS_SUPPORT: const_arr_supported = False Logger.warning( "Using default_initial_value with arrays, " "but one of the selected solvers, " "{} does not support constant arrays. " "Any assumptions on initial array values will " "have to be done manually".format( problem.solver_name)) break for sv in state_vars - initialized_vars: if sv.get_type().is_bv_type(): width = sv.get_type().width if int(def_init_val) == 1: val = BV((2**width) - 1, width) else: val = BV(0, width) num_def_init_vars += 1 elif sv.get_type().is_array_type() and \ sv.get_type().elem_type.is_bv_type() and \ const_arr_supported: svtype = sv.get_type() width = svtype.elem_type.width if int(def_init_val) == 1: val = BV((2**width) - 1, width) else: val = BV(0, width) # create a constant array with a default value val = Array(svtype.index_type, val) else: continue def_init_ts.add_state_var(sv) new_init.append(EqualsOrIff(sv, val)) def_init_ts.set_behavior(simplify(And(new_init)), TRUE(), TRUE()) hts.add_ts(def_init_ts) Logger.msg( "Set {}/{} state elements to zero " "in initial state\n".format(num_def_init_vars, num_state_vars), 1) problems_config.hts = hts # TODO: Update this so that we can control whether embedded assertions are solved automatically if not general_config.skip_embedded: for invar_prop in invar_props: problems_config.add_problem( verification=VerificationType.SAFETY, name=invar_prop[0], description=invar_prop[1], properties=invar_prop[2]) self.properties.append(invar_prop[2]) for ltl_prop in ltl_props: problems_config.add_problem(verification=VerificationType.LTL, name=invar_prop[0], description=invar_prop[1], properties=invar_prop[2]) self.properties.append(ltl_prop[2]) Logger.log( "Solving with abstract_clock=%s, add_clock=%s" % (general_config.abstract_clock, general_config.add_clock), 2) # ensure the miter_out variable exists miter_out = None for problem in problems_config.problems: if problem.name is not None: Logger.log( "\n*** Analyzing problem \"%s\" ***" % (problem.name), 1) Logger.msg("Solving \"%s\" " % problem.name, 0, not (Logger.level(1))) # apply parametric behaviors (such as toggling the clock) # Note: This is supposed to be *before* creating the combined system for equivalence checking # we want this assumption to be applied to both copies of the clock problem_hts = ParametricBehavior.apply_to_problem( problems_config.hts, problem, general_config, self.model_info) if problem.verification == VerificationType.EQUIVALENCE: hts2 = problems_config.get_second_model(problem) problem_hts, miter_out = Miter.combine_systems( hts, hts2, problem.bmc_length, general_config.symbolic_init, problem.properties, True) try: # convert the formulas to PySMT FNodes # lemmas, assumptions and precondition always use the regular parser lemmas, assumptions, precondition = self.convert_formulae( [ problem.lemmas, problem.assumptions, problem.precondition ], parser=self.sparser, relative_path=problems_config.relative_path) if problem.verification != VerificationType.LTL: parser = self.sparser else: parser = self.lparser prop = None if problem.properties is not None: prop = self.convert_formula( problem.properties, relative_path=problems_config.relative_path, parser=parser) assert len(prop) == 1, "Properties should already have been split into " \ "multiple problems but found {} properties here".format(len(prop)) prop = prop[0] self.properties.append(prop) else: if problem.verification == VerificationType.SIMULATION: prop = TRUE() elif (problem.verification is not None) and (problem.verification != VerificationType.EQUIVALENCE): Logger.error( "Property not provided for problem {}".format( problem.name)) if problem.verification == VerificationType.EQUIVALENCE: assert miter_out is not None # set property to be the miter output # if user provided a different equivalence property, this has already # been incorporated in the miter_out prop = miter_out # reset the miter output miter_out = None if precondition: assert len(precondition ) == 1, "There should only be one precondition" prop = Implies(precondition[0], prop) # TODO: keep assumptions separate from the hts # IMPORTANT: CLEAR ANY PREVIOUS ASSUMPTIONS AND LEMMAS # This was previously done in __solve_problem and has been moved here # during the frontend refactor in April 2019 # this is necessary because the problem hts is just a reference to the # overall (shared) HTS problem_hts.assumptions = None problem_hts.lemmas = None # Compute the Cone Of Influence # Returns a *new* hts (not pointing to the original one anymore) if problem.coi: if Logger.level(2): timer = Logger.start_timer("COI") hts = self.coi.compute(hts, prop) if Logger.level(2): Logger.get_timer(timer) if general_config.time: timer_solve = Logger.start_timer( "Problem %s" % problem.name, False) status, trace, traces, region = self.__solve_problem( problem_hts, prop, lemmas, assumptions, problem) # set status for this problem problems_config.set_problem_status(problem, status) # TODO: Determine whether we need both trace and traces assert trace is None or traces is None, "Expecting either a trace or a list of traces" if trace is not None: problem_traces = self.__process_trace( hts, trace, general_config, problem) problems_config.set_problem_traces(problem, problem_traces) if traces is not None: traces_to_add = [] for trace in traces: problem_trace = self.__process_trace( hts, trace, general_config, problem) for pt in problem_trace: traces_to_add.append(pt) problems_config.set_problem_traces(problem, traces_to_add) if problem.verification == VerificationType.PARAMETRIC: assert region is not None problems_config.set_problem_region(problem, region) if status is not None: Logger.msg(" %s\n" % status, 0, not (Logger.level(1))) if (assume_if_true) and \ (status == VerificationStatus.TRUE) and \ (problem.assumptions == None) and \ (problem.verification == VerificationType.SAFETY): # TODO: relax the assumption on problem.assumptions # can still add it, just need to make it an implication ass_ts = TS("Previous assumption from property") if TS.has_next(prop): ass_ts.trans = prop else: ass_ts.invar = prop # add assumptions to main system problem_hts.reset_formulae() problem_hts.add_ts(ass_ts) if general_config.time: problems_config.set_problem_time( problem, Logger.get_timer(timer_solve, False)) except KeyboardInterrupt as e: Logger.msg("\b\b Skipped!\n", 0)
def parse_file(self, strfile, config, flags=None): self.config = config self.__reset_structures() Logger.msg("Reading CoreIR system... ", 1) top_module = self.context.load_from_file(strfile) if config.run_passes: self.run_passes() Modules.abstract_clock = self.config.abstract_clock Modules.symbolic_init = self.config.symbolic_init top_def = top_module.definition interface = list(top_module.type.items()) modules = {} sym_map = {} not_defined_mods = [] hts = HTS(top_module.name) invar_props = [] ltl_props = [] Logger.msg("Starting encoding... ", 1) count = 0 def extract_value(x, modname, inst_intr, inst_conf, inst_mod): if x in inst_intr: return self.BVVar(modname + x, inst_intr[x].size) if x in inst_conf: xval = inst_conf[x].value if type(xval) == bool: xval = 1 if xval else 0 else: if type(xval) != int: try: if xval.is_x(): xval = None else: xval = xval.as_uint() except: try: xval = xval.val except: xval = xval.unsigned_value return xval if inst_mod.generated: inst_args = inst_mod.generator_args if x in inst_args: return inst_args[x].value return None if Logger.level(1): timer = Logger.start_timer("IntConvertion", False) en_tprinting = False if Logger.level(2): ttimer = Logger.start_timer("Convertion", False) if self.config.deterministic: td_instances = top_def.instances top_def_instances = [(inst.selectpath, inst.config, inst.module) for inst in td_instances] top_def_instances.sort() else: top_def_instances = list(top_def.instances) totalinst = len(top_def_instances) for inst in top_def_instances: if Logger.level(1): count += 1 if count % 300 == 0: dtime = Logger.get_timer(timer, False) if dtime > 2: en_tprinting = True if en_tprinting: Logger.inline( "%s" % status_bar( (float(count) / float(totalinst))), 1) timer = Logger.start_timer("IntConvertion", False) if Logger.level(2): Logger.get_timer(timer, False) ts = None if self.config.deterministic: (inst_name, inst_conf, inst_mod) = inst else: inst_name = inst.selectpath inst_conf = inst.config inst_mod = inst.module inst_type = inst_mod.name inst_intr = dict(inst_mod.type.items()) modname = (SEP.join(inst_name)) + SEP values_dic = {} for x in self.attrnames: values_dic[x] = extract_value(x, modname, inst_intr, inst_conf, inst_mod) def args(ports_list): return [values_dic[x] for x in ports_list] sym = self.__mod_to_sym(inst_type, args) if sym is not None: sym_map[sym[0].symbol_name()] = (sym[0], sym[1]) continue ts = self.__mod_to_impl(inst_type, args) if ts is not None: if flags is not None: if CoreIRModelFlags.NO_INIT in flags: ts.init = TRUE() if CoreIRModelFlags.FC_LEMMAS in flags: for v in ts.vars: v_name = v.symbol_name() if (CR in v_name) or (RCR in v_name): cons_v_name = v_name[:len( CR)] if CR in v_name else v_name[:len(RCR)] cons_v = Symbol(cons_v_name, v.symbol_type()) lemma = EqualsOrIff( cons_v, BV(values_dic[self.VALUE], cons_v.symbol_type().width)) hts.add_lemma(lemma) for v in ts.state_vars: lemma = EqualsOrIff( v, BV(values_dic[self.INIT], v.symbol_type().width)) hts.add_lemma(lemma) hts.add_ts(ts) else: if inst_type not in not_defined_mods: intface = ", ".join([ "%s" % (v) for v in values_dic if values_dic[v] is not None ]) Logger.error( "Module type \"%s\" with interface \"%s\" is not defined" % (inst_type, intface)) not_defined_mods.append(inst_type) Logger.clear_inline(1) if self.config.deterministic: interface.sort() for var in interface: varname = SELF + SEP + var[0] bvvar = self.BVVar(varname, var[1].size) if (var[1].is_input()): hts.add_input_var(bvvar) else: hts.add_output_var(bvvar) # Adding clock behavior if (self.CLK in var[0].lower()) and (var[1].is_input()): self.clock_list.add(bvvar) if self.config.abstract_clock: self.abstract_clock_list.add( (bvvar, (BV(0, var[1].size), BV(1, var[1].size)))) varmap = dict([(s.symbol_name(), s) for s in hts.vars]) def split_paths(path): ret = [] for el in path: ret += el.split(CSEP) return ret def dict_select(dic, el): return dic[el] if el in dic else None eq_conns = [] eq_vars = set([]) if self.config.deterministic: td_connections = top_def.connections top_def_connections = [ ((conn.first.selectpath, conn.second.selectpath) if conn.first.selectpath < conn.second.selectpath else (conn.second.selectpath, conn.first.selectpath), conn) for conn in td_connections ] top_def_connections.sort() else: top_def_connections = list(top_def.connections) for conn in top_def_connections: if self.config.deterministic: first_selectpath = split_paths(conn[0][0]) second_selectpath = split_paths(conn[0][1]) else: first_selectpath = split_paths(conn.first.selectpath) second_selectpath = split_paths(conn.second.selectpath) first = SEP.join(first_selectpath) second = SEP.join(second_selectpath) firstvar = None secondvar = None if is_number(first_selectpath[-1]): firstname = SEP.join(first_selectpath[:-1]) else: firstname = SEP.join(first_selectpath) if is_number(second_selectpath[-1]): secondname = SEP.join(second_selectpath[:-1]) else: secondname = SEP.join(second_selectpath) first = (dict_select(varmap, self.remap_or2an(firstname)), None) second = (dict_select(varmap, self.remap_or2an(secondname)), None) firstvar = first[0] secondvar = second[0] if (firstvar is None) and (self.remap_or2an(firstname) in sym_map): firstvar = sym_map[self.remap_or2an(firstname)][1] if (secondvar is None) and (self.remap_or2an(secondname) in sym_map): secondvar = sym_map[self.remap_or2an(secondname)][1] if (firstvar is None) and (secondvar is not None): Logger.error("Symbol \"%s\" is not defined" % firstname) first = (Symbol(self.remap_or2an(firstname), secondvar.symbol_type()), None) else: if firstvar.is_constant(): sel = int(first_selectpath[-1]) if (is_number( first_selectpath[-1])) else None first = (firstvar, sel) else: if (is_number(first_selectpath[-1])) and ( firstvar.symbol_type() != BOOL) and (firstvar.symbol_type().width > 1): sel = int(first_selectpath[-1]) first = (firstvar, sel) if (firstvar is not None) and (secondvar is None): Logger.error("Symbol \"%s\" is not defined" % secondname) second = (Symbol(self.remap_or2an(secondname), firstvar.symbol_type()), None) else: if secondvar.is_constant(): sel = int(second_selectpath[-1]) if (is_number( second_selectpath[-1])) else None second = (secondvar, sel) else: if (is_number(second_selectpath[-1])) and ( secondvar.symbol_type() != BOOL) and (secondvar.symbol_type().width > 1): sel = int(second_selectpath[-1]) second = (secondvar, sel) assert ((firstvar is not None) and (secondvar is not None)) eq_conns.append((first, second)) if firstvar.is_symbol(): eq_vars.add(firstvar) if secondvar.is_symbol(): eq_vars.add(secondvar) conns_len = len(eq_conns) if self.pack_connections: eq_conns = self.__pack_connections(eq_conns) if len(eq_conns) < conns_len: Logger.log("Packed %d connections" % (conns_len - len(eq_conns)), 1) eq_formula = TRUE() for eq_conn in eq_conns: (fst, snd) = eq_conn if fst[1] is None: first = fst[0] else: if len(fst) > 2: first = BVExtract(fst[0], fst[1], fst[2]) else: first = BVExtract(fst[0], fst[1], fst[1]) if snd[1] is None: second = snd[0] else: if len(snd) > 2: second = BVExtract(snd[0], snd[1], snd[2]) else: second = BVExtract(snd[0], snd[1], snd[1]) if (first.get_type() != BOOL) and (second.get_type() == BOOL): second = Ite(second, BV(1, 1), BV(0, 1)) if (first.get_type() == BOOL) and (second.get_type() != BOOL): first = Ite(first, BV(1, 1), BV(0, 1)) eq_formula = And(eq_formula, EqualsOrIff(first, second)) Logger.log(str(EqualsOrIff(first, second)), 3) ts = TS("Connections") ts.invar = eq_formula ts.vars = eq_vars hts.add_ts(ts) if self.enc_map is not None: del (self.enc_map) if Logger.level(2): Logger.get_timer(ttimer) return (hts, invar_props, ltl_props)
def parse_file(self, file_path, config, flags=None): # coreir needs a string representing the path strfile = str(file_path) self.config = config self.__reset_structures() Logger.msg("Reading CoreIR system... ", 1) top_module = self.context.load_from_file(strfile) if config.run_coreir_passes: self.run_passes() Modules.abstract_clock = self.config.abstract_clock Modules.symbolic_init = self.config.symbolic_init top_def = top_module.definition interface = list(top_module.type.items()) modules = {} sym_map = {} not_defined_mods = [] hts = HTS(top_module.name) invar_props = [] ltl_props = [] Logger.msg("Starting encoding... ", 1) count = 0 def extract_value(x, modname, inst_intr, inst_conf, inst_mod): if x in inst_intr: return self.BVVar(modname + x, inst_intr[x].size) if x in inst_conf: xval = inst_conf[x].value if type(xval) == bool: xval = 1 if xval else 0 else: if type(xval) != int: try: xval = xval.as_uint() except: xval = None return xval if inst_mod.generated: inst_args = inst_mod.generator_args if x in inst_args: return inst_args[x].value return None if Logger.level(1): timer = Logger.start_timer("IntConvertion", False) en_tprinting = False if Logger.level(2): ttimer = Logger.start_timer("Convertion", False) td_instances = top_def.instances top_def_instances = [(inst.selectpath, inst.config, inst.module) for inst in td_instances] # sorting keeps the behavior deterministic top_def_instances.sort() totalinst = len(top_def_instances) for inst in top_def_instances: if Logger.level(1): count += 1 if count % 300 == 0: dtime = Logger.get_timer(timer, False) if dtime > 2: en_tprinting = True if en_tprinting: Logger.inline( "%s" % status_bar( (float(count) / float(totalinst))), 1) timer = Logger.start_timer("IntConvertion", False) if Logger.level(2): Logger.get_timer(timer, False) ts = None (inst_name, inst_conf, inst_mod) = inst inst_type = inst_mod.name inst_intr = dict(inst_mod.type.items()) modname = (SEP.join(inst_name)) + SEP values_dic = {} for x in self.attrnames: values_dic[x] = extract_value(x, modname, inst_intr, inst_conf, inst_mod) def args(ports_list): return [values_dic[x] for x in ports_list] sym = self.__mod_to_sym(inst_type, args) if sym is not None: sym_map[sym[0].symbol_name()] = (sym[0], sym[1]) continue ts = self.__mod_to_impl(inst_type, args) if ts is not None: if flags is not None: if CoreIRModelFlags.NO_INIT in flags: ts.init = TRUE() if CoreIRModelFlags.FC_LEMMAS in flags: for v in ts.vars: v_name = v.symbol_name() if (CR in v_name) or (RCR in v_name): cons_v_name = v_name[:len( CR)] if CR in v_name else v_name[:len(RCR)] cons_v = Symbol(cons_v_name, v.symbol_type()) lemma = EqualsOrIff( cons_v, BV(values_dic[self.VALUE], cons_v.symbol_type().width)) hts.add_lemma(lemma) for v in ts.state_vars: lemma = EqualsOrIff( v, BV(values_dic[self.INIT], v.symbol_type().width)) hts.add_lemma(lemma) hts.add_ts(ts) else: if inst_type not in not_defined_mods: intface = ", ".join([ "%s" % (v) for v in values_dic if values_dic[v] is not None ]) Logger.error( "Module type \"%s\" with interface \"%s\" is not defined" % (inst_type, intface)) not_defined_mods.append(inst_type) Logger.clear_inline(1) # sorting keeps the behavior deterministic interface.sort() for var in interface: varname = SELF + SEP + var[0] bvvar = self.BVVar(varname, var[1].size) if (var[1].is_input()): hts.add_input_var(bvvar) else: hts.add_output_var(bvvar) if var[1].kind == NAMED and var[1].name == COREIR_CLK: self.clock_list.add(bvvar) if self.config.abstract_clock: self.abstract_clock_list.add( (bvvar, (BV(0, var[1].size), BV(1, var[1].size)))) else: # add state variable that stores the previous clock value # This is IMPORTANT for model checking soundness, but # it isn't obvious that this is necessary # # imagine we have an explicit clock encoding (not abstract_clock), e.g. # next(state_var) = (!clk & next(clk)) ? <state_update> : <old value> # and if we're trying to prove something using k-induction, there's a "loop free" # constraint that the state and output variables don't repeat (reach the same # state twice) in the trace # but on a negedge clock, there can be scenarios where no state or outputs # can be updated and we'll get a trivial unsat which will be interpreted as # a converged proof -- uh oh # # adding this state element just ensures that the loop free constraint won't # be violated trivially # e.g. on a neg-edge clock, this new state element will have changed # make it hidden (won't be printed) # HIDDEN_VAR is a prefix that printers check for trailing_clock_var = self.BVVar( "{}{}__prev".format(HIDDEN_VAR, varname), var[1].size) ts = TS() ts.add_state_var(trailing_clock_var) # the initial state for this trailing variable is unconstrained ts.set_behavior( TRUE(), EqualsOrIff(TS.get_prime(trailing_clock_var), bvvar), TRUE()) hts.add_ts(ts) varmap = dict([(s.symbol_name(), s) for s in hts.vars]) def split_paths(path): ret = [] for el in path: ret += el.split(CSEP) return ret def dict_select(dic, el): return dic[el] if el in dic else None eq_conns = [] eq_vars = set([]) td_connections = top_def.connections top_def_connections = [ ((conn.first.selectpath, conn.second.selectpath) if conn.first.selectpath < conn.second.selectpath else (conn.second.selectpath, conn.first.selectpath), conn) for conn in td_connections ] # sorting keeps the behavior deterministic top_def_connections.sort() for conn in top_def_connections: first_selectpath = split_paths(conn[0][0]) second_selectpath = split_paths(conn[0][1]) first = SEP.join(first_selectpath) second = SEP.join(second_selectpath) firstvar = None secondvar = None if is_number(first_selectpath[-1]): firstname = SEP.join(first_selectpath[:-1]) else: firstname = SEP.join(first_selectpath) if is_number(second_selectpath[-1]): secondname = SEP.join(second_selectpath[:-1]) else: secondname = SEP.join(second_selectpath) first = (dict_select(varmap, self.remap_or2an(firstname)), None) second = (dict_select(varmap, self.remap_or2an(secondname)), None) firstvar = first[0] secondvar = second[0] if (firstvar is None) and (self.remap_or2an(firstname) in sym_map): firstvar = sym_map[self.remap_or2an(firstname)][1] if (secondvar is None) and (self.remap_or2an(secondname) in sym_map): secondvar = sym_map[self.remap_or2an(secondname)][1] if (firstvar is None) and (secondvar is not None): Logger.error("Symbol \"%s\" is not defined" % firstname) first = (Symbol(self.remap_or2an(firstname), secondvar.symbol_type()), None) else: if firstvar.is_constant(): sel = int(first_selectpath[-1]) if (is_number( first_selectpath[-1])) else None first = (firstvar, sel) else: if (is_number(first_selectpath[-1])) and ( firstvar.symbol_type() != BOOL) and (firstvar.symbol_type().width > 1): sel = int(first_selectpath[-1]) first = (firstvar, sel) if (firstvar is not None) and (secondvar is None): Logger.error("Symbol \"%s\" is not defined" % secondname) second = (Symbol(self.remap_or2an(secondname), firstvar.symbol_type()), None) else: if secondvar.is_constant(): sel = int(second_selectpath[-1]) if (is_number( second_selectpath[-1])) else None second = (secondvar, sel) else: if (is_number(second_selectpath[-1])) and ( secondvar.symbol_type() != BOOL) and (secondvar.symbol_type().width > 1): sel = int(second_selectpath[-1]) second = (secondvar, sel) assert ((firstvar is not None) and (secondvar is not None)) eq_conns.append((first, second)) if firstvar.is_symbol(): eq_vars.add(firstvar) if secondvar.is_symbol(): eq_vars.add(secondvar) conns_len = len(eq_conns) if self.pack_connections: eq_conns = self.__pack_connections(eq_conns) if len(eq_conns) < conns_len: Logger.log("Packed %d connections" % (conns_len - len(eq_conns)), 1) eq_formula = TRUE() for eq_conn in eq_conns: (fst, snd) = eq_conn if fst[1] is None: first = fst[0] else: if len(fst) > 2: first = BVExtract(fst[0], fst[1], fst[2]) else: first = BVExtract(fst[0], fst[1], fst[1]) if snd[1] is None: second = snd[0] else: if len(snd) > 2: second = BVExtract(snd[0], snd[1], snd[2]) else: second = BVExtract(snd[0], snd[1], snd[1]) if (first.get_type() != BOOL) and (second.get_type() == BOOL): second = Ite(second, BV(1, 1), BV(0, 1)) if (first.get_type() == BOOL) and (second.get_type() != BOOL): first = Ite(first, BV(1, 1), BV(0, 1)) eq_formula = And(eq_formula, EqualsOrIff(first, second)) Logger.log(str(EqualsOrIff(first, second)), 3) ts = TS("Connections") ts.invar = eq_formula ts.vars = eq_vars hts.add_ts(ts) if self.enc_map is not None: del (self.enc_map) if Logger.level(2): Logger.get_timer(ttimer) # check that clocks were detected if there's any state if hts.state_vars: assert self.clock_list, "Expecting clocks if there are state variables" return (hts, invar_props, ltl_props)