def test_simple_model_checking0(self): path = "examples/rpython_performance/Lift2.mch" if os.name=='nt': path="examples\rpython_performance\Lift2" ast_string = file_to_AST_str(path) root = str_ast_to_python_ast(ast_string) # Test env = Environment() mch = parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend env._max_int = 2**31 solution_file_read = False bstates = set_up_constants(root, env, mch, solution_file_read) assert len(bstates)==0 # no setup possible bstates = exec_initialisation(root, env, mch, solution_file_read) assert len(bstates)==1 # only one possibility (floor:=4) assert len(env.state_space.seen_states)==0 assert isinstance(bstates[0], BState) env.state_space.set_current_state(bstates[0]) assert len(env.state_space.seen_states)==1 invatiant = root.children[2] assert isinstance(invatiant, AInvariantMachineClause) assert interpret(invatiant, env) assert len(env.state_space.stack)==2 next_states = calc_next_states(env, mch) assert len(next_states)==2 assert len(env.state_space.stack)==2 # init and empty setup assert env.get_value('floor')==4 env.state_space.undo() assert len(env.state_space.stack)==1 # setup assert len(env.state_space.seen_states)==1 for n_state in next_states: bstate = n_state.bstate assert isinstance(bstate, BState) if not env.state_space.is_seen_state(bstate): env.state_space.set_current_state(bstate) assert len(env.state_space.stack)==3 # dec, inc, setup assert len(env.state_space.seen_states)==3 assert env.get_value('floor')==3 or env.get_value('floor')==5 # TODO: Bstate needs refactoring. # - Remove init state # - dont map None to values if parsing unit is no machine # - check empty on stack length 0 # model checking loop while not env.state_space.empty(): assert interpret(invatiant, env) next_states = calc_next_states(env, mch) env.state_space.undo() for n_state in next_states: bstate = n_state.bstate if not env.state_space.is_seen_state(bstate): env.state_space.set_current_state(bstate) assert len(env.state_space.seen_states)==100
def test_simple_model_checking2(self): path = "examples/rpython_performance/SigmaLoop.mch" if os.name=='nt': path="examples/rpython_performance\SigmaLoop" ast_string = file_to_AST_str(path) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._max_int = 2**31 mch = parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend solution_file_read = False bstates = set_up_constants(root, env, mch, solution_file_read) assert len(bstates)==0 # no setup possible bstates = exec_initialisation(root, env, mch, solution_file_read) assert len(bstates)==1 # only one possibility (sum:=45) assert len(env.state_space.seen_states)==0 assert isinstance(bstates[0], BState) env.state_space.set_current_state(bstates[0]) assert len(env.state_space.seen_states)==1 invatiant = root.children[2] assert isinstance(invatiant, AInvariantMachineClause) assert interpret(invatiant, env) assert len(env.state_space.stack)==2 next_states = calc_next_states(env, mch) assert len(next_states)==1 assert len(env.state_space.stack)==2 # init and empty setup assert env.get_value('sum')==55 env.state_space.set_current_state(next_states[0].bstate) assert env.get_value('sum')==55
def _run_model_checking_mode(env, mch): inv = mch.aInvariantMachineClause s_space = env.state_space while not env.state_space.empty(): # FIXME: dirty fix to avoid invariant checking of set up states if env.state_space.get_state().opName=="set up": env.state_space.undo() continue jitdriver.jit_merge_point(inv=inv, s_space=s_space, env=env, mch=mch) if not DISABLE_INVARIANT_MC_CHECK: w_bool = inv.eval(env) # 7. model check if not w_bool.bvalue: print "WARNING: invariant violation found after checking", len(env.state_space.seen_states),"states" violation = env.state_space.get_state() violation.print_bstate() #print env.state_space.history return -1 if EVAL_CHILD_INVARIANT: bstate = env.state_space.get_state() for bmachine in bstate.bmch_list: if not bmachine is None and not bmachine.mch_name==mch.mch_name : w_bool = interpret(bmachine.aInvariantMachineClause, env) if not w_bool.bvalue: print "WARNING: invariant violation in",bmachine.mch_name ," found after checking", len(env.state_space.seen_states),"states" return False next_states = calc_next_states(env, mch) s_space.undo() schedule_new_states(next_states, s_space) print "checked",len(s_space.seen_states),"states.\033[1m\033[92m No invariant violation found.\033[00m" if DISABLE_INVARIANT_MC_CHECK: print "invariant check was disabled" return 0
def __calc_states_and_print_ui(root, env, mch, solution_file_read): # Schneider Book page 62-64: # The parameters p make the constraints C True # #p.C # Sets St and constants k which meet the constraints c make the properties B True # C => #St,k.B if not env.set_up_done: next_states = set_up_constants(root, env, mch, solution_file_read) if len(next_states)>0: print_set_up_bstates(next_states, mch) return next_states else: # no bstates and no exception: set up to do (e.g no constants) env.set_up_done = True # If C and B is True there should be Variables v which make the Invaraiant I True # B & C => #v.I if not env.init_done: next_states = exec_initialisation(root, env, mch, solution_file_read) if len(next_states)>0: undo_possible = not env.state_space.empty() print_init_bstates(next_states, mch, undo_possible) return next_states else: # no bstates and no exception: no init to do (e.g no variables) env.init_done = True print mch.mch_name," - Invariant:", eval_Invariant(root, env, mch) # TODO: move print to animation_clui if EVAL_CHILD_INVARIANT: bstate = env.state_space.get_state() for bmachine in bstate.bmch_list: if not bmachine is None and not bmachine.mch_name==mch.mch_name : print bmachine.mch_name, " - Invariant:", interpret(bmachine.aInvariantMachineClause, env) bstate_lst = calc_next_states(env, mch) show_ui(env, mch, bstate_lst) next_states = [] for n in bstate_lst: # all other data inside bstate_lst has already been processed by show_ui next_states.append(n.bstate) return next_states
def test_bool_law(self): path = "examples/BoolLaws.mch" if os.name=='nt': path="examples\BoolLaws.mch" ast_string = file_to_AST_str(path) root = str_ast_to_python_ast(ast_string) # Test env = Environment() mch = remove_defs_and_parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend env._max_int = 2**31 solution_file_read = False bstates = set_up_constants(root, env, mch, solution_file_read) assert len(bstates)==1 # only one possibility assert isinstance(bstates[0], BState) env.state_space.set_current_state(bstates[0]) bstates = exec_initialisation(root, env, mch, solution_file_read) assert len(bstates)==1 # only one possibility assert len(env.state_space.seen_states)==1 assert isinstance(bstates[0], BState) env.state_space.set_current_state(bstates[0]) assert len(env.state_space.seen_states)==2 invatiant = root.children[5] assert isinstance(invatiant, AInvariantMachineClause) assert interpret(invatiant, env) assert len(env.state_space.stack)==3 # init and setup and ref state next_states = calc_next_states(env, mch) assert len(next_states)==3 while not env.state_space.empty(): assert interpret(invatiant, env) next_states = calc_next_states(env, mch) env.state_space.undo() for n_state in next_states: bstate = n_state.bstate if not env.state_space.is_seen_state(bstate): env.state_space.set_current_state(bstate) if env.state_space.get_state().opName=="set up": assert len(env.state_space.stack)==2 # setup and ref state NOT init env.state_space.undo() #negative exponent bug: (x>0 => y**x=C) crashes # inconsistence between variables and constants """ MACHINE Test VARIABLES x INVARIANT x>0 => 10**x = 10 & -3>0 => 10**(-3) = 10 INITIALISATION x:=-3 END def test_arith_law(self): path = "examples/ArithmeticLaws.mch" if os.name=='nt': path="examples\ArithmeticLaws.mch" ast_string = file_to_AST_str(path) root = str_ast_to_python_ast(ast_string) # Test env = Environment() mch = remove_defs_and_parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend env._max_int = 2**31 solution_file_read = False bstates = set_up_constants(root, env, mch, solution_file_read) assert len(bstates)==0 # no setup possible bstates = exec_initialisation(root, env, mch, solution_file_read) assert len(bstates)==1 # only one possibility assert len(env.state_space.seen_states)==0 assert isinstance(bstates[0], BState) env.state_space.set_current_state(bstates[0]) assert len(env.state_space.seen_states)==1 invatiant = root.children[3] assert isinstance(invatiant, AInvariantMachineClause) assert interpret(invatiant, env) assert len(env.state_space.stack)==2 next_states = calc_next_states(env, mch) """
def run_model_checking_mode(arguments): print "WARNING: model checking still experimental" root, env, parse_object, solution_file_present = startup(arguments) if not isinstance(parse_object, BMachine): print "Error: only model checking of b machines" return assert isinstance(parse_object, BMachine) # 6. typecheck type_check_root_bmch(root, env, parse_object) # also checks all included, seen, used and extend mch = parse_object bstates = set_up_constants(root, env, mch, solution_file_read=False) # also evals properties # TODO: implement setup and init non determinism if len(bstates)==1: env.state_space.set_current_state(bstates[0]) elif len(bstates)>1: print "WARNING: non det. set up constants not supported yet" return bstates = exec_initialisation(root, env, mch, solution_file_read=False) for bstate in bstates: if not env.state_space.is_seen_state(bstate): env.state_space.set_current_state(bstate) #if not len(bstates)==1: # print "WARNING: only one init. expected" # print "real init number:", len(bstates) # return if not mch.has_invariant_mc: print "WARNING: no invariant present" return #env.state_space.set_current_state(bstates[0]) while not env.state_space.empty(): # 7. model check # FIXME: dirty fix to avoid invariant checking of set up states if env.state_space.get_state().opName=="set up": env.state_space.undo() continue if not DISABLE_INVARIANT_MC_CHECK: if not interpret(mch.aInvariantMachineClause, env): print "WARNING: invariant violation found after checking", len(env.state_space.seen_states),"states" violation = env.state_space.get_state() violation.print_bstate() #print violation.opName return False if EVAL_CHILD_INVARIANT: bstate = env.state_space.get_state() for bmachine in bstate.bmch_list: if not bmachine is None and not bmachine.mch_name==mch.mch_name : if not interpret(bmachine.aInvariantMachineClause, env): print "WARNING: invariant violation in",bmachine.mch_name ," found after checking", len(env.state_space.seen_states),"states" return False next_states = calc_next_states(env, mch) env.state_space.undo() for s in next_states: bstate = s.bstate #print s.opName #bstate.print_bstate() # TODO: check double values with Lift2.mch example if not env.state_space.is_seen_state(bstate): env.state_space.set_current_state(bstate) print "checked",len(env.state_space.seen_states),"states.\033[1m\033[92mNo invariant violation found.\033[00m" if DISABLE_INVARIANT_MC_CHECK: print "invariant check was disabled" return True