Ejemplo n.º 1
0
    def test_seven(self):
        c = saw.connection.connect(reset_server=True)
        if __name__ == "__main__": saw.view(saw.LogResults())

        seven_bc = str(Path('tests', 'saw', 'test-files', 'seven.bc'))

        c.llvm_load_module('m', seven_bc).result()

        contract = {
            "pre vars": [],
            "pre conds": [],
            "pre allocated": [],
            "pre points tos": [],
            "argument vals": [],
            "post vars": [],
            "post conds": [],
            "post allocated": [],
            "post points tos": [],
            "return val": {
                "setup value": "Cryptol",
                "expression": "7 : [32]"
            }
        }

        prover = ProofScript([abc]).to_json()
        c.llvm_verify('m', 'seven', [], False, contract, prover, 'ok').result()
Ejemplo n.º 2
0
    def test_trivial(self):
        c = saw.connection.connect(reset_server=True)
        if __name__ == "__main__": saw.view(saw.LogResults())

        cry_file = str(Path('tests','saw','test-files', 'Foo.cry'))
        c.cryptol_load_file(cry_file)


        null_bc = str(Path('tests','saw','test-files', 'null.bc'))

        c.llvm_load_module('m', null_bc).result()

        contract = {
            "pre vars": [],
            "pre conds": [],
            "pre allocated": [],
            "pre points tos": [],
            "argument vals": [],
            "post vars": [],
            "post conds": [],
            "post allocated": [],
            "post points tos": [],
            "return val": {"setup value": "null"}
        }

        prover = ProofScript([abc]).to_json()
        c.llvm_verify('m', 'always_null', [], False, contract, prover, 'ok').result()
Ejemplo n.º 3
0
    def test_swap(self):
        saw.connect(reset_server=True)

        if __name__ == "__main__": saw.view(saw.LogResults())
        swap_bc = str(Path('tests', 'saw', 'test-files', 'swap.bc'))

        mod = saw.llvm_load_module(swap_bc)

        result = saw.llvm_verify(mod, 'swap', Swap())
        self.assertIs(result.is_success(), True)
Ejemplo n.º 4
0
    def test_ghost(self):

        saw.connect(reset_server=True)
        if __name__ == "__main__": saw.view(saw.LogResults())
        ghost_bc = str(Path('tests', 'saw', 'test-files', 'ghost.bc'))

        mod = saw.llvm_load_module(ghost_bc)

        counter = saw.create_ghost_variable('counter')
        get_and_increment_ov = saw.llvm_assume(
            mod, 'get_and_increment', GetAndIncrementContract(counter))
        self.assertIs(get_and_increment_ov.is_success(), True)
        f_ov = saw.llvm_verify(mod,
                               'f',
                               FContract(counter),
                               lemmas=[get_and_increment_ov])
        self.assertIs(f_ov.is_success(), True)
Ejemplo n.º 5
0
    def test_provers(self):
        saw.connect(reset_server=True)

        if __name__ == "__main__": saw.view(saw.LogResults())

        simple_thm = cry('\(x:[8]) -> x != x+1')
        self.assertTrue(saw.prove(simple_thm, ProofScript([abc])).is_valid())
        self.assertTrue(
            saw.prove(simple_thm, ProofScript([z3([])])).is_valid())

        self.assertTrue(
            saw.prove(simple_thm, ProofScript([Admit()])).is_valid())
        self.assertTrue(
            saw.prove(cry('True'), ProofScript([Trivial()])).is_valid())

        simple_non_thm = cry('\(x:[8]) -> x != 5')
        pr = saw.prove(simple_non_thm, ProofScript([z3([])]))
        self.assertFalse(pr.is_valid())
        cex = pr.get_counterexample()
        self.assertEqual(cex, [('x', BV(8, 0x05))])
