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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations