Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This topic covers how to analyze the bugs reported by BugDetectiveFlow Analysis

In this section:

Table of Contents
maxLevel2

Using

...

Flow Analysis
Anchor
Using BugDetective Static Analysis
Using BugDetective Static Analysis

This topic explains how to run BugDetective Flow Analysis to expose bugs such as use of uninitialized memory, null pointer dereferencing, division by zero, memory and resource leaks.

Info
iconfalse

Important

An optional Flow Analysis license (with C++test Server Edition) is required to use BugDetectiveFlow Analysis.

Running

...

Flow Analysis

BugDetective Flow Analysis is a new type of static analysis technology that uses several analysis techniques, including simulation of application execution paths, to identify paths that could trigger runtime defects. Defects detected include use of uninitialized memory, null pointer dereferencing, division by zero, memory and resource leaks.

Since this analysis involves identifying and tracing complex paths, it exposes bugs that typically evade syntax-based static code analysis and unit testing, and would be difficult to find through manual testing or inspection. BugDetective’s Flow Analysis' ability to expose bugs without executing code is especially valuable for users with legacy code bases and embedded code (where runtime detection of such errors is not effective or possible).For a more detailed description of BugDetective, see the Concepts and Terms topic BugDetective (Data Flow) Static Analysis.

Info
iconfalse

Analyzing Headers

C++test does not directly analyze headers unless they are included by a source file under test. See How do I analyze header files/what files are analyzed? for details.

...

The general procedure for running BugDetective isFlow Analysis is:

  1. Identify or create a Test Configuration with your preferred BugDetective standard Flow Analysis standard analysis settings.
  2. Start the test using the preferred Test Configuration.
  3. Review and respond to the results.
  4. (Optional) Fine-tune BugDetective Flow Analysis settings as needed.

Configuring Batch-Mode

...

Flow Analysis Analysis with cpptestcli

Regularly-schedule batch-mode BugDetective analysis Flow Analysis analysis should simply execute a built-in or custom Test Configuration that analyzes your project according to the BugDetective rules Flow Analysis rules important to your team.

For example:

...

See Testing from the Command Line Interface for more details on configuring batch-mode tests.

Anchor
mode
mode
Running

...

Flow Analysis in Incremental Mode

By default, BugDetective performs Flow Analysis performs a complete analysis of the scope it is run on. This can take considerable time when running on large code bases.

The most common way of performing BugDetective analysis Flow Analysis analysis is to run nightly tests on a single code base that changes slightly from day to day. BugDetective’s Its incremental analysis mode is designed to reduce the time required to run analysis in this typical scenario. With incremental analysis mode, BugDetective memorizes Flow Analysis memorizes important analysis data during the initial run, then reuses it during the subsequent runs—rerunning analysis only for parts of the code that have been modified or are tightly connected to the modified code.

When using incremental analysis, remember that:

  • The initial run of BugDetective may Flow Analysis may be slightly slower than running without incremental analysis. This is because BugDetective in Flow Analysis in addition to performing a complete analysis of the code base, BugDetective saves Flow Analysis saves data to be reused in subsequent runs.
  • Disk space is required to store the necessary data.

Incremental Mode can be enabled using the controls in the Test Configuration manager’s Static tab. The available controls are detailed in Configuring BugDetective Flow Analysis Analysis Options.

Running

...

Flow Analysis with Swapping of Analysis Data Enabled

Swapping of analysis data mode is enabled by default. 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 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 BugDetectiveFlow Analysis. If this occurs, BugDetective Flow Analysis will remove from memory parts of the analysis data that are not currently necessary and reread it from disk later.

...

Introducing Built-in Flow Analysis Test Configurations
Anchor
Introducing Built-in Flow Analysis Test_Configurations
Introducing Built-in Flow Analysis Test_Configurations

Flow Analysis

...

Fast

The standard Flow Analysis Test Configuration includes all the Flow Analysis rules—except security rules and those that require customization. The fast configuration uses "Shallowest" depth of analysis and runs faster than the standard and aggressive configurations (see descriptions below). The fast configuration finds a moderate amount of problems and prevents violation number explosion. We recommend using this configuration to enforce Flow Analysis rules for any new project. If more rigorous analysis is required, consider switching to Flow Analysis Standard.

...

We recommend the "Flow Analysis Aggressive" Test Configuration if you want C++test to report a violation for any suspect code. This configuration encourages BugDetective Flow Analysis to report a violation any time that it suspects a problem—even in cases in which a false positive may be reported. Applying this configuration will result in more bugs being reported, but it can also increase the number of false alarms.

Understanding

...

Flow Analysis’ Analysis Scope

