Exemplo n.º 1
0
    def instr(cls, filename, filePostfix, locs_d, mkStmts):
        stmts = cls.mkProgStmts(filename, locs_d, mkStmts)

        fname = filename + filePostfix
        CM.vwrite(fname, '\n'.join(stmts))
        CM.vcmd("astyle -Y {}".format(fname))
        return fname
Exemplo n.º 2
0
    def instrAssertsRT(self,
                       invs,
                       inps,
                       inps_d,
                       invdecls,
                       lineno,
                       startFun="mainQ"):
        assert isinstance(invs, set) and invs, dinvs
        assert isinstance(inps, set), inps
        assert (inps_d is None
                or (isinstance(inps_d, OrderedDict) and inps_d)), inps_d

        #mk_f(invs, invdecls, lineno)
        _mk = lambda myinvs, _, loc: RT.mkAssertInvs(myinvs, loc)
        stmts = self.mkProgStmts(self.filename, invs, invdecls, lineno, _mk)

        #comment startFun(..argv[]) and add symbolic input
        stmts_ = []
        for stmt in stmts:
            if startFun in stmt and "argv" in stmt:
                for varname, (vartyp, (minV, maxV)) in inps_d.iteritems():
                    stmt = RT.mkSymbolic(varname, vartyp)
                    stmts_.append(stmt)
                    if minV is not None and maxV is not None:
                        stmts__ = RT.mkAssumeRanges(varname, minV, maxV)
                        stmts_.extend(stmts__)

                #klee_assume(x!=0 || y!=1); klee_assume(x!=2 || y!=3);
                if inps:
                    stmts__ = RT.mkAssumeInps(inps)
                    stmts_.extend(stmts__)

                #call mainQ(inp0, ..);
                stmt = "{}({});".format(startFun,
                                        ",".join(map(str, inps_d.iterkeys())))
                stmts_.append(stmt)

            elif (all(x in stmt for x in ['assert', '(', ')', ';'])
                  and '//' not in stmt):

                stmt = RT.mkAssert(stmt)
                stmts_.append(stmt)
            else:
                stmts_.append(stmt)

        stmts = stmts_

        #add header, e.g., #include ...
        stmts = RT.mkHeaders() + stmts

        fname = self.filename + ".assert.c"
        CM.vwrite(fname, '\n'.join(stmts))
        CM.vcmd("astyle -Y {}".format(fname))
        return fname
Exemplo n.º 3
0
    def instrAsserts(self, invs, inps, inpsd, invdecls, startFun="mainQ"):
        assert isinstance(invs, dict), invs
        assert (inpsd is None
                or (isinstance(inpsd, OrderedDict) and inpsd)), inpsd
        assert isinstance(invdecls, OrderedDict) and invdecls, invdecls
        assert inps is None or (isinstance(inps, set) and inps), inps

        inpParts = self.mkPrintfArgs(inpsd) if inpsd else (None, None)

        _mk = lambda invs, loc: KLEE.mkAssertInvs(
            invs, loc, inpParts, self.mkPrintfArgs(invdecls[loc]))

        stmts = self.mkProgStmts(self.filename, invs, _mk)
        #comment startFun(..argv[]) and add symbolic input
        stmts_ = []
        for stmt in stmts:
            if startFun in stmt and "argv" in stmt:
                # stmt = "//" + stmt
                # stmts_.append(stmt)
                for varname, (vartyp, (minV, maxV)) in inpsd.iteritems():
                    stmt = KLEE.mkSymbolic(varname, vartyp)
                    stmts_.append(stmt)
                    if minV is not None and maxV is not None:
                        stmts__ = KLEE.mkAssumeRanges(varname, minV, maxV)
                        stmts_.extend(stmts__)

                #klee_assume(x!=0 || y!=1); klee_assume(x!=2 || y!=3);
                if inps:
                    ss = inpsd.keys()
                    #so that assertions are in order
                    #(KLEE will give diff outputs based on order)
                    inps = sorted(inps)
                    stmts__ = KLEE.mkAssumeInps(ss, inps)
                    stmts_.extend(stmts__)

                #call mainQ(inp0, ..);
                stmt = "{}({});".format(startFun,
                                        ",".join(map(str, inpsd.iterkeys())))
                stmts_.append(stmt)
            else:
                stmts_.append(stmt)

        stmts = stmts_

        #add header
        stmts = ["#include <klee/klee.h>"] + stmts
        uid = str(hash(str(invs))).replace("-", "_")
        fname = "{}_{}.{}".format(self.filename, uid, "klee_assert.c")
        CM.vwrite(fname, '\n'.join(stmts))
        CM.vcmd("astyle -Y {}".format(fname))
        return fname
Exemplo n.º 4
0
    def instrKleeAsserts(self, dinvs, inps, inps_d, startFun="mainQ"):
        assert isinstance(dinvs, DInvs), dinvs
        assert (inps_d is None
                or (isinstance(inps_d, OrderedDict) and inps_d)), inps_d

        assert isinstance(inps, Inps), inps

        if inps_d:
            parts = self.mkPrintfArgs(inps_d)
        else:
            parts = (None, None)
        _mk = lambda invs, loc: KLEE.mkAssertInvs(invs, loc, parts)
        stmts = self.mkProgStmts(self.filename, dinvs, _mk)
        #comment startFun(..argv[]) and add symbolic input
        stmts_ = []
        for stmt in stmts:
            if startFun in stmt and "argv" in stmt:
                # stmt = "//" + stmt
                # stmts_.append(stmt)
                for varname, (vartyp, (minV, maxV)) in inps_d.iteritems():
                    stmt = KLEE.mkSymbolic(varname, vartyp)
                    stmts_.append(stmt)
                    if minV is not None and maxV is not None:
                        stmts__ = KLEE.mkAssumeRanges(varname, minV, maxV)
                        stmts_.extend(stmts__)

                #klee_assume(x!=0 || y!=1); klee_assume(x!=2 || y!=3);
                if inps:
                    stmts__ = KLEE.mkAssumeInps(inps)
                    stmts_.extend(stmts__)

                #call mainQ(inp0, ..);
                stmt = "{}({});".format(startFun,
                                        ",".join(map(str, inps_d.iterkeys())))
                stmts_.append(stmt)
            else:
                stmts_.append(stmt)

        stmts = stmts_

        #add header
        stmts = ["#include <klee/klee.h>"] + stmts

        fname = self.filename + ".klee_assert.c"
        CM.vwrite(fname, '\n'.join(stmts))
        CM.vcmd("astyle -Y {}".format(fname))
        return fname
Exemplo n.º 5
0
    def myrun(cls, code):
        filename = "Test"
        filename_ext = filename + cls.file_ext
        CM.vwrite(filename_ext, code)
        
        if cls.cmd_compile:
            cmd = cls.cmd_compile.format(filename_ext)
            logger.debug("cmd {}".format(cmd))
            logger.detail(cmd)
            rs, rs_err = CM.vcmd(cmd)

            assert not rs_err, rs_err.decode('ascii')
            assert not rs, rs.decode('ascii')
        
        cmd = cls.cmd_run.format(filename)
        logger.debug("cmd: {}".format(cmd))
        rs, rs_err = CM.vcmd(cmd)
        assert not rs_err, rs_err.decode('ascii')
        rs = rs.decode('ascii').strip()
        stats = [cls.get_stat(stat.strip()) for stat in rs.split()]
        return stats