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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static int three() {
return 3;
}

static void oneElement(@Refinement("length(a) == 1") int[] a) {}

public static void main(String[] args) {
@Refinement("_ < 10")
int a = 5;
Expand All @@ -20,5 +22,12 @@ public static void main(String[] args) {
@Refinement("c < 100")
int c = (a < 100) ? three() : a;
c = (a < 100) ? three() * 3 : a * 5;

boolean some = args.length > 0;
if (some)
oneElement(some ? new int[1] : new int[2]);
@Refinement("_ > 0")
int d = 1;
d += (some ? a < 100 : false) ? 1 : 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@ public static void main(String[] args) {
int a = 5;
a = (a == 2) ? 6 + three() : 4 * three(); // Expect: Refinement Error
}

static void oneElement(@Refinement("length(a) == 1") int[] a) {}

static void pick(boolean b) {
oneElement(b ? new int[1] : new int[2]); // Expect: Refinement Error
}

boolean flag;

void clear() {
@Refinement("_ == true")
boolean was = flag ? (flag = false) : true; // Expect: Refinement Error
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,8 @@ public <R> void visitCtMethod(CtMethod<R> method) {
context.clearInstanceVariables();
context.enterContext();
context.restoreInstanceVariables();
if (!method.getSignature().equals("main(java.lang.String[])")) {
mfc.loadFunctionInfo(method);
snapshotWrittenParameters(method);
}
mfc.loadFunctionInfo(method);
snapshotWrittenParameters(method);
try {
super.visitCtMethod(method);
} catch (LJError e) {
Expand Down Expand Up @@ -564,7 +562,7 @@ public void visitCtIf(CtIf ifElement) {
Predicate lastExpRefs = substituteAllVariablesForLastInstance(expRefs);
if (canCompleteNormally(ifElement.getThenStatement()) || ifElement.getElseStatement() == null
|| canCompleteNormally(ifElement.getElseStatement()))
lastExpRefs = snapshotMutableParameters(lastExpRefs, exp);
lastExpRefs = snapshotMutableVariables(lastExpRefs, exp);
expRefs = Predicate.createConjunction(expRefs, lastExpRefs);

// TODO Change in future
Expand Down Expand Up @@ -670,7 +668,7 @@ private boolean visitSwitch(CtAbstractSwitch<?> switchElement) {
// the selector is evaluated once, so the cases compare its value then, not what its variables hold later
String selectorName = GeneratedNames.of(GeneratedNames.SWITCH_SELECTOR, context.getCounter());
Predicate selectorRefs = getRefinement(selector).substituteVariable(Keys.WILDCARD, selectorName);
selectorRefs = snapshotMutableParameters(substituteAllVariablesForLastInstance(selectorRefs), selector);
selectorRefs = snapshotMutableVariables(substituteAllVariablesForLastInstance(selectorRefs), selector);
context.addInstanceToContext(selectorName, selector.getType(), selectorRefs, selector);

// the cases with no statements share the statements of the next case, the default one is tested last
Expand Down Expand Up @@ -824,21 +822,26 @@ private static CtStatement endingJump(CtStatement statement) {
return statement instanceof CtCFlowBreak ? statement : null;
}

private Predicate snapshotMutableParameters(Predicate predicate, CtExpression<?> condition) {
// An unchanged parameter can keep its name; a later write must not change this guard's meaning.
private Predicate snapshotMutableVariables(Predicate predicate, CtExpression<?> condition) {
// An unchanged parameter or field can keep its name; a later write must not change this guard's meaning.
CtExecutable<?> executable = condition.getParent(CtExecutable.class);
Set<CtVariable<?>> written = new LinkedHashSet<>();
if (executable != null)
for (CtVariableWrite<?> write : executable
.getElements(new TypeFilter<CtVariableWrite<?>>(CtVariableWrite.class)))
if (write.getVariable().getDeclaration() != null)
written.add(write.getVariable().getDeclaration());
Set<String> parameters = new LinkedHashSet<>();
for (CtVariableRead<?> read : condition.getElements(new TypeFilter<CtVariableRead<?>>(CtVariableRead.class)))
if (read.getVariable().getDeclaration()instanceof CtParameter<?> parameter && written.contains(parameter))
parameters.add(read.getVariable().getSimpleName());
Set<String> mutable = new LinkedHashSet<>();
for (CtVariableRead<?> read : condition.getElements(new TypeFilter<CtVariableRead<?>>(CtVariableRead.class))) {
if (!written.contains(read.getVariable().getDeclaration()))
continue;
if (read instanceof CtFieldRead<?> fieldRead)
mutable.add(Utils.qualifyFieldName(fieldRead.getVariable()));
else if (read.getVariable().getDeclaration() instanceof CtParameter<?>)
mutable.add(read.getVariable().getSimpleName());
}
for (String name : predicate.getVariableNames()) {
if (!parameters.contains(name) || !(context.getVariableByName(name)instanceof Variable variable)
if (!mutable.contains(name) || !(context.getVariableByName(name)instanceof Variable variable)
|| variable.getLastInstance().isPresent())
continue;
String snapshot = String.format(Formats.INSTANCE, name, context.getCounter());
Expand Down Expand Up @@ -1322,7 +1325,7 @@ private void assumeLoopCondition(CtExpression<Boolean> condition) {
*/
private RefinedVariable assumeCondition(CtExpression<Boolean> condition, boolean holds, String kind) {
// conditions with side effects (e.g. (n = read()) > 0) are not encoded, so they assume nothing
if (condition == null || !condition.getElements(new TypeFilter<>(CtVariableWrite.class)).isEmpty())
if (condition == null || hasSideEffects(condition))
return null;
Predicate refs = getRefinement(condition);
if (isUninformativeCondition(refs, condition))
Expand Down Expand Up @@ -1356,14 +1359,39 @@ public <T> void visitCtConditional(CtConditional<T> conditional) {
// each branch is checked knowing which way the condition went, as the branches of an if
CtExpression<Boolean> condition = conditional.getCondition();
scan(condition);
Predicate value = conditionValue(condition);
scanAssuming(conditional.getThenExpression(), condition, true);
scanAssuming(conditional.getElseExpression(), condition, false);
Predicate cond = getRefinement(conditional.getCondition());
Predicate c = Predicate.createITE(cond, getRefinement(conditional.getThenExpression()),
Predicate c = Predicate.createITE(value, getRefinement(conditional.getThenExpression()),
getRefinement(conditional.getElseExpression()));
conditional.putMetadata(Keys.REFINEMENT, c);
}

/**
* The value of the condition of a conditional, as a fresh boolean: the wildcard of the condition's refinement is
* the condition, while the wildcard of the conditional's refinement is its result. The fresh boolean keeps the
* values the condition was evaluated on, as later writes (even in a branch) do not change which branch was taken.
*/
private Predicate conditionValue(CtExpression<Boolean> condition) {
String name = GeneratedNames.of(GeneratedNames.TERNARY_CONDITION, context.getCounter());
Predicate refs = getRefinement(condition);
Predicate value;
// as in assumeCondition, conditions with side effects are not encoded, and uninformative ones say nothing
if (hasSideEffects(condition) || isUninformativeCondition(refs, condition))
value = new Predicate();
else if (refs.getVariableNames().contains(Keys.WILDCARD) || condition instanceof CtInvocation<?>)
value = refs.substituteVariable(Keys.WILDCARD, name);
else
value = Predicate.createEquals(Predicate.createVar(name), refs);
value = snapshotMutableVariables(substituteAllVariablesForLastInstance(value), condition);
context.addInstanceToContext(name, factory.Type().BOOLEAN_PRIMITIVE, value, condition);
return Predicate.createVar(name);
}

private static boolean hasSideEffects(CtExpression<?> expression) {
return !expression.getElements(new TypeFilter<>(CtVariableWrite.class)).isEmpty();
}

private void scanAssuming(CtExpression<?> branch, CtExpression<Boolean> condition, boolean holds) {
RefinedVariable pathVar = assumeCondition(condition, holds, GeneratedNames.TERNARY_CONDITION);
scan(branch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ private static String parentQualifiedName(CtElement element) {
}

private <R> void auxGetMethodRefinements(CtMethod<R> method, RefinedFunction rf) throws LJError {
// main cannot have refinement - for now
if (method.getSignature().equals("main(java.lang.String[])"))
return;
List<CtParameter<?>> params = method.getParameters();
Predicate ref = handleFunctionRefinements(rf, method, params);
method.putMetadata(Keys.REFINEMENT, ref);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,9 @@ private Predicate getOperatorAssignmentRefinement(CtExpression<?> element) throw

private Predicate getConditionRefinement(CtExpression<Boolean> condition) throws LJError {
Predicate refinement = rtc.getRefinement(condition);
Optional<Predicate> value = unwrapWildcardEquality(refinement);
if (value.isPresent())
return value.get();
// a refinement with the wildcard constrains the condition's value, which is not the value itself
if (refinement.getVariableNames().contains(Keys.WILDCARD))
return valueFromRefinement(condition, refinement);
return refinement;
}

Expand Down
Loading