Search in sources :

Example 16 with Event

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

the class WitnessBuilder method reOrderBasedOnAtomicity.

private List<Event> reOrderBasedOnAtomicity(Program program, List<Event> order) {
    List<Event> result = new ArrayList<>();
    // Maintained for constant lookup time
    Set<Event> processedEvents = new HashSet<>();
    // All the atomic blocks in the code that have to stay together in any execution
    List<List<Event>> atomicBlocks = program.getCache().getEvents(FilterBasic.get(Tag.SVCOMP.SVCOMPATOMIC)).stream().map(e -> ((EndAtomic) e).getBlock().stream().filter(order::contains).collect(Collectors.toList())).collect(Collectors.toList());
    for (Event next : order) {
        if (processedEvents.contains(next)) {
            // next was added as part of a previous block
            continue;
        }
        List<Event> block = atomicBlocks.stream().filter(b -> Collections.binarySearch(b, next) >= 0).findFirst().orElseGet(() -> Collections.singletonList(next));
        result.addAll(block);
        processedEvents.addAll(block);
    }
    return result;
}
Also used : Options(org.sosy_lab.common.configuration.Options) java.util(java.util) Utils.intVar(com.dat3m.dartagnan.wmm.utils.Utils.intVar) MessageDigest(java.security.MessageDigest) SimpleDateFormat(java.text.SimpleDateFormat) EdgeAttributes(com.dat3m.dartagnan.witness.EdgeAttributes) Thread(com.dat3m.dartagnan.program.Thread) WITNESS_ORIGINAL_PROGRAM_PATH(com.dat3m.dartagnan.configuration.OptionNames.WITNESS_ORIGINAL_PROGRAM_PATH) EndAtomic(com.dat3m.dartagnan.program.event.lang.svcomp.EndAtomic) Model(org.sosy_lab.java_smt.api.Model) ProverEnvironment(org.sosy_lab.java_smt.api.ProverEnvironment) Option(org.sosy_lab.common.configuration.Option) Result(com.dat3m.dartagnan.utils.Result) SolverContext(org.sosy_lab.java_smt.api.SolverContext) Program(com.dat3m.dartagnan.program.Program) BigInteger(java.math.BigInteger) FilterBasic(com.dat3m.dartagnan.program.filter.FilterBasic) DateFormat(java.text.DateFormat) PTHREAD(com.dat3m.dartagnan.program.event.Tag.C11.PTHREAD) InvalidConfigurationException(org.sosy_lab.common.configuration.InvalidConfigurationException) VerificationTask(com.dat3m.dartagnan.verification.VerificationTask) Store(com.dat3m.dartagnan.program.event.core.Store) GraphAttributes(com.dat3m.dartagnan.witness.GraphAttributes) Predicate(java.util.function.Predicate) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) MemEvent(com.dat3m.dartagnan.program.event.core.MemEvent) File(java.io.File) Tag(com.dat3m.dartagnan.program.event.Tag) WRITE(com.dat3m.dartagnan.program.event.Tag.WRITE) String.valueOf(java.lang.String.valueOf) SolverException(org.sosy_lab.java_smt.api.SolverException) Event(com.dat3m.dartagnan.program.event.core.Event) BConst(com.dat3m.dartagnan.expression.BConst) Load(com.dat3m.dartagnan.program.event.core.Load) RegWriter(com.dat3m.dartagnan.program.event.core.utils.RegWriter) FAIL(com.dat3m.dartagnan.utils.Result.FAIL) MemEvent(com.dat3m.dartagnan.program.event.core.MemEvent) Event(com.dat3m.dartagnan.program.event.core.Event)

Example 17 with Event

use of com.dat3m.dartagnan.program.event.core.Event 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 18 with Event

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

the class WitnessBuilder method getSCExecutionOrder.

private List<Event> getSCExecutionOrder(Model model) {
    List<Event> execEvents = new ArrayList<>();
    // TODO: we recently added many cline to many events and this might affect the witness generation.
    Predicate<Event> executedCEvents = e -> e.wasExecuted(model) && e.getCLine() > -1;
    execEvents.addAll(task.getProgram().getCache().getEvents(FilterBasic.get(Tag.INIT)).stream().filter(executedCEvents).collect(Collectors.toList()));
    execEvents.addAll(task.getProgram().getEvents().stream().filter(executedCEvents).collect(Collectors.toList()));
    Map<Integer, List<Event>> map = new HashMap<>();
    for (Event e : execEvents) {
        // TODO improve this: these events correspond to return statements
        if (e instanceof MemEvent && ((MemEvent) e).getMemValue() instanceof BConst && !((BConst) ((MemEvent) e).getMemValue()).getValue()) {
            continue;
        }
        BigInteger var = model.evaluate(intVar("hb", e, ctx));
        if (var != null) {
            map.computeIfAbsent(var.intValue(), x -> new ArrayList<>()).add(e);
        }
    }
    List<Event> exec = map.keySet().stream().sorted().flatMap(key -> map.get(key).stream()).collect(Collectors.toList());
    return exec.isEmpty() ? execEvents : exec;
}
Also used : Options(org.sosy_lab.common.configuration.Options) java.util(java.util) Utils.intVar(com.dat3m.dartagnan.wmm.utils.Utils.intVar) MessageDigest(java.security.MessageDigest) SimpleDateFormat(java.text.SimpleDateFormat) EdgeAttributes(com.dat3m.dartagnan.witness.EdgeAttributes) Thread(com.dat3m.dartagnan.program.Thread) WITNESS_ORIGINAL_PROGRAM_PATH(com.dat3m.dartagnan.configuration.OptionNames.WITNESS_ORIGINAL_PROGRAM_PATH) EndAtomic(com.dat3m.dartagnan.program.event.lang.svcomp.EndAtomic) Model(org.sosy_lab.java_smt.api.Model) ProverEnvironment(org.sosy_lab.java_smt.api.ProverEnvironment) Option(org.sosy_lab.common.configuration.Option) Result(com.dat3m.dartagnan.utils.Result) SolverContext(org.sosy_lab.java_smt.api.SolverContext) Program(com.dat3m.dartagnan.program.Program) BigInteger(java.math.BigInteger) FilterBasic(com.dat3m.dartagnan.program.filter.FilterBasic) DateFormat(java.text.DateFormat) PTHREAD(com.dat3m.dartagnan.program.event.Tag.C11.PTHREAD) InvalidConfigurationException(org.sosy_lab.common.configuration.InvalidConfigurationException) VerificationTask(com.dat3m.dartagnan.verification.VerificationTask) Store(com.dat3m.dartagnan.program.event.core.Store) GraphAttributes(com.dat3m.dartagnan.witness.GraphAttributes) Predicate(java.util.function.Predicate) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) MemEvent(com.dat3m.dartagnan.program.event.core.MemEvent) File(java.io.File) Tag(com.dat3m.dartagnan.program.event.Tag) WRITE(com.dat3m.dartagnan.program.event.Tag.WRITE) String.valueOf(java.lang.String.valueOf) SolverException(org.sosy_lab.java_smt.api.SolverException) Event(com.dat3m.dartagnan.program.event.core.Event) BConst(com.dat3m.dartagnan.expression.BConst) Load(com.dat3m.dartagnan.program.event.core.Load) RegWriter(com.dat3m.dartagnan.program.event.core.utils.RegWriter) FAIL(com.dat3m.dartagnan.utils.Result.FAIL) BigInteger(java.math.BigInteger) BConst(com.dat3m.dartagnan.expression.BConst) MemEvent(com.dat3m.dartagnan.program.event.core.MemEvent) MemEvent(com.dat3m.dartagnan.program.event.core.MemEvent) Event(com.dat3m.dartagnan.program.event.core.Event) BigInteger(java.math.BigInteger)

Example 19 with Event

use of com.dat3m.dartagnan.program.event.core.Event 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 20 with Event

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

the class RelCo method encodeApprox.

