Ejemplo n.º 1
0
JNIEXPORT jstring JNICALL Java_org_joogie_prover_old_Z3ProverEx_getModel(
    JNIEnv * env, jclass clazz) {

    Z3_string modelAsString = Z3_model_to_string(context, model);
    if (Z3_OK != Z3_get_error_code(context))
        return NULL;

    Z3_del_model(context, model);
    model = NULL;

    return (*env)->NewStringUTF(env, modelAsString);
}
Ejemplo n.º 2
0
    static DWORD __stdcall do_check(LPVOID _this) {
		thread_check* th = static_cast<thread_check*>(_this);
        Z3_config cfg = Z3_mk_config();
        Z3_set_param_value(cfg,"MODEL","true");
        Z3_context ctx = Z3_mk_context(cfg);
        Z3_parse_smtlib_string(ctx, "(benchmark b :logic QF_UF :extrafuns ((f U U) (x U)) :formula (= (f x) x))", 0, 0, 0, 0, 0, 0);
        Z3_ast f = Z3_get_smtlib_formula(ctx, 0);
        Z3_assert_cnstr(ctx, f);
        Z3_model m = 0;
        Z3_lbool r = Z3_check_and_get_model(ctx,&m);
        EnterCriticalSection(&th->m_cs);
        printf("%d\n", r);
        LeaveCriticalSection(&th->m_cs);
        if (m) {
            Z3_del_model(ctx, m);
        }
		return 0;
    }