Ejemplo n.º 6
0
    def test_add(self):
        saw.connect(reset_server=True)

        if __name__ == "__main__": saw.view(saw.LogResults())

        cls = saw.jvm_load_class("Add")

        add_result1 = saw.jvm_verify(cls, 'add', Add())
        self.assertIs(add_result1.is_success(), True)
        add_result2 = saw.jvm_assume(cls, 'add', Add())
        self.assertIs(add_result2.is_success(), True)

        dbl_result1 = saw.jvm_verify(cls,
                                     'dbl',
                                     Double(),
                                     lemmas=[add_result1])
        self.assertIs(dbl_result1.is_success(), True)
        dbl_result2 = saw.jvm_verify(cls,
                                     'dbl',
                                     Double(),
                                     lemmas=[add_result2])
        self.assertIs(dbl_result2.is_success(), True)
Ejemplo n.º 7
0
    def test_salsa20(self):
        c = saw.connection.connect(reset_server=True)

        if __name__ == "__main__": saw.view(saw.LogResults())

        bcname = str(Path('tests', 'saw', 'test-files', 'salsa20.bc'))
        cryname = str(Path('tests', 'saw', 'test-files', 'Salsa20.cry'))

        c.cryptol_load_file(cryname).result()
        c.llvm_load_module('m', bcname).result()

        # SetupVals
        value = {"setup value": "Cryptol", "expression": "value"}
        shift = {"setup value": "Cryptol", "expression": "shift"}
        res = {"setup value": "Cryptol", "expression": "value <<< shift"}

        y0p = {"setup value": "named", "name": "y0p"}
        y1p = {"setup value": "named", "name": "y1p"}
        y2p = {"setup value": "named", "name": "y2p"}
        y3p = {"setup value": "named", "name": "y3p"}

        y0 = {"setup value": "Cryptol", "expression": "y0"}
        y1 = {"setup value": "Cryptol", "expression": "y1"}
        y2 = {"setup value": "Cryptol", "expression": "y2"}
        y3 = {"setup value": "Cryptol", "expression": "y3"}

        y0f = {
            "setup value": "Cryptol",
            "expression": "(quarterround [y0, y1, y2, y3]) @ 0"
        }
        y1f = {
            "setup value": "Cryptol",
            "expression": "(quarterround [y0, y1, y2, y3]) @ 1"
        }
        y2f = {
            "setup value": "Cryptol",
            "expression": "(quarterround [y0, y1, y2, y3]) @ 2"
        }
        y3f = {
            "setup value": "Cryptol",
            "expression": "(quarterround [y0, y1, y2, y3]) @ 3"
        }

        yp = {"setup value": "named", "name": "yp"}
        y = {"setup value": "Cryptol", "expression": "y"}

        rr_res = {"setup value": "Cryptol", "expression": "rowround y"}
        cr_res = {"setup value": "Cryptol", "expression": "columnround y"}
        dr_res = {"setup value": "Cryptol", "expression": "doubleround y"}
        hash_res = {"setup value": "Cryptol", "expression": "Salsa20 y"}
        expand_res = {
            "setup value": "Cryptol",
            "expression": "Salsa20_expansion`{a=2}(k, n)"
        }
        crypt_res = {
            "setup value": "Cryptol",
            "expression": "Salsa20_encrypt (k, v, m)"
        }

        rotl_contract = {
            "pre vars": [{
                "server name": "value",
                "name": "value",
                "type": i32.to_json()
            }, {
                "server name": "shift",
                "name": "shift",
                "type": i32.to_json()
            }],
            "pre conds": ["0 < shift /\\ shift < 32"],
            "pre allocated": [],
            "pre points tos": [],
            "argument vals": [value, shift],
            "post vars": [],
            "post conds": [],
            "post allocated": [],
            "post points tos": [],
            "return val":
            res
        }

        qr_contract = {
            "pre vars": [{
                "server name": "y0",
                "name": "y0",
                "type": i32.to_json()
            }, {
                "server name": "y1",
                "name": "y1",
                "type": i32.to_json()
            }, {
                "server name": "y2",
                "name": "y2",
                "type": i32.to_json()
            }, {
                "server name": "y3",
                "name": "y3",
                "type": i32.to_json()
            }],
            "pre conds": [],
            "pre allocated": [{
                "server name": "y0p",
                "type": i32.to_json(),
                "mutable": True,
                "alignment": None
            }, {
                "server name": "y1p",
                "type": i32.to_json(),
                "mutable": True,
                "alignment": None
            }, {
                "server name": "y2p",
                "type": i32.to_json(),
                "mutable": True,
                "alignment": None
            }, {
                "server name": "y3p",
                "type": i32.to_json(),
                "mutable": True,
                "alignment": None
            }],
            "pre points tos": [{
                "pointer": y0p,
                "points to": y0
            }, {
                "pointer": y1p,
                "points to": y1
            }, {
                "pointer": y2p,
                "points to": y2
            }, {
                "pointer": y3p,
                "points to": y3
            }],
            "argument vals": [y0p, y1p, y2p, y3p],
            "post vars": [],
            "post conds": [],
            "post allocated": [],
            "post points tos": [{
                "pointer": y0p,
                "points to": y0f
            }, {
                "pointer": y1p,
                "points to": y1f
            }, {
                "pointer": y2p,
                "points to": y2f
            }, {
                "pointer": y3p,
                "points to": y3f
            }],
            "return val":
            None
        }

        def oneptr_update_contract(ty, res):
            return {
                "pre vars": [{
                    "server name": "y",
                    "name": "y",
                    "type": ty.to_json()
                }],
                "pre conds": [],
                "pre allocated": [{
                    "server name": "yp",
                    "type": ty.to_json(),
                    "mutable": True,
                    "alignment": None
                }],
                "pre points tos": [{
                    "pointer": yp,
                    "points to": y
                }],
                "argument vals": [yp],
                "post vars": [],
                "post conds": [],
                "post allocated": [],
                "post points tos": [{
                    "pointer": yp,
                    "points to": res
                }],
                "return val":
                None
            }

        rr_contract = oneptr_update_contract(LLVMArrayType(i32, 16), rr_res)
        cr_contract = oneptr_update_contract(LLVMArrayType(i32, 16), cr_res)
        dr_contract = oneptr_update_contract(LLVMArrayType(i32, 16), dr_res)
        hash_contract = oneptr_update_contract(LLVMArrayType(i8, 64), hash_res)

        kp = {"setup value": "named", "name": "kp"}
        np = {"setup value": "named", "name": "np"}
        ksp = {"setup value": "named", "name": "ksp"}
        k = {"setup value": "Cryptol", "expression": "k"}
        n = {"setup value": "Cryptol", "expression": "n"}
        zero = {"setup value": "Cryptol", "expression": "0 : [32]"}

        expand_contract = {
            "pre vars": [{
                "server name": "k",
                "name": "k",
                "type": LLVMArrayType(i8, 32).to_json()
            }, {
                "server name": "n",
                "name": "n",
                "type": LLVMArrayType(i8, 16).to_json()
            }],
            "pre conds": [],
            "pre allocated": [{
                "server name": "kp",
                "type": LLVMArrayType(i8, 32).to_json(),
                "mutable": True,
                "alignment": None
            }, {
                "server name": "np",
                "type": LLVMArrayType(i8, 16).to_json(),
                "mutable": True,
                "alignment": None
            }, {
                "server name": "ksp",
                "type": LLVMArrayType(i8, 64).to_json(),
                "mutable": True,
                "alignment": None
            }],
            "pre points tos": [{
                "pointer": kp,
                "points to": k
            }, {
                "pointer": np,
                "points to": n
            }],
            "argument vals": [kp, np, ksp],
            "post vars": [],
            "post conds": [],
            "post allocated": [],
            "post points tos": [{
                "pointer": ksp,
                "points to": expand_res
            }],
            "return val":
            None
        }

        vp = {"setup value": "named", "name": "vp"}
        mp = {"setup value": "named", "name": "mp"}
        v = {"setup value": "Cryptol", "expression": "v"}
        m = {"setup value": "Cryptol", "expression": "m"}

        def crypt_contract(size: int):
            return {
                "pre vars": [{
                    "server name": "k",
                    "name": "k",
                    "type": LLVMArrayType(i8, 32).to_json()
                }, {
                    "server name": "v",
                    "name": "v",
                    "type": LLVMArrayType(i8, 8).to_json()
                }, {
                    "server name": "m",
                    "name": "m",
                    "type": LLVMArrayType(i8, size).to_json()
                }],
                "pre conds": [],
                "pre allocated": [{
                    "server name": "kp",
                    "type": LLVMArrayType(i8, 32).to_json(),
                    "mutable": True,
                    "alignment": None
                }, {
                    "server name": "vp",
                    "type": LLVMArrayType(i8, 8).to_json(),
                    "mutable": True,
                    "alignment": None
                }, {
                    "server name": "mp",
                    "type": LLVMArrayType(i8, size).to_json(),
                    "mutable": True,
                    "alignment": None
                }],
                "pre points tos": [{
                    "pointer": kp,
                    "points to": k
                }, {
                    "pointer": vp,
                    "points to": v
                }, {
                    "pointer": mp,
                    "points to": m
                }],
                "argument vals": [
                    kp, vp, zero, mp, {
                        "setup value": "Cryptol",
                        "expression": (str(size) + " : [32]")
                    }
                ],
                "post vars": [],
                "post conds": [],
                "post allocated": [],
                "post points tos": [{
                    "pointer": mp,
                    "points to": crypt_res
                }],
                "return val":
                zero
            }

        prover = ProofScript([abc]).to_json()

        c.llvm_verify('m', 'rotl', [], False, rotl_contract, prover,
                      'rotl_ov').result()
        c.llvm_verify('m', 's20_quarterround', ['rotl_ov'], False, qr_contract,
                      prover, 'qr_ov').result()
        c.llvm_verify('m', 's20_rowround', ['qr_ov'], False, rr_contract,
                      prover, 'rr_ov').result()
        c.llvm_verify('m', 's20_columnround', ['rr_ov'], False, cr_contract,
                      prover, 'cr_ov').result()
        c.llvm_verify('m', 's20_doubleround', ['cr_ov', 'rr_ov'], False,
                      dr_contract, prover, 'dr_ov').result()
        c.llvm_verify('m', 's20_hash', ['dr_ov'], False, hash_contract, prover,
                      'hash_ov').result()
        c.llvm_verify('m', 's20_expand32', ['hash_ov'], False, expand_contract,
                      prover, 'expand_ov').result()
        c.llvm_verify('m', 's20_crypt32', ['expand_ov'], False,
                      crypt_contract(63), prover, 'crypt_ov').result()
