Search in sources :

Example 1 with Store

use of com.dat3m.dartagnan.program.event.core.Store in project Dat3M by hernanponcedeleon.

the class CoSymmetryBreaking method computeMustEdges.

/*
        Computes co must-edges that are implied by this symmetry breaking
        May return an empty list.
    */
public List<Tuple> computeMustEdges() {
    List<Tuple> mustEdges = new ArrayList<>();
    for (Info info : infoMap.values()) {
        if (!info.hasMustEdges) {
            continue;
        }
        Store w = info.writes.get(0);
        final int numThreads = info.threads.size();
        for (int i = 0; i < numThreads; i++) {
            for (int j = i + 1; j < numThreads; j++) {
                mustEdges.add(new Tuple(symm.map(w, info.threads.get(i)), symm.map(w, info.threads.get(j))));
            }
        }
    }
    return mustEdges;
}
Also used : Store(com.dat3m.dartagnan.program.event.core.Store) Tuple(com.dat3m.dartagnan.wmm.utils.Tuple)

Example 2 with Store

use of com.dat3m.dartagnan.program.event.core.Store in project Dat3M by hernanponcedeleon.

the class CoSymmetryBreaking method encode.

public BooleanFormula encode(EquivalenceClass<Thread> symmClass, SolverContext ctx) {
    BooleanFormulaManager bmgr = ctx.getFormulaManager().getBooleanFormulaManager();
    BooleanFormula enc = bmgr.makeTrue();
    if (symmClass.getEquivalence() != symm) {
        return enc;
    }
    Info info = infoMap.get(symmClass);
    if (info == null) {
        logger.warn("Cannot encode co-symmetry because no information has been computed. " + "Make sure that <initialize> gets called before encoding.");
        return enc;
    }
    List<Thread> symmThreads = info.threads;
    // ============= Construct rows =============
    Thread t1 = symmThreads.get(0);
    List<Thread> otherThreads = symmThreads.subList(1, symmThreads.size());
    List<Tuple> r1Tuples = new ArrayList<>();
    for (Store w : info.writes) {
        for (Thread t2 : otherThreads) {
            r1Tuples.add(new Tuple(w, symm.map(w, t2)));
        }
    }
    // Starting row
    List<BooleanFormula> r1 = new ArrayList<>(r1Tuples.size() + 1);
    if (info.hasMustEdges) {
        r1.add(info.writes.get(0).exec());
    }
    r1.addAll(Lists.transform(r1Tuples, t -> co.getSMTVar(t, ctx)));
    // Construct symmetric rows
    Thread rep = symmClass.getRepresentative();
    for (int i = 1; i < symmThreads.size(); i++) {
        Thread t2 = symmThreads.get(i);
        Function<Event, Event> p = symm.createTransposition(t1, t2);
        List<Tuple> r2Tuples = r1Tuples.stream().map(t -> t.permute(p)).collect(Collectors.toList());
        List<BooleanFormula> r2 = new ArrayList<>(r2Tuples.size() + 1);
        if (info.hasMustEdges) {
            r2.add(symm.map(info.writes.get(0), t2).exec());
        }
        r2.addAll(Lists.transform(r2Tuples, t -> co.getSMTVar(t, ctx)));
        final String id = "_" + rep.getId() + "_" + i;
        // NOTE: We want to have r1 >= r2 but lexLeader encodes r1 <= r2, so we swap r1 and r2.
        enc = bmgr.and(enc, SymmetryEncoder.encodeLexLeader(id, r2, r1, ctx));
        t1 = t2;
        r1Tuples = r2Tuples;
        r1 = r2;
    }
    return enc;
}
Also used : SymmetryEncoder(com.dat3m.dartagnan.encoding.SymmetryEncoder) java.util(java.util) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Thread(com.dat3m.dartagnan.program.Thread) Function(java.util.function.Function) Lists(com.google.common.collect.Lists) TupleSet(com.dat3m.dartagnan.wmm.utils.TupleSet) SolverContext(org.sosy_lab.java_smt.api.SolverContext) Tuple(com.dat3m.dartagnan.wmm.utils.Tuple) FilterBasic(com.dat3m.dartagnan.program.filter.FilterBasic) EquivalenceClass(com.dat3m.dartagnan.utils.equivalence.EquivalenceClass) AliasAnalysis(com.dat3m.dartagnan.program.analysis.AliasAnalysis) VerificationTask(com.dat3m.dartagnan.verification.VerificationTask) ThreadSymmetry(com.dat3m.dartagnan.program.analysis.ThreadSymmetry) Store(com.dat3m.dartagnan.program.event.core.Store) Relation(com.dat3m.dartagnan.wmm.relation.Relation) Collectors(java.util.stream.Collectors) Tag(com.dat3m.dartagnan.program.event.Tag) BooleanFormulaManager(org.sosy_lab.java_smt.api.BooleanFormulaManager) RelationNameRepository(com.dat3m.dartagnan.wmm.relation.RelationNameRepository) Logger(org.apache.logging.log4j.Logger) Event(com.dat3m.dartagnan.program.event.core.Event) Preconditions(com.google.common.base.Preconditions) LogManager(org.apache.logging.log4j.LogManager) Axiom(com.dat3m.dartagnan.wmm.axiom.Axiom) BooleanFormulaManager(org.sosy_lab.java_smt.api.BooleanFormulaManager) Store(com.dat3m.dartagnan.program.event.core.Store) Thread(com.dat3m.dartagnan.program.Thread) Event(com.dat3m.dartagnan.program.event.core.Event) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Tuple(com.dat3m.dartagnan.wmm.utils.Tuple)

