Merge pull request #14206 from smowton/smowton/feature/add-java-miscompilation-tests

Java: add tests for programs that don't compile
This commit is contained in:
Chris Smowton
2023-09-15 11:14:04 +01:00
committed by GitHub
32 changed files with 152 additions and 4 deletions

View File

@@ -477,6 +477,8 @@ private module ControlFlowGraphImpl {
or
this instanceof Call // includes both expressions and statements
or
this instanceof ErrorExpr
or
this instanceof ReturnStmt
or
this instanceof ThrowStmt

View File

@@ -276,7 +276,8 @@ class ExprParent extends @exprparent, Top { }
* An error expression.
*
* These may be generated by upgrade or downgrade scripts when databases
* cannot be fully converted.
* cannot be fully converted, or generated by the extractor when extracting
* source code containing errors.
*/
class ErrorExpr extends Expr, @errorexpr {
override string toString() { result = "<error expr>" }
@@ -1243,7 +1244,11 @@ class ClassInstanceExpr extends Expr, ConstructorCall, @classinstancexpr {
override Stmt getEnclosingStmt() { result = Expr.super.getEnclosingStmt() }
/** Gets a printable representation of this expression. */
override string toString() { result = "new " + this.getConstructor().getName() + "(...)" }
override string toString() {
result = "new " + this.getConstructor().getName() + "(...)"
or
not exists(this.getConstructor()) and result = "<ClassInstanceExpr that calls a missing constructor>"
}
override string getAPrimaryQlClass() { result = "ClassInstanceExpr" }
}

View File

@@ -1,2 +0,0 @@
| exprs.kt:278:52:278:66 | <error expr> | ErrorExpr | unexpected dead end |
| exprs.kt:278:52:278:66 | { ... } | BlockStmt | unexpected dead end |

View File

@@ -0,0 +1,30 @@
Test.java:
# 0| [CompilationUnit] Test
# 1| 1: [Class] Test
# 3| 2: [Method] yield
# 3| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 3| 0: [Parameter] x
# 3| 0: [TypeAccess] int
# 3| 5: [BlockStmt] { ... }
# 3| 0: [ReturnStmt] return ...
# 3| 0: [VarAccess] x
# 5| 3: [Method] secondCall
# 5| 3: [TypeAccess] void
# 5| 5: [BlockStmt] { ... }
# 7| 4: [Method] yieldWrapper
# 7| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 7| 0: [Parameter] x
# 7| 0: [TypeAccess] int
# 7| 5: [BlockStmt] { ... }
# 8| 0: [LocalVariableDeclStmt] var ...;
# 8| 0: [TypeAccess] int
# 8| 1: [LocalVariableDeclExpr] ret
# 8| 0: [ErrorExpr] <error expr>
# 8| 0: [MethodAccess] yield(...)
# 8| 0: [VarAccess] x
# 9| 1: [ExprStmt] <Expr>;
# 9| 0: [MethodAccess] secondCall(...)
# 10| 2: [ReturnStmt] return ...
# 10| 0: [VarAccess] ret

View File

@@ -0,0 +1 @@
semmle/code/java/PrintAst.ql

View File

@@ -0,0 +1,17 @@
public class Test {
public int yield(int x) { return x; }
public void secondCall() { }
public int yieldWrapper(int x) {
int ret = yield(x);
secondCall();
return ret;
}
}
// Diagnostic Matches: Erroneous node in tree: (ERROR)
// Diagnostic Matches: In file Test.java:8:15 no end location for JCMethodInvocation : yield(x)
// Diagnostic Matches: 1 errors during annotation processing

View File

@@ -0,0 +1 @@
//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17

View File

@@ -0,0 +1 @@
| file://:0:0:0:0 | 1 errors during annotation processing |

View File

@@ -0,0 +1,4 @@
import java
import semmle.code.java.Diagnostics
select any(Diagnostic d | not d.toString().matches("Not rewriting trap file for%"))

View File

@@ -0,0 +1,3 @@
import java
select any(Expr e | e.getType() instanceof ErrorType)

View File

@@ -0,0 +1,6 @@
Test.java:
# 0| [CompilationUnit] Test
# 1| 1: [Class] Test
#-----| -1: (Base Types)
# 1| 0: [TypeAccess] <any>
# 1| 0: [TypeAccess] String

View File

@@ -0,0 +1 @@
semmle/code/java/PrintAst.ql

View File

@@ -0,0 +1,3 @@
public class Test implements Unavailable<String> { }
// Diagnostic Matches: 1 errors during annotation processing

View File

@@ -0,0 +1 @@
//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17

View File

@@ -0,0 +1,3 @@
| Test.java:0:0:0:0 | 2 javac errors |
| Test.java:6:23:6:39 | Unexpected symbol for constructor: new NoSuchClass() |
| file://:0:0:0:0 | 2 errors during annotation processing |

View File

@@ -0,0 +1,4 @@
import java
import semmle.code.java.Diagnostics
select any(Diagnostic d | not d.toString().matches("Not rewriting trap file for%"))

View File

@@ -0,0 +1 @@
| Test.java:7:12:7:14 | nsc |

View File

@@ -0,0 +1,3 @@
import java
select any(Expr e | e.getType() instanceof ErrorType)

View File

@@ -0,0 +1,15 @@
Test.java:
# 0| [CompilationUnit] Test
#-----| -1: (Imports)
# 1| 1: [ImportType] import NoSuchClass
# 3| 1: [Class] Test
# 5| 2: [Method] test
# 5| 3: [TypeAccess] NoSuchClass
# 5| 5: [BlockStmt] { ... }
# 6| 0: [LocalVariableDeclStmt] var ...;
# 6| 0: [TypeAccess] NoSuchClass
# 6| 1: [LocalVariableDeclExpr] nsc
# 6| 0: [ClassInstanceExpr] <ClassInstanceExpr that calls a missing constructor>
# 6| -3: [TypeAccess] NoSuchClass
# 7| 1: [ReturnStmt] return ...
# 7| 0: [VarAccess] nsc

View File

@@ -0,0 +1 @@
semmle/code/java/PrintAst.ql

View File

@@ -0,0 +1,14 @@
import nosuchpackage.NoSuchClass;
public class Test {
public NoSuchClass test() {
NoSuchClass nsc = new NoSuchClass();
return nsc;
}
}
// Diagnostic Matches: Unexpected symbol for constructor: new NoSuchClass()
// Diagnostic Matches: 2 javac errors
// Diagnostic Matches: 2 errors during annotation processing

View File

@@ -0,0 +1 @@
//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17

View File

@@ -0,0 +1 @@
| file://:0:0:0:0 | 2 errors during annotation processing |

View File

@@ -0,0 +1,4 @@
import java
import semmle.code.java.Diagnostics
select any(Diagnostic d | not d.toString().matches("Not rewriting trap file for%"))

View File

@@ -0,0 +1,5 @@
module module.with.wrong.name {
exports somepkg;
}
// Diagnostic Matches: 2 errors during annotation processing

View File

@@ -0,0 +1 @@
//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17

View File

@@ -0,0 +1,5 @@
package somepkg;
public class SomeClass {
public static void someMethod() {}
}

View File

@@ -0,0 +1,2 @@
| Test.java:0:0:0:0 | 1 javac errors |
| Test.java:4:13:4:30 | Unable to extract method reference Unavailable.f()::g with no owner type |

View File

@@ -0,0 +1,4 @@
import java
import semmle.code.java.Diagnostics
select any(Diagnostic d | not d.toString().matches("Not rewriting trap file for%"))

View File

@@ -0,0 +1,10 @@
public class Test {
public static void test() {
var x = Unavailable.f()::g;
}
}
// Diagnostic Matches: 1 javac errors
// Diagnostic Matches: Unable to extract method reference Unavailable.f()::g with no owner type

View File

@@ -0,0 +1 @@
//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17