-
Notifications
You must be signed in to change notification settings - Fork 11
Rule for Request #707: AvoidRecreatingMessageDigests - MessageDigest.getInstance is expensive because of classloading #710
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
Open
jborgers
wants to merge
1
commit into
pmd7
Choose a base branch
from
pmd7-issue707
base: pmd7
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
There are no files selected for viewing
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
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
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 |
|---|---|---|
|
|
@@ -888,6 +888,59 @@ class Good { | |
| </properties> | ||
| </rule> | ||
|
|
||
| <rule name="AvoidRecreatingMessageDigests" | ||
| language="java" | ||
| message="Avoid re-creating MessageDigest objects, this is expensive." | ||
| class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" | ||
|
|
||
| externalInfoUrl="https://github.com/jborgers/PMD-jPinpoint-rules/tree/pmd7/docs/JavaCodePerformance.md#iuosf02"> | ||
| <description>Problem: Creating a MessageDigest object is expensive because of loading of algorithms and other classes. Additionally, it uses synchronized which leads to lock contention when used with multiple threads. | ||
| Solution: Since MessageDigest is not thread-safe, an instance cannot simply be shared among threads. Create the MessageDigest only once and clone it for each use. MessageDigest.clone() is efficient and typically supported by the provider, yet note that support is not guaranteed. | ||
| (jpinpoint-rules)</description> | ||
| <priority>2</priority> | ||
| <properties> | ||
| <property name="tags" value="cpu,io,jpinpoint-rule,performance,sustainability-high" type="String" description="classification"/> | ||
| <property name="xpath"> | ||
| <value><![CDATA[ | ||
| //MethodDeclaration[not((@Name='main' and @Static=true()) or ModifierList/Annotation/@SimpleName='PostConstruct')]//( | ||
| LocalVariableDeclaration[ClassType[pmd-java:typeIs('java.security.MessageDigest')]]/VariableDeclarator/MethodCall[@MethodName='getInstance'] | ||
| | MethodCall[starts-with(@MethodName,'digest')]/ConstructorCall[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')] | ||
| | LocalVariableDeclaration[ClassType[pmd-java:typeIs('java.security.MessageDigest')]]//MethodCall/TypeExpression[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')] | ||
| | MethodCall[starts-with(@MethodName,'sha') or starts-with(@MethodName,'md')]/TypeExpression[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document why these two method prefixes
Comment on lines
+906
to
+909
|
||
| ) | ||
| ]]></value> | ||
| </property> | ||
| </properties> | ||
| <example> | ||
| <![CDATA[ | ||
| import java.security.MessageDigest; | ||
| import org.apache.commons.codec.digest.DigestUtils; | ||
| import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_256; | ||
|
|
||
| class MessageDigestIssue { | ||
| MessageDigest mdField = MessageDigest.getInstance("SHA-256"); | ||
| byte[] dataToDigest = "helloworld".getBytes("UTF-8"); | ||
|
|
||
| byte[] bad() { | ||
| MessageDigest mdLocal = MessageDigest.getInstance("SHA-256"); // bad | ||
| return mdLocal.digest(dataToDigest); | ||
| } | ||
| byte[] good() { | ||
| MessageDigest mdLocal = mdField.clone(); | ||
| return mdLocal.digest(dataToDigest); | ||
| } | ||
| byte[] badDigestUtils() { | ||
| return new DigestUtils(SHA_256).digest(dataToDigest); // bad | ||
| } | ||
| byte[] goodDigestUtils() { | ||
| MessageDigest mdLocal = mdField.clone(); | ||
| return DigestUtils.digest(mdLocal, dataToDigest); | ||
| } | ||
| } | ||
| ]]> | ||
| </example> | ||
| </rule> | ||
|
|
||
| <rule name="AvoidRecreatingSecurityProviders" | ||
| language="java" | ||
| message="Avoid re-creating security providers, this is expensive." | ||
|
|
@@ -903,7 +956,7 @@ class Good { | |
| <property name="xpath"> | ||
| <value><![CDATA[ | ||
| //MethodDeclaration | ||
| [not((@Name='main' and @Static=true())or ModifierList/Annotation/@SimpleName='PostConstruct' | ||
| [not((@Name='main' and @Static=true()) or ModifierList/Annotation/@SimpleName='PostConstruct' | ||
| or .//IfStatement//InfixExpression | ||
| [@Operator='=='][VariableAccess[pmd-java:typeIs('java.security.Provider')] and NullLiteral] | ||
| )] | ||
|
|
||
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
6 changes: 6 additions & 0 deletions
6
...t/java/com/jpinpoint/perf/lang/java/ruleset/common/AvoidRecreatingMessageDigestsTest.java
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,6 @@ | ||
| package com.jpinpoint.perf.lang.java.ruleset.common; | ||
|
|
||
| import net.sourceforge.pmd.test.PmdRuleTst; | ||
|
|
||
| public class AvoidRecreatingMessageDigestsTest extends PmdRuleTst { | ||
| } |
94 changes: 94 additions & 0 deletions
94
...sources/com/jpinpoint/perf/lang/java/ruleset/common/xml/AvoidRecreatingMessageDigests.xml
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,94 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <test-data | ||
| xmlns="http://pmd.sourceforge.net/rule-tests" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd"> | ||
| <test-code> | ||
| <description>violation: avoid recreating message digests</description> | ||
| <expected-problems>5</expected-problems> | ||
| <expected-linenumbers>10,19,23,31,36</expected-linenumbers> | ||
| <code><![CDATA[ | ||
| import java.security.MessageDigest; | ||
| import org.apache.commons.codec.digest.DigestUtils; | ||
| import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_256; | ||
|
|
||
| class MessageDigestIssue { | ||
| MessageDigest mdField = MessageDigest.getInstance("SHA-256"); | ||
| byte[] dataToDigest = "helloworld".getBytes("UTF-8"); | ||
|
|
||
| byte[] bad() { | ||
| MessageDigest mdLocal = MessageDigest.getInstance("SHA-256"); // bad1 | ||
| return mdLocal.digest(dataToDigest); | ||
| } | ||
| byte[] good() { | ||
| MessageDigest mdLocal = mdField.clone(); | ||
| return mdLocal.digest(dataToDigest); | ||
| } | ||
|
|
||
| byte[] bad1DigestUtils() { | ||
| return new DigestUtils(SHA_256).digest(dataToDigest); // bad2 | ||
| } | ||
|
|
||
| String bad2DigestUtils() { | ||
| return new DigestUtils(SHA_256).digestAsHex(dataToDigest); //bad3 | ||
| } | ||
|
|
||
| MessageDigest good1DigestUtils() { | ||
| return DigestUtils.getMd5Digest(); // okay, can be used to initialize once | ||
|
jborgers marked this conversation as resolved.
|
||
| } | ||
|
|
||
| byte[] bad3DigestUtils() { | ||
| MessageDigest mdLocal = DigestUtils.getMd5Digest(); // bad4, as local var | ||
| return mdLocal.digest(dataToDigest); | ||
| } | ||
|
|
||
| byte[] bad4DigestUtils() { | ||
| return DigestUtils.sha256(dataToDigest); // bad5 | ||
| } | ||
|
|
||
| // for the simple good cases I see no advantage of using DigestUtils. | ||
| // But for others like streaming it seems useful, to add | ||
| byte[] good2DigestUtils() { | ||
| MessageDigest mdLocal = mdField.clone(); | ||
| return DigestUtils.digest(mdLocal, dataToDigest); | ||
| } | ||
|
|
||
| MessageDigest good3DigestUtils() { | ||
| MessageDigest mdLocal = mdField.clone(); | ||
| return DigestUtils.updateDigest(mdLocal, dataToDigest); | ||
| } | ||
| } | ||
| ]]></code> | ||
| </test-code> | ||
| <test-code> | ||
| <description>no violation: creating a MessageDigest in main is allowed</description> | ||
| <expected-problems>0</expected-problems> | ||
| <code><![CDATA[ | ||
| import java.security.MessageDigest; | ||
|
|
||
| class MessageDigestInMain { | ||
| public static void main(String[] args) throws Exception { | ||
| MessageDigest mdLocal = MessageDigest.getInstance("SHA-256"); // okay, in main | ||
| mdLocal.digest("helloworld".getBytes("UTF-8")); | ||
| } | ||
| } | ||
| ]]></code> | ||
| </test-code> | ||
| <test-code> | ||
| <description>no violation: creating a MessageDigest in a @PostConstruct method is allowed</description> | ||
| <expected-problems>0</expected-problems> | ||
| <code><![CDATA[ | ||
| import java.security.MessageDigest; | ||
| import javax.annotation.PostConstruct; | ||
|
|
||
| class MessageDigestInPostConstruct { | ||
| private MessageDigest mdField; | ||
|
|
||
| @PostConstruct | ||
| void init() throws Exception { | ||
| mdField = MessageDigest.getInstance("SHA-256"); // okay, in @PostConstruct | ||
| } | ||
| } | ||
| ]]></code> | ||
| </test-code> | ||
| </test-data> | ||
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.