Skip to content

Commit fd42089

Browse files
Throw an exception if the input for the SMT-LIB2 sanitizer contains stack operations like (pop), (reset-assertions) or (reset). Change the expected behavior in SanitizerTest.
1 parent 3e4575b commit fd42089

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

src/org/sosy_lab/java_smt/basicimpl/AbstractFormulaManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,16 @@ private String sanitize(String formulaStr) {
294294
// Keep only declaration, definitions and assertion
295295
builder.append(token).append('\n');
296296

297+
} else if (Tokenizer.isUnsupportedToken(token)) {
298+
// Throw an exception if the script contains commands like (pop) or (reset) that change the
299+
// state of the assertion stack.
300+
// We could keept track of the state of the stack and only consider the formulas that remain
301+
// on it until the end
302+
// of the script. However, this does not seem worth it at the moment. If needed, this
303+
// feature can still be added
304+
// later.
305+
throw new IllegalArgumentException();
306+
297307
} else {
298308
// Remove everything else
299309
}

src/org/sosy_lab/java_smt/basicimpl/Tokenizer.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,22 @@ public static boolean isSetLogicToken(String token) {
181181
public static boolean isExitToken(String token) {
182182
return matchesOneOf(token, "exit");
183183
}
184+
185+
/**
186+
* Check if this is an unsupported token.
187+
*
188+
* <p>The list of unsupported tokens contains:
189+
*
190+
* <ul>
191+
* <li>push
192+
* <li>pop
193+
* <li>reset-assertions
194+
* <li>reset
195+
* </ul>
196+
*
197+
* <p>Use {@link #tokenize(String)} to turn an SMT-LIB2 script into a string of input tokens.
198+
*/
199+
public static boolean isUnsupportedToken(String token) {
200+
return matchesOneOf(token, "push", "pop", "reset-assertions", "reset");
201+
}
184202
}

src/org/sosy_lab/java_smt/test/SanitizerTest.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ public void init() {
2828
public void logicTest() throws SolverException, InterruptedException {
2929
// Valid input string that sets the logic to QF_ALL
3030
BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))");
31-
BooleanFormula validLogic =
32-
mgr.parse("(set-logic QF_ALL)" + "(declare-const v Int)" + "(assert (= v 3))");
33-
assertThatFormula(expected).isEquivalentTo(validLogic);
31+
String validLogic = "(set-logic QF_ALL)" + "(declare-const v Int)" + "(assert (= v 3))";
32+
assertThatFormula(mgr.parse(validLogic)).isEquivalentTo(expected);
3433

35-
// Invalid string that sets QF_UF, even though integers are needed
36-
// Most solvers seem to just ignore the logic that was chosen
34+
// Invalid string that sets logic QF_UF, even though integers are needed
35+
// FIXME: We don't check for this as most solvers seem to ignore the logic anyway
3736
String wrongLogic = "(set-logic QF_UF)" + "(declare-const v Int)" + "(assert (= v 3))";
38-
assertThrows(IllegalArgumentException.class, () -> mgr.parse(wrongLogic));
37+
assertThatFormula(mgr.parse(wrongLogic)).isEquivalentTo(expected);
3938

4039
// Try setting logic after another command was already used
4140
String logicAfterOption =
@@ -51,13 +50,15 @@ public void logicTest() throws SolverException, InterruptedException {
5150
assertThrows(IllegalArgumentException.class, () -> mgr.parse(logicTwice));
5251

5352
// Call (reset) and *then* set the logic again
53+
// FIXME: We currently don't support the (reset) command and expect and exception to be thrown
54+
// here
5455
String logicReset =
5556
"(set-logic QF_UF)"
5657
+ "(reset)"
5758
+ "(set-logic ALL)"
5859
+ "(declare-const v Int)"
5960
+ "(assert (= v 3))";
60-
assertThatFormula(mgr.parse(logicReset)).isEquivalentTo(expected);
61+
assertThrows(IllegalArgumentException.class, () -> mgr.parse(logicReset));
6162
}
6263

6364
@Test
@@ -77,12 +78,13 @@ public void exitAtTheEnd() throws SolverException, InterruptedException {
7778

7879
@Test
7980
public void stackPushTest() throws SolverException, InterruptedException {
80-
BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))");
81+
// FIXME: We currently don't support stack operations and expect an exceptions to be thrown for
82+
// these inputs
8183

82-
// Push assertions and then pop the stack to remove them
84+
// Push an assertion and then use (pop) to remove it again
8385
String stackPush =
8486
"(declare-const v Int)" + "(push 1)" + "(assert (= v 0))" + "(pop 1)" + "(assert (= v 3))";
85-
assertThatFormula(mgr.parse(stackPush)).isEquivalentTo(expected);
87+
assertThrows(IllegalArgumentException.class, () -> mgr.parse(stackPush));
8688

8789
// Use (reset-assertions) to remove all assertions from the stack
8890
String stackResetAssertions =
@@ -91,7 +93,7 @@ public void stackPushTest() throws SolverException, InterruptedException {
9193
+ "(reset-assertions)"
9294
+ "(declare-const v Int)"
9395
+ "(assert (= v 0))";
94-
assertThatFormula((mgr.parse(stackResetAssertions))).isEquivalentTo(expected);
96+
assertThrows(IllegalArgumentException.class, () -> mgr.parse(stackResetAssertions));
9597

9698
// With :global-declarations the reset will also remove all declarations
9799
String globalStackResetAssertions =
@@ -100,7 +102,7 @@ public void stackPushTest() throws SolverException, InterruptedException {
100102
+ "(assert (= v 3))"
101103
+ "(reset-assertions)"
102104
+ "(assert (= v 0))";
103-
assertThatFormula(mgr.parse(globalStackResetAssertions)).isEquivalentTo(expected);
105+
assertThrows(IllegalArgumentException.class, () -> mgr.parse(globalStackResetAssertions));
104106

105107
// Just calling (reset) will also clear the stack
106108
String stackReset =
@@ -109,6 +111,6 @@ public void stackPushTest() throws SolverException, InterruptedException {
109111
+ "(reset)"
110112
+ "(declare-const v Int)"
111113
+ "(assert (= v 3))";
112-
assertThatFormula(mgr.parse(stackReset)).isEquivalentTo(expected);
114+
assertThrows(IllegalArgumentException.class, () -> mgr.parse(stackReset));
113115
}
114116
}

0 commit comments

Comments
 (0)