BugDetective Flow Analysis performs interprocedural analysis that can cross boundaries of functions, classes, namespaces and compilation units within the scope of files selected for analysis. If there is a function call and the called function is defined in a different file, BugDetective Flow Analysis will be able to trace the path inside the function—as long as the file containing it is included into analysis scope. Because of this, the precision of analysis highly depends on the scope.

...

  • Never put independent projects in the same analysis scope. Different projects can have different definitions of a type with the same fully-qualified name or a function with specific signature. If there is a call to a function that is defined multiple times, BugDetective Flow Analysis may be unable to trace the call into the correct function.
  • If you want to analyze compilation unit A and it depends on compilation unit B, then it is typically recommended to include both A and B in the analysis scope so that BugDetective Flow Analysis can take into account any necessary information from unit B when analyzing A. The same pattern applies to dependent projects. Of course, the more files that are included in the analysis, the longer the analysis will take. BugDetective Flow Analysis does allow analysis on just a single file. However, such limited analysis is likely to be less precise.

Anchor
Bugdetective
Bugdetective
Reviewing

...

Flow Analysis Static Analysis Results

This topic covers how to analyze the bugs reported by BugDetectiveFlow Analysis.

Accessing Results

BugDetective Flow Analysis findings are reported alongside static code analysis violations in Quality Tasks view. BugDetective Flow Analysis content and format, however, is significantly different than that used for static code analysis violations. Results are organized into a prioritized task list that can be viewed by rule category or by severity. To view results by severity, open the Quality Tasks pull-down menu and choose Show> Details.

...

In the Quality Tasks view, each BugDetective Flow Analysis violation is represented by a hierarchical flow path that precisely describes the code that leads to the identified problem. Each element in the path is a line of code that is executed during runtime. If a flow path has a call to a function, the element representing that function call is a node whose subnodes represent execution flow within the called function. The final element in the execution path is always the point where the bug manifests itself. The complete path is presented in order to explain why there is a bug at the final point.

...

Info
iconfalse
titleHow do source code changes affect flow paths?

If the source code changes since BugDetective Flow Analysis was run:

  • BugDetective Flow Analysis violations will continue to use the source code of the original flow path. This ensure that a valid violation path is shown.
  • If an element in the flow path is outdated (i.e., the current source code does not match the analyzed code), the element—plus the violation node—will be marked with a special "out of date" icon and tool tip. Moreover, since the related source code is no longer available, the option to double-click an element to see the related code will be disabled.

...

To highlight the source code referenced by a BugDetective Flow Analysis violation message, simply double-click that message. An editor will open with the source code corresponding to the violation selected and the flow path highlighted in light blue. Any line at which an exception is thrown is marked with pink. All the lines which are important for the violation (violation cause, violation point, critical data flow) are highlighted in a dark color.

...

Tip

Tips

  • You can disable or suppress rules that you do not want checked. For details, see Static Tab Settings: Defining How Static Analysis is Performed.
  • To learn about the BugDetective Flow Analysis rules that are included with C++test, choose Parasoft> Help, open the C++test Static Analysis Rules book, then browse the available BugDetective Flow Analysis category rule description files.

...

Full path presentation is also available. When a violation is shown in the detailed form, the expandable task node contains all the elements of the execution path on which BugDetective Flow Analysis thinks the detected bug manifests itself. In other words, BugDetective Flow Analysis will show the exact sequence of lines that are executed to produce this bug. This gives you complete information about the problem found and the assumptions that BugDetective Flow Analysis made. Sometimes this information is absolutely essential for understanding why BugDetective Flow Analysis reports a possible problem.

In many cases, such detailed information may not be necessary to understand the problem reported. In these cases, the compact form is convenient. When a violation is shown in the compact form, the expandable task node shows only the most important elements of the complete execution path on which BugDetective Flow Analysis thinks the detected bug manifests itself. BugDetective Flow Analysis treats the following execution path elements as important and shows in the compact view:

...

  1. Choose Parasoft> Preferences.
  2. Open the Quality Tasks page.
  3. Enable the Show complete paths for BugDetective violations option.

Anchor
Customazing_bug
Customazing_bug
Customizing

...

Flow Analysis Static Analysis

This topic explains how to customize BugDetective Flow Analysis analysis—including what types of bugs detected, rule parameterization, analysis options, and the resources are checked by specific rules.

Specifying Which Bugs to Detect

The types of bugs that BugDetective Flow Analysis detects is determined by which rules you have enabled in the Test Configuration—in much the same way as you configure static code analysis with coding standards. The basic static analysis settings (maximum tasks reported per rule, etc.—see Static Tab Settings: Defining How Static Analysis is Performed for details) apply to both static code analysis and BugDetective Flow Analysis analysis.

For example, if you want to disable or enable BugDetective Flow Analysis rules, you can do so by checking/clearing boxes in the Test Configuration's rule tree (in the Static> Rules Tree tab).

