Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ private static Predicate defaultValues(List<GhostFunction> sets, Predicate targe
case "char" -> Predicate.createLit("\u0000", Types.CHAR);
case "boolean" -> Predicate.createLit("false", Types.BOOLEAN);
case "double" -> Predicate.createLit("0.0", Types.DOUBLE);
case "float" -> Predicate.createLit("0.0", Types.FLOAT);
default -> throw new RuntimeException("Ghost not implemented for type " + retType);
};
Predicate p = Predicate.createEquals(Predicate.createInvocation(sg.getQualifiedName(), target),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ public static Predicate createLit(String value, String type) {
case Types.BOOLEAN -> new LiteralBoolean(value);
case Types.INT, Types.SHORT -> new LiteralInt(value);
case Types.LONG -> new LiteralLong(value);
case Types.DOUBLE, Types.FLOAT -> new LiteralReal(value);
case Types.DOUBLE -> new LiteralReal(value);
case Types.FLOAT -> new LiteralReal(value, true);
case Types.CHAR -> new LiteralChar(value);
default -> throw new IllegalArgumentException("Unsupported literal type: " + type);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
public class LiteralReal extends Expression {

private final double value;
private final boolean isFloat;

public LiteralReal(double v) {
value = v;
isFloat = false;
}

public LiteralReal(String v) {
this(v, false);
}

public LiteralReal(String v, boolean isFloat) {
value = Double.parseDouble(v);
this.isFloat = isFloat;
}

@Override
Expand All @@ -30,6 +37,10 @@ public double getValue() {
return value;
}

public boolean isFloat() {
return isFloat;
}

@Override
public void getVariableNames(List<String> toAdd) {
// end leaf
Expand All @@ -42,7 +53,7 @@ public void getStateInvocations(List<String> toAdd, List<String> all) {

@Override
public Expression clone() {
return new LiteralReal(value);
return new LiteralReal(Double.toString(value), isFloat);
}

@Override
Expand All @@ -55,18 +66,18 @@ public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Double.hashCode(value);
result = prime * result + Boolean.hashCode(isFloat);
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
if (obj == null || getClass() != obj.getClass())
return false;

LiteralReal other = (LiteralReal) obj;
return Double.doubleToLongBits(value) == Double.doubleToLongBits(other.value);
return Double.doubleToLongBits(value) == Double.doubleToLongBits(other.value) && isFloat == other.isFloat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public Expr<?> visitLiteralChar(LiteralChar lit) {

@Override
public Expr<?> visitLiteralReal(LiteralReal lit) {
if (lit.isFloat()) {
return ctx.makeFloatLiteral(lit.getValue());
}
return ctx.makeDoubleLiteral(lit.getValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ private static Expr<?> getExpr(Context z3, String name, CtTypeReference<?> type)
.mkIntConst(name);
case "boolean", "java.lang.Boolean" -> z3.mkBoolConst(name);
case "long", "java.lang.Long" -> z3.mkRealConst(name);
case "float", "double", "java.lang.Float", "java.lang.Double" -> (FPExpr) z3.mkConst(name, z3.mkFPSort64());
case "float", "java.lang.Float" -> (FPExpr) z3.mkConst(name, z3.mkFPSort32());
case "double", "java.lang.Double" -> (FPExpr) z3.mkConst(name, z3.mkFPSort64());
case "int[]" -> z3.mkArrayConst(name, z3.mkIntSort(), z3.mkIntSort());
default -> z3.mkConst(name, z3.mkUninterpretedSort(typeName));
};
Expand Down
101 changes: 67 additions & 34 deletions liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.microsoft.z3.Sort;
import com.microsoft.z3.Symbol;
import com.microsoft.z3.Model;
import com.microsoft.z3.FPSort;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -142,6 +143,10 @@ public Expr<?> makeDoubleLiteral(double value) {
return z3.mkFP(value, z3.mkFPSort64());
}

public Expr<?> makeFloatLiteral(double value) {
return z3.mkFP(value, z3.mkFPSort32());
}

public Expr<?> makeString(String s) {
return z3.mkString(s);
}
Expand Down Expand Up @@ -280,8 +285,10 @@ private Expr<?> makeStore(Expr<?>[] params) {

// #####################Boolean Operations#####################
public Expr<?> makeEquals(Expr<?> e1, Expr<?> e2) {
if (e1 instanceof FPExpr || e2 instanceof FPExpr)
return z3.mkFPEq(toFP(e1), toFP(e2));
if (e1 instanceof FPExpr || e2 instanceof FPExpr) {
FPSort targetSort = commonFPSort(e1, e2);
return z3.mkFPEq(toFP(e1, targetSort), toFP(e2, targetSort));
}
if (e1 instanceof RealExpr || e2 instanceof RealExpr)
return z3.mkEq(toReal(e1), toReal(e2));
if (isReference(e1) && isReference(e2))
Expand Down Expand Up @@ -348,35 +355,43 @@ private Map<Sort, Expr<?>> referenceViews(String name, Expr<?> nativeView) {

@SuppressWarnings({ "unchecked", "rawtypes" })
public Expr<?> makeLt(Expr<?> e1, Expr<?> e2) {
if (e1 instanceof FPExpr || e2 instanceof FPExpr)
return z3.mkFPLt(toFP(e1), toFP(e2));
if (e1 instanceof FPExpr || e2 instanceof FPExpr) {
FPSort targetSort = commonFPSort(e1, e2);
return z3.mkFPLt(toFP(e1, targetSort), toFP(e2, targetSort));
}
if (e1 instanceof RealExpr || e2 instanceof RealExpr)
return z3.mkLt(toReal(e1), toReal(e2));
return z3.mkLt((ArithExpr) e1, (ArithExpr) e2);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public Expr<?> makeLtEq(Expr<?> e1, Expr<?> e2) {
if (e1 instanceof FPExpr || e2 instanceof FPExpr)
return z3.mkFPLEq(toFP(e1), toFP(e2));
if (e1 instanceof FPExpr || e2 instanceof FPExpr) {
FPSort targetSort = commonFPSort(e1, e2);
return z3.mkFPLEq(toFP(e1, targetSort), toFP(e2, targetSort));
}
if (e1 instanceof RealExpr || e2 instanceof RealExpr)
return z3.mkLe(toReal(e1), toReal(e2));
return z3.mkLe((ArithExpr) e1, (ArithExpr) e2);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public Expr<?> makeGt(Expr<?> e1, Expr<?> e2) {
if (e1 instanceof FPExpr || e2 instanceof FPExpr)
return z3.mkFPGt(toFP(e1), toFP(e2));
if (e1 instanceof FPExpr || e2 instanceof FPExpr) {
FPSort targetSort = commonFPSort(e1, e2);
return z3.mkFPGt(toFP(e1, targetSort), toFP(e2, targetSort));
}
if (e1 instanceof RealExpr || e2 instanceof RealExpr)
return z3.mkGt(toReal(e1), toReal(e2));
return z3.mkGt((ArithExpr) e1, (ArithExpr) e2);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public Expr<?> makeGtEq(Expr<?> e1, Expr<?> e2) {
if (e1 instanceof FPExpr || e2 instanceof FPExpr)
return z3.mkFPGEq(toFP(e1), toFP(e2));
if (e1 instanceof FPExpr || e2 instanceof FPExpr) {
FPSort targetSort = commonFPSort(e1, e2);
return z3.mkFPGEq(toFP(e1, targetSort), toFP(e2, targetSort));
}
if (e1 instanceof RealExpr || e2 instanceof RealExpr)
return z3.mkGe(toReal(e1), toReal(e2));
return z3.mkGe((ArithExpr) e1, (ArithExpr) e2);
Expand Down Expand Up @@ -413,44 +428,54 @@ public Expr<?> makeMinus(Expr<?> eval) {
// #####################Arithmetic Operations#####################
@SuppressWarnings({ "unchecked", "rawtypes" })
public Expr<?> makeAdd(Expr<?> eval, Expr<?> eval2) {
if (eval instanceof FPExpr || eval2 instanceof FPExpr)
return z3.mkFPAdd(z3.mkFPRoundNearestTiesToEven(), toFP(eval), toFP(eval2));
if (eval instanceof FPExpr || eval2 instanceof FPExpr) {
FPSort targetSort = commonFPSort(eval, eval2);
return z3.mkFPAdd(z3.mkFPRoundNearestTiesToEven(), toFP(eval, targetSort), toFP(eval2, targetSort));
}
if (eval instanceof RealExpr || eval2 instanceof RealExpr)
return z3.mkAdd(toReal(eval), toReal(eval2));
return z3.mkAdd((ArithExpr) eval, (ArithExpr) eval2);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public Expr<?> makeSub(Expr<?> eval, Expr<?> eval2) {
if (eval instanceof FPExpr || eval2 instanceof FPExpr)
return z3.mkFPSub(z3.mkFPRoundNearestTiesToEven(), toFP(eval), toFP(eval2));
if (eval instanceof FPExpr || eval2 instanceof FPExpr) {
FPSort targetSort = commonFPSort(eval, eval2);
return z3.mkFPSub(z3.mkFPRoundNearestTiesToEven(), toFP(eval, targetSort), toFP(eval2, targetSort));
}
if (eval instanceof RealExpr || eval2 instanceof RealExpr)
return z3.mkSub(toReal(eval), toReal(eval2));
return z3.mkSub((ArithExpr) eval, (ArithExpr) eval2);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public Expr<?> makeMul(Expr<?> eval, Expr<?> eval2) {
if (eval instanceof FPExpr || eval2 instanceof FPExpr)
return z3.mkFPMul(z3.mkFPRoundNearestTiesToEven(), toFP(eval), toFP(eval2));
if (eval instanceof FPExpr || eval2 instanceof FPExpr) {
FPSort targetSort = commonFPSort(eval, eval2);
return z3.mkFPMul(z3.mkFPRoundNearestTiesToEven(), toFP(eval, targetSort), toFP(eval2, targetSort));
}
if (eval instanceof RealExpr || eval2 instanceof RealExpr)
return z3.mkMul(toReal(eval), toReal(eval2));
return z3.mkMul((ArithExpr) eval, (ArithExpr) eval2);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public Expr<?> makeDiv(Expr<?> eval, Expr<?> eval2) {
if (eval instanceof FPExpr || eval2 instanceof FPExpr)
return z3.mkFPDiv(z3.mkFPRoundNearestTiesToEven(), toFP(eval), toFP(eval2));
if (eval instanceof FPExpr || eval2 instanceof FPExpr) {
FPSort targetSort = commonFPSort(eval, eval2);
return z3.mkFPDiv(z3.mkFPRoundNearestTiesToEven(), toFP(eval, targetSort), toFP(eval2, targetSort));
}
if (eval instanceof RealExpr || eval2 instanceof RealExpr)
return z3.mkDiv(toReal(eval), toReal(eval2));
// Z3 integer division does not model Java's truncation toward zero for negative operands
return z3.mkDiv((ArithExpr) eval, (ArithExpr) eval2);
}

public Expr<?> makeMod(Expr<?> eval, Expr<?> eval2) {
if (eval instanceof FPExpr || eval2 instanceof FPExpr)
return z3.mkFPRem(toFP(eval), toFP(eval2));
if (eval instanceof FPExpr || eval2 instanceof FPExpr) {
FPSort targetSort = commonFPSort(eval, eval2);
return z3.mkFPRem(toFP(eval, targetSort), toFP(eval2, targetSort));
}
// Z3 integer modulo does not model Java's dividend-signed remainder for negative operands
if (eval instanceof RealExpr || eval2 instanceof RealExpr)
return z3.mkMod(toInt(eval), toInt(eval2));
Expand All @@ -473,21 +498,29 @@ private IntExpr toInt(Expr<?> e) {
throw new NotImplementedException();
}

private FPExpr toFP(Expr<?> e) {
FPExpr f;
if (e instanceof FPExpr) {
f = (FPExpr) e;
} else if (e instanceof IntNum)
f = z3.mkFP(((IntNum) e).getInt(), z3.mkFPSort64());
else if (e instanceof IntExpr ee) {
RealExpr re = z3.mkInt2Real(ee);
f = z3.mkFPToFP(z3.mkFPRoundNearestTiesToEven(), re, z3.mkFPSort64());
} else if (e instanceof RealExpr) {
f = z3.mkFPToFP(z3.mkFPRoundNearestTiesToEven(), (RealExpr) e, z3.mkFPSort64());
} else {
throw new NotImplementedException();
private FPSort commonFPSort(Expr<?> e1, Expr<?> e2) {
if ((e1 instanceof FPExpr && e1.getSort().equals(z3.mkFPSort64()))
|| (e2 instanceof FPExpr && e2.getSort().equals(z3.mkFPSort64()))) {
return z3.mkFPSort64();
}
return z3.mkFPSort32();
}

private FPExpr toFP(Expr<?> e, FPSort targetSort) {
if (e instanceof FPExpr f) {
if (f.getSort().equals(targetSort)) {
return f;
}
return z3.mkFPToFP(z3.mkFPRoundNearestTiesToEven(), f, targetSort);
}

if (e instanceof IntExpr || e instanceof RealExpr) {
RealExpr real = e instanceof IntExpr ? z3.mkInt2Real((IntExpr) e) : (RealExpr) e;

return z3.mkFPToFP(z3.mkFPRoundNearestTiesToEven(), real, targetSort);
}
return f;

throw new NotImplementedException();
}

public Expr<?> makeIte(Expr<?> c, Expr<?> t, Expr<?> e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package liquidjava.api.tests;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import liquidjava.api.CommandLineLauncher;
import liquidjava.diagnostics.Diagnostics;

class TestFloatGhostDefault {

@TempDir
Path dir;

@Test
void floatGhostUsesZeroAsDefaultValue() throws IOException {
String source = """
import liquidjava.specification.Ghost;
import liquidjava.specification.StateRefinement;

@Ghost("float level")
class Tank {
@StateRefinement(to = "true")
public void fill() {}
}

public class Main {
public static void main(String[] args) {
Tank t = new Tank();
t.fill();
}
}
""";

Files.writeString(dir.resolve("Main.java"), source);

CommandLineLauncher.launch(dir.toString());

assertTrue(Diagnostics.getInstance().getErrors().isEmpty(),
() -> "Float ghost should not produce errors: " + Diagnostics.getInstance().getErrors());
}
}