forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 6
feat(errorprone): enforce no-java.lang.Math rule at compile time #130
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
halibobo1205
wants to merge
1
commit into
release_v4.8.2
Choose a base branch
from
feat/forbid-java-lang-math-errorprone
base: release_v4.8.2
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.
+121
−93
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 was deleted.
Oops, something went wrong.
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
119 changes: 119 additions & 0 deletions
119
errorprone/src/main/java/errorprone/ForbidJavaLangMath.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,119 @@ | ||
| package errorprone; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import com.google.errorprone.BugPattern; | ||
| import com.google.errorprone.VisitorState; | ||
| import com.google.errorprone.bugpatterns.BugChecker; | ||
| import com.google.errorprone.matchers.Description; | ||
| import com.google.errorprone.util.ASTHelpers; | ||
| import com.sun.source.tree.IdentifierTree; | ||
| import com.sun.source.tree.MemberReferenceTree; | ||
| import com.sun.source.tree.MemberSelectTree; | ||
| import com.sun.source.tree.MethodInvocationTree; | ||
| import com.sun.source.tree.Tree; | ||
| import com.sun.tools.javac.code.Symbol; | ||
|
|
||
| /** | ||
| * Forbids any direct use of {@code java.lang.Math}. | ||
| * | ||
| * <p>{@code java.lang.Math} permits the JIT to use platform-specific intrinsics for some | ||
| * methods (notably the transcendental and floating-point ones), so results are not | ||
| * guaranteed to be bit-for-bit identical across CPUs / JVMs. In a consensus system that | ||
| * non-determinism can fork the chain. All math must therefore go through | ||
| * {@code org.tron.common.math.StrictMathWrapper}, which is backed by {@code java.lang.StrictMath} | ||
| * and produces reproducible results. | ||
| * | ||
| * <p>This checker replaces the previous regex-based {@code .github/workflows/math-check.yml} | ||
| * scan. It resolves symbols on the type-attributed AST, so it has no false positives from | ||
| * strings/comments or unrelated classes named {@code Math}, and it catches every usage form: | ||
| * <ul> | ||
| * <li>direct calls: {@code Math.max(a, b)}</li> | ||
| * <li>fully-qualified calls: {@code java.lang.Math.max(a, b)}</li> | ||
| * <li>statically-imported calls: {@code import static java.lang.Math.max; ... max(a, b)}</li> | ||
| * <li>method references: {@code Math::max}</li> | ||
| * <li>field access: {@code Math.PI}, {@code Math.E}</li> | ||
| * <li>statically-imported fields used bare: {@code import static java.lang.Math.PI; ... PI}</li> | ||
| * <li>class literals (reflection back door): {@code Math.class}</li> | ||
| * </ul> | ||
| * | ||
| * <p>{@code java.lang.StrictMath} itself is intentionally allowed — it is the deterministic | ||
| * primitive that {@code StrictMathWrapper} and the deprecated {@code MathWrapper} are built on. | ||
| * Those wrappers legitimately call {@code java.lang.Math}/{@code StrictMath} and are exempted via | ||
| * {@code @SuppressWarnings("ForbidJavaLangMath")}. | ||
| */ | ||
| @AutoService(BugChecker.class) | ||
| @BugPattern( | ||
| name = "ForbidJavaLangMath", | ||
| summary = "Direct use of java.lang.Math is forbidden: its results are not guaranteed to be " | ||
| + "bit-for-bit identical across platforms, which can break consensus. Use " | ||
| + "org.tron.common.math.StrictMathWrapper instead.", | ||
| severity = BugPattern.SeverityLevel.ERROR | ||
| ) | ||
| public class ForbidJavaLangMath extends BugChecker | ||
| implements BugChecker.MethodInvocationTreeMatcher, | ||
| BugChecker.MemberReferenceTreeMatcher, | ||
| BugChecker.MemberSelectTreeMatcher, | ||
| BugChecker.IdentifierTreeMatcher { | ||
|
|
||
| private static final String JAVA_LANG_MATH = "java.lang.Math"; | ||
|
|
||
| @Override | ||
| public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
| return flagIfMathMember(ASTHelpers.getSymbol(tree), tree); | ||
| } | ||
|
|
||
| @Override | ||
| public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) { | ||
| return flagIfMathMember(ASTHelpers.getSymbol(tree), tree); | ||
| } | ||
|
|
||
| @Override | ||
| public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) { | ||
| // Type-level references such as the class literal `Math.class` resolve to the Math | ||
| // ClassSymbol rather than a member, and are a back door to java.lang.Math via reflection | ||
| // (e.g. Math.class.getMethod("sin").invoke(...)). Flag them explicitly. | ||
| if (tree.getIdentifier().contentEquals("class") | ||
| && isJavaLangMathClass(ASTHelpers.getSymbol(tree.getExpression()))) { | ||
| return describeMatch(tree); | ||
| } | ||
| // Method selects (Math.max) are already reported via matchMethodInvocation / | ||
| // matchMemberReference; only flag field selects (Math.PI, Math.E) here to avoid | ||
| // double-reporting the same usage. | ||
| Symbol sym = ASTHelpers.getSymbol(tree); | ||
| if (!(sym instanceof Symbol.VarSymbol)) { | ||
| return Description.NO_MATCH; | ||
| } | ||
| return flagIfMathMember(sym, tree); | ||
|
halibobo1205 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Override | ||
| public Description matchIdentifier(IdentifierTree tree, VisitorState state) { | ||
| // Catches statically-imported constants referenced bare (e.g. `import static | ||
| // java.lang.Math.PI; ... double c = PI;`). Statically-imported *methods* are caught by | ||
| // matchMethodInvocation, so restrict this to fields to avoid double-reporting. | ||
| Symbol sym = ASTHelpers.getSymbol(tree); | ||
| if (!(sym instanceof Symbol.VarSymbol)) { | ||
| return Description.NO_MATCH; | ||
| } | ||
| return flagIfMathMember(sym, tree); | ||
| } | ||
|
|
||
| private static boolean isJavaLangMathClass(Symbol sym) { | ||
| return sym instanceof Symbol.ClassSymbol | ||
| && ((Symbol.ClassSymbol) sym).getQualifiedName().contentEquals(JAVA_LANG_MATH); | ||
| } | ||
|
|
||
| private Description flagIfMathMember(Symbol sym, Tree tree) { | ||
| if (sym == null) { | ||
| return Description.NO_MATCH; | ||
| } | ||
| Symbol.ClassSymbol enclosingClass = ASTHelpers.enclosingClass(sym); | ||
| if (enclosingClass == null) { | ||
| return Description.NO_MATCH; | ||
| } | ||
| if (enclosingClass.getQualifiedName().contentEquals(JAVA_LANG_MATH)) { | ||
| return describeMatch(tree); | ||
| } | ||
| return Description.NO_MATCH; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.