Ejemplo n.º 1
0
    def test_solver_basic(self):
        s = get_solver()
        in1 = claripy.BVS("in1", 256)
        in2 = claripy.BVS("in2", 256)

        self.assertTrue(s.satisfiable(extra_constraints=[Sha3(in1) == Sha3(in2)]))
        self.assertTrue(s.satisfiable(extra_constraints=[Sha3(in1) != Sha3(in2)]))
        # These next two always hold anyway.
        self.assertTrue(s.satisfiable(extra_constraints=[Sha3(in1) + 1 != Sha3(in2)]))
        self.assertFalse(s.satisfiable(extra_constraints=[Sha3(in1) + 1 == Sha3(in2)]))

        s.add(in1 == in2)
        self.assertTrue(s.satisfiable(extra_constraints=[Sha3(in1) == Sha3(in2)]))
        self.assertFalse(s.satisfiable(extra_constraints=[Sha3(in1) != Sha3(in2)]))
        # These next two always hold anyway.
        self.assertTrue(s.satisfiable(extra_constraints=[Sha3(in1) + 1 != Sha3(in2)]))
        self.assertFalse(s.satisfiable(extra_constraints=[Sha3(in1) + 1 == Sha3(in2)]))

        s = get_solver()
        s.add(in1 != in2)
        self.assertFalse(s.satisfiable(extra_constraints=[Sha3(in1) == Sha3(in2)]))
        self.assertTrue(s.satisfiable(extra_constraints=[Sha3(in1) != Sha3(in2)]))
        # These next two always hold anyway.
        self.assertTrue(s.satisfiable(extra_constraints=[Sha3(in1) + 1 != Sha3(in2)]))
        self.assertFalse(s.satisfiable(extra_constraints=[Sha3(in1) + 1 == Sha3(in2)]))
Ejemplo n.º 2
0
 def test_solver_copy(self):
     s = get_solver()
     in1 = claripy.BVS("in1", 256)
     s.add(Sha3(in1) == 0)
     self.assertFalse(s.satisfiable())
     s2 = s.branch()
     self.assertFalse(s2.satisfiable())
Ejemplo n.º 3
0
 def test_cannot_combine(self):
     """If we didn't do a replace(), we cannot combine the same thing."""
     s = get_solver()
     a = claripy.BVS("a", 256)
     s.add(Sha3(a) == 8)
     s2 = s.branch()
     with self.assertRaises(ValueError):
         s.combine([s2])
Ejemplo n.º 4
0
    def test_solver_one_var(self):
        s = get_solver()
        in1 = claripy.BVS("in1", 256)

        self.assertFalse(s.satisfiable(extra_constraints=[Sha3(in1) == 42]))
        self.assertFalse(s.satisfiable(extra_constraints=[Sha3(in1) == 0]))
        self.assertTrue(s.satisfiable(extra_constraints=[Sha3(in1) == Sha3(bvv(42))]))
        self.assertTrue(s.satisfiable(extra_constraints=[Sha3(in1) == Sha3(bvv(0))]))
        self.assertTrue(
            s.satisfiable(extra_constraints=[Sha3(in1 + 1) + 2 == Sha3(bvv(0)) + 2])
        )
Ejemplo n.º 5
0
    def test_solver_three_symbols(self):
        s = get_solver()
        in1 = claripy.BVS("in1", 256)
        in2 = claripy.BVS("in2", 256)
        in3 = claripy.BVS("in2", 256)

        self.assertFalse(
            s.satisfiable(
                extra_constraints=[Sha3(in1) == Sha3(Sha3(in3)) + Sha3(Sha3(Sha3(in2)))]
            )
        )
        self.assertTrue(
            s.satisfiable(
                extra_constraints=[in1 == Sha3(Sha3(in3)) + Sha3(Sha3(Sha3(in2)))]
            )
        )
Ejemplo n.º 6
0
    def test_solver_recursive_unbalanced(self):
        s = get_solver()
        in1 = claripy.BVS("in1", 256)
        in2 = claripy.BVS("in2", 256)

        self.assertFalse(
            s.satisfiable(extra_constraints=[Sha3(Sha3(in1)) == Sha3(bvv(0))])
        )
        self.assertTrue(s.satisfiable(extra_constraints=[Sha3(Sha3(in1)) == Sha3(in2)]))
        logging.debug("here")
        self.assertTrue(s.satisfiable(extra_constraints=[Sha3(in1) == Sha3(Sha3(in2))]))

        self.assertTrue(
            s.satisfiable(extra_constraints=[Sha3(Sha3(Sha3(in1))) == Sha3(in2)])
        )
        self.assertTrue(
            s.satisfiable(extra_constraints=[Sha3(in1) == Sha3(Sha3(Sha3(in2)))])
        )