...

Some rules are parameterized, meaning that you can customize their settings to make the analysis process more flexible and tailored to your unique project needs. As a result, BugDetective Flow Analysis can even be used to detect violations bound to usage of very specific APIs.

...

To explain how this parameter works, let’s first look at the process of finding and reporting a violation. At first, BugDetective Flow Analysis finds paths starting from the violation cause (the point where the bad or suspicious data originates) and ending at the violation point (the point where bad or suspicious data is used in a dangerous or suspicious operation). When BugDetective Flow Analysis finds such a path violating a rule, it normally tries to validate the violation by verifying if the path is reachable from the beginning of the top-level function (the top-level function of the hierarchical representation of the violation path which may call other functions) of the violation path.

...

Code Block
typedef struct SomeStruct {
    int hashCode; 
} SomeStruct;
extern SomeStruct* getObject();
void check(int initialized)
{
    SomeStruct* obj; 
    if (initialized) {
	obj = getObject();
    } else {
	obj = 0;
    }
    int hashCode = 0;
    // some other actions
    if (initialized) {
	hashCode = obj->hashCode;
    }
    // some other actions
}

When BugDetective Flow Analysis analyzes this code, the following violation is found at first:

...

The next stage is to validate the violation by checking whether the path of the violation can be reached from the beginning of the function. During this stage, BugDetective Flow Analysis determines that "obj" can be assigned a null value only if "initialized" is false. However,  in this case, the line where dereferencing occurs is not reachable. This is an example of how validating violation paths by verifying their reachability from the beginning of violation's top-level function can help to eliminate false positives. If the example was different and the path was reachable from the beginning of the function, the violation would pass validation—and in the detailed path mode, BugDetective Flow Analysis would show the violation with the completed path, starting from the beginning of the top-level function, to explain how exactly it thinks the path may be reachable.

In some complicated cases, restricted analysis depth (used to keep analysis speed at a reasonable level) may prevent BugDetective Flow Analysis from completing the violation validation procedure. If that happens, the violation remains unvalidated and BugDetective Flow Analysis does not know whether it is reachable from the beginning of the corresponding function. In this case, the Report unvalidated violations parameter determines whether the violation is reported. Enabling this option increases the number of defects discovered by BugDetectiveFlow Analysis, but it can also increase the number of false positives.

...

Anchor
Configuring BugDetective Analysis_Options
Configuring BugDetective Analysis_Options
Anchor
Configuring BugDetective Analysis_Options
Configuring BugDetective Analysis_Options
Additionally, you can control specific BugDetective Flow Analysis options, such as analysis depth, support for multithreading APIs,  and violation reporting verbosity, by modifying the parameters in the Test Configuration’s Static> Flow Analysis Advanced Settings tab.

...

Performance Tab Options
Anchor
Performance Tab Options
Performance Tab Options

  • Incremental analysi osptions s options control the incremental analysis feature, which is described in Running BugDetective Flow Analysis in Incremental Mode. Available options are:
    • Enable incremental analysis: Determines whether incremental analysis is used.
    • Compact incremental caches every [days]: Determines how often compactization of incremental caches is  run. Incremental analysis is optimized for speed; although BugDetective Flow Analysis strives to always keep cache sizes small and remove unnecessary data, source code changes may result in these caches containing some data that will no longer be used. Compactization, which is run regularly as defined by this parameter, removes all outdated data. More precisely, if the time that has elapsed since the previous compactization is greater than the number of days specified for this option, compactization is performed immediately after the incremental run of BugDetectiveFlow Analysis
    • Clear cache: Clears the incremental analysis cache for the current workspace. After the cache is cleaned the first run of incremental analysis on the workspace will require complete analysis of the code base.
  • Depth of analysis options determine the depth of BugDetective Flow Analysis analysis. Deeper analysis means more violations are found. However, the trade off of deeper analysis is slower performance and (slightly) increased memory consumption. Available options are:
    • shallowest (fastest): Finds only the most obvious problems in the source code. It is limited to cases where the cause of the problem is located close to the code where the problem occurs. The execution paths of violations found by this type of analysis normally span several lines of code in a single function. Only rarely will they span more than 3 function calls.
    • shallow (fast): Like the "shallowest" analysis type, finds only the most obvious problems in the source code. However, it produces a greater overall number of findings and allows for examination of somewhat longer execution paths.
    • standard: Finds many complicated problems with execution paths containing tens of elements. Standard analysis goes beyond shallow analysis and also looks for more complicated problems, which can occur because of bad flow in a single function or due to improper interaction between different functions in different parts of the analyzed project. Violations found by this type of analysis often reveal non-trivial bugs in the analyzed source code and often span tens of lines of code.
    • deep (slow): Allows for detection of a greater number of problems of the same complexity and nature as those defined for 'standard' depth. This type of analysis is slower than the standard one.
    • thorough (slower): Finds more complicated problems. This type of analysis will perform a thorough scan of the code base; this requires more time, but will uncover many very complicated problems whose violation paths can span more than a hundred lines of code in different parts of the scanned application. This option is recommended for nightly runs.than a hundred lines of code in different parts of the scanned application. This option is recommended for nightly runs.
  • Strategy for timeout options allow you to configure your timeout strategy ensure to the analysis completes within a reasonable time. Available options are:
    • time: Analysis of the given hotspot is stopped after spending the defined amount of time on it. Note: in some cases, using this option can result in a slightly unstable number of violations being reported.
    • instructions: Analysis of the given hotspot is stopped after executing the defined number of Flow Analysis instructions.Note: to determine the proper number of instructions to be set up for your environment, review information about timeouts in the Setup Problems section of the generated report.
    • off: No timeout. Note: using this option may require significantly more time to finish the analysis.
    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.
  • Anchor
    enable_swapping
    enable_swapping
    Enable swapping of analysis data to disk:
    Enabled by default. BugDetective Flow Analysis writes to disk the data necessary for analysis. Swapping is done in a similar process as incremental analysis, as well as uses the same persistent storage.You can disable this option, which may result in faster analysis, if you are running BugDetective 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). For description of the swapping feature please refer to the Running BugDetective Flow Analysis with Swapping of Analysis Data Enabled section.

