Esempio n. 1
0
    def infer(self, loc, template, uks, exprs, dtraces, inps):
        assert isinstance(loc, str), loc
        assert sageutil.is_sage_expr(template), template
        assert isinstance(uks, list), uks
        assert isinstance(exprs, set) and exprs, exprs
        assert isinstance(dtraces, DTraces) and dtraces, dtraces
        assert isinstance(inps, Inps) and inps, inps

        vs = tuple(self.invdecls[loc])
        cache = set()
        eqts = set()  #results
        exprs = list(exprs)

        newInps = []
        curIter = 0
        while True:
            curIter += 1
            logger.debug("{}: iter {} infer using {} exprs".format(
                loc, curIter, len(exprs)))

            newEqts = Miscs.solveEqts(exprs, uks, template)
            unchecks = [eqt for eqt in newEqts if eqt not in cache]

            if not unchecks:
                logger.debug("{}: no new results -- break".format(loc))
                break

            logger.debug('{}: {} candidates:\n{}'.format(
                loc, len(newEqts), '\n'.join(map(str, newEqts))))

            logger.debug("{}: check {} unchecked ({} candidates)".format(
                loc, len(unchecks), len(newEqts)))

            dinvs = DInvs.mk(loc, Invs.mk(map(Inv, unchecks)))
            dInps, dCexs, dinvs = self.prover.checkRange(dinvs, inps=None)

            if dInps: newInps.append(dInps)
            _ = [eqts.add(inv) for inv in dinvs[loc] if not inv.isDisproved]
            _ = [
                cache.add(inv.inv) for inv in dinvs[loc]
                if inv.stat is not None
            ]

            if loc not in dCexs:
                logger.debug(
                    "{}: no disproved candidates -- break".format(loc))
                break

            cexs = Traces.extract(dCexs[loc], vs)
            exprs_ = cexs.instantiate(template, None)

            logger.debug("{}: {} new cex exprs".format(loc, len(exprs_)))
            exprs.extend(exprs_)

        return eqts, newInps
Esempio n. 2
0
    def guessCheck(self, loc, term, minV, maxV, ubMinV, ubMaxV, disproves):
        assert minV <= maxV, (minV, maxV, term)
        assert ubMinV < ubMaxV, (ubMinV, ubMaxV)
        #assert isinstance(traces, DTraces), traces
        assert isinstance(disproves, set), disproves

        if minV == maxV: return maxV
        elif maxV - minV == 1:
            if minV in disproves:
                return maxV
            inv = Inv(term <= minV)
            inv_ = DInvs.mk(loc, Invs.mk([inv]))
            _, dCexs, _ = self.prover.check(inv_, None, ubMinV, ubMaxV)

            if loc in dCexs:
                assert dCexs[loc]
                disproves.add(minV)
                return maxV
            else:
                return minV

        v = sage.all.ceil((maxV + minV) / 2.0)
        inv = Inv(term <= v)
        inv_ = DInvs.mk(loc, Invs.mk([inv]))
        _, dCexs, _ = self.prover.check(inv_, None, ubMinV, ubMaxV)

        if loc in dCexs:  #disproved
            assert dCexs[loc]
            cexs = Traces.extract(dCexs[loc],
                                  tuple(self.invdecls[loc]),
                                  useOne=False)
            minV = int(max(cexs.myeval(term)))
            disproves.add(v)
        else:
            maxV = v

        return self.guessCheck(
            loc,
            term,  #traces, inps,
            minV,
            maxV,
            ubMinV,
            ubMaxV,
            disproves)
Esempio n. 3
0
    def naive(self, loc, term, minV, maxV, ubMinV, ubMaxV, disproves):
        assert minV <= maxV, (minV, maxV, term)
        assert ubMinV < ubMaxV, (ubMinV, ubMaxV)
        #assert isinstance(traces, DTraces), traces
        assert isinstance(disproves, set), disproves

        for v in range(minV - 1, maxV + 1):
            inv = Inv(term <= v)
            inv_ = DInvs.mk(loc, Invs.mk([inv]))
            _, dCexs, _ = self.prover.check(inv_, None, ubMinV, ubMaxV)
            if loc in dCexs:  #disproved
                assert dCexs[loc]
                cexs = Traces.extract(dCexs[loc],
                                      tuple(self.invdecls[loc]),
                                      useOne=False)
                minV = int(max(cexs.myeval(term)))
                disproves.add(v)
            else:
                return v