Search in sources :

Example 36 with BoolExpr

use of com.microsoft.z3.BoolExpr in project Dat3M by hernanponcedeleon.

the class Power method satPowerPPO.

private static BoolExpr satPowerPPO(Set<Event> events, Context ctx) throws Z3Exception {
    BoolExpr enc = ctx.mkTrue();
    for (Event e1 : events) {
        for (Event e2 : events) {
            BoolExpr orClause1 = ctx.mkFalse();
            BoolExpr orClause2 = ctx.mkFalse();
            BoolExpr orClause3 = ctx.mkFalse();
            BoolExpr orClause4 = ctx.mkFalse();
            BoolExpr orClause5 = ctx.mkFalse();
            BoolExpr orClause6 = ctx.mkFalse();
            BoolExpr orClause7 = ctx.mkFalse();
            BoolExpr orClause8 = ctx.mkFalse();
            for (Event e3 : events) {
                orClause1 = ctx.mkOr(orClause1, ctx.mkAnd(edge("ic", e1, e3, ctx), edge("ci", e3, e2, ctx)));
                orClause2 = ctx.mkOr(orClause2, ctx.mkAnd(edge("ii", e1, e3, ctx), edge("ii", e3, e2, ctx)));
                orClause3 = ctx.mkOr(orClause3, ctx.mkAnd(edge("ic", e1, e3, ctx), edge("cc", e3, e2, ctx)));
                orClause4 = ctx.mkOr(orClause4, ctx.mkAnd(edge("ii", e1, e3, ctx), edge("ic", e3, e2, ctx)));
                orClause5 = ctx.mkOr(orClause5, ctx.mkAnd(edge("ci", e1, e3, ctx), edge("ii", e3, e2, ctx)));
                orClause6 = ctx.mkOr(orClause6, ctx.mkAnd(edge("cc", e1, e3, ctx), edge("ci", e3, e2, ctx)));
                orClause7 = ctx.mkOr(orClause7, ctx.mkAnd(edge("ci", e1, e3, ctx), edge("ic", e3, e2, ctx)));
                orClause8 = ctx.mkOr(orClause8, ctx.mkAnd(edge("cc", e1, e3, ctx), edge("cc", e3, e2, ctx)));
            }
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("(ic;ci)", e1, e2, ctx), orClause1));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("(ii;ii)", e1, e2, ctx), orClause2));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("(ic;cc)", e1, e2, ctx), orClause3));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("(ii;ic)", e1, e2, ctx), orClause4));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("(ci;ii)", e1, e2, ctx), orClause5));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("(cc;ci)", e1, e2, ctx), orClause6));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("(ci;ic)", e1, e2, ctx), orClause7));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("(cc;cc)", e1, e2, ctx), orClause8));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("ii", e1, e2, ctx), ctx.mkOr(edge("ii0", e1, e2, ctx), edge("ci", e1, e2, ctx), edge("(ic;ci)", e1, e2, ctx), edge("(ii;ii)", e1, e2, ctx))));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("ic", e1, e2, ctx), ctx.mkOr(edge("ic0", e1, e2, ctx), edge("ii", e1, e2, ctx), edge("cc", e1, e2, ctx), edge("(ic;cc)", e1, e2, ctx), edge("(ii;ic)", e1, e2, ctx))));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("ci", e1, e2, ctx), ctx.mkOr(edge("ci0", e1, e2, ctx), edge("(ci;ii)", e1, e2, ctx), edge("(cc;ci)", e1, e2, ctx))));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("cc", e1, e2, ctx), ctx.mkOr(edge("cc0", e1, e2, ctx), edge("ci", e1, e2, ctx), edge("(ci;ic)", e1, e2, ctx), edge("(cc;cc)", e1, e2, ctx))));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("ii", e1, e2, ctx), ctx.mkOr(ctx.mkAnd(edge("ii0", e1, e2, ctx), ctx.mkGt(intCount("ii", e1, e2, ctx), intCount("ii0", e1, e2, ctx))), ctx.mkAnd(edge("ci", e1, e2, ctx), ctx.mkGt(intCount("ii", e1, e2, ctx), intCount("ci", e1, e2, ctx))), ctx.mkAnd(edge("(ic;ci)", e1, e2, ctx), ctx.mkGt(intCount("ii", e1, e2, ctx), intCount("(ic;ci)", e1, e2, ctx))), ctx.mkAnd(edge("(ii;ii)", e1, e2, ctx), ctx.mkGt(intCount("ii", e1, e2, ctx), intCount("(ii;ii)", e1, e2, ctx))))));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("ic", e1, e2, ctx), ctx.mkOr(ctx.mkAnd(edge("ic0", e1, e2, ctx), ctx.mkGt(intCount("ic", e1, e2, ctx), intCount("ic0", e1, e2, ctx))), ctx.mkAnd(edge("ii", e1, e2, ctx), ctx.mkGt(intCount("ic", e1, e2, ctx), intCount("ii", e1, e2, ctx))), ctx.mkAnd(edge("cc", e1, e2, ctx), ctx.mkGt(intCount("ic", e1, e2, ctx), intCount("cc", e1, e2, ctx))), ctx.mkAnd(edge("(ic;cc)", e1, e2, ctx), ctx.mkGt(intCount("ic", e1, e2, ctx), intCount("(ic;cc)", e1, e2, ctx))), ctx.mkAnd(edge("(ii;ic)", e1, e2, ctx), ctx.mkGt(intCount("ic", e1, e2, ctx), intCount("(ii;ic)", e1, e2, ctx))))));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("ci", e1, e2, ctx), ctx.mkOr(ctx.mkAnd(edge("ci0", e1, e2, ctx), ctx.mkGt(intCount("ci", e1, e2, ctx), intCount("ci0", e1, e2, ctx))), ctx.mkAnd(edge("(ci;ii)", e1, e2, ctx), ctx.mkGt(intCount("ci", e1, e2, ctx), intCount("(ci;ii)", e1, e2, ctx))), ctx.mkAnd(edge("(cc;ci)", e1, e2, ctx), ctx.mkGt(intCount("ci", e1, e2, ctx), intCount("(cc;ci)", e1, e2, ctx))))));
            enc = ctx.mkAnd(enc, ctx.mkEq(edge("cc", e1, e2, ctx), ctx.mkOr(ctx.mkAnd(edge("cc0", e1, e2, ctx), ctx.mkGt(intCount("cc", e1, e2, ctx), intCount("cc0", e1, e2, ctx))), ctx.mkAnd(edge("ci", e1, e2, ctx), ctx.mkGt(intCount("cc", e1, e2, ctx), intCount("ci", e1, e2, ctx))), ctx.mkAnd(edge("(ci;ic)", e1, e2, ctx), ctx.mkGt(intCount("cc", e1, e2, ctx), intCount("(ci;ic)", e1, e2, ctx))), ctx.mkAnd(edge("(cc;cc)", e1, e2, ctx), ctx.mkGt(intCount("cc", e1, e2, ctx), intCount("(cc;cc)", e1, e2, ctx))))));
        }
    }
    return enc;
}
Also used : BoolExpr(com.microsoft.z3.BoolExpr) Event(dartagnan.program.Event) MemEvent(dartagnan.program.MemEvent)

