Skip to content

Commit 56ea9b6

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Java: move original files
1 parent dc242da commit 56ea9b6

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# J-FIN-002: Calling garbage collection methods in application code may cause inconsistent program state
2+
3+
Calling garbage collection or finalizer methods in application code may cause inconsistent program state or unpredicatable behavior.
4+
5+
## Overview
6+
7+
Triggering garbage collection explicitly may either have no effect or may trigger unnecessary garbage collection, leading to erratic behavior or deadlock.
8+
9+
## Recommendation
10+
11+
Avoid calling finalizers and garbage collection methods in application code. Allow the JVM to determine a garbage collection schedule instead.
12+
13+
## Example
14+
15+
```java
16+
public class Test {
17+
void f() throws Throwable {
18+
System.gc(); // NON_COMPLIANT
19+
Runtime.getRuntime().gc(); // NON_COMPLIANT
20+
System.runFinalizersOnExit(true); //NON_COMPLIANT
21+
this.finalize(); // NON_COMPLIANT
22+
}
23+
}
24+
25+
```
26+
27+
# Implementation Notes
28+
29+
This rule covers a concept related to J-FIN-001; this rule is focused on the use of existing finalizer invocations rather than attempts to write a custom implementation (J-FIN-001).
30+
31+
## References
32+
33+
- [Do not use finalizers](https://wiki.sei.cmu.edu/confluence/display/java/MET12-J.+Do+not+use+finalizers)
34+
- [CWE-586](https://cwe.mitre.org/data/definitions/586)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @id java/do-not-use-finalizers
3+
* @name J-D-004: Calling garbage collection methods in application code may cause inconsistent program state
4+
* @description Calling garbage collection or finalizer methods in application code may cause
5+
* inconsistent program state or unpredicatable behavior.
6+
* @kind problem
7+
* @precision high
8+
* @problem.severity error
9+
* @tags correctness
10+
* external/cwe/cwe-586
11+
*/
12+
13+
import java
14+
15+
from MethodCall c, Method m
16+
where
17+
c.getMethod() = m and
18+
(
19+
m.hasQualifiedName("java.lang", "System", ["gc", "runFinalizersOnExit"])
20+
or
21+
m.hasQualifiedName("java.lang", "Runtime", "gc")
22+
or
23+
m.hasQualifiedName(_, _, "finalize")
24+
)
25+
select c, "Call to prohibited method that may modify the JVM's garbage collection process."
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| Test.java:3:9:3:19 | gc(...) | Call to prohibited method that may modify the JVM's garbage collection process. |
2+
| Test.java:4:9:4:33 | gc(...) | Call to prohibited method that may modify the JVM's garbage collection process. |
3+
| Test.java:5:9:5:23 | finalize(...) | Call to prohibited method that may modify the JVM's garbage collection process. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/J-FIN-002/DoNotUseFinalizers.ql
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Test {
2+
void f() throws Throwable {
3+
System.gc(); // NON_COMPLIANT
4+
Runtime.getRuntime().gc(); // NON_COMPLIANT
5+
this.finalize(); // NON_COMPLIANT
6+
// this is removed in Java 11
7+
//System.runFinalizersOnExit(true); // NON_COMPLIANT
8+
}
9+
10+
void f1() throws Throwable {
11+
f(); // COMPLIANT
12+
}
13+
}

0 commit comments

Comments
 (0)