Ejemplo n.º 8
0
import os
import os.path

from saw_client import LogResults, connect, llvm_load_module, view

dir_path = os.path.dirname(os.path.realpath(__file__))

connect()
view(LogResults())

path = [os.path.dirname(dir_path), "c", "libsignal-everything.bc"]
bcname = os.path.join(*path)
print(bcname)
mod = llvm_load_module(bcname)
Ejemplo n.º 9
0
    def test_swap(self):
        c = saw.connection.connect(reset_server=True)
        if __name__ == "__main__": saw.view(saw.LogResults())

        swap_bc = str(Path('tests', 'saw', 'test-files', 'swap.bc'))

        c.llvm_load_module('m', swap_bc).result()

        i32 = {"type": "primitive type", "primitive": "integer", "size": 32}

        # ServerNames
        xp_name = {"name": "xp"}
        yp_name = {"name": "yp"}

        # SetupVals
        xp = {"setup value": "named", "name": "xp"}
        yp = {"setup value": "named", "name": "yp"}
        x = {"setup value": "Cryptol", "expression": "x"}
        y = {"setup value": "Cryptol", "expression": "x"}

        contract = {
            "pre vars": [{
                "server name": "x",
                "name": "x",
                "type": i32
            }, {
                "server name": "y",
                "name": "y",
                "type": i32
            }],
            "pre conds": [],
            "pre allocated": [{
                "server name": "xp",
                "type": i32,
                "mutable": True,
                "alignment": None
            }, {
                "server name": "yp",
                "type": i32,
                "mutable": True,
                "alignment": None
            }],
            "pre points tos": [{
                "pointer": xp,
                "points to": x
            }, {
                "pointer": yp,
                "points to": y
            }],
            "argument vals": [xp, yp],
            "post vars": [],
            "post conds": [],
            "post allocated": [],
            "post points tos": [{
                "pointer": xp,
                "points to": y
            }, {
                "pointer": yp,
                "points to": x
            }],
            "return val":
            None
        }

        prover = ProofScript([abc]).to_json()
        c.llvm_verify('m', 'swap', [], False, contract, prover, 'ok').result()