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

AssertionError in MustCallInference::inferOwningField with @MustCallUnknown Fields #6480

Closed
iamsanjaymalakar opened this issue Mar 7, 2024 · 0 comments · Fixed by #6481
Closed
Assignees

Comments

@iamsanjaymalakar
Copy link
Member

Description:

The Checker Framework throws an AssertionError when attempting to infer the @Owning annotation for fields with a @MustCallUnknown annotation. This occurs during the processing of generic class fields when the MustCallInference method incorrectly handles fields marked as @MustCallUnknown, leading to a failure in the assertion due to both @CalledMethods({}) and mustCallValues being empty.

Steps to Reproduce:

Create a Java file with the following content:

class Generic<T> {
  public T data;

  public Generic(T data) {
    this.data = data;
  }
}

public class GenericClassFieldCrash {
  private void onPacket(Generic foo) {
    String.format("socket received: data '%s'", foo.data);
  }
}

Run the Checker Framework (version 3.43.0-SNAPSHOT) on this file with the Resource Leak Checker enabled. Or run the resource leak inference test suite.

./gradlew AinferResourceLeakAjavaTest

Expected Behavior:

The Checker Framework should either correctly infer the @Owning annotation for fields marked as @MustCallUnknown or conservatively avoid inferring an @Owning annotation to prevent an AssertionError.

Actual Behavior:

The Checker Framework crashes with an AssertionError, indicating a failure to handle @MustCallUnknown fields correctly during the inference process.

Stack Trace:

error: SourceChecker.typeProcess: unexpected Throwable (AssertionError) while processing /Users/sanjay/checker-framework/checker/tests/ainfer-resourceleak/non-annotated/GenericClassFieldCrash.java; message: Must-call obligation of owning field data is empty.
  ; The Checker Framework crashed.  Please report the crash.  Version: Checker Framework 3.43.0-SNAPSHOT, branch master, 2024-03-04, commit 7743e5d, dirty=true. 
  Compilation unit: /Users/sanjay/checker-framework/checker/tests/ainfer-resourceleak/non-annotated/GenericClassFieldCrash.java
  Last visited tree at line 10 column 1:
  public class GenericClassFieldCrash {
  Exception: java.lang.AssertionError: Must-call obligation of owning field data is empty.; java.lang.AssertionError: Must-call obligation of owning field data is empty.
  	at org.checkerframework.checker.resourceleak.MustCallInference.inferOwningField(MustCallInference.java:402)
  	at org.checkerframework.checker.resourceleak.MustCallInference.inferOwningForParamOrField(MustCallInference.java:716)
  	at org.checkerframework.checker.resourceleak.MustCallInference.inferOwningForRecieverOrFormalParamPassedToCall(MustCallInference.java:693)
  	at org.checkerframework.checker.resourceleak.MustCallInference.inferOwningFromInvocation(MustCallInference.java:796)
  	at org.checkerframework.checker.resourceleak.MustCallInference.runInference(MustCallInference.java:255)
  	at org.checkerframework.checker.resourceleak.MustCallInference.runMustCallInference(MustCallInference.java:213)
  	......
	  at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
	  at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)

Analysis:

The method MustCallInference::inferOwningField incorrectly assumes that a field with a @MustCallUnknown annotation can have a non-empty must-call obligation. This leads to an assertion failure when both the called methods annotation (cmAnno) and the must-call values (mustCallValues) are empty, despite passing the must-call consistency check.

Proposed Solution:

Modify the ResourceLeakAnnotatedTypeFactory::isFieldWithNonemptyMustCallValue method to return false for fields whose MC type is @MustCallUnknown. This change will conservatively avoid inferring an @Owning annotation. Thus, avoiding incorrectly inferring an @Owning annotation for fields marked as @MustCallUnknown, addressing the assertion error.

Environment:

Checker Framework Version: 3.43.0-SNAPSHOT
Java Version: JDK 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant