|
| 1 | +/* |
| 2 | + * SonarQube Java |
| 3 | + * Copyright (C) 2012-2024 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.java.checks; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import org.sonar.check.Rule; |
| 25 | +import org.sonar.java.model.ExpressionUtils; |
| 26 | +import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; |
| 27 | +import org.sonar.plugins.java.api.JavaFileScannerContext; |
| 28 | +import org.sonar.plugins.java.api.JavaVersion; |
| 29 | +import org.sonar.plugins.java.api.JavaVersionAwareVisitor; |
| 30 | +import org.sonar.plugins.java.api.semantic.MethodMatchers; |
| 31 | +import org.sonar.plugins.java.api.tree.BaseTreeVisitor; |
| 32 | +import org.sonar.plugins.java.api.tree.MethodInvocationTree; |
| 33 | +import org.sonar.plugins.java.api.tree.MethodTree; |
| 34 | +import org.sonar.plugins.java.api.tree.NewClassTree; |
| 35 | +import org.sonar.plugins.java.api.tree.Tree; |
| 36 | + |
| 37 | +@Rule(key = "S6881") |
| 38 | +public class BlockingOperationsInVirtualThreadsCheck extends IssuableSubscriptionVisitor implements JavaVersionAwareVisitor { |
| 39 | + |
| 40 | + /* |
| 41 | + * #### Disclaimers #### |
| 42 | + * |
| 43 | + * This rule would benefit from a call graph analysis to determine which blocking operations are executed in a thread. For proper |
| 44 | + * reporting, it would be important to report issues with a flow, clearly showing the path from the blocking operation to the thread |
| 45 | + * creation. While we already have tools to find reachable methods (see TreeHelper#findReachableMethodsInSameFile), they do not provide |
| 46 | + * the necessary information to build a flow and hence a report would be quite confusing. For now, the rule is limited to only very basic |
| 47 | + * cases. |
| 48 | + * |
| 49 | + * On the other hand, this rule may falsely trigger on some cases where the blocking operation is not executed in the thread, but e.g. |
| 50 | + * returned within a lambda or object to be executed later. I.e. a thread creating a runnable for later execution. |
| 51 | + */ |
| 52 | + |
| 53 | + private static final String JAVA_LANG_THREAD = "java.lang.Thread"; |
| 54 | + |
| 55 | + @Override |
| 56 | + public List<Tree.Kind> nodesToVisit() { |
| 57 | + return List.of(Tree.Kind.NEW_CLASS, Tree.Kind.METHOD_INVOCATION, Tree.Kind.METHOD); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean isCompatibleWithJavaVersion(JavaVersion version) { |
| 62 | + return version.isJava21Compatible(); |
| 63 | + } |
| 64 | + |
| 65 | + // This rule currently only supports a limited set of blocking operations, focused on core Java HTTP requests. |
| 66 | + private static final MethodMatchers blockingOperations = MethodMatchers.or( |
| 67 | + MethodMatchers.create() |
| 68 | + .ofTypes("java.net.URLConnection", "java.net.HttpURLConnection") |
| 69 | + .names("getResponseCode", "getResponseMessage") |
| 70 | + .withAnyParameters() |
| 71 | + .build(), |
| 72 | + MethodMatchers.create() |
| 73 | + .ofTypes(JAVA_LANG_THREAD) |
| 74 | + .names("sleep") |
| 75 | + .addParametersMatcher("long") |
| 76 | + .build()); |
| 77 | + |
| 78 | + private static final MethodMatchers threadCreationConstructors = MethodMatchers.create() |
| 79 | + .ofSubTypes(JAVA_LANG_THREAD) |
| 80 | + .constructor() |
| 81 | + .addParametersMatcher("java.lang.Runnable") |
| 82 | + .build(); |
| 83 | + |
| 84 | + private static final MethodMatchers platformThreadMethods = MethodMatchers.create() |
| 85 | + .ofTypes(JAVA_LANG_THREAD + "$Builder$OfPlatform") |
| 86 | + .names("start", "unstarted") |
| 87 | + .addParametersMatcher("java.lang.Runnable") |
| 88 | + .build(); |
| 89 | + |
| 90 | + // For cases where a method is overwritten to define a thread's behavior (instead of e.g. passing a lambda to a Thread's constructor). |
| 91 | + private static final MethodMatchers overwritableThreadMethods = MethodMatchers.create() |
| 92 | + .ofSubTypes(JAVA_LANG_THREAD) |
| 93 | + .names("run") |
| 94 | + .addWithoutParametersMatcher() |
| 95 | + .build(); |
| 96 | + |
| 97 | + @Override |
| 98 | + public void visitNode(Tree tree) { |
| 99 | + switch (tree.kind()) { |
| 100 | + case NEW_CLASS -> onConstructorFound((NewClassTree) tree); |
| 101 | + case METHOD_INVOCATION -> onMethodInvocationFound((MethodInvocationTree) tree); |
| 102 | + case METHOD -> onMethodFound((MethodTree) tree); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private void onConstructorFound(NewClassTree newClassTree) { |
| 107 | + if (threadCreationConstructors.matches(newClassTree)) { |
| 108 | + analyzeForIssues(newClassTree.arguments().get(0), newClassTree.identifier()); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private void onMethodInvocationFound(MethodInvocationTree mit) { |
| 113 | + if (platformThreadMethods.matches(mit)) { |
| 114 | + analyzeForIssues(mit.arguments().get(0), mit.methodSelect()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private void onMethodFound(MethodTree tree) { |
| 119 | + if (overwritableThreadMethods.matches(tree)) { |
| 120 | + analyzeForIssues(tree, tree.simpleName()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private void analyzeForIssues(Tree tree, Tree secondary) { |
| 125 | + var finder = new BlockingOperationFinder(); |
| 126 | + tree.accept(finder); |
| 127 | + finder.collectedBlockingOperations.forEach(mit -> reportIssue( |
| 128 | + ExpressionUtils.methodName(mit), |
| 129 | + "Use virtual threads for heavy blocking operations.", |
| 130 | + List.of(new JavaFileScannerContext.Location("Containing thread", secondary)), |
| 131 | + null)); |
| 132 | + } |
| 133 | + |
| 134 | + private static class BlockingOperationFinder extends BaseTreeVisitor { |
| 135 | + final List<MethodInvocationTree> collectedBlockingOperations = new ArrayList<>(); |
| 136 | + |
| 137 | + @Override |
| 138 | + public void visitMethodInvocation(MethodInvocationTree mit) { |
| 139 | + if (blockingOperations.matches(mit)) { |
| 140 | + collectedBlockingOperations.add(mit); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments