package org.graalvm.compiler.hotspot.phases;
import java.util.Iterator;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeFlood;
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
import org.graalvm.compiler.hotspot.nodes.ArrayRangeWriteBarrier;
import org.graalvm.compiler.hotspot.nodes.G1PostWriteBarrier;
import org.graalvm.compiler.hotspot.nodes.ObjectWriteBarrier;
import org.graalvm.compiler.hotspot.nodes.SerialWriteBarrier;
import org.graalvm.compiler.nodeinfo.Verbosity;
import org.graalvm.compiler.nodes.DeoptimizingNode;
import org.graalvm.compiler.nodes.FixedWithNextNode;
import org.graalvm.compiler.nodes.LoopBeginNode;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.extended.ArrayRangeWrite;
import org.graalvm.compiler.nodes.java.LoweredAtomicReadAndWriteNode;
import org.graalvm.compiler.nodes.java.LogicCompareAndSwapNode;
import org.graalvm.compiler.nodes.memory.FixedAccessNode;
import org.graalvm.compiler.nodes.memory.HeapAccess;
import org.graalvm.compiler.nodes.memory.HeapAccess.BarrierType;
import org.graalvm.compiler.nodes.memory.ReadNode;
import org.graalvm.compiler.nodes.memory.WriteNode;
import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
import org.graalvm.compiler.nodes.type.StampTool;
import org.graalvm.compiler.nodes.util.GraphUtil;
import org.graalvm.compiler.phases.Phase;
public class WriteBarrierVerificationPhase extends Phase {
private final GraalHotSpotVMConfig config;
public WriteBarrierVerificationPhase(GraalHotSpotVMConfig config) {
this.config = config;
}
@Override
protected void run(StructuredGraph graph) {
processWrites(graph);
}
private void processWrites(StructuredGraph graph) {
for (Node node : graph.getNodes()) {
if (isObjectWrite(node) || isObjectArrayRangeWrite(node)) {
if (node instanceof WriteNode) {
WriteNode writeNode = (WriteNode) node;
if (StampTool.isPointerAlwaysNull(writeNode.value())) {
continue;
}
}
validateWrite(node);
}
}
}
private void validateWrite(Node write) {
if (hasAttachedBarrier((FixedWithNextNode) write)) {
return;
}
NodeFlood frontier = write.graph().createNodeFlood();
expandFrontier(frontier, write);
Iterator<Node> iterator = frontier.iterator();
while (iterator.hasNext()) {
Node currentNode = iterator.next();
if (isSafepoint(currentNode)) {
throw new AssertionError("Write barrier must be present " + write.toString(Verbosity.All) + " / " + write.inputs());
}
if (useG1GC()) {
if (!(currentNode instanceof G1PostWriteBarrier) || (!validateBarrier((FixedAccessNode) write, (ObjectWriteBarrier) currentNode))) {
expandFrontier(frontier, currentNode);
}
} else {
if (!(currentNode instanceof SerialWriteBarrier) || (!validateBarrier((FixedAccessNode) write, (ObjectWriteBarrier) currentNode)) ||
((currentNode instanceof SerialWriteBarrier) && !validateBarrier((FixedAccessNode) write, (ObjectWriteBarrier) currentNode))) {
expandFrontier(frontier, currentNode);
}
}
}
}
private boolean useG1GC() {
return config.useG1GC;
}
private boolean hasAttachedBarrier(FixedWithNextNode node) {
final Node next = node.next();
final Node previous = node.predecessor();
boolean validatePreBarrier = useG1GC() && (isObjectWrite(node) || !((ArrayRangeWrite) node).isInitialization());
if (node instanceof WriteNode) {
WriteNode writeNode = (WriteNode) node;
if (writeNode.getLocationIdentity().isInit()) {
validatePreBarrier = false;
}
}
if (isObjectWrite(node)) {
return (isObjectBarrier(node, next) || StampTool.isPointerAlwaysNull(getValueWritten(node))) && (!validatePreBarrier || isObjectBarrier(node, previous));
} else if (isObjectArrayRangeWrite(node)) {
return (isArrayBarrier(node, next) || StampTool.isPointerAlwaysNull(getValueWritten(node))) && (!validatePreBarrier || isArrayBarrier(node, previous));
} else {
return true;
}
}
private static boolean isObjectBarrier(FixedWithNextNode node, final Node next) {
return next instanceof ObjectWriteBarrier && validateBarrier((FixedAccessNode) node, (ObjectWriteBarrier) next);
}
private static boolean isArrayBarrier(FixedWithNextNode node, final Node next) {
return (next instanceof ArrayRangeWriteBarrier) && ((ArrayRangeWrite) node).getAddress() == ((ArrayRangeWriteBarrier) next).getAddress();
}
private static boolean isObjectWrite(Node node) {
return node instanceof FixedAccessNode && ((HeapAccess) node).getBarrierType() != BarrierType.NONE && !(node instanceof ReadNode);
}
private static boolean isObjectArrayRangeWrite(Node node) {
return node instanceof ArrayRangeWrite && ((ArrayRangeWrite) node).writesObjectArray();
}
private static void expandFrontier(NodeFlood frontier, Node node) {
for (Node previousNode : node.cfgPredecessors()) {
if (previousNode != null) {
frontier.add(previousNode);
}
}
}
private static boolean isSafepoint(Node node) {
if (node instanceof FixedAccessNode) {
return false;
}
return ((node instanceof DeoptimizingNode) && ((DeoptimizingNode) node).canDeoptimize()) || (node instanceof LoopBeginNode);
}
private static ValueNode getValueWritten(FixedWithNextNode write) {
if (write instanceof WriteNode) {
return ((WriteNode) write).value();
} else if (write instanceof LogicCompareAndSwapNode) {
return ((LogicCompareAndSwapNode) write).getNewValue();
} else if (write instanceof LoweredAtomicReadAndWriteNode) {
return ((LoweredAtomicReadAndWriteNode) write).getNewValue();
} else {
throw GraalError.shouldNotReachHere(String.format("unexpected write node %s", write));
}
}
private static boolean validateBarrier(FixedAccessNode write, ObjectWriteBarrier barrier) {
assert write instanceof WriteNode || write instanceof LogicCompareAndSwapNode || write instanceof LoweredAtomicReadAndWriteNode : "Node must be of type requiring a write barrier " + write;
if (!barrier.usePrecise()) {
if (barrier.getAddress() instanceof OffsetAddressNode && write.getAddress() instanceof OffsetAddressNode) {
return GraphUtil.unproxify(((OffsetAddressNode) barrier.getAddress()).getBase()) == GraphUtil.unproxify(((OffsetAddressNode) write.getAddress()).getBase());
}
}
return barrier.getAddress() == write.getAddress();
}
}