Class ResourceLeakChecker

All Implemented Interfaces:
Processor, OptionConfiguration

@SupportedOptions({"permitStaticOwning","permitInitializationLeak","countMustCall","resourceLeakIgnoredExceptions","noCreatesMustCallFor","noLightweightOwnership","noResourceAliases","enableWpiForRlc","enableReturnsReceiverForRlc"}) public class ResourceLeakChecker extends AggregateChecker
The entry point for the Resource Leak Checker. This checker only counts the number of MustCall annotations. This checker calls the RLCCalledMethodsChecker as a direct subchecker, which then in turn calls the MustCallChecker as a subchecker, and afterwards this checker traverses the CFG to check whether all MustCall obligations are fulfilled.

The checker hierarchy is: this "empty" RLC → RLCCalledMethodsChecker → MustCallChecker.

The MustCallChecker is a subchecker of the RLCCm checker (instead of a sibling), since we want them to operate on the same CFG (so we can get both a CM and MC store for a given CFG block), which only works if one of them is a subchecker of the other.

  • Field Details

    • COUNT_MUST_CALL

      public static final String COUNT_MUST_CALL
      Command-line option for counting how many must-call obligations were checked by the Resource Leak Checker, and emitting the number after processing all files. Used for generating tables for a research paper. Not of interest to most users.

      A must-call obligation is counted if the checker tracks it and the static type of the expression that holds the obligation when it is created is a JDK class. That expression is one of the following:

      • an invocation whose result has a must-call obligation, other than an invocation of a method that returns its receiver (@This), which creates no counted obligation,
      • an owning formal parameter whose declared type has a must-call obligation, or
      • a must-call-alias formal parameter, whatever its declared type.

      Such an obligation is found; the first number that the checker emits is the number of found obligations. A found obligation is verified unless the checker fails to verify it; the second number is the number of verified obligations.

      The class of the run-time object is not considered. For example, Closeable c = new MyCloseable(); creates no counted obligation, because the type of new MyCloseable() is not a JDK class. By contrast, the formal parameter of void m(@Owning Closeable c) has a counted obligation, even if every caller passes a MyCloseable.

      A wrapper, such as new BufferedInputStream(in), shares the obligation of the object that it wraps. The wrapper is counted only if the obligation of the wrapped object is not counted on some path to the wrapper. A wrapper of an object that the checker does not track, such as a field or an object without a must-call obligation, is not counted, because the checker does not track the wrapper either. For example, new BufferedReader(new InputStreamReader(System.in)) creates no counted obligation. An invocation of a CreatesMustCallFor method creates no counted obligation; the obligation of its target retains the target's count. An obligation that the checker fails to verify is not counted as verified, even if the checker does not report the failure (for example, because a warning is suppressed, or because the -AskipUses or -AonlyUses command-line option makes the checker skip the code at which it would report the failure).

      An owning field is not counted. Storing a resource in an owning field verifies the obligation of the resource.

      See Also:
    • ENABLE_WPI_FOR_RLC

      public static final String ENABLE_WPI_FOR_RLC
      Ordinarily, when the -Ainfer flag is used, whole-program inference is run for every checker and sub-checker. However, the Resource Leak Checker is different. The -Ainfer flag enables the RLC's own (non-WPI) inference mechanism (MustCallInference). To use WPI in addition to this mechanism for its sub-checkers, use the -AenableWpiForRlc flag, which is intended only for testing and experiments.
      See Also:
    • ENABLE_RETURNS_RECEIVER

      public static final String ENABLE_RETURNS_RECEIVER
      The Returns Receiver Checker is disabled by default for the Resource Leak Checker, as it adds significant overhead and typically provides little benefit. To enable it, use the -AenableReturnsReceiverForRlc flag.
      See Also:
  • Constructor Details

    • ResourceLeakChecker

      public ResourceLeakChecker()
      Creates a ResourceLeakChecker.
  • Method Details

    • getSupportedCheckers

      protected Set<Class<? extends SourceChecker>> getSupportedCheckers()
      Description copied from class: AggregateChecker
      Returns the list of independent subcheckers to be run together. An aggregate checker overrides this method to indicate its immediate subcheckers.
      Specified by:
      getSupportedCheckers in class AggregateChecker
      Returns:
      the list of immediate subcheckers to be run
    • countFoundObligation

      public @Nullable Object countFoundObligation(Object key, TypeMirror type)
      Record that the checker found the must-call obligation that key identifies, if the COUNT_MUST_CALL command-line option was supplied and type is a JDK class. Recording the same key again has no effect.
      Parameters:
      key - an object that identifies a must-call obligation, such as the element of an owning formal parameter. Keys are compared with equals.
      type - the static type of the expression that holds the obligation when it is created
      Returns:
      key if the option was supplied and type is a JDK class (even if key was already recorded), or null otherwise
    • countFailedObligation

      public void countFailedObligation(@Nullable Object key)
      Record that the checker failed to verify the must-call obligation that key identifies. The failure is counted only if countFoundObligation(java.lang.Object, javax.lang.model.type.TypeMirror) records the same key, before or after this call. Recording the same key again has no effect.
      Parameters:
      key - an object that identifies a must-call obligation, as for countFoundObligation(java.lang.Object, javax.lang.model.type.TypeMirror), or null to do nothing
    • typeProcessingOver

      public void typeProcessingOver()
      Description copied from class: AbstractTypeProcessor
      A method to be called once all the classes are processed.

      Subclasses can override this method to do any aggregate analysis (e.g., generate a report, persistence) or resource deallocation.

      Method AbstractTypeProcessor.getCompilerLog() can be used to access the number of compiler errors.

      Overrides:
      typeProcessingOver in class SourceChecker
    • getSuppressWarningsPrefixes

      public NavigableSet<String> getSuppressWarningsPrefixes()
      Description copied from class: SourceChecker
      Returns a modifiable set of lower-case strings that are prefixes for SuppressWarnings strings.

      The collection must not be empty and must not contain only SourceChecker.SUPPRESS_ALL_PREFIX.

      Overrides:
      getSuppressWarningsPrefixes in class SourceChecker
      Returns:
      non-empty modifiable set of lower-case prefixes for SuppressWarnings strings