-
Notifications
You must be signed in to change notification settings - Fork 2k
Java: Add new quality query to detect finalize calls
#19075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jcogs33
merged 22 commits into
github:main
from
jcogs33:jcogs33/java/do-not-use-finalizers
Apr 22, 2025
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
56ea9b6
Java: move original files
9a6e241
Java: update to only find 'finalize' calls and add 'super.finalize' e…
d9482ae
Java: update tests to use inline expectations
c689a0e
Java: add more test cases
dd57d1a
Java: add quality tag
44445db
Java: minor refactor
2e25498
Java: add change note
f73eda0
Java: add previous-id and change 'use' to 'call'
ed22a16
Java: exclude overloads of finalize
3631df0
Java: add to code-quality suite
caf21a8
Java: update qhelp and add 'performace' tag
416643c
Java: update qhelp recommendation and example
e621f9f
Java: update comments in tests
c4b8396
fix typo in query description
jcogs33 1a2c34d
Java: update qhelp implementation notes for clarity
05d7b9a
Java: add reliability tag
0380279
Java: update qhelp implementation notes for more clarity
fc21abc
Java: update qhelp implementation notes to say 'method declarations'
798907d
Java: remove change note
2b91605
Apply docs review suggestion
jcogs33 72d49f2
Merge branch 'main' into jcogs33/java/do-not-use-finalizers
jcogs33 3aa6b49
Java: Add new query to java-code-quality.qls.expected
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Java: move original files
- Loading branch information
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
java/ql/src/Violations of Best Practice/Undesirable Calls/DoNotUseFinalizers.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # J-FIN-002: Calling garbage collection methods in application code may cause inconsistent program state | ||
|
|
||
| Calling garbage collection or finalizer methods in application code may cause inconsistent program state or unpredicatable behavior. | ||
|
|
||
| ## Overview | ||
|
|
||
| Triggering garbage collection explicitly may either have no effect or may trigger unnecessary garbage collection, leading to erratic behavior or deadlock. | ||
|
|
||
| ## Recommendation | ||
|
|
||
| Avoid calling finalizers and garbage collection methods in application code. Allow the JVM to determine a garbage collection schedule instead. | ||
|
|
||
| ## Example | ||
|
|
||
| ```java | ||
| public class Test { | ||
| void f() throws Throwable { | ||
| System.gc(); // NON_COMPLIANT | ||
| Runtime.getRuntime().gc(); // NON_COMPLIANT | ||
| System.runFinalizersOnExit(true); //NON_COMPLIANT | ||
| this.finalize(); // NON_COMPLIANT | ||
| } | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| # Implementation Notes | ||
|
|
||
| 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). | ||
|
|
||
| ## References | ||
|
|
||
| - [Do not use finalizers](https://wiki.sei.cmu.edu/confluence/display/java/MET12-J.+Do+not+use+finalizers) | ||
| - [CWE-586](https://cwe.mitre.org/data/definitions/586) |
25 changes: 25 additions & 0 deletions
25
java/ql/src/Violations of Best Practice/Undesirable Calls/DoNotUseFinalizers.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** | ||
| * @id java/do-not-use-finalizers | ||
| * @name J-D-004: Calling garbage collection methods in application code may cause inconsistent program state | ||
| * @description Calling garbage collection or finalizer methods in application code may cause | ||
| * inconsistent program state or unpredicatable behavior. | ||
| * @kind problem | ||
| * @precision high | ||
| * @problem.severity error | ||
| * @tags correctness | ||
| * external/cwe/cwe-586 | ||
| */ | ||
|
|
||
| import java | ||
|
|
||
| from MethodCall c, Method m | ||
| where | ||
| c.getMethod() = m and | ||
| ( | ||
| m.hasQualifiedName("java.lang", "System", ["gc", "runFinalizersOnExit"]) | ||
| or | ||
| m.hasQualifiedName("java.lang", "Runtime", "gc") | ||
| or | ||
| m.hasQualifiedName(_, _, "finalize") | ||
| ) | ||
| select c, "Call to prohibited method that may modify the JVM's garbage collection process." |
3 changes: 3 additions & 0 deletions
3
java/ql/test/query-tests/DoNotUseFinalizers/DoNotUseFinalizers.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| | Test.java:3:9:3:19 | gc(...) | Call to prohibited method that may modify the JVM's garbage collection process. | | ||
| | Test.java:4:9:4:33 | gc(...) | Call to prohibited method that may modify the JVM's garbage collection process. | | ||
| | Test.java:5:9:5:23 | finalize(...) | Call to prohibited method that may modify the JVM's garbage collection process. | |
1 change: 1 addition & 0 deletions
1
java/ql/test/query-tests/DoNotUseFinalizers/DoNotUseFinalizers.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/J-FIN-002/DoNotUseFinalizers.ql | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| public class Test { | ||
| void f() throws Throwable { | ||
| System.gc(); // NON_COMPLIANT | ||
| Runtime.getRuntime().gc(); // NON_COMPLIANT | ||
| this.finalize(); // NON_COMPLIANT | ||
| // this is removed in Java 11 | ||
| //System.runFinalizersOnExit(true); // NON_COMPLIANT | ||
| } | ||
|
|
||
| void f1() throws Throwable { | ||
| f(); // COMPLIANT | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.