Skip to content
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

Prevent endless recursion, limit traversal depth. #6691

Merged
merged 2 commits into from
Dec 4, 2023

Conversation

sdedic
Copy link
Member

@sdedic sdedic commented Nov 13, 2023

Prevents infinite recursion through structures in project model, should fix #6674 and #4779. This is done by:

  • in dump of container (iterable, map, named container) avoids to dump a value equal to the container itself. This avoids simple recursions, such as with Path reported in Loading a Gradle project that uses SpotBugs plugin reports an unknown problem #6674
  • limits deph of structure traversal
  • wraps toString() calls in try-catch, so potential error (incl. stack overflow) does not abort and just dumps an object placeholder.

The other part delivers more error details to NB. Fo unexpected exceptions, stack traces are provided in the detail Report field. Reports are marked with severity so now the loader can declare unexpected (exception) fatal (error), nonfatal (warning) and info messaes.

Unexpected exceptions are presented in project problems with their (truncated to 100 lines) stacktraces. Build system exceptions (i.e. unresolved artifact) should not present stacktraces, but full details from the nested exceptions.

Just error and exception levels pop up project problems window and notifications. Stacktraces are received by NB and can be optionally logged (with appropriate loglevel) - useful for user diagnostic.

I have added a test that a project with Spotbugs plugin loads OK + checks to verify recursive structure avoidance.

@sdedic sdedic added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) Gradle [ci] enable "build tools" tests VSCode Extension [ci] enable VSCode Extension tests enterprise [ci] enable enterprise job labels Nov 13, 2023
@apache apache locked and limited conversation to collaborators Nov 13, 2023
@apache apache unlocked this conversation Nov 13, 2023
@sdedic sdedic self-assigned this Nov 14, 2023
@sdedic sdedic force-pushed the gradle/project-loading-recursion branch 2 times, most recently from 1061236 to f927c8c Compare November 14, 2023 11:01
@sdedic sdedic added this to the NB21 milestone Nov 14, 2023
@@ -226,15 +240,13 @@ public NbProjectInfo buildAll() {
// introspection is only allowed for gradle 7.4 and above.
// TODO: investigate if some of the instrospection could be done for earlier Gradles.
sinceGradle("7.0", () -> {
initIntrospection();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rearranging exception handling code uncovered an exception from detectTaskProperties silently ignored in NB19-20. Adding task properties to the conditional block for >= 7.0

@@ -539,19 +553,53 @@ private static boolean isPrimitiveOrString(Class c) {
* @param propertyTypes
* @param defaultValues
*/
private void startInspectObjectAndValues(Class clazz, Object object, String prefix, Map<String, Map<String, String>> globalTypes, Map<String, String> propertyTypes, Map<String, Object> defaultValues) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just force-reset depth at the start of the inspection (top-level object).

} else {
defaultValues.put(prefix + "." + k, Objects.toString(v)); // NOI18N
} else if (!v.equals(value)) {
defaultValues.put(prefix + "." + k, objectToString(v)); // NOI18N
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toString itself can cause StackOverflow with recursive structures.

@@ -420,10 +454,30 @@ private static GradleReport createReport(Throwable e) {
} else {
reported = e;
}
String cn = e.getClass().getName();
if (cn.contains("GradleScriptException") || cn.contains("ResolutionException")) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are common exception classes for failures reported by the build system itself, mark as 'user' or expected exceptions.

@sdedic sdedic requested review from dbalek and lkishalmi November 14, 2023 12:24
@sdedic sdedic linked an issue Nov 14, 2023 that may be closed by this pull request
@sdedic sdedic marked this pull request as ready for review November 15, 2023 08:57
Copy link
Contributor

@dbalek dbalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine.

@sdedic
Copy link
Member Author

sdedic commented Nov 29, 2023

@lkishalmi do the changes look sane ? I'd merge it in the following days

Copy link
Contributor

@lkishalmi lkishalmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally fine, just small but important change is required.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COMPATIBLE_CACHE_VERSION needs to be increased, in order to force reload the info from Gradle.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patched into dd185ef. Thanks.

@sdedic sdedic force-pushed the gradle/project-loading-recursion branch from f927c8c to d726811 Compare November 29, 2023 20:25
Copy link
Contributor

@lkishalmi lkishalmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@sdedic sdedic merged commit ca7b58b into apache:master Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enterprise [ci] enable enterprise job Gradle [ci] enable "build tools" tests Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) VSCode Extension [ci] enable VSCode Extension tests
Projects
None yet
3 participants