Example 37 with BoolExpr

use of com.microsoft.z3.BoolExpr in project Dat3M by hernanponcedeleon.

the class Encodings method initsUniquePath.

public static BoolExpr initsUniquePath(Program p, Context ctx) {
    BoolExpr prec = ctx.mkTrue();
    BoolExpr post = ctx.mkTrue();
    for (Event e : p.getEvents().stream().filter(e -> e instanceof MemEvent).collect(Collectors.toSet())) {
        prec = ctx.mkAnd(prec, ctx.mkOr(e.getGuard(), ctx.mkNot(e.executes(ctx))));
    }
    for (Event e : p.getEvents().stream().filter(e -> e instanceof Init && e.getLoc() instanceof HighLocation).collect(Collectors.toSet())) {
        BoolExpr guards = ctx.mkAnd(ctx.mkLt(ctx.mkSub(uniqueValue(e, ctx), ctx.mkInt(1)), initValue(e, ctx)), ctx.mkGt(ctx.mkAdd(uniqueValue(e, ctx), ctx.mkInt(1)), initValue(e, ctx)));
        post = ctx.mkAnd(post, guards);
    }
    return ctx.mkImplies(prec, post);
}
Also used : HighLocation(dartagnan.program.HighLocation) Utils.lastValueReg(dartagnan.utils.Utils.lastValueReg) Utils.uniqueValue(dartagnan.utils.Utils.uniqueValue) com.microsoft.z3(com.microsoft.z3) Event(dartagnan.program.Event) Set(java.util.Set) If(dartagnan.program.If) Collectors(java.util.stream.Collectors) Utils.initValue(dartagnan.utils.Utils.initValue) Init(dartagnan.program.Init) Register(dartagnan.program.Register) Utils.initValue2(dartagnan.utils.Utils.initValue2) Local(dartagnan.program.Local) Load(dartagnan.program.Load) MemEvent(dartagnan.program.MemEvent) Utils.ssaReg(dartagnan.utils.Utils.ssaReg) Program(dartagnan.program.Program) Utils.edge(dartagnan.utils.Utils.edge) Utils.lastValueLoc(dartagnan.utils.Utils.lastValueLoc) Store(dartagnan.program.Store) MapSSA(dartagnan.utils.MapSSA) Location(dartagnan.program.Location) HighLocation(dartagnan.program.HighLocation) Init(dartagnan.program.Init) MemEvent(dartagnan.program.MemEvent) Event(dartagnan.program.Event) MemEvent(dartagnan.program.MemEvent)

