diff --git a/CLAUDE.md b/CLAUDE.md index 270feaa7..26b74d01 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -288,6 +288,59 @@ cfg-extraction (see loop facts above). in the entry method hashes as before. Lambdas still stop all states (`invokedynamic`), threads are still excluded. swat-benchmarks `loop-frames` (RESULTS.md, "Loop-state matching across frames"). +- **Hash containers with symbolic keys** (since 2026-11-17, executor `CollectionInvocation.hashCall`, + `HashEntries`): a HashMap, LinkedHashMap, ConcurrentHashMap (modelled like a HashMap since then, + also with concrete keys), HashSet or LinkedHashSet that SWAT saw created keeps + its exact entries (key shadows in insertion order, value shadows). A call by key (get, + getOrDefault, containsKey, put, putIfAbsent, remove, the copy constructors, putAll; add, contains, + remove, addAll on sets; size, isEmpty, clear) with String, Integer, Long, Short, Byte, Character, + Boolean or null keys is recorded as the equality chain `key.equals(k_j)` over the stored keys: + library branches like those of the String models (`checkAndSetLibraryBranch`, masked in the SA + walk), so the path fixes which entry matched and the solver reaches the others. Before, any + symbolic key was a context loss. + - **Anything else gives the record up** (`CollectionInvocation.forget`): a call outside the + tables, a key of another class (Double, Float, classes with their own equals), a call that + ran application code or threw, the container or a view of it passed to code that is not + modelled, a change through a view or an iterator, more than 64 entries. With a symbolic key + in the container that is a context loss, as is every later call by key. + - **Iteration order:** a plain hash container iterates by hash code, which is not modelled: with + two or more entries and a symbolic key stored since it was last cleared, `next()` and + `toArray` are a context loss (sizes and `hasNext` are exact). A list copied from one is marked + (`ContainerContents.hashOrdered`): reading an element of it by position is the context loss, + its size is not. Linked containers iterate in the recorded order. + - **Checked against the JVM:** what the record says a call returns is compared with what it + returned (`checkPrimitive`, `resolveExact`); a difference gives the record up, with context + loss if a symbolic comparison was involved. + - **Only keys whose shadow value is known** (`ObjectValue.valueUnknown`): a String or boxed + shadow made by NEW has a default value until a constructor model sets it. Comparing a shadow + left at its default put `c == 0` on the path instead of `c == 'a'` (a false SAFE in + `hash-char-literal_false` before the check). `GETVALUE_Object` also marks a String or boxed + shadow whose value differs from the object it is fetched for. + - **Boxed constants** (fixed with this, 2026-11-17): the models of `Character/Short/Byte/Boolean.` + took only a shadow of their own primitive type, but a constant or an int-typed expression is + passed as an int (BIPUSH, ICONST): the box kept the default 0 or false, whatever the object + held (`Boolean flag = true` read back as false, a context loss at the first unboxing, e.g. + in KeywordSearchJobSettings). They now convert as I2C, I2S and I2B do. + - **Empty immutable containers** (`Collections.emptyMap/emptySet/emptyList`): get, containsKey, + containsValue and contains return null or false whatever the argument is, so they are no + context loss either. + - **Lists with exactly known elements** (built by appending, `ContainerContents.exactOrdered`): + `equals` of two of them (ArrayList, LinkedList, Vector, Stack; also through `Objects.equals`) is + false for different sizes, else the same kind of chain over the element pairs up to the + first that differ (`CollectionInvocation.listEquals`); a HashSet can be built from one, and + from a map's key set. Any call on the list that is not modelled ends that. + - **SWAT's own helpers do not end a record:** the reference comparison `map == null` is rewritten + to `RefIdentity.same`, which is passed the container; neither it nor the ignored sinks + (`InvocationHandler.IGNORED_INVOCATIONS`) count as code that may change it. + - **Results** (2026-11-17, 3 GB, 1 core, 900 s): of the 124 round-2 runs that were UNKNOWN by + context loss, 33 become correct (31 SAFE, 2 VIOLATION), none wrong; 244 other runs of tasks + with hash containers keep their verdicts. Left in cluster A: MessagePdu (the arrays behind + `Arrays.asList` have no address when copied), the `_exceptionprone` runs (uncaught-exception + downgrade), PasswordSlotManager and ViewHierarchyEncoder (file I/O). + - **Tests:** swat-benchmarks `model-soundness`, `hash-*`, `hashset-*`, `emptymap-*`, `list-equals*` + (`scripts/model_soundness_hash.py`; its README lists which tasks must stay UNKNOWN), and the + executor's `HashContainerModelTest` (`./gradlew :symbolic-executor:test --tests + 'de.uzl.its.value.reference.HashContainerModelTest'`). - **Hangs**: the executor's `DeadlockWatchdog` (config `deadlockWatchdog`) ends a run whose application threads all wait forever, and `ResourceBudget` has a wall-clock deadline (default: the CPU limit), so a hang ends UNKNOWN instead of being killed by the harness. diff --git a/scripts/svcomp-package/compile-target.sh b/scripts/svcomp-package/compile-target.sh index 5d2e5ff5..b7187dc8 100755 --- a/scripts/svcomp-package/compile-target.sh +++ b/scripts/svcomp-package/compile-target.sh @@ -32,4 +32,10 @@ java_files=$(find "$target_directory" -name "*.java") echo "javac -source 1.8 -target 1.8 -cp \"$classpath\" -d \"$build_directory\" $java_files" # Compile all the found .java files -javac -source 1.8 -target 1.8 -cp "$classpath" -d "$build_directory" $java_files \ No newline at end of file +if ! javac -source 1.8 -target 1.8 -cp "$classpath" -d "$build_directory" $java_files; then + # From JDK 25 on, every compilation unit imports java.lang.IO (JEP 512), which makes a + # wildcard-imported class named IO ambiguous (juliet's testcasesupport.IO: "reference to IO is + # ambiguous"). --release 8 compiles against the Java 8 API, which has no java.lang.IO. + echo "javac --release 8 -cp \"$classpath\" -d \"$build_directory\" $java_files" + javac --release 8 -cp "$classpath" -d "$build_directory" $java_files +fi diff --git a/scripts/svcomp-package/requirements.txt b/scripts/svcomp-package/requirements.txt index 2aa30685..05285212 100644 --- a/scripts/svcomp-package/requirements.txt +++ b/scripts/svcomp-package/requirements.txt @@ -17,5 +17,5 @@ typing-inspection==0.4.4 typing_extensions==4.16.0 urllib3==2.8.0 uvicorn==0.54.0 -witnesscreator @ git+https://github.com/SWAT-project/WitnessCreator@v1.1.0 +witnesscreator @ git+https://github.com/SWAT-project/WitnessCreator@v1.1.1 z3-solver==5.3.1.0 diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/SymbolicInstructionVisitor.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/SymbolicInstructionVisitor.java index 91942883..4d4d4c52 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/SymbolicInstructionVisitor.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/SymbolicInstructionVisitor.java @@ -33,10 +33,13 @@ import de.uzl.its.swat.symbolic.value.primitive.numeric.floatingpoint.DoubleValue; import de.uzl.its.swat.symbolic.value.primitive.numeric.floatingpoint.FloatValue; import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.*; +import de.uzl.its.swat.symbolic.value.reference.CapturingLambdaValue; import de.uzl.its.swat.symbolic.value.reference.ObjectValue; import de.uzl.its.swat.symbolic.value.reference.StringBuilderValue; import de.uzl.its.swat.symbolic.value.reference.array.*; import de.uzl.its.swat.symbolic.value.reference.lang.*; +import de.uzl.its.swat.symbolic.value.reference.regex.MatcherValue; +import de.uzl.its.swat.symbolic.value.reference.regex.PatternValue; import de.uzl.its.swat.common.logging.records.InvocationEntry; import de.uzl.its.swat.thread.ThreadHandler; @@ -1431,9 +1434,24 @@ public void visitGETSTATIC(GETSTATIC inst) throws SymbolicInstructionException { public void visitGETVALUE_Object(GETVALUE_Object inst) throws SymbolicInstructionException { try { Value peek = stack.peekOperand(); + if (inst.val != null && (peek instanceof StringValue || peek instanceof BoxedValue)) { + // A String or boxed shadow whose value is not the object's is no key the exact + // model of hash containers compares (see HashEntries.supported) + boolean same = peek instanceof StringValue string + ? inst.val instanceof String s && s.equals(string.concrete) + : ((BoxedValue) peek).getVal() != null && inst.val.equals(((BoxedValue) peek).getVal().concrete); + if (!same) { + ((ObjectValue) peek).valueUnknown = true; + } + } Value tmp; boolean isSymbolic = false; + if (peek instanceof CapturingLambdaValue lambda && lambda.className == null && inst.val != null) { + // Created by INVOKEDYNAMIC before the JVM named the generated class + lambda.className = Util.formatClassName(inst.val.getClass().getName()); + } + if (peek == PlaceHolder.symbolicInstance) { isSymbolic = true; peek = PlaceHolder.instance; @@ -1461,6 +1479,12 @@ public void visitGETVALUE_Object(GETVALUE_Object inst) throws SymbolicInstruc stack.popOperand(); // try to get object tmp = stack.getFromHeap(inst.address); + if ((tmp instanceof PatternValue pv && !pv.shadows(inst.val)) + || (tmp instanceof MatcherValue mv && !mv.shadows(inst.val))) { + // A different object with the same identity hash: the regex shadows hold + // exact concrete state, never reuse one for another object. + tmp = null; + } // check if the object was created earlier and then reuse it if (tmp != null) { if (isSymbolic) { @@ -1536,6 +1560,9 @@ public void visitGETVALUE_Object(GETVALUE_Object inst) throws SymbolicInstruc } } else { + if (peek instanceof MatcherValue mv) { + mv.bind(inst.val); + } (peek.asObjectValue()).setAddress(inst.address); stack.putToHeap(inst.address, peek); } @@ -2501,7 +2528,7 @@ private boolean recordCallBranches(long iid, String name, List */ - private void callRunsApplicationCode(String name, String desc) throws NoThreadContextException { + private void callRunsApplicationCode(String owner, String name, String desc, long invokeId) throws NoThreadContextException { ObjectValue instance; Value[] arguments; try { @@ -2510,6 +2537,20 @@ private void callRunsApplicationCode(String name, String desc) throws NoThreadCo } catch (Exception e) { return; // the callee is instrumented code with its own frame } + if (instance != null && CollectionInvocation.isContainer(instance)) { + // No model is applied: the exact entries of a hash container are not known any more + CollectionInvocation.escaped(instance, owner, name, desc, invokeId); + } + boolean containerArgument = false; + for (Value argument : arguments) { + containerArgument |= argument instanceof ObjectValue ov && ov.getContents() != null; + } + if (containerArgument && !Util.shouldInstrument(Util.formatClassName(owner))) { + // Maybe a library method that calls back: it may change the containers it is given + for (Value argument : arguments) { + CollectionInvocation.escaped(argument, owner, name, desc, invokeId); + } + } if (instance instanceof de.uzl.its.swat.symbolic.value.reference.StringBuilderValue sbv) { if (sbv.unmodelledChange(name)) { logger.warn("StringBuilder.{} ran application code: its chars are not tracked, causes context loss", name); @@ -2545,6 +2586,8 @@ private void recordThrowingInstanceCall(long iid, String owner, String name, Str ObjectValue instance = stack.getInstance(); Value[] arguments = stack.fetchArgumentsFromLocals(Type.getArgumentTypes(desc), true); explained = recordStringCallBranches(iid, name, desc, instance, arguments, false); + // A container call that threw is not modelled: its exact entries are given up + CollectionInvocation.escaped(instance, owner, name, desc, invokeId); } catch (Exception e) { explained = false; } @@ -2570,7 +2613,11 @@ private void recordThrowingCall(long iid, String owner, String name, String desc try { Value[] arguments = stack.fetchArgumentsFromLocals(Type.getArgumentTypes(desc), isInstance); symbolic = isInstance && stack.getInstance().isSymbolic(); - for (Value argument : arguments) symbolic = symbolic || argument.isSymbolic(); + for (Value argument : arguments) { + symbolic = symbolic || argument.isSymbolic(); + // The call may have changed a container it was given before it threw + CollectionInvocation.escaped(argument, owner, name, desc, invokeId); + } } catch (Exception e) { symbolic = true; // cannot tell, so assume the worst } @@ -2607,12 +2654,13 @@ public void visitINVOKEINTERFACE(INVOKEINTERFACE inst) throws SymbolicInstructio new ArrayList<>(Arrays.asList(arguments)), true, instance); + recordCallBranches(inst.iid, inst.name, CollectionInvocation.takeBranches(), true); stack.setReturnValue(retVal); } else if (stack.getNextInst() instanceof INVOKEMETHOD_EXCEPTION) { recordThrowingInstanceCall(inst.iid, inst.owner, inst.name, inst.desc, inst.invokeId); } else { - callRunsApplicationCode(inst.name, inst.desc); + callRunsApplicationCode(inst.owner, inst.name, inst.desc, inst.invokeId); } if (!inst.owner.equals("de/uzl/its/swat/Main")) { @@ -2647,11 +2695,12 @@ public void visitINVOKESPECIAL(INVOKESPECIAL inst) throws SymbolicInstructionExc new ArrayList<>(Arrays.asList(arguments)), true, instance); + recordCallBranches(inst.iid, inst.name, CollectionInvocation.takeBranches(), true); stack.setReturnValue(retVal); } else if (stack.getNextInst() instanceof INVOKEMETHOD_EXCEPTION) { recordThrowingInstanceCall(inst.iid, inst.owner, inst.name, inst.desc, inst.invokeId); } else { - callRunsApplicationCode(inst.name, inst.desc); + callRunsApplicationCode(inst.owner, inst.name, inst.desc, inst.invokeId); } if (!inst.owner.equals("de/uzl/its/swat/Main")) { @@ -2685,6 +2734,7 @@ public void visitINVOKESTATIC(INVOKESTATIC inst) throws SymbolicInstructionExcep new ArrayList<>(Arrays.asList(arguments)), false, null); + recordCallBranches(inst.iid, inst.name, CollectionInvocation.takeBranches(), true); stack.setReturnValue(retVal); } else if (stack.getNextInst() instanceof INVOKEMETHOD_EXCEPTION) { recordThrowingStaticCall(inst.iid, inst.owner, inst.name, inst.desc, inst.invokeId); @@ -2771,11 +2821,12 @@ public void visitINVOKEVIRTUAL(INVOKEVIRTUAL inst) throws SymbolicInstructionExc new ArrayList<>(Arrays.asList(arguments)), true, instance); + recordCallBranches(inst.iid, inst.name, CollectionInvocation.takeBranches(), true); stack.setReturnValue(retVal); } else if (stack.getNextInst() instanceof INVOKEMETHOD_EXCEPTION) { recordThrowingInstanceCall(inst.iid, inst.owner, inst.name, inst.desc, inst.invokeId); } else { - callRunsApplicationCode(inst.name, inst.desc); + callRunsApplicationCode(inst.owner, inst.name, inst.desc, inst.invokeId); } if (!inst.owner.equals("de/uzl/its/swat/Main")) { @@ -2799,16 +2850,29 @@ public void visitINVOKEDYNAMIC(INVOKEDYNAMIC inst) throws SymbolicInstructionExc if (stack.getNextInst() instanceof INVOKEMETHOD_END) { Value[] arguments = stack.fetchArgumentsFromLocals(Type.getArgumentTypes(inst.desc), false); - Value retVal = InvocationHandler.invoke( - symbolicTraceHandler, - inst.desc, - inst.owner, - inst.name, - inst.invokeId, - new ArrayList<>(Arrays.asList(arguments)), - false, - null); - stack.setReturnValue(retVal); + // A lambda capturing arguments for instrumented code: the capture itself only + // stores them (see CapturingLambdaValue), the body runs when the lambda is called. + CapturingLambdaValue lambda = inst.owner.equals("java/lang/invoke/LambdaMetafactory") + ? CapturingLambdaValue.create( + ThreadHandler.getSolverContext(currentThread().getId()), + ValueFactory.createNumericalValue(ValueType.intValue, arguments.length).asIntValue(), + inst.name, inst.desc, inst.lambda, arguments) + : null; + if (lambda != null) { + logger.debug("[INVOKEDYNAMIC] Lambda capturing {} argument(s): {}", arguments.length, lambda); + stack.setReturnValue(lambda); + } else { + Value retVal = InvocationHandler.invoke( + symbolicTraceHandler, + inst.desc, + inst.owner, + inst.name, + inst.invokeId, + new ArrayList<>(Arrays.asList(arguments)), + false, + null); + stack.setReturnValue(retVal); + } } else if (stack.getNextInst() instanceof INVOKEMETHOD_EXCEPTION) { recordThrowingCall(inst.iid, inst.owner, inst.name, inst.desc, inst.invokeId, false); } @@ -4124,6 +4188,10 @@ private void visitGETVALUE_primitive(GETVALUE_primitive inst, ValueType type) th } else { stack.popOperand(); } + if (placeHolder instanceof ContainerPlaceHolder containerPlaceHolder) { + // What a hash container's exact entries say the call returned, against the JVM + CollectionInvocation.checkPrimitive(containerPlaceHolder, inst.v); + } Value v = ValueFactory.createNumericalValue(type, inst.v); if (isSymbolic) { v.MAKE_SYMBOLIC(); diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/InvocationHandler.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/InvocationHandler.java index a66b091a..b91d42ce 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/InvocationHandler.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/InvocationHandler.java @@ -38,6 +38,14 @@ public class InvocationHandler { "java/io/InputStream", "java/util/Scanner")); + /** + * Library classes whose methods only read the objects they are given: passing them a hash + * container does not end the exact record of its entries (see CollectionInvocation). + */ + private static final java.util.Set READS_ITS_ARGUMENTS = java.util.Set.of( + "java/util/Objects", "java/lang/String", "java/lang/StringBuilder", "java/lang/StringBuffer", + "java/io/PrintStream", "java/lang/System"); + // The concatenation keeps the ShadowJar relocation from rewriting the owner private static final String OBJECT_FACTORY = "org/".concat("sosy_lab/sv_benchmarks/ObjectFactory"); @@ -88,10 +96,17 @@ private static boolean hasSymbolicElement(Value v) throws NotImplementedEx if (containerResult != null) { return containerResult; } - boolean ignored = IGNORED_INVOCATIONS.contains(owner + "/" + name) || IGNORED_INVOCATIONS.contains(owner); + // A call the container models do not cover may change the containers it is given (not + // SWAT's own helpers, e.g. the reference comparison RefIdentity.same, nor the ignored sinks) + if (!ignored && !owner.startsWith("de/uzl/its/swat/") && !READS_ITS_ARGUMENTS.contains(owner)) { + for (Value argument : arguments) { + CollectionInvocation.escaped(argument, owner, name, desc, invokeId); + } + } + // We do that first to include the pointer in instance invocation cases. Ignored // invocations are never recorded, so their arguments need no (possibly deep) scan. boolean containsSymbolicArgument = !ignored && arguments.stream().anyMatch(v -> { @@ -106,6 +121,9 @@ private static boolean hasSymbolicElement(Value v) throws NotImplementedEx if (isInstance) { containsSymbolicArgument = containsSymbolicArgument || (!ignored && instance.isSymbolic()); retValue = instance.invokeMethod(name, types, arguments.toArray(new Value[0])); + if (instance.valueUnknown && name.equals("") && !(retValue instanceof PlaceHolder)) { + instance.valueUnknown = false; // the constructor's model has set the shadow value + } } else { retValue = StaticInvocation.invokeMethod( diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/StaticInvocation.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/StaticInvocation.java index 64f80076..3b4a36aa 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/StaticInvocation.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/StaticInvocation.java @@ -16,10 +16,12 @@ import de.uzl.its.swat.symbolic.invoke.java.lang.ShortInvocation; import de.uzl.its.swat.symbolic.invoke.java.lang.StringInvocation; import de.uzl.its.swat.symbolic.invoke.java.lang.SystemInvocation; +import de.uzl.its.swat.symbolic.invoke.java.util.ArraysInvocation; import de.uzl.its.swat.symbolic.invoke.java.util.ObjectsInvocation; import de.uzl.its.swat.symbolic.trace.SymbolicTraceHandler; import de.uzl.its.swat.symbolic.value.PlaceHolder; import de.uzl.its.swat.symbolic.value.Value; +import de.uzl.its.swat.symbolic.value.reference.lang.StringValue; public final class StaticInvocation { @@ -57,10 +59,25 @@ public final class StaticInvocation { case "java/lang/System" -> SystemInvocation.invokeStaticMethod(name, args, desc); case "de/uzl/its/swat/common/RefIdentity" -> RefIdentityInvocation.invokeStaticMethod( name, args, symbolicStateHandler); + case "java/util/Arrays" -> ArraysInvocation.invokeStaticMethod(name, args, desc); case "java/util/Objects" -> ObjectsInvocation.invokeStaticMethod( name, args, desc, symbolicStateHandler); + case "java/util/regex/Pattern" -> invokePatternStatic(name, desc, args); default -> PlaceHolder.instance; }; return ret; } + + /** + * Pattern.matches(regex, input) is, by its specification, input.matches(regex) for a String + * input: modelled as String.matches (a concrete regular expression JavaRegex translates). + */ + private static Value invokePatternStatic(String name, Type[] desc, Value[] args) + throws NotImplementedException, ValueConversionException { + if (name.equals("matches") && args.length == 2 + && args[0] instanceof StringValue regex && args[1] instanceof StringValue input) { + return input.invokeMethod("matches", new Type[] {desc[0]}, new Value[] {regex}); + } + return PlaceHolder.instance; + } } diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/java/util/ArraysInvocation.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/java/util/ArraysInvocation.java new file mode 100644 index 00000000..d6849d6d --- /dev/null +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/java/util/ArraysInvocation.java @@ -0,0 +1,313 @@ +package de.uzl.its.swat.symbolic.invoke.java.util; + +import de.uzl.its.swat.common.exceptions.NotImplementedException; +import de.uzl.its.swat.symbolic.value.PlaceHolder; +import de.uzl.its.swat.symbolic.value.Value; +import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.BooleanValue; +import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.ByteValue; +import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.CharValue; +import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.IntValue; +import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.LongValue; +import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.ShortValue; +import de.uzl.its.swat.symbolic.value.reference.ObjectValue; +import de.uzl.its.swat.symbolic.value.reference.array.AbstractArrayValue; +import de.uzl.its.swat.symbolic.value.reference.array.BooleanArrayValue; +import de.uzl.its.swat.symbolic.value.reference.array.ByteArrayValue; +import de.uzl.its.swat.symbolic.value.reference.array.CharArrayValue; +import de.uzl.its.swat.symbolic.value.reference.array.IntArrayValue; +import de.uzl.its.swat.symbolic.value.reference.array.LongArrayValue; +import de.uzl.its.swat.symbolic.value.reference.array.ObjectArrayValue; +import de.uzl.its.swat.symbolic.value.reference.array.ShortArrayValue; +import de.uzl.its.swat.symbolic.value.reference.lang.BooleanObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.BoxedValue; +import de.uzl.its.swat.symbolic.value.reference.lang.ByteObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.CharacterObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.IntegerObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.LongObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.ShortObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.StringValue; +import java.lang.reflect.Array; +import org.objectweb.asm.Type; +import org.sosy_lab.java_smt.api.BitvectorFormula; +import org.sosy_lab.java_smt.api.BitvectorFormulaManager; +import org.sosy_lab.java_smt.api.BooleanFormula; +import org.sosy_lab.java_smt.api.BooleanFormulaManager; +import org.sosy_lab.java_smt.api.Formula; +import org.sosy_lab.java_smt.api.SolverContext; + +/** + * Exact models of {@code java.util.Arrays.equals} and {@code java.util.Arrays.hashCode} for arrays + * of a concrete length whose elements SWAT encodes exactly. Everything else (float/double + * elements, symbolic lengths, unknown elements, Object[] elements other than String, boxed + * integers and null, Strings in a hash) returns a PlaceHolder, i.e. stays a missing invocation. + * + *

The model runs after the concrete call returned; its concrete result is computed from the + * shadows, so a stale shadow shows up as a divergence at GETVALUE (context loss). Elements without + * symbolic variables are taken by their concrete value, so a stale constant formula cannot be + * relied on. + */ +public final class ArraysInvocation { + + /** Longer arrays are not modelled: each element adds a conjunct / a multiplication. */ + static final int MAX_LENGTH = 4096; + + private ArraysInvocation() {} + + public static Value invokeStaticMethod(String name, Value[] args, Type[] desc) throws NotImplementedException { + if (name.equals("equals") && args.length == 2 && desc.length == 2) { + return invokeEquals(args[0], args[1], desc[0].getDescriptor(), desc[1].getDescriptor()); + } + if (name.equals("hashCode") && args.length == 1 && desc.length == 1) { + return hashCode(args[0], desc[0].getDescriptor()); + } + return PlaceHolder.instance; + } + + private static boolean isNull(Value v) { + return v instanceof ObjectValue o && o.isNullValue(); + } + + private static boolean symbolic(SolverContext ctx, Formula f) { + return !ctx.getFormulaManager().extractVariables(f).isEmpty(); + } + + /** The concrete length of a primitive array shadow, or -1 if it is not known exactly. */ + private static int length(AbstractArrayValue a) { + if (a.size == null || a.size.isSymbolic() || a.concrete == null) return -1; + int n = a.size.concrete; + if (n < 0 || n > MAX_LENGTH || Array.getLength(a.concrete) != n) return -1; + return n; + } + + private static int length(ObjectArrayValue a) { + if (a.size == null || a.size.isSymbolic() || a.getFields() == null) return -1; + int n = a.size.concrete; + if (n < 0 || n > MAX_LENGTH || a.getFields().length != n) return -1; + return n; + } + + // ---------------------------------------------------------------- equals + + private static Value invokeEquals(Value a, Value b, String descA, String descB) throws NotImplementedException { + if (!descA.equals(descB)) return PlaceHolder.instance; + SolverContext ctx; + if (a instanceof ObjectValue oa) ctx = oa.context; else return PlaceHolder.instance; + // a == b: true; a or b null: false (the references are concrete) + if (isNull(a) || isNull(b)) return new BooleanValue(ctx, isNull(a) && isNull(b)); + if (a == b) return new BooleanValue(ctx, true); + return switch (descA) { + case "[I", "[J", "[S", "[B", "[C", "[Z" -> primitiveEquals(ctx, a, b, descA); + case "[Ljava/lang/Object;" -> objectEquals(ctx, a, b); + default -> PlaceHolder.instance; + }; + } + + private static Value primitiveEquals(SolverContext ctx, Value a, Value b, String desc) { + if (!(a instanceof AbstractArrayValue x) || !(b instanceof AbstractArrayValue y) + || a.getClass() != b.getClass() || !descMatches(desc, x)) { + return PlaceHolder.instance; + } + int n = length(x); + int m = length(y); + if (n < 0 || m < 0) return PlaceHolder.instance; + BooleanFormulaManager bmgr = ctx.getFormulaManager().getBooleanFormulaManager(); + if (n != m) return new BooleanValue(ctx, false); + boolean result = true; + BooleanFormula formula = bmgr.makeTrue(); + for (int i = 0; i < n; i++) { + BooleanValue eq = elementEquals(ctx, element(ctx, x, i), element(ctx, y, i)); + if (eq == null) return PlaceHolder.instance; + result &= eq.concrete; + formula = bmgr.and(formula, eq.formula); + } + return new BooleanValue(ctx, result, formula); + } + + /** Element i of a primitive array, as an int (byte/short sign-, char zero-extended), long or boolean. */ + private static Value element(SolverContext ctx, AbstractArrayValue array, int i) { + IntValue idx = new IntValue(ctx, i); + if (array instanceof IntArrayValue a) return constantIfConcrete(ctx, a.getElement(idx)); + if (array instanceof ByteArrayValue a) return constantIfConcrete(ctx, a.getElement(idx).asIntValue()); + if (array instanceof ShortArrayValue a) return constantIfConcrete(ctx, a.getElement(idx).asIntValue()); + if (array instanceof CharArrayValue a) return constantIfConcrete(ctx, a.getElement(idx).asIntValue()); + if (array instanceof LongArrayValue a) return constantIfConcrete(ctx, a.getElement(idx)); + if (array instanceof BooleanArrayValue a) return constantIfConcrete(ctx, a.getElement(idx)); + return null; + } + + private static Value constantIfConcrete(SolverContext ctx, Value v) { + if (v instanceof IntValue i && !symbolic(ctx, i.formula)) return new IntValue(ctx, i.concrete); + if (v instanceof LongValue l && !symbolic(ctx, l.formula)) return new LongValue(ctx, l.concrete); + if (v instanceof BooleanValue z && !symbolic(ctx, z.formula)) return new BooleanValue(ctx, z.concrete); + return v; + } + + /** x == y for two values of the same primitive kind (int, long or boolean), else null. */ + private static BooleanValue elementEquals(SolverContext ctx, Value x, Value y) { + BitvectorFormulaManager bv = ctx.getFormulaManager().getBitvectorFormulaManager(); + BooleanFormulaManager bmgr = ctx.getFormulaManager().getBooleanFormulaManager(); + if (x instanceof IntValue i && y instanceof IntValue j) { + return new BooleanValue(ctx, i.concrete.intValue() == j.concrete.intValue(), bv.equal(i.formula, j.formula)); + } + if (x instanceof LongValue i && y instanceof LongValue j) { + return new BooleanValue(ctx, i.concrete.longValue() == j.concrete.longValue(), bv.equal(i.formula, j.formula)); + } + if (x instanceof BooleanValue i && y instanceof BooleanValue j) { + return new BooleanValue(ctx, i.concrete.booleanValue() == j.concrete.booleanValue(), bmgr.equivalence(i.formula, j.formula)); + } + return null; + } + + /** + * Arrays.equals(Object[], Object[]): Objects.equals(a[i], b[i]) for every i, i.e. identity, + * else a non-null a[i] whose equals decides. Exact for null, String and the boxed integral + * types (their equals is false for another class); any other non-identical element is not + * modelled. + */ + private static Value objectEquals(SolverContext ctx, Value a, Value b) throws NotImplementedException { + if (a.getClass() != ObjectArrayValue.class || b.getClass() != ObjectArrayValue.class) return PlaceHolder.instance; + ObjectArrayValue x = (ObjectArrayValue) a; + ObjectArrayValue y = (ObjectArrayValue) b; + int n = length(x); + int m = length(y); + if (n < 0 || m < 0) return PlaceHolder.instance; + if (n != m) return new BooleanValue(ctx, false); + BooleanFormulaManager bmgr = ctx.getFormulaManager().getBooleanFormulaManager(); + boolean result = true; + BooleanFormula formula = bmgr.makeTrue(); + for (int i = 0; i < n; i++) { + Value u = x.getFields()[i]; + Value v = y.getFields()[i]; + BooleanValue eq = objectElementEquals(ctx, u, v); + if (eq == null) return PlaceHolder.instance; + result &= eq.concrete; + formula = bmgr.and(formula, eq.formula); + } + return new BooleanValue(ctx, result, formula); + } + + private static boolean exactlyModelled(Value v) { + if (v == null) return false; + Class c = v.getClass(); + if (c == StringValue.class) return true; + if (c == IntegerObjectValue.class || c == LongObjectValue.class || c == ShortObjectValue.class + || c == ByteObjectValue.class || c == CharacterObjectValue.class || c == BooleanObjectValue.class) { + return ((BoxedValue) v).getVal() != null && ((BoxedValue) v).getVal().concrete != null; + } + return false; + } + + private static BooleanValue objectElementEquals(SolverContext ctx, Value u, Value v) throws NotImplementedException { + if (u == null || v == null || u instanceof PlaceHolder || v instanceof PlaceHolder) return null; // unknown + boolean uNull = isNull(u); + boolean vNull = isNull(v); + if (uNull || vNull) { + // null equals only null; a non-null a[i] decides by its equals, false for null if exact + if (!uNull && !exactlyModelled(u)) return null; + return new BooleanValue(ctx, uNull && vNull); + } + if (u == v) return new BooleanValue(ctx, true); + if (!exactlyModelled(u) || !exactlyModelled(v)) return null; + if (u.getClass() != v.getClass()) return new BooleanValue(ctx, false); // equals of another class + if (u instanceof StringValue s && v instanceof StringValue t) { + // as StringValue's equals model + if (s.concrete == null || t.concrete == null) return null; + return new BooleanValue(ctx, s.concrete.equals(t.concrete), + ctx.getFormulaManager().getStringFormulaManager().equal(s.formula, t.formula)); + } + Value p = ((BoxedValue) u).getVal(); + Value q = ((BoxedValue) v).getVal(); + Value pi = asComparable(ctx, p); + Value qi = asComparable(ctx, q); + if (pi == null || qi == null) return null; + return elementEquals(ctx, pi, qi); + } + + /** A boxed value as int (byte, short, char, int), long or boolean, for equality and hashing. */ + private static Value asComparable(SolverContext ctx, Value p) { + if (p instanceof IntValue i) return constantIfConcrete(ctx, i); + if (p instanceof ByteValue i) return constantIfConcrete(ctx, i.asIntValue()); + if (p instanceof ShortValue i) return constantIfConcrete(ctx, i.asIntValue()); + if (p instanceof CharValue i) return constantIfConcrete(ctx, i.asIntValue()); + if (p instanceof LongValue l) return constantIfConcrete(ctx, l); + if (p instanceof BooleanValue z) return constantIfConcrete(ctx, z); + return null; + } + + // ---------------------------------------------------------------- hashCode + + /** Arrays.hashCode: 0 for null, else h = 31 * h + hash(e) from h = 1, in 32-bit arithmetic. */ + private static Value hashCode(Value a, String desc) throws NotImplementedException { + if (!(a instanceof ObjectValue oa)) return PlaceHolder.instance; + SolverContext ctx = oa.context; + if (isNull(a)) return new IntValue(ctx, 0); + Value[] elements; + switch (desc) { + case "[I", "[J", "[S", "[B", "[C", "[Z" -> { + if (!(a instanceof AbstractArrayValue x)) return PlaceHolder.instance; + if (!descMatches(desc, x)) return PlaceHolder.instance; + int n = length(x); + if (n < 0) return PlaceHolder.instance; + elements = new Value[n]; + for (int i = 0; i < n; i++) elements[i] = element(ctx, x, i); + } + case "[Ljava/lang/Object;" -> { + if (a.getClass() != ObjectArrayValue.class) return PlaceHolder.instance; + ObjectArrayValue x = (ObjectArrayValue) a; + int n = length(x); + if (n < 0) return PlaceHolder.instance; + elements = new Value[n]; + for (int i = 0; i < n; i++) { + Value e = x.getFields()[i]; + if (e == null || e instanceof PlaceHolder) return PlaceHolder.instance; + if (isNull(e)) { + elements[i] = new IntValue(ctx, 0); // null hashes to 0 + } else if (exactlyModelled(e) && e instanceof BoxedValue boxed) { + // Integer/Short/Byte/Character.hashCode() is the (widened) value, + // Long.hashCode() and Boolean.hashCode() as below. (No String: its + // hashCode is not modelled.) + elements[i] = asComparable(ctx, boxed.getVal()); + } else { + return PlaceHolder.instance; + } + } + } + default -> { + return PlaceHolder.instance; + } + } + IntValue h = new IntValue(ctx, 1); + IntValue thirtyOne = new IntValue(ctx, 31); + for (Value e : elements) { + IntValue eh = elementHash(ctx, e); + if (eh == null) return PlaceHolder.instance; + h = thirtyOne.IMUL(h).IADD(eh); + } + return h; + } + + private static boolean descMatches(String desc, AbstractArrayValue x) { + return switch (desc) { + case "[I" -> x instanceof IntArrayValue; + case "[J" -> x instanceof LongArrayValue; + case "[S" -> x instanceof ShortArrayValue; + case "[B" -> x instanceof ByteArrayValue; + case "[C" -> x instanceof CharArrayValue; + case "[Z" -> x instanceof BooleanArrayValue; + default -> false; + }; + } + + /** int: the value; long: (int) (v ^ (v >>> 32)); boolean: v ? 1231 : 1237. */ + private static IntValue elementHash(SolverContext ctx, Value e) { + if (e instanceof IntValue i) return i; + if (e instanceof LongValue l) return l.LXOR(l.LUSHR(new IntValue(ctx, 32))).L2I(); + if (e instanceof BooleanValue z) { + BitvectorFormulaManager bv = ctx.getFormulaManager().getBitvectorFormulaManager(); + BooleanFormulaManager bmgr = ctx.getFormulaManager().getBooleanFormulaManager(); + BitvectorFormula f = bmgr.ifThenElse(z.formula, bv.makeBitvector(32, 1231), bv.makeBitvector(32, 1237)); + return new IntValue(ctx, z.concrete ? 1231 : 1237, f); + } + return null; + } +} diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/java/util/CollectionInvocation.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/java/util/CollectionInvocation.java index 3600f6e9..ff9b8013 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/java/util/CollectionInvocation.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/java/util/CollectionInvocation.java @@ -5,6 +5,7 @@ import de.uzl.its.swat.common.exceptions.NotImplementedException; import de.uzl.its.swat.common.logging.GlobalLogger; import de.uzl.its.swat.common.logging.records.InvocationEntry; +import de.uzl.its.swat.symbolic.invoke.java.lang.StringCallBranches.Branch; import de.uzl.its.swat.symbolic.shadow.ShadowContext; import de.uzl.its.swat.symbolic.trace.SymbolicTraceHandler; import de.uzl.its.swat.symbolic.value.ContainerPlaceHolder; @@ -15,12 +16,14 @@ import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.IntValue; import de.uzl.its.swat.symbolic.value.reference.ContainerContents; import de.uzl.its.swat.symbolic.value.reference.ContainerContents.Kind; +import de.uzl.its.swat.symbolic.value.reference.HashEntries; import de.uzl.its.swat.symbolic.value.reference.ObjectValue; import de.uzl.its.swat.symbolic.value.reference.array.AbstractArrayValue; import de.uzl.its.swat.symbolic.value.reference.array.ObjectArrayValue; import de.uzl.its.swat.symbolic.value.reference.lang.BoxedValue; import de.uzl.its.swat.symbolic.value.reference.lang.StringValue; import de.uzl.its.swat.thread.ThreadHandler; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -39,9 +42,28 @@ * {@code equals}, {@code hashCode}, {@code toString}, streams, ...) is not modelled: a container * holding a symbolic value counts as symbolic, so such a call records context loss as before. * - *

Hashed parts are modelled only while they are concrete: a map key must not be symbolic, and a - * map whose stored keys have become symbolic is not modelled any more. Index arguments must be - * concrete too. Anything else falls back to the generic handling of a missing invocation. + *

Hashed parts are modelled by reference only while they are concrete: there a map key must + * not be symbolic, and a map whose stored keys have become symbolic is not modelled any more. Index + * arguments must be concrete too. Anything else falls back to the generic handling of a missing + * invocation. + * + *

Exact hash containers. A {@code HashMap}, {@code LinkedHashMap}, {@code + * ConcurrentHashMap}, {@code HashSet} or {@code LinkedHashSet} whose creation and every call since were modelled has an exact record of + * its entries ({@link HashEntries}). With it, calls by key are modelled for symbolic String and + * boxed integral keys too ({@link #hashCall}): the lookup is recorded as the equality chain {@code + * key.equals(k_j)} over the stored keys, library branches as those of the String models ({@link + * #takeBranches}), so the path fixes which entry matched and the solver reaches the others. What + * the call returns then follows from the record, and is checked against what the JVM returned + * ({@link #resolve}, {@link #checkPrimitive}). + * + *

The record is given up ({@link #forget}) by anything that is not modelled exactly: a call + * outside the tables, a key of another class (its equals is unknown), a call that ran application + * code or threw, the container (or a view of it) passed to code that is not modelled, a change + * through a view, a result that does not match. If the container held a symbolic key, giving up is + * a context loss, and every later call by key is one as well ({@link + * ContainerContents#keysSymbolic}). The iteration order of a plain hash container depends on the + * keys' hash codes: iterating (or copying) one with two or more entries and a symbolic key is a + * context loss, whereas a linked one iterates in the recorded order. */ public final class CollectionInvocation { private static final Logger logger = GlobalLogger.getSymbolicExecutionLogger(); @@ -56,6 +78,80 @@ public final class CollectionInvocation { for (String c : List.of("HashMap", "LinkedHashMap", "Hashtable")) { KIND_BY_CLASS.put("java/util/" + c, Kind.MAP); } + for (String c : List.of("HashSet", "LinkedHashSet")) { + KIND_BY_CLASS.put("java/util/" + c, Kind.SET); + } + // By key it behaves like a HashMap, except that a null key or value throws (and a call + // that throws is never modelled) + KIND_BY_CLASS.put("java/util/concurrent/ConcurrentHashMap", Kind.MAP); + } + + /** The classes with an exact record of their entries. */ + private static final Set HASH_CLASSES = Set.of( + "java/util/HashMap", "java/util/LinkedHashMap", "java/util/HashSet", "java/util/LinkedHashSet", + "java/util/concurrent/ConcurrentHashMap"); + + /** The calls modelled on the exact entries of a hash container. */ + private enum HashOp { + INIT, COPY_INIT, COPY, GET, GET_OR_DEFAULT, CONTAINS, PUT, PUT_IF_ABSENT, REMOVE, ADD, SIZE, IS_EMPTY, CLEAR + } + + private static final Map HASH_MAP_OPS = Map.ofEntries( + Map.entry("()V", HashOp.INIT), + Map.entry("(I)V", HashOp.INIT), + Map.entry("(IF)V", HashOp.INIT), + Map.entry("(Ljava/util/Map;)V", HashOp.COPY_INIT), + Map.entry("putAll(Ljava/util/Map;)V", HashOp.COPY), + Map.entry("get(Ljava/lang/Object;)Ljava/lang/Object;", HashOp.GET), + Map.entry("getOrDefault(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", HashOp.GET_OR_DEFAULT), + Map.entry("containsKey(Ljava/lang/Object;)Z", HashOp.CONTAINS), + Map.entry("put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", HashOp.PUT), + Map.entry("putIfAbsent(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", HashOp.PUT_IF_ABSENT), + Map.entry("remove(Ljava/lang/Object;)Ljava/lang/Object;", HashOp.REMOVE), + Map.entry("size()I", HashOp.SIZE), + Map.entry("isEmpty()Z", HashOp.IS_EMPTY), + Map.entry("clear()V", HashOp.CLEAR)); + + private static final Map HASH_SET_OPS = Map.ofEntries( + Map.entry("()V", HashOp.INIT), + Map.entry("(I)V", HashOp.INIT), + Map.entry("(IF)V", HashOp.INIT), + Map.entry("(Ljava/util/Collection;)V", HashOp.COPY_INIT), + Map.entry("addAll(Ljava/util/Collection;)Z", HashOp.COPY), + Map.entry("add(Ljava/lang/Object;)Z", HashOp.ADD), + Map.entry("contains(Ljava/lang/Object;)Z", HashOp.CONTAINS), + Map.entry("remove(Ljava/lang/Object;)Z", HashOp.REMOVE), + Map.entry("size()I", HashOp.SIZE), + Map.entry("isEmpty()Z", HashOp.IS_EMPTY), + Map.entry("clear()V", HashOp.CLEAR)); + + /** The immutable empty containers of {@code Collections.emptyMap()}, {@code emptySet()}, {@code emptyList()}. */ + private static final Set EMPTY_CLASSES = Set.of( + "java/util/Collections$EmptyMap", "java/util/Collections$EmptySet", "java/util/Collections$EmptyList"); + + /** Lookups whose result in an empty container does not depend on the argument. */ + private static final Set EMPTY_LOOKUPS = Set.of( + "get(Ljava/lang/Object;)Ljava/lang/Object;", "containsKey(Ljava/lang/Object;)Z", + "containsValue(Ljava/lang/Object;)Z", "contains(Ljava/lang/Object;)Z"); + + /** Returned by {@link #hashCall} for a call that is left to the generic handling. */ + private static final Value NOT_MODELLED = new PlaceHolder(false); + + /** The branches of the last modelled call (the equality chain of a lookup by key). */ + private static final ThreadLocal> BRANCHES = ThreadLocal.withInitial(ArrayList::new); + + /** + * The library branches the model of the last call relies on, for the visitor to record: on + * the path, they fix which entry of a hash container a lookup found. + */ + public static List takeBranches() { + List pending = BRANCHES.get(); + if (pending.isEmpty()) { + return List.of(); + } + List taken = List.copyOf(pending); + pending.clear(); + return taken; } private enum Op { @@ -80,6 +176,8 @@ private record Model(Op op, int arg, int key, boolean initFirst) {} private static final Map LIST = new HashMap<>(); private static final Map MAP = new HashMap<>(); private static final Map VIEW = new HashMap<>(); + /** On a hash set with exact entries, next to the calls by element ({@link #HASH_SET_OPS}). */ + private static final Map SET = new HashMap<>(); private static void put(Map table, Op op, int arg, int key, String... sigs) { for (String sig : sigs) table.put(sig, new Model(op, arg, key, sig.startsWith(""))); @@ -127,6 +225,9 @@ private static void put(Map table, Op op, int arg, int key, Strin "keys()Ljava/util/Enumeration;", "elements()Ljava/util/Enumeration;"); put(MAP, Op.PLAIN, -1, -1, "size()I", "isEmpty()Z", "clear()V"); + put(SET, Op.VIEW, -1, -1, "iterator()Ljava/util/Iterator;"); + put(SET, Op.ARRAY, -1, -1, "toArray()[Ljava/lang/Object;", "toArray([Ljava/lang/Object;)[Ljava/lang/Object;"); + put(VIEW, Op.PLAIN, -1, -1, "hasNext()Z", "hasMoreElements()Z", "hasPrevious()Z", "nextIndex()I", "previousIndex()I", "size()I", "isEmpty()Z", "remove()V"); @@ -152,6 +253,7 @@ private CollectionInvocation() {} boolean isInstance, ObjectValue instance, long invokeId) throws NotImplementedException { + BRANCHES.get().clear(); if (isInstance) { if (instance == null || instance.isNullValue()) { return null; @@ -159,24 +261,462 @@ private CollectionInvocation() {} ContainerContents contents = instance.getContents(); Kind kind = contents != null ? contents.kind : kindOf(instance); if (kind == null) { + if (contents == null && instance.className != null + && EMPTY_CLASSES.contains(instance.className.replace('.', '/')) + && EMPTY_LOOKUPS.contains(name + desc)) { + // Collections.emptyMap/emptySet/emptyList: nothing is a key or an element, so + // the lookup returns null or false whatever its argument is (taken from the JVM) + logger.debug("Modelled lookup {}.{}{} in an empty immutable container", owner, name, desc); + return PlaceHolder.instance; + } return null; } + if (kind == Kind.MAP || kind == Kind.SET) { + Value result; + try { + result = hashCall(kind, owner, name, desc, args, instance, invokeId); + } catch (RuntimeException e) { + // The record may be half updated: give it up + logger.warn("Exact model of {}.{}{} failed", owner, name, desc, e); + BRANCHES.get().clear(); + forget(instance.getContents(), owner, name, desc, invokeId); + result = kind == Kind.SET ? NOT_MODELLED : null; + } + if (result != null) { + return result == NOT_MODELLED ? null : result; + } + contents = instance.getContents(); + } + if (kind == Kind.LIST && name.equals("equals") && desc.equals("(Ljava/lang/Object;)Z")) { + Value equal = listEquals(instance, args[0], + new InvocationEntry(owner, name, desc, true, invokeId, true)); + if (equal != null) { + return equal; + } + } Model model = (switch (kind) { case LIST -> LIST; case MAP -> MAP; + case SET -> SET; case VIEW -> VIEW; }).get(name + desc); + if (kind == Kind.SET && (contents == null || contents.hash() == null)) { + model = null; // a set is modelled only while its exact entries are known + } if (model == null) { + // Not modelled: the call may have changed the container (or the one behind a view) + forget(contents, owner, name, desc, invokeId); return null; } - return apply(model, kind, owner, name, desc, args, instance, invokeId); + if (kind == Kind.VIEW && name.equals("remove")) { + forget(contents, owner, name, desc, invokeId); // Iterator.remove changes the container behind it + } + Value result = apply(model, kind, owner, name, desc, args, instance, invokeId); + if (result == null) { + forget(instance.getContents(), owner, name, desc, invokeId); // not modelled after all + } + return result; } if (owner.equals("java/util/Collections") || owner.equals("java/util/Arrays")) { return invokeStatic(owner, name, desc, args, invokeId); } + if (owner.equals("java/util/Objects") && name.equals("equals") + && desc.equals("(Ljava/lang/Object;Ljava/lang/Object;)Z") + && args[0] instanceof ObjectValue first && !first.isNullValue() + && first.getContents() != null && first.getContents().kind == Kind.LIST) { + // Objects.equals(a, b) with a list a: a.equals(b) + return listEquals(first, args[1], new InvocationEntry(owner, name, desc, false, invokeId, true)); + } return null; } + /** The list classes whose equals compares the elements in order (AbstractList's contract). */ + private static final Set LISTS_EQUAL_BY_ELEMENTS = Set.of( + "java/util/ArrayList", "java/util/LinkedList", "java/util/Vector", "java/util/Stack"); + + /** + * {@code list.equals(other)} for two lists whose elements are known exactly, in order, and + * are all values the exact model compares (null, Strings, boxed integral values, see {@link + * HashEntries#equal}): lists of different sizes differ; else the elements are compared pair by + * pair up to the first that differ, each comparison a branch of the call (see {@link + * #takeBranches}). + * + * @return the result, or {@code null} if the call is not modelled + */ + private static Value listEquals(ObjectValue list, Value other, InvocationEntry call) + throws NotImplementedException { + if (!(other instanceof ObjectValue o) || !isListEqualByElements(list)) { + return null; + } + ContainerContents contents = list.getContents(); + if (o == list) { + return ContainerPlaceHolder.exactPrimitive(contents, true, false, call); + } + if (o.isNullValue()) { + return ContainerPlaceHolder.exactPrimitive(contents, false, false, call); + } + if (!isListEqualByElements(o)) { + return null; // another kind of list, or not a list: its elements (or class) are not known + } + List> xs = contents.exactOrdered(); + List> ys = o.getContents().exactOrdered(); + if (xs.size() != ys.size()) { + // (Flagged symbolic so that a result that differs, from lists that are not what the + // records say, is a context loss if they hold symbolic elements.) + boolean symbolic = contents.isSymbolic(new HashSet<>()) || o.getContents().isSymbolic(new HashSet<>()); + return ContainerPlaceHolder.exactPrimitive(contents, false, symbolic, call); + } + List branches = new ArrayList<>(); + boolean equal = true; + for (int i = 0; i < xs.size() && equal; i++) { + Branch eq = HashEntries.equal(xs.get(i), ys.get(i)); + if (eq == null) { + return null; + } + branches.add(eq); + equal = eq.holds(); + } + BRANCHES.get().addAll(branches); + logger.debug("Modelled {}.{}{} of two lists: {}", call.owner(), call.name(), call.desc(), equal); + return ContainerPlaceHolder.exactPrimitive(contents, equal, true, call); + } + + /** Whether the object is a list with the elementwise equals whose elements are known exactly. */ + private static boolean isListEqualByElements(ObjectValue list) { + return list.className != null && LISTS_EQUAL_BY_ELEMENTS.contains(list.className.replace('.', '/')) + && list.getContents() != null && list.getContents().kind == Kind.LIST + && list.getContents().exactOrdered() != null && list.getContents().exactOrdered().size() <= HashEntries.MAX_ENTRIES; + } + + /** + * A call on a hash map or set by key (or element), modelled on its exact entries. + * + * @return the result of the call; {@link #NOT_MODELLED} to leave the call to the generic + * handling; {@code null} for a call the general model of {@link #apply} decides (a call + * that is not by key, a map without exact entries, or a map call with concrete keys that + * returns a reference, which is found by its address as before: the entries are updated) + */ + private static Value hashCall( + Kind kind, + String owner, + String name, + String desc, + Value[] args, + ObjectValue instance, + long invokeId) throws NotImplementedException { + HashOp op = (kind == Kind.MAP ? HASH_MAP_OPS : HASH_SET_OPS).get(name + desc); + if (op == null) { + return null; + } + InvocationEntry call = new InvocationEntry(owner, name, desc, true, invokeId, true); + String className = instance.className == null ? "" : instance.className.replace('.', '/'); + boolean linked = className.startsWith("java/util/Linked"); + List branches = new ArrayList<>(); + if (op == HashOp.INIT || op == HashOp.COPY_INIT) { + if (!HASH_CLASSES.contains(className)) { + return kind == Kind.SET ? NOT_MODELLED : null; + } + if (op == HashOp.INIT && kind == Kind.MAP) { + return null; // apply creates the contents, with the entries + } + ContainerContents fresh = new ContainerContents(kind); + HashEntries entries = new HashEntries(linked); + fresh.setHash(entries); + if (op == HashOp.COPY_INIT && copyInto(kind, fresh, entries, args[0], branches) == null) { + // A source without exact entries: a map as before, a set is not modelled + return kind == Kind.SET ? NOT_MODELLED : null; + } + instance.setContents(fresh); + BRANCHES.get().addAll(branches); + logger.debug("Modelled hash container call {}.{}{} on {}", owner, name, desc, instance); + return PlaceHolder.instance; + } + ContainerContents contents = instance.getContents(); + HashEntries entries = contents == null ? null : contents.hash(); + if (entries == null) { + return kind == Kind.SET ? NOT_MODELLED : null; + } + switch (op) { + case SIZE -> { + return ContainerPlaceHolder.exactPrimitive(contents, entries.size(), contents.hasSymbolicKeys(), call); + } + case IS_EMPTY -> { + return ContainerPlaceHolder.exactPrimitive(contents, entries.size() == 0, contents.hasSymbolicKeys(), call); + } + case CLEAR -> { + entries.clear(); + return PlaceHolder.instance; + } + case COPY -> { + Boolean changed = copyInto(kind, contents, entries, args[0], branches); + if (changed == null) { + return lose(kind, contents, call); + } + BRANCHES.get().addAll(branches); + logger.debug("Modelled hash container call {}.{}{} on {}", owner, name, desc, instance); + return kind == Kind.SET + ? ContainerPlaceHolder.exactPrimitive(contents, changed, !branches.isEmpty(), call) + : PlaceHolder.instance; + } + default -> { } + } + // A call by key + Value key = args[0]; + boolean symbolic = contents.hasSymbolicKeys() || key.isSymbolic(); + if (!HashEntries.supported(key) || (symbolic && entries.size() > HashEntries.MAX_ENTRIES)) { + return lose(kind, contents, call); + } + int at = entries.find(key, entries.size(), branches); + if (at == -2) { + return lose(kind, contents, call); + } + HashEntries.Entry found = at >= 0 ? entries.entries().get(at) : null; + Value result = found == null ? null : found.value; + switch (op) { + case GET -> { } + case GET_OR_DEFAULT -> { + if (found == null) result = args[1]; + } + case PUT -> { + if (found != null) { + found.value = args[1]; + } else { + entries.append(key, args[1]); + if (symbolic) contents.addKey(key); + } + if (symbolic) contents.addValue(args[1]); + } + case PUT_IF_ABSENT -> { + if (found == null) { + entries.append(key, args[1]); + if (symbolic) { + contents.addKey(key); + contents.addValue(args[1]); + } + } else if (!(found.value instanceof ObjectValue old)) { + return lose(kind, contents, call); // cannot tell whether the old value is null + } else if (old.isNullValue()) { + found.value = args[1]; // a key mapped to null counts as absent + if (symbolic) contents.addValue(args[1]); + } + } + case REMOVE -> { + if (found != null) entries.remove(at); + } + case ADD -> { + if (found == null) { + entries.append(key, null); + contents.addElement(key); + } + } + case CONTAINS -> { } + default -> throw new IllegalStateException("Not a call by key: " + op); + } + BRANCHES.get().addAll(branches); + logger.debug("Modelled hash container call {}.{}{} on {}: entry {}", owner, name, desc, instance, at); + if (op == HashOp.CONTAINS || (kind == Kind.SET && op == HashOp.REMOVE)) { + return ContainerPlaceHolder.exactPrimitive(contents, found != null, symbolic, call); + } + if (op == HashOp.ADD) { + return ContainerPlaceHolder.exactPrimitive(contents, found == null, symbolic, call); + } + if (!symbolic) { + return null; // concrete keys only: the reference returned is found by its address, as before + } + return ContainerPlaceHolder.exactElement(contents, result, call); + } + + /** + * Adds what the container in {@code arg} holds to exact entries, as {@code putAll}, {@code + * addAll} and the copy constructors do: each key is looked up and stored like a single one. + * The source must have exact contents itself: a map or set with exact entries, or (into a + * set) a list whose order is known. + * + * @return whether an entry was added, or {@code null} if the copy cannot be modelled exactly + * (the entries may then be half updated and must be given up) + */ + private static Boolean copyInto( + Kind kind, ContainerContents contents, HashEntries entries, Value arg, List branches) + throws NotImplementedException { + if (!(arg instanceof ObjectValue ov) || ov.isNullValue() || ov.getContents() == null) { + return null; + } + ContainerContents source = ov.getContents(); + if (source == contents) { + return false; + } + List> keys = new ArrayList<>(); + List> values = new ArrayList<>(); + boolean distinct; // the keys are pairwise different, so each is new or equals an old entry + boolean inOrder; // the keys are listed in the order the source iterates + if (source.hash() != null && source.kind == kind) { + for (HashEntries.Entry e : source.hash().entries()) { + keys.add(e.key); + values.add(e.value); + } + distinct = true; + inOrder = source.hash().iteratesInOrder(); + } else if (kind == Kind.SET && source.keySetOf() != null && source.keySetOf().hash() != null) { + // The key set of a map with exact entries + for (HashEntries.Entry e : source.keySetOf().hash().entries()) { + keys.add(e.key); + values.add(null); + } + distinct = true; + inOrder = source.keySetOf().hash().iteratesInOrder(); + } else if (kind == Kind.SET && source.kind == Kind.LIST && source.exactOrdered() != null) { + for (Value element : source.exactOrdered()) { + keys.add(element); + values.add(null); + } + distinct = false; + inOrder = true; + } else { + return null; + } + boolean symbolic = contents.hasSymbolicKeys(); + for (Value key : keys) { + if (key == null || !HashEntries.supported(key)) { + return null; + } + symbolic |= key.isSymbolic(); + } + if (symbolic && entries.size() + keys.size() > HashEntries.MAX_ENTRIES) { + return null; + } + int before = entries.size(); + int added = 0; + for (int i = 0; i < keys.size(); i++) { + Value key = keys.get(i); + int at = entries.find(key, distinct ? before : entries.size(), branches); + if (at == -2) { + return null; + } + if (at < 0) { + entries.append(key, values.get(i)); + added++; + if (kind == Kind.MAP) { + contents.addKey(key); + } else { + contents.addElement(key); + } + } else if (kind == Kind.MAP) { + entries.entries().get(at).value = values.get(i); + } + if (kind == Kind.MAP) { + contents.addValue(values.get(i)); + } + } + if (added >= 2 && !inOrder) { + entries.forgetOrder(); // a linked container got them in the source's (hash) order + } + if (added >= 2 && source.hashOrderUnknown()) { + entries.taintOrder(); // and that order depends on a symbolic key + } + return added > 0; + } + + /** Gives up the exact entries at a call that is then handled without them. */ + private static Value lose(Kind kind, ContainerContents contents, InvocationEntry call) { + forget(contents, call); + return kind == Kind.SET ? NOT_MODELLED : null; + } + + private static void forget(ContainerContents contents, String owner, String name, String desc, long invokeId) { + if (contents != null) { + forget(contents, new InvocationEntry(owner, name, desc, true, invokeId, true)); + } + } + + /** + * Gives up the exact entries of a hash container (and of those behind a view), because {@code + * call} is not modelled exactly. If one held a symbolic key, nothing on the path says what it + * holds from here on: a context loss. + */ + private static void forget(ContainerContents contents, InvocationEntry call) { + if (contents != null && contents.forgetHash()) { + contextLoss(call, "gives up the exact entries of a hash container with symbolic keys"); + } + } + + /** + * A call that is not modelled got {@code value}: if it is a hash container with exact entries + * (or a view of one), the call may have changed it, so the entries are given up. + */ + public static void escaped(Value value, String owner, String name, String desc, long invokeId) { + if (value instanceof ObjectValue ov && !ov.isNullValue() && ov.getContents() != null) { + forget(ov.getContents(), new InvocationEntry(owner, name, desc, false, invokeId, true)); + } + } + + private static void contextLoss(InvocationEntry call, String why) { + logger.warn("{}.{}{} {}, causes context loss", call.owner(), call.name(), call.desc(), why); + try { + long threadId = Thread.currentThread().getId(); + ThreadHandler.getSymbolicTraceHandler(threadId).recordSymbolicContextLoss(); + ThreadHandler.recordMissingInvocation(threadId, call); + ThreadHandler.recordContextLossInvocation(threadId, call); + } catch (NoThreadContextException e) { + throw new IllegalStateException(e); + } + } + + /** + * Checks the boolean or int a call on exact entries returned against what the entries say. A + * difference means the record is not what the JVM holds: it is given up. + */ + public static void checkPrimitive(ContainerPlaceHolder ph, Object concrete) { + if (!ph.exact || ph.result != Result.PRIMITIVE) { + return; + } + Object expected = ph.expected; + boolean same = expected instanceof Boolean b + ? (concrete instanceof Boolean c ? c.equals(b) : concrete instanceof Number n && (n.intValue() != 0) == b) + : concrete instanceof Number n && expected instanceof Integer i && n.intValue() == i; + if (!same) { + mismatch(ph, concrete); + } + } + + private static void mismatch(ContainerPlaceHolder ph, Object concrete) { + logger.warn("Result of {}.{}{} is {}, the exact entries say {}", + ph.call.owner(), ph.call.name(), ph.call.desc(), concrete, ph.expected); + // Also if the entries were given up since: the call's branches and result relied on them + boolean symbolic = ph.symbolic || ph.contents.hasSymbolicKeys(); + ph.contents.forgetHash(); + if (symbolic) { + contextLoss(ph.call, "returned something else than its exact entries say"); + } + } + + /** + * The shadow of the element the exact entries say a call returned, if the concrete result is + * that element; {@code null} if it is not. + */ + private static Value resolveExact(ContainerPlaceHolder ph, Object concrete, int address, ShadowContext stack) + throws Exception { + boolean isNull = address == ObjectValue.ADDRESS_NULL || concrete == null; + if (ph.expected == null || (ph.expected instanceof ObjectValue n && n.isNullValue())) { + return isNull ? ValueFactory.createNULLValue() : null; + } + if (isNull || !(ph.expected instanceof ObjectValue expected)) { + return null; + } + if (expected instanceof StringValue s) { + if (!(concrete instanceof String) || !concrete.equals(s.concrete)) return null; + } else if (expected instanceof BoxedValue b) { + if (b.getVal() == null || b.getVal().concrete == null || !concrete.equals(b.getVal().concrete)) return null; + } else if ((expected.getAddress() != ObjectValue.ADDRESS_UNKNOWN && expected.getAddress() != address) + || !matches(expected, concrete)) { + return null; + } + if (expected.getAddress() == ObjectValue.ADDRESS_UNKNOWN) { + expected.setAddress(address); + stack.putToHeap(address, expected); + } + return expected; + } + /** Whether calls on this object are modelled as a JDK container's (see {@link #invoke}). */ public static boolean isContainer(ObjectValue instance) { return instance.getContents() != null || kindOf(instance) != null; @@ -211,6 +751,11 @@ private static Kind kindOf(ObjectValue instance) { contents.forgetOrder(); // a list SWAT did not see created: its earlier elements are unknown } } + if (model.op() == Op.INIT && kind == Kind.MAP && instance.className != null + && HASH_CLASSES.contains(instance.className.replace('.', '/'))) { + // A new hash map: its exact entries are known (none), see hashCall + contents.setHash(new HashEntries(instance.className.replace('.', '/').startsWith("java/util/Linked"))); + } trackOrder(model, name + desc, contents); if (kind == Kind.MAP && contents.keysSymbolic(new HashSet<>())) { return null; @@ -221,6 +766,11 @@ private static Kind kindOf(ObjectValue instance) { if (source == null) { return null; } + if (source.hashOrderUnknown()) { + // The copy gets the elements in the source's hash order, whatever the source + // holds later: reading an element of the copy by position is a context loss + contents.markHashOrdered(); + } contents.addSource(source); } if (model.op() == Op.STORE || model.op() == Op.STORE_ELEMENT) { @@ -235,6 +785,13 @@ private static Kind kindOf(ObjectValue instance) { } instance.setContents(contents); InvocationEntry call = new InvocationEntry(owner, name, desc, true, invokeId, true); + boolean readsByPosition = model.op() == Op.ELEMENT || model.op() == Op.STORE_ELEMENT || model.op() == Op.ARRAY; + if (readsByPosition && contents.hashOrderUnknown()) { + // Which element a view, an iterator or a copy of a hash container yields next depends + // on the hash codes of its symbolic keys, which the path does not fix. (How many it + // yields does not: sizes and hasNext are exact, and so is copying all of them.) + contextLoss(call, "depends on the iteration order of a hash container with symbolic keys"); + } logger.debug("Modelled container call {}.{}{} on {}", owner, name, desc, instance); return switch (model.op()) { case INIT, STORE, COPY, PLAIN -> PlaceHolder.instance; @@ -250,8 +807,12 @@ private static Kind kindOf(ObjectValue instance) { } yield new ContainerPlaceHolder(Result.ELEMENT, contents, false, candidates, call); } - case VIEW -> new ContainerPlaceHolder( - Result.VIEW, contents, name.equals("entrySet") || contents.yieldsEntries, List.of(), call); + case VIEW -> { + ContainerPlaceHolder view = new ContainerPlaceHolder( + Result.VIEW, contents, name.equals("entrySet") || contents.yieldsEntries, List.of(), call); + view.keySet = kind == Kind.MAP && name.equals("keySet"); + yield view; + } case ARRAY -> new ContainerPlaceHolder(Result.ARRAY, contents, false, contents.ordered() == null ? List.of() : List.copyOf(contents.ordered()), call); }; @@ -373,6 +934,13 @@ private static ContainerContents sourceOf(Value arg, boolean intoMap) thro int address, ShadowContext stack, SymbolicTraceHandler symbolicTraceHandler) throws Exception { + if (ph.exact && ph.result == Result.ELEMENT) { + Value element = resolveExact(ph, concrete, address, stack); + if (element != null) { + return element; + } + mismatch(ph, concrete); + } if (address == ObjectValue.ADDRESS_NULL || concrete == null) { return ValueFactory.createNULLValue(); } @@ -403,7 +971,11 @@ private static ContainerContents sourceOf(Value arg, boolean intoMap) thro && !(ov instanceof AbstractArrayValue) && !(ov instanceof ObjectArrayValue)) { if (ov.getContents() == null) { - ov.setContents(ContainerContents.viewOf(ph.contents, ph.yieldsEntries)); + ContainerContents view = ContainerContents.viewOf(ph.contents, ph.yieldsEntries); + if (ph.keySet) { + view.setKeySetOf(ph.contents); + } + ov.setContents(view); } else { ov.getContents().addSource(ph.contents); } @@ -452,6 +1024,7 @@ private static ContainerContents sourceOf(Value arg, boolean intoMap) thro stack.putToHeap(address, array); return array; } + default -> { } } return recover(concrete, address, stack); } diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/java/util/ObjectsInvocation.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/java/util/ObjectsInvocation.java index 68d77efa..86861ef4 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/java/util/ObjectsInvocation.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/invoke/java/util/ObjectsInvocation.java @@ -20,6 +20,9 @@ public class ObjectsInvocation { SymbolicTraceHandler symbolicStateHandler) throws NotImplementedException, ValueConversionException, NoThreadContextException { return switch (name) { case "equals" -> invokeEquals(args, desc); + // Objects.hash(Object... values) is Arrays.hashCode(values) + case "hash" -> desc.length == 1 && desc[0].getDescriptor().equals("[Ljava/lang/Object;") + ? ArraysInvocation.invokeStaticMethod("hashCode", args, desc) : PlaceHolder.instance; default -> PlaceHolder.instance; }; } diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/processor/SymbolicInstructionProcessor.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/processor/SymbolicInstructionProcessor.java index 2d2a8bc6..65b53ae7 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/processor/SymbolicInstructionProcessor.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/processor/SymbolicInstructionProcessor.java @@ -18,6 +18,7 @@ import de.uzl.its.swat.symbolic.shadow.Frame; import de.uzl.its.swat.symbolic.shadow.ShadowContext; import de.uzl.its.swat.symbolic.value.Value; +import de.uzl.its.swat.symbolic.value.reference.CapturingLambdaValue; import de.uzl.its.swat.thread.ThreadHandler; import java.util.*; @@ -393,8 +394,9 @@ && adoptLambdaFrame(visitor.getStack(), expectedCnameFull, * body would not match it and be skipped. For a lambda that captures nothing (a static * {@code lambda$...} method taking exactly the interface method's arguments) the generated * class only forwards the arguments, so the frame is replaced by one for the body with the - * same arguments. Capturing lambdas are left alone: their captured values are not on the - * frame. + * same arguments. A lambda with captured arguments is adopted only through the shadow its + * INVOKEDYNAMIC created, which holds them (see adoptCapturingLambdaFrame); otherwise it is + * left alone: its captured values are not on the frame. * * @return whether the frame was replaced */ @@ -409,11 +411,15 @@ private boolean adoptLambdaFrame(ShadowContext stack, String expectedClassFull, } String name = parts[1]; String desc = parts[2]; + Frame expected = stack.getActiveFrame(); + List> locals = expected.getLocals(); + if (!locals.isEmpty() && locals.get(0) instanceof CapturingLambdaValue lambda) { + return adoptCapturingLambdaFrame(stack, lambda, expected, expectedClassFull, actualClassFull, + actualMethodIdentifier, name, desc, logger); + } if (!isStaticMethod(actualClassFull, name, desc)) { return false; } - Frame expected = stack.getActiveFrame(); - List> locals = expected.getLocals(); int argSlots = 0; for (org.objectweb.asm.Type t : org.objectweb.asm.Type.getArgumentTypes(desc)) { argSlots += t.getSize(); @@ -442,6 +448,51 @@ private boolean adoptLambdaFrame(ShadowContext stack, String expectedClassFull, return true; } + /** + * Lets the body of a lambda with captured arguments run as the interface method called on it. + * The receiver's shadow was created by the lambda's INVOKEDYNAMIC and names the + * implementation method; the body is adopted only if it is that method, called through that + * interface method, with the captured types followed by the interface arguments as its exact + * parameters (for an instance method, the receiver is the first captured value). Its frame + * then holds the captured values followed by the call's arguments, as LambdaMetafactory passes + * them. Anything else stays untracked (a context loss). + */ + private boolean adoptCapturingLambdaFrame(ShadowContext stack, CapturingLambdaValue lambda, Frame expected, + String expectedClassFull, String actualClassFull, String actualMethodIdentifier, String name, + String desc, Logger logger) { + if (!lambda.runsExactly(expected.getMethodName(), actualClassFull, name, desc)) { + logger.info("Not running {} as {}:{}: the call does not match its lambda {}", actualMethodIdentifier, + expectedClassFull, expected.getMethodName(), lambda); + return false; + } + List> locals = expected.getLocals(); + int argSlots = 0; + for (org.objectweb.asm.Type t : lambda.instantiatedType.getArgumentTypes()) { + argSlots += t.getSize(); + } + if (locals.size() - 1 != argSlots + || lambda.instantiatedType.getReturnType().getSize() != expected.nReturnWords) { + return false; + } + Frame body = new Frame(actualClassFull, name, expected.nReturnWords); + body.setCallSite(expected.getCallSite()); + for (int i = 0; i < lambda.captured.length; i++) { + if (lambda.capturedTypes[i].getSize() == 2) { + body.addLocal2(lambda.captured[i]); + } else { + body.addLocal(lambda.captured[i]); + } + } + for (Value local : locals.subList(1, locals.size())) { + body.addLocal(local); + } + stack.popFrame(); + stack.pushFrame(body); + logger.info("Running lambda body {} with {} captured value(s) as {}:{}", actualMethodIdentifier, + lambda.captured.length, expectedClassFull, expected.getMethodName()); + return true; + } + /** * Whether {@code receiver} is an object of a class the JVM generated for a lambda in {@code * classFull} (named {@code $$Lambda...}). diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/ContainerPlaceHolder.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/ContainerPlaceHolder.java index 258d66f9..4669455f 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/ContainerPlaceHolder.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/ContainerPlaceHolder.java @@ -17,7 +17,9 @@ public enum Result { /** A view, iterator, enumeration or entry backed by {@link #contents}. */ VIEW, /** A new object array holding elements of {@link #contents} ({@code toArray()}). */ - ARRAY + ARRAY, + /** A boolean or int the exact entries of {@link #contents} determine ({@link #expected}). */ + PRIMITIVE } public final Result result; @@ -28,6 +30,17 @@ public enum Result { public final List> candidates; /** The call, recorded as the culprit if the result cannot be matched to a shadow. */ public final InvocationEntry call; + /** + * Whether the exact entries of a hash container say what the call returns: {@link #expected} + * (for an element: its shadow, null for Java's null; for a primitive: a Boolean or Integer). + * The concrete result is checked against it, see {@code CollectionInvocation}. + */ + public final boolean exact; + public final Object expected; + /** Whether an exact result was decided by comparing symbolic keys (branches on the path). */ + public final boolean symbolic; + /** Whether a returned view is the key set of the map {@link #contents} ({@code Map.keySet()}). */ + public boolean keySet = false; public ContainerPlaceHolder( Result result, @@ -35,12 +48,38 @@ public ContainerPlaceHolder( boolean yieldsEntries, List> candidates, InvocationEntry call) { + this(result, contents, yieldsEntries, candidates, call, false, null, false); + } + + private ContainerPlaceHolder( + Result result, + ContainerContents contents, + boolean yieldsEntries, + List> candidates, + InvocationEntry call, + boolean exact, + Object expected, + boolean symbolic) { super(false); this.result = result; this.contents = contents; this.yieldsEntries = yieldsEntries; this.candidates = candidates; this.call = call; + this.exact = exact; + this.expected = expected; + this.symbolic = symbolic; + } + + /** The element (a shadow, or null for Java's null) the exact entries say the call returns. */ + public static ContainerPlaceHolder exactElement(ContainerContents contents, Value expected, InvocationEntry call) { + return new ContainerPlaceHolder(Result.ELEMENT, contents, false, List.of(), call, true, expected, true); + } + + /** The boolean or int the exact entries say the call returns. */ + public static ContainerPlaceHolder exactPrimitive( + ContainerContents contents, Object expected, boolean symbolic, InvocationEntry call) { + return new ContainerPlaceHolder(Result.PRIMITIVE, contents, false, List.of(), call, true, expected, symbolic); } @Override diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/ValueFactory.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/ValueFactory.java index f048803a..d262d8a0 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/ValueFactory.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/ValueFactory.java @@ -16,6 +16,7 @@ import de.uzl.its.swat.symbolic.value.reference.StringBuilderValue; import de.uzl.its.swat.symbolic.value.reference.array.*; import de.uzl.its.swat.symbolic.value.reference.lang.*; +import de.uzl.its.swat.symbolic.value.reference.regex.PatternValue; import de.uzl.its.swat.thread.ThreadHandler; import java.util.*; @@ -151,6 +152,9 @@ public static StringValue createStringValue(String concrete, int address) throws return createFloatArrayValue(arr, address); } else if (concrete instanceof double[] arr) { return createDoubleArrayValue(arr, address); + } else if (concrete instanceof java.util.regex.Pattern pattern) { + // Immutable: its regular expression and flags are read from the concrete Pattern. + return new PatternValue(context, pattern, address); } else { Class cls = concrete.getClass(); String className = cls.getName(); @@ -180,27 +184,35 @@ public static StringValue createStringValue(String concrete, int address) throws if (className.equals(Util.formatClassName(Boolean.class.getName())) || classInterfacesAndParents.contains(Util.formatClassName(Boolean.class.getName()))) { ret = new BooleanObjectValue(context, new BooleanValue(context, false), ObjectValue.ADDRESS_UNKNOWN); + ret.valueUnknown = true; // until the constructor's model sets the value } else if (className.equals(Util.formatClassName(Byte.class.getName())) || classInterfacesAndParents.contains(Util.formatClassName(Byte.class.getName()))) { ret = new ByteObjectValue(context, new ByteValue(context, (byte) 0), ObjectValue.ADDRESS_UNKNOWN); + ret.valueUnknown = true; // until the constructor's model sets the value } else if (className.equals(Util.formatClassName(Integer.class.getName())) || classInterfacesAndParents.contains(Util.formatClassName(Integer.class.getName()))) { ret = new IntegerObjectValue(context, new IntValue(context, 0), ObjectValue.ADDRESS_UNKNOWN); + ret.valueUnknown = true; // until the constructor's model sets the value } else if (className.equals(Util.formatClassName(Short.class.getName())) || classInterfacesAndParents.contains(Util.formatClassName(Short.class.getName()))) { ret = new ShortObjectValue(context, new ShortValue(context, (short) 0), ObjectValue.ADDRESS_UNKNOWN); + ret.valueUnknown = true; // until the constructor's model sets the value } else if (className.equals(Util.formatClassName(Long.class.getName())) || classInterfacesAndParents.contains(Util.formatClassName(Long.class.getName()))) { ret = new LongObjectValue(context, new LongValue(context, 0L), ObjectValue.ADDRESS_UNKNOWN); + ret.valueUnknown = true; // until the constructor's model sets the value } else if (className.equals(Util.formatClassName(Double.class.getName())) || classInterfacesAndParents.contains(Util.formatClassName(Double.class.getName()))) { ret = new DoubleObjectValue(context, new DoubleValue(context, 0.0d), ObjectValue.ADDRESS_UNKNOWN); + ret.valueUnknown = true; // until the constructor's model sets the value } else if (className.equals(Util.formatClassName(Float.class.getName())) || classInterfacesAndParents.contains(Util.formatClassName(Float.class.getName()))) { ret = new FloatObjectValue(context, new FloatValue(context, 0.0f), ObjectValue.ADDRESS_UNKNOWN); + ret.valueUnknown = true; // until the constructor's model sets the value } else if (className.equals(Util.formatClassName(Character.class.getName())) || classInterfacesAndParents.contains(Util.formatClassName(Character.class.getName()))) { ret = new CharacterObjectValue(context, new CharValue(context, '\u0000'), ObjectValue.ADDRESS_UNKNOWN); + ret.valueUnknown = true; // until the constructor's model sets the value } else if (className.equals(Util.formatClassName(StringBuilder.class.getName())) || className.equals(Util.formatClassName(StringBuffer.class.getName()))) { // The chars are set by the constructor, which is the next call. @@ -208,6 +220,7 @@ public static StringValue createStringValue(String concrete, int address) throws } else if (className.equals(Util.formatClassName(String.class.getName())) || classInterfacesAndParents.contains(Util.formatClassName(String.class.getName()))) { ret = new StringValue(context, "", ObjectValue.ADDRESS_UNKNOWN); // This is not correct but _normally_ the next call should be the function + ret.valueUnknown = true; } else { ret = new ObjectValue<>( diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/CapturingLambdaValue.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/CapturingLambdaValue.java new file mode 100644 index 00000000..e3218533 --- /dev/null +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/CapturingLambdaValue.java @@ -0,0 +1,144 @@ +package de.uzl.its.swat.symbolic.value.reference; + +import de.uzl.its.swat.common.Util; +import de.uzl.its.swat.symbolic.value.Value; +import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.IntValue; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.objectweb.asm.Type; +import org.sosy_lab.java_smt.api.SolverContext; + +/** + * The shadow of a lambda object that {@code LambdaMetafactory} created with captured arguments + * for an implementation method in instrumented code. + * + *

The captured values are the object's fields (the generated class stores them as {@code + * arg$1..arg$n}), so the object is symbolic exactly when a captured value is: passing it to a + * call SWAT does not model is a context loss, as for any object holding a symbolic value. When an + * interface call on it runs the implementation method, the body's frame is the captured values + * followed by the call's arguments (see {@code SymbolicInstructionProcessor.adoptLambdaFrame}). + */ +public class CapturingLambdaValue extends ObjectValue { + + /** The bootstrap arguments, as instrumented: {@code [samType, implHandle, instantiatedType, ...]}. */ + private static final Pattern HANDLE = + Pattern.compile("^(\\S+)\\.([^.(\\s]+)(\\([^)\\s]*\\)\\S+) \\((\\d+)( itf)?\\)$"); + + public static final int H_INVOKEVIRTUAL = 5; + public static final int H_INVOKESTATIC = 6; + public static final int H_INVOKESPECIAL = 7; + public static final int H_INVOKEINTERFACE = 9; + + /** The interface method the lambda implements. */ + public final String samName; + public final String implOwner; + public final String implName; + public final String implDesc; + public final int implTag; + /** The interface method's type as the lambda instantiates it (generic types filled in). */ + public final Type instantiatedType; + public final Type[] capturedTypes; + public final Value[] captured; + + private CapturingLambdaValue(SolverContext context, IntValue nFields, String samName, + String implOwner, String implName, String implDesc, int implTag, Type instantiatedType, + Type[] capturedTypes, Value[] captured) { + super(context, null, nFields, ADDRESS_UNKNOWN); + this.samName = samName; + this.implOwner = implOwner; + this.implName = implName; + this.implDesc = implDesc; + this.implTag = implTag; + this.instantiatedType = instantiatedType; + this.capturedTypes = capturedTypes; + this.captured = captured; + Value[] fields = getFields(); + System.arraycopy(captured, 0, fields, 0, captured.length); + } + + /** + * The shadow for an {@code invokedynamic} through {@code LambdaMetafactory}, or null if this + * is not one this class models: nothing captured, an implementation outside instrumented code + * (e.g. a method reference to a JDK method), a handle kind other than a method call, or + * bootstrap arguments it cannot read. + * + * @param samName the interface method's name (the {@code invokedynamic}'s name) + * @param indyDesc the {@code invokedynamic}'s descriptor: captured types, then the interface + * @param bsmArgs the bootstrap arguments as instrumented ({@code Arrays.toString}) + */ + public static CapturingLambdaValue create(SolverContext context, IntValue nFields, String samName, + String indyDesc, String bsmArgs, Value[] captured) { + Type[] capturedTypes = Type.getArgumentTypes(indyDesc); + if (capturedTypes.length == 0 || captured.length != capturedTypes.length + || nFields.concrete != captured.length || bsmArgs == null + || !bsmArgs.startsWith("[") || !bsmArgs.endsWith("]")) { + return null; + } + String[] parts = bsmArgs.substring(1, bsmArgs.length() - 1).split(", "); + if (parts.length < 3) { + return null; + } + Matcher m = HANDLE.matcher(parts[1]); + if (!m.matches()) { + return null; + } + int tag = Integer.parseInt(m.group(4)); + if (tag != H_INVOKEVIRTUAL && tag != H_INVOKESTATIC && tag != H_INVOKESPECIAL + && tag != H_INVOKEINTERFACE) { + return null; + } + String owner = m.group(1); + if (!Util.shouldInstrument(owner)) { + return null; + } + Type instantiated; + try { + instantiated = Type.getMethodType(parts[2]); + Type.getArgumentTypes(m.group(3)); + } catch (RuntimeException e) { + return null; + } + for (Value v : captured) { + if (v == null) { + return null; + } + } + return new CapturingLambdaValue(context, nFields, samName, Util.formatClassName(owner), + m.group(2), m.group(3), tag, instantiated, capturedTypes, captured.clone()); + } + + /** + * Whether calling {@code samName} on this lambda runs {@code owner.name desc} as LambdaMetafactory + * would without any adaptation: the implementation's parameters (with the receiver first for + * an instance method) are exactly the captured types followed by the instantiated interface + * parameters, and it returns exactly the instantiated return type. Adaptations (unboxing, + * widening, a dropped return value, a receiver of a subtype) are not modelled. + */ + public boolean runsExactly(String calledName, String owner, String name, String desc) { + if (!samName.equals(calledName) || !implOwner.equals(Util.formatClassName(owner)) + || !implName.equals(name) || !implDesc.equals(desc)) { + return false; + } + Type[] implParams = Type.getArgumentTypes(implDesc); + Type[] instParams = instantiatedType.getArgumentTypes(); + int receiver = implTag == H_INVOKESTATIC ? 0 : 1; + if (receiver + implParams.length != capturedTypes.length + instParams.length) { + return false; + } + for (int i = 0; i < capturedTypes.length + instParams.length; i++) { + Type expected = i < capturedTypes.length ? capturedTypes[i] : instParams[i - capturedTypes.length]; + Type actual = i < receiver ? Type.getObjectType(implOwner) : implParams[i - receiver]; + if (!expected.equals(actual)) { + return false; + } + } + return Type.getReturnType(implDesc).equals(instantiatedType.getReturnType()); + } + + @Override + public String toString() { + return "CapturingLambdaValue[" + this.internalID + "] @" + Integer.toHexString(address) + + " { " + samName + " -> " + implOwner + "." + implName + implDesc + ", captured=" + + captured.length + ", className=" + className + "}"; + } +} diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/ContainerContents.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/ContainerContents.java index f0ab4d5b..62e78c42 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/ContainerContents.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/ContainerContents.java @@ -40,6 +40,8 @@ public enum Kind { LIST, /** A hash map: the keys are hashed and compared, the values are only stored and returned. */ MAP, + /** A hash set, modelled only while its exact elements are known ({@link HashEntries}). */ + SET, /** A view, iterator, enumeration or map entry, or an immutable container: only read. */ VIEW } @@ -70,6 +72,28 @@ public enum Kind { * anything else changed the list (see CollectionInvocation). */ private List> ordered; + /** Whether {@link #ordered} is exact: no call that is not modelled got the list since. */ + private boolean orderExact = true; + + /** + * For a hash map or set: its exact entries, or null if they are not known (the container was + * not seen created, or a call on it was not modelled exactly, see CollectionInvocation). + */ + private HashEntries hash; + /** + * Whether a symbolic String or boxed key was stored (maps), or a symbolic element (sets). Such + * a container is modelled only through its exact entries; once those are lost, every call on + * it is a context loss. + */ + private boolean symbolicKeys = false; + /** + * Whether the container was filled from a hash container whose iteration order was unknown + * then (a list copied from a HashSet with symbolic elements): the order of its own elements + * is unknown for good. + */ + private boolean hashOrdered = false; + /** For a view: the map whose key set it is ({@code Map.keySet()}), see {@link #keySetOf()}. */ + private ContainerContents keySetOf; public ContainerContents(Kind kind) { this(kind, false); @@ -97,14 +121,124 @@ public void appendOrdered(Value value) { public void clearOrdered() { if (kind == Kind.LIST) { ordered = new ArrayList<>(); + orderExact = true; } } + /** + * The elements of the list in order, if they are known exactly: as {@link #ordered}, but null + * also once a call that is not modelled may have changed the list. + */ + public List> exactOrdered() { + return orderExact ? ordered : null; + } + /** The list changed in a way not tracked in order: positions are unknown from now on. */ public void forgetOrder() { ordered = null; } + /** The exact entries of a hash container, or null if they are not known. */ + public HashEntries hash() { + return hash; + } + + public void setHash(HashEntries hash) { + this.hash = hash; + } + + /** Whether a symbolic key or set element was ever stored in this container itself. */ + public boolean hasSymbolicKeys() { + return symbolicKeys; + } + + /** Records an element added to a hash set: a value of the container that is compared like a key. */ + public void addElement(Value element) throws NotImplementedException { + if (element == null) { + return; + } + addValue(element); + if (element.isSymbolic()) { + symbolicKeys = true; + } + } + + /** + * Gives up the exact entries of this container, and of the containers a view shows (a call + * that is not modelled may have changed them through the view). + * + * @return whether one of them holds (or held) a symbolic key: then nothing constrains what the + * container holds any more, which is a context loss + */ + public boolean forgetHash() { + return forgetHash(Collections.newSetFromMap(new IdentityHashMap<>())); + } + + private boolean forgetHash(Set seen) { + if (!seen.add(this)) { + return false; + } + boolean symbolic = hash != null && symbolicKeys; + hash = null; + orderExact = false; + if (kind == Kind.VIEW) { + for (Object s : sources) { + if (s instanceof ContainerContents c) symbolic |= c.forgetHash(seen); + } + } + return symbolic; + } + + /** + * Whether the order in which this container, or one it shows or was copied from, iterates may + * depend on a symbolic key (see {@link HashEntries#orderUnknown}), or its exact entries were + * lost while it held one. + */ + public boolean hashOrderUnknown() throws NotImplementedException { + return hashOrderUnknown(Collections.newSetFromMap(new IdentityHashMap<>())); + } + + private boolean hashOrderUnknown(Set seen) throws NotImplementedException { + if (!seen.add(this)) { + return false; + } + if (hashOrdered || (symbolicKeys && (hash == null || hash.orderUnknown()))) { + return true; + } + for (Object s : sources) { + if (s instanceof ContainerContents c && c.hashOrderUnknown(seen)) return true; + } + return false; + } + + /** + * For the contents of a view: the map it is the key set of, if it shows nothing else. + */ + public ContainerContents keySetOf() { + return keySetOf != null && sources.size() == 1 && sources.get(0) == keySetOf ? keySetOf : null; + } + + /** Marks a view of {@code map} (its only source) as the map's key set. */ + public void setKeySetOf(ContainerContents map) { + keySetOf = map; + } + + /** Records that the elements were copied in an iteration order that is unknown (see {@link #hashOrdered}). */ + public void markHashOrdered() { + hashOrdered = true; + } + + /** Whether a container among the sources (transitively) ever stored a symbolic key. */ + private boolean sourceHasSymbolicKeys(Set seen) { + if (!seen.add(this)) { + return false; + } + for (Object s : sources) { + if (s instanceof ContainerContents c && (c.symbolicKeys || c.sourceHasSymbolicKeys(seen))) return true; + } + return false; + } + /** A view of {@code source}: a read-only container that holds what the source holds. */ public static ContainerContents viewOf(ContainerContents source, boolean yieldsEntries) { ContainerContents view = new ContainerContents(Kind.VIEW, yieldsEntries); @@ -130,9 +264,13 @@ public void addKey(Value key) throws NotImplementedException { if (key == null) { return; } - // A String or boxed key was concrete when stored and stays so; only mutable keys are rechecked + // A String or boxed key is concrete or symbolic for good; only mutable keys are rechecked. + // A symbolic one is stored only by the exact model of a hash map (HashEntries). if (!isValueTyped(key)) { keys.add(key); + } else if (key.isSymbolic()) { + symbolicKeys = true; + symbolicImmutable = true; } index(key); } @@ -172,12 +310,19 @@ private boolean isSymbolic(Set> visited, Set seen) throws No return false; } - /** Whether a map key stored here (not in the sources) has become symbolic. */ + /** + * Whether the map may hold a symbolic key that its exact entries do not account for: a mutable + * key stored here that has become symbolic, a symbolic String or boxed key when the exact + * entries are lost, or one in a map this one was copied from as a source. + */ public boolean keysSymbolic(Set> visited) throws NotImplementedException { for (Value k : keys) { if (k.isSymbolic(visited)) return true; } - return false; + if (symbolicKeys && hash == null) { + return true; + } + return kind == Kind.MAP && sourceHasSymbolicKeys(Collections.newSetFromMap(new IdentityHashMap<>())); } /** diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/HashEntries.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/HashEntries.java new file mode 100644 index 00000000..fcdc0e4b --- /dev/null +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/HashEntries.java @@ -0,0 +1,217 @@ +package de.uzl.its.swat.symbolic.value.reference; + +import de.uzl.its.swat.common.exceptions.NotImplementedException; +import de.uzl.its.swat.symbolic.invoke.java.lang.StringCallBranches.Branch; +import de.uzl.its.swat.symbolic.value.Value; +import de.uzl.its.swat.symbolic.value.primitive.numeric.NumericalValue; +import de.uzl.its.swat.symbolic.value.reference.lang.BooleanObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.BoxedValue; +import de.uzl.its.swat.symbolic.value.reference.lang.ByteObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.CharacterObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.IntegerObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.LongObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.ShortObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.StringValue; +import java.util.ArrayList; +import java.util.List; +import org.sosy_lab.java_smt.api.BitvectorFormula; +import org.sosy_lab.java_smt.api.BitvectorFormulaManager; +import org.sosy_lab.java_smt.api.BooleanFormula; +import org.sosy_lab.java_smt.api.BooleanFormulaManager; +import org.sosy_lab.java_smt.api.FormulaManager; +import org.sosy_lab.java_smt.api.SolverContext; +import org.sosy_lab.java_smt.api.StringFormula; + +/** + * The exact entries of a {@code HashMap}, {@code LinkedHashMap}, {@code ConcurrentHashMap}, {@code + * HashSet} or {@code LinkedHashSet} that SWAT saw created and has modelled every call on since: the key shadows + * (symbolic or concrete) in insertion order, each with the shadow of its value (maps). + * + *

A lookup is an equality chain: the key is compared with the stored keys in order, as {@code + * key.equals(k_j)}, up to the first one it equals. Each comparison is a branch of the library call + * ({@link Branch}), which holds or not as the concrete values say. The stored keys are pairwise + * different (concretely, and by the branches of their insertions on the path), so the chain fixes + * which entry the JVM found, and the solver takes the other sides to reach the other entries. + * + *

Only keys whose {@code equals} is exact and free of side effects are compared: {@code null}, + * String, and Integer, Long, Short, Byte, Character and Boolean, where two keys of different + * classes are never equal ({@link #supported}). Anything else ends the exact record, see {@code + * CollectionInvocation}. + * + *

The order of the entries is the iteration order of a linked container only; that of a plain + * hash container depends on the keys' hash codes, which are not modelled ({@link #orderUnknown}). + */ +public final class HashEntries { + + /** Lookups in larger containers are not modelled: each is a chain of that many branches. */ + public static final int MAX_ENTRIES = 64; + + /** A key with the value stored under it ({@code null} in a set). */ + public static final class Entry { + public final Value key; + public Value value; + + Entry(Value key, Value value) { + this.key = key; + this.value = value; + } + } + + /** Whether iteration follows insertion order (LinkedHashMap, LinkedHashSet). */ + public final boolean linked; + + /** For a linked container: whether {@link #entries} is in iteration order. */ + private boolean orderKnown = true; + + /** + * Whether a symbolic key was stored since the container was last empty by {@code clear()}. + * It may have shaped the table (which bin a key is in, a bin's order once it is a tree) for + * the keys that came after it, also when it is removed again. + */ + private boolean symbolicSinceClear = false; + + private final List entries = new ArrayList<>(); + + public HashEntries(boolean linked) { + this.linked = linked; + } + + public int size() { + return entries.size(); + } + + public List entries() { + return entries; + } + + public void clear() { + entries.clear(); + orderKnown = true; + symbolicSinceClear = false; + } + + public void append(Value key, Value value) throws NotImplementedException { + entries.add(new Entry(key, value)); + symbolicSinceClear |= key.isSymbolic(); + } + + public Entry remove(int index) { + return entries.remove(index); + } + + /** The entries were added in an order that is not the iteration order. */ + public void forgetOrder() { + orderKnown = false; + } + + /** The entries were added in an order that may depend on a symbolic key (a copy in hash order). */ + public void taintOrder() { + symbolicSinceClear = true; + } + + /** + * Whether the iteration order may depend on a symbolic key in a way the path does not fix: + * two or more entries in a container that does not iterate in the order recorded here and + * that has held a symbolic key since it was last cleared. + */ + public boolean orderUnknown() { + return entries.size() >= 2 && (!linked || !orderKnown) && symbolicSinceClear; + } + + /** Whether {@link #entries} is the iteration order whatever the keys are. */ + public boolean iteratesInOrder() { + return entries.size() < 2 || (linked && orderKnown); + } + + /** + * The index of the entry {@code key} equals, or -1 if there is none, comparing with the first + * {@code limit} entries. The comparisons made are added to {@code branches}. + * + * @return the index, -1 for no match, or -2 if a comparison is not supported + */ + public int find(Value key, int limit, List branches) { + for (int i = 0; i < limit && i < entries.size(); i++) { + Branch eq = equal(key, entries.get(i).key); + if (eq == null) { + return -2; + } + branches.add(eq); + if (eq.holds()) { + return i; + } + } + return -1; + } + + /** + * Whether the value can be a key of an exact record: {@code null}, a String or a boxed integral + * value (or Boolean) with a known concrete value. Float and Double are left out (their equals + * compares bits, not values), as is every other class (its equals is unknown), and a shadow + * whose value is not known to be the object's ({@link ObjectValue#valueUnknown}): the chain + * compares shadows, so a wrong one would put a wrong constraint on the path. + */ + public static boolean supported(Value key) { + if (key instanceof StringValue s) { + return !s.valueUnknown && s.concrete != null && s.formula != null && !s.isNullValue(); + } + if (key instanceof BoxedValue b) { + return !b.valueUnknown && boxClass(b) != null && b.getVal() != null && b.getVal().concrete != null + && b.getVal().formula != null && !b.isNullValue(); + } + return isNull(key); + } + + private static boolean isNull(Value key) { + return key != null && key.getClass() == ObjectValue.class && ((ObjectValue) key).isNullValue(); + } + + private static Class boxClass(BoxedValue b) { + Class c = b.getClass(); + return c == IntegerObjectValue.class || c == LongObjectValue.class || c == ShortObjectValue.class + || c == ByteObjectValue.class || c == CharacterObjectValue.class || c == BooleanObjectValue.class + ? c : null; + } + + /** + * {@code a.equals(b)} for two supported keys (also when one is null, as HashMap compares + * them): the condition, and whether it holds concretely. {@code null} if the keys cannot be + * compared after all. + */ + public static Branch equal(Value a, Value b) { + if (!supported(a) || !supported(b)) { + return null; + } + SolverContext context = ((ObjectValue) a).context; + FormulaManager fmgr = context.getFormulaManager(); + BooleanFormulaManager bmgr = fmgr.getBooleanFormulaManager(); + if (isNull(a) || isNull(b)) { + boolean both = isNull(a) && isNull(b); + return new Branch(bmgr.makeBoolean(both), both, false); + } + if (a instanceof StringValue x && b instanceof StringValue y) { + return new Branch(fmgr.getStringFormulaManager().equal((StringFormula) x.formula, (StringFormula) y.formula), + x.concrete.equals(y.concrete), false); + } + if (a instanceof BoxedValue x && b instanceof BoxedValue y && a.getClass() == b.getClass()) { + NumericalValue u = x.getVal(); + NumericalValue v = y.getVal(); + boolean holds = u.concrete.equals(v.concrete); + if (a.getClass() == BooleanObjectValue.class) { + if (!(u.formula instanceof BooleanFormula f) || !(v.formula instanceof BooleanFormula g)) { + return null; + } + return new Branch(bmgr.equivalence(f, g), holds, false); + } + if (!(u.formula instanceof BitvectorFormula f) || !(v.formula instanceof BitvectorFormula g)) { + return null; + } + BitvectorFormulaManager bvmgr = fmgr.getBitvectorFormulaManager(); + if (bvmgr.getLength(f) != bvmgr.getLength(g)) { + return null; + } + return new Branch(bvmgr.equal(f, g), holds, false); + } + // Keys of different classes (a String and an Integer, an Integer and a Long): never equal + return new Branch(bmgr.makeFalse(), false, false); + } +} diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/ObjectValue.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/ObjectValue.java index 0842cded..c6a759e5 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/ObjectValue.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/ObjectValue.java @@ -41,6 +41,13 @@ public class ObjectValue extends Value { // For uninstrumented JDK containers: the shadow of what modelled calls stored in them. @Setter @Getter private ContainerContents contents; @Getter private IntValue nFields; + /** + * For a String or boxed value: whether its shadow value is not known to be that of the object + * (created by NEW with a default value, and no model of a constructor has set it since; or a + * modelled result that differs from the object the JVM returned). Such a shadow is no key for + * the exact model of hash containers (HashEntries), which compares keys by their shadows. + */ + public boolean valueUnknown = false; private BooleanFormulaManager bfm; diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/StringBuilderValue.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/StringBuilderValue.java index 1ef22753..ef978cc6 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/StringBuilderValue.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/StringBuilderValue.java @@ -7,6 +7,7 @@ import de.uzl.its.swat.symbolic.value.PlaceHolder; import de.uzl.its.swat.symbolic.value.Value; import de.uzl.its.swat.symbolic.value.VoidValue; +import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.BooleanValue; import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.CharValue; import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.IntValue; import de.uzl.its.swat.symbolic.value.reference.array.CharArrayValue; @@ -132,6 +133,11 @@ public void resync(String chars) { */ @Override public Value invokeMethod(String name, Type[] desc, Value[] args) throws NotImplementedException, ValueConversionException { + if (name.equals("equals") && args.length == 1 && args[0] instanceof ObjectValue other) { + // StringBuilder and StringBuffer (final) do not override Object.equals: identity, + // whatever the chars. References are concrete; a diverging shadow shows at GETVALUE. + return new BooleanValue(context, this == other); + } if ((unknown || stringValue == null) && !name.equals("")) return PlaceHolder.instance; Value result; try { diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/BooleanObjectValue.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/BooleanObjectValue.java index 9c1e25ff..062101a4 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/BooleanObjectValue.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/BooleanObjectValue.java @@ -97,6 +97,13 @@ public String MAKE_SYMBOLIC() throws NullPointerException { this.val = new BooleanValue(context, bv.concrete, bv.formula); return VoidValue.instance; } + // Boolean(boolean) with the argument loaded as an int (ILOAD of a boolean local): as + // Boolean.valueOf(boolean), the int is 0 or 1 + if (desc.length == 1 && desc[0].equals(Type.BOOLEAN_TYPE) && args[0] instanceof IntValue iv + && (iv.concrete == 0 || iv.concrete == 1)) { + this.val = iv.asBooleanValue(); + return VoidValue.instance; + } return PlaceHolder.instance; } diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/ByteObjectValue.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/ByteObjectValue.java index 835e4dac..637e3b07 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/ByteObjectValue.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/ByteObjectValue.java @@ -118,6 +118,12 @@ public String MAKE_SYMBOLIC() throws NullPointerException { this.val = new ByteValue(context, bv.concrete, bv.formula); return VoidValue.instance; } + // A constant or an int-typed expression: the JVM passes it as an int (BIPUSH, ICONST, ...). + // Without this the shadow kept its default value, whatever the object holds. + if (args[0] instanceof IntValue iv) { + this.val = iv.I2B(); + return VoidValue.instance; + } return PlaceHolder.instance; } diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/CharacterObjectValue.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/CharacterObjectValue.java index 44cf1955..df16f9e5 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/CharacterObjectValue.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/CharacterObjectValue.java @@ -151,6 +151,12 @@ public int hashCode() { this.val = new CharValue(context, cv.concrete, cv.formula); return VoidValue.instance; } + // A constant or an int-typed expression: the JVM passes it as an int (BIPUSH, ICONST, ...). + // Without this the shadow kept its default value, whatever the object holds. + if (args[0] instanceof IntValue iv) { + this.val = iv.I2C(); + return VoidValue.instance; + } return PlaceHolder.instance; } diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/ShortObjectValue.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/ShortObjectValue.java index 90494bc0..4cd04b5b 100644 --- a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/ShortObjectValue.java +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/lang/ShortObjectValue.java @@ -110,6 +110,12 @@ public String MAKE_SYMBOLIC() throws NullPointerException { this.val = new ShortValue(context, sv.concrete, sv.formula); return VoidValue.instance; } + // A constant or an int-typed expression: the JVM passes it as an int (BIPUSH, ICONST, ...). + // Without this the shadow kept its default value, whatever the object holds. + if (args[0] instanceof IntValue iv) { + this.val = iv.I2S(); + return VoidValue.instance; + } return PlaceHolder.instance; } diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/regex/MatcherValue.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/regex/MatcherValue.java new file mode 100644 index 00000000..2202cce2 --- /dev/null +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/regex/MatcherValue.java @@ -0,0 +1,97 @@ +package de.uzl.its.swat.symbolic.value.reference.regex; + +import com.google.common.collect.ImmutableSet; +import de.uzl.its.swat.common.exceptions.NotImplementedException; +import de.uzl.its.swat.common.exceptions.ValueConversionException; +import de.uzl.its.swat.symbolic.value.PlaceHolder; +import de.uzl.its.swat.symbolic.value.Value; +import de.uzl.its.swat.symbolic.value.reference.ObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.StringValue; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.objectweb.asm.Type; +import org.sosy_lab.java_smt.api.SolverContext; + +/** + * Shadow of a Matcher + * that {@link PatternValue} made for a symbolic String: the pattern (no flags, translatable by + * JavaRegex) and the input (an immutable String). + * + *

Only {@code matches()} is modelled, as {@code input.matches(regex)}, which is what + * String.matches does. It depends on the input, the region and the pattern only, not on an + * earlier match. Any other call (find, group, reset, region, usePattern, ...) is not modelled: it + * returns a PlaceHolder, which records the context loss (the matcher is symbolic), and from then + * on matches() is not modelled either, since the call may have changed the input, region or + * pattern. Nor is it before the shadow is bound to the JVM's Matcher (see {@link #bind}). + */ +public final class MatcherValue extends ObjectValue { + private static final String MATCHER = "java/util/regex/Matcher"; + private static final Type[] MATCHES_DESC = {Type.getType(String.class)}; + + private final Pattern pattern; + private final StringValue input; + + /** Whether a call SWAT does not model may have changed the matcher. */ + private boolean unknown = false; + + /** The concrete Matcher this shadows, once the JVM returned it. */ + private Matcher matcher; + + public MatcherValue(SolverContext context, Pattern pattern, StringValue input) { + super(context, ObjectValue.ADDRESS_UNKNOWN); + this.className = MATCHER; + this.pattern = pattern; + this.input = input; + } + + /** + * Binds this shadow to the Matcher the JVM returned for it. If that is not a matcher of the + * same pattern (it always should be), the matcher is not modelled. + */ + public void bind(Object concrete) { + if (matcher == null && concrete instanceof Matcher m && m.pattern() == pattern) { + matcher = m; + } else { + unknown = true; + } + } + + /** Whether this shadows the concrete object {@code concrete}. */ + public boolean shadows(Object concrete) { + return matcher != null && matcher == concrete; + } + + @Override + public Value invokeMethod(String name, Type[] desc, Value[] args) + throws NotImplementedException, ValueConversionException { + if (!unknown && matcher != null && name.equals("matches") && args.length == 0) { + return input.invokeMethod("matches", MATCHES_DESC, + new Value[] {new StringValue(context, pattern.pattern(), ObjectValue.ADDRESS_UNKNOWN)}); + } + unknown = true; + return PlaceHolder.instance; + } + + @Override + public boolean isSymbolic() { + return input.isSymbolic(); + } + + @Override + public boolean isSymbolic(Set> visited) { + return isSymbolic(); + } + + @Override + public ImmutableSet getSymbolicVariables() { + return input.getSymbolicVariables(); + } + + @Override + public String toString() { + return "MatcherValue @" + Integer.toHexString(address) + " { " + pattern.pattern() + + (unknown ? ", unknown" : "") + " }"; + } +} diff --git a/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/regex/PatternValue.java b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/regex/PatternValue.java new file mode 100644 index 00000000..54e50ed2 --- /dev/null +++ b/symbolic-executor/src/main/java/de/uzl/its/swat/symbolic/value/reference/regex/PatternValue.java @@ -0,0 +1,55 @@ +package de.uzl.its.swat.symbolic.value.reference.regex; + +import de.uzl.its.swat.common.exceptions.NotImplementedException; +import de.uzl.its.swat.common.exceptions.ValueConversionException; +import de.uzl.its.swat.symbolic.value.Value; +import de.uzl.its.swat.symbolic.value.reference.ObjectValue; +import de.uzl.its.swat.symbolic.value.reference.lang.JavaRegex; +import de.uzl.its.swat.symbolic.value.reference.lang.StringValue; +import java.util.regex.Pattern; +import org.objectweb.asm.Type; +import org.sosy_lab.java_smt.api.SolverContext; + +/** + * Shadow of a Pattern, + * made from the concrete (immutable) Pattern the JVM holds, so its regular expression and flags + * are exact. It is never symbolic. + * + *

Only {@code matcher(s)} for a symbolic String {@code s} is modelled, and only for a pattern + * without flags whose regular expression {@link JavaRegex} translates: it returns a {@link + * MatcherValue}. Everything else is not modelled (a PlaceHolder, as for any unmodelled call). + */ +public final class PatternValue extends ObjectValue { + private static final String PATTERN = "java/util/regex/Pattern"; + + /** The concrete Pattern this shadows. */ + private final Pattern pattern; + + public PatternValue(SolverContext context, Pattern pattern, int address) { + super(context, address); + this.className = PATTERN; + this.pattern = pattern; + } + + /** Whether this shadows the concrete object {@code concrete}. */ + public boolean shadows(Object concrete) { + return pattern == concrete; + } + + @Override + public Value invokeMethod(String name, Type[] desc, Value[] args) + throws NotImplementedException, ValueConversionException { + if (name.equals("matcher") && args.length == 1 && args[0] instanceof StringValue input + && input.isSymbolic() && pattern.flags() == 0 + && JavaRegex.toRegex(context.getFormulaManager().getStringFormulaManager(), pattern.pattern()) != null) { + return new MatcherValue(context, pattern, input); + } + return super.invokeMethod(name, desc, args); + } + + @Override + public String toString() { + return "PatternValue @" + Integer.toHexString(address) + " { " + pattern.pattern() + " }"; + } +} diff --git a/symbolic-executor/src/test/groovy/de/uzl/its/swat/symbolic/RegexAgentSpec.groovy b/symbolic-executor/src/test/groovy/de/uzl/its/swat/symbolic/RegexAgentSpec.groovy new file mode 100644 index 00000000..8a88265b --- /dev/null +++ b/symbolic-executor/src/test/groovy/de/uzl/its/swat/symbolic/RegexAgentSpec.groovy @@ -0,0 +1,100 @@ +package de.uzl.its.swat.symbolic + +import com.fasterxml.jackson.databind.ObjectMapper +import de.uzl.its.swat.testsupport.NondetInputs +import spock.lang.Shared +import spock.lang.Specification +import spock.lang.Unroll + +import javax.tools.ToolProvider +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.TimeUnit + +/** + * L2 (forked agent): Pattern.compile(c).matcher(s).matches() and Pattern.matches(c, s) on a + * symbolic string are modelled as String.matches (a regex membership, no context loss); find, + * reset, flags and regular expressions JavaRegex does not translate still record context loss. + * Runs via the {@code agentTest} Gradle task (needs the agent jar from {@code copyJar}). + */ +class RegexAgentSpec extends Specification { + + static final Path AGENT_JAR = Path.of("lib", "symbolic-executor.jar").toAbsolutePath() + static final Path NATIVE_LIBS = Path.of("..", "libs", "java-library-path").toAbsolutePath() + static final Path TARGET = Path.of("src", "test", "resources", "targets", "regex").toAbsolutePath() + static final Path VERIFIER = Path.of("src", "test", "resources", "targets", "svcomp", "org").toAbsolutePath() + + @Shared Path classes + @Shared String input + + def setupSpec() { + assert Files.isRegularFile(AGENT_JAR): "agent jar missing, run copyJar first: ${AGENT_JAR}" + classes = Files.createTempDirectory("swat-regex-agent-spec") + def sources = ([TARGET, VERIFIER].collectMany { root -> + Files.walk(root).filter { it.toString().endsWith(".java") }.collect { it.toString() } + }) + def err = new ByteArrayOutputStream() + int rc = ToolProvider.getSystemJavaCompiler().run(null, null, err, + (["-source", "1.8", "-target", "1.8", "-g", "-d", classes.toString()] + sources) as String[]) + assert rc == 0: "javac failed: ${err}" + input = "java/lang/String_${NondetInputs.ids(classes, "Main")[0]}".toString() + } + + def cleanupSpec() { + classes?.toFile()?.deleteDir() + } + + @Unroll + def "a modelled match is a regex membership of the symbolic string (#scenario, input '#value')"() { + when: + String out = runOutput(scenario, value) + Map trace = parse(out) + + then: + out.readLines().contains(expected) + !trace.symbolicContextLoss + trace.trace.any { !it.concreteOnly && it.constraint?.contains("str.in_re ${input}".toString()) } + + where: + scenario | value || expected + "matches" | "" || "no match" + "matches" | "0123456789#" || "match" + "patternMatches" | "" || "no match" + "patternMatches" | "42" || "match" + "twice" | "7" || "match" + } + + @Unroll + def "an unmodelled regex call on a symbolic string records context loss (#scenario)"() { + expect: + parse(runOutput(scenario, "")).symbolicContextLoss + + where: + scenario << ["find", "reset", "untranslatable", "flags"] + } + + private static Map parse(String out) { + int start = out.indexOf("\n{\n") + int end = out.indexOf("\n}\n", start) + assert start >= 0 && end > start: "no trace JSON in output:\n${out}" + return new ObjectMapper().readValue(out.substring(start + 1, end + 2), Map) + } + + private String runOutput(String scenario, String value) { + def java = Path.of(System.getProperty("java.home"), "bin", "java").toString() + def cmd = [java, + "-javaagent:${AGENT_JAR}".toString(), + "-Djava.library.path=${NATIVE_LIBS}".toString(), + "-Dconfig.path=/nonexistent/swat.cfg", + "-Dinstrumentation.transformer=SV_COMP", + "-Dsolver.mode=PRINT", + "-DexitOnError=false", + "-DlogShadowStateToConsole=false"] + + (value.isEmpty() ? [] : ["-Dswat.input.${input}=${value}".toString()]) + + ["-cp", classes.toString(), "-ea", "Main", scenario] + def proc = new ProcessBuilder(cmd).directory(classes.toFile()).redirectErrorStream(true).start() + def out = proc.inputStream.text + assert proc.waitFor(5, TimeUnit.MINUTES): "target did not finish:\n${out}" + return out + } +} diff --git a/symbolic-executor/src/test/groovy/de/uzl/its/value/reference/HashContainerModelTest.groovy b/symbolic-executor/src/test/groovy/de/uzl/its/value/reference/HashContainerModelTest.groovy new file mode 100644 index 00000000..72465bbd --- /dev/null +++ b/symbolic-executor/src/test/groovy/de/uzl/its/value/reference/HashContainerModelTest.groovy @@ -0,0 +1,372 @@ +package de.uzl.its.value.reference + +import de.uzl.its.swat.symbolic.invoke.java.util.CollectionInvocation +import de.uzl.its.swat.symbolic.value.ContainerPlaceHolder +import de.uzl.its.swat.symbolic.value.PlaceHolder +import de.uzl.its.swat.symbolic.value.Value +import de.uzl.its.swat.symbolic.value.primitive.numeric.floatingpoint.DoubleValue +import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.IntValue +import de.uzl.its.swat.symbolic.value.primitive.numeric.integral.LongValue +import de.uzl.its.swat.symbolic.value.reference.ContainerContents +import de.uzl.its.swat.symbolic.value.reference.HashEntries +import de.uzl.its.swat.symbolic.value.reference.ObjectValue +import de.uzl.its.swat.symbolic.value.reference.lang.DoubleObjectValue +import de.uzl.its.swat.symbolic.value.reference.lang.IntegerObjectValue +import de.uzl.its.swat.symbolic.value.reference.lang.LongObjectValue +import de.uzl.its.swat.symbolic.value.reference.lang.StringValue +import de.uzl.its.swat.thread.ThreadHandler +import org.sosy_lab.java_smt.api.SolverContext +import spock.lang.Specification + +import static java.lang.Thread.currentThread + +/** The exact model of hash containers with symbolic keys (CollectionInvocation.hashCall, HashEntries). */ +class HashContainerModelTest extends Specification { + + static final String GET = "(Ljava/lang/Object;)Ljava/lang/Object;" + static final String PUT = "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;" + static final String HAS = "(Ljava/lang/Object;)Z" + + SolverContext context + int nextAddress = 100 + + def setup() { + if (ThreadHandler.hasThreadContext(currentThread().id)) { + ThreadHandler.removeThreadContext(currentThread().id) + } + ThreadHandler.addThreadContext(currentThread().id, "Test-Thread", -2) + context = ThreadHandler.getSolverContext(currentThread().id) + } + + private boolean contextLoss() { + return ThreadHandler.getSymbolicTraceHandler(currentThread().id).symbolicTrace.symbolicContextLoss + } + + private StringValue string(String concrete, boolean symbolic = false) { + def s = new StringValue(context, concrete, nextAddress++) + if (symbolic) s.MAKE_SYMBOLIC() + return s + } + + private IntegerObjectValue integer(int concrete, boolean symbolic = false) { + def v = new IntValue(context, concrete) + if (symbolic) v.MAKE_SYMBOLIC() + return new IntegerObjectValue(context, v, nextAddress++) + } + + private ObjectValue nullValue() { + return new ObjectValue(context, ObjectValue.ADDRESS_NULL) + } + + /** A new container of the class, after its (modelled) constructor. */ + private ObjectValue container(String className) { + def c = new ObjectValue(context, className, new IntValue(context, 0), nextAddress++) + assert call(c, "", "()V") != null + return c + } + + private Value call(ObjectValue instance, String name, String desc, Value... args) { + return CollectionInvocation.invoke(instance.className, name, desc, args, true, instance, 0L) + } + + def "only null, Strings and boxed integral values with a known shadow value are keys"() { + given: + def unknown = string("x") + unknown.valueUnknown = true + + expect: + HashEntries.supported(string("a", true)) + HashEntries.supported(integer(1, true)) + HashEntries.supported(nullValue()) + !HashEntries.supported(unknown) + !HashEntries.supported(new DoubleObjectValue(context, new DoubleValue(context, 0.0d), 5)) + !HashEntries.supported(new ObjectValue(context, "Foo", new IntValue(context, 0), 6)) + } + + def "a boxed value built from an int-typed constant has the constant's value"() { + given: + def types = [] as org.objectweb.asm.Type[] + def character = de.uzl.its.swat.symbolic.value.ValueFactory.createObjectValue(0, "java/lang/Character") + def bool = de.uzl.its.swat.symbolic.value.ValueFactory.createObjectValue(0, "java/lang/Boolean") + def small = de.uzl.its.swat.symbolic.value.ValueFactory.createObjectValue(0, "java/lang/Short") + def tiny = de.uzl.its.swat.symbolic.value.ValueFactory.createObjectValue(0, "java/lang/Byte") + + expect: "the shadow NEW makes has a default value, which is no key" + character.valueUnknown + !HashEntries.supported(character) + + when: + character.invokeMethod("", types, [new IntValue(context, 97)] as Value[]) + bool.invokeMethod("", types, [new IntValue(context, 1)] as Value[]) + small.invokeMethod("", types, [new IntValue(context, -7)] as Value[]) + tiny.invokeMethod("", types, [new IntValue(context, -3)] as Value[]) + + then: + character.val.concrete == ('a' as char) + bool.val.concrete == true + small.val.concrete == (short) -7 + tiny.val.concrete == (byte) -3 + } + + def "keys of different classes are never equal, and null equals only null"() { + given: + def five = integer(5, true) + def longFive = new LongObjectValue(context, new LongValue(context, 5L), nextAddress++) + def fmgr = context.formulaManager + + expect: + !HashEntries.equal(five, longFive).holds() + fmgr.extractVariables(HashEntries.equal(five, longFive).condition()).isEmpty() + !HashEntries.equal(string("5", true), five).holds() + !HashEntries.equal(nullValue(), string("null", true)).holds() + HashEntries.equal(nullValue(), nullValue()).holds() + HashEntries.equal(five, integer(5)).holds() + !fmgr.extractVariables(HashEntries.equal(five, integer(5)).condition()).isEmpty() + } + + def "a lookup with a symbolic key records the chain up to the entry it equals"() { + given: + def map = container("java/util/HashMap") + def v1 = string("one") + def v2 = string("two") + call(map, "put", PUT, string("a"), v1) + call(map, "put", PUT, string("b"), v2) + call(map, "put", PUT, string("c"), string("three")) + + when: + def result = call(map, "get", GET, string("b", true)) + def branches = CollectionInvocation.takeBranches() + + then: "it differs from the first key and equals the second; the third is not compared" + branches*.holds() == [false, true] + result instanceof ContainerPlaceHolder + result.exact + result.expected.is(v2) + !contextLoss() + + when: "no key is equal" + result = call(map, "containsKey", HAS, string("z", true)) + branches = CollectionInvocation.takeBranches() + + then: + branches*.holds() == [false, false, false] + result.expected == false + map.contents.hash().size() == 3 + } + + def "a put with an equal symbolic key replaces the value, another one adds an entry"() { + given: + def map = container("java/util/HashMap") + def old = string("old") + call(map, "put", PUT, string("k"), old) + + when: + def result = call(map, "put", PUT, string("k", true), string("new")) + + then: + CollectionInvocation.takeBranches()*.holds() == [true] + result.expected.is(old) + map.contents.hash().size() == 1 + !map.contents.hasSymbolicKeys() // the stored key is still the concrete one + + when: + result = call(map, "put", PUT, string("j", true), string("other")) + + then: + CollectionInvocation.takeBranches()*.holds() == [false] + result.expected == null + map.contents.hash().size() == 2 + map.contents.hasSymbolicKeys() + call(map, "size", "()I").expected == 2 + !contextLoss() + } + + def "a key of another class gives the record up: a context loss once a symbolic key is stored"() { + given: + def concrete = container("java/util/HashMap") + def symbolic = container("java/util/HashMap") + call(concrete, "put", PUT, string("k"), string("v")) + call(symbolic, "put", PUT, string("k", true), string("v")) + def other = new ObjectValue(context, "Foo", new IntValue(context, 0), nextAddress++) + + when: + call(concrete, "put", PUT, other, string("v")) + + then: + concrete.contents.hash() == null + !contextLoss() + + when: + def result = call(symbolic, "get", GET, other) + + then: + result == null // not modelled + symbolic.contents.hash() == null + contextLoss() + symbolic.contents.keysSymbolic(new HashSet<>()) // and no call by key is modelled any more + } + + def "a call outside the tables gives the record up"() { + given: + def map = container("java/util/HashMap") + call(map, "put", PUT, string("k", true), string("v")) + + expect: + call(map, "containsValue", HAS, string("v")) == null + map.contents.hash() == null + contextLoss() + } + + def "a result that differs from the record gives it up"() { + given: + def map = container("java/util/HashMap") + call(map, "put", PUT, string("k", true), string("v")) + def size = call(map, "size", "()I") + + when: + CollectionInvocation.checkPrimitive(size, 1) + + then: + map.contents.hash() != null + !contextLoss() + + when: + CollectionInvocation.checkPrimitive(size, 2) + + then: + map.contents.hash() == null + contextLoss() + } + + def "the iteration order of a plain hash container with a symbolic key is unknown, a linked one's is not"() { + given: + def plain = container("java/util/HashMap") + def linked = container("java/util/LinkedHashMap") + call(plain, "put", PUT, string("b"), string("v")) + call(linked, "put", PUT, string("b"), string("v")) + + expect: + !plain.contents.hashOrderUnknown() + + when: + call(plain, "put", PUT, string("a", true), string("w")) + call(linked, "put", PUT, string("a", true), string("w")) + + then: + plain.contents.hashOrderUnknown() + !linked.contents.hashOrderUnknown() + !contextLoss() // only iterating is one + + when: "a linked map copied from the plain one gets the entries in hash order" + def copy = new ObjectValue(context, "java/util/LinkedHashMap", new IntValue(context, 0), nextAddress++) + call(copy, "", "(Ljava/util/Map;)V", plain) + + then: + copy.contents.hash().size() == 2 + copy.contents.hashOrderUnknown() + } + + def "a set built from a list compares every element with the ones before"() { + given: + def list = container("java/util/ArrayList") + def a = string("z", true) + call(list, "add", HAS, a) + call(list, "add", HAS, string("z")) + def set = new ObjectValue(context, "java/util/HashSet", new IntValue(context, 0), nextAddress++) + + when: + def result = call(set, "", "(Ljava/util/Collection;)V", list) + + then: + result == PlaceHolder.instance + CollectionInvocation.takeBranches()*.holds() == [true] + set.contents.hash().size() == 1 + call(set, "contains", HAS, string("y", true)).expected == false + + when: "a call that is not modelled got the list: its elements are not known exactly" + call(list, "remove", HAS, string("z")) + def second = new ObjectValue(context, "java/util/HashSet", new IntValue(context, 0), nextAddress++) + + then: + call(second, "", "(Ljava/util/Collection;)V", list) == null + second.contents == null + } + + def "equals of two lists with exact elements compares them pair by pair"() { + given: + def x = container("java/util/ArrayList") + def y = container("java/util/ArrayList") + call(x, "add", HAS, string("k")) + call(x, "add", HAS, string("m", true)) + call(x, "add", HAS, string("n")) + call(y, "add", HAS, string("k")) + call(y, "add", HAS, string("z")) + + expect: "different sizes: no comparison" + call(x, "equals", HAS, y).expected == false + CollectionInvocation.takeBranches().isEmpty() + call(x, "equals", HAS, x).expected == true + call(x, "equals", HAS, nullValue()).expected == false + + when: + call(y, "add", HAS, string("n")) + def result = call(x, "equals", HAS, y) + + then: "up to the first pair that differs" + result.expected == false + CollectionInvocation.takeBranches()*.holds() == [true, false] + CollectionInvocation.invoke("java/util/Objects", "equals", "(Ljava/lang/Object;Ljava/lang/Object;)Z", + [x, y] as Value[], false, null, 0L).expected == false + + when: "a call that is not modelled got the list" + CollectionInvocation.escaped(x, "java/util/Collections", "reverse", "(Ljava/util/List;)V", 0L) + + then: + call(x, "equals", HAS, y) == null + } + + def "addAll of a map's key set compares its keys with the set's elements"() { + given: + def map = container("java/util/HashMap") + call(map, "put", PUT, string("j", true), string("v")) + call(map, "put", PUT, string("k"), string("w")) + def keys = new ObjectValue(context, 'java/util/HashMap$KeySet', new IntValue(context, 0), nextAddress++) + def view = ContainerContents.viewOf(map.contents, false) + view.setKeySetOf(map.contents) + keys.setContents(view) + def set = new ObjectValue(context, "java/util/HashSet", new IntValue(context, 0), nextAddress++) + call(set, "", "()V") + call(set, "add", HAS, string("j")) + + when: + def result = call(set, "addAll", "(Ljava/util/Collection;)Z", keys) + + then: "the symbolic key equals the element, the other key is new (a comparison of constants)" + result.expected == true + CollectionInvocation.takeBranches()*.holds() == [true, false] + set.contents.hash().size() == 2 + + when: "a view that is not marked as the key set (values()) is no source" + def values = new ObjectValue(context, 'java/util/HashMap$Values', new IntValue(context, 0), nextAddress++) + values.setContents(ContainerContents.viewOf(map.contents, false)) + + then: + call(set, "addAll", "(Ljava/util/Collection;)Z", values) == null + set.contents.hash() == null + } + + def "a ConcurrentHashMap and the lookups of an empty immutable map are modelled, a Hashtable only by reference"() { + given: + def concurrent = container("java/util/concurrent/ConcurrentHashMap") + def table = container("java/util/Hashtable") + def empty = new ObjectValue(context, 'java/util/Collections$EmptyMap', new IntValue(context, 0), nextAddress++) + + expect: + concurrent.contents.hash() != null + call(concurrent, "putIfAbsent", PUT, string("k", true), string("v")).exact + table.contents.hash() == null + call(table, "get", GET, string("k", true)) == null + call(empty, "get", GET, string("k", true)) == PlaceHolder.instance + call(empty, "put", PUT, string("k", true), string("v")) == null + } +} diff --git a/symbolic-executor/src/test/resources/targets/regex/Main.java b/symbolic-executor/src/test/resources/targets/regex/Main.java new file mode 100644 index 00000000..d0132a76 --- /dev/null +++ b/symbolic-executor/src/test/resources/targets/regex/Main.java @@ -0,0 +1,51 @@ +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; + +/** + * Target for the agent test of the java.util.regex models. The first argument picks a scenario; + * each one branches on a regex match of the symbolic string. + */ +public class Main { + private static final Pattern AID = Pattern.compile("[0-9A-Fa-f]{10,32}\\*?\\#?"); + + public static void main(String[] args) { + String s = Verifier.nondetString(); + boolean r; + switch (args[0]) { + case "matches": + r = AID.matcher(s).matches(); + break; + case "patternMatches": + r = Pattern.matches("[0-9]+", s); + break; + case "twice": { + Matcher m = Pattern.compile("[0-9]+").matcher(s); + r = m.matches() && m.matches(); + break; + } + case "find": + r = Pattern.compile("[0-9]+").matcher(s).find(); + break; + case "reset": { + Matcher m = Pattern.compile("[0-9]+").matcher(s); + m.reset("12"); + r = m.matches(); + break; + } + case "untranslatable": + r = Pattern.compile("a.c").matcher(s).matches(); + break; + case "flags": + r = Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE).matcher(s).matches(); + break; + default: + throw new IllegalArgumentException(args[0]); + } + if (r) { + System.out.println("match"); + } else { + System.out.println("no match"); + } + } +} diff --git a/symbolic-explorer/data/BinaryExecutionTree/Tree.py b/symbolic-explorer/data/BinaryExecutionTree/Tree.py index 5e0deefb..715a424c 100755 --- a/symbolic-explorer/data/BinaryExecutionTree/Tree.py +++ b/symbolic-explorer/data/BinaryExecutionTree/Tree.py @@ -66,8 +66,8 @@ def __init__(self, endpoint_id: Union[str, int, None] = None) -> None: # executed path reached it (see register_loop_states). self.loop_state_first: dict[str, Position] = {} # Relevance (see dfs): the interned sigs of sequences of relevant decisions, the sides the - # last walk found covered (explored, solved or infeasible) per (sig, branch key), and how - # often a walk made a sliced branch relevant again (poisoned it). + # last walk found covered (explored or infeasible, not just solved or given up) per (sig, + # branch key), and how often a walk made a sliced branch relevant again (poisoned it). self.sig_ids: dict[tuple[Ident, bool], int] = {} self.covered: set[tuple[Ident, bool]] = set() self.poisoned_total = 0 diff --git a/symbolic-explorer/data/Database.py b/symbolic-explorer/data/Database.py index e4516c40..18841a5c 100755 --- a/symbolic-explorer/data/Database.py +++ b/symbolic-explorer/data/Database.py @@ -246,7 +246,8 @@ def mark_last_leaf_abnormal(self, endpoint_id: Union[str, int]) -> None: def projected_covered(self, endpoint_id: Union[str, int], ident: Ident, branched: bool) -> bool: """Whether the last walk found the side of the branch `ident` (a candidate's sig and key, - see dfs) explored by some path with the same relevant decisions.""" + see dfs) explored by some path with the same relevant decisions, or proven infeasible. A + solution found for it, run or not, or giving up on it does not count (dfs: claimed).""" endpoint_id = str(endpoint_id) with lock: return (ident, branched) in self.tree[endpoint_id].covered diff --git a/symbolic-explorer/requirements.txt b/symbolic-explorer/requirements.txt index aa7488da..c95ff112 100644 --- a/symbolic-explorer/requirements.txt +++ b/symbolic-explorer/requirements.txt @@ -18,5 +18,5 @@ typing-inspection==0.4.4 typing_extensions==4.16.0 urllib3==2.8.0 uvicorn==0.54.0 -witnesscreator @ git+https://github.com/SWAT-project/WitnessCreator@v1.1.0 +witnesscreator @ git+https://github.com/SWAT-project/WitnessCreator@v1.1.1 z3-solver==5.3.1.0 diff --git a/symbolic-explorer/strategy/DFS.py b/symbolic-explorer/strategy/DFS.py index f56d3172..4966b84a 100755 --- a/symbolic-explorer/strategy/DFS.py +++ b/symbolic-explorer/strategy/DFS.py @@ -98,8 +98,15 @@ def dfs(tree: Tree | None, root: Node | Leaf | None, solved_branches: AbstractSe index: GraphIndex | None = sa_entry.graph_index if sa_entry is not None else None # The candidates with their open side (see side below), in the walk's order. possible_nodes: list[tuple[Node, tuple[Ident, bool]]] = [] - # The (candidate identity, side) pairs explored, solved or found infeasible on some path. + # The (candidate identity, side) pairs some path explored, or found infeasible: what the + # walk records as covered (tree.covered), which forgives a given-up branch and tells that a + # sliced query's run reached its branch (SVCompDriver). Only a real traversal or an UNSAT + # proof counts there. covered: set[tuple[Ident, bool]] = set() + # The pairs a solution was found for (to be run, or run already: solutions are kept), or + # given up on (retired): they keep other candidates with the same identity out, but claim + # nothing about that side. Counted as covered, a branch's own marker forgave it (false SAFE). + claimed: set[tuple[Ident, bool]] = set() # A pre-order over the tree: a node, then the subtree of its skipped side, then that of its # branched side. It keeps its own stack, since paths can be longer than the interpreter's @@ -190,8 +197,10 @@ def dfs(tree: Tree | None, root: Node | Leaf | None, solved_branches: AbstractSe covered.add((ident, False)) if node.branched is not None: covered.add((ident, True)) - if node.gid in solved_branches or node.gid in unsat_branch_ids: + if node.gid in unsat_branch_ids: covered.add((ident, node.branched is None)) # its open side + elif node.gid in solved_branches: + claimed.add((ident, node.branched is None)) else: counts[role] += 1 sig_skipped = sig_branched = sig @@ -238,13 +247,13 @@ def dfs(tree: Tree | None, root: Node | Leaf | None, solved_branches: AbstractSe work.append(_At(child, sa_child, sa_stack, trace_stack, clinit_depth, child_sig, open_regions, child_walk_only)) - # Candidates whose side a path with the same relevant decisions has covered already, and - # repeats of the same candidate, are left out. + # Candidates whose side a path with the same relevant decisions has covered or claimed + # already, and repeats of the same candidate, are left out. tree.covered = covered kept: list[Node] = [] seen: set[tuple[Ident, bool]] = set() for n, side in possible_nodes: - if side in covered or side in seen: + if side in covered or side in claimed or side in seen: counts['covered'] += 1 if distances is not None: distances.pop(n, None) diff --git a/symbolic-explorer/tests/test_divergence.py b/symbolic-explorer/tests/test_divergence.py index b8b4db9c..a1286254 100644 --- a/symbolic-explorer/tests/test_divergence.py +++ b/symbolic-explorer/tests/test_divergence.py @@ -24,6 +24,10 @@ from svcomp.ResourceBudget import ResourceBudget from solver.SolverHandler import SATResult, Z3Handler from strategy.StrategyService import StrategyService +from data.BinaryExecutionTree.Tree import Tree +from strategy.DFS import dfs +from tests.test_relevance import example, link +from tests.test_sa_walk import branch_node, keyed, leaf def trace_branch(id, has_branched): @@ -197,6 +201,93 @@ def test_skipped_side(self): self.assertEqual(self.driver.unreached_given_up_branches(), []) +class GivenUpCoverageTest(DivergenceTestBase): + """What forgives a given-up branch, or tells that a sliced query's run reached its branch on + another path with the same relevant decisions: only a side some path explored, or an UNSAT + proof, as the walk records them (tree.covered). A side merely solved (solutions are kept after + their run) or given up on (retired) only keeps other candidates with the same identity out.""" + + def setUp(self): + super().setUp() + self.driver.projected_reached = 0 + self.driver.pending_projected = None + self.g = keyed(example().load()) + # Two paths that differ only in the irrelevant b (print in the loop or not), each reaching + # the assert's branch a with its skipped side open: a1 and a2 have the same identity. + h1, self.a1 = self.tail('1') + h2, self.a2 = self.tail('2') + b = branch_node('b0', skipped=h1, branched=h2, key='b') + self.root = branch_node('h0', skipped=b, key='h') + link(self.root, b) + self.walk_tree = Tree() + svcomp.Database.instance().projected_covered = \ + lambda endpoint_id, ident, branched: (ident, branched) in self.walk_tree.covered + self.walk(set(), set()) # gives a1 and a2 their identity + self.assertEqual(self.a1.ident, self.a2.ident) + + @staticmethod + def tail(name): + a = branch_node(f'a{name}', branched=leaf(), key='a') + a.sliced_query = False + h = branch_node(f'hx{name}', branched=a, key='h') + link(h, a) + return h, a + + def walk(self, solved, unsat): + """A walk as select_branch makes it (Database.visit_tree: the retired gids among the solved).""" + return [n.id for n in dfs(self.walk_tree, self.root, solved | self.retired, unsat, self.g.entry_node)] + + def give_up(self, target): + with self.assertLogs(svcomp.logger, level='INFO'): + self.driver.give_up_branch(target, False, 'Branch unexpectedly taken/skipped.') + + def reach_skipped_side(self, node): + node.skipped = leaf() + node.skipped.parent = node + + def test_a_given_up_branch_does_not_forgive_itself(self): + self.give_up(self.a1) + # a2 is left out as before (same identity), but a1's own marker covers nothing. + self.assertEqual(self.walk(set(), set()), []) + self.assertEqual(self.driver.unreached_given_up_branches(), [self.a1]) + # A run takes that side on the other path: forgiven. + self.reach_skipped_side(self.a2) + self.walk(set(), set()) + self.assertEqual(self.driver.unreached_given_up_branches(), []) + + def test_a_solution_for_the_same_side_does_not_forgive(self): + self.give_up(self.a1) + self.assertEqual(self.walk({'a2'}, set()), []) + self.assertEqual(self.driver.unreached_given_up_branches(), [self.a1]) + + def test_an_unsat_proof_forgives(self): + self.give_up(self.a1) + self.walk(set(), {'a2'}) + self.assertEqual(self.driver.unreached_given_up_branches(), []) + + def test_a_sliced_run_that_reached_nothing_is_a_divergence(self): + # a1 was solved with a sliced query; its run took a1's branched side again. Its solution + # is kept (Database.solutions), so its gid is among the solved in the next walk. + self.a1.sliced_query = True + self.driver.pending_projected = (self.a1, {}, False, 'Branch unexpectedly taken/skipped.') + self.walk({'a1'}, set()) + with self.assertLogs(svcomp.logger, level='INFO') as logs: + self.assertTrue(self.driver.check_projected_divergence()) + self.assertIn('not reached with the same relevant decisions either', '\n'.join(logs.output)) + self.assertEqual(self.dropped, ['a1']) + self.assertEqual(self.driver.projected_reached, 0) + + def test_a_sliced_run_that_reached_the_side_on_another_path_is_none(self): + self.a1.sliced_query = True + self.driver.pending_projected = (self.a1, {}, False, 'Branch unexpectedly taken/skipped.') + self.reach_skipped_side(self.a2) + self.walk({'a1'}, set()) + with self.assertLogs(svcomp.logger, level='INFO'): + self.assertFalse(self.driver.check_projected_divergence()) + self.assertEqual(self.dropped, []) + self.assertEqual(self.driver.projected_reached, 1) + + class SolveRetryTest(DivergenceTestBase): """solve_retry tries the bounds in RETRY_BOUNDS, then no bound; only UNSAT without a bound gives the branch up.""" diff --git a/targets/sv-comp/swat-benchmarks/README.md b/targets/sv-comp/swat-benchmarks/README.md index ac6a50f3..4f69dd8e 100644 --- a/targets/sv-comp/swat-benchmarks/README.md +++ b/targets/sv-comp/swat-benchmarks/README.md @@ -34,11 +34,21 @@ the graph and the executor can disagree (arrays from JDK calls, implicit and ung across frames, static initializers, String/enum switch, try/finally, object `==`, lambdas, streams, reflection, recursion, default methods, anonymous/inner classes, `synchronized`), plus narrowing tasks added after the first runs (`*-noarg`, `library-throw-catch`, `cce-catch-up1`, -`explicit-throw-generic-catch`, `clinit-unconditional`) and executor reproducers (`exec-*`). In +`explicit-throw-generic-catch`, `clinit-unconditional`), executor reproducers (`exec-*`) and +explorer reproducers (`explore-*`). In every pair the assert is `if (x > 1000 && x < HI) assert false;` with HI = 100000 (VIOLATION) or 1001 (SAFE), and it is only reached after flipping an input-dependent branch that the risky construct sits on. Each task's comment says what it tests. +`model-soundness` (`scripts/modelsoundness.py`): SAFE/VIOLATION pairs for the executor's models of +library calls, one group per model; a VIOLATION task that comes out SAFE means a model hid a +reachable path. `regex-*` (2026-11-17): `Pattern.compile(c).matcher(s).matches()` and +`Pattern.matches(c, s)` on a symbolic string are modelled (`regex-matcher-*`, +`regex-pattern-matches`: SAFE and VIOLATION expected); a regular expression JavaRegex does not +translate, flags, `find()` and a matcher reused with `reset` are not (`regex-untranslatable`, +`regex-flags`, `regex-find`, `regex-reset*`: the SAFE variant must stay UNKNOWN, the VIOLATION +variant must not be SAFE). + `relevance` and `relevance-soundness` (`scripts/relevance.py`): tasks for the relevance analysis (slicing). `relevance` has the design example verbatim (`example`, `example-long`) and families over the loop length n in {10, 100, 1000} whose input-dependent branches cannot affect the assert @@ -80,6 +90,24 @@ has an `@Irrelevant` the relevance analysis confirms (no fact assumed), `irrelev one it refutes (the method holds the assertion), and `irrelevant-caller_true` one that also slices the branches in `main` that only feed it. +`model-soundness`: hand-written SAFE/VIOLATION pairs for the executor's models of library +behaviour; no `_true` task may come out SAFE through a wrong model, and no `_false` task SAFE at +all. Same assert shape as `sa-soundness`. The `lambda-*` tasks (lambdas with captured arguments, +2026-11-17) are a captured input used in a lambda run by an Executor (`executor-capture`), a +value computed from two captured inputs (`capture-outcome`), an instance-capturing lambda +(`instance-capture`), a capturing lambda that is never called (`unused-capture`), long and double +captures (`wide-capture`) and a boxing Function (`boxing`), which SWAT models (SAFE expected), and +library code calling a capturing lambda back (`foreach-capture`, `sort-comparator`, +`thread-capture`) and a method reference that needs an unboxing adaptation +(`methodref-unboxing`), which must stay context losses (UNKNOWN expected for `_true`). + +More `model-soundness` pairs: `arrays-*`, `sb-equals-*`, `logger-*` +(`scripts/model_soundness_arrays.py`): Arrays.equals and Arrays.hashCode/Objects.hash (one differing element, Integer vs Long elements, hash overflow, the +Long/Boolean element hashes), StringBuilder/StringBuffer.equals as identity, and controls that must +not become SAFE: a symbolic array length and a String element in a hash (both must stay UNKNOWN on +the `_true` task), a custom Logger Handler and a StreamHandler over a buffer that assert on the +logged message (a Logger call must not be ignored as a plain sink). + `real-tools-facts` (generated, gitignored, not for regular runs): `scripts/real_tools_facts.py` writes the `real-tools` tasks with hand-written facts, one of them deliberately wrong. See RESULTS.md, "Hand-written facts on real-tools". @@ -92,6 +120,30 @@ relevance analysis cannot see (the assert reads library state); `summary` (a han a quadratic sum, which SWAT's affine summaries cannot merge); `target` (`@Target` on a shell sink in a program without assertions). Compare with `--no-user-facts`. +`model-soundness`, `hash-*`, `hashset-*`, `emptymap-*` and `list-equals*` +(`scripts/model_soundness_hash.py`): SAFE/VIOLATION pairs for the executor's exact model of hash +containers with symbolic keys (HashMap, LinkedHashMap, ConcurrentHashMap, HashSet, LinkedHashSet; +String and boxed integral keys; lookups as equality chains over the stored keys). Exact cases, SAFE +only with the model: two symbolic keys that may be equal (`hash-collide`), put over an equal key, +remove then size, containsKey of a missing key, Integer against Long and String keys, the null key, +putIfAbsent/getOrDefault with null values, copies and putAll, clear, a map of maps, the identity of +the value that put, get and remove return (`hash-identity-*`: `==` is decided on the objects, so the +path must say which entry matched), LinkedHashMap/LinkedHashSet iteration, the set calls (also +`addAll` of a map's key set, `hashset-keyset-copy`), lookups in `Collections.emptyMap()` +(`emptymap-get`), and `equals` of two lists with exactly known elements (`list-equals`, +`list-equals-size`). Controls that must not become SAFE on the `_false` task (the header says "must +stay UNKNOWN"): the iteration order of a plain hash container with a symbolic key (`hash-iterate*`, +`hashset-iterate-copy`, `hash-linked-copy-order`; `hash-iterate-order_false` and +`hash-concurrent-iterate_false` are the ones a search that ignores the order reports SAFE), keys +with their own equals (`hash-user-key*`), Double keys, changes through a view, an iterator or +library code (`hash-view-remove*`, `hash-iterator-remove`, `hash-escape`, `hashset-list-changed`), a +set copied from a source the model does not read (`hashset-aslist`), a list that library code +reordered before `equals` (`list-equals-changed`), and a key whose shadow value SWAT does not know +(`hash-unknown-shadow`, a Short from a String; `hash-new-string`). `hash-char-literal`, +`hash-short-literal` and `hash-boolean-key` have keys boxed from constants: their shadow was the +default 0 until the constructor models took int-typed arguments, and a model that compares such a +shadow makes the VIOLATION task a false SAFE. + `real-tools` (`scripts/tools.py`, harnesses in `scripts/tools/`): harnesses around unmodified GNU java-getopt 1.0.13 (`getopt-minitar`, a tar-like CLI) and opencsv 2.3's `CSVParser` (`opencsv-import`). The tools are not in the repository: `scripts/fetch_tools.sh` downloads their diff --git a/targets/sv-comp/swat-benchmarks/RESULTS.md b/targets/sv-comp/swat-benchmarks/RESULTS.md index fb4ab59e..a73fd54e 100644 --- a/targets/sv-comp/swat-benchmarks/RESULTS.md +++ b/targets/sv-comp/swat-benchmarks/RESULTS.md @@ -264,6 +264,14 @@ and by `dispatcher/switch_n32` and `instance_n32` and `virtual-dispatch/local_n3 switches). The two reproducers added last (`exec-equal-string-inputs`, `exec-indexof-negative-from`) were not checked against the code before their fixes. +`explore-given-up-tochararray-len` and `-contains` (2026-11-17): `toCharArray()` pins the length +of a String input, and every run of a solution for the pin's other side diverges, so the explorer +gives the pin up. A given-up branch rules out SAFE unless some run reaches its side, but the walk +counted the branch's own solved (retired) marker as covering that side: false SAFE on both +`_false` tasks (and the same marker made every run of a sliced query count as reaching its +branch). Since `fix/given-up-self-coverage` only explored sides and UNSAT proofs count +(`DFS.dfs`: covered vs. claimed); all four tasks are UNKNOWN (no run reaches the pin's other side). + ### Results after the fixes swat-benchmarks (300 tasks; the 2026-10-14 runs had 266, without the narrowing tasks and real diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_false.yml new file mode 100644 index 00000000..4f605b7e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-equals-byte_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_false/Main.java new file mode 100644 index 00000000..77505a6a --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): byte:-128 +// Arrays.equals(byte[], byte[]) with sign-extended elements: (byte) 255 == -1. The SAFE +// variant has (byte) 254 == -2 against -1. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + byte x = Verifier.nondetByte(); + byte[] a = {x, (byte) -1}; + byte[] b = {(byte) -128, (byte) 255}; + if (Arrays.equals(a, b)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_true.yml new file mode 100644 index 00000000..3850d057 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-equals-byte_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_true/Main.java new file mode 100644 index 00000000..ac94e85e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-byte_true/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): byte:-128 +// Arrays.equals(byte[], byte[]) with sign-extended elements: (byte) 255 == -1. The SAFE +// variant has (byte) 254 == -2 against -1. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + byte x = Verifier.nondetByte(); + byte[] a = {x, (byte) -1}; + byte[] b = {(byte) -128, (byte) 254}; + if (Arrays.equals(a, b)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_false.yml new file mode 100644 index 00000000..ac3139cd --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-equals-int_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_false/Main.java new file mode 100644 index 00000000..bf6485b3 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:3 int:7 +// Arrays.equals(int[], int[]): equal iff every element is. The SAFE variant differs in a +// concrete element (5 vs 6), so no x, y make the arrays equal. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int y = Verifier.nondetInt(); + int[] a = {x, 5, 7}; + int[] b = {3, 5, y}; + if (Arrays.equals(a, b)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_true.yml new file mode 100644 index 00000000..b5e95371 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-equals-int_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_true/Main.java new file mode 100644 index 00000000..2d620d45 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-int_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): int:3 int:7 +// Arrays.equals(int[], int[]): equal iff every element is. The SAFE variant differs in a +// concrete element (5 vs 6), so no x, y make the arrays equal. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int y = Verifier.nondetInt(); + int[] a = {x, 5, 7}; + int[] b = {3, 6, y}; + if (Arrays.equals(a, b)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_false.yml new file mode 100644 index 00000000..6f7fe5d2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-equals-mixed_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_false/Main.java new file mode 100644 index 00000000..48308047 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): char:Z long:4294967296 boolean:true +// Arrays.equals for char[], long[] and boolean[]; the long must be 2^32 (its upper half). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + char c = Verifier.nondetChar(); + long v = Verifier.nondetLong(); + boolean z = Verifier.nondetBoolean(); + if (Arrays.equals(new char[] {c}, new char[] {'Z'}) && Arrays.equals(new long[] {v}, new long[] {4294967296L}) + && Arrays.equals(new boolean[] {z}, new boolean[] {true})) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_true.yml new file mode 100644 index 00000000..b3b50950 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-equals-mixed_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_true/Main.java new file mode 100644 index 00000000..498de29d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-mixed_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): char:Z long:4294967296 boolean:true +// As arrays-equals-mixed_false, but the low half of the long must also be 1: long equality is +// 64-bit, so v == 2^32 has (int) v == 0 and the assert is unreachable. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + char c = Verifier.nondetChar(); + long v = Verifier.nondetLong(); + boolean z = Verifier.nondetBoolean(); + if (Arrays.equals(new char[] {c}, new char[] {'Z'}) && Arrays.equals(new long[] {v}, new long[] {4294967296L}) + && Arrays.equals(new boolean[] {z}, new boolean[] {true}) && (int) v == 1) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_false.yml new file mode 100644 index 00000000..6b570e81 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-equals-obj_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_false/Main.java new file mode 100644 index 00000000..3ce2f349 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:ab int:5 +// Arrays.equals(Object[], Object[]) over String, Integer and null elements. In the SAFE +// variant the second element is a Long: Integer.equals(Long) is false for every value. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + int x = Verifier.nondetInt(); + Object[] a = {s, Integer.valueOf(x), null}; + Object[] b = {"ab", Integer.valueOf(5), null}; + if (Arrays.equals(a, b)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_true.yml new file mode 100644 index 00000000..39c2fafc --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-equals-obj_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_true/Main.java new file mode 100644 index 00000000..f3e180c2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-obj_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:ab int:5 +// Arrays.equals(Object[], Object[]) over String, Integer and null elements. In the SAFE +// variant the second element is a Long: Integer.equals(Long) is false for every value. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + int x = Verifier.nondetInt(); + Object[] a = {s, Integer.valueOf(x), null}; + Object[] b = {"ab", Long.valueOf(5), null}; + if (Arrays.equals(a, b)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_false.yml new file mode 100644 index 00000000..cb111f1f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-equals-symlen_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_false/Main.java new file mode 100644 index 00000000..d791fe9a --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:2 +// Arrays.equals with an array of symbolic length (not modelled: stays a context loss). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + int n = Verifier.nondetInt(); + if (n < 0 || n > 3) return; + int[] a = new int[n]; + int[] b = new int[2]; + if (Arrays.equals(a, b)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_true.yml new file mode 100644 index 00000000..41545f38 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-equals-symlen_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_true/Main.java new file mode 100644 index 00000000..3bc0596e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-equals-symlen_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): int:2 +// Must stay UNKNOWN: Arrays.equals with an array of symbolic length is not modelled. (SAFE: +// equal arrays have equal lengths, so n == 2.) +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + int n = Verifier.nondetInt(); + if (n < 0 || n > 3) return; + int[] a = new int[n]; + int[] b = new int[2]; + if (Arrays.equals(a, b) && n != 2) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_false.yml new file mode 100644 index 00000000..ffe5524e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-hash-boolean_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_false/Main.java new file mode 100644 index 00000000..e79193d7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_false/Main.java @@ -0,0 +1,12 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): boolean:true +// Arrays.hashCode(boolean[]): Boolean.hashCode is 1231 for true, 1237 for false. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + boolean z = Verifier.nondetBoolean(); + int h = Arrays.hashCode(new boolean[] {z}); + if (h == 1262) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_true.yml new file mode 100644 index 00000000..83b3b88b --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-hash-boolean_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_true/Main.java new file mode 100644 index 00000000..fd5a13c3 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boolean_true/Main.java @@ -0,0 +1,12 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): boolean:true +// The hash of a one-element boolean[] is 31 + 1231 or 31 + 1237, nothing else. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + boolean z = Verifier.nondetBoolean(); + int h = Arrays.hashCode(new boolean[] {z}); + if (h != 1262 && h != 1268) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_false.yml new file mode 100644 index 00000000..e8870314 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-hash-boxed_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_false/Main.java new file mode 100644 index 00000000..164468a7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:5 boolean:true long:4294967296 +// Objects.hash(Integer, Boolean, null, Long) is Arrays.hashCode(Object[]): 2255468 for +// x = 5, z = true, v = 2^32. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Objects; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + boolean z = Verifier.nondetBoolean(); + long v = Verifier.nondetLong(); + if (Objects.hash(x, z, null, v) == 2255468) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_true.yml new file mode 100644 index 00000000..e9d238ff --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-hash-boxed_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_true/Main.java new file mode 100644 index 00000000..802b9398 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-boxed_true/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): int:5 boolean:true long:4294967296 +// With z = false, x = 5 and v = 2^32 fixed the hash is 2261234 (1237 instead of 1231), +// never 2255468. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Objects; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + boolean z = Verifier.nondetBoolean(); + long v = Verifier.nondetLong(); + if (!z && x == 5 && v == 4294967296L && Objects.hash(x, z, null, v) == 2255468) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_false.yml new file mode 100644 index 00000000..f757306b --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-hash-long_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_false/Main.java new file mode 100644 index 00000000..b3a290e5 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_false/Main.java @@ -0,0 +1,13 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): long:4294967296 +// Arrays.hashCode(long[]): Long.hashCode(v) = (int) (v ^ (v >>> 32)); v = 2^32 hashes to 1, +// so h == 32 although (int) v == 0 (a model using (int) v would say SAFE). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + long v = Verifier.nondetLong(); + int h = Arrays.hashCode(new long[] {v}); + if (h == 32 && (int) v == 0) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_true.yml new file mode 100644 index 00000000..461c05ba --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-hash-long_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_true/Main.java new file mode 100644 index 00000000..b6e4f9cd --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-long_true/Main.java @@ -0,0 +1,12 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): long:4294967296 +// v with (int) v == 0 and v >>> 32 == 1 is 2^32, whose hash is 31 + 1 = 32, not 31. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + long v = Verifier.nondetLong(); + int h = Arrays.hashCode(new long[] {v}); + if (h == 31 && (int) v == 0 && (v >>> 32) == 1) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_false.yml new file mode 100644 index 00000000..fe3187ec --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-hash-overflow_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_false/Main.java new file mode 100644 index 00000000..234c7de5 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_false/Main.java @@ -0,0 +1,13 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:69273666 +// Arrays.hashCode(int[]) = 31 * (31 + x) + 1 wraps around: x = 69273666 > 0 gives a +// negative hash (an unbounded-integer model would say it cannot). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int h = Arrays.hashCode(new int[] {x, 1}); + if (x > 0 && h < 0) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_true.yml new file mode 100644 index 00000000..d2123e44 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-hash-overflow_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_true/Main.java new file mode 100644 index 00000000..1d7cee54 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-overflow_true/Main.java @@ -0,0 +1,13 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): int:69273666 +// For 0 < x < 69273635, 962 + 31 * x <= 2147483616 does not overflow, so the hash stays +// positive. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int h = Arrays.hashCode(new int[] {x, 1}); + if (x > 0 && x < 69273635 && h < 0) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_false.yml new file mode 100644 index 00000000..95e23568 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-hash-string_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_false/Main.java new file mode 100644 index 00000000..b63304d2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_false/Main.java @@ -0,0 +1,12 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:a +// Arrays.hashCode(Object[]) with a String element (String.hashCode is not modelled: stays a +// context loss). {"a"} hashes to 31 + 97. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (Arrays.hashCode(new Object[] {s}) == 128) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_true.yml new file mode 100644 index 00000000..7182b963 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - arrays-hash-string_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_true/Main.java new file mode 100644 index 00000000..dd3a0c41 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/arrays-hash-string_true/Main.java @@ -0,0 +1,12 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:a +// Must stay UNKNOWN (String.hashCode is not modelled). SAFE: a one-char string hashing to +// 97 is "a". +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (Arrays.hashCode(new Object[] {s}) == 128 && s.length() == 1 && !s.equals("a")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_false.yml new file mode 100644 index 00000000..f01daeb4 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - emptymap-get_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_false/Main.java new file mode 100644 index 00000000..43d20030 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:k +// Lookups in Collections.emptyMap() and emptySet() with a symbolic key: null and false for +// every key. SAFE: nothing is found. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Map m = Collections.emptyMap(); + Set e = Collections.emptySet(); + if (m.get(s) == null && !e.contains(s) && s.equals("k")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_true.yml new file mode 100644 index 00000000..b50171a0 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - emptymap-get_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_true/Main.java new file mode 100644 index 00000000..3b6c7e33 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/emptymap-get_true/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:k +// Lookups in Collections.emptyMap() and emptySet() with a symbolic key: null and false for +// every key. SAFE: nothing is found. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Map m = Collections.emptyMap(); + Set e = Collections.emptySet(); + if (m.get(s) != null || m.containsKey(s) || e.contains(s)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_false.yml new file mode 100644 index 00000000..fb04d1b0 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-boolean-key_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_false/Main.java new file mode 100644 index 00000000..3bafbc80 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): boolean:true byte:-3 +// Boolean and Byte keys from constants, looked up with symbolic values. SAFE: only true and +// (byte) -3 are keys. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + boolean b = Verifier.nondetBoolean(); + byte y = Verifier.nondetByte(); + Map m = new HashMap<>(); + m.put(true, 1); + m.put((byte) -3, 2); + if (m.containsKey(b) && m.containsKey(y)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_true.yml new file mode 100644 index 00000000..03d5bbb2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-boolean-key_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_true/Main.java new file mode 100644 index 00000000..64bfc797 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-boolean-key_true/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): boolean:true byte:-3 +// Boolean and Byte keys from constants, looked up with symbolic values. SAFE: only true and +// (byte) -3 are keys. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + boolean b = Verifier.nondetBoolean(); + byte y = Verifier.nondetByte(); + Map m = new HashMap<>(); + m.put(true, 1); + m.put((byte) -3, 2); + if (m.size() != 2 || (m.containsKey(b) && !b) || (m.containsKey(y) && y != -3)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_false.yml new file mode 100644 index 00000000..0318c0ad --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-char-key_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_false/Main.java new file mode 100644 index 00000000..d744fa69 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): char:b +// Character keys (boxed chars compare by value). SAFE: only 'b' maps to 2. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + char c = Verifier.nondetChar(); + Map m = new HashMap<>(); + m.put('a', 1); + m.put('b', 2); + Integer v = m.get(c); + if (v != null && v == 2) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_true.yml new file mode 100644 index 00000000..1ba9125b --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-char-key_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_true/Main.java new file mode 100644 index 00000000..e9e2867f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-key_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): char:b +// Character keys (boxed chars compare by value). SAFE: only 'b' maps to 2. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + char c = Verifier.nondetChar(); + Map m = new HashMap<>(); + m.put('a', 1); + m.put('b', 2); + Integer v = m.get(c); + if (v != null && v == 2 && c != 'b') assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_false.yml new file mode 100644 index 00000000..354db7f4 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-char-literal_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_false/Main.java new file mode 100644 index 00000000..7352e226 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_false/Main.java @@ -0,0 +1,18 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): char:a +// A Character key from a char constant, looked up with a symbolic char that is not 0. The +// shadow of the boxed constant must be 'a': with the default '\0' (as before the +// constructor models took int-typed constants) a model that compares it puts c == 0 on the +// path instead of c == 'a', and the VIOLATION task becomes a false SAFE. SAFE: only 'a' is +// a key. The all-zero run ends early. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + char c = Verifier.nondetChar(); + if (c == 0) return; + Map m = new HashMap<>(); + m.put('a', 1); + if (m.containsKey(c)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_true.yml new file mode 100644 index 00000000..29c3697e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-char-literal_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_true/Main.java new file mode 100644 index 00000000..abe8fb1d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-char-literal_true/Main.java @@ -0,0 +1,18 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): char:a +// A Character key from a char constant, looked up with a symbolic char that is not 0. The +// shadow of the boxed constant must be 'a': with the default '\0' (as before the +// constructor models took int-typed constants) a model that compares it puts c == 0 on the +// path instead of c == 'a', and the VIOLATION task becomes a false SAFE. SAFE: only 'a' is +// a key. The all-zero run ends early. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + char c = Verifier.nondetChar(); + if (c == 0) return; + Map m = new HashMap<>(); + m.put('a', 1); + if (m.containsKey(c) && c != 'a') assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_false.yml new file mode 100644 index 00000000..a0a2221c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-clear_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_false/Main.java new file mode 100644 index 00000000..e4d74d03 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:k +// clear, then a new entry: the keys from before are gone. SAFE: only "k" is a key. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put(a, 1); + m.clear(); + m.put("k", 2); + if (m.containsKey(a) && m.size() == 1) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_true.yml new file mode 100644 index 00000000..59d355e9 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-clear_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_true/Main.java new file mode 100644 index 00000000..bd639888 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-clear_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:k +// clear, then a new entry: the keys from before are gone. SAFE: only "k" is a key. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put(a, 1); + m.clear(); + m.put("k", 2); + if (m.containsKey(a) && !a.equals("k")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_false.yml new file mode 100644 index 00000000..50fc88a0 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-collide_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_false/Main.java new file mode 100644 index 00000000..bca8f12d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:k string:k +// Two symbolic keys that may or may not be equal: put(a), put(b) leaves one entry iff +// a.equals(b), and then get(a) is the second value. SAFE: with one entry, get(a) is 2. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put(a, 1); + m.put(b, 2); + if (a.equals("k") && m.size() == 1 && m.get(a) == 2) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_true.yml new file mode 100644 index 00000000..387c79cd --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-collide_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_true/Main.java new file mode 100644 index 00000000..3ede078c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-collide_true/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:k string:k +// Two symbolic keys that may or may not be equal: put(a), put(b) leaves one entry iff +// a.equals(b), and then get(a) is the second value. SAFE: with one entry, get(a) is 2. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put(a, 1); + m.put(b, 2); + if (a.equals("k") && m.size() == 1 && m.get(a) != 2) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_false.yml new file mode 100644 index 00000000..6968907e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-concurrent-iterate_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_false/Main.java new file mode 100644 index 00000000..8b8ff9f2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:p +// As hash-iterate-order, for a ConcurrentHashMap: its iteration order is the hash order too. +// Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new java.util.concurrent.ConcurrentHashMap<>(); + m.put("b", 1); + m.put(a, 2); + int first = m.values().iterator().next(); + if (first == 2 && a.length() == 1 && a.charAt(0) > 'b') assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_true.yml new file mode 100644 index 00000000..cf37cf43 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-concurrent-iterate_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_true/Main.java new file mode 100644 index 00000000..53e84d9d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent-iterate_true/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:p +// As hash-iterate-order, for a ConcurrentHashMap: its iteration order is the hash order too. +// Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new java.util.concurrent.ConcurrentHashMap<>(); + m.put("b", 1); + m.put(a, 2); + int first = m.values().iterator().next(); + if (first == 2 && a.length() == 1 && a.charAt(0) > 'b' && a.charAt(0) < 'p') assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_false.yml new file mode 100644 index 00000000..7696fe44 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-concurrent_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_false/Main.java new file mode 100644 index 00000000..8afd534f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_false/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:k string:k +// A ConcurrentHashMap with symbolic String keys: put, putIfAbsent, get, getOrDefault and +// remove by key as in a HashMap. SAFE: "v1" stays only under a (putIfAbsent of an equal b +// changes nothing, and remove(b) then removes it). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + Map m = new java.util.concurrent.ConcurrentHashMap<>(); + m.put(a, "v1"); + String old = m.putIfAbsent(b, "v2"); + String got = m.getOrDefault(b, "none"); + m.remove(b); + if (old != null && got.equals("v1") && m.isEmpty() && a.equals("k")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_true.yml new file mode 100644 index 00000000..544b9e4a --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-concurrent_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_true/Main.java new file mode 100644 index 00000000..2b73ff00 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-concurrent_true/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:k string:k +// A ConcurrentHashMap with symbolic String keys: put, putIfAbsent, get, getOrDefault and +// remove by key as in a HashMap. SAFE: "v1" stays only under a (putIfAbsent of an equal b +// changes nothing, and remove(b) then removes it). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + Map m = new java.util.concurrent.ConcurrentHashMap<>(); + m.put(a, "v1"); + String old = m.putIfAbsent(b, "v2"); + String got = m.getOrDefault(b, "none"); + m.remove(b); + if (old != null && (!got.equals("v1") || !m.isEmpty() || !a.equals(b))) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_false.yml new file mode 100644 index 00000000..5ad9264d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-contains-missing_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_false/Main.java new file mode 100644 index 00000000..6419a1b8 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:c +// containsKey of a symbolic key the map does not hold: the path must say it differs from +// every stored key. SAFE: a one-char string in [a, b] is a key. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("a", 1); + m.put("b", 2); + if (!m.containsKey(s) && s.length() == 1 && s.charAt(0) >= 'a' && s.charAt(0) <= 'c') assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_true.yml new file mode 100644 index 00000000..c6fc6a8c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-contains-missing_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_true/Main.java new file mode 100644 index 00000000..04eae1b7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-contains-missing_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:c +// containsKey of a symbolic key the map does not hold: the path must say it differs from +// every stored key. SAFE: a one-char string in [a, b] is a key. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("a", 1); + m.put("b", 2); + if (!m.containsKey(s) && s.length() == 1 && s.charAt(0) >= 'a' && s.charAt(0) <= 'b') assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_false.yml new file mode 100644 index 00000000..e9c98dd3 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-copy_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_false/Main.java new file mode 100644 index 00000000..e63ec2cd --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_false/Main.java @@ -0,0 +1,21 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:1 int:7 +// Copy constructor and putAll with symbolic Integer keys: the copies hold the same entries, +// and putAll replaces the value of an equal key. SAFE: only y == 7 puts 2 under 7 (x == 7 +// with y == x is excluded). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int y = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(x, 1); + m.put(y, 2); + Map c = new HashMap<>(m); + Map d = new HashMap<>(); + d.put(7, 0); + d.putAll(c); + if (d.size() == 2 && d.get(7) == 2 && x != 7) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_true.yml new file mode 100644 index 00000000..61eb0ed1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-copy_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_true/Main.java new file mode 100644 index 00000000..bf86c689 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-copy_true/Main.java @@ -0,0 +1,21 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): int:1 int:7 +// Copy constructor and putAll with symbolic Integer keys: the copies hold the same entries, +// and putAll replaces the value of an equal key. SAFE: only y == 7 puts 2 under 7 (x == 7 +// with y == x is excluded). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int y = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(x, 1); + m.put(y, 2); + Map c = new HashMap<>(m); + Map d = new HashMap<>(); + d.put(7, 0); + d.putAll(c); + if (d.size() == 2 && d.get(7) == 2 && x != 7 && y != 7) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_false.yml new file mode 100644 index 00000000..c3becd2d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-double-key_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_false/Main.java new file mode 100644 index 00000000..dafff8bc --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): double:-0.0 +// Double keys: Double.equals compares bits, so -0.0 is not the key 0.0 although -0.0 == 0.0. +// Not modelled. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + double d = Verifier.nondetDouble(); + Map m = new HashMap<>(); + m.put(0.0, 1); + if (!m.containsKey(d) && d == 0.0) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_true.yml new file mode 100644 index 00000000..1145f3db --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-double-key_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_true/Main.java new file mode 100644 index 00000000..59538d22 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-double-key_true/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): double:-0.0 +// Double keys: Double.equals compares bits, so -0.0 is not the key 0.0 although -0.0 == 0.0. +// Not modelled. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + double d = Verifier.nondetDouble(); + Map m = new HashMap<>(); + m.put(0.0, 1); + if (m.containsKey(d) && d != 0.0) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_false.yml new file mode 100644 index 00000000..e1e81bfd --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-escape_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_false/Main.java new file mode 100644 index 00000000..adff9359 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:p +// A set changed by library code that is not modelled (Collections.addAll): its exact +// elements are not known afterwards. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Set s = new HashSet<>(); + s.add(a); + Collections.addAll(s, "p", "q"); + if (s.size() == 2) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_true.yml new file mode 100644 index 00000000..8136d455 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-escape_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_true/Main.java new file mode 100644 index 00000000..96631e5e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-escape_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:p +// A set changed by library code that is not modelled (Collections.addAll): its exact +// elements are not known afterwards. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Set s = new HashSet<>(); + s.add(a); + Collections.addAll(s, "p", "q"); + if (s.size() == 2 && !a.equals("p") && !a.equals("q")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_false.yml new file mode 100644 index 00000000..2f9f87fd --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-getordefault_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_false/Main.java new file mode 100644 index 00000000..0f431ce5 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:k +// getOrDefault: a key mapped to null gives null, not the default (VIOLATION variant, +// a == "k"); in the SAFE variant no value is null. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("k", null); + String v = m.getOrDefault(a, "default"); + if (v == null) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_true.yml new file mode 100644 index 00000000..530f96e1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-getordefault_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_true/Main.java new file mode 100644 index 00000000..6bb0aec1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-getordefault_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:k +// getOrDefault: a key mapped to null gives null, not the default (VIOLATION variant, +// a == "k"); in the SAFE variant no value is null. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("k", "old"); + String v = m.getOrDefault(a, "default"); + if (v == null) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_false.yml new file mode 100644 index 00000000..d4241598 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-identity-get_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_false/Main.java new file mode 100644 index 00000000..eb31fc85 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_false/Main.java @@ -0,0 +1,20 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:q string:q +// The identity of what get returns, with possibly equal symbolic keys: the lookup key b +// finds v1 iff it equals a (which may itself have replaced the entry of "x"). SAFE: get +// returns v1 only for b equal to a, and v0 only for "x". +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + Object v0 = new Object(); + Object v1 = new Object(); + Map m = new HashMap<>(); + m.put("x", v0); + m.put(a, v1); + Object got = m.get(b); + if (got == v1 && b.equals("q")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_true.yml new file mode 100644 index 00000000..5afd9223 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-identity-get_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_true/Main.java new file mode 100644 index 00000000..b5568b13 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-get_true/Main.java @@ -0,0 +1,20 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:q string:q +// The identity of what get returns, with possibly equal symbolic keys: the lookup key b +// finds v1 iff it equals a (which may itself have replaced the entry of "x"). SAFE: get +// returns v1 only for b equal to a, and v0 only for "x". +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + Object v0 = new Object(); + Object v1 = new Object(); + Map m = new HashMap<>(); + m.put("x", v0); + m.put(a, v1); + Object got = m.get(b); + if ((got == v1 && !b.equals(a)) || (got == v0 && (!b.equals("x") || a.equals("x"))) || (got != v0 && got != v1 && got != null)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_false.yml new file mode 100644 index 00000000..ced9f8ed --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-identity-put_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_false/Main.java new file mode 100644 index 00000000..84dbf75f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_false/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:k string:k +// The identity of what put returns: the previous value of the entry the key equals, v0 iff +// the two symbolic keys are equal. Reference comparisons are decided on the objects, so the +// path must say which entry matched. SAFE: put returns v0 only for equal keys. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + Object v0 = new Object(); + Object v1 = new Object(); + Map m = new HashMap<>(); + m.put(a, v0); + Object previous = m.put(b, v1); + if (previous == v0 && a.equals("k")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_true.yml new file mode 100644 index 00000000..c4c553f1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-identity-put_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_true/Main.java new file mode 100644 index 00000000..dee25edc --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-put_true/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:k string:k +// The identity of what put returns: the previous value of the entry the key equals, v0 iff +// the two symbolic keys are equal. Reference comparisons are decided on the objects, so the +// path must say which entry matched. SAFE: put returns v0 only for equal keys. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + Object v0 = new Object(); + Object v1 = new Object(); + Map m = new HashMap<>(); + m.put(a, v0); + Object previous = m.put(b, v1); + if ((previous == v0 && !a.equals(b)) || (previous != v0 && previous != null)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_false.yml new file mode 100644 index 00000000..4ec72dbe --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-identity-remove_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_false/Main.java new file mode 100644 index 00000000..d4c00087 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_false/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:5 int:5 +// The identity of what remove returns and of the values left, with possibly equal symbolic +// Integer keys. SAFE: remove(y) returns v1 iff y == x or x == 1 and y == 1. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int y = Verifier.nondetInt(); + Object v0 = new Object(); + Object v1 = new Object(); + Map m = new HashMap<>(); + m.put(1, v0); + m.put(x, v1); + Object removed = m.remove(y); + if (removed == v1 && m.get(1) == v0 && x == 5) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_true.yml new file mode 100644 index 00000000..a3671fe4 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-identity-remove_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_true/Main.java new file mode 100644 index 00000000..99dbd74e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-identity-remove_true/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): int:5 int:5 +// The identity of what remove returns and of the values left, with possibly equal symbolic +// Integer keys. SAFE: remove(y) returns v1 iff y == x or x == 1 and y == 1. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int y = Verifier.nondetInt(); + Object v0 = new Object(); + Object v1 = new Object(); + Map m = new HashMap<>(); + m.put(1, v0); + m.put(x, v1); + Object removed = m.remove(y); + if ((removed == v1 && y != x) || (removed == v0 && (y != 1 || x == 1)) || (removed == null && (y == x || y == 1))) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_false.yml new file mode 100644 index 00000000..f90a7903 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-int-long_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_false/Main.java new file mode 100644 index 00000000..ee9a7ed0 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:5 +// Integer and Long keys: an Integer never equals a Long of the same value. The VIOLATION +// variant puts Integer.valueOf(x), which replaces the entry of 5 for x == 5; the SAFE one +// puts Long.valueOf(x), which is always a second entry. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(Integer.valueOf(5), "int"); + m.put(Integer.valueOf(x), "other"); + if (m.size() == 1) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_true.yml new file mode 100644 index 00000000..cfeb7f4f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-int-long_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_true/Main.java new file mode 100644 index 00000000..09f2a6cd --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-int-long_true/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): int:5 +// Integer and Long keys: an Integer never equals a Long of the same value. The VIOLATION +// variant puts Integer.valueOf(x), which replaces the entry of 5 for x == 5; the SAFE one +// puts Long.valueOf(x), which is always a second entry. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(Integer.valueOf(5), "int"); + m.put(Long.valueOf(x), "other"); + if (m.size() == 1 || !"int".equals(m.get(5))) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_false.yml new file mode 100644 index 00000000..da2e9b34 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-iterate-order_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_false/Main.java new file mode 100644 index 00000000..497c480d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_false/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:p +// The first value of a HashMap is the symbolic key's only if that key hashes before "b": true +// for "" and "p" (table index 0), false for "c" (index 3 after 2). No branch says so: a search +// that takes the order of one run as fixed sees first == 1 for every one-char key after 'b' +// and reports the VIOLATION task SAFE. Must stay UNKNOWN (SAFE variant: 'c' to 'o' all +// come after "b"). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("b", 1); + m.put(a, 2); + int first = m.values().iterator().next(); + if (first == 2 && a.length() == 1 && a.charAt(0) > 'b') assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_true.yml new file mode 100644 index 00000000..1c9053ee --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-iterate-order_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_true/Main.java new file mode 100644 index 00000000..6707f53e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-order_true/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:p +// The first value of a HashMap is the symbolic key's only if that key hashes before "b": true +// for "" and "p" (table index 0), false for "c" (index 3 after 2). No branch says so: a search +// that takes the order of one run as fixed sees first == 1 for every one-char key after 'b' +// and reports the VIOLATION task SAFE. Must stay UNKNOWN (SAFE variant: 'c' to 'o' all +// come after "b"). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("b", 1); + m.put(a, 2); + int first = m.values().iterator().next(); + if (first == 2 && a.length() == 1 && a.charAt(0) > 'b' && a.charAt(0) < 'p') assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_false.yml new file mode 100644 index 00000000..5535d7e3 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-iterate-values_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_false/Main.java new file mode 100644 index 00000000..94dc7657 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:c +// As hash-iterate, over values(): their order is the keys' hash order too. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("b", 1); + m.put(a, 2); + int first = m.values().iterator().next(); + if (first == 1 && a.equals("c")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_true.yml new file mode 100644 index 00000000..62f3db93 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-iterate-values_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_true/Main.java new file mode 100644 index 00000000..522f3a2c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate-values_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:c +// As hash-iterate, over values(): their order is the keys' hash order too. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("b", 1); + m.put(a, 2); + int first = m.values().iterator().next(); + if (first == 1 && a.equals("a")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_false.yml new file mode 100644 index 00000000..525de516 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-iterate_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_false/Main.java new file mode 100644 index 00000000..e425f504 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_false/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:c +// Iteration of a HashMap with a symbolic key: the order depends on the key's hash code +// ("b" comes before "c" but after "a" and ""), which no branch on the path says. Must stay +// UNKNOWN: an order taken as fixed makes the VIOLATION task a false SAFE. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("b", 1); + m.put(a, 2); + String first = m.keySet().iterator().next(); + if (first.equals("b") && a.equals("c")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_true.yml new file mode 100644 index 00000000..472deda5 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-iterate_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_true/Main.java new file mode 100644 index 00000000..b3adc01e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterate_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:c +// Iteration of a HashMap with a symbolic key: the order depends on the key's hash code +// ("b" comes before "c" but after "a" and ""), which no branch on the path says. Must stay +// UNKNOWN: an order taken as fixed makes the VIOLATION task a false SAFE. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("b", 1); + m.put(a, 2); + String first = m.keySet().iterator().next(); + if (first.equals("b") && a.equals("a")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_false.yml new file mode 100644 index 00000000..b2ff54da --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-iterator-remove_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_false/Main.java new file mode 100644 index 00000000..2c9f363b --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_false/Main.java @@ -0,0 +1,18 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:k +// An entry removed through an iterator: the map's exact entries are not known afterwards. +// Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new LinkedHashMap<>(); + m.put("k", 1); + m.put(a, 2); + Iterator it = m.keySet().iterator(); + it.next(); + it.remove(); + if (m.isEmpty()) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_true.yml new file mode 100644 index 00000000..ce814a0f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-iterator-remove_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_true/Main.java new file mode 100644 index 00000000..cdf843e3 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-iterator-remove_true/Main.java @@ -0,0 +1,18 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:k +// An entry removed through an iterator: the map's exact entries are not known afterwards. +// Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new LinkedHashMap<>(); + m.put("k", 1); + m.put(a, 2); + Iterator it = m.keySet().iterator(); + it.next(); + it.remove(); + if (m.isEmpty() && !a.equals("k")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_false.yml new file mode 100644 index 00000000..f3aeface --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-linked-copy-order_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_false/Main.java new file mode 100644 index 00000000..d3246b06 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_false/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:p +// A LinkedHashMap copied from a HashMap with a symbolic key iterates in the HashMap's hash +// order, not in the order the keys were put (see hash-iterate-order). Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("b", 1); + m.put(a, 2); + Map l = new LinkedHashMap<>(m); + int first = l.values().iterator().next(); + if (first == 2 && a.length() == 1 && a.charAt(0) > 'b') assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_true.yml new file mode 100644 index 00000000..1475560e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-linked-copy-order_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_true/Main.java new file mode 100644 index 00000000..24377123 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-copy-order_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:p +// A LinkedHashMap copied from a HashMap with a symbolic key iterates in the HashMap's hash +// order, not in the order the keys were put (see hash-iterate-order). Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("b", 1); + m.put(a, 2); + Map l = new LinkedHashMap<>(m); + int first = l.values().iterator().next(); + if (first == 2 && a.length() == 1 && a.charAt(0) > 'b' && a.charAt(0) < 'p') assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_false.yml new file mode 100644 index 00000000..e883ce45 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-linked-iterate_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_false/Main.java new file mode 100644 index 00000000..92990869 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_false/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:a +// Iteration of a LinkedHashMap with a symbolic key: insertion order, whatever the keys are. +// SAFE: "b" was put first, and putting it again does not move it. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new LinkedHashMap<>(); + m.put("b", 1); + m.put(a, 2); + Iterator it = m.keySet().iterator(); + String first = it.next(); + if (it.hasNext() && it.next().equals("a")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_true.yml new file mode 100644 index 00000000..5953689f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-linked-iterate_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_true/Main.java new file mode 100644 index 00000000..910e10a7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-linked-iterate_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:a +// Iteration of a LinkedHashMap with a symbolic key: insertion order, whatever the keys are. +// SAFE: "b" was put first, and putting it again does not move it. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new LinkedHashMap<>(); + m.put("b", 1); + m.put(a, 2); + Iterator it = m.keySet().iterator(); + String first = it.next(); + if (!first.equals("b")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_false.yml new file mode 100644 index 00000000..6a86bb8c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-nested_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_false/Main.java new file mode 100644 index 00000000..d913561f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_false/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:p string:id +// A map of maps with symbolic keys: get returns the very map that was put, so what is put +// into it shows through the outer map. SAFE: only q == "id" makes "id" a key. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String p = Verifier.nondetString(); + String q = Verifier.nondetString(); + Map> outer = new HashMap<>(); + outer.put(p, new HashMap()); + outer.get(p).put(q, "v"); + Map inner = outer.get(p); + if (inner.containsKey("id") && p.equals("p")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_true.yml new file mode 100644 index 00000000..8dd1222a --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-nested_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_true/Main.java new file mode 100644 index 00000000..497add53 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-nested_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:p string:id +// A map of maps with symbolic keys: get returns the very map that was put, so what is put +// into it shows through the outer map. SAFE: only q == "id" makes "id" a key. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String p = Verifier.nondetString(); + String q = Verifier.nondetString(); + Map> outer = new HashMap<>(); + outer.put(p, new HashMap()); + outer.get(p).put(q, "v"); + Map inner = outer.get(p); + if (inner.containsKey("id") && !q.equals("id")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_false.yml new file mode 100644 index 00000000..fa9e563f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-new-string_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_false/Main.java new file mode 100644 index 00000000..a48224ca --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:hi +// A String key built by a constructor SWAT may not model (from bytes): its shadow must not +// be compared unless it is the string. SAFE or UNKNOWN on the SAFE task. The all-zero run ends +// early. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (s.isEmpty()) return; + Map m = new HashMap<>(); + m.put(new String(new byte[] {104, 105}), 1); + if (m.containsKey(s)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_true.yml new file mode 100644 index 00000000..863a04b9 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-new-string_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_true/Main.java new file mode 100644 index 00000000..c756ea0c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-new-string_true/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:hi +// A String key built by a constructor SWAT may not model (from bytes): its shadow must not +// be compared unless it is the string. SAFE or UNKNOWN on the SAFE task. The all-zero run ends +// early. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (s.isEmpty()) return; + Map m = new HashMap<>(); + m.put(new String(new byte[] {104, 105}), 1); + if (m.containsKey(s) && !s.equals("hi")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_false.yml new file mode 100644 index 00000000..d45e7f95 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-null-key_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_false/Main.java new file mode 100644 index 00000000..682ebf7c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:null +// The null key next to a symbolic String key: the String "null" is not the null key, and a +// nondet String is never null. SAFE: the null entry is never replaced. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put(null, "n"); + m.put(s, "s"); + if (m.size() == 2 && m.get(null).equals("n") && m.containsKey("null")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_true.yml new file mode 100644 index 00000000..0e17c461 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-null-key_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_true/Main.java new file mode 100644 index 00000000..5370b03d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-null-key_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:null +// The null key next to a symbolic String key: the String "null" is not the null key, and a +// nondet String is never null. SAFE: the null entry is never replaced. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put(null, "n"); + m.put(s, "s"); + if (m.size() != 2 || !m.get(null).equals("n")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_false.yml new file mode 100644 index 00000000..8f12a967 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-overwrite_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_false/Main.java new file mode 100644 index 00000000..2de4af2a --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:key +// put with a symbolic key overwrites the entry of an equal concrete key; get of the concrete +// key then returns the new value. SAFE: only the key "key" overwrites it. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String k = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("key", "old"); + String before = m.put(k, "new"); + if (m.get("key").equals("new") && before != null) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_true.yml new file mode 100644 index 00000000..dfdc2c93 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-overwrite_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_true/Main.java new file mode 100644 index 00000000..c741b9ba --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-overwrite_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:key +// put with a symbolic key overwrites the entry of an equal concrete key; get of the concrete +// key then returns the new value. SAFE: only the key "key" overwrites it. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String k = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("key", "old"); + String before = m.put(k, "new"); + if (m.get("key").equals("new") && !k.equals("key")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_false.yml new file mode 100644 index 00000000..6282d76e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-putifabsent_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_false/Main.java new file mode 100644 index 00000000..ba3f317c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:k +// putIfAbsent: a key mapped to null counts as absent and gets the new value (VIOLATION +// variant, a == "k"); a key with a value keeps it (SAFE variant). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("k", null); + m.put("j", "J"); + m.putIfAbsent(a, "new"); + if ("new".equals(m.get("k"))) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_true.yml new file mode 100644 index 00000000..4ade4e9f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-putifabsent_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_true/Main.java new file mode 100644 index 00000000..8f317f0e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-putifabsent_true/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:k +// putIfAbsent: a key mapped to null counts as absent and gets the new value (VIOLATION +// variant, a == "k"); a key with a value keeps it (SAFE variant). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("k", "old"); + m.put("j", "J"); + m.putIfAbsent(a, "new"); + if ("new".equals(m.get("k"))) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_false.yml new file mode 100644 index 00000000..7431d30f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-remove-size_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_false/Main.java new file mode 100644 index 00000000..922b997e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_false/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:3 +// remove with a symbolic Integer key, then size and containsKey. SAFE: only x == 3 removes +// the entry of 3. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(1, "a"); + m.put(2, "b"); + m.put(3, "c"); + String removed = m.remove(x); + if (m.size() == 2 && !m.containsKey(3) && removed.equals("c")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_true.yml new file mode 100644 index 00000000..92f813d7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-remove-size_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_true/Main.java new file mode 100644 index 00000000..801f4d56 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-remove-size_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): int:3 +// remove with a symbolic Integer key, then size and containsKey. SAFE: only x == 3 removes +// the entry of 3. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(1, "a"); + m.put(2, "b"); + m.put(3, "c"); + String removed = m.remove(x); + if (m.size() == 2 && !m.containsKey(3) && x != 3) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_false.yml new file mode 100644 index 00000000..b812b251 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-short-literal_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_false/Main.java new file mode 100644 index 00000000..39c22b42 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): short:7 +// As hash-char-literal, with a Short key from a constant. SAFE: only 7 is a key. The +// all-zero run ends early. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + short x = Verifier.nondetShort(); + if (x == 0) return; + Map m = new HashMap<>(); + m.put((short) 7, 1); + if (m.containsKey(x)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_true.yml new file mode 100644 index 00000000..12ab7d35 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-short-literal_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_true/Main.java new file mode 100644 index 00000000..41ddfb41 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-short-literal_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): short:7 +// As hash-char-literal, with a Short key from a constant. SAFE: only 7 is a key. The +// all-zero run ends early. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + short x = Verifier.nondetShort(); + if (x == 0) return; + Map m = new HashMap<>(); + m.put((short) 7, 1); + if (m.containsKey(x) && x != 7) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_false.yml new file mode 100644 index 00000000..015bef9b --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-string-vs-int_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_false/Main.java new file mode 100644 index 00000000..5bf52e39 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:5 int:5 +// A String key and an Integer key are never equal ("5" is not 5). SAFE: the map always has +// two entries. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + int x = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(s, "string"); + m.put(x, "int"); + if (m.size() == 2 && s.equals("5") && x == 5) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_true.yml new file mode 100644 index 00000000..7126a8d4 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-string-vs-int_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_true/Main.java new file mode 100644 index 00000000..8231e4e3 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-string-vs-int_true/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:5 int:5 +// A String key and an Integer key are never equal ("5" is not 5). SAFE: the map always has +// two entries. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + int x = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(s, "string"); + m.put(x, "int"); + if (m.size() != 2) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_false.yml new file mode 100644 index 00000000..18658aa2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-unknown-shadow_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_false/Main.java new file mode 100644 index 00000000..336ddba8 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_false/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): short:7 +// A Short key built by a constructor SWAT does not model (from a String): its shadow keeps +// the default 0, so it must not be compared (comparing it puts x == 0 on the path instead of +// x == 7, and the VIOLATION task becomes a false SAFE). Must stay UNKNOWN. The all-zero run +// ends early. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + short x = Verifier.nondetShort(); + if (x == 0) return; + Map m = new HashMap<>(); + m.put(new Short("7"), 1); + if (m.containsKey(x)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_true.yml new file mode 100644 index 00000000..8df7e40b --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-unknown-shadow_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_true/Main.java new file mode 100644 index 00000000..e1519864 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-unknown-shadow_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): short:7 +// A Short key built by a constructor SWAT does not model (from a String): its shadow keeps +// the default 0, so it must not be compared (comparing it puts x == 0 on the path instead of +// x == 7, and the VIOLATION task becomes a false SAFE). Must stay UNKNOWN. The all-zero run +// ends early. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + short x = Verifier.nondetShort(); + if (x == 0) return; + Map m = new HashMap<>(); + m.put(new Short("7"), 1); + if (m.containsKey(x) && x != 7) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_false.yml new file mode 100644 index 00000000..f76b20a7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-user-key-mixed_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_false/Main.java new file mode 100644 index 00000000..fbab5136 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_false/Main.java @@ -0,0 +1,23 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:s int:13 +// A map with a symbolic String key and a key with its own equals: the model gives up at the +// second, and the map is not exact any more. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + static final class Key { + final int v; + Key(int v) { this.v = v; } + @Override public boolean equals(Object o) { return o instanceof Key && ((Key) o).v % 10 == v % 10; } + @Override public int hashCode() { return v % 10; } + } + public static void main(String[] args) { + String s = Verifier.nondetString(); + int x = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(s, "string"); + m.put(new Key(3), "three"); + m.remove(new Key(x)); + if (s.equals("s") && x != 3 && m.size() == 1) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_true.yml new file mode 100644 index 00000000..6e82cbe2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-user-key-mixed_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_true/Main.java new file mode 100644 index 00000000..86fb873a --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key-mixed_true/Main.java @@ -0,0 +1,23 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:s int:13 +// A map with a symbolic String key and a key with its own equals: the model gives up at the +// second, and the map is not exact any more. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + static final class Key { + final int v; + Key(int v) { this.v = v; } + @Override public boolean equals(Object o) { return o instanceof Key && ((Key) o).v % 10 == v % 10; } + @Override public int hashCode() { return v % 10; } + } + public static void main(String[] args) { + String s = Verifier.nondetString(); + int x = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(s, "string"); + m.put(new Key(3), "three"); + m.remove(new Key(x)); + if (s.equals("s") && x >= 0 && x < 10 && x != 3 && m.size() == 1) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_false.yml new file mode 100644 index 00000000..e4102376 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-user-key_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_false/Main.java new file mode 100644 index 00000000..993958b7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_false/Main.java @@ -0,0 +1,20 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:13 +// A key class with its own equals and hashCode (equal modulo 10): not a key the model +// compares. Must stay UNKNOWN (SAFE would need the hash code to be tracked into the map). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + static final class Key { + final int v; + Key(int v) { this.v = v; } + @Override public boolean equals(Object o) { return o instanceof Key && ((Key) o).v % 10 == v % 10; } + @Override public int hashCode() { return v % 10; } + } + public static void main(String[] args) { + int x = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(new Key(3), "three"); + if (x != 3 && m.containsKey(new Key(x))) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_true.yml new file mode 100644 index 00000000..64542dbc --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-user-key_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_true/Main.java new file mode 100644 index 00000000..f8252085 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-user-key_true/Main.java @@ -0,0 +1,20 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): int:13 +// A key class with its own equals and hashCode (equal modulo 10): not a key the model +// compares. Must stay UNKNOWN (SAFE would need the hash code to be tracked into the map). +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + static final class Key { + final int v; + Key(int v) { this.v = v; } + @Override public boolean equals(Object o) { return o instanceof Key && ((Key) o).v % 10 == v % 10; } + @Override public int hashCode() { return v % 10; } + } + public static void main(String[] args) { + int x = Verifier.nondetInt(); + Map m = new HashMap<>(); + m.put(new Key(3), "three"); + if (x >= 0 && x < 10 && x != 3 && m.containsKey(new Key(x))) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_false.yml new file mode 100644 index 00000000..711d3b17 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-view-remove-key_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_false/Main.java new file mode 100644 index 00000000..ed7a21b4 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:k +// As hash-view-remove, with the symbolic key in the map and a concrete one removed through +// the view. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put(a, 1); + m.keySet().remove("k"); + if (m.isEmpty()) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_true.yml new file mode 100644 index 00000000..3f8df857 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-view-remove-key_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_true/Main.java new file mode 100644 index 00000000..631b8269 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove-key_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:k +// As hash-view-remove, with the symbolic key in the map and a concrete one removed through +// the view. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put(a, 1); + m.keySet().remove("k"); + if (m.isEmpty() && !a.equals("k")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_false.yml new file mode 100644 index 00000000..da6fec36 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-view-remove_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_false/Main.java new file mode 100644 index 00000000..c09e0ce1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:k +// A key removed through the keySet view, which is not modelled: the map's exact entries are +// not known afterwards. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("k", 1); + m.keySet().remove(a); + if (m.isEmpty()) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_true.yml new file mode 100644 index 00000000..d50d7b84 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hash-view-remove_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_true/Main.java new file mode 100644 index 00000000..d03e0cf6 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hash-view-remove_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:k +// A key removed through the keySet view, which is not modelled: the map's exact entries are +// not known afterwards. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put("k", 1); + m.keySet().remove(a); + if (m.isEmpty() && !a.equals("k")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_false.yml new file mode 100644 index 00000000..e2e5092a --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-add_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_false/Main.java new file mode 100644 index 00000000..f2ee28bc --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:x string:x +// HashSet.add of a symbolic element returns whether it was new; contains of another symbolic +// one. SAFE: if a was not new, the set is {"x"} and holds b only for b == "x". +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + Set s = new HashSet<>(); + s.add("x"); + boolean fresh = s.add(a); + if (!fresh && s.size() == 1 && s.contains(b) && b.length() == 1) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_true.yml new file mode 100644 index 00000000..37756b9c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-add_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_true/Main.java new file mode 100644 index 00000000..4705a3bb --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-add_true/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:x string:x +// HashSet.add of a symbolic element returns whether it was new; contains of another symbolic +// one. SAFE: if a was not new, the set is {"x"} and holds b only for b == "x". +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + Set s = new HashSet<>(); + s.add("x"); + boolean fresh = s.add(a); + if (!fresh && s.contains(b) && !b.equals("x")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_false.yml new file mode 100644 index 00000000..b1047354 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-aslist_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_false/Main.java new file mode 100644 index 00000000..789cf8ca --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_false/Main.java @@ -0,0 +1,13 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:z +// A HashSet built from Arrays.asList, a source the model does not copy from: the set is not +// modelled. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Set s = new HashSet<>(Arrays.asList(a, "z")); + if (s.size() == 1) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_true.yml new file mode 100644 index 00000000..f53769c0 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-aslist_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_true/Main.java new file mode 100644 index 00000000..7606e6b0 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-aslist_true/Main.java @@ -0,0 +1,13 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:z +// A HashSet built from Arrays.asList, a source the model does not copy from: the set is not +// modelled. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Set s = new HashSet<>(Arrays.asList(a, "z")); + if (s.size() == 1 && !a.equals("z")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_false.yml new file mode 100644 index 00000000..dc56c5ad --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-copy_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_false/Main.java new file mode 100644 index 00000000..25f917de --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_false/Main.java @@ -0,0 +1,21 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:z string:z +// A HashSet built from a list with symbolic elements (and addAll of another set): equal +// elements collapse. SAFE: one element is left only if a is "z" too. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + List l = new ArrayList<>(); + l.add(a); + l.add(b); + l.add("z"); + Set s = new HashSet<>(l); + Set t = new LinkedHashSet<>(); + t.add("z"); + boolean changed = t.addAll(s); + if (!changed && s.size() == 1 && t.size() == 1 && a.length() == 1) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_true.yml new file mode 100644 index 00000000..52d55cd8 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-copy_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_true/Main.java new file mode 100644 index 00000000..36e62e0e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-copy_true/Main.java @@ -0,0 +1,21 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:z string:z +// A HashSet built from a list with symbolic elements (and addAll of another set): equal +// elements collapse. SAFE: one element is left only if a is "z" too. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + List l = new ArrayList<>(); + l.add(a); + l.add(b); + l.add("z"); + Set s = new HashSet<>(l); + Set t = new LinkedHashSet<>(); + t.add("z"); + boolean changed = t.addAll(s); + if (!changed && !a.equals("z")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_false.yml new file mode 100644 index 00000000..5912cfdc --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-iterate-copy_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_false/Main.java new file mode 100644 index 00000000..39792bbb --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:c +// A HashSet with a symbolic element copied into a list: the list's order is the hash order. +// Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Set s = new HashSet<>(); + s.add("b"); + s.add(a); + List l = new ArrayList<>(s); + if (l.get(0).equals("b") && a.equals("c")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_true.yml new file mode 100644 index 00000000..8205e896 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-iterate-copy_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_true/Main.java new file mode 100644 index 00000000..d412086c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-iterate-copy_true/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:c +// A HashSet with a symbolic element copied into a list: the list's order is the hash order. +// Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Set s = new HashSet<>(); + s.add("b"); + s.add(a); + List l = new ArrayList<>(s); + if (l.get(0).equals("b") && a.equals("a")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_false.yml new file mode 100644 index 00000000..299db452 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-keyset-copy_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_false/Main.java new file mode 100644 index 00000000..849820e1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_false/Main.java @@ -0,0 +1,18 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:j +// addAll of a map's key set (with a symbolic key) into a set. SAFE: two elements are left +// only if a is "j" or "k". +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put(a, 1); + m.put("k", 2); + Set s = new HashSet<>(); + s.add("j"); + s.addAll(m.keySet()); + if (s.size() == 2 && a.equals("j")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_true.yml new file mode 100644 index 00000000..5e1d91bc --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-keyset-copy_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_true/Main.java new file mode 100644 index 00000000..5f296097 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-keyset-copy_true/Main.java @@ -0,0 +1,18 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:j +// addAll of a map's key set (with a symbolic key) into a set. SAFE: two elements are left +// only if a is "j" or "k". +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Map m = new HashMap<>(); + m.put(a, 1); + m.put("k", 2); + Set s = new HashSet<>(); + s.add("j"); + s.addAll(m.keySet()); + if (s.size() == 2 && !a.equals("j") && !a.equals("k")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_false.yml new file mode 100644 index 00000000..230a2625 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-linked-order_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_false/Main.java new file mode 100644 index 00000000..d0a5ed1b --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_false/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:a +// A LinkedHashSet iterates in insertion order, also with symbolic elements. SAFE: "b" stays +// first. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Set s = new LinkedHashSet<>(); + s.add("b"); + s.add(a); + Iterator it = s.iterator(); + String first = it.next(); + if (it.hasNext() && it.next().equals("a")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_true.yml new file mode 100644 index 00000000..e7aad44c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-linked-order_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_true/Main.java new file mode 100644 index 00000000..96a9a2a1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-linked-order_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:a +// A LinkedHashSet iterates in insertion order, also with symbolic elements. SAFE: "b" stays +// first. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + Set s = new LinkedHashSet<>(); + s.add("b"); + s.add(a); + Iterator it = s.iterator(); + String first = it.next(); + if (!first.equals("b")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_false.yml new file mode 100644 index 00000000..9c86a630 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-list-changed_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_false/Main.java new file mode 100644 index 00000000..9d20cfd6 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_false/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:z +// A HashSet built from a list that a call SWAT does not model changed (remove(Object)): the +// list's elements are not known exactly, so neither are the set's. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + List l = new ArrayList<>(); + l.add("y"); + l.add("z"); + l.remove(a); + Set s = new HashSet<>(l); + if (s.size() == 1 && s.contains("y")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_true.yml new file mode 100644 index 00000000..0c253259 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-list-changed_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_true/Main.java new file mode 100644 index 00000000..46f9fc69 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-list-changed_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:z +// A HashSet built from a list that a call SWAT does not model changed (remove(Object)): the +// list's elements are not known exactly, so neither are the set's. Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + List l = new ArrayList<>(); + l.add("y"); + l.add("z"); + l.remove(a); + Set s = new HashSet<>(l); + if (s.size() == 1 && s.contains("y") && !a.equals("z")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_false.yml new file mode 100644 index 00000000..f5ead6a9 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-remove_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_false/Main.java new file mode 100644 index 00000000..7a2c3547 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_false/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:2 +// HashSet.remove after adding a symbolic Integer. SAFE: one element is left only if x was +// 1 or 2. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + Set s = new HashSet<>(); + s.add(1); + s.add(2); + s.add(x); + boolean removed = s.remove(2); + if (removed && s.size() == 1 && x != 1) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_true.yml new file mode 100644 index 00000000..9af2cc8d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - hashset-remove_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_true/Main.java new file mode 100644 index 00000000..65cec413 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/hashset-remove_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): int:2 +// HashSet.remove after adding a symbolic Integer. SAFE: one element is left only if x was +// 1 or 2. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + Set s = new HashSet<>(); + s.add(1); + s.add(2); + s.add(x); + boolean removed = s.remove(2); + if (removed && s.size() == 1 && x != 1 && x != 2) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_false.yml new file mode 100644 index 00000000..a0723d45 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-boxing_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_false/Main.java new file mode 100644 index 00000000..ea195164 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:1000 int:1 int:7 +import java.util.function.Function; +import org.sosy_lab.sv_benchmarks.Verifier; +// A capturing Function: the argument is boxed, unboxed in the body, and the result boxed. +public class Main { + public static void main(String[] args) { + int a = Verifier.nondetInt(); + int c = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + Function f = v -> v + c; + if (b == 7) { + int x = f.apply(a); + if (x > 1000 && x < 100000) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_true.yml new file mode 100644 index 00000000..de50522f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-boxing_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_true/Main.java new file mode 100644 index 00000000..b11e7617 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-boxing_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an integer x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): int:1000 int:1 int:7 +import java.util.function.Function; +import org.sosy_lab.sv_benchmarks.Verifier; +// A capturing Function: the argument is boxed, unboxed in the body, and the result boxed. +public class Main { + public static void main(String[] args) { + int a = Verifier.nondetInt(); + int c = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + Function f = v -> v + c; + if (b == 7) { + int x = f.apply(a); + if (x > 1000 && x < 1001) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_false.yml new file mode 100644 index 00000000..07ed180e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-capture-outcome_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_false/Main.java new file mode 100644 index 00000000..720e0d7f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:1001 int:0 int:7 +import java.util.function.IntSupplier; +import org.sosy_lab.sv_benchmarks.Verifier; +// The value a capturing lambda computes from two captured inputs (order matters) decides the assert. +public class Main { + public static void main(String[] args) { + int a = Verifier.nondetInt(); + int c = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + IntSupplier s = () -> a - 2 * c; + if (b == 7) { + int x = s.getAsInt(); + if (x > 1000 && x < 100000) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_true.yml new file mode 100644 index 00000000..a551b099 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-capture-outcome_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_true/Main.java new file mode 100644 index 00000000..42f32a77 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-capture-outcome_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an integer x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): int:1001 int:0 int:7 +import java.util.function.IntSupplier; +import org.sosy_lab.sv_benchmarks.Verifier; +// The value a capturing lambda computes from two captured inputs (order matters) decides the assert. +public class Main { + public static void main(String[] args) { + int a = Verifier.nondetInt(); + int c = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + IntSupplier s = () -> a - 2 * c; + if (b == 7) { + int x = s.getAsInt(); + if (x > 1000 && x < 1001) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_false.yml new file mode 100644 index 00000000..0806bf41 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-executor-capture_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_false/Main.java new file mode 100644 index 00000000..5c0969ca --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:1001 int:7 +import java.util.concurrent.Executor; +import org.sosy_lab.sv_benchmarks.Verifier; +// A captured input is used inside a lambda that an Executor (itself a lambda) runs as a Runnable. +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + Executor executor = command -> command.run(); + if (b == 7) { + executor.execute(() -> { + if (x > 1000 && x < 100000) assert false; + }); + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_true.yml new file mode 100644 index 00000000..8e640a4d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-executor-capture_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_true/Main.java new file mode 100644 index 00000000..320df749 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-executor-capture_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an integer x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): int:1001 int:7 +import java.util.concurrent.Executor; +import org.sosy_lab.sv_benchmarks.Verifier; +// A captured input is used inside a lambda that an Executor (itself a lambda) runs as a Runnable. +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + Executor executor = command -> command.run(); + if (b == 7) { + executor.execute(() -> { + if (x > 1000 && x < 1001) assert false; + }); + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_false.yml new file mode 100644 index 00000000..2324040e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-foreach-capture_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_false/Main.java new file mode 100644 index 00000000..8225deaa --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_false/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:1000 int:7 +import java.util.ArrayList; +import java.util.List; +import org.sosy_lab.sv_benchmarks.Verifier; +// A capturing lambda is called back by List.forEach (library code): must stay a context loss unless modelled. +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + List list = new ArrayList<>(); + list.add(1); + if (b == 7) { + list.forEach(v -> { + int s = x + v; + if (s > 1000 && s < 100000) assert false; + }); + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_true.yml new file mode 100644 index 00000000..7ac9ea79 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-foreach-capture_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_true/Main.java new file mode 100644 index 00000000..3796c74c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-foreach-capture_true/Main.java @@ -0,0 +1,20 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an integer x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): int:1000 int:7 +import java.util.ArrayList; +import java.util.List; +import org.sosy_lab.sv_benchmarks.Verifier; +// A capturing lambda is called back by List.forEach (library code): must stay a context loss unless modelled. +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + List list = new ArrayList<>(); + list.add(1); + if (b == 7) { + list.forEach(v -> { + int s = x + v; + if (s > 1000 && s < 1001) assert false; + }); + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_false.yml new file mode 100644 index 00000000..5d9d059c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-instance-capture_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_false/Main.java new file mode 100644 index 00000000..46dbb624 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_false/Main.java @@ -0,0 +1,22 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:1000 int:1 int:7 +import org.sosy_lab.sv_benchmarks.Verifier; +// An instance-capturing lambda (receiver captured first) reads a field and a captured local. +public class Main { + int f; + + void check(int y) { + Runnable r = () -> { + int x = f + y; + if (x > 1000 && x < 100000) assert false; + }; + r.run(); + } + + public static void main(String[] args) { + Main m = new Main(); + m.f = Verifier.nondetInt(); + int y = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + if (b == 7) m.check(y); + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_true.yml new file mode 100644 index 00000000..d0a293be --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-instance-capture_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_true/Main.java new file mode 100644 index 00000000..ba12c036 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-instance-capture_true/Main.java @@ -0,0 +1,23 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an integer x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): int:1000 int:1 int:7 +import org.sosy_lab.sv_benchmarks.Verifier; +// An instance-capturing lambda (receiver captured first) reads a field and a captured local. +public class Main { + int f; + + void check(int y) { + Runnable r = () -> { + int x = f + y; + if (x > 1000 && x < 1001) assert false; + }; + r.run(); + } + + public static void main(String[] args) { + Main m = new Main(); + m.f = Verifier.nondetInt(); + int y = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + if (b == 7) m.check(y); + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_false.yml new file mode 100644 index 00000000..a5ac1000 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-methodref-unboxing_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_false/Main.java new file mode 100644 index 00000000..df6a30bc --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_false/Main.java @@ -0,0 +1,22 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:1000 int:1 int:7 +import java.util.function.Function; +import org.sosy_lab.sv_benchmarks.Verifier; +// A bound method reference whose method takes an int, called as Function: LambdaMetafactory unboxes the argument (an adaptation SWAT does not model). +public class Main { + int c; + + int addTo(int v) { + int x = v + c; + if (x > 1000 && x < 100000) assert false; + return x; + } + + public static void main(String[] args) { + int a = Verifier.nondetInt(); + Main m = new Main(); + m.c = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + Function f = m::addTo; + if (b == 7) f.apply(a); + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_true.yml new file mode 100644 index 00000000..66453f02 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-methodref-unboxing_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_true/Main.java new file mode 100644 index 00000000..e5aa5f14 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-methodref-unboxing_true/Main.java @@ -0,0 +1,23 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an integer x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): int:1000 int:1 int:7 +import java.util.function.Function; +import org.sosy_lab.sv_benchmarks.Verifier; +// A bound method reference whose method takes an int, called as Function: LambdaMetafactory unboxes the argument (an adaptation SWAT does not model). +public class Main { + int c; + + int addTo(int v) { + int x = v + c; + if (x > 1000 && x < 1001) assert false; + return x; + } + + public static void main(String[] args) { + int a = Verifier.nondetInt(); + Main m = new Main(); + m.c = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + Function f = m::addTo; + if (b == 7) f.apply(a); + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_false.yml new file mode 100644 index 00000000..842e74d1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-sort-comparator_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_false/Main.java new file mode 100644 index 00000000..cf1f93ac --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_false/Main.java @@ -0,0 +1,21 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:1001 int:7 +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.sosy_lab.sv_benchmarks.Verifier; +// A capturing Comparator is called back by Collections.sort (library code). +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + List list = new ArrayList<>(); + list.add(2); + list.add(1); + if (b == 7) { + Collections.sort(list, (p, q) -> { + if (x > 1000 && x < 100000) assert false; + return p - q; + }); + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_true.yml new file mode 100644 index 00000000..1915f955 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-sort-comparator_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_true/Main.java new file mode 100644 index 00000000..b2f0ee94 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-sort-comparator_true/Main.java @@ -0,0 +1,22 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an integer x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): int:1001 int:7 +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.sosy_lab.sv_benchmarks.Verifier; +// A capturing Comparator is called back by Collections.sort (library code). +public class Main { + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + List list = new ArrayList<>(); + list.add(2); + list.add(1); + if (b == 7) { + Collections.sort(list, (p, q) -> { + if (x > 1000 && x < 1001) assert false; + return p - q; + }); + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_false.yml new file mode 100644 index 00000000..9a9c986a --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-thread-capture_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_false/Main.java new file mode 100644 index 00000000..d912735c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:1001 int:7 +import org.sosy_lab.sv_benchmarks.Verifier; +// A capturing lambda runs in another thread (Thread.start, then join). +public class Main { + public static void main(String[] args) throws InterruptedException { + int x = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + if (b == 7) { + Thread t = new Thread(() -> { + if (x > 1000 && x < 100000) assert false; + }); + t.start(); + t.join(); + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_true.yml new file mode 100644 index 00000000..df6ac071 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-thread-capture_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_true/Main.java new file mode 100644 index 00000000..98d0c263 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-thread-capture_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an integer x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): int:1001 int:7 +import org.sosy_lab.sv_benchmarks.Verifier; +// A capturing lambda runs in another thread (Thread.start, then join). +public class Main { + public static void main(String[] args) throws InterruptedException { + int x = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + if (b == 7) { + Thread t = new Thread(() -> { + if (x > 1000 && x < 1001) assert false; + }); + t.start(); + t.join(); + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_false.yml new file mode 100644 index 00000000..2a9b7299 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-unused-capture_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_false/Main.java new file mode 100644 index 00000000..ad81fa22 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): int:1001 int:7 +import java.util.function.IntSupplier; +import org.sosy_lab.sv_benchmarks.Verifier; +// A lambda captures an input but is never called (the NotificationCenter shape); the assert is outside it. +public class Main { + static IntSupplier make(int v) { + return () -> v + 1; + } + + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + IntSupplier unused = make(x); + if (b == 7 && x > 1000 && x < 100000) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_true.yml new file mode 100644 index 00000000..a956dfeb --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-unused-capture_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_true/Main.java new file mode 100644 index 00000000..8bc261a1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-unused-capture_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an integer x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): int:1001 int:7 +import java.util.function.IntSupplier; +import org.sosy_lab.sv_benchmarks.Verifier; +// A lambda captures an input but is never called (the NotificationCenter shape); the assert is outside it. +public class Main { + static IntSupplier make(int v) { + return () -> v + 1; + } + + public static void main(String[] args) { + int x = Verifier.nondetInt(); + int b = Verifier.nondetInt(); + IntSupplier unused = make(x); + if (b == 7 && x > 1000 && x < 1001) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_false.yml new file mode 100644 index 00000000..9e9fde01 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-wide-capture_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_false/Main.java new file mode 100644 index 00000000..c7d029c5 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): long:1001 double:2.5 int:7 +import java.util.function.LongSupplier; +import org.sosy_lab.sv_benchmarks.Verifier; +// A lambda captures a long and a double (two-slot values) besides an int. +public class Main { + public static void main(String[] args) { + long l = Verifier.nondetLong(); + double d = Verifier.nondetDouble(); + int b = Verifier.nondetInt(); + LongSupplier s = () -> (d > 2.0 && b == 7) ? l : 0L; + long x = s.getAsLong(); + if (x > 1000 && x < 100000) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_true.yml new file mode 100644 index 00000000..f8b4510e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - lambda-wide-capture_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_true/Main.java new file mode 100644 index 00000000..b03b09e1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/lambda-wide-capture_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an integer x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): long:1001 double:2.5 int:7 +import java.util.function.LongSupplier; +import org.sosy_lab.sv_benchmarks.Verifier; +// A lambda captures a long and a double (two-slot values) besides an int. +public class Main { + public static void main(String[] args) { + long l = Verifier.nondetLong(); + double d = Verifier.nondetDouble(); + int b = Verifier.nondetInt(); + LongSupplier s = () -> (d > 2.0 && b == 7) ? l : 0L; + long x = s.getAsLong(); + if (x > 1000 && x < 1001) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_false.yml new file mode 100644 index 00000000..cfc40e70 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - list-equals-changed_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_false/Main.java new file mode 100644 index 00000000..fd5451c6 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_false/Main.java @@ -0,0 +1,20 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:z +// equals of a list that library code SWAT does not model has changed (Collections.reverse): +// its elements are not known in order any more, and comparing the recorded ones puts +// a == "k" on the path instead of a == "z". Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + List x = new ArrayList<>(); + x.add(a); + x.add("k"); + Collections.reverse(x); + List y = new ArrayList<>(); + y.add("k"); + y.add("z"); + if (x.equals(y)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_true.yml new file mode 100644 index 00000000..83569444 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - list-equals-changed_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_true/Main.java new file mode 100644 index 00000000..8e5f430d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-changed_true/Main.java @@ -0,0 +1,20 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:z +// equals of a list that library code SWAT does not model has changed (Collections.reverse): +// its elements are not known in order any more, and comparing the recorded ones puts +// a == "k" on the path instead of a == "z". Must stay UNKNOWN. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + List x = new ArrayList<>(); + x.add(a); + x.add("k"); + Collections.reverse(x); + List y = new ArrayList<>(); + y.add("k"); + y.add("z"); + if (x.equals(y) && !a.equals("z")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_false.yml new file mode 100644 index 00000000..e55a38c2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - list-equals-size_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_false/Main.java new file mode 100644 index 00000000..64edbdb7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_false/Main.java @@ -0,0 +1,18 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:q string:q +// Objects.equals of two lists of different sizes: never equal, whatever the elements are. +// SAFE: the lists differ. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + List x = new ArrayList<>(); + x.add(a); + List y = new ArrayList<>(); + y.add(a); + y.add(b); + if (!Objects.equals(x, y) && a.equals("q") && x.equals(x)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_true.yml new file mode 100644 index 00000000..9017b29b --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - list-equals-size_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_true/Main.java new file mode 100644 index 00000000..398827fe --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals-size_true/Main.java @@ -0,0 +1,18 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:q string:q +// Objects.equals of two lists of different sizes: never equal, whatever the elements are. +// SAFE: the lists differ. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + List x = new ArrayList<>(); + x.add(a); + List y = new ArrayList<>(); + y.add(a); + y.add(b); + if (Objects.equals(x, y) || !x.equals(x)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_false.yml new file mode 100644 index 00000000..36c2a3f9 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - list-equals_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_false/Main.java new file mode 100644 index 00000000..b2d12e9f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_false/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:k string:m +// ArrayList.equals of two lists with symbolic String elements: equal iff the elements are, +// pair by pair. SAFE: only a == "k" and b == "m" make them equal. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + List x = new ArrayList<>(); + x.add(a); + x.add("m"); + List y = new ArrayList<>(); + y.add("k"); + y.add(b); + if (x.equals(y) && a.length() == 1) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_true.yml new file mode 100644 index 00000000..24e19678 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - list-equals_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_true/Main.java new file mode 100644 index 00000000..e718fe38 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/list-equals_true/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:k string:m +// ArrayList.equals of two lists with symbolic String elements: equal iff the elements are, +// pair by pair. SAFE: only a == "k" and b == "m" make them equal. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String a = Verifier.nondetString(); + String b = Verifier.nondetString(); + List x = new ArrayList<>(); + x.add(a); + x.add("m"); + List y = new ArrayList<>(); + y.add("k"); + y.add(b); + if (x.equals(y) && (!a.equals("k") || !b.equals("m"))) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-handler_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-handler_false.yml new file mode 100644 index 00000000..41390954 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-handler_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - logger-handler_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-handler_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-handler_false/Main.java new file mode 100644 index 00000000..1477dfa7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-handler_false/Main.java @@ -0,0 +1,23 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:boom +// A custom Handler (application code, run by Logger.warning) asserts on the message: an +// ignored or context-free Logger call must not hide it. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.util.logging.Handler; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +public class Main { + public static void main(String[] args) { + Logger logger = Logger.getLogger("swat.model-soundness"); + logger.setUseParentHandlers(false); + logger.addHandler(new Handler() { + public void publish(LogRecord r) { + if (r.getMessage().equals("boom")) assert false; + } + public void flush() {} + public void close() {} + }); + String s = Verifier.nondetString(); + logger.warning(s); + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-stream_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-stream_false.yml new file mode 100644 index 00000000..ad471d44 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-stream_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - logger-stream_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-stream_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-stream_false/Main.java new file mode 100644 index 00000000..9a88f588 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/logger-stream_false/Main.java @@ -0,0 +1,22 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:boom +// A JDK StreamHandler writes the message into a buffer that the program reads back: the +// Logger call stores the symbolic message. +import org.sosy_lab.sv_benchmarks.Verifier; +import java.io.ByteArrayOutputStream; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; +import java.util.logging.StreamHandler; + +public class Main { + public static void main(String[] args) { + Logger logger = Logger.getLogger("swat.model-soundness.stream"); + logger.setUseParentHandlers(false); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + StreamHandler handler = new StreamHandler(out, new SimpleFormatter()); + logger.addHandler(handler); + String s = Verifier.nondetString(); + logger.warning(s); + handler.flush(); + if (out.toString().contains("boom")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_false.yml new file mode 100644 index 00000000..00ded0a8 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-find_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_false/Main.java new file mode 100644 index 00000000..0ca214da --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:x12 +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// Matcher.find() is not modelled: context loss. A model that took it for matches() would hide +// the violation (a match inside the string). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]{2}").matcher(s); + if (m.find()) { + if (s.startsWith("x")) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_true.yml new file mode 100644 index 00000000..7822db4b --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-find_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_true/Main.java new file mode 100644 index 00000000..03ea1d10 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-find_true/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: SAFE. find() of [0-9]{2} in a string of one char fails. find() is not modelled: UNKNOWN. +// Paired VIOLATION inputs (must not fail here): string:x12 +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// Matcher.find() is not modelled: context loss. A model that took it for matches() would hide +// the violation (a match inside the string). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]{2}").matcher(s); + if (m.find()) { + if (s.length() == 1) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_false.yml new file mode 100644 index 00000000..342ce172 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-flags_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_false/Main.java new file mode 100644 index 00000000..dc78369c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:AB +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// A pattern with flags (CASE_INSENSITIVE): not modelled, context loss. A model that ignored the +// flags would hide the violation (an upper-case match). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Pattern p = Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE); + if (p.matcher(s).matches()) { + if (s.startsWith("A")) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_true.yml new file mode 100644 index 00000000..c029dfc0 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-flags_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_true/Main.java new file mode 100644 index 00000000..e7f67966 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-flags_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. with CASE_INSENSITIVE a match is letters only, so it has no digit "1". Flags are not modelled: UNKNOWN. +// Paired VIOLATION inputs (must not fail here): string:AB +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// A pattern with flags (CASE_INSENSITIVE): not modelled, context loss. A model that ignored the +// flags would hide the violation (an upper-case match). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Pattern p = Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE); + if (p.matcher(s).matches()) { + if (s.startsWith("1")) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_false.yml new file mode 100644 index 00000000..7bbe61e2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-matcher-aid_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_false/Main.java new file mode 100644 index 00000000..a116b908 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:0123456789# +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// The pattern of argv-mhe's AidGroup (escaped '*' and '#', a bounded repetition). +public class Main { + private static final Pattern AID = Pattern.compile("[0-9A-Fa-f]{10,32}\\*?\\#?"); + + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (AID.matcher(s).matches()) { + if (s.endsWith("#")) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_true.yml new file mode 100644 index 00000000..e3f93eb5 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-matcher-aid_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_true/Main.java new file mode 100644 index 00000000..64d21cf5 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-aid_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. a match of the pattern starts with at least 10 hex digits. +// Paired VIOLATION inputs (must not fail here): string:0123456789# +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// The pattern of argv-mhe's AidGroup (escaped '*' and '#', a bounded repetition). +public class Main { + private static final Pattern AID = Pattern.compile("[0-9A-Fa-f]{10,32}\\*?\\#?"); + + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (AID.matcher(s).matches()) { + if (s.length() < 10) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_false.yml new file mode 100644 index 00000000..4f88ecd7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-matcher-match_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_false/Main.java new file mode 100644 index 00000000..4810a812 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:12 +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// The matching side of Matcher.matches() on a symbolic string, the pattern in a static field. +public class Main { + private static final Pattern DIGITS = Pattern.compile("[0-9]+"); + + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (DIGITS.matcher(s).matches()) { + if (s.startsWith("1")) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_true.yml new file mode 100644 index 00000000..1fb725a7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-matcher-match_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_true/Main.java new file mode 100644 index 00000000..7c8563e8 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-match_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. a string of digits only does not start with "x". +// Paired VIOLATION inputs (must not fail here): string:12 +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// The matching side of Matcher.matches() on a symbolic string, the pattern in a static field. +public class Main { + private static final Pattern DIGITS = Pattern.compile("[0-9]+"); + + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (DIGITS.matcher(s).matches()) { + if (s.startsWith("x")) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_false.yml new file mode 100644 index 00000000..cbf6888d --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-matcher-nomatch_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_false/Main.java new file mode 100644 index 00000000..332557d6 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_false/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:1a +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// The non-matching side of Matcher.matches() on a symbolic string. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Pattern p = Pattern.compile("[0-9]{2}"); + Matcher m = p.matcher(s); + if (!m.matches() && s.length() == 2 && s.charAt(0) >= '0' && s.charAt(0) <= '9') { + if (s.charAt(1) == 'a') assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_true.yml new file mode 100644 index 00000000..4edaf84c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-matcher-nomatch_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_true/Main.java new file mode 100644 index 00000000..b6261994 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-nomatch_true/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: SAFE. a string of two chars that are both digits matches [0-9]{2}. +// Paired VIOLATION inputs (must not fail here): string:1a +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// The non-matching side of Matcher.matches() on a symbolic string. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Pattern p = Pattern.compile("[0-9]{2}"); + Matcher m = p.matcher(s); + if (!m.matches() && s.length() == 2 && s.charAt(0) >= '0' && s.charAt(0) <= '9') { + if (s.charAt(1) >= '0' && s.charAt(1) <= '9') assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_false.yml new file mode 100644 index 00000000..95c93053 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-matcher-twice_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_false/Main.java new file mode 100644 index 00000000..e1cd1c26 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:7 +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// matches() called twice on one matcher (it does not depend on the earlier match). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]").matcher(s); + boolean first = m.matches(); + boolean second = m.matches(); + if (first && second) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_true.yml new file mode 100644 index 00000000..18fedde1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-matcher-twice_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_true/Main.java new file mode 100644 index 00000000..3504fc54 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-matcher-twice_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. matches() gives the same answer twice on an unchanged matcher. +// Paired VIOLATION inputs (must not fail here): string:7 +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// matches() called twice on one matcher (it does not depend on the earlier match). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]").matcher(s); + boolean first = m.matches(); + boolean second = m.matches(); + if (first != second) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_false.yml new file mode 100644 index 00000000..853f2d98 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-pattern-matches_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_false/Main.java new file mode 100644 index 00000000..640ab500 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_false/Main.java @@ -0,0 +1,12 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:ab +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// The static Pattern.matches(regex, s), which is s.matches(regex). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (Pattern.matches("[a-z]+", s)) { + if (s.startsWith("a")) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_true.yml new file mode 100644 index 00000000..e7e33668 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-pattern-matches_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_true/Main.java new file mode 100644 index 00000000..b5a9f1a3 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-pattern-matches_true/Main.java @@ -0,0 +1,13 @@ +// Expected verdict: SAFE. a string of digits only does not start with "a". +// Paired VIOLATION inputs (must not fail here): string:ab +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// The static Pattern.matches(regex, s), which is s.matches(regex). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (Pattern.matches("[0-9]+", s)) { + if (s.startsWith("a")) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_false.yml new file mode 100644 index 00000000..efc4cf7a --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-reset-concrete_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_false/Main.java new file mode 100644 index 00000000..64e64b52 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_false/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:a +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// A matcher of a symbolic string reset to a concrete one: not modelled, context loss. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]+").matcher(s); + boolean first = m.matches(); + m.reset("123"); + boolean second = m.matches(); + if (!first && s.length() == 1) { + if (second) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_true.yml new file mode 100644 index 00000000..5f349590 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-reset-concrete_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_true/Main.java new file mode 100644 index 00000000..5af471d2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset-concrete_true/Main.java @@ -0,0 +1,18 @@ +// Expected verdict: SAFE. after reset("123"), matches() is true, so the assert needs !true. reset is not modelled: UNKNOWN. +// Paired VIOLATION inputs (must not fail here): string:a +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// A matcher of a symbolic string reset to a concrete one: not modelled, context loss. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]+").matcher(s); + boolean first = m.matches(); + m.reset("123"); + boolean second = m.matches(); + if (!first && s.length() == 1) { + if (!second) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_false.yml new file mode 100644 index 00000000..648f7bdf --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-reset_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_false/Main.java new file mode 100644 index 00000000..b2d05909 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_false/Main.java @@ -0,0 +1,19 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:a string:1 +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// A matcher reused on another input with reset(t): not modelled, context loss. A model that kept +// matching s would hide the violation (s does not match, t does). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + String t = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]+").matcher(s); + boolean first = m.matches(); + m.reset(t); + boolean second = m.matches(); + if (!first && second) { + if (t.length() == 1) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_true.yml new file mode 100644 index 00000000..699b6886 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-reset_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_true/Main.java new file mode 100644 index 00000000..664d6b6f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-reset_true/Main.java @@ -0,0 +1,20 @@ +// Expected verdict: SAFE. after reset(t), matches() of [0-9]+ is about t, which is a digit string and does not start with "x". reset is not modelled: UNKNOWN. +// Paired VIOLATION inputs (must not fail here): string:a string:1 +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// A matcher reused on another input with reset(t): not modelled, context loss. A model that kept +// matching s would hide the violation (s does not match, t does). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + String t = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]+").matcher(s); + boolean first = m.matches(); + m.reset(t); + boolean second = m.matches(); + if (!first && second) { + if (t.startsWith("x")) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_false.yml new file mode 100644 index 00000000..7352732b --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-untranslatable_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_false/Main.java new file mode 100644 index 00000000..e606c6d7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_false/Main.java @@ -0,0 +1,12 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:abc +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// A regular expression SWAT does not translate ("."): context loss, SAFE must not be claimed. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (Pattern.compile("a.c").matcher(s).matches()) { + if (s.length() == 3) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_true.yml new file mode 100644 index 00000000..3902cc57 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - regex-untranslatable_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_true/Main.java new file mode 100644 index 00000000..b45bf2d1 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/regex-untranslatable_true/Main.java @@ -0,0 +1,13 @@ +// Expected verdict: SAFE. a match of a.c has three chars. "." is not translated: SWAT must say UNKNOWN. +// Paired VIOLATION inputs (must not fail here): string:abc +import java.util.regex.Pattern; +import org.sosy_lab.sv_benchmarks.Verifier; +// A regular expression SWAT does not translate ("."): context loss, SAFE must not be claimed. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (Pattern.compile("a.c").matcher(s).matches()) { + if (s.length() != 3) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_false.yml new file mode 100644 index 00000000..acf0370e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - sb-equals-identity_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_false/Main.java new file mode 100644 index 00000000..079a9b54 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:ab boolean:true +// StringBuilder.equals is identity: the same builder equals itself (f true), another one with +// the same chars does not. +import org.sosy_lab.sv_benchmarks.Verifier; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + boolean f = Verifier.nondetBoolean(); + StringBuilder sb = new StringBuilder(s); + StringBuilder other = f ? sb : new StringBuilder(s); + if (sb.equals(other) && s.length() == 2) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_true.yml new file mode 100644 index 00000000..ecbd5f32 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - sb-equals-identity_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_true/Main.java new file mode 100644 index 00000000..4f852781 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-identity_true/Main.java @@ -0,0 +1,13 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:ab boolean:true +// StringBuffer.equals is identity: a different buffer with the same chars is never equal. +import org.sosy_lab.sv_benchmarks.Verifier; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + boolean f = Verifier.nondetBoolean(); + StringBuffer sb = new StringBuffer(s); + StringBuffer other = new StringBuffer(s); + if (f && sb.equals(other) && s.length() == 2) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_false.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_false.yml new file mode 100644 index 00000000..919e34e4 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - sb-equals-string_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_false/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_false/Main.java new file mode 100644 index 00000000..839506e2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_false/Main.java @@ -0,0 +1,11 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:ab +// Sanity pair: the builder holds the same chars as the String. +import org.sosy_lab.sv_benchmarks.Verifier; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + StringBuilder sb = new StringBuilder(s); + if (!sb.equals(s) && sb.toString().equals("ab")) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_true.yml b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_true.yml new file mode 100644 index 00000000..a862bacd --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - sb-equals-string_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_true/Main.java b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_true/Main.java new file mode 100644 index 00000000..d6f428f7 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/model-soundness/sb-equals-string_true/Main.java @@ -0,0 +1,12 @@ +// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): string:ab +// StringBuilder does not override equals: a builder never equals a String, whatever its +// chars (jbmc-regression/StringBuilderConstructors02). +import org.sosy_lab.sv_benchmarks.Verifier; + +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + StringBuilder sb = new StringBuilder(s); + if (sb.equals(s)) assert false; + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_false.yml b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_false.yml new file mode 100644 index 00000000..8e162850 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - explore-given-up-tochararray-contains_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_false/Main.java b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_false/Main.java new file mode 100644 index 00000000..6ff5a0a0 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_false/Main.java @@ -0,0 +1,14 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string: int:1001 +import org.sosy_lab.sv_benchmarks.Verifier; +// As explore-given-up-tochararray-len, but the length pin is only implied: s.contains() needs +// six chars or more, a length the pinned runs never have. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + int a = Verifier.nondetInt(); + char[] c = s.toCharArray(); + if (s.contains("")) { + if (a > 1000 && a < 100000) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_true.yml b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_true.yml new file mode 100644 index 00000000..245bf98c --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - explore-given-up-tochararray-contains_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_true/Main.java b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_true/Main.java new file mode 100644 index 00000000..adef867e --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-contains_true/Main.java @@ -0,0 +1,15 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an int x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): string: int:1001 +import org.sosy_lab.sv_benchmarks.Verifier; +// As explore-given-up-tochararray-len, but the length pin is only implied: s.contains() needs +// six chars or more, a length the pinned runs never have. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + int a = Verifier.nondetInt(); + char[] c = s.toCharArray(); + if (s.contains("")) { + if (a > 1000 && a < 1001) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_false.yml b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_false.yml new file mode 100644 index 00000000..eb988149 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_false.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - explore-given-up-tochararray-len_false/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: false +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_false/Main.java b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_false/Main.java new file mode 100644 index 00000000..103871bb --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_false/Main.java @@ -0,0 +1,16 @@ +// Expected verdict: VIOLATION. Violating inputs (nondet call order): string:abcdefg int:1001 +import org.sosy_lab.sv_benchmarks.Verifier; +// toCharArray() pins the length of s (the array's length is concrete), and every run of a +// solution for the pin's other side diverges, so the explorer gives the pin up. A given-up branch +// rules out SAFE unless some run reaches its side; the branch's own solved marker counted as that +// (false SAFE before fix/given-up-self-coverage). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + int a = Verifier.nondetInt(); + char[] c = s.toCharArray(); + if (c.length == 7) { + if (a > 1000 && a < 100000) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_true.yml b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_true.yml new file mode 100644 index 00000000..bc8b5fd2 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_true.yml @@ -0,0 +1,9 @@ +format_version: '2.0' +input_files: + - ../common/ + - explore-given-up-tochararray-len_true/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: true +options: + language: Java diff --git a/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_true/Main.java b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_true/Main.java new file mode 100644 index 00000000..b2c5d5ca --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/java/sa-soundness/explore-given-up-tochararray-len_true/Main.java @@ -0,0 +1,17 @@ +// Expected verdict: SAFE. the assert needs x > 1000 && x < 1001 for an int x: unsatisfiable. +// Paired VIOLATION inputs (must not fail here): string:abcdefg int:1001 +import org.sosy_lab.sv_benchmarks.Verifier; +// toCharArray() pins the length of s (the array's length is concrete), and every run of a +// solution for the pin's other side diverges, so the explorer gives the pin up. A given-up branch +// rules out SAFE unless some run reaches its side; the branch's own solved marker counted as that +// (false SAFE before fix/given-up-self-coverage). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + int a = Verifier.nondetInt(); + char[] c = s.toCharArray(); + if (c.length == 7) { + if (a > 1000 && a < 1001) assert false; + } + } +} diff --git a/targets/sv-comp/swat-benchmarks/scripts/gen.py b/targets/sv-comp/swat-benchmarks/scripts/gen.py index 71e1c640..26f91cee 100755 --- a/targets/sv-comp/swat-benchmarks/scripts/gen.py +++ b/targets/sv-comp/swat-benchmarks/scripts/gen.py @@ -16,12 +16,14 @@ from tools import all_tool_tasks from relevance import all_relevance_tasks from loopsummary import all_loop_summary_tasks +from modelsoundness import all_model_soundness_tasks JAVA_DIR = Path(__file__).resolve().parent.parent / 'java' def main(suites): - tasks = [*all_family_tasks(), *all_soundness_tasks(), *all_tool_tasks(), *all_relevance_tasks(), *all_loop_summary_tasks()] + tasks = [*all_family_tasks(), *all_soundness_tasks(), *all_tool_tasks(), *all_relevance_tasks(), *all_loop_summary_tasks(), + *all_model_soundness_tasks()] if suites: tasks = [t for t in tasks if t.suite in suites] for suite in sorted({t.suite for t in tasks}): diff --git a/targets/sv-comp/swat-benchmarks/scripts/model_soundness_arrays.py b/targets/sv-comp/swat-benchmarks/scripts/model_soundness_arrays.py new file mode 100755 index 00000000..a0e79293 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/scripts/model_soundness_arrays.py @@ -0,0 +1,241 @@ +#!/usr/bin/env python3 +"""Writes the model-soundness tasks for the Arrays.equals/hashCode, StringBuilder.equals and Logger +models (java/model-soundness/arrays-*, sb-equals-*, logger-*). SAFE/VIOLATION pairs: a wrong or +too-eager model turns a VIOLATION task into a false SAFE; the `_true` tasks are SAFE only if the +model is exact, except those marked "must stay UNKNOWN", which no exact model can prove. + + ./model_soundness_arrays.py (then ./build.sh model-soundness; ./check_labels.py model-soundness) +""" +from pathlib import Path + +SUITE = Path(__file__).resolve().parent.parent / 'java' / 'model-soundness' + +YML = """format_version: '2.0' +input_files: + - ../common/ + - {name}/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: {verdict} +options: + language: Java +""" + + +def task(name, comment, inputs, imports, body, extra=''): + safe = name.endswith('_true') + head = ('// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): ' if safe + else '// Expected verdict: VIOLATION. Violating inputs (nondet call order): ') + inputs + lines = [head] + [f'// {c}' for c in comment.strip().splitlines()] + src = '\n'.join(lines) + '\n' + src += ''.join(f'import {i};\n' for i in ['org.sosy_lab.sv_benchmarks.Verifier'] + imports) + src += '\npublic class Main {\n' + extra + ' public static void main(String[] args) {\n' + src += ''.join(f' {l}\n' for l in body.strip().splitlines()) + src += ' }\n}\n' + d = SUITE / name + d.mkdir(parents=True, exist_ok=True) + (d / 'Main.java').write_text(src) + (SUITE / f'{name}.yml').write_text(YML.format(name=name, verdict='true' if safe else 'false')) + + +def main(): + A = ['java.util.Arrays'] + # ---- Arrays.equals + for v, b1 in (('false', '5'), ('true', '6')): + task(f'arrays-equals-int_{v}', + 'Arrays.equals(int[], int[]): equal iff every element is. The SAFE variant differs in a\n' + 'concrete element (5 vs 6), so no x, y make the arrays equal.', + 'int:3 int:7', A, f""" +int x = Verifier.nondetInt(); +int y = Verifier.nondetInt(); +int[] a = {{x, 5, 7}}; +int[] b = {{3, {b1}, y}}; +if (Arrays.equals(a, b)) assert false;""") + for v, b1 in (('false', '255'), ('true', '254')): + task(f'arrays-equals-byte_{v}', + 'Arrays.equals(byte[], byte[]) with sign-extended elements: (byte) 255 == -1. The SAFE\n' + 'variant has (byte) 254 == -2 against -1.', + 'byte:-128', A, f""" +byte x = Verifier.nondetByte(); +byte[] a = {{x, (byte) -1}}; +byte[] b = {{(byte) -128, (byte) {b1}}}; +if (Arrays.equals(a, b)) assert false;""") + task('arrays-equals-mixed_false', + 'Arrays.equals for char[], long[] and boolean[]; the long must be 2^32 (its upper half).', + 'char:Z long:4294967296 boolean:true', A, """ +char c = Verifier.nondetChar(); +long v = Verifier.nondetLong(); +boolean z = Verifier.nondetBoolean(); +if (Arrays.equals(new char[] {c}, new char[] {'Z'}) && Arrays.equals(new long[] {v}, new long[] {4294967296L}) + && Arrays.equals(new boolean[] {z}, new boolean[] {true})) assert false;""") + task('arrays-equals-mixed_true', + 'As arrays-equals-mixed_false, but the low half of the long must also be 1: long equality is\n' + '64-bit, so v == 2^32 has (int) v == 0 and the assert is unreachable.', + 'char:Z long:4294967296 boolean:true', A, """ +char c = Verifier.nondetChar(); +long v = Verifier.nondetLong(); +boolean z = Verifier.nondetBoolean(); +if (Arrays.equals(new char[] {c}, new char[] {'Z'}) && Arrays.equals(new long[] {v}, new long[] {4294967296L}) + && Arrays.equals(new boolean[] {z}, new boolean[] {true}) && (int) v == 1) assert false;""") + for v, box in (('false', 'Integer.valueOf(5)'), ('true', 'Long.valueOf(5)')): + task(f'arrays-equals-obj_{v}', + 'Arrays.equals(Object[], Object[]) over String, Integer and null elements. In the SAFE\n' + 'variant the second element is a Long: Integer.equals(Long) is false for every value.', + 'string:ab int:5', A, f""" +String s = Verifier.nondetString(); +int x = Verifier.nondetInt(); +Object[] a = {{s, Integer.valueOf(x), null}}; +Object[] b = {{"ab", {box}, null}}; +if (Arrays.equals(a, b)) assert false;""") + task('arrays-equals-symlen_false', + 'Arrays.equals with an array of symbolic length (not modelled: stays a context loss).', + 'int:2', A, """ +int n = Verifier.nondetInt(); +if (n < 0 || n > 3) return; +int[] a = new int[n]; +int[] b = new int[2]; +if (Arrays.equals(a, b)) assert false;""") + task('arrays-equals-symlen_true', + 'Must stay UNKNOWN: Arrays.equals with an array of symbolic length is not modelled. (SAFE:\n' + 'equal arrays have equal lengths, so n == 2.)', + 'int:2', A, """ +int n = Verifier.nondetInt(); +if (n < 0 || n > 3) return; +int[] a = new int[n]; +int[] b = new int[2]; +if (Arrays.equals(a, b) && n != 2) assert false;""") + # ---- Arrays.hashCode + task('arrays-hash-overflow_false', + 'Arrays.hashCode(int[]) = 31 * (31 + x) + 1 wraps around: x = 69273666 > 0 gives a\n' + 'negative hash (an unbounded-integer model would say it cannot).', + 'int:69273666', A, """ +int x = Verifier.nondetInt(); +int h = Arrays.hashCode(new int[] {x, 1}); +if (x > 0 && h < 0) assert false;""") + task('arrays-hash-overflow_true', + 'For 0 < x < 69273635, 962 + 31 * x <= 2147483616 does not overflow, so the hash stays\n' + 'positive.', + 'int:69273666', A, """ +int x = Verifier.nondetInt(); +int h = Arrays.hashCode(new int[] {x, 1}); +if (x > 0 && x < 69273635 && h < 0) assert false;""") + task('arrays-hash-long_false', + 'Arrays.hashCode(long[]): Long.hashCode(v) = (int) (v ^ (v >>> 32)); v = 2^32 hashes to 1,\n' + 'so h == 32 although (int) v == 0 (a model using (int) v would say SAFE).', + 'long:4294967296', A, """ +long v = Verifier.nondetLong(); +int h = Arrays.hashCode(new long[] {v}); +if (h == 32 && (int) v == 0) assert false;""") + task('arrays-hash-long_true', + 'v with (int) v == 0 and v >>> 32 == 1 is 2^32, whose hash is 31 + 1 = 32, not 31.', + 'long:4294967296', A, """ +long v = Verifier.nondetLong(); +int h = Arrays.hashCode(new long[] {v}); +if (h == 31 && (int) v == 0 && (v >>> 32) == 1) assert false;""") + task('arrays-hash-boolean_false', + 'Arrays.hashCode(boolean[]): Boolean.hashCode is 1231 for true, 1237 for false.', + 'boolean:true', A, """ +boolean z = Verifier.nondetBoolean(); +int h = Arrays.hashCode(new boolean[] {z}); +if (h == 1262) assert false;""") + task('arrays-hash-boolean_true', + 'The hash of a one-element boolean[] is 31 + 1231 or 31 + 1237, nothing else.', + 'boolean:true', A, """ +boolean z = Verifier.nondetBoolean(); +int h = Arrays.hashCode(new boolean[] {z}); +if (h != 1262 && h != 1268) assert false;""") + O = ['java.util.Objects'] + task('arrays-hash-boxed_false', + 'Objects.hash(Integer, Boolean, null, Long) is Arrays.hashCode(Object[]): 2255468 for\n' + 'x = 5, z = true, v = 2^32.', + 'int:5 boolean:true long:4294967296', O, """ +int x = Verifier.nondetInt(); +boolean z = Verifier.nondetBoolean(); +long v = Verifier.nondetLong(); +if (Objects.hash(x, z, null, v) == 2255468) assert false;""") + task('arrays-hash-boxed_true', + 'With z = false, x = 5 and v = 2^32 fixed the hash is 2261234 (1237 instead of 1231),\n' + 'never 2255468.', + 'int:5 boolean:true long:4294967296', O, """ +int x = Verifier.nondetInt(); +boolean z = Verifier.nondetBoolean(); +long v = Verifier.nondetLong(); +if (!z && x == 5 && v == 4294967296L && Objects.hash(x, z, null, v) == 2255468) assert false;""") + task('arrays-hash-string_false', + 'Arrays.hashCode(Object[]) with a String element (String.hashCode is not modelled: stays a\n' + 'context loss). {"a"} hashes to 31 + 97.', + 'string:a', A, """ +String s = Verifier.nondetString(); +if (Arrays.hashCode(new Object[] {s}) == 128) assert false;""") + task('arrays-hash-string_true', + 'Must stay UNKNOWN (String.hashCode is not modelled). SAFE: a one-char string hashing to\n' + '97 is "a".', + 'string:a', A, """ +String s = Verifier.nondetString(); +if (Arrays.hashCode(new Object[] {s}) == 128 && s.length() == 1 && !s.equals("a")) assert false;""") + # ---- StringBuilder/StringBuffer.equals is identity + task('sb-equals-string_true', + 'StringBuilder does not override equals: a builder never equals a String, whatever its\n' + 'chars (jbmc-regression/StringBuilderConstructors02).', + 'string:ab', [], """ +String s = Verifier.nondetString(); +StringBuilder sb = new StringBuilder(s); +if (sb.equals(s)) assert false;""") + task('sb-equals-string_false', + 'Sanity pair: the builder holds the same chars as the String.', + 'string:ab', [], """ +String s = Verifier.nondetString(); +StringBuilder sb = new StringBuilder(s); +if (!sb.equals(s) && sb.toString().equals("ab")) assert false;""") + task('sb-equals-identity_false', + 'StringBuilder.equals is identity: the same builder equals itself (f true), another one with\n' + 'the same chars does not.', + 'string:ab boolean:true', [], """ +String s = Verifier.nondetString(); +boolean f = Verifier.nondetBoolean(); +StringBuilder sb = new StringBuilder(s); +StringBuilder other = f ? sb : new StringBuilder(s); +if (sb.equals(other) && s.length() == 2) assert false;""") + task('sb-equals-identity_true', + 'StringBuffer.equals is identity: a different buffer with the same chars is never equal.', + 'string:ab boolean:true', [], """ +String s = Verifier.nondetString(); +boolean f = Verifier.nondetBoolean(); +StringBuffer sb = new StringBuffer(s); +StringBuffer other = new StringBuffer(s); +if (f && sb.equals(other) && s.length() == 2) assert false;""") + # ---- Logger sinks with application code or readable output behind them + L = ['java.util.logging.Handler', 'java.util.logging.LogRecord', 'java.util.logging.Logger'] + task('logger-handler_false', + 'A custom Handler (application code, run by Logger.warning) asserts on the message: an\n' + 'ignored or context-free Logger call must not hide it.', + 'string:boom', L, """ +Logger logger = Logger.getLogger("swat.model-soundness"); +logger.setUseParentHandlers(false); +logger.addHandler(new Handler() { + public void publish(LogRecord r) { + if (r.getMessage().equals("boom")) assert false; + } + public void flush() {} + public void close() {} +}); +String s = Verifier.nondetString(); +logger.warning(s);""") + task('logger-stream_false', + 'A JDK StreamHandler writes the message into a buffer that the program reads back: the\n' + 'Logger call stores the symbolic message.', + 'string:boom', ['java.io.ByteArrayOutputStream', 'java.util.logging.Logger', + 'java.util.logging.SimpleFormatter', 'java.util.logging.StreamHandler'], """ +Logger logger = Logger.getLogger("swat.model-soundness.stream"); +logger.setUseParentHandlers(false); +ByteArrayOutputStream out = new ByteArrayOutputStream(); +StreamHandler handler = new StreamHandler(out, new SimpleFormatter()); +logger.addHandler(handler); +String s = Verifier.nondetString(); +logger.warning(s); +handler.flush(); +if (out.toString().contains("boom")) assert false;""") + + +if __name__ == '__main__': + main() diff --git a/targets/sv-comp/swat-benchmarks/scripts/model_soundness_hash.py b/targets/sv-comp/swat-benchmarks/scripts/model_soundness_hash.py new file mode 100755 index 00000000..b98ed887 --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/scripts/model_soundness_hash.py @@ -0,0 +1,610 @@ +#!/usr/bin/env python3 +"""Writes the model-soundness tasks for the exact model of hash containers with symbolic keys +(java/model-soundness/hash-*, hashset-*, emptymap-*, list-equals*): HashMap, LinkedHashMap, ConcurrentHashMap, +HashSet, LinkedHashSet with +String and boxed integral keys, lookups as equality chains over the stored keys. + +SAFE/VIOLATION pairs: a wrong or too-eager model turns a VIOLATION task into a false SAFE. A `_true` +task is SAFE only if the model is exact; those marked "must stay UNKNOWN" use something the model +must not cover (iteration order of a plain hash container, keys with their own equals, Double keys, +changes through a view or by library code), where SAFE on the `_false` twin would be a false SAFE. + + ./model_soundness_hash.py (then ./build.sh model-soundness; ./check_labels.py model-soundness) +""" +from pathlib import Path + +SUITE = Path(__file__).resolve().parent.parent / 'java' / 'model-soundness' + +YML = """format_version: '2.0' +input_files: + - ../common/ + - {name}/ +properties: + - property_file: ../properties/valid-assert.prp + expected_verdict: {verdict} +options: + language: Java +""" + + +def task(name, comment, inputs, body, extra=''): + safe = name.endswith('_true') + head = ('// Expected verdict: SAFE. Paired VIOLATION inputs (must not fail here): ' if safe + else '// Expected verdict: VIOLATION. Violating inputs (nondet call order): ') + inputs + lines = [head] + [f'// {c}' for c in comment.strip().splitlines()] + src = '\n'.join(lines) + '\n' + src += 'import org.sosy_lab.sv_benchmarks.Verifier;\nimport java.util.*;\n' + src += '\npublic class Main {\n' + extra + ' public static void main(String[] args) {\n' + src += ''.join(f' {l}\n' for l in body.strip().splitlines()) + src += ' }\n}\n' + d = SUITE / name + d.mkdir(parents=True, exist_ok=True) + (d / 'Main.java').write_text(src) + (SUITE / f'{name}.yml').write_text(YML.format(name=name, verdict='true' if safe else 'false')) + + +def pair(name, comment, inputs, body, false_cond, true_cond, extra=''): + """A VIOLATION and a SAFE task that differ only in the condition before `assert false`.""" + for verdict, cond in (('false', false_cond), ('true', true_cond)): + task(f'{name}_{verdict}', comment, inputs, body.strip() + f'\nif ({cond}) assert false;', extra) + + +def main(): + pair('hash-collide', + 'Two symbolic keys that may or may not be equal: put(a), put(b) leaves one entry iff\n' + 'a.equals(b), and then get(a) is the second value. SAFE: with one entry, get(a) is 2.', + 'string:k string:k', """ +String a = Verifier.nondetString(); +String b = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put(a, 1); +m.put(b, 2);""", + 'a.equals("k") && m.size() == 1 && m.get(a) == 2', + 'a.equals("k") && m.size() == 1 && m.get(a) != 2') + pair('hash-overwrite', + 'put with a symbolic key overwrites the entry of an equal concrete key; get of the concrete\n' + 'key then returns the new value. SAFE: only the key "key" overwrites it.', + 'string:key', """ +String k = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put("key", "old"); +String before = m.put(k, "new");""", + 'm.get("key").equals("new") && before != null', + 'm.get("key").equals("new") && !k.equals("key")') + pair('hash-remove-size', + 'remove with a symbolic Integer key, then size and containsKey. SAFE: only x == 3 removes\n' + 'the entry of 3.', + 'int:3', """ +int x = Verifier.nondetInt(); +Map m = new HashMap<>(); +m.put(1, "a"); +m.put(2, "b"); +m.put(3, "c"); +String removed = m.remove(x);""", + 'm.size() == 2 && !m.containsKey(3) && removed.equals("c")', + 'm.size() == 2 && !m.containsKey(3) && x != 3') + pair('hash-contains-missing', + 'containsKey of a symbolic key the map does not hold: the path must say it differs from\n' + 'every stored key. SAFE: a one-char string in [a, b] is a key.', + 'string:c', """ +String s = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put("a", 1); +m.put("b", 2);""", + "!m.containsKey(s) && s.length() == 1 && s.charAt(0) >= 'a' && s.charAt(0) <= 'c'", + "!m.containsKey(s) && s.length() == 1 && s.charAt(0) >= 'a' && s.charAt(0) <= 'b'") + for v, box, cond in (('false', 'Integer', 'm.size() == 1'), ('true', 'Long', 'm.size() == 1 || !"int".equals(m.get(5))')): + task(f'hash-int-long_{v}', + 'Integer and Long keys: an Integer never equals a Long of the same value. The VIOLATION\n' + 'variant puts Integer.valueOf(x), which replaces the entry of 5 for x == 5; the SAFE one\n' + 'puts Long.valueOf(x), which is always a second entry.', + 'int:5', f""" +int x = Verifier.nondetInt(); +Map m = new HashMap<>(); +m.put(Integer.valueOf(5), "int"); +m.put({box}.valueOf(x), "other"); +if ({cond}) assert false;""") + pair('hash-null-key', + 'The null key next to a symbolic String key: the String "null" is not the null key, and a\n' + 'nondet String is never null. SAFE: the null entry is never replaced.', + 'string:null', """ +String s = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put(null, "n"); +m.put(s, "s");""", + 'm.size() == 2 && m.get(null).equals("n") && m.containsKey("null")', + 'm.size() != 2 || !m.get(null).equals("n")') + pair('hash-iterate', + 'Iteration of a HashMap with a symbolic key: the order depends on the key\'s hash code\n' + '("b" comes before "c" but after "a" and ""), which no branch on the path says. Must stay\n' + 'UNKNOWN: an order taken as fixed makes the VIOLATION task a false SAFE.', + 'string:c', """ +String a = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put("b", 1); +m.put(a, 2); +String first = m.keySet().iterator().next();""", + 'first.equals("b") && a.equals("c")', + 'first.equals("b") && a.equals("a")') + pair('hash-iterate-values', + 'As hash-iterate, over values(): their order is the keys\' hash order too. Must stay UNKNOWN.', + 'string:c', """ +String a = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put("b", 1); +m.put(a, 2); +int first = m.values().iterator().next();""", + 'first == 1 && a.equals("c")', + 'first == 1 && a.equals("a")') + pair('hash-iterate-order', + 'The first value of a HashMap is the symbolic key\'s only if that key hashes before "b": true\n' + 'for "" and "p" (table index 0), false for "c" (index 3 after 2). No branch says so: a search\n' + 'that takes the order of one run as fixed sees first == 1 for every one-char key after \'b\'\n' + 'and reports the VIOLATION task SAFE. Must stay UNKNOWN (SAFE variant: \'c\' to \'o\' all\n' + 'come after "b").', + 'string:p', """ +String a = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put("b", 1); +m.put(a, 2); +int first = m.values().iterator().next();""", + "first == 2 && a.length() == 1 && a.charAt(0) > 'b'", + "first == 2 && a.length() == 1 && a.charAt(0) > 'b' && a.charAt(0) < 'p'") + pair('hashset-iterate-copy', + 'A HashSet with a symbolic element copied into a list: the list\'s order is the hash order.\n' + 'Must stay UNKNOWN.', + 'string:c', """ +String a = Verifier.nondetString(); +Set s = new HashSet<>(); +s.add("b"); +s.add(a); +List l = new ArrayList<>(s);""", + 'l.get(0).equals("b") && a.equals("c")', + 'l.get(0).equals("b") && a.equals("a")') + pair('hash-linked-iterate', + 'Iteration of a LinkedHashMap with a symbolic key: insertion order, whatever the keys are.\n' + 'SAFE: "b" was put first, and putting it again does not move it.', + 'string:a', """ +String a = Verifier.nondetString(); +Map m = new LinkedHashMap<>(); +m.put("b", 1); +m.put(a, 2); +Iterator it = m.keySet().iterator(); +String first = it.next();""", + 'it.hasNext() && it.next().equals("a")', + '!first.equals("b")') + key = """ static final class Key { + final int v; + Key(int v) { this.v = v; } + @Override public boolean equals(Object o) { return o instanceof Key && ((Key) o).v % 10 == v % 10; } + @Override public int hashCode() { return v % 10; } + } +""" + pair('hash-user-key', + 'A key class with its own equals and hashCode (equal modulo 10): not a key the model\n' + 'compares. Must stay UNKNOWN (SAFE would need the hash code to be tracked into the map).', + 'int:13', """ +int x = Verifier.nondetInt(); +Map m = new HashMap<>(); +m.put(new Key(3), "three");""", + 'x != 3 && m.containsKey(new Key(x))', + 'x >= 0 && x < 10 && x != 3 && m.containsKey(new Key(x))', extra=key) + pair('hash-user-key-mixed', + 'A map with a symbolic String key and a key with its own equals: the model gives up at the\n' + 'second, and the map is not exact any more. Must stay UNKNOWN.', + 'string:s int:13', """ +String s = Verifier.nondetString(); +int x = Verifier.nondetInt(); +Map m = new HashMap<>(); +m.put(s, "string"); +m.put(new Key(3), "three"); +m.remove(new Key(x));""", + 's.equals("s") && x != 3 && m.size() == 1', + 's.equals("s") && x >= 0 && x < 10 && x != 3 && m.size() == 1', extra=key) + pair('hash-double-key', + 'Double keys: Double.equals compares bits, so -0.0 is not the key 0.0 although -0.0 == 0.0.\n' + 'Not modelled. Must stay UNKNOWN.', + 'double:-0.0', """ +double d = Verifier.nondetDouble(); +Map m = new HashMap<>(); +m.put(0.0, 1);""", + '!m.containsKey(d) && d == 0.0', + 'm.containsKey(d) && d != 0.0') + pair('hash-view-remove', + 'A key removed through the keySet view, which is not modelled: the map\'s exact entries are\n' + 'not known afterwards. Must stay UNKNOWN.', + 'string:k', """ +String a = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put("k", 1); +m.keySet().remove(a);""", + 'm.isEmpty()', + 'm.isEmpty() && !a.equals("k")') + pair('hash-view-remove-key', + 'As hash-view-remove, with the symbolic key in the map and a concrete one removed through\n' + 'the view. Must stay UNKNOWN.', + 'string:k', """ +String a = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put(a, 1); +m.keySet().remove("k");""", + 'm.isEmpty()', + 'm.isEmpty() && !a.equals("k")') + pair('hash-iterator-remove', + 'An entry removed through an iterator: the map\'s exact entries are not known afterwards.\n' + 'Must stay UNKNOWN.', + 'string:k', """ +String a = Verifier.nondetString(); +Map m = new LinkedHashMap<>(); +m.put("k", 1); +m.put(a, 2); +Iterator it = m.keySet().iterator(); +it.next(); +it.remove();""", + 'm.isEmpty()', + 'm.isEmpty() && !a.equals("k")') + pair('hash-escape', + 'A set changed by library code that is not modelled (Collections.addAll): its exact\n' + 'elements are not known afterwards. Must stay UNKNOWN.', + 'string:p', """ +String a = Verifier.nondetString(); +Set s = new HashSet<>(); +s.add(a); +Collections.addAll(s, "p", "q");""", + 's.size() == 2', + 's.size() == 2 && !a.equals("p") && !a.equals("q")') + for v, old in (('false', 'null'), ('true', '"old"')): + task(f'hash-putifabsent_{v}', + 'putIfAbsent: a key mapped to null counts as absent and gets the new value (VIOLATION\n' + 'variant, a == "k"); a key with a value keeps it (SAFE variant).', + 'string:k', f""" +String a = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put("k", {old}); +m.put("j", "J"); +m.putIfAbsent(a, "new"); +if ("new".equals(m.get("k"))) assert false;""") + task(f'hash-getordefault_{v}', + 'getOrDefault: a key mapped to null gives null, not the default (VIOLATION variant,\n' + 'a == "k"); in the SAFE variant no value is null.', + 'string:k', f""" +String a = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put("k", {old}); +String v = m.getOrDefault(a, "default"); +if (v == null) assert false;""") + pair('hash-copy', + 'Copy constructor and putAll with symbolic Integer keys: the copies hold the same entries,\n' + 'and putAll replaces the value of an equal key. SAFE: only y == 7 puts 2 under 7 (x == 7\n' + 'with y == x is excluded).', + 'int:1 int:7', """ +int x = Verifier.nondetInt(); +int y = Verifier.nondetInt(); +Map m = new HashMap<>(); +m.put(x, 1); +m.put(y, 2); +Map c = new HashMap<>(m); +Map d = new HashMap<>(); +d.put(7, 0); +d.putAll(c);""", + 'd.size() == 2 && d.get(7) == 2 && x != 7', + 'd.size() == 2 && d.get(7) == 2 && x != 7 && y != 7') + pair('hash-clear', + 'clear, then a new entry: the keys from before are gone. SAFE: only "k" is a key.', + 'string:k', """ +String a = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put(a, 1); +m.clear(); +m.put("k", 2);""", + 'm.containsKey(a) && m.size() == 1', + 'm.containsKey(a) && !a.equals("k")') + pair('hash-nested', + 'A map of maps with symbolic keys: get returns the very map that was put, so what is put\n' + 'into it shows through the outer map. SAFE: only q == "id" makes "id" a key.', + 'string:p string:id', """ +String p = Verifier.nondetString(); +String q = Verifier.nondetString(); +Map> outer = new HashMap<>(); +outer.put(p, new HashMap()); +outer.get(p).put(q, "v"); +Map inner = outer.get(p);""", + 'inner.containsKey("id") && p.equals("p")', + 'inner.containsKey("id") && !q.equals("id")') + pair('hash-char-key', + 'Character keys (boxed chars compare by value). SAFE: only \'b\' maps to 2.', + 'char:b', """ +char c = Verifier.nondetChar(); +Map m = new HashMap<>(); +m.put('a', 1); +m.put('b', 2); +Integer v = m.get(c);""", + 'v != null && v == 2', + "v != null && v == 2 && c != 'b'") + pair('hash-char-literal', + 'A Character key from a char constant, looked up with a symbolic char that is not 0. The\n' + 'shadow of the boxed constant must be \'a\': with the default \'\\0\' (as before the\n' + 'constructor models took int-typed constants) a model that compares it puts c == 0 on the\n' + 'path instead of c == \'a\', and the VIOLATION task becomes a false SAFE. SAFE: only \'a\' is\n' + 'a key. The all-zero run ends early.', + 'char:a', """ +char c = Verifier.nondetChar(); +if (c == 0) return; +Map m = new HashMap<>(); +m.put('a', 1);""", + 'm.containsKey(c)', + "m.containsKey(c) && c != 'a'") + pair('hash-short-literal', + 'As hash-char-literal, with a Short key from a constant. SAFE: only 7 is a key. The\n' + 'all-zero run ends early.', + 'short:7', """ +short x = Verifier.nondetShort(); +if (x == 0) return; +Map m = new HashMap<>(); +m.put((short) 7, 1);""", + 'm.containsKey(x)', + 'm.containsKey(x) && x != 7') + pair('hash-identity-put', + 'The identity of what put returns: the previous value of the entry the key equals, v0 iff\n' + 'the two symbolic keys are equal. Reference comparisons are decided on the objects, so the\n' + 'path must say which entry matched. SAFE: put returns v0 only for equal keys.', + 'string:k string:k', """ +String a = Verifier.nondetString(); +String b = Verifier.nondetString(); +Object v0 = new Object(); +Object v1 = new Object(); +Map m = new HashMap<>(); +m.put(a, v0); +Object previous = m.put(b, v1);""", + 'previous == v0 && a.equals("k")', + '(previous == v0 && !a.equals(b)) || (previous != v0 && previous != null)') + pair('hash-identity-get', + 'The identity of what get returns, with possibly equal symbolic keys: the lookup key b\n' + 'finds v1 iff it equals a (which may itself have replaced the entry of "x"). SAFE: get\n' + 'returns v1 only for b equal to a, and v0 only for "x".', + 'string:q string:q', """ +String a = Verifier.nondetString(); +String b = Verifier.nondetString(); +Object v0 = new Object(); +Object v1 = new Object(); +Map m = new HashMap<>(); +m.put("x", v0); +m.put(a, v1); +Object got = m.get(b);""", + 'got == v1 && b.equals("q")', + '(got == v1 && !b.equals(a)) || (got == v0 && (!b.equals("x") || a.equals("x"))) || (got != v0 && got != v1 && got != null)') + pair('hash-identity-remove', + 'The identity of what remove returns and of the values left, with possibly equal symbolic\n' + 'Integer keys. SAFE: remove(y) returns v1 iff y == x or x == 1 and y == 1.', + 'int:5 int:5', """ +int x = Verifier.nondetInt(); +int y = Verifier.nondetInt(); +Object v0 = new Object(); +Object v1 = new Object(); +Map m = new HashMap<>(); +m.put(1, v0); +m.put(x, v1); +Object removed = m.remove(y);""", + 'removed == v1 && m.get(1) == v0 && x == 5', + '(removed == v1 && y != x) || (removed == v0 && (y != 1 || x == 1)) || (removed == null && (y == x || y == 1))') + pair('hash-unknown-shadow', + 'A Short key built by a constructor SWAT does not model (from a String): its shadow keeps\n' + 'the default 0, so it must not be compared (comparing it puts x == 0 on the path instead of\n' + 'x == 7, and the VIOLATION task becomes a false SAFE). Must stay UNKNOWN. The all-zero run\n' + 'ends early.', + 'short:7', """ +short x = Verifier.nondetShort(); +if (x == 0) return; +Map m = new HashMap<>(); +m.put(new Short("7"), 1);""", + 'm.containsKey(x)', + 'm.containsKey(x) && x != 7') + pair('hash-boolean-key', + 'Boolean and Byte keys from constants, looked up with symbolic values. SAFE: only true and\n' + '(byte) -3 are keys.', + 'boolean:true byte:-3', """ +boolean b = Verifier.nondetBoolean(); +byte y = Verifier.nondetByte(); +Map m = new HashMap<>(); +m.put(true, 1); +m.put((byte) -3, 2);""", + 'm.containsKey(b) && m.containsKey(y)', + 'm.size() != 2 || (m.containsKey(b) && !b) || (m.containsKey(y) && y != -3)') + pair('hash-new-string', + 'A String key built by a constructor SWAT may not model (from bytes): its shadow must not\n' + 'be compared unless it is the string. SAFE or UNKNOWN on the SAFE task. The all-zero run ends\n' + 'early.', + 'string:hi', """ +String s = Verifier.nondetString(); +if (s.isEmpty()) return; +Map m = new HashMap<>(); +m.put(new String(new byte[] {104, 105}), 1);""", + 'm.containsKey(s)', + 'm.containsKey(s) && !s.equals("hi")') + pair('hash-string-vs-int', + 'A String key and an Integer key are never equal ("5" is not 5). SAFE: the map always has\n' + 'two entries.', + 'string:5 int:5', """ +String s = Verifier.nondetString(); +int x = Verifier.nondetInt(); +Map m = new HashMap<>(); +m.put(s, "string"); +m.put(x, "int");""", + 'm.size() == 2 && s.equals("5") && x == 5', + 'm.size() != 2') + pair('emptymap-get', + 'Lookups in Collections.emptyMap() and emptySet() with a symbolic key: null and false for\n' + 'every key. SAFE: nothing is found.', + 'string:k', """ +String s = Verifier.nondetString(); +Map m = Collections.emptyMap(); +Set e = Collections.emptySet();""", + 'm.get(s) == null && !e.contains(s) && s.equals("k")', + 'm.get(s) != null || m.containsKey(s) || e.contains(s)') + pair('hash-concurrent', + 'A ConcurrentHashMap with symbolic String keys: put, putIfAbsent, get, getOrDefault and\n' + 'remove by key as in a HashMap. SAFE: "v1" stays only under a (putIfAbsent of an equal b\n' + 'changes nothing, and remove(b) then removes it).', + 'string:k string:k', """ +String a = Verifier.nondetString(); +String b = Verifier.nondetString(); +Map m = new java.util.concurrent.ConcurrentHashMap<>(); +m.put(a, "v1"); +String old = m.putIfAbsent(b, "v2"); +String got = m.getOrDefault(b, "none"); +m.remove(b);""", + 'old != null && got.equals("v1") && m.isEmpty() && a.equals("k")', + 'old != null && (!got.equals("v1") || !m.isEmpty() || !a.equals(b))') + pair('hash-concurrent-iterate', + 'As hash-iterate-order, for a ConcurrentHashMap: its iteration order is the hash order too.\n' + 'Must stay UNKNOWN.', + 'string:p', """ +String a = Verifier.nondetString(); +Map m = new java.util.concurrent.ConcurrentHashMap<>(); +m.put("b", 1); +m.put(a, 2); +int first = m.values().iterator().next();""", + "first == 2 && a.length() == 1 && a.charAt(0) > 'b'", + "first == 2 && a.length() == 1 && a.charAt(0) > 'b' && a.charAt(0) < 'p'") + pair('hash-linked-copy-order', + 'A LinkedHashMap copied from a HashMap with a symbolic key iterates in the HashMap\'s hash\n' + 'order, not in the order the keys were put (see hash-iterate-order). Must stay UNKNOWN.', + 'string:p', """ +String a = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put("b", 1); +m.put(a, 2); +Map l = new LinkedHashMap<>(m); +int first = l.values().iterator().next();""", + "first == 2 && a.length() == 1 && a.charAt(0) > 'b'", + "first == 2 && a.length() == 1 && a.charAt(0) > 'b' && a.charAt(0) < 'p'") + pair('hashset-aslist', + 'A HashSet built from Arrays.asList, a source the model does not copy from: the set is not\n' + 'modelled. Must stay UNKNOWN.', + 'string:z', """ +String a = Verifier.nondetString(); +Set s = new HashSet<>(Arrays.asList(a, "z"));""", + 's.size() == 1', + 's.size() == 1 && !a.equals("z")') + pair('hashset-keyset-copy', + 'addAll of a map\'s key set (with a symbolic key) into a set. SAFE: two elements are left\n' + 'only if a is "j" or "k".', + 'string:j', """ +String a = Verifier.nondetString(); +Map m = new HashMap<>(); +m.put(a, 1); +m.put("k", 2); +Set s = new HashSet<>(); +s.add("j"); +s.addAll(m.keySet());""", + 's.size() == 2 && a.equals("j")', + 's.size() == 2 && !a.equals("j") && !a.equals("k")') + pair('list-equals', + 'ArrayList.equals of two lists with symbolic String elements: equal iff the elements are,\n' + 'pair by pair. SAFE: only a == "k" and b == "m" make them equal.', + 'string:k string:m', """ +String a = Verifier.nondetString(); +String b = Verifier.nondetString(); +List x = new ArrayList<>(); +x.add(a); +x.add("m"); +List y = new ArrayList<>(); +y.add("k"); +y.add(b);""", + 'x.equals(y) && a.length() == 1', + 'x.equals(y) && (!a.equals("k") || !b.equals("m"))') + pair('list-equals-size', + 'Objects.equals of two lists of different sizes: never equal, whatever the elements are.\n' + 'SAFE: the lists differ.', + 'string:q string:q', """ +String a = Verifier.nondetString(); +String b = Verifier.nondetString(); +List x = new ArrayList<>(); +x.add(a); +List y = new ArrayList<>(); +y.add(a); +y.add(b);""", + '!Objects.equals(x, y) && a.equals("q") && x.equals(x)', + 'Objects.equals(x, y) || !x.equals(x)') + pair('list-equals-changed', + 'equals of a list that library code SWAT does not model has changed (Collections.reverse):\n' + 'its elements are not known in order any more, and comparing the recorded ones puts\n' + 'a == "k" on the path instead of a == "z". Must stay UNKNOWN.', + 'string:z', """ +String a = Verifier.nondetString(); +List x = new ArrayList<>(); +x.add(a); +x.add("k"); +Collections.reverse(x); +List y = new ArrayList<>(); +y.add("k"); +y.add("z");""", + 'x.equals(y)', + 'x.equals(y) && !a.equals("z")') + pair('hashset-add', + 'HashSet.add of a symbolic element returns whether it was new; contains of another symbolic\n' + 'one. SAFE: if a was not new, the set is {"x"} and holds b only for b == "x".', + 'string:x string:x', """ +String a = Verifier.nondetString(); +String b = Verifier.nondetString(); +Set s = new HashSet<>(); +s.add("x"); +boolean fresh = s.add(a);""", + '!fresh && s.size() == 1 && s.contains(b) && b.length() == 1', + '!fresh && s.contains(b) && !b.equals("x")') + pair('hashset-remove', + 'HashSet.remove after adding a symbolic Integer. SAFE: one element is left only if x was\n' + '1 or 2.', + 'int:2', """ +int x = Verifier.nondetInt(); +Set s = new HashSet<>(); +s.add(1); +s.add(2); +s.add(x); +boolean removed = s.remove(2);""", + 'removed && s.size() == 1 && x != 1', + 'removed && s.size() == 1 && x != 1 && x != 2') + pair('hashset-copy', + 'A HashSet built from a list with symbolic elements (and addAll of another set): equal\n' + 'elements collapse. SAFE: one element is left only if a is "z" too.', + 'string:z string:z', """ +String a = Verifier.nondetString(); +String b = Verifier.nondetString(); +List l = new ArrayList<>(); +l.add(a); +l.add(b); +l.add("z"); +Set s = new HashSet<>(l); +Set t = new LinkedHashSet<>(); +t.add("z"); +boolean changed = t.addAll(s);""", + '!changed && s.size() == 1 && t.size() == 1 && a.length() == 1', + '!changed && !a.equals("z")') + pair('hashset-list-changed', + 'A HashSet built from a list that a call SWAT does not model changed (remove(Object)): the\n' + 'list\'s elements are not known exactly, so neither are the set\'s. Must stay UNKNOWN.', + 'string:z', """ +String a = Verifier.nondetString(); +List l = new ArrayList<>(); +l.add("y"); +l.add("z"); +l.remove(a); +Set s = new HashSet<>(l);""", + 's.size() == 1 && s.contains("y")', + 's.size() == 1 && s.contains("y") && !a.equals("z")') + pair('hashset-linked-order', + 'A LinkedHashSet iterates in insertion order, also with symbolic elements. SAFE: "b" stays\n' + 'first.', + 'string:a', """ +String a = Verifier.nondetString(); +Set s = new LinkedHashSet<>(); +s.add("b"); +s.add(a); +Iterator it = s.iterator(); +String first = it.next();""", + 'it.hasNext() && it.next().equals("a")', + '!first.equals("b")') + + +if __name__ == '__main__': + main() diff --git a/targets/sv-comp/swat-benchmarks/scripts/modelsoundness.py b/targets/sv-comp/swat-benchmarks/scripts/modelsoundness.py new file mode 100644 index 00000000..fd61054f --- /dev/null +++ b/targets/sv-comp/swat-benchmarks/scripts/modelsoundness.py @@ -0,0 +1,239 @@ +"""model-soundness: SAFE/VIOLATION pairs for SWAT's models of library calls (one group per model). +A model must agree with the JVM, or not be used (context loss): a VIOLATION task that comes out +SAFE means a model hid a reachable path. In each pair the initial run (all inputs 0, the empty +string) does not reach the assert. + +Each entry: name -> (source, witness, safe_reason). The source holds @SAFE|VIOLATION@ for the +one spot where the two variants differ. witness: the violating inputs in nondet call order. + +Expected SWAT results: the pairs of a modelled call give SAFE and VIOLATION; for the calls that +are deliberately not modelled (marked "context loss") the SAFE variant must stay UNKNOWN and the +VIOLATION variant must not be SAFE. +""" + +import re + +from task import Task + +IMPORT = 'import org.sosy_lab.sv_benchmarks.Verifier;\n' + +TASKS = {} + + +def task(name, witness, safe_reason): + def register(fn): + TASKS[name] = (fn.__doc__, witness, safe_reason) + return fn + return register + + +# --- java.util.regex: Pattern.compile(c).matcher(s).matches() (modelled) ----------------------- + +@task('regex-matcher-match', [('string', '12')], + 'a string of digits only does not start with "x".') +def _(): + """import java.util.regex.Pattern; +IMPORT +// The matching side of Matcher.matches() on a symbolic string, the pattern in a static field. +public class Main { + private static final Pattern DIGITS = Pattern.compile("[0-9]+"); + + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (DIGITS.matcher(s).matches()) { + if (s.startsWith(@"x"|"1"@)) assert false; + } + } +} +""" + + +@task('regex-matcher-nomatch', [('string', '1a')], + 'a string of two chars that are both digits matches [0-9]{2}.') +def _(): + """import java.util.regex.Matcher; +import java.util.regex.Pattern; +IMPORT +// The non-matching side of Matcher.matches() on a symbolic string. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Pattern p = Pattern.compile("[0-9]{2}"); + Matcher m = p.matcher(s); + if (!m.matches() && s.length() == 2 && s.charAt(0) >= '0' && s.charAt(0) <= '9') { + if (@s.charAt(1) >= '0' && s.charAt(1) <= '9'|s.charAt(1) == 'a'@) assert false; + } + } +} +""" + + +@task('regex-matcher-aid', [('string', '0123456789#')], + 'a match of the pattern starts with at least 10 hex digits.') +def _(): + """import java.util.regex.Pattern; +IMPORT +// The pattern of argv-mhe's AidGroup (escaped '*' and '#', a bounded repetition). +public class Main { + private static final Pattern AID = Pattern.compile("[0-9A-Fa-f]{10,32}\\\\*?\\\\#?"); + + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (AID.matcher(s).matches()) { + if (@s.length() < 10|s.endsWith("#")@) assert false; + } + } +} +""" + + +@task('regex-pattern-matches', [('string', 'ab')], + 'a string of digits only does not start with "a".') +def _(): + """import java.util.regex.Pattern; +IMPORT +// The static Pattern.matches(regex, s), which is s.matches(regex). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (Pattern.matches(@"[0-9]+"|"[a-z]+"@, s)) { + if (s.startsWith("a")) assert false; + } + } +} +""" + + +@task('regex-matcher-twice', [('string', '7')], + 'matches() gives the same answer twice on an unchanged matcher.') +def _(): + """import java.util.regex.Matcher; +import java.util.regex.Pattern; +IMPORT +// matches() called twice on one matcher (it does not depend on the earlier match). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]").matcher(s); + boolean first = m.matches(); + boolean second = m.matches(); + if (@first != second|first && second@) assert false; + } +} +""" + + +# --- java.util.regex: deliberately not modelled (context loss) ---------------------------------- + +@task('regex-untranslatable', [('string', 'abc')], + 'a match of a.c has three chars. "." is not translated: SWAT must say UNKNOWN.') +def _(): + """import java.util.regex.Pattern; +IMPORT +// A regular expression SWAT does not translate ("."): context loss, SAFE must not be claimed. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + if (Pattern.compile("a.c").matcher(s).matches()) { + if (@s.length() != 3|s.length() == 3@) assert false; + } + } +} +""" + + +@task('regex-flags', [('string', 'AB')], + 'with CASE_INSENSITIVE a match is letters only, so it has no digit "1". Flags are not modelled: UNKNOWN.') +def _(): + """import java.util.regex.Pattern; +IMPORT +// A pattern with flags (CASE_INSENSITIVE): not modelled, context loss. A model that ignored the +// flags would hide the violation (an upper-case match). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Pattern p = Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE); + if (p.matcher(s).matches()) { + if (s.startsWith(@"1"|"A"@)) assert false; + } + } +} +""" + + +@task('regex-find', [('string', 'x12')], + 'find() of [0-9]{2} in a string of one char fails. find() is not modelled: UNKNOWN.') +def _(): + """import java.util.regex.Matcher; +import java.util.regex.Pattern; +IMPORT +// Matcher.find() is not modelled: context loss. A model that took it for matches() would hide +// the violation (a match inside the string). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]{2}").matcher(s); + if (m.find()) { + if (@s.length() == 1|s.startsWith("x")@) assert false; + } + } +} +""" + + +@task('regex-reset', [('string', 'a'), ('string', '1')], + 'after reset(t), matches() of [0-9]+ is about t, which is a digit string and does not start with "x".' + ' reset is not modelled: UNKNOWN.') +def _(): + """import java.util.regex.Matcher; +import java.util.regex.Pattern; +IMPORT +// A matcher reused on another input with reset(t): not modelled, context loss. A model that kept +// matching s would hide the violation (s does not match, t does). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + String t = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]+").matcher(s); + boolean first = m.matches(); + m.reset(t); + boolean second = m.matches(); + if (!first && second) { + if (@t.startsWith("x")|t.length() == 1@) assert false; + } + } +} +""" + + +@task('regex-reset-concrete', [('string', 'a')], + 'after reset("123"), matches() is true, so the assert needs !true. reset is not modelled: UNKNOWN.') +def _(): + """import java.util.regex.Matcher; +import java.util.regex.Pattern; +IMPORT +// A matcher of a symbolic string reset to a concrete one: not modelled, context loss. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + Matcher m = Pattern.compile("[0-9]+").matcher(s); + boolean first = m.matches(); + m.reset("123"); + boolean second = m.matches(); + if (!first && s.length() == 1) { + if (@!second|second@) assert false; + } + } +} +""" + + +def render(src: str, safe: bool) -> str: + src = src.replace('IMPORT\n', IMPORT) + return re.sub(r'@(.*?)\|(.*?)@', lambda m: m.group(1) if safe else m.group(2), src) + + +def all_model_soundness_tasks(): + for name, (src, witness, safe_reason) in TASKS.items(): + for safe in (True, False): + yield Task('model-soundness', name, safe, render(src, safe), witness, safe_reason) diff --git a/targets/sv-comp/swat-benchmarks/scripts/soundness.py b/targets/sv-comp/swat-benchmarks/scripts/soundness.py index a939ea14..1885d3aa 100644 --- a/targets/sv-comp/swat-benchmarks/scripts/soundness.py +++ b/targets/sv-comp/swat-benchmarks/scripts/soundness.py @@ -1245,6 +1245,44 @@ def _(): """ +@task('explore-given-up-tochararray-len', witness=[('string', 'abcdefg'), ('int', 1001)]) +def _(): + """IMPORT +// toCharArray() pins the length of s (the array's length is concrete), and every run of a +// solution for the pin's other side diverges, so the explorer gives the pin up. A given-up branch +// rules out SAFE unless some run reaches its side; the branch's own solved marker counted as that +// (false SAFE before fix/given-up-self-coverage). +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + int a = Verifier.nondetInt(); + char[] c = s.toCharArray(); + if (c.length == 7) { + if (@HIT(a)@) assert false; + } + } +} +""" + + +@task('explore-given-up-tochararray-contains', witness=[('string', ''), ('int', 1001)]) +def _(): + """IMPORT +// As explore-given-up-tochararray-len, but the length pin is only implied: s.contains() needs +// six chars or more, a length the pinned runs never have. +public class Main { + public static void main(String[] args) { + String s = Verifier.nondetString(); + int a = Verifier.nondetInt(); + char[] c = s.toCharArray(); + if (s.contains("")) { + if (@HIT(a)@) assert false; + } + } +} +""" + + @task('npe-in-callee-branch') def _(): """IMPORT