Example 3 with Store

use of com.dat3m.dartagnan.program.event.core.Store in project Dat3M by hernanponcedeleon.

the class WitnessBuilder method build.

public WitnessGraph build() {
    for (Thread t : task.getProgram().getThreads()) {
        for (Event e : t.getEntry().getSuccessors()) {
            eventThreadMap.put(e, t.getId() - 1);
        }
    }
    WitnessGraph graph = new WitnessGraph();
    graph.addAttribute(UNROLLBOUND.toString(), valueOf(task.getProgram().getUnrollingBound()));
    graph.addAttribute(WITNESSTYPE.toString(), type + "_witness");
    graph.addAttribute(SOURCECODELANG.toString(), "C");
    graph.addAttribute(PRODUCER.toString(), "Dartagnan");
    graph.addAttribute(SPECIFICATION.toString(), "CHECK( init(main()), LTL(G ! call(reach_error())))");
    graph.addAttribute(PROGRAMFILE.toString(), originalProgramFilePath);
    graph.addAttribute(PROGRAMHASH.toString(), getFileSHA256(new File(originalProgramFilePath)));
    graph.addAttribute(ARCHITECTURE.toString(), "32bit");
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    // "If the timestamp is in UTC time, it ends with a 'Z'."
    // https://github.com/sosy-lab/sv-witnesses/blob/main/README-GraphML.md
    graph.addAttribute(CREATIONTIME.toString(), df.format(new Date()) + "Z");
    Node v0 = new Node("N0");
    v0.addAttribute("entry", "true");
    Node v1 = new Node("N1");
    Node v2 = new Node("N2");
    Edge edge = new Edge(v0, v1);
    edge.addAttribute(CREATETHREAD.toString(), "0");
    graph.addEdge(edge);
    edge = new Edge(v1, v2);
    edge.addAttribute(THREADID.toString(), "0");
    edge.addAttribute(ENTERFUNCTION.toString(), "main");
    graph.addEdge(edge);
    int nextNode = 2;
    int threads = 1;
    if (type.equals("correctness")) {
        return graph;
    }
    try (Model model = prover.getModel()) {
        List<Event> execution = reOrderBasedOnAtomicity(task.getProgram(), getSCExecutionOrder(model));
        for (int i = 0; i < execution.size(); i++) {
            Event e = execution.get(i);
            if (i + 1 < execution.size()) {
                Event next = execution.get(i + 1);
                if (e.getCLine() == next.getCLine() && e.getThread() == next.getThread()) {
                    continue;
                }
            }
            edge = new Edge(new Node("N" + nextNode), new Node("N" + (nextNode + 1)));
            edge.addAttribute(THREADID.toString(), valueOf(eventThreadMap.get(e)));
            edge.addAttribute(STARTLINE.toString(), valueOf(e.getCLine()));
            // CLines and thus won't create an edge (as expected)
            if (e.hasFilter(WRITE) && e.hasFilter(PTHREAD)) {
                edge.addAttribute(CREATETHREAD.toString(), valueOf(threads));
                threads++;
            }
            if (e instanceof Load) {
                RegWriter l = (RegWriter) e;
                edge.addAttribute(EVENTID.toString(), valueOf(e.getUId()));
                edge.addAttribute(LOADEDVALUE.toString(), l.getWrittenValue(e, model, ctx).toString());
            }
            if (e instanceof Store) {
                Store s = (Store) e;
                edge.addAttribute(EVENTID.toString(), valueOf(e.getUId()));
                edge.addAttribute(STOREDVALUE.toString(), s.getMemValue().getIntValue(s, model, ctx).toString());
            }
            graph.addEdge(edge);
            nextNode++;
            if (e.hasFilter(Tag.ASSERTION)) {
                break;
            }
        }
    } catch (SolverException ignore) {
    // The if above guarantees that if we reach this try, a Model exists
    }
    graph.getNode("N" + nextNode).addAttribute("violation", "true");
    return graph;
}
Also used : Load(com.dat3m.dartagnan.program.event.core.Load) Store(com.dat3m.dartagnan.program.event.core.Store) Thread(com.dat3m.dartagnan.program.Thread) RegWriter(com.dat3m.dartagnan.program.event.core.utils.RegWriter) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Model(org.sosy_lab.java_smt.api.Model) MemEvent(com.dat3m.dartagnan.program.event.core.MemEvent) Event(com.dat3m.dartagnan.program.event.core.Event) SolverException(org.sosy_lab.java_smt.api.SolverException) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with Store

