Esempio n. 1
0
/// Outputs the current value of the domain.
/// \par parameters: The abstract interpreter and the namespace.
/// \return The domain, formatted as a JSON object.
jsont dep_graph_domaint::output_json(
  const ai_baset &ai,
  const namespacet &ns) const
{
  json_arrayt graph;

  for(const auto &cd : control_deps)
  {
    json_objectt &link=graph.push_back().make_object();
    link["locationNumber"]=
      json_numbert(std::to_string(cd->location_number));
    link["sourceLocation"]=json(cd->source_location);
    link["type"]=json_stringt("control");
  }

  for(const auto &dd : data_deps)
  {
    json_objectt &link=graph.push_back().make_object();
    link["locationNumber"]=
      json_numbert(std::to_string(dd->location_number));
    link["sourceLocation"]=json(dd->source_location);
      json_stringt(dd->source_location.as_string());
    link["type"]=json_stringt("data");
  }

  return graph;
}
Esempio n. 2
0
void convert(
  const namespacet &ns,
  const goto_tracet &goto_trace,
  jsont &dest)
{
  json_arrayt &dest_array=dest.make_array();

  source_locationt previous_source_location;

  for(const auto &step : goto_trace.steps)
  {
    const source_locationt &source_location=step.pc->source_location;

    jsont json_location;

    if(source_location.is_not_nil() && source_location.get_file()!="")
      json_location=json(source_location);
    else
      json_location=json_nullt();

    switch(step.type)
    {
    case goto_trace_stept::typet::ASSERT:
      if(!step.cond_value)
      {
        irep_idt property_id;

        if(step.pc->is_assert())
          property_id=source_location.get_property_id();
        else if(step.pc->is_goto()) // unwinding, we suspect
        {
          property_id=
            id2string(step.pc->source_location.get_function())+
            ".unwind."+std::to_string(step.pc->loop_number);
        }

        json_objectt &json_failure=dest_array.push_back().make_object();

        json_failure["stepType"]=json_stringt("failure");
        json_failure["hidden"]=jsont::json_boolean(step.hidden);
        json_failure["thread"]=json_numbert(std::to_string(step.thread_nr));
        json_failure["reason"]=json_stringt(id2string(step.comment));
        json_failure["property"]=json_stringt(id2string(property_id));

        if(!json_location.is_null())
          json_failure["sourceLocation"]=json_location;
      }
      break;

    case goto_trace_stept::typet::ASSIGNMENT:
    case goto_trace_stept::typet::DECL:
      {
        irep_idt identifier=step.lhs_object.get_identifier();
        json_objectt &json_assignment=dest_array.push_back().make_object();

        json_assignment["stepType"]=json_stringt("assignment");

        if(!json_location.is_null())
          json_assignment["sourceLocation"]=json_location;

        std::string value_string, binary_string, type_string, full_lhs_string;
        json_objectt full_lhs_value;

        if(step.full_lhs.is_not_nil())
          full_lhs_string=from_expr(ns, identifier, step.full_lhs);

#if 0
        if(it.full_lhs_value.is_not_nil())
          full_lhs_value_string=from_expr(ns, identifier, it.full_lhs_value);
#endif

        if(step.full_lhs_value.is_not_nil())
          full_lhs_value = json(step.full_lhs_value, ns);

        const symbolt *symbol;
        irep_idt base_name, display_name;

        if(!ns.lookup(identifier, symbol))
        {
          base_name=symbol->base_name;
          display_name=symbol->display_name();
          if(type_string=="")
            type_string=from_type(ns, identifier, symbol->type);

          json_assignment["mode"]=json_stringt(id2string(symbol->mode));
        }

        json_assignment["value"]=full_lhs_value;
        json_assignment["lhs"]=json_stringt(full_lhs_string);
        json_assignment["hidden"]=jsont::json_boolean(step.hidden);
        json_assignment["thread"]=json_numbert(std::to_string(step.thread_nr));

        json_assignment["assignmentType"]=
          json_stringt(
            step.assignment_type==
              goto_trace_stept::assignment_typet::ACTUAL_PARAMETER?
            "actual-parameter":
            "variable");
      }
      break;

    case goto_trace_stept::typet::OUTPUT:
      {
        json_objectt &json_output=dest_array.push_back().make_object();

        json_output["stepType"]=json_stringt("output");
        json_output["hidden"]=jsont::json_boolean(step.hidden);
        json_output["thread"]=json_numbert(std::to_string(step.thread_nr));
        json_output["outputID"]=json_stringt(id2string(step.io_id));

        json_arrayt &json_values=json_output["values"].make_array();

        for(const auto &arg : step.io_args)
        {
          if(arg.is_nil())
            json_values.push_back(json_stringt(""));
          else
            json_values.push_back(json(arg, ns));
        }

        if(!json_location.is_null())
          json_output["sourceLocation"]=json_location;
      }
      break;

    case goto_trace_stept::typet::INPUT:
      {
        json_objectt &json_input=dest_array.push_back().make_object();

        json_input["stepType"]=json_stringt("input");
        json_input["hidden"]=jsont::json_boolean(step.hidden);
        json_input["thread"]=json_numbert(std::to_string(step.thread_nr));
        json_input["inputID"]=json_stringt(id2string(step.io_id));

        json_arrayt &json_values=json_input["values"].make_array();

        for(const auto &arg : step.io_args)
        {
          if(arg.is_nil())
            json_values.push_back(json_stringt(""));
          else
            json_values.push_back(json(arg, ns));
        }

        if(!json_location.is_null())
          json_input["sourceLocation"]=json_location;
      }
      break;

    case goto_trace_stept::typet::FUNCTION_CALL:
    case goto_trace_stept::typet::FUNCTION_RETURN:
      {
        std::string tag=
          (step.type==goto_trace_stept::typet::FUNCTION_CALL)?
            "function-call":"function-return";
        json_objectt &json_call_return=dest_array.push_back().make_object();

        json_call_return["stepType"]=json_stringt(tag);
        json_call_return["hidden"]=jsont::json_boolean(step.hidden);
        json_call_return["thread"]=json_numbert(std::to_string(step.thread_nr));

        const symbolt &symbol=ns.lookup(step.identifier);
        json_objectt &json_function=json_call_return["function"].make_object();
        json_function["displayName"]=
          json_stringt(id2string(symbol.display_name()));
        json_function["identifier"]=json_stringt(id2string(step.identifier));
        json_function["sourceLocation"]=json(symbol.location);

        if(!json_location.is_null())
          json_call_return["sourceLocation"]=json_location;
      }
      break;

    default:
      if(source_location!=previous_source_location)
      {
        // just the source location
        if(!json_location.is_null())
        {
          json_objectt &json_location_only=dest_array.push_back().make_object();
          json_location_only["stepType"]=json_stringt("location-only");
          json_location_only["hidden"]=jsont::json_boolean(step.hidden);
          json_location_only["thread"]=
            json_numbert(std::to_string(step.thread_nr));
          json_location_only["sourceLocation"]=json_location;
        }
      }
    }

    if(source_location.is_not_nil() && source_location.get_file()!="")
      previous_source_location=source_location;
  }
}
Esempio n. 3
0
json_objectt json(
  const typet &type,
  const namespacet &ns)
{
  if(type.id()==ID_symbol)
    return json(ns.follow(type), ns);

  json_objectt result;

  if(type.id()==ID_unsignedbv)
  {
    result["name"]=json_stringt("integer");
    result["width"]=
      json_numbert(i2string(to_unsignedbv_type(type).get_width()));
  }
  else if(type.id()==ID_signedbv)
  {
    result["name"]=json_stringt("integer");
    result["width"]=json_numbert(i2string(to_signedbv_type(type).get_width()));
  }
  else if(type.id()==ID_floatbv)
  {
    result["name"]=json_stringt("float");
    result["width"]=json_numbert(i2string(to_floatbv_type(type).get_width()));
  }
  else if(type.id()==ID_bv)
  {
    result["name"]=json_stringt("integer");
    result["width"]=json_numbert(i2string(to_bv_type(type).get_width()));
  }
  else if(type.id()==ID_c_bit_field)
  {
    result["name"]=json_stringt("integer");
    result["width"]=
      json_numbert(i2string(to_c_bit_field_type(type).get_width()));
  }
  else if(type.id()==ID_c_enum_tag)
  {
    // we return the base type
    return json(ns.follow_tag(to_c_enum_tag_type(type)).subtype(), ns);
  }
  else if(type.id()==ID_fixedbv)
  {
    result["name"]=json_stringt("fixed");
    result["width"]=json_numbert(i2string(to_fixedbv_type(type).get_width()));
  }
  else if(type.id()==ID_pointer)
  {
    result["name"]=json_stringt("pointer");
    result["subtype"]=json(type.subtype(), ns);
  }
  else if(type.id()==ID_bool)
  {
    result["name"]=json_stringt("boolean");
  }
  else if(type.id()==ID_array)
  {
    result["name"]=json_stringt("array");
    result["subtype"]=json(type.subtype(), ns);
  }
  else if(type.id()==ID_vector)
  {
    result["name"]=json_stringt("vector");
    result["subtype"]=json(type.subtype(), ns);
    result["size"]=json(to_vector_type(type).size(), ns);
  }
  else if(type.id()==ID_struct)
  {
    result["name"]=json_stringt("struct");
    json_arrayt &members=result["members"].make_array();
    const struct_typet::componentst &components=
      to_struct_type(type).components();
    for(const auto & it : components)
    {
      json_objectt &e=members.push_back().make_object();
      e["name"]=json_stringt(id2string(it.get_name()));
      e["type"]=json(it.type(), ns);
    }
  }
  else if(type.id()==ID_union)
  {
    result["name"]=json_stringt("union");
    json_arrayt &members=result["members"].make_array();
    const union_typet::componentst &components=
      to_union_type(type).components();
    for(const auto & it : components)
    {
      json_objectt &e=members.push_back().make_object();
      e["name"]=json_stringt(id2string(it.get_name()));
      e["type"]=json(it.type(), ns);
    }
  }
  else
    result["name"]=json_stringt("unknown");

  return result;
}
Esempio n. 4
0
void symex_parse_optionst::report_cover(
  const path_searcht::property_mapt &property_map)
{
  // report
  unsigned goals_covered=0;

  for(const auto &prop_pair : property_map)
    if(prop_pair.second.is_failure())
      goals_covered++;

  switch(get_ui())
  {
    case ui_message_handlert::uit::PLAIN:
    {
      status() << "\n** coverage results:" << eom;

      for(const auto &prop_pair : property_map)
      {
        const auto &property=prop_pair.second;

        status() << "[" << prop_pair.first << "]";

        if(property.source_location.is_not_nil())
          status() << ' ' << property.source_location;

        if(!property.description.empty())
          status() << ' ' << property.description;

        status() << ": " << (property.is_failure()?"SATISFIED":"FAILED")
                 << eom;
      }

      status() << '\n';

      break;
    }

    case ui_message_handlert::uit::XML_UI:
    {
      for(const auto &prop_pair : property_map)
      {
        const auto &property=prop_pair.second;

        xmlt xml_result("result");
        xml_result.set_attribute("goal", id2string(prop_pair.first));
        xml_result.set_attribute(
          "description", id2string(property.description));
        xml_result.set_attribute(
          "status", property.is_failure()?"SATISFIED":"FAILED");

        if(property.source_location.is_not_nil())
          xml_result.new_element()=xml(property.source_location);

        if(property.is_failure())
        {
          const namespacet ns(goto_model.symbol_table);

          if(cmdline.isset("trace"))
          {
            convert(ns, property.error_trace, xml_result.new_element());
          }
          else
          {
            xmlt &xml_test=xml_result.new_element("test");

            for(const auto &step : property.error_trace.steps)
            {
              if(step.is_input())
              {
                xmlt &xml_input=xml_test.new_element("input");
                xml_input.set_attribute("id", id2string(step.io_id));
                if(step.io_args.size()==1)
                  xml_input.new_element("value")=
                    xml(step.io_args.front(), ns);
              }
            }
          }
        }

        std::cout << xml_result << "\n";
      }

      break;
    }
    case ui_message_handlert::uit::JSON_UI:
    {
      json_objectt json_result;
      json_arrayt &result_array=json_result["results"].make_array();
      for(const auto &prop_pair : property_map)
      {
        const auto &property=prop_pair.second;

        json_objectt &result=result_array.push_back().make_object();
        result["status"]=
          json_stringt(property.is_failure()?"satisfied":"failed");
        result["goal"]=json_stringt(id2string(prop_pair.first));
        result["description"]=json_stringt(id2string(property.description));

        if(property.source_location.is_not_nil())
          result["sourceLocation"]=json(property.source_location);

        if(property.is_failure())
        {
          const namespacet ns(goto_model.symbol_table);

          if(cmdline.isset("trace"))
          {
            jsont &json_trace=result["trace"];
            convert(ns, property.error_trace, json_trace);
          }
          else
          {
            json_arrayt &json_test=result["test"].make_array();

            for(const auto &step : property.error_trace.steps)
            {
              if(step.is_input())
              {
                json_objectt json_input;
                json_input["id"]=json_stringt(id2string(step.io_id));
                if(step.io_args.size()==1)
                  json_input["value"]=json(step.io_args.front(), ns);
                json_test.push_back(json_input);
              }
            }
          }
        }
      }
      json_result["totalGoals"]=
        json_numbert(std::to_string(property_map.size()));
      json_result["goalsCovered"]=json_numbert(std::to_string(goals_covered));
      std::cout << ",\n" << json_result;
      break;
    }
  }

  status() << "** " << goals_covered
           << " of " << property_map.size() << " covered ("
           << std::fixed << std::setw(1) << std::setprecision(1)
           << (property_map.empty()?
               100.0:100.0*goals_covered/property_map.size())
           << "%)"
           << eom;

  if(get_ui()==ui_message_handlert::uit::PLAIN)
  {
    std::set<std::string> tests;

    for(const auto &prop_pair : property_map)
      if(prop_pair.second.is_failure())
        tests.insert(get_test(prop_pair.second.error_trace));

    std::cout << "Test suite:" << '\n';

    for(const auto &t : tests)
      std::cout << t << '\n';
  }
}
Esempio n. 5
0
json_objectt json(
  const exprt &expr,
  const namespacet &ns)
{
  json_objectt result;
  
  const typet &type=ns.follow(expr.type());

  if(expr.id()==ID_constant)
  {
    if(type.id()==ID_unsignedbv ||
       type.id()==ID_signedbv ||
       type.id()==ID_c_bit_field)
    {
      std::size_t width=to_bitvector_type(type).get_width();
    
      result["name"]=json_stringt("integer");
      result["binary"]=json_stringt(expr.get_string(ID_value));
      result["width"]=json_numbert(i2string(width));
      
      const typet &underlying_type=
        type.id()==ID_c_bit_field?type.subtype():
        type;

      bool is_signed=underlying_type.id()==ID_signedbv;
        
      std::string sig=is_signed?"":"unsigned ";

      if(width==config.ansi_c.char_width)
        result["c_type"]=json_stringt(sig+"char");
      else if(width==config.ansi_c.int_width)
        result["c_type"]=json_stringt(sig+"int");
      else if(width==config.ansi_c.short_int_width)
        result["c_type"]=json_stringt(sig+"short int");
      else if(width==config.ansi_c.long_int_width)
        result["c_type"]=json_stringt(sig+"long int");
      else if(width==config.ansi_c.long_long_int_width)
        result["c_type"]=json_stringt(sig+"long long int");

      mp_integer i;
      if(!to_integer(expr, i))
        result["data"]=json_stringt(integer2string(i));
    }
    else if(type.id()==ID_c_enum)
    {
      result["name"]=json_stringt("integer");
      result["binary"]=json_stringt(expr.get_string(ID_value));
      result["width"]=json_numbert(type.subtype().get_string(ID_width));
      result["c_type"]=json_stringt("enum");

      mp_integer i;
      if(!to_integer(expr, i))
        result["data"]=json_stringt(integer2string(i));
    }
    else if(type.id()==ID_c_enum_tag)
    {
      constant_exprt tmp;
      tmp.type()=ns.follow_tag(to_c_enum_tag_type(type));
      tmp.set_value(to_constant_expr(expr).get_value());
      return json(tmp, ns);
    }
    else if(type.id()==ID_bv)
    {
      result["name"]=json_stringt("bitvector");
      result["binary"]=json_stringt(expr.get_string(ID_value));
    }
    else if(type.id()==ID_fixedbv)
    {
      result["name"]=json_stringt("fixed");
      result["width"]=json_numbert(type.get_string(ID_width));
      result["binary"]=json_stringt(expr.get_string(ID_value));
      result["data"]=
        json_stringt(fixedbvt(to_constant_expr(expr)).to_ansi_c_string());
    }
    else if(type.id()==ID_floatbv)
    {
      result["name"]=json_stringt("float");
      result["width"]=json_numbert(type.get_string(ID_width));
      result["binary"]=json_stringt(expr.get_string(ID_value));
      result["data"]=
        json_stringt(ieee_floatt(to_constant_expr(expr)).to_ansi_c_string());
    }
    else if(type.id()==ID_pointer)
    {
      result["name"]=json_stringt("pointer");
      result["binary"]=json_stringt(expr.get_string(ID_value));
      if(expr.get(ID_value)==ID_NULL)
        result["data"]=json_stringt("NULL");
    }
    else if(type.id()==ID_bool)
    {
      result["name"]=json_stringt("boolean");
      result["binary"]=json_stringt(expr.is_true()?"1":"0");
      result["data"]=jsont::json_boolean(expr.is_true());
    }
    else
    {
      result["name"]=json_stringt("unknown");
    }
  }
  else if(expr.id()==ID_array)
  {
    result["name"]=json_stringt("array");
    json_arrayt &elements=result["elements"].make_array();
    
    unsigned index=0;
    
    forall_operands(it, expr)
    {
      json_objectt &e=elements.push_back().make_object();
      e["index"]=json_numbert(i2string(index));
      e["value"]=json(*it, ns);
      index++;
    }