Example 38 with BoolExpr

use of com.microsoft.z3.BoolExpr in project Dat3M by hernanponcedeleon.

the class Encodings method getReachedStateLow.

public static BoolExpr getReachedStateLow(Program p, Model model, Context ctx) {
    Set<Location> locs = p.getEvents().stream().filter(e -> e instanceof MemEvent).map(e -> e.getLoc()).filter(l -> !(l instanceof HighLocation)).collect(Collectors.toSet());
    BoolExpr reachedState = ctx.mkTrue();
    for (Location loc : locs) {
        reachedState = ctx.mkAnd(reachedState, ctx.mkEq(lastValueLoc(loc, ctx), model.getConstInterp(lastValueLoc(loc, ctx))));
    }
    return reachedState;
}
Also used : HighLocation(dartagnan.program.HighLocation) Utils.lastValueReg(dartagnan.utils.Utils.lastValueReg) Utils.uniqueValue(dartagnan.utils.Utils.uniqueValue) com.microsoft.z3(com.microsoft.z3) Event(dartagnan.program.Event) Set(java.util.Set) If(dartagnan.program.If) Collectors(java.util.stream.Collectors) Utils.initValue(dartagnan.utils.Utils.initValue) Init(dartagnan.program.Init) Register(dartagnan.program.Register) Utils.initValue2(dartagnan.utils.Utils.initValue2) Local(dartagnan.program.Local) Load(dartagnan.program.Load) MemEvent(dartagnan.program.MemEvent) Utils.ssaReg(dartagnan.utils.Utils.ssaReg) Program(dartagnan.program.Program) Utils.edge(dartagnan.utils.Utils.edge) Utils.lastValueLoc(dartagnan.utils.Utils.lastValueLoc) Store(dartagnan.program.Store) MapSSA(dartagnan.utils.MapSSA) Location(dartagnan.program.Location) HighLocation(dartagnan.program.HighLocation) MemEvent(dartagnan.program.MemEvent) HighLocation(dartagnan.program.HighLocation) Location(dartagnan.program.Location)