use of com.dat3m.dartagnan.program.event.core.Store in project Dat3M by hernanponcedeleon.

the class WitnessGraph method encode.

public BooleanFormula encode(Program program, SolverContext ctx) {
    BooleanFormulaManager bmgr = ctx.getFormulaManager().getBooleanFormulaManager();
    IntegerFormulaManager imgr = ctx.getFormulaManager().getIntegerFormulaManager();
    BooleanFormula enc = bmgr.makeTrue();
    List<Event> previous = new ArrayList<>();
    for (Edge edge : edges.stream().filter(Edge::hasCline).collect(Collectors.toList())) {
        List<Event> events = program.getCache().getEvents(FilterBasic.get(MEMORY)).stream().filter(e -> e.getCLine() == edge.getCline()).collect(Collectors.toList());
        if (!previous.isEmpty() && !events.isEmpty()) {
            enc = bmgr.and(enc, bmgr.or(Lists.cartesianProduct(previous, events).stream().map(p -> edge("hb", p.get(0), p.get(1), ctx)).toArray(BooleanFormula[]::new)));
        }
        if (!events.isEmpty()) {
            previous = events;
        }
        if (edge.hasAttributed(EVENTID.toString()) && edge.hasAttributed(LOADEDVALUE.toString())) {
            int id = Integer.parseInt(edge.getAttributed(EVENTID.toString()));
            if (program.getCache().getEvents(FilterBasic.get(READ)).stream().anyMatch(e -> e.getUId() == id)) {
                Load load = (Load) program.getCache().getEvents(FilterBasic.get(READ)).stream().filter(e -> e.getUId() == id).findFirst().get();
                BigInteger value = new BigInteger(edge.getAttributed(LOADEDVALUE.toString()));
                enc = bmgr.and(enc, generalEqual(load.getResultRegisterExpr(), imgr.makeNumber(value), ctx));
            }
        }
        if (edge.hasAttributed(EVENTID.toString()) && edge.hasAttributed(STOREDVALUE.toString())) {
            int id = Integer.parseInt(edge.getAttributed(EVENTID.toString()));
            if (program.getCache().getEvents(FilterBasic.get(WRITE)).stream().anyMatch(e -> e.getUId() == id)) {
                Store store = (Store) program.getCache().getEvents(FilterBasic.get(WRITE)).stream().filter(e -> e.getUId() == id).findFirst().get();
                BigInteger value = new BigInteger(edge.getAttributed(STOREDVALUE.toString()));
                enc = bmgr.and(enc, generalEqual(store.getMemValueExpr(), imgr.makeNumber(value), ctx));
            }
        }
    }
    return enc;
}
Also used : java.util(java.util) Store(com.dat3m.dartagnan.program.event.core.Store) PROGRAMFILE(com.dat3m.dartagnan.witness.GraphAttributes.PROGRAMFILE) FileWriter(java.io.FileWriter) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) EdgeAttributes(com.dat3m.dartagnan.witness.EdgeAttributes) Utils.edge(com.dat3m.dartagnan.wmm.utils.Utils.edge) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) IntegerFormulaManager(org.sosy_lab.java_smt.api.IntegerFormulaManager) BooleanFormulaManager(org.sosy_lab.java_smt.api.BooleanFormulaManager) Tag(com.dat3m.dartagnan.program.event.Tag) Lists(com.google.common.collect.Lists) Files(com.google.common.io.Files) SolverContext(org.sosy_lab.java_smt.api.SolverContext) Program(com.dat3m.dartagnan.program.Program) Event(com.dat3m.dartagnan.program.event.core.Event) BigInteger(java.math.BigInteger) Load(com.dat3m.dartagnan.program.event.core.Load) FilterBasic(com.dat3m.dartagnan.program.filter.FilterBasic) Utils.generalEqual(com.dat3m.dartagnan.expression.utils.Utils.generalEqual) Load(com.dat3m.dartagnan.program.event.core.Load) BooleanFormulaManager(org.sosy_lab.java_smt.api.BooleanFormulaManager) Event(com.dat3m.dartagnan.program.event.core.Event) BigInteger(java.math.BigInteger) Store(com.dat3m.dartagnan.program.event.core.Store) IntegerFormulaManager(org.sosy_lab.java_smt.api.IntegerFormulaManager) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula)

