Esempio n. 1
0
void modelchecker_smvt::read_counterexample(
    const std::list<std::string> &file,
    std::list<std::string>::const_iterator it,
    abstract_modelt &abstract_model,
    const threadst &threads,
    abstract_counterexamplet &counterexample)
{
  while(it!=file.end() && 
      (std::string(*it, 0, 1)=="*" ||
       std::string(*it, 0, 1)=="-" ||
       std::string(*it, 0, 5)=="Trace"))
    it++;

  abstract_statet abstract_state;

  bool data_set=false;

  std::vector<smv_ce_thread_infot> thread_infos;
  thread_infos.resize(threads.size());

  {
    unsigned i=0;
    for(threadst::const_iterator t_it=threads.begin();
        t_it!=threads.end();
        t_it++, i++)
      thread_infos[i].build(inlined, *t_it);
  }

  for(; it!=file.end(); it++)
  {
    //std::cout << "Xx " << *it << "\n";

    if(std::string(*it, 0, 3)=="-- ")
      break;
    else if(*it=="resources used:")
      break;
    else if(std::string(*it, 0, 6)=="state " ||
        std::string(*it, 0, 10)=="-> State: " ||
        std::string(*it, 0, 10)=="-> Input: " ||
        *it=="")
    {
      if(!data_set) continue;

      read_counterexample_store(
          abstract_model, counterexample, thread_infos, abstract_state);

      data_set=false;
    }
    else
    {
      std::string::size_type p=it->find('=');

      if(p==std::string::npos)
        throw "unexpected line in counterexample: "+*it;

      std::string original_variable(*it, 0, p-1);
      std::string value(*it, p+2, std::string::npos);

      while(!original_variable.empty() &&
          original_variable[0]==' ')
        original_variable.erase(0, 1);

      std::string variable=original_variable;

      unsigned thread_nr=0;
      bool thread_local=false;

      if(variable.empty())
        throw "failed to get variable name";
      else if(variable[0]=='t') // checked for emptyness above
      {
        thread_local=true;
        thread_nr=safe_str2unsigned(variable.c_str()+1);

        std::string::size_type q=original_variable.find('.');

        if(q==std::string::npos)
          throw "unexpected line in counterexample: "+*it;

        variable=std::string(original_variable, q+1, std::string::npos);

        if(variable.empty())
          throw "failed to get sub-variable name from "+original_variable;
      }

      if(variable=="PC")
      {
        thread_infos[thread_nr].PC=safe_str2unsigned(value.c_str());
        data_set=true;
      }
      else if(variable=="runs")
      {
        thread_infos[thread_nr].runs=ce_boolean(value);
        data_set=true;
      }
      else if(has_prefix(variable, "b"))
      {
        unsigned nr=safe_str2unsigned(variable.c_str()+1);
        if(nr>=abstract_model.variables.size())
          throw "invalid variable in abstract counterexample: "+
            variable;

        for(unsigned tc=0; tc < threads.size(); ++tc)
        {
          if(thread_local &&
              !abstract_model.variables[nr].is_shared_global() &&
              tc!=thread_nr)
            continue;

          abstract_stept::thread_to_predicate_valuest::iterator it2 =
            abstract_state.thread_states.insert(
                std::make_pair(tc, abstract_stept::predicate_valuest(abstract_model.variables.size(), false))).first;

          it2->second[nr]=ce_boolean(value);
        }
        data_set=true;
      }
      else if(has_prefix(variable, "guard"))
      {
        unsigned nr=safe_str2unsigned(variable.c_str()+5);
        if(nr>=thread_infos[thread_nr].guards.size())
          throw "invalid variable in abstract counterexample: "+
            variable;

        thread_infos[thread_nr].guards[nr]=ce_boolean(value);
      }
      else if(has_prefix(variable, "nondet"))
      {
      }
      else if(has_prefix(variable, "start_thread_"))
      {
      }
      else if(variable=="end_thread")
      {
      }
      else if(variable=="started")
      {
      }
      else if(has_prefix(variable, "po_"))
      {
      }
      else if(variable=="atomic")
      {
      }
      else
        throw "unknown variable in abstract counterexample: "+
          original_variable+" (stripped: `"+variable+"')";
    }
Esempio n. 2
0
void modelchecker_smvt::read_counterexample_cadence_smv(
  const std::list<std::string> &file,
  std::list<std::string>::const_iterator it,
  abstract_modelt &abstract_model,
  const threadst &threads,
  abstract_counterexamplet &counterexample)
{
  while(it!=file.end() && *it!="{")
    it++;

  if(it==file.end())
    throw "unexpected end of counterexample";

  it++;

  std::vector<smv_ce_thread_infot> thread_infos;
  thread_infos.resize(threads.size());

  {
    unsigned i=0;
    for(threadst::const_iterator t_it=threads.begin();
        t_it!=threads.end();
        t_it++, i++)
      thread_infos[i].build(inlined, *t_it);
  }

  for(; it!=file.end(); it++)
  {
    if(*it=="}")
      break; // done with the trace

    if(std::string(*it, 0, 8)!="/* state")
      throw "expected state in counterexample, but got "+*it;
      
    abstract_statet abstract_state;
    abstract_state.predicate_values.resize(
      abstract_model.variables.size());

    it++;
    if(it==file.end())
      throw "unexpected end of counterexample";

    for(; it!=file.end(); it++)
    {
      if(std::string(*it, 0, 1)=="#")
      {
        // ignore
      }
      else if(*it=="}")
      {
        // done with the state
        read_counterexample_store(
          abstract_model, counterexample, thread_infos, abstract_state);
        break;
      }
      else
      {
        std::string::size_type p=it->find('=');

        if(p==std::string::npos)
          throw "unexpected line in counterexample: "+*it;

        std::string original_variable(*it, 0, p-1);
        std::string value(*it, p+2, std::string::npos);

        while(!original_variable.empty() &&
              original_variable[0]==' ')
          original_variable.erase(0, 1);

        while(!original_variable.empty() &&
              original_variable[original_variable.size()-1]==' ')
          original_variable.erase(original_variable.size()-1, std::string::npos);

        std::string variable=original_variable;

        if(!variable.empty() && variable[0]=='\\')
          variable.erase(0, 1);

        unsigned thread_nr=0;

        if(variable.empty())
          throw "failed to get variable name";
        else if(variable[0]=='t') // checked for emptyness above
        {
          thread_nr=atoi(variable.c_str()+1);

          std::string::size_type q=original_variable.find('.');

          if(q==std::string::npos)
            throw "unexpected line in counterexample: "+*it;

          variable=std::string(original_variable, q+1, std::string::npos);

          if(!variable.empty() && variable[0]=='\\')
            variable.erase(0, 1);

          if(variable.empty())
            throw "failed to get sub-variable name from "+original_variable;
        }

        if(variable=="PC")
          thread_infos[thread_nr].PC=atoi(value.c_str());
        else if(variable=="runs")
          thread_infos[thread_nr].runs=atoi(value.c_str());
        else if(has_prefix(variable, "b"))
        {
          unsigned nr=atoi(variable.c_str()+1);
          if(nr>=abstract_state.predicate_values.size())
            throw "invalid variable in abstract counterexample: "+
              variable;

          abstract_state.predicate_values[nr]=atoi(value.c_str());
        }
        else if(has_prefix(variable, "guard"))
        {
          unsigned nr=atoi(variable.c_str()+5);
          if(nr>=thread_infos[thread_nr].guards.size())
            throw "invalid variable in abstract counterexample: "+
              variable;

          thread_infos[thread_nr].guards[nr]=atoi(value.c_str());
        }
        else if(has_prefix(variable, "nondet"))
        {
        }
        else if(has_prefix(variable, "start_thread_"))
        {
        }
        else if(variable=="end_thread")
        {
        }
        else if(variable=="started")
        {
        }
        else if(has_prefix(variable, "po_"))
        {
        }
        else if(variable=="atomic")
        {
        }
        else
          throw "unknown variable in abstract counterexample: "+
                original_variable+" (stripped: "+variable+")";
      }
    }
  }
  
  //std::cout << counterexample << std::endl;
}