Flow Analysis builds paths through the analyzed code to detect different kinds of problems. Since the analysis of all possible paths that span through the whole application may be infeasible, you can set up the desired level of depth of analysis. A deeper analysis will result in more findings, but the performance will be slower and the memory consumption will increase slightly.
You can specify the depth of analysis by using the test configuration interface in DTP. Go to Report Center> Test Configurations> Static Analysis> Flow Analysis Advanced Settings> Performance> Depth of analysis and choose one of the following options by selecting a radio button:
The depth of Flow Analysis is set to Standard by default.
Apart from the depth of analysis, Flow Analysis uses an additional timeout guard to ensure the analysis completes within a reasonable time. An appropriate strategy can be set by using the test configuration interface in DTP. Go to Report Center> Test Configurations> Static Analysis> Flow Analysis Advanced Settings> Performance> Strategy for Timeouts and choose one of the following options by selecting a radio button:
The default timeout option is time set to 60 seconds. To get information about the Flow Analysis timeouts that occurred during the analysis, review the Setup Problems section of the report generated after the analysis.
In this mode, analysis data is written to disk. Swapping of analysis data uses the same persistent storage and is done in a similar process as incremental analysis. If the analysis is run on a large project, the analysis data that represents a semantical model of the analyzed source code may consume all the memory available for running Flow Analysis. If this occurs, Flow Analysis will remove from memory parts of the analysis data that are not currently necessary and reread it from disk later.
In general, we recommend runningJtestin a large JVM heap configured with the Xmx JVM option. This is to minimize swapping, which results in greater performance. If sufficient memory is available, swapping of analysis data may be disabled, which may speed up code analysis.
You can enable or disable the mode by using the test configuration interface in DTP:
Enable swapping of analysis data to disk: Enabled by default. If this option is disabled, it may result in faster analysis, if you are running Flow Analysis analysis on small to moderate size projects that do not require a lot of memory or when plenty of memory is available (for example, for 64-bit systems).
You can configure the following options by using the test configuration interface in DTP:
The Null-checking methods option allows you to specify the expected return value when a null parameter is passed to a method. This reduces false positives and excessive paths that would normally be built when the return value for null variables are unknown.
Select the Enabled checkbox and provide the following information:
Flow Analysis will analyze methods in the analysis scope. Null-checking method parameterizations are required when the methods are out of the scope of the current analysis (e.g. third-party libraries). The classes in the following example are defined in a different assembly and are outside the scope of the analysis (UString
). The class uses static methods to manipulate code with Java strings in various ways, including the UString.isEmptyOrNull(String)
, which is defined:
public class UString { static boolean isEmptyOrNull(String variable) { if ((variable == null) || (variable.length() == 0)) { return true; } return false; } } |
And there is the following class, which is analyzed by Flow Analysis’s BD-EXCEPT-NP rule:
public class SomeClass { void someMethod(String variable) { boolean flag = variable == null; // violation search starts here if (UString.isEmptyOrNull(variable)) { /** * In case variable is null, we will always get to this branch of the if clause. */ System.out.println("String is empty"); } else { variable.toString(); // FALSE POSITIVE BD-EXCEPT-NP VIOLATION } } } |
Flow Analysis assumes that the return value for the method isEmptyOrNull()
is unknown. This is because the method is not in the analysis scope. Flow Analysis will analyze the then
and else
branches of the if
statement and find a violation in one of these statements. Calling this method, however, returns true when the variable is null.
By adding UString.isEmptyOrNull()
to the list of methods in the Null-checking Methods parameterization with the specified return value, Flow Analysis will not report a violation if the tracked variable is passed to this method. This is because the wrong branch of the if
statement will not be analyzed, resulting in an avoided false positive.
Methods added to this parameterization should be static methods that have a primitive boolean return value. This restriction is managed to avoid excessive complications in parameterization and ensuring that there are no other variables that could affect the result of the null-checking method.
The Resources tab allows you to define which resources the BD.RES category (Resources) rules should check. These rules check for the correct usage of all resources that are defined and enabled on this tab.
Click the arrow to expand the Resource Allocators and Resource Closers tabs and complete the tables that open with the information about allocators and closers. Details about completing these tabs are provided below.
The Resource allocators table can be completed with the descriptors of methods that can produce a resource. The table has the following columns:
It is common that allocation methods return an error code to indicate allocation failure. When an allocation method returns a resource, a NULL value normally indicates an allocation failure. When Flow Analysis is looking for resource leaks, it needs to understand if allocation succeeded or failed; this helps it report only missing calls to deallocation methods on paths where allocation actually occurred. In cases where a resource allocator method returns a resource, Flow Analysis assumes that the resource is successfully allocated if the returned value is not NULL.
The Resource closers table can be completed with the descriptors of methods that can close a resource. The table has the following columns:
The Always analyzed methods option allows you to define methods that will always be analyzed when encountered on the execution path. This helps you ensure that the rule will analyze methods it would normally not enter when checking a given path.
Select the Enable checkbox and complete the table with the following information:
The Spring HTTP request annotations that taint method parameters option allows you to configure Spring annotations that taint one or more parameters of a method. This option only affects the rules from the BD.SECURITY category that have the 'Sources of tainted data' parameter and that parameter is configured to treat data from 'Spring http requests' as tainted.
You can configure annotations in one of the following tables:
The tables have the following columns:
The Injection annotations option allows you to define the annotations that are used for injection. As a result, if a field or setter method is annotated, Flow Analysis will not consider the field value to be uninitialized or null.
The table has the following columns:
Analyzing projects that use the Spring framework does not require additional configuration. Flow Analysis can automatically recognize the Spring framework features and adapt reported violations to provide accurate results.
The following sections provide details on how Flow Analysis handles the framework's features and how it affects violations reported by Flow Analysis rules.
Flow Analysis can recognize annotations that are used for Spring injection, which automatically initialize annotated fields. If a filed is annotated, Flow Analysis will not consider the field value to be uninitialized or null. This prevents the following rules from reporting false positives:
You can view the default list of injection annotations that auto-initialize fields or customize the list by modifying your test configuration (Static Analysis Settings > Inversion of Control Frameworks Settings).
Flow Analysis can identify when the Spring controller classes and HTTP request processing methods are used. As a result:
Flow Analysis assumes that Java Validation API is insufficient to ensure proper validation of tainted data. Java Validation is typically used to validate data obtained from external sources, while tainted data must be properly sanitized before it is used in a particular context. This means that proper validation requires that different validation methods be used depending on whether the content of the article is being passed the SQL query or is embedded in the HTTP response.
Flow Analysis can recognize Spring-specific sources of potentially tainted data. Flow Analysis rules that detect tainted data trigger when data obtained from the following sources is passed to a dangerous method without validation:
For rules that assume that Spring http requests may be sources of tainted data, you can specify Spring annotations that taint one or more parameters of a method; see Specifying Spring Annotations That Taint Method Parameters for details.