示例#1
0
    def verify(self, hole_assignments, input_bits, iter_cnt=1):
        """Verify hole value assignments for the sketch with a specific input
        bit lengths with z3.

        Returns:
            A tuple of two dicts from string to ints, where the first one
            represents counterexamples for packet variables and the second for
            state group variables.
            If the hole value assignments work for the input_bits, returns
            a tuple of two empty dicts.
        """
        # Check all holes have values.
        for hole in self.sketch_generator.hole_names_:
            assert hole in hole_assignments

        # Generate a sketch file to verify the hole value assignments with
        # the specified input bit lengths.
        sketch_to_verify = self.sketch_generator.generate_sketch(
            program_file=self.program_file,
            mode=Mode.VERIFY,
            hole_assignments=hole_assignments)

        # Write sketch to a file.
        file_basename = self.sketch_name + '_verify_iter_' + str(iter_cnt)
        sketch_filename = file_basename + '.sk'
        Path(sketch_filename).write_text(sketch_to_verify)

        sketch_ir = sketch_utils.generate_ir(sketch_filename)

        z3_formula = z3_utils.get_z3_formula(sketch_ir, input_bits)

        return z3_utils.generate_counterexamples(z3_formula)
示例#2
0
 def test_conversion(self):
     # Smoke test for bool-to-int and int-to-bool conversion
     # doesn't do anything too substantial, but checks that
     # the string is being parsed and converted into z3.
     z3_utils.get_z3_formula(
         """0 = CONST BOOL 1
                                1 = CONST BOOL 0
                                2 = LT BOOL 0 1
                                3 = TIMES INT 0 1
                                4 = PLUS INT 0 1
                                5 = S INT foobar 2""", 10)
     z3_utils.get_z3_formula(
         """0 = CONST INT  1
                                1 = CONST INT  0
                                2 = AND BOOL 0 1
                                3 = OR BOOL 0 1
                                4 = XOR BOOL 0 1
                                5 = S INT foobar 2""", 10)
示例#3
0
    def test_hello(self):
        base_path = Path(__file__).parent
        sketch_ir = Path(base_path / './data/hello.dag').resolve().read_text()
        formula_from_ir = z3_utils.get_z3_formula(sketch_ir, input_bits=2)
        ir_pkt_fields, ir_state_vars = z3_utils.generate_counterexamples(
            formula_from_ir)

        formula_from_smt = z3_utils.parse_smt2_file(
            str(Path(base_path / './data/hello.smt').resolve()))
        smt_pkt_fields, smt_state_vars = z3_utils.generate_counterexamples(
            formula_from_smt)

        self.assertDictEqual(ir_pkt_fields, smt_pkt_fields)
        self.assertDictEqual(ir_state_vars, smt_state_vars)