@Override
protected BooleanFormula encodeApprox(SolverContext ctx) {
    AliasAnalysis alias = analysisContext.get(AliasAnalysis.class);
    WmmAnalysis wmmAnalysis = analysisContext.get(WmmAnalysis.class);
    FormulaManager fmgr = ctx.getFormulaManager();
    BooleanFormulaManager bmgr = fmgr.getBooleanFormulaManager();
    IntegerFormulaManager imgr = fmgr.getIntegerFormulaManager();
    BooleanFormula enc = bmgr.makeTrue();
    List<Event> eventsInit = task.getProgram().getCache().getEvents(FilterBasic.get(INIT));
    List<Event> eventsStore = task.getProgram().getCache().getEvents(FilterMinus.get(FilterBasic.get(WRITE), FilterBasic.get(INIT)));
    for (Event e : eventsInit) {
        enc = bmgr.and(enc, imgr.equal(getIntVar(e, ctx), imgr.makeNumber(BigInteger.ZERO)));
    }
    List<IntegerFormula> intVars = new ArrayList<>();
    for (Event w : eventsStore) {
        IntegerFormula coVar = getIntVar(w, ctx);
        enc = bmgr.and(enc, imgr.greaterThan(coVar, imgr.makeNumber(BigInteger.ZERO)));
        intVars.add(coVar);
    }
    BooleanFormula distinct = intVars.size() > 1 ? imgr.distinct(intVars) : bmgr.makeTrue();
    enc = bmgr.and(enc, distinct);
    for (Event w : task.getProgram().getCache().getEvents(FilterBasic.get(WRITE))) {
        MemEvent w1 = (MemEvent) w;
        BooleanFormula lastCo = w1.exec();
        for (Tuple t : maxTupleSet.getByFirst(w1)) {
            MemEvent w2 = (MemEvent) t.getSecond();
            BooleanFormula relation = getSMTVar(t, ctx);
            BooleanFormula execPair = getExecPair(t, ctx);
            lastCo = bmgr.and(lastCo, bmgr.not(relation));
            Formula a1 = w1.getMemAddressExpr();
            Formula a2 = w2.getMemAddressExpr();
            BooleanFormula sameAddress = generalEqual(a1, a2, ctx);
            enc = bmgr.and(enc, bmgr.equivalence(relation, bmgr.and(execPair, sameAddress, imgr.lessThan(getIntVar(w1, ctx), getIntVar(w2, ctx)))));
            // ============ Local consistency optimizations ============
            if (getMinTupleSet().contains(t)) {
                enc = bmgr.and(enc, bmgr.equivalence(relation, execPair));
            } else if (wmmAnalysis.isLocallyConsistent()) {
                if (w2.is(INIT) || t.isBackward()) {
                    enc = bmgr.and(enc, bmgr.equivalence(relation, bmgr.makeFalse()));
                }
                if (w1.is(INIT) || t.isForward()) {
                    enc = bmgr.and(enc, bmgr.implication(bmgr.and(execPair, sameAddress), relation));
                }
            }
        }
        if (task.getProgram().getFormat().equals(LITMUS) || task.getProperty().contains(LIVENESS)) {
            BooleanFormula lastCoExpr = getLastCoVar(w1, ctx);
            enc = bmgr.and(enc, bmgr.equivalence(lastCoExpr, lastCo));
            for (Event i : eventsInit) {
                Init init = (Init) i;
                if (!alias.mayAlias(w1, init)) {
                    continue;
                }
                IExpr address = init.getAddress();
                Formula a1 = w1.getMemAddressExpr();
                Formula a2 = address.toIntFormula(init, ctx);
                BooleanFormula sameAddress = generalEqual(a1, a2, ctx);
                Formula v1 = w1.getMemValueExpr();
                Formula v2 = init.getBase().getLastMemValueExpr(ctx, init.getOffset());
                BooleanFormula sameValue = generalEqual(v1, v2, ctx);
                enc = bmgr.and(enc, bmgr.implication(bmgr.and(lastCoExpr, sameAddress), sameValue));
            }
        }
    }
    return enc;
}
Also used : IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) ArrayList(java.util.ArrayList) AliasAnalysis(com.dat3m.dartagnan.program.analysis.AliasAnalysis) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) Init(com.dat3m.dartagnan.program.event.core.Init) MemEvent(com.dat3m.dartagnan.program.event.core.MemEvent) WmmAnalysis(com.dat3m.dartagnan.wmm.analysis.WmmAnalysis) MemEvent(com.dat3m.dartagnan.program.event.core.MemEvent) Event(com.dat3m.dartagnan.program.event.core.Event) IExpr(com.dat3m.dartagnan.expression.IExpr) Tuple(com.dat3m.dartagnan.wmm.utils.Tuple)

Aggregations

Event (com.dat3m.dartagnan.program.event.core.Event)104 Tuple (com.dat3m.dartagnan.wmm.utils.Tuple)34 Thread (com.dat3m.dartagnan.program.Thread)32 Register (com.dat3m.dartagnan.program.Register)25 TupleSet (com.dat3m.dartagnan.wmm.utils.TupleSet)25 MemEvent (com.dat3m.dartagnan.program.event.core.MemEvent)20 Tag (com.dat3m.dartagnan.program.event.Tag)16 Label (com.dat3m.dartagnan.program.event.core.Label)16 Collectors (java.util.stream.Collectors)16 BooleanFormula (org.sosy_lab.java_smt.api.BooleanFormula)16 BooleanFormulaManager (org.sosy_lab.java_smt.api.BooleanFormulaManager)16 LogManager (org.apache.logging.log4j.LogManager)15 Logger (org.apache.logging.log4j.Logger)15 Program (com.dat3m.dartagnan.program.Program)12 ExecutionAnalysis (com.dat3m.dartagnan.program.analysis.ExecutionAnalysis)12 RegWriter (com.dat3m.dartagnan.program.event.core.utils.RegWriter)12 java.util (java.util)12 SolverContext (org.sosy_lab.java_smt.api.SolverContext)12 FilterBasic (com.dat3m.dartagnan.program.filter.FilterBasic)11 CondJump (com.dat3m.dartagnan.program.event.core.CondJump)10