int main() { VC vc = vc_createValidityChecker(NULL); /* Prove that for integers x and y: * x > 0 AND y > 0 => 2x + y >= 3 */ Type integer = vc_intType(vc); Expr x = vc_varExpr(vc, "x", integer); Expr y = vc_varExpr(vc, "y", integer); Expr zero = vc_ratExpr(vc, 0, 1); Expr x_positive = vc_gtExpr(vc, x, zero); Expr y_positive = vc_gtExpr(vc, y, zero); Expr two = vc_ratExpr(vc, 2, 1); Expr twox = vc_multExpr(vc, two, x); Expr twox_plus_y = vc_plusExpr(vc, twox, y); Expr three = vc_ratExpr(vc, 3, 1); Expr twox_plus_y_geq_3 = vc_geExpr(vc, twox_plus_y, three); Expr formula = vc_impliesExpr(vc, vc_andExpr(vc, x_positive, y_positive), twox_plus_y_geq_3); char* formulaString = vc_printExprString(vc, formula); printf("Checking validity of formula %s with CVC4.\n", formulaString); printf("CVC4 should return 1 (meaning VALID).\n"); printf("Result from CVC4 is: %d\n", vc_query(vc, formula)); free(formulaString); return 0; }
void test2() { VC vc = vc_createValidityChecker(NULL); // Check x = y -> g(x,y) = g(y,x) Type r = vc_realType(vc); Expr x = vc_varExpr(vc, "x", r); Expr y = vc_varExpr(vc, "y", r); Type realxreal2real = vc_funType2(vc, r, r, r); Op g = vc_createOp(vc, "g", realxreal2real); Expr gxy = vc_funExpr2(vc, g, x, y); Expr gyx = vc_funExpr2(vc, g, y, x); Expr xeqy = vc_eqExpr(vc, x, y); Expr gxyeqgyx = vc_eqExpr(vc, gxy, gyx); Expr e = vc_impliesExpr(vc, xeqy, gxyeqgyx); Type v[2]; Op h; Expr hxy, hyx, hxyeqhyx; int res; res = check(vc, e); FatalAssert(res == 1, "Expected Valid"); vc_deleteType(realxreal2real); vc_deleteOp(g); vc_deleteExpr(gxy); vc_deleteExpr(gyx); vc_deleteExpr(gxyeqgyx); vc_deleteExpr(e); v[0] = r; v[1] = r; realxreal2real = vc_funTypeN(vc, v, r, 2); h = vc_createOp(vc, "h", realxreal2real); hxy = vc_funExpr2(vc, h, x, y); hyx = vc_funExpr2(vc, h, y, x); hxyeqhyx = vc_eqExpr(vc, hxy, hyx); e = vc_impliesExpr(vc, xeqy, hxyeqhyx); res = check(vc, e); FatalAssert(res == 1, "Expected Valid"); vc_deleteType(r); vc_deleteExpr(x); vc_deleteExpr(y); vc_deleteType(realxreal2real); vc_deleteExpr(hxy); vc_deleteExpr(hxyeqhyx); vc_deleteExpr(e); vc_destroyValidityChecker(vc); }
int main(int argc, char *argv[]) { VC vc = vc_createValidityChecker(); vc_setFlags('n'); vc_setFlags('d'); vc_setFlags('p'); Expr nresp1 = vc_varExpr(vc, "nresp1", vc_bv32Type(vc)); Expr packet_get_int0 = vc_varExpr(vc, "packet_get_int0", vc_bv32Type(vc)); Expr exprs[] = { // nresp1 == packet_get_int0 vc_eqExpr(vc, nresp1, packet_get_int0), // nresp1 > 0 vc_bvGtExpr(vc, nresp1, vc_bv32ConstExprFromInt(vc, 0)) }; Expr res = vc_andExprN(vc, exprs, sizeof(exprs)/sizeof(exprs[0])); vc_printExpr(vc, res); int x = vc_query(vc, res); printf("vc_query result = %d\n", x); vc_printCounterExample(vc); Expr cex = vc_getCounterExample(vc, res); //vc_printExpr(vc, cex); }
TEST(Leaks, boolean) { VC vc; vc = vc_createValidityChecker(); Expr x = vc_varExpr(vc, "x", vc_boolType(vc)); Expr y = vc_varExpr(vc, "y", vc_boolType(vc)); Expr x_and_y = vc_andExpr(vc, x, y); Expr not_x_and_y = vc_notExpr(vc, x_and_y); Expr not_x = vc_notExpr(vc, x); Expr not_y = vc_notExpr(vc, y); Expr not_x_or_not_y = vc_orExpr(vc, not_x, not_y); Expr equiv = vc_iffExpr(vc, not_x_and_y, not_x_or_not_y); printf("%d\n", vc_query(vc, equiv)); vc_DeleteExpr(equiv); vc_DeleteExpr(not_x_or_not_y); vc_DeleteExpr(not_y); vc_DeleteExpr(not_x); vc_DeleteExpr(not_x_and_y); vc_DeleteExpr(x_and_y); vc_DeleteExpr(y); vc_DeleteExpr(x); vc_Destroy(vc); }
TEST(sbdiv, one) { VC vc = vc_createValidityChecker(); //vc_setFlags(vc, 'p'); vc_setFlags(vc, 'd'); Type int_type = vc_bv32Type(vc); Expr zero = vc_bv32ConstExprFromInt(vc, 0); Expr int_max = vc_bvConstExprFromInt(vc, 32, 0x7fffffff); Expr a = vc_varExpr(vc, "a", int_type); Expr b = vc_varExpr(vc, "b", int_type); vc_assertFormula(vc, vc_sbvGtExpr(vc, b, zero)); vc_assertFormula(vc, vc_sbvLeExpr(vc, a, vc_sbvDivExpr(vc, 32, int_max, b))); int query = vc_query(vc, vc_falseExpr(vc)); ASSERT_FALSE(query); }
TEST(multiple_queries,one) { VC vc = vc_createValidityChecker(); vc_setFlags(vc,'n'); vc_setFlags(vc,'c'); vc_setFlags(vc,'d'); vc_setFlags(vc,'p'); Type bv8 = vc_bvType(vc, 8); Expr a = vc_varExpr(vc, "a", bv8); Expr ct_0 = vc_bvConstExprFromInt(vc, 8, 0); Expr a_eq_0 = vc_eqExpr(vc, a, ct_0); /* Query 1 */ vc_push(vc); int query = vc_query(vc, a_eq_0); vc_pop(vc); printf("query = %d\n", query); /* Query 2 */ Expr a_neq_0 = vc_notExpr(vc, a_eq_0); vc_push(vc); query = vc_query(vc, a_neq_0); vc_pop(vc); printf("query = %d\n", query); vc_Destroy(vc); ASSERT_TRUE(false && "FIXME: Actually test something"); }
int main() { VC vc = vc_createValidityChecker(); vc_setFlags(vc,'n'); vc_setFlags(vc,'c'); vc_setFlags(vc,'d'); vc_setFlags(vc,'p'); Type bv8 = vc_bvType(vc, 8); Expr a = vc_varExpr(vc, "a", bv8); Expr ct_0 = vc_bvConstExprFromInt(vc, 8, 0); Expr a_eq_0 = vc_eqExpr(vc, a, ct_0); /* Query 1 */ vc_push(vc); int query = vc_query(vc, a_eq_0); vc_pop(vc); printf("query = %d\n", query); /* Query 2 */ Expr a_neq_0 = vc_notExpr(vc, a_eq_0); vc_push(vc); query = vc_query(vc, a_neq_0); vc_pop(vc); printf("query = %d\n", query); vc_Destroy(vc); return 0; }
void test5() { Flags flags = vc_createFlags(); VC vc; Type r; Expr x, xEQx, p; // vc_setBoolFlag(flags, "proofs", TRUE); vc = vc_createValidityChecker(flags); r = vc_realType(vc); x = vc_varExpr(vc, "x", r); xEQx = vc_eqExpr(vc, x, x); vc_query(vc, xEQx); // p = vc_getProof(vc); // vc_printExpr(vc, p); vc_deleteType(r); vc_deleteExpr(x); vc_deleteExpr(xEQx); // vc_deleteExpr(p); vc_destroyValidityChecker(vc); vc_deleteFlags(flags); }
TEST(push_pop, one) { VC vc = vc_createValidityChecker(); //vc_setFlags(vc, 'n'); vc_setFlags(vc, 'd'); //vc_setFlags(vc, 'p'); // vc_setFlags(vc,'v'); // vc_setFlags(vc,'s'); Type bv8 = vc_bvType(vc, 8); Expr a = vc_varExpr(vc, "a", bv8); Expr ct_0 = vc_bvConstExprFromInt(vc, 8, 0); Expr a_eq_0 = vc_eqExpr(vc, a, ct_0); int query = vc_query(vc, a_eq_0); ASSERT_FALSE(query); vc_push(vc); query = vc_query(vc, a_eq_0); ASSERT_FALSE(query); vc_pop(vc); vc_DeleteExpr(a_eq_0); vc_DeleteExpr(a); vc_Destroy(vc); }
TEST(push_pop, two) { VC vc = vc_createValidityChecker(); //vc_setFlags(vc, 'n'); vc_setFlags(vc, 'd'); //vc_setFlags(vc, 'p'); //vc_setFlags(vc, 'v'); //vc_setFlags(vc, 's'); vc_setFlags(vc, 'c'); vc_push(vc); Type bv8 = vc_bvType(vc, 8); Expr a = vc_varExpr(vc, "a", bv8); Expr ct_0 = vc_bvConstExprFromInt(vc, 8, 0); Expr a_eq_0 = vc_eqExpr(vc, a, ct_0); vc_assertFormula(vc, a_eq_0); vc_printAsserts(vc); vc_push(vc); Expr queryexp = vc_eqExpr(vc, a, vc_bvConstExprFromInt(vc, 8, 0)); //vc_printExpr(vc, queryexp); int query; query = vc_query(vc, queryexp); vc_printCounterExample(vc); vc_pop(vc); vc_pop(vc); ASSERT_TRUE(query); }
TEST(sbdiv, one) { VC vc = vc_createValidityChecker(); vc_setFlags(vc, 'p'); Type int_type = vc_bv32Type(vc); Expr zero = vc_bv32ConstExprFromInt(vc, 0); Expr int_max = vc_bvConstExprFromInt(vc, 32, 0x7fffffff); Expr a = vc_varExpr(vc, "a", int_type); Expr b = vc_varExpr(vc, "b", int_type); vc_assertFormula(vc, vc_sbvGtExpr(vc, b, zero)); vc_assertFormula(vc, vc_sbvLeExpr(vc, a, vc_sbvDivExpr(vc, 32, int_max, b))); std::cout << vc_query(vc, vc_falseExpr(vc)) << std::endl; // FIXME: Actually test something // ASSERT_TRUE(false && "FIXME: Actually test something"); }
/* C pointer support: C interface to support C memory arrays in CVCL */ Expr vc_bvCreateMemoryArray(VC vc, char * arrayName) { Type bv8 = vc_bvType(vc,8); Type bv32 = vc_bvType(vc,32); Type malloced_mem0 = vc_arrayType(vc,bv32,bv8); return vc_varExpr(vc, arrayName, malloced_mem0); }
TEST(push_pop, one) { VC vc = vc_createValidityChecker(); vc_setFlags(vc, 'n'); vc_setFlags(vc, 'd'); vc_setFlags(vc, 'p'); // vc_setFlags(vc,'v'); // vc_setFlags(vc,'s'); Type bv8 = vc_bvType(vc, 8); Expr a = vc_varExpr(vc, "a", bv8); Expr ct_0 = vc_bvConstExprFromInt(vc, 8, 0); Expr a_eq_0 = vc_eqExpr(vc, a, ct_0); int query = vc_query(vc, a_eq_0); printf("query = %d\n", query); vc_push(vc); query = vc_query(vc, a_eq_0); vc_pop(vc); printf("query = %d\n", query); vc_DeleteExpr(a_eq_0); vc_DeleteExpr(a); vc_Destroy(vc); // FIXME: Actually test something // ASSERT_TRUE(false && "FIXME: Actually test something"); }
// FIXME: this test name sucks! TEST(x,one) { VC vc = vc_createValidityChecker(); vc_setFlags(vc,'n'); vc_setFlags(vc,'d'); vc_setFlags(vc,'p'); Expr nresp1 = vc_varExpr(vc, "nresp1", vc_bv32Type(vc)); Expr packet_get_int0 = vc_varExpr(vc, "packet_get_int0", vc_bv32Type(vc)); Expr sz = vc_varExpr(vc, "sz", vc_bv32Type(vc)); Expr d0,d1,d2; Expr exprs[] = { // nresp1 == packet_get_int0 vc_eqExpr(vc, nresp1, packet_get_int0), // nresp1 > 0 vc_bvGtExpr(vc, nresp1, vc_bv32ConstExprFromInt(vc, 0)), // sz == nresp1 * 4 vc_eqExpr(vc, sz, d0=vc_bv32MultExpr(vc, nresp1, vc_bv32ConstExprFromInt(vc, 4))), // sz > nresp1 || sz < 0 vc_orExpr(vc, d1=vc_sbvGeExpr(vc, sz, nresp1), d2=vc_sbvLtExpr(vc, sz, vc_bv32ConstExprFromInt(vc, 0))), }; Expr res = vc_andExprN(vc, exprs, sizeof(exprs)/sizeof(exprs[0])); //vc_printExpr(vc, res); vc_query(vc,res); vc_DeleteExpr(nresp1); vc_DeleteExpr(packet_get_int0); vc_DeleteExpr(sz); vc_DeleteExpr(d0); vc_DeleteExpr(d1); vc_DeleteExpr(d2); vc_DeleteExpr(exprs[0]); vc_DeleteExpr(exprs[1]); vc_DeleteExpr(exprs[2]); vc_DeleteExpr(exprs[3]); vc_DeleteExpr(res); vc_Destroy(vc); // FIXME: Actually test something //ASSERT_TRUE(false && "FIXME: Actually test something"); }
void test9 (void) { VC vc = vc_createValidityChecker (((void *) 0)); Type bv32 = vc_bvType (vc, 32); Expr zero = vc_bvConstExprFromInt (vc, 32, 0); Expr one = vc_bvConstExprFromInt (vc, 32, 1); Expr a = vc_varExpr (vc, "a", bv32); Expr three = vc_bvConstExprFromInt (vc, 32, 3); Expr three64 = vc_bvSignExtend (vc, three, 64); Expr a64 = vc_bvSignExtend (vc, a, 64); Expr prod64 = vc_bvMultExpr (vc, 64, a64, three64); Expr min = vc_bvConstExprFromInt (vc, 32, (-2147483647 - 1)); Expr max = vc_bvConstExprFromInt (vc, 32, 2147483647); Expr prod64_sge_min = vc_bvSGeExpr (vc, prod64, min); Expr prod64_sle_max = vc_bvSLeExpr (vc, prod64, max); Expr prod64_sge_min_and_sle_max = vc_andExpr (vc, prod64_sge_min, prod64_sle_max); vc_assertFormula (vc, prod64_sge_min_and_sle_max); Expr D3 = vc_varExpr (vc, "D3", bv32); Expr prod = vc_bvMultExpr (vc, 32, a, three); Expr D3_eq_prod = vc_eqExpr (vc, D3, prod); vc_assertFormula (vc, D3_eq_prod); Expr D4 = vc_varExpr (vc, "D4", bv32); Expr D3_sle_a_cond = vc_bvSLeExpr (vc, D3, a); Expr D3_sle_a_expr = vc_iteExpr (vc, D3_sle_a_cond, one, zero); Expr D4_eq_D3_sle_a_expr = vc_eqExpr (vc, D4, D3_sle_a_expr); vc_assertFormula (vc, D4_eq_D3_sle_a_expr); Expr D6 = vc_varExpr (vc, "D6", bv32); Expr D3_slt_a_cond = vc_bvSLtExpr (vc, D3, a); Expr D3_slt_a_expr = vc_iteExpr (vc, D3_slt_a_cond, one, zero); Expr D6_eq_D3_slt_a_expr = vc_eqExpr (vc, D6, D3_slt_a_expr); vc_assertFormula (vc, D6_eq_D3_slt_a_expr); Expr zero_lt_a = vc_bvSLtExpr (vc, zero, a); vc_assertFormula (vc, zero_lt_a); Expr D4_eq_one = vc_eqExpr (vc, D4, one); Expr not_D4_eq_one = vc_notExpr (vc, D4_eq_one); vc_query (vc, not_D4_eq_one); Expr D6_eq_one = vc_eqExpr (vc, D6, one); Expr not_D6_eq_one = vc_notExpr (vc, D6_eq_one); vc_query (vc, not_D6_eq_one); vc_destroyValidityChecker(vc); }
void test11() { Flags flags = vc_createFlags(); VC vc = vc_createValidityChecker(flags); Type a = vc_createType(vc, "a"); Type aa = vc_funType1(vc, a, a); Op f = vc_createOp(vc, "f", aa); Expr x = vc_varExpr(vc, "x", a); Expr fx = vc_funExpr1(vc, f, x); Expr ffx = vc_funExpr1(vc, f, fx); Expr e = vc_eqExpr(vc, x, fx); Expr expectE = vc_eqExpr(vc, fx, ffx); Expr newE = vc_substExpr(vc, e, &x, 1, &fx, 1); FatalAssert(expectE == newE, "Expected equal"); vc_destroyValidityChecker(vc); }
void test10() { Flags flags = vc_createFlags(); VC vc = vc_createValidityChecker(flags); Type a = vc_createType(vc, "a"); Type aa = vc_funType1(vc, a, a); Op f1 = vc_createOp(vc, "f", aa); Type aa2; Op f2 = vc_lookupOp(vc, "f", &aa2); FatalAssert(f2 != NULL, "Expected f2 not NULL"); FatalAssert(f1 == f2, "Expected equal"); Expr x = vc_varExpr(vc, "x", a); Expr f1x = vc_funExpr1(vc, f1, x); Expr f2x = vc_funExpr1(vc, f2, x); Expr eq = vc_eqExpr(vc, f1x, f2x); int res = vc_query(vc, eq); printf("eq: %d\n", res); vc_destroyValidityChecker(vc); }
void go (enum ifaceflag_t f) { VC vc; vc = vc_createValidityChecker (); vc_setInterfaceFlags(vc, f, 0); //vc_setFlags(vc,'s',0); // CVC_FILE is a macro that expands to a file path vc_parseExpr(vc, CVC_FILE); Expr a = vc_varExpr(vc, "a", vc_bvType(vc, 8)); Expr ct_0 = vc_bvConstExprFromInt(vc, 8, 0); Expr a_eq_0 = vc_eqExpr(vc, a, ct_0); int query = vc_query(vc, a_eq_0); vc_Destroy (vc); ASSERT_TRUE(false && "FIXME: Actually test something"); }
int main(int argc, char** argv) { int width=8; VC handle = vc_createValidityChecker(); // Create variable "x" Expr x = vc_varExpr(handle, "x", vc_bvType(handle, width)); // Create bitvector x + x Expr xPlusx = vc_bvPlusExpr(handle, width, x, x); // Create bitvector constant 2 Expr two = vc_bvConstExprFromInt(handle, width, 2); // Create bitvector 2*x Expr xTimes2 = vc_bvMultExpr(handle, width, two, x); // Create bool expression x + x = 2*x Expr equality = vc_eqExpr(handle, xPlusx , xTimes2); vc_assertFormula(handle, vc_trueExpr(handle) ); // We are asking STP: ∀ x. true → ( x + x = 2*x ) // This should be VALID. printf("######First Query\n"); handleQuery(handle, equality); // We are asking STP: ∀ x. true → ( x + x = 2 ) // This should be INVALID. printf("######Second Query\n"); // Create bool expression x + x = 2 Expr badEquality = vc_eqExpr(handle, xPlusx , two); handleQuery(handle, badEquality); // Clean up vc_Destroy(handle); return 0; }
TEST(push_pop, two) { VC vc = vc_createValidityChecker(); vc_setFlags(vc, 'n'); vc_setFlags(vc, 'd'); vc_setFlags(vc, 'p'); vc_setFlags(vc, 'v'); vc_setFlags(vc, 's'); vc_setFlags(vc, 'c'); vc_push(vc); Type bv8 = vc_bvType(vc, 8); Expr a = vc_varExpr(vc, "a", bv8); Expr ct_0 = vc_bvConstExprFromInt(vc, 8, 0); Expr a_eq_0 = vc_eqExpr(vc, a, ct_0); int query; // query = vc_query(vc, a_eq_0); // printf("query = %d\n", query); Expr a_neq_0 = vc_notExpr(vc, a_eq_0); vc_assertFormula(vc, a_eq_0); vc_printAsserts(vc); vc_push(vc); Expr queryexp = vc_eqExpr(vc, a, vc_bvConstExprFromInt(vc, 8, 0)); vc_printExpr(vc, queryexp); query = vc_query(vc, queryexp); vc_printCounterExample(vc); vc_pop(vc); vc_pop(vc); printf("query = %d\n", query); // FIXME: Actually test something // ASSERT_TRUE(false && "FIXME: Actually test something"); }
TEST(extend_adder_notexpr, one) { VC vc = vc_createValidityChecker(); vc_setFlags(vc, 'n'); vc_setFlags(vc, 'd'); // 8-bit variable 'x' Expr x = vc_varExpr(vc, "x", vc_bvType(vc, 8)); // 32 bit constant value 1 Expr one = vc_bvConstExprFromInt(vc, 32, 1); // 24 bit constant value 0 Expr bit24_zero = vc_bvConstExprFromInt(vc, 24, 0); // 32 bit constant value 0 Expr bit32_zero = vc_bvConstExprFromInt(vc, 32, 0); // Extending 8-bit variable to 32-bit value Expr zero_concat_x = vc_bvConcatExpr(vc, bit24_zero, x); Expr xp1 = vc_bvPlusExpr(vc, 32, zero_concat_x, one); // Insteading of concat operation, I also tried with SignExtend // Expr signextend_x=vc_bvSignExtend(vc,x,32); // Expr xp1=vc_bvPlusExpr(vc,32,signextend_x,one); // x+1=0 Expr eq = vc_eqExpr(vc, xp1, bit32_zero); // x+1!=0 eq = vc_notExpr(vc, eq); vc_query(vc, eq); vc_printCounterExample(vc); // FIXME: Actually test something // ASSERT_TRUE(false && "FIXME: Actually test something"); }
Expr parse() { char token[15]; Expr expr = NULL, leftExpr, rightExpr, eqExpr; readToken(token); switch(token[0]){ case '+': leftExpr = parse(); rightExpr = parse(); expr = vc_plusExpr(vc, leftExpr, rightExpr); break; case '-': if(token[1] == '\0'){ leftExpr = parse(); rightExpr = parse(); expr = vc_minusExpr(vc, leftExpr, rightExpr); } else{ // assert token[1] is a digit expr = parseNumber(token); } break; case '*': leftExpr = parse(); rightExpr = parse(); expr = vc_multExpr(vc, leftExpr, rightExpr); break; case '<': if (token[1] == '='){ leftExpr = parse(); rightExpr = parse(); expr = vc_leExpr(vc, leftExpr, rightExpr); } else{ leftExpr = parse(); rightExpr = parse(); expr = vc_ltExpr(vc, leftExpr, rightExpr); } break; case '>': if (token[1] == '='){ leftExpr = parse(); rightExpr = parse(); expr = vc_geExpr(vc, leftExpr, rightExpr); } else{ leftExpr = parse(); rightExpr = parse(); expr = vc_gtExpr(vc, leftExpr, rightExpr); } break; case '=': leftExpr = parse(); rightExpr = parse(); expr = vc_eqExpr(vc, leftExpr, rightExpr); break; case '!': // assert token[1] == '='; leftExpr = parse(); rightExpr = parse(); eqExpr = vc_eqExpr(vc, leftExpr, rightExpr); linkedlist_add(&exprPool, eqExpr); expr = vc_notExpr(vc, eqExpr); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': expr = parseNumber(token); break; case 'x': expr = hashmap_get(&vars, token); if (expr == NULL){ expr = vc_varExpr(vc, token, intType); char* token_copy = (char*) malloc(sizeof(char)*(strlen(token)+1)); strcpy(token_copy, token); hashmap_put(&vars, token_copy, expr); linkedlist_add(&exprPool, expr); } return expr; case 'r': expr = hashmap_get(&vars, token); if (expr == NULL){ expr = vc_varExpr(vc, token, realType); char* token_copy = (char*) malloc(sizeof(char)*(strlen(token)+1)); strcpy(token_copy, token); hashmap_put(&vars, token_copy, expr); linkedlist_add(&exprPool, expr); } return expr; default: printf( "%s", token); throwRuntimeException( "unexpected type of token" ); } linkedlist_add(&exprPool, expr); return expr; }
TEST(reported_issue_120, one) { VC vc = vc_createValidityChecker(); // Numbers will be non-negatives integers bounded at 2^32 Type bv32 = vc_bvType(vc, 32); // Determine whether the following equations are satisfiable: // v + 4 = n // 4 = n // Construct variable n Expr n = vc_varExpr(vc, "n", bv32); // Construct v + 4 Expr v = vc_varExpr(vc, "v", bv32); Expr ct_4 = vc_bvConstExprFromInt(vc, 32, 4); Expr add_v_4 = vc_bvPlusExpr(vc, 32, v, ct_4); // Because numbers are represented as bit vectors, // addition can roll over. So construct a constraint // expresses that v+4 does not overflow the bounds: // v + 4 >= v // Expr ge = vc_bvGeExpr(vc, add_v_4, v); // Push a new context printf("Push\n"); vc_push(vc); // Assert v + 4 = n printf("Assert v + 4 = n\n"); Expr f_add = vc_eqExpr(vc, add_v_4, n); vc_assertFormula(vc, f_add); vc_printExpr(vc, f_add); printf("\n------\n"); // Assert the bounds constraint printf("Assert v + 4 >= v\n"); vc_assertFormula(vc, ge); vc_printExpr(vc, ge); printf("\n------\n"); // Assert 4 = n printf("Assert 4 = n\n"); Expr f_numeq = vc_eqExpr(vc, ct_4, n); vc_assertFormula(vc, f_numeq); vc_printExpr(vc, f_numeq); printf("\n------\n"); // Check for satisfiability printf("Check\n"); vc_printAsserts(vc); printf("\n------\n"); int query = vc_query(vc, vc_falseExpr(vc)); ASSERT_EQ(query, 0); // Pop context printf("Pop\n"); vc_pop(vc); printf("query = %d\n", query); }
// FIXME: Needs better name TEST(Leaks, leak) { for (int i = 0; i < 10; i++) { VC vc; vc = vc_createValidityChecker(); vc_setFlags(vc, 'n'); vc_setFlags(vc, 'd'); vc_setFlags(vc, 'p'); // create 50 expression Expr a1 = vc_varExpr(vc, "a1", vc_bv32Type(vc)); Expr a2 = vc_varExpr(vc, "a2", vc_bv32Type(vc)); Expr a3 = vc_varExpr(vc, "a3", vc_bv32Type(vc)); Expr a4 = vc_varExpr(vc, "a4", vc_bv32Type(vc)); Expr a5 = vc_varExpr(vc, "a5", vc_bv32Type(vc)); Expr a6 = vc_varExpr(vc, "a6", vc_bv32Type(vc)); Expr a7 = vc_varExpr(vc, "a7", vc_bv32Type(vc)); Expr a8 = vc_varExpr(vc, "a8", vc_bv32Type(vc)); Expr a9 = vc_varExpr(vc, "a9", vc_bv32Type(vc)); Expr a10 = vc_varExpr(vc, "a10", vc_bv32Type(vc)); Expr a11 = vc_varExpr(vc, "a11", vc_bv32Type(vc)); Expr a12 = vc_varExpr(vc, "a12", vc_bv32Type(vc)); Expr a13 = vc_varExpr(vc, "a13", vc_bv32Type(vc)); Expr a14 = vc_varExpr(vc, "a14", vc_bv32Type(vc)); Expr a15 = vc_varExpr(vc, "a15", vc_bv32Type(vc)); Expr a16 = vc_varExpr(vc, "a16", vc_bv32Type(vc)); Expr a17 = vc_varExpr(vc, "a17", vc_bv32Type(vc)); Expr a18 = vc_varExpr(vc, "a18", vc_bv32Type(vc)); Expr a19 = vc_varExpr(vc, "a19", vc_bv32Type(vc)); Expr a20 = vc_varExpr(vc, "a20", vc_bv32Type(vc)); Expr a21 = vc_varExpr(vc, "a21", vc_bv32Type(vc)); Expr a22 = vc_varExpr(vc, "a22", vc_bv32Type(vc)); Expr a23 = vc_varExpr(vc, "a23", vc_bv32Type(vc)); Expr a24 = vc_varExpr(vc, "a24", vc_bv32Type(vc)); Expr a25 = vc_varExpr(vc, "a25", vc_bv32Type(vc)); Expr a26 = vc_varExpr(vc, "a26", vc_bv32Type(vc)); Expr a27 = vc_varExpr(vc, "a27", vc_bv32Type(vc)); Expr a28 = vc_varExpr(vc, "a28", vc_bv32Type(vc)); Expr a29 = vc_varExpr(vc, "a29", vc_bv32Type(vc)); Expr a30 = vc_varExpr(vc, "a30", vc_bv32Type(vc)); Expr a31 = vc_varExpr(vc, "a31", vc_bv32Type(vc)); Expr a32 = vc_varExpr(vc, "a32", vc_bv32Type(vc)); Expr a33 = vc_varExpr(vc, "a33", vc_bv32Type(vc)); Expr a34 = vc_varExpr(vc, "a34", vc_bv32Type(vc)); Expr a35 = vc_varExpr(vc, "a35", vc_bv32Type(vc)); Expr a36 = vc_varExpr(vc, "a36", vc_bv32Type(vc)); Expr a37 = vc_varExpr(vc, "a37", vc_bv32Type(vc)); Expr a38 = vc_varExpr(vc, "a38", vc_bv32Type(vc)); Expr a39 = vc_varExpr(vc, "a39", vc_bv32Type(vc)); Expr a40 = vc_varExpr(vc, "a40", vc_bv32Type(vc)); Expr a41 = vc_varExpr(vc, "a41", vc_bv32Type(vc)); Expr a42 = vc_varExpr(vc, "a42", vc_bv32Type(vc)); Expr a43 = vc_varExpr(vc, "a43", vc_bv32Type(vc)); Expr a44 = vc_varExpr(vc, "a44", vc_bv32Type(vc)); Expr a45 = vc_varExpr(vc, "a45", vc_bv32Type(vc)); Expr a46 = vc_varExpr(vc, "a46", vc_bv32Type(vc)); Expr a47 = vc_varExpr(vc, "a47", vc_bv32Type(vc)); Expr a48 = vc_varExpr(vc, "a48", vc_bv32Type(vc)); Expr a49 = vc_varExpr(vc, "a49", vc_bv32Type(vc)); Expr a50 = vc_varExpr(vc, "a50", vc_bv32Type(vc)); vc_DeleteExpr(a1); vc_DeleteExpr(a2); vc_DeleteExpr(a3); vc_DeleteExpr(a4); vc_DeleteExpr(a5); vc_DeleteExpr(a6); vc_DeleteExpr(a7); vc_DeleteExpr(a8); vc_DeleteExpr(a9); vc_DeleteExpr(a10); vc_DeleteExpr(a11); vc_DeleteExpr(a12); vc_DeleteExpr(a13); vc_DeleteExpr(a14); vc_DeleteExpr(a15); vc_DeleteExpr(a16); vc_DeleteExpr(a17); vc_DeleteExpr(a18); vc_DeleteExpr(a19); vc_DeleteExpr(a20); vc_DeleteExpr(a21); vc_DeleteExpr(a22); vc_DeleteExpr(a23); vc_DeleteExpr(a24); vc_DeleteExpr(a25); vc_DeleteExpr(a26); vc_DeleteExpr(a27); vc_DeleteExpr(a28); vc_DeleteExpr(a29); vc_DeleteExpr(a30); vc_DeleteExpr(a31); vc_DeleteExpr(a32); vc_DeleteExpr(a33); vc_DeleteExpr(a34); vc_DeleteExpr(a35); vc_DeleteExpr(a36); vc_DeleteExpr(a37); vc_DeleteExpr(a38); vc_DeleteExpr(a39); vc_DeleteExpr(a40); vc_DeleteExpr(a41); vc_DeleteExpr(a42); vc_DeleteExpr(a43); vc_DeleteExpr(a44); vc_DeleteExpr(a45); vc_DeleteExpr(a46); vc_DeleteExpr(a47); vc_DeleteExpr(a48); vc_DeleteExpr(a49); vc_DeleteExpr(a50); vc_Destroy(vc); } }
void test4(int regressLevel) { VC vc = vc_createValidityChecker(NULL); // Check x >= 10 /\ x >= 40 /\ y <= 0 --> // x >= 1 /\ y < 10 Type r = vc_realType(vc); Expr x = vc_varExpr(vc, "x", r); Expr y = vc_varExpr(vc, "y", r); Expr ten = vc_ratExpr(vc, 10, 1); Expr ge = vc_geExpr(vc, x, ten); Expr forty = vc_ratExpr(vc, 40, 1); Expr ge2 = vc_geExpr(vc, x, forty); Expr zero = vc_ratExpr(vc, 0, 1); Expr ge3 = vc_leExpr(vc, y, zero); Expr children[3]; Expr hyp, one, conc, query; int i; children[0] = ge; children[1] = ge2; children[2] = ge3; hyp = vc_andExprN(vc, children, 3); vc_deleteType(r); vc_deleteExpr(ge); vc_deleteExpr(forty); vc_deleteExpr(ge2); vc_deleteExpr(zero); vc_deleteExpr(ge3); one = vc_ratExpr(vc, 1, 1); ge = vc_geExpr(vc, x, one); ge2 = vc_ltExpr(vc, y, ten); conc = vc_andExpr(vc, ge, ge2); query = vc_impliesExpr(vc, hyp, conc); vc_deleteExpr(x); vc_deleteExpr(y); vc_deleteExpr(ten); vc_deleteExpr(hyp); vc_deleteExpr(one); vc_deleteExpr(ge); vc_deleteExpr(ge2); vc_deleteExpr(conc); for (i = 0; i < 100*regressLevel; i++) vc_query(vc, query); vc_deleteExpr(query); vc_destroyValidityChecker(vc); }
// Create a variable with a given name and type value caml_vc_varExpr(value vc, value name, value t) { CAMLparam3(vc, name, t); CAMLreturn(alloc_Expr(vc_varExpr(VC_val(vc),String_val(name),Type_val(t)))); }
void test7() { VC vc = vc_createValidityChecker(NULL); Type r = vc_realType(vc); Type b = vc_boolType(vc); Expr x = vc_varExpr(vc, "x", r); Expr y = vc_varExpr(vc, "y", r); Expr z = vc_varExpr(vc, "z", r); Type real2real = vc_funType1(vc, r, r); Type real2bool = vc_funType1(vc, r, b); Op f = vc_createOp(vc, "f", real2real); Op p = vc_createOp(vc, "p", real2bool); Expr fx = vc_funExpr1(vc, f, x); Expr fy = vc_funExpr1(vc, f, y); Expr fxeqfy = vc_eqExpr(vc, fx, fy); Expr px = vc_funExpr1(vc, p, x); Expr py = vc_funExpr1(vc, p, y); Expr xeqy = vc_eqExpr(vc, x, y); Expr yeqx = vc_eqExpr(vc, y, x); Expr xeqz = vc_eqExpr(vc, x, z); Expr zeqx = vc_eqExpr(vc, z, x); Expr yeqz = vc_eqExpr(vc, y, z); Expr zeqy = vc_eqExpr(vc, z, y); Expr notxeqz = vc_notExpr(vc, xeqz); int c; vc_registerAtom(vc, xeqy); vc_registerAtom(vc, yeqx); vc_registerAtom(vc, xeqz); vc_registerAtom(vc, zeqx); vc_registerAtom(vc, yeqz); vc_registerAtom(vc, zeqy); vc_registerAtom(vc, px); vc_registerAtom(vc, py); vc_registerAtom(vc, fxeqfy); printf("Push\n"); vc_push(vc); printf("Assert x = y\n"); vc_assertFormula(vc, xeqy); c = printImpliedLiterals(vc); FatalAssert(c==3,"Implied literal error 0"); printf("Push\n"); vc_push(vc); printf("Assert x /= z\n"); vc_assertFormula(vc, notxeqz); c = printImpliedLiterals(vc); FatalAssert(c==4,"Implied literal error 1"); printf("Pop\n"); vc_pop(vc); printf("Pop\n"); vc_pop(vc); vc_deleteExpr(notxeqz); vc_deleteExpr(zeqy); vc_deleteExpr(yeqz); vc_deleteExpr(zeqx); vc_deleteExpr(xeqz); vc_deleteExpr(yeqx); vc_deleteExpr(xeqy); vc_deleteExpr(py); vc_deleteExpr(px); vc_deleteExpr(fxeqfy); vc_deleteExpr(fy); vc_deleteExpr(fx); vc_deleteOp(p); vc_deleteOp(f); vc_deleteType(real2bool); vc_deleteType(real2real); vc_deleteExpr(z); vc_deleteExpr(y); vc_deleteExpr(x); vc_deleteType(b); vc_deleteType(r); vc_destroyValidityChecker(vc); }
void test8 (void) { Flags flags = vc_createFlags(); /* vc_setStrSeqFlag(flags, "trace", "pushpop", 1); */ /* vc_setStrSeqFlag(flags, "trace", "assertLit", 1); */ /* vc_setStrSeqFlag(flags, "trace", "assertFactCore", 1); */ /* vc_setStrSeqFlag(flags, "trace", "assertFormula", 1); */ VC vc = vc_createValidityChecker (flags); Type bv32 = vc_bvType (vc, 32); Expr zero = vc_bvConstExprFromInt (vc, 32, 0); Expr one = vc_bvConstExprFromInt (vc, 32, 1); Expr a = vc_varExpr (vc, "a", bv32); Expr three = vc_bvConstExprFromInt (vc, 32, 3); Expr prod = vc_bvMultExpr (vc, 32, a, three); { Expr a64 = vc_bvSignExtend (vc, a, 64); Expr three64 = vc_bvSignExtend (vc, three, 64); Expr prod64 = vc_bvMultExpr (vc, 64, a64, three64); Expr max = vc_bvConstExprFromInt (vc, 32, 2147483647); Expr min = vc_bvConstExprFromInt (vc, 32, (-2147483647 - 1)); Expr prod64_sge_min = vc_bvSGeExpr (vc, prod64, min); Expr prod64_sle_max = vc_bvSLeExpr (vc, prod64, max); Expr prod64_sge_min_and_sle_max = vc_andExpr (vc, prod64_sge_min, prod64_sle_max); vc_assertFormula (vc, prod64_sge_min_and_sle_max); } Expr D4 = vc_varExpr (vc, "D4", bv32); { Expr cond = vc_bvSLtExpr (vc, a, prod); Expr test = vc_iteExpr (vc, cond, one, zero); Expr D4_eq_test = vc_eqExpr (vc, D4, test); vc_assertFormula (vc, D4_eq_test); } Expr D6 = vc_varExpr (vc, "D6", bv32); { Expr cond = vc_bvSLeExpr (vc, a, prod); Expr test = vc_iteExpr (vc, cond, one, zero); Expr D6_eq_test = vc_eqExpr (vc, D6, test); vc_assertFormula (vc, D6_eq_test); } vc_push (vc); vc_pop (vc); vc_push (vc); { Expr cond = vc_bvSLtExpr (vc, a, zero); Expr test = vc_iteExpr (vc, cond, one, zero); Expr test_eq_one = vc_eqExpr (vc, test, one); vc_assertFormula (vc, test_eq_one); vc_push (vc); { Expr D4_eq_one = vc_eqExpr (vc, D4, one); vc_query (vc, D4_eq_one); } vc_pop (vc); vc_push (vc); vc_pop (vc); vc_push (vc); vc_pop (vc); } vc_pop (vc); { Expr cond = vc_eqExpr (vc, a, zero); Expr test = vc_iteExpr (vc, cond, one, zero); Expr test_eq_one = vc_eqExpr (vc, test, one); vc_assertFormula (vc, test_eq_one); vc_push (vc); vc_pop (vc); { Expr zero_eq_one = vc_eqExpr (vc, zero, one); vc_query (vc, zero_eq_one); } } vc_destroyValidityChecker(vc); }
void test1() { Flags flags = vc_createFlags(); VC vc; Type b; Expr p, np, e; Type r, real2real; Expr x, y, fx, fy, xeqy, fxeqfy, w, z, weqx, yeqz, one, two, xeqone, xeqtwo, simp, simp2; Op f; Expr* assertions; int i, size, res; Kind k; vc_setStringFlag(flags, "dump-log", ".testc1.cvc"); vc_setStrSeqFlag(flags, "trace", "pushpop", 1); vc = vc_createValidityChecker(flags); // Check p OR ~p b = vc_boolType(vc); p = vc_varExpr(vc, "p", vc_boolType(vc)); np = vc_notExpr(vc, p); e = vc_orExpr(vc, p, np); res = check(vc, e); FatalAssert(res == 1, "Expected Valid"); FatalAssert(vc_getKind(e) == OR, "Expected TRUE for kind check"); FatalAssert(vc_getKind(vc_getType(vc, e)) == BOOLEAN, "Expected TRUE for type kind check"); vc_deleteType(b); vc_deleteExpr(p); vc_deleteExpr(np); vc_deleteExpr(e); /* Check x = y -> f(x) = f(y) */ r = vc_realType(vc); x = vc_varExpr(vc, "x", r); y = vc_varExpr(vc, "y", r); real2real = vc_funType1(vc, r, r); f = vc_createOp(vc, "f", real2real); fx = vc_funExpr1(vc, f, x); fy = vc_funExpr1(vc, f, y); xeqy = vc_eqExpr(vc, x, y); fxeqfy = vc_eqExpr(vc, fx, fy); e = vc_impliesExpr(vc, xeqy, fxeqfy); res = check(vc, e); FatalAssert(res == 1, "Expected Valid"); vc_deleteType(real2real); vc_deleteExpr(e); // Check f(x) = f(y) -> x = y e = vc_impliesExpr(vc, fxeqfy, xeqy); vc_push(vc); res = check(vc, e); FatalAssert(res == 0, "Expected Invalid"); vc_deleteExpr(e); // Get counter-example printf("Stack level: %d\n", vc_stackLevel(vc)); printf("Counter-example:\n"); assertions = vc_getCounterExample(vc, &size); for (i = 0; i < size; ++i) { vc_printExpr(vc, assertions[i]); } vc_deleteVector(assertions); printf("End of counter-example\n\n"); printf("Concrete model:\n"); assertions = vc_getConcreteModel(vc, &size); for (i = 0; i < size; ++i) { vc_printExpr(vc, assertions[i]); } vc_deleteVector(assertions); printf("End of concrete model\n\n"); // Reset to initial scope printf("Resetting\n"); vc_pop(vc); printf("Stack level: %d\n\n", vc_stackLevel(vc)); // Check w = x & x = y & y = z & f(x) = f(y) & x = 1 & z = 2 w = vc_varExpr(vc, "w", r); z = vc_varExpr(vc, "z", r); printf("Push Scope\n\n"); vc_push(vc); weqx = vc_eqExpr(vc, w, x); yeqz = vc_eqExpr(vc, y, z); one = vc_ratExpr(vc, 1, 1); two = vc_ratExpr(vc, 2, 1); xeqone = vc_eqExpr(vc, x, one); xeqtwo = vc_eqExpr(vc, x, two); newAssertion(vc, weqx); newAssertion(vc, xeqy); newAssertion(vc, yeqz); newAssertion(vc, fxeqfy); newAssertion(vc, xeqone); newAssertion(vc, xeqtwo); printf("\nsimplify(w) = "); simp = vc_simplify(vc, w); char* str = vc_printExprString(vc, simp); printf("%s\n", str); vc_deleteString(str); printf("Inconsistent?: %d\n", vc_inconsistent(vc, &assertions, &size)); check_error("Error occured during inconsistency check"); printf("Assumptions Used:\n"); for (i = 0; i < size; ++i) { vc_printExpr(vc, assertions[i]); } vc_deleteVector(assertions); printf("\nPop Scope\n\n"); vc_pop(vc); printf("simplify(w) = "); simp2 = vc_simplify(vc, w); vc_printExpr(vc, simp2); printf("\n"); printf("Inconsistent?: %d\n", vc_inconsistent(vc, &assertions, &size)); vc_deleteVector(assertions); vc_deleteType(r); vc_deleteExpr(x); vc_deleteExpr(y); vc_deleteOp(f); vc_deleteExpr(fx); vc_deleteExpr(fy); vc_deleteExpr(xeqy); vc_deleteExpr(fxeqfy); vc_deleteExpr(w); vc_deleteExpr(z); vc_deleteExpr(weqx); vc_deleteExpr(yeqz); vc_deleteExpr(one); vc_deleteExpr(two); vc_deleteExpr(xeqone); vc_deleteExpr(xeqtwo); vc_deleteExpr(simp); vc_deleteExpr(simp2); vc_destroyValidityChecker(vc); vc_deleteFlags(flags); }