Ejemplo n.º 3
0
void check_and_display_model(SOLVER_CONTEXT c, FILE *out)
{
  Z3_context ctx = (Z3_context) c;
  Z3_model m      = 0;
  Z3_lbool result = Z3_check_and_get_model(ctx, &m);
  switch (result) {
  case Z3_L_FALSE:
    fprintf(out, "unsat\n");
    break;
  case Z3_L_UNDEF:
    fprintf(out, "unknown\n");
    fprintf(out, "potential model:\n");
    display_model(ctx, out, m);
    break;
  case Z3_L_TRUE:
    fprintf(out, "sat\n");
    display_model(ctx, out, m);
    break;
    }
  if (m) {
    Z3_del_model(ctx, m);
  }
  return;
}
Ejemplo n.º 4
0
int main(int argc, const char **argv) {

  /* Create a Z3 context to contain formulas */

  Z3_config cfg = Z3_mk_config();
  Z3_context ctx = iz3_mk_context(cfg);
  Z3_set_error_handler(ctx, throw_z3_error);
    
  /* Make some constraints, by parsing an smtlib formatted file given as arg 1 */

  try {
    Z3_parse_smtlib_file(ctx, argv[1], 0, 0, 0, 0, 0, 0);
  }
  catch(const z3_error &err){
    std::cerr << "Z3 error: " << Z3_get_error_msg(err.c) << "\n";
    std::cerr << Z3_get_smtlib_error(ctx) << "\n";
    return(1);
  } 

  /* Get the constraints from the parser. */

  int num = Z3_get_smtlib_num_formulas(ctx);

  if(num == 0){
    std::cerr << "iZ3 error: File contains no formulas.\n";
    return 1;
  }


  Z3_ast *constraints = (Z3_ast *)malloc(num * sizeof(Z3_ast));

  int i;
  for (i = 0; i < num; i++)
    constraints[i] = Z3_get_smtlib_formula(ctx, i);

  /* if we get only one formula, and it is a conjunction, split it into conjuncts. */
  if(num == 1){
    Z3_app app = Z3_to_app(ctx,constraints[0]);
    Z3_func_decl func = Z3_get_app_decl(ctx,app);
    Z3_decl_kind dk = Z3_get_decl_kind(ctx,func);
    if(dk == Z3_OP_AND){
      int nconjs = Z3_get_app_num_args(ctx,app);
      if(nconjs > 1){
	std::cout << "Splitting formula into " << nconjs << " conjuncts...\n";
	num = nconjs;
	constraints = new Z3_ast[num];
	for(int k = 0; k < num; k++)
	  constraints[k] = Z3_get_app_arg(ctx,app,k);
      }
    }
  }


  /* print out the result for grins. */

  // Z3_string smtout = Z3_benchmark_to_smtlib_string (ctx, "foo", "QFLIA", "sat", "", num, constraints, Z3_mk_true(ctx));

  // Z3_string smtout = Z3_ast_to_string(ctx,constraints[0]);
  // Z3_string smtout = Z3_context_to_string(ctx);
  // puts(smtout);

  // iz3_print(ctx,num,constraints,"iZ3temp.smt");

  /* Make room for interpolants. */

  Z3_ast *interpolants = (Z3_ast *)malloc((num-1) * sizeof(Z3_ast));

  /* Make room for the model. */

  Z3_model model = 0;

  /* Call the prover */

  Z3_lbool result = iz3_interpolate(ctx, num, constraints, interpolants, &model);

  switch (result) {
  
    /* If UNSAT, print the interpolants */
  case Z3_L_FALSE:
    printf("unsat, interpolants:\n");
    for(i = 0; i < num-1; i++)
      printf("%s\n", Z3_ast_to_string(ctx, interpolants[i]));
    std::cout << "Checking interpolants...\n";
    const char *error;
    if(iZ3_check_interpolant(ctx, num, constraints, 0, interpolants, &error))
      std::cout << "Interpolant is correct\n";
    else {
      std::cout << "Interpolant is incorrect\n";
      std::cout << error << "\n";
    }
    break;
  case Z3_L_UNDEF:
    printf("fail\n");
    break;
  case Z3_L_TRUE:
    printf("sat\n");
    printf("model:\n%s\n", Z3_model_to_string(ctx, model));
    break;
  }

  /* Delete the model if there is one */
  
  if (model)
    Z3_del_model(ctx, model);
  
  /* Delete logical context (note, we call iz3_del_context, not
     Z3_del_context */

  iz3_del_context(ctx);

  return 0;
}
Ejemplo n.º 5
0
int main(int argc, const char **argv) {
    
  bool tree_mode = false;
  bool check_mode = false;
  bool profile_mode = false;
  bool incremental_mode = false;
  std::string output_file;
  bool flat_mode = false;
  bool anonymize = false;
  bool write = false;

  Z3_config cfg = Z3_mk_config();
  // Z3_interpolation_options options = Z3_mk_interpolation_options();
  Z3_params options = 0;

  /* Parse the command line */
  int argn = 1;
  while(argn < argc-1){
    std::string flag = argv[argn];
    if(flag[0] == '-'){
      if(flag == "-t" || flag == "--tree")
	tree_mode = true;
      else if(flag == "-c" || flag == "--check")
	check_mode = true;
      else if(flag == "-p" || flag == "--profile")
	profile_mode = true;
#if 0
      else if(flag == "-w" || flag == "--weak")
        Z3_set_interpolation_option(options,"weak","1");
      else if(flag == "--secondary")
        Z3_set_interpolation_option(options,"secondary","1");
#endif
      else if(flag == "-i" || flag == "--incremental")
        incremental_mode = true;
      else if(flag == "-o"){
        argn++;
        if(argn >= argc) return usage(argv);
        output_file = argv[argn];
      }
      else if(flag == "-f" || flag == "--flat")
        flat_mode = true;
      else if(flag == "-a" || flag == "--anon")
        anonymize = true;
      else if(flag == "-w" || flag == "--write")
        write = true;
      else if(flag == "-s" || flag == "--simple")
        Z3_set_param_value(cfg,"PREPROCESS","false");
      else
        return usage(argv);
    }
    argn++;
  }
  if(argn != argc-1)
    return usage(argv);
  const char *filename = argv[argn];


  /* Create a Z3 context to contain formulas */
  Z3_context ctx = Z3_mk_interpolation_context(cfg);

  if(write || anonymize)
    Z3_set_ast_print_mode(ctx,Z3_PRINT_SMTLIB2_COMPLIANT);
  else if(!flat_mode)
    Z3_set_ast_print_mode(ctx,Z3_PRINT_SMTLIB_COMPLIANT);
  
  /* Read an interpolation problem */

  unsigned num;
  Z3_ast *constraints;
  unsigned *parents = 0;
  const char *error;
  bool ok;
  unsigned num_theory;
  Z3_ast *theory;

  ok = Z3_read_interpolation_problem(ctx, &num, &constraints, tree_mode ? &parents : 0, filename, &error, &num_theory, &theory);

  /* If parse failed, print the error message */

  if(!ok){
    std::cerr << error << "\n";
    return 1;
  }

  /* if we get only one formula, and it is a conjunction, split it into conjuncts. */
  if(!tree_mode && num == 1){
    Z3_app app = Z3_to_app(ctx,constraints[0]);
    Z3_func_decl func = Z3_get_app_decl(ctx,app);
    Z3_decl_kind dk = Z3_get_decl_kind(ctx,func);
    if(dk == Z3_OP_AND){
      int nconjs = Z3_get_app_num_args(ctx,app);
      if(nconjs > 1){
	std::cout << "Splitting formula into " << nconjs << " conjuncts...\n";
	num = nconjs;
	constraints = new Z3_ast[num];
	for(int k = 0; k < num; k++)
	  constraints[k] = Z3_get_app_arg(ctx,app,k);
      }
    }
  }

  /* Write out anonymized version. */

  if(write || anonymize){
#if 0
    Z3_anonymize_ast_vector(ctx,num,constraints);
#endif
    std::string ofn = output_file.empty() ? "iz3out.smt2" : output_file;
    Z3_write_interpolation_problem(ctx, num, constraints, parents, ofn.c_str(), num_theory,  theory);
    std::cout << "anonymized problem written to " << ofn << "\n";
    exit(0);
  }

  /* Compute an interpolant, or get a model. */

  Z3_ast *interpolants = (Z3_ast *)malloc((num-1) * sizeof(Z3_ast));
  Z3_model model = 0;
  Z3_lbool result;

  if(!incremental_mode){
    /* In non-incremental mode, we just pass the constraints. */
      result = Z3_L_UNDEF; // FIXME: Z3_interpolate(ctx, num, constraints, parents,  options, interpolants, &model, 0, false, num_theory, theory);
  }
  else {

    /* This is a somewhat useless demonstration of incremental mode.
      Here, we assert the constraints in the context, then pass them to
      iZ3 in an array, so iZ3 knows the sequence. Note it's safe to pass
      "true", even though we haven't techically asserted if. */

    Z3_push(ctx);
    std::vector<Z3_ast> asserted(num);

    /* We start with nothing asserted. */
    for(int i = 0; i < num; i++)
      asserted[i] = Z3_mk_true(ctx);

    /* Now we assert the constrints one at a time until UNSAT. */

    for(int i = 0; i < num; i++){
      asserted[i] = constraints[i];
      Z3_assert_cnstr(ctx,constraints[i]);  // assert one constraint
      result = Z3_L_UNDEF; // FIXME: Z3_interpolate(ctx, num, &asserted[0], parents,  options, interpolants, &model, 0, true, 0, 0);
      if(result == Z3_L_FALSE){
	for(unsigned j = 0; j < num-1; j++)
          /* Since we want the interpolant formulas to survive a "pop", we
            "persist" them here. */
          Z3_persist_ast(ctx,interpolants[j],1);
        break;
      }
    }
    Z3_pop(ctx,1);
  }
  
  switch (result) {
  
    /* If UNSAT, print the interpolants */
  case Z3_L_FALSE:
    printf("unsat\n");
    if(output_file.empty()){
      printf("interpolant:\n");
      for(int i = 0; i < num-1; i++)
        printf("%s\n", Z3_ast_to_string(ctx, interpolants[i]));
    }
    else {
#if 0
      Z3_write_interpolation_problem(ctx,num-1,interpolants,0,output_file.c_str());
      printf("interpolant written to %s\n",output_file.c_str());
#endif
    }
#if 1
    if(check_mode){
      std::cout << "Checking interpolant...\n";
      bool chk;
      chk = Z3_check_interpolant(ctx,num,constraints,parents,interpolants,&error,num_theory,theory);  
      if(chk)
	std::cout << "Interpolant is correct\n";
      else {
        std::cout << "Interpolant is incorrect\n";
        std::cout << error;
        return 1;
      }
    }
#endif
    break;
  case Z3_L_UNDEF:
    printf("fail\n");
    break;
  case Z3_L_TRUE:
    printf("sat\n");
    printf("model:\n%s\n", Z3_model_to_string(ctx, model));
    break;
  }

  if(profile_mode)
    std::cout << Z3_interpolation_profile(ctx);

  /* Delete the model if there is one */
  
  if (model)
    Z3_del_model(ctx, model);
  
  /* Delete logical context. */

  Z3_del_context(ctx);
  free(interpolants);

  return 0;
}
Ejemplo n.º 6
0
int test(){
  int i;

  /* Create a Z3 context to contain formulas */

  Z3_config cfg = Z3_mk_config();
  Z3_context ctx = iz3_mk_context(cfg);
    
  int num = 2;

  Z3_ast *constraints = (Z3_ast *)malloc(num * sizeof(Z3_ast));

#if 1
  Z3_sort arr = Z3_mk_array_sort(ctx,Z3_mk_int_sort(ctx),Z3_mk_bool_sort(ctx)); 
  Z3_symbol  as  = Z3_mk_string_symbol(ctx, "a");
  Z3_symbol  bs  = Z3_mk_string_symbol(ctx, "b");
  Z3_symbol  xs  = Z3_mk_string_symbol(ctx, "x");

  Z3_ast a = Z3_mk_const(ctx,as,arr);
  Z3_ast b = Z3_mk_const(ctx,bs,arr);
  Z3_ast x = Z3_mk_const(ctx,xs,Z3_mk_int_sort(ctx));
  
  Z3_ast c1 = Z3_mk_eq(ctx,a,Z3_mk_store(ctx,b,x,Z3_mk_true(ctx)));
  Z3_ast c2 = Z3_mk_not(ctx,Z3_mk_select(ctx,a,x));
#else
  Z3_symbol  xs  = Z3_mk_string_symbol(ctx, "x");
  Z3_ast x = Z3_mk_const(ctx,xs,Z3_mk_bool_sort(ctx));
  Z3_ast c1 = Z3_mk_eq(ctx,x,Z3_mk_true(ctx));
  Z3_ast c2 = Z3_mk_eq(ctx,x,Z3_mk_false(ctx));

#endif

  constraints[0] = c1;
  constraints[1] = c2;
  
  /* print out the result for grins. */

  // Z3_string smtout = Z3_benchmark_to_smtlib_string (ctx, "foo", "QFLIA", "sat", "", num, constraints, Z3_mk_true(ctx));

  // Z3_string smtout = Z3_ast_to_string(ctx,constraints[0]);
  // Z3_string smtout = Z3_context_to_string(ctx);
  // puts(smtout);

  iz3_print(ctx,num,constraints,"iZ3temp.smt");

  /* Make room for interpolants. */

  Z3_ast *interpolants = (Z3_ast *)malloc((num-1) * sizeof(Z3_ast));

  /* Make room for the model. */

  Z3_model model = 0;

  /* Call the prover */

  Z3_lbool result = iz3_interpolate(ctx, num, constraints, interpolants, &model);

  switch (result) {
  
    /* If UNSAT, print the interpolants */
  case Z3_L_FALSE:
    printf("unsat, interpolants:\n");
    for(i = 0; i < num-1; i++)
      printf("%s\n", Z3_ast_to_string(ctx, interpolants[i]));
    break;
  case Z3_L_UNDEF:
    printf("fail\n");
    break;
  case Z3_L_TRUE:
    printf("sat\n");
    printf("model:\n%s\n", Z3_model_to_string(ctx, model));
    break;
  }

  /* Delete the model if there is one */
  
  if (model)
    Z3_del_model(ctx, model);
  
  /* Delete logical context (note, we call iz3_del_context, not
     Z3_del_context */

  iz3_del_context(ctx);

  return 1;
}
Ejemplo n.º 7
0
int unsat_core(/*in*/
	       SOLVER_CONTEXT ctx, SOLVER_TERM * cs, unsigned cs_size, 
	       /*out*/
	       unsigned** bitvector_core){
  /*
   Suppose the formula x=0 and y=2 and x>2.

   Then, we add a positive literal {p1,p2,p3} to each clause as follows:
      (x=0 or p1) and (y=2 or p2) and (x>2 or p3)
   but assuming that p1=0, p2=0, and p3=0 to keep unchanged the formula.

   Then, we run the solver. In this case, the conflict arises due to
   either:  

     a)   x=0 is true  and x>2 is false or
     b)   x=0 is false and x>2 is true 

   How we can extract this information?

   Case a)
          T     F         T     F         F     T
        (x=0 or p1) and (y=2 or p2) and (x>2 or p3)
        The conflict literal is p3 because p3=F
   Case b)
          F     T         T      F        T      F
        (x=0 or p1) and (y=2 or p2) and (x>2 or p3)
        The conflict literal is p1 because p1=F

   Then, the solver will add the clause p1 or p3 which means that the
   unsatisfiable core is the 1st and 3rd constraint from the original
   formula.
   */  

  Z3_ast * assumptions;
  Z3_ast p;
  static char v[50];
  Z3_ast g[2];
  int i, ret, index;
  Z3_ast   proof;
  Z3_model m      = 0;
  Z3_lbool result;
  Z3_ast * cs_core;
  unsigned cs_core_size;
  unsigned * b;
  int bindex;

  assumptions = (Z3_ast *) malloc(sizeof(Z3_ast) * cs_size);
  if (assumptions == NULL){
    fprintf(stderr,"No more memory! \n");
    exit(1);    
  }

  for(i = 0; i < cs_size; i++){
    sprintf(v,"p_%d",i);
    p = mk_bool_var((Z3_context) ctx,v);
    g[0]  = cs[i];
    g[1]  = p;
    Z3_assert_cnstr((Z3_context) ctx, Z3_mk_or((Z3_context)ctx, 2, g)); // c_i or p_i
    assumptions[i] = Z3_mk_not((Z3_context) ctx, p);                    // not p_i
  }
 
  // (not p_1),...,(not p_n) |- (c_1 OR p_1) AND ... AND (c_n OR p_n)
  cs_core = (Z3_ast *) calloc(cs_size,sizeof(Z3_ast)); 
  cs_core_size = cs_size;
  
  result = Z3_check_assumptions((Z3_context) ctx, cs_size, assumptions, 
				&m, &proof, &cs_core_size, cs_core);


  // all elements are initialized to 0
  b = (unsigned *) calloc(cs_size, sizeof(unsigned));  
  if (b == NULL){
    fprintf(stderr,"No more memory! \n");
    exit(1);    
  }
  
  // printf("Size of the unsat core %d\n",cs_core_size);
  switch (result) {
  case Z3_L_FALSE:
    for (i = 0; i < cs_core_size; ++i){
      bindex =find_core_assumption_index((Z3_context) ctx, assumptions, 
					 cs_size, cs_core[i]);
      b[bindex] = (unsigned) 1u;
    }	    
    // printf("unsat\n");
    ret = SOLVER_ANS_FALSE;
    break;
  case Z3_L_UNDEF:
    // printf("undefined\n");
    ret = SOLVER_ANS_UNDEF;
    break;
  case Z3_L_TRUE:
    // printf("sat\n");
    ret = SOLVER_ANS_TRUE;
    break;
  }
  if (m) {
    Z3_del_model(ctx, m);
  }    

  *bitvector_core = b;
  return ret;
}