Example 39 with BoolExpr

use of com.microsoft.z3.BoolExpr in project Dat3M by hernanponcedeleon.

the class Encodings method diffInitialHigh.

public static BoolExpr diffInitialHigh(Program p, Context ctx) {
    Set<Event> highInits = p.getEvents().stream().filter(e -> e instanceof Init).filter(e -> e.getLoc() instanceof HighLocation).collect(Collectors.toSet());
    BoolExpr initState = ctx.mkTrue();
    for (Event e : highInits) {
        if (e.getLoc().getIValue() == null) {
            initState = ctx.mkAnd(initState, ctx.mkNot(ctx.mkEq(initValue(e, ctx), initValue2(e, ctx))));
        }
    }
    return initState;
}
Also used : HighLocation(dartagnan.program.HighLocation) Utils.lastValueReg(dartagnan.utils.Utils.lastValueReg) Utils.uniqueValue(dartagnan.utils.Utils.uniqueValue) com.microsoft.z3(com.microsoft.z3) Event(dartagnan.program.Event) Set(java.util.Set) If(dartagnan.program.If) Collectors(java.util.stream.Collectors) Utils.initValue(dartagnan.utils.Utils.initValue) Init(dartagnan.program.Init) Register(dartagnan.program.Register) Utils.initValue2(dartagnan.utils.Utils.initValue2) Local(dartagnan.program.Local) Load(dartagnan.program.Load) MemEvent(dartagnan.program.MemEvent) Utils.ssaReg(dartagnan.utils.Utils.ssaReg) Program(dartagnan.program.Program) Utils.edge(dartagnan.utils.Utils.edge) Utils.lastValueLoc(dartagnan.utils.Utils.lastValueLoc) Store(dartagnan.program.Store) MapSSA(dartagnan.utils.MapSSA) Location(dartagnan.program.Location) HighLocation(dartagnan.program.HighLocation) Init(dartagnan.program.Init) Event(dartagnan.program.Event) MemEvent(dartagnan.program.MemEvent)

Example 40 with BoolExpr

use of com.microsoft.z3.BoolExpr in project Dat3M by hernanponcedeleon.

the class Porthos method main.

public static void main(String[] args) throws Z3Exception, IOException {
    List<String> MCMs = Arrays.asList("sc", "tso", "pso", "rmo", "alpha", "power", "arm");
    Options options = new Options();
    Option sourceOpt = new Option("s", "source", true, "source MCM");
    sourceOpt.setRequired(true);
    options.addOption(sourceOpt);
    Option targetOpt = new Option("t", "target", true, "target MCM");
    targetOpt.setRequired(true);
    options.addOption(targetOpt);
    Option inputOpt = new Option("i", "input", true, "input file path");
    inputOpt.setRequired(true);
    options.addOption(inputOpt);
    options.addOption("state", false, "PORTHOS performs state portability");
    options.addOption(Option.builder("draw").hasArg().desc("If a buf is found, it outputs a graph \\path_to_file.dot").build());
    options.addOption(Option.builder("rels").hasArgs().desc("Relations to be drawn in the graph").build());
    options.addOption(Option.builder("unroll").hasArg().desc("Unrolling steps").build());
    CommandLineParser parserCmd = new DefaultParser();
    HelpFormatter formatter = new HelpFormatter();
    CommandLine cmd;
    try {
        cmd = parserCmd.parse(options, args);
    } catch (ParseException e) {
        System.out.println(e.getMessage());
        formatter.printHelp("PORTHOS", options);
        System.exit(1);
        return;
    }
    String source = cmd.getOptionValue("source");
    if (!MCMs.stream().anyMatch(mcms -> mcms.trim().equals(source))) {
        System.out.println("Unrecognized source");
        System.exit(0);
        return;
    }
    String target = cmd.getOptionValue("target");
    if (!MCMs.stream().anyMatch(mcms -> mcms.trim().equals(target))) {
        System.out.println("Unrecognized target");
        System.exit(0);
        return;
    }
    String inputFilePath = cmd.getOptionValue("input");
    if (!inputFilePath.endsWith("pts") && !inputFilePath.endsWith("litmus")) {
        System.out.println("Unrecognized program format");
        System.exit(0);
        return;
    }
    File file = new File(inputFilePath);
    boolean statePortability = cmd.hasOption("state");
    String[] rels = new String[100];
    if (cmd.hasOption("rels")) {
        rels = cmd.getOptionValues("rels");
    }
    String program = FileUtils.readFileToString(file, "UTF-8");
    ANTLRInputStream input = new ANTLRInputStream(program);
    Program p = new Program(inputFilePath);
    if (inputFilePath.endsWith("litmus")) {
        LitmusLexer lexer = new LitmusLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        LitmusParser parser = new LitmusParser(tokens);
        p = parser.program(inputFilePath).p;
    }
    if (inputFilePath.endsWith("pts")) {
        PorthosLexer lexer = new PorthosLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PorthosParser parser = new PorthosParser(tokens);
        p = parser.program(inputFilePath).p;
    }
    int steps = 1;
    if (cmd.hasOption("unroll")) {
        steps = Integer.parseInt(cmd.getOptionValue("unroll"));
    }
    p.initialize(steps);
    Program pSource = p.clone();
    Program pTarget = p.clone();
    pSource.compile(source, false, true);
    Integer startEId = Collections.max(pSource.getEvents().stream().filter(e -> e instanceof Init).map(e -> e.getEId()).collect(Collectors.toSet())) + 1;
    pTarget.compile(target, false, true, startEId);
    Context ctx = new Context();
    ctx.setPrintMode(Z3_ast_print_mode.Z3_PRINT_SMTLIB_FULL);
    Solver s = ctx.mkSolver();
    Solver s2 = ctx.mkSolver();
    BoolExpr sourceDF = pSource.encodeDF(ctx);
    BoolExpr sourceCF = pSource.encodeCF(ctx);
    BoolExpr sourceDF_RF = pSource.encodeDF_RF(ctx);
    BoolExpr sourceDomain = Domain.encode(pSource, ctx);
    BoolExpr sourceMM = pSource.encodeMM(ctx, source);
    s.add(pTarget.encodeDF(ctx));
    s.add(pTarget.encodeCF(ctx));
    s.add(pTarget.encodeDF_RF(ctx));
    s.add(Domain.encode(pTarget, ctx));
    s.add(pTarget.encodeMM(ctx, target));
    s.add(pTarget.encodeConsistent(ctx, target));
    s.add(sourceDF);
    s.add(sourceCF);
    s.add(sourceDF_RF);
    s.add(sourceDomain);
    s.add(sourceMM);
    s.add(pSource.encodeInconsistent(ctx, source));
    s.add(encodeCommonExecutions(pTarget, pSource, ctx));
    s2.add(sourceDF);
    s2.add(sourceCF);
    s2.add(sourceDF_RF);
    s2.add(sourceDomain);
    s2.add(sourceMM);
    s2.add(pSource.encodeConsistent(ctx, source));
    if (!statePortability) {
        if (s.check() == Status.SATISFIABLE) {
            System.out.println("The program is not portable");
            // System.out.println("       0");
            if (cmd.hasOption("draw")) {
                String outputPath = cmd.getOptionValue("draw");
                Utils.drawGraph(p, pSource, pTarget, ctx, s.getModel(), outputPath, rels);
            }
            return;
        } else {
            System.out.println("The program is portable");
            // System.out.println("       1");
            return;
        }
    }
    int iterations = 0;
    Status lastCheck = Status.SATISFIABLE;
    Set<Expr> visited = new HashSet<Expr>();
    while (lastCheck == Status.SATISFIABLE) {
        lastCheck = s.check();
        if (lastCheck == Status.SATISFIABLE) {
            iterations = iterations + 1;
            Model model = s.getModel();
            s2.push();
            BoolExpr reachedState = encodeReachedState(pTarget, model, ctx);
            visited.add(reachedState);
            assert (iterations == visited.size());
            s2.add(reachedState);
            if (s2.check() == Status.UNSATISFIABLE) {
                System.out.println("The program is not state-portable");
                System.out.println("Iterations: " + iterations);
                // System.out.println("       0");
                return;
            } else {
                s2.pop();
                s.add(ctx.mkNot(reachedState));
            }
        } else {
            System.out.println("The program is state-portable");
            System.out.println("Iterations: " + iterations);
            // System.out.println("       1");
            return;
        }
    }
}
Also used : Arrays(java.util.Arrays) Solver(com.microsoft.z3.Solver) org.apache.commons.cli(org.apache.commons.cli) Context(com.microsoft.z3.Context) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) HashSet(java.util.HashSet) LitmusParser(dartagnan.LitmusParser) PorthosLexer(dartagnan.PorthosLexer) PorthosParser(dartagnan.PorthosParser) BoolExpr(com.microsoft.z3.BoolExpr) Status(com.microsoft.z3.Status) Program(dartagnan.program.Program) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Utils(dartagnan.utils.Utils) Collectors(java.util.stream.Collectors) File(java.io.File) Z3_ast_print_mode(com.microsoft.z3.enumerations.Z3_ast_print_mode) Init(dartagnan.program.Init) List(java.util.List) Model(com.microsoft.z3.Model) Domain(dartagnan.wmm.Domain) Encodings.encodeReachedState(dartagnan.utils.Encodings.encodeReachedState) Expr(com.microsoft.z3.Expr) Z3Exception(com.microsoft.z3.Z3Exception) Collections(java.util.Collections) LitmusLexer(dartagnan.LitmusLexer) Encodings.encodeCommonExecutions(dartagnan.utils.Encodings.encodeCommonExecutions) BoolExpr(com.microsoft.z3.BoolExpr) Solver(com.microsoft.z3.Solver) LitmusLexer(dartagnan.LitmusLexer) PorthosParser(dartagnan.PorthosParser) PorthosLexer(dartagnan.PorthosLexer) Init(dartagnan.program.Init) HashSet(java.util.HashSet) Context(com.microsoft.z3.Context) Status(com.microsoft.z3.Status) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Program(dartagnan.program.Program) LitmusParser(dartagnan.LitmusParser) BoolExpr(com.microsoft.z3.BoolExpr) Expr(com.microsoft.z3.Expr) Model(com.microsoft.z3.Model) File(java.io.File) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Aggregations

BoolExpr (com.microsoft.z3.BoolExpr)141 Status (com.microsoft.z3.Status)55 Test (org.junit.Test)51 ArithExpr (com.microsoft.z3.ArithExpr)27 GraphEdge (org.batfish.symbolic.GraphEdge)25 Context (com.microsoft.z3.Context)24 HashMap (java.util.HashMap)22 Expr (com.microsoft.z3.Expr)21 Set (java.util.Set)20 Collectors (java.util.stream.Collectors)18 BitVecExpr (com.microsoft.z3.BitVecExpr)17 Event (dartagnan.program.Event)17 MemEvent (dartagnan.program.MemEvent)17 Program (dartagnan.program.Program)16 Map (java.util.Map)16 Local (dartagnan.program.Local)15 ArrayList (java.util.ArrayList)15 BatfishException (org.batfish.common.BatfishException)14 Graph (org.batfish.symbolic.Graph)14 com.microsoft.z3 (com.microsoft.z3)12