...

  • Do not report violations when cause cannot be shown:  Determines whether  BugDetective Flow Analysis reports violations where causes cannot be shown.
    Some BugDetective Flow Analysis rules require that BugDetective Flow Analysis checks all the possible paths leading to a certain point and verifies that a certain condition is met for all those paths. In such cases, a violation is associated with a set of paths (whereas in simple cases, a violation is represented by only one path). All of the paths in such a violation end with the violation point common to all the paths in the violation. However, different paths may start at different points in code. The beginning of each path is a violation cause (a point in code which stipulates a violation of a certain condition later in the code at the violation point). If a multipath violation's different paths have different causes, BugDetective Flow Analysis will show only the violation point (and not the violation causes).

    Violations containing only the violation point may be difficult to understand (compared to regular cases where BugDetective Flow Analysis shows complete paths starting from violation causes and leading to violation points). That’s why we provide an option to hide violations where the cause cannot be shown.

    See the example below for additional details.

  • Do not report violations whose paths pass via inline assembly code: Prevents the reporting of violations whose paths pass via inline assembly code instructions.
  • Level of reporting similar violations (i.e those that share a violation point, or both the point and the cause): Allows you to determine whether BugDetective Flow Analysis reports all the violations that it can find in the analyzed code or if it hides some of them. Available options are:
    • Report all similar violations: Reports all identified BugDetective Flow Analysis violations.
    • Do not show more than one violation with the same cause and violation point: Prevents the reporting of duplicate BugDetective Flow Analysis violations. Duplicate violations are those that share the same violation cause and the same violation point (even though their flow paths may be different).
    • Do not show more than one violation per suspicious point: Restricts reporting to one violation (for a single rule) per suspicious point. When verbosity is set to this level, BugDetective Flow Analysis performance is somewhat faster.

...

This tab allows you to define functions that terminate application execution. C/C++ developers sometimes use functions that terminate application execution in the event of a fatal error from which recovery is impossible. Examples of such functions are abort() and exit() from the standard library. Since BugDetective Flow Analysis analyzes the application’s execution flow, it's important for it to be aware of the terminating functions that break execution flow by immediately stopping the application. Without such knowledge, BugDetective Flow Analysis might make incorrect assumptions about the application flow.

BugDetective Flow Analysis is aware of the terminating functions that are defined in the standard library. However, this is often not sufficient because non-standard libraries define their own terminating functions. If your application uses one of these functions, you should communicate that to BugDetective Flow Analysis by specifying the custom terminating function in this tab. Otherwise, BugDetective Flow Analysis may produce false positives with execution paths passing by terminating functions.

...

The Test Configuration manager’s Static>  BugDetective Flow Analysis Advanced Options> 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.

...

It is common that allocation functions return an error code to indicate allocation failure. When an allocation function returns a pointer to a resource, a NULL pointer normally indicates an allocation failure. When BugDetective 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 functions on paths where allocation actually occurred. In cases where a resource allocator function returns a pointer to a resource, BugDetective Flow Analysis assumes that the resource is successfully allocated if the pointer is not NULL.

...

We want to define methods allocating/deallocating the resource so that BugDetective Flow Analysis can be used to find leaks of this non-standard resource. To achieve this, we need to perform the following steps:

...

Now let's add support for this type of resource in BugDetective Flow Analysis by performing the following actions:

...