Example 5 with Store

use of com.dat3m.dartagnan.program.event.core.Store in project Dat3M by hernanponcedeleon.

the class RelRf method atomicBlockOptimization.

private void atomicBlockOptimization() {
    // TODO: This function can not only reduce rf-edges
    // but we could also figure out implied coherences:
    // Assume w1 and w2 are aliasing in the same block and w1 is before w2,
    // then if w1 is co-before some external w3, then so is w2, i.e.
    // co(w1, w3) => co(w2, w3), but we also have co(w2, w3) => co(w1, w3)
    // so co(w1, w3) <=> co(w2, w3).
    // This information is not expressible in terms of min/must sets, but
    // we could still encode it.
    int sizeBefore = maxTupleSet.size();
    // Atomics blocks: BeginAtomic -> EndAtomic
    ExecutionAnalysis exec = analysisContext.get(ExecutionAnalysis.class);
    AliasAnalysis alias = analysisContext.get(AliasAnalysis.class);
    FilterAbstract filter = FilterIntersection.get(FilterBasic.get(RMW), FilterBasic.get(SVCOMP.SVCOMPATOMIC));
    for (Event end : task.getProgram().getCache().getEvents(filter)) {
        // Collect memEvents of the atomic block
        List<Store> writes = new ArrayList<>();
        List<Load> reads = new ArrayList<>();
        EndAtomic endAtomic = (EndAtomic) end;
        for (Event b : endAtomic.getBlock()) {
            if (b instanceof Load) {
                reads.add((Load) b);
            } else if (b instanceof Store) {
                writes.add((Store) b);
            }
        }
        for (Load r : reads) {
            // If there is any write w inside the atomic block that is guaranteed to
            // execute before the read and that aliases with it,
            // then the read won't be able to read any external writes
            boolean hasImpliedWrites = writes.stream().anyMatch(w -> w.getCId() < r.getCId() && exec.isImplied(r, w) && alias.mustAlias(r, w));
            if (hasImpliedWrites) {
                maxTupleSet.removeIf(t -> t.getSecond() == r && t.isCrossThread());
            }
        }
    }
    logger.info("Atomic block optimization eliminated " + (sizeBefore - maxTupleSet.size()) + " reads");
}
Also used : Load(com.dat3m.dartagnan.program.event.core.Load) EndAtomic(com.dat3m.dartagnan.program.event.lang.svcomp.EndAtomic) ExecutionAnalysis(com.dat3m.dartagnan.program.analysis.ExecutionAnalysis) FilterAbstract(com.dat3m.dartagnan.program.filter.FilterAbstract) MemEvent(com.dat3m.dartagnan.program.event.core.MemEvent) Event(com.dat3m.dartagnan.program.event.core.Event) Store(com.dat3m.dartagnan.program.event.core.Store) AliasAnalysis(com.dat3m.dartagnan.program.analysis.AliasAnalysis)

Aggregations

Store (com.dat3m.dartagnan.program.event.core.Store)6 Event (com.dat3m.dartagnan.program.event.core.Event)4 Thread (com.dat3m.dartagnan.program.Thread)3 Load (com.dat3m.dartagnan.program.event.core.Load)3 AliasAnalysis (com.dat3m.dartagnan.program.analysis.AliasAnalysis)2 Tag (com.dat3m.dartagnan.program.event.Tag)2 MemEvent (com.dat3m.dartagnan.program.event.core.MemEvent)2 FilterBasic (com.dat3m.dartagnan.program.filter.FilterBasic)2 Axiom (com.dat3m.dartagnan.wmm.axiom.Axiom)2 Tuple (com.dat3m.dartagnan.wmm.utils.Tuple)2 TupleSet (com.dat3m.dartagnan.wmm.utils.TupleSet)2 Lists (com.google.common.collect.Lists)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 BooleanFormula (org.sosy_lab.java_smt.api.BooleanFormula)2 BooleanFormulaManager (org.sosy_lab.java_smt.api.BooleanFormulaManager)2 SolverContext (org.sosy_lab.java_smt.api.SolverContext)2 SymmetryEncoder (com.dat3m.dartagnan.encoding.SymmetryEncoder)1 Utils.generalEqual (com.dat3m.dartagnan.expression.utils.Utils.generalEqual)1 Program (com.dat3m.dartagnan.program.Program)1