Ejemplo n.º 7
0
    def test_solver_arithmetics(self):
        s = get_solver()
        in1 = claripy.BVS("in1", 256)
        in2 = claripy.BVS("in2", 256)

        self.assertTrue(
            s.satisfiable(extra_constraints=[Sha3(in1) + 1 == Sha3(in2) + 1]))
        self.assertFalse(
            s.satisfiable(extra_constraints=[Sha3(in1) + 1 == Sha3(in2) + 2]))
        self.assertFalse(
            s.satisfiable(extra_constraints=[Sha3(in1) + 1 == Sha3(in2) + 2]))

        self.assertTrue(
            s.satisfiable(extra_constraints=[Sha3(in1 + 1) == Sha3(in2 + 1)]))
        self.assertTrue(
            s.satisfiable(extra_constraints=[Sha3(in1 + 1) == Sha3(in2 - 1)]))
        self.assertFalse(
            s.satisfiable(
                extra_constraints=[Sha3(in1 + 1) + 42 == Sha3(in2 - 1)]))
Ejemplo n.º 8
0
    def __init__(self, env=None):
        self.env = env
        self.pc = 0  # pylint:disable=invalid-name
        self.stack = []

        # TODO: explain
        self.score = 0

        self.memory = memory.Memory()

        # That's an override to the storage in the blockchain.
        # It's the storage that has been written at the end of the execution of
        # the contract.
        self.storage_written = {}

        # Storage read while executing the contract.
        self.storage_read = {}

        self.calls = []
        self.selfdestruct_to = None

        self.solver = utils.get_solver()
Ejemplo n.º 9
0
    def test_solver_recursive(self):
        s = get_solver()
        in1 = claripy.BVS("in1", 256)
        in2 = claripy.BVS("in2", 256)

        self.assertFalse(
            s.satisfiable(extra_constraints=[Sha3(Sha3(in1)) == 0]))
        self.assertFalse(
            s.satisfiable(extra_constraints=[Sha3(Sha3(in1)) == Sha3(0)]))
        self.assertTrue(
            s.satisfiable(
                extra_constraints=[Sha3(Sha3(in1)) == Sha3(Sha3(0))]))

        s.add(Sha3(in1) == Sha3(in2))
        self.assertTrue(s.satisfiable())

        self.assertTrue(
            s.satisfiable(
                extra_constraints=[Sha3(Sha3(in1) + 1) == Sha3(Sha3(in2) +
                                                               1)]))

        s.add(Sha3(Sha3(in1) + 1) == Sha3(Sha3(in2) + 1))
        self.assertTrue(s.satisfiable())

        self.assertFalse(
            s.satisfiable(
                extra_constraints=[Sha3(Sha3(in1) + 2) == Sha3(Sha3(in2) +
                                                               1)]))
        self.assertFalse(
            s.satisfiable(
                extra_constraints=[Sha3(Sha3(in1)) + 1 == Sha3(Sha3(in2) +
                                                               1)]))

        s.add(Sha3(Sha3(in1)) + 3 == Sha3(Sha3(in2)) + 1)
        self.assertFalse(s.satisfiable())

        s_copy = s.branch()
        self.assertFalse(s_copy.satisfiable())
Ejemplo n.º 10
0
    def __init__(self, env=None):
        self.env = env
        self.pc = 0  # pylint:disable=invalid-name
        self.stack = []

        # The "depth" we needed to reach that state. Basically how many branches
        # we executed to reach it (can be increased more if we got there by fuzzing).
        self.depth = 0

        self.memory = memory.Memory()

        # That's an override to the storage in the blockchain.
        # It's the storage that has been written at the end of the execution of
        # the contract.
        self.storage_written = {}

        # Storage read while executing the contract.
        self.storage_read = {}

        self.calls = []
        self.selfdestruct_to = None

        self.solver = utils.get_solver()
Ejemplo n.º 11
0
 def test_sha3_equality_different_length(self):
     a = claripy.BVV(1, 8)
     s = get_solver()
     s.add(Sha3(a) == Sha3(claripy.BVV(1, 256)))
     self.assertFalse(s.satisfiable())
Ejemplo n.º 12
0
 def test_sha3_unequality(self):
     a = claripy.BVV(1, 256)
     s = get_solver()
     s.add(Sha3(a) != Sha3(claripy.BVV(1, 256)))
     self.assertFalse(s.satisfiable())