/** * Computes the next level from src according to the strategy in result. */ static inline void compute_strategy_level(vset_t strategy_level, const int* src, const int player, const recursive_result* strategy) { // Initialise strategy level to the empty set vset_clear(strategy_level); // Find the right strategy level. For each level that contains src, there is either a transition // to the same level, or (preferrably) to a lower level. for (int i=0; i < strategy->strategy_boundary_count[player]; i++) { int begin = (i==0) ? 0 : strategy->strategy_boundaries[player][i-1]; int end = strategy->strategy_boundaries[player][i]; //printf("considering segment %d (begin=%d, end=%d).\n", i, begin, end); //assert(first <= last); assert(strategy->strategy_boundaries[player][i]<=strategy->strategy_levels_count[player]); bool src_found = false; for (int j = begin; j < end && !src_found; j++) { if (vset_member(strategy->strategy_levels[player][j], src)) { if (j==begin) { vset_union(strategy_level, strategy->strategy_levels[player][j]); //printf("choosing move from segment %d, moving from level %d to level %d.\n", i, j, j); } else { vset_union(strategy_level, strategy->strategy_levels[player][j-1]); //printf("choosing move from segment %d, moving from level %d to level %d.\n", i, j, j-1); } assert(!vset_is_empty(strategy_level)); src_found = true; } } } // strategy_level now contains any target states that can be reached from src // in one step and are conform the strategy. }
void reach_sat_fix(reach_proc_t reach_proc, vset_t visited, bitvector_t *reach_groups, long *eg_count, long *next_count, long *guard_count) { (void) reach_proc; (void) guard_count; if (PINS_USE_GUARDS) Abort("guard-splitting not supported with saturation=sat-fix"); int level = 0; vset_t old_vis = vset_create(domain, -1, NULL); vset_t deadlocks = dlk_detect?vset_create(domain, -1, NULL):NULL; vset_t dlk_temp = dlk_detect?vset_create(domain, -1, NULL):NULL; LACE_ME; while (!vset_equal(visited, old_vis)) { if (trc_output != NULL) save_level(visited); vset_copy(old_vis, visited); stats_and_progress_report(NULL, visited, level); level++; for(int i = 0; i < nGrps; i++){ if (!bitvector_is_set(reach_groups, i)) continue; expand_group_next(i, visited); reach_chain_stop(); (*eg_count)++; } if (dlk_detect) vset_copy(deadlocks, visited); if (USE_PARALLELISM) vset_least_fixpoint_par(visited, visited, group_next, nGrps); else vset_least_fixpoint(visited, visited, group_next, nGrps); (*next_count)++; check_invariants(visited, level); if (dlk_detect) { for (int i = 0; i < nGrps; i++) { vset_prev(dlk_temp, visited, group_next[i],deadlocks); reduce(i, dlk_temp); vset_minus(deadlocks, dlk_temp); vset_clear(dlk_temp); } deadlock_check(deadlocks, reach_groups); } vset_reorder(domain); } vset_destroy(old_vis); if (dlk_detect) { vset_destroy(deadlocks); vset_destroy(dlk_temp); } }
void reach_sat(reach_proc_t reach_proc, vset_t visited, bitvector_t *reach_groups, long *eg_count, long *next_count, long *guard_count) { (void) reach_proc; (void) next_count; (void) guard_count; if (PINS_USE_GUARDS) Abort("guard-splitting not supported with saturation=sat"); if (act_detect != NULL && trc_output != NULL) Abort("Action detection with trace generation not supported"); for (int i = 0; i < nGrps; i++) { if (bitvector_is_set(reach_groups, i)) { struct expand_info *ctx = RTmalloc(sizeof(struct expand_info)); ctx->group = i; ctx->group_explored = group_explored[i]; ctx->eg_count = eg_count; vrel_set_expand(group_next[i], expand_group_next_projected, ctx); } } if (trc_output != NULL) save_level(visited); stats_and_progress_report(NULL, visited, 0); if (USE_PARALLELISM) vset_least_fixpoint_par(visited, visited, group_next, nGrps); else vset_least_fixpoint(visited, visited, group_next, nGrps); stats_and_progress_report(NULL, visited, 1); check_invariants(visited, -1); if (dlk_detect) { vset_t deadlocks = vset_create(domain, -1, NULL); vset_t dlk_temp = vset_create(domain, -1, NULL); vset_copy(deadlocks, visited); for (int i = 0; i < nGrps; i++) { vset_prev(dlk_temp, visited, group_next[i],deadlocks); reduce(i, dlk_temp); vset_minus(deadlocks, dlk_temp); vset_clear(dlk_temp); } deadlock_check(deadlocks, reach_groups); vset_destroy(deadlocks); vset_destroy(dlk_temp); } }
int main(int argc, char *argv[]){ char *files[2]; RTinitPopt(&argc,&argv,options,1,2,files,NULL,"<model> [<lts>]", "Perform an enumerative reachability analysis of <model>\n" "Run the TorX remote procedure call protocol on <model> (--torx).\n\n" "Options"); if (files[1]) { Warning(info,"Writing output to %s",files[1]); write_lts=1; } else { Warning(info,"No output, just counting the number of states"); write_lts=0; } if (application==RunTorX && write_lts) Fatal(1,error,"A TorX server does not write to a file"); Warning(info,"loading model from %s",files[0]); model_t model=GBcreateBase(); GBsetChunkMethods(model,new_string_index,NULL, (int2chunk_t)SIgetC,(chunk2int_t)SIputC,(get_count_t)SIgetCount); GBloadFile(model,files[0],&model); if (RTverbosity >=2) { fprintf(stderr,"Dependency Matrix:\n"); GBprintDependencyMatrix(stderr,model); } if (matrix) { GBprintDependencyMatrix(stdout,model); exit(0); } lts_type_t ltstype=GBgetLTStype(model); N=lts_type_get_state_length(ltstype); edge_info_t e_info=GBgetEdgeInfo(model); K=e_info->groups; Warning(info,"length is %d, there are %d groups",N,K); state_labels=lts_type_get_state_label_count(ltstype); edge_labels=lts_type_get_edge_label_count(ltstype); Warning(info,"There are %d state labels and %d edge labels",state_labels,edge_labels); if (state_labels&&write_lts&&!write_state) { Fatal(1,error,"Writing state labels, but not state vectors unsupported. " "Writing of state vector is enabled with the option --write-state"); } int src[N]; GBgetInitialState(model,src); Warning(info,"got initial state"); int level=0; switch(application){ case ReachVset: domain=vdom_create_default(N); visited_set=vset_create(domain,0,NULL); next_set=vset_create(domain,0,NULL); if (write_lts){ output=lts_output_open(files[1],model,1,0,1,"viv",NULL); lts_output_set_root_vec(output,(uint32_t*)src); lts_output_set_root_idx(output,0,0); output_handle=lts_output_begin(output,0,0,0); } vset_add(visited_set,src); vset_add(next_set,src); vset_t current_set=vset_create(domain,0,NULL); while (!vset_is_empty(next_set)){ if (RTverbosity >= 1) Warning(info,"level %d has %d states, explored %d states %d trans", level,(visited-explored),explored,trans); level++; vset_copy(current_set,next_set); vset_clear(next_set); vset_enum(current_set,explore_state_vector,model); } long long size; long nodes; vset_count(visited_set,&nodes,&size); Warning(info,"%lld reachable states represented symbolically with %ld nodes",size,nodes); break; case ReachTreeDBS: dbs=TreeDBScreate(N); if(TreeFold(dbs,src)!=0){ Fatal(1,error,"expected 0"); } if (write_lts){ output=lts_output_open(files[1],model,1,0,1,write_state?"vsi":"-ii",NULL); if (write_state) lts_output_set_root_vec(output,(uint32_t*)src); lts_output_set_root_idx(output,0,0); output_handle=lts_output_begin(output,0,0,0); } int limit=visited; while(explored<visited){ if (limit==explored){ if (RTverbosity >= 1) Warning(info,"level %d has %d states, explored %d states %d trans", level,(visited-explored),explored,trans); limit=visited; level++; } TreeUnfold(dbs,explored,src); explore_state_index(model,explored,src); } break; case RunTorX: { torx_struct_t context = { model, ltstype }; torx_ui(&context); return 0; } } if (write_lts){ lts_output_end(output,output_handle); Warning(info,"finishing the writing"); lts_output_close(&output); Warning(info,"state space has %d levels %d states %d transitions",level,visited,trans); } else { printf("state space has %d levels %d states %d transitions\n",level,visited,trans); } return 0; }
int choose_strategy_move(const parity_game* g, const recursive_result* strategy, const int player, const int* src, int* current_player, bool* strategy_play, bool* result, bool* deadlock, int* dst) { int group = -1; vset_t level = vset_create(g->domain, -1, NULL); vset_t tmp = vset_create(g->domain, -1, NULL); vset_t singleton = vset_create(g->domain, -1, NULL); vset_add(singleton, src); // Do a random run through the game, conforming to // the strategy in result. *current_player = (vset_member(g->v_player[player], src) ? player : 1-player); Print(hre_debug, "Using strategy of player %d. The position is owned by player %d.", player, *current_player); if (*current_player==player && vset_member(strategy->win[player], src)) { //printf("Player %d chooses according to his/her winning strategy.\n", player); // Choose a transition according to the strategy of player *strategy_play = true; vset_t strategy_level = vset_create(g->domain, -1, NULL); compute_strategy_level(strategy_level, src, player, strategy); vset_clear(level); for (int i=0; i < g->num_groups; i++) { vset_next(tmp, singleton, g->e[i]); vset_intersect(tmp, strategy_level); if (!vset_is_empty(tmp)) { vset_copy(level, tmp); // Choose a random element: //printf("Choosing a transition from transition group %d.\n", i); group = i; vset_random(level, dst); break; } } // Check for deadlocks if (vset_is_empty(level)) { *deadlock = true; *result = (*current_player != player); Print(infoLong, "Deadlock for player %d, player %s has won, result is %s.", *current_player, (*current_player==1) ? "0 (even / or)" : "1 (odd / and)", *result ? "true" : "false"); } } else { //Print(info, "Not player %d's turn or player %d has no winning strategy. Choosing an arbitrary move for player %d.", // player, player, *current_player); // this states belongs to (1-player) or player does not have a winning strategy *strategy_play = false; vset_t strategy_level = vset_create(g->domain, -1, NULL); /* if (vset_member(result.win[current_player], src)) { // winning move after all compute_strategy_level(strategy_level, src, current_player, result); if (vset_is_empty(strategy_level)) { Print(info, "Unexpected: src is in win[%d], but strategy_level is empty.", current_player); } else { strategy_play = true; } } */ // choose a random move vset_clear(level); for (int i=0; i < g->num_groups; i++) { vset_next(tmp, singleton, g->e[i]); if (*strategy_play && !vset_is_empty(tmp)) { vset_intersect(tmp, strategy_level); if (!vset_is_empty(tmp)) { vset_copy(level, tmp); // Choose a random element: //printf("Choosing a transition from transition group %d.\n", i); group = i; vset_random(level, dst); break; } } else { vset_union(level, tmp); } } // Check for deadlocks if (vset_is_empty(level)) { *deadlock = true; *result = (*current_player != player); Print(infoLong, "Deadlock for player %d, player %s has won, result is %s.", *current_player, (*current_player==1) ? "0 (even / or)" : "1 (odd / and)", *result ? "true" : "false"); } else if (!*strategy_play) { //Print(info, "choose randomly"); vset_random(level, dst); } } return group; }