JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++
Binaries
C#
Java

Writing and Debugging CodeSonar Plug-Ins

This section provides general information about creating plug-ins to attach custom functionality to the CodeSonar analysis.

Language Module Support: plug-ins can be applied to C/C++, C#, and Java analyses (including analyses that involve some combination of these). However, C# and Java analyses do not generate internal representation for points, symbols, or ASTs. In consequence, plug-ins that rely on properties and relationships of these elements will generally not produce useful information for those analyses.

CodeSonar SaaS Note: If you want to use your own custom plug-ins with CodeSonar SaaS, contact CodeSecure support for assistance. The installation instructions provided in this page are not sufficient to make plug-ins available to SaaS analyses.



Overview

This page provides general information about writing CodeSonar plug-ins. We also provide sections with language-specific details: C++, Python, C.

In general, writing a CodeSonar plug-in will involve the following steps.

  1. Create a source file for the plug-in.
  2. If your plug-in defines a new warning class or new metric class, create it.
  3. Add one or more visitors to perform the main work of the plug-in. This may include handling warnings, metric values, or both.
  4. If you intend to use the plug-in with parallel-mode analyses, perform any necessary parallelization work.
  5. [C and C++ plug-ins only] Compile the plug-in into a library with a suitable name.
  6. Install the plug-in.
  7. Test and debug the plug-in.

Warnings

Plug-ins can:

New Warning Classes

Plug-ins can define new CodeSonar warning classes.

API language Types New Warning Classes
Warning Class Warning Class Flags Must be defined... Defined with
C++ warningclass warningclass_flags ...in the top-level scope of the plug-in, or in cs_plug_main(). analysis::create_warningclass()
Python warningclass warningclass_flags ...in the top-level scope of the plug-in. analysis.create_warningclass()
C cs_warningclass_t csonar_warningclass_flags ...in a setup visitor. csonar_create_warningclass()
csonar_create_warningclass_ex()

Issuing Warnings

Plug-ins can issue warnings of any class.

Depending on the warning-issuing function used, warnings can be associated with...

Warnings issued by a CodeSonar plug-in will be issued by one of the following functions.

API language Issue warnings with
C++ warningclass::report() or warningclass::report_return_warning() (both overloaded)
Python warningclass.report() or warningclass.report_return_warning() (both overloaded)
C the appropriate csonar_report_*_warning() function.

Retracting Warnings

For incremental analyses, warning retraction is handled during the drop traversal.

The primary mechanism for warning retraction is based on the retraction info parameter specified when the warning was issued. This parameter specifies PDGs (procedures) and compilation units such that if any of the listed PDGs or compilation units change between incremental analyses, the warning instance will be retracted. Each warning-issuing function also provides a default handling option: see function documentation for details.

If specifying retraction information at reporting time is insufficient for some reason, you can also manually retract warnings.

API language Retraction info parameter type (when warning reported) Manually retract with
C++ warning_retraction_info warning::retract()
Python warning_retraction_info warning.retract()
C cs_warning_retraction_info_t csonar_retract_warning()

For more information about how and when your custom checkers should retract warnings, see Custom Checks: Accounting for Incrementality.

Obtaining Information About Warning Classes

The API provides functions for checking properties of warning classes.

It also provides functionality for retrieving warning classes by ID or name.

Metrics

Plug-ins can:

New Metric Classes

Plug-ins can define new metric classes.

API Language Types New metric classes
Metric Class Metric Flag Must be created in... Create with
C++ project_metricclass
compunit_metricclass
sfile_metricclass
procedure_metricclass
metricclass_flags ...the top-level scope of the plug-in, or in cs_plug_main(). The create() method of the metric class manager with the corresponding granularity.
Python project_metricclass
compunit_metricclass
sfile_metricclass
procedure_metricclass
metricclass_flags ...the top-level scope of the plug-in. Definition mechanism depends on whether the metric is to be reported automatically or manually.
  • For automatically-reported metrics, apply a suitable metric decorator to the computation function.
  • For metrics that will be reported by the plug-in, use the create() method of the metric class manager with the corresponding granularity.
C cs_metricclass_t cs_metricclass_flags ...a setup visitor. Granularity-specific creation functions csonar_metric_create_class_*().

Reporting Metric Values

The Plug-In API provides a number of options for reporting metric values. In particular, each metric class defined in the plug-in will have two important properties specified by flag settings:

Retracting Metric Values

For incremental analyses, metric retraction is handled during the drop traversal.

Both automatic and manual retraction can take place.

Obtaining Information About Metric Classes and Values

The CodeSonar plug-in API provides additional functionality for metric values and metric classes.

Visitors

The bulk of plug-in functionality will be applied through one or more visitors, each of which specifies:

Detailed information is provided in section Visitors. The following tables summarize the key points.

API Language Visitors must be added...
C++ ...in the top-level scope of the plug-in, or in cs_plug_main().
Python ...in the top-level scope of the plug-in.
C ...in cs_plug_main().

Visitors other than step visitors are added by defining a visitor function and then instructing CodeSonar to add it as a particular kind of visitor. Step visitors involve a slightly different mechanism.

All visitor types except step visitors

API language Add a visitor with Main work of visitor carried out by
C++ analysis::add_*_visitor()(vobj, langs)
where vobj points to an instance of a concrete subclass VSC of class visitor with member data suitably initialized.
vobj->operator()
Python Applying the appropriate visitor decorator to visitor function vf(). vf()
C csonar_add_*_visitor(langs, vf, ctx)
Where vf points to the visitor function.
vf()

Step Visitors

Note: Step visitors will generally not produce useful information when applied to C# and Java analyses, because internal representation for points is not generated for those analyses.

API language Add a step visitor with... Step Visitor Operations
open copy transition close
C++ analysis::add_step_bottom_up_visitor(ssobj, langs)
where ssobj points to an instance of a concrete subclass SSSC of class step_state with member data suitably initialized.
ssobj->copy() SSSC::copy() SSSC::transition() SSSC destructor
Python analysis.add_step_bottom_up_visitor(ssobj, langs)
where ssobj is an instance of a concrete subclass SSSC of class step_state with member data suitably initialized.
ssobj.copy() SSSC.copy() SSSC.transition() SSSC destructor
C csonar_add_step_bottom_up_visitor( langs, svis, ctx )
where svis points to a cs_step_visitor_dispatch_t
svis->open() svis->copy() svis->transition() svis->close()

Parallelizing and Distributing

A plug-in requires explicit parallelization work if:

In such cases, the plug-in must incorporate inter-process communication mechanisms to ensure that the global state information is correctly shared between multiple slave processes, unless the global state is is only used within a single PDG and its components (and not, for example, to communicate between PDGs or accumulate information across multiple PDGs).

When the analysis is running in parallel mode, the build/analysis traversals (and therefore plug-in processing) proceed as follows.

  1. [Incremental build/analyses only] The analysis master performs the drop traversal and calls all drop visitors.
  2. The analysis master calls all program setup visitors, then all program visitors.
  3. The analysis master performs the serial depth-first traversal, calling serial depth-first phase visitors.
  4. The analysis master calls all program finish visitors, then all program parallel visitors.
  5. The analysis master performs the parallel depth-first traversal, calling parallel depth-first phase visitors.
  6. The analysis master calls all program parallel finish visitors.
  7. The analysis master performs the pointer analysis traversal. No visitors are associated with this traversal.
  8. The analysis master calls all program bottom-up visitors.
  9. [parallel phase] The analysis master manages slave processes, while the slave processes perform the remainder of the bottom up traversal.

    Typically, the unit of work carried out by a slave corresponds to the analysis of a single PDG (procedure). This entails calling all PDG bottom-up visitors, PDG_VERTEX (point) bottom-up visitors, step visitors, and PDG finish visitors on the appropriate elements of the PDG.

  10. The analysis master calls all program bottom-up finish visitors.

The plug-in API provides access to the multiprocess mode for the CodeSonar process in which the plug-in is running:

API language Get multiprocess mode with
C++ analysis::get_multiprocess_mode()
Python analysis::get_multiprocess_mode()
C csonar_get_multiprocess_mode()

Distributed Analysis Note

A remote-requesting analysis master M will only accept an analysis slave S if every CodeSonar plug-in in the set of plug-ins for M is also in the set of plug-ins for S. (Note that it is acceptable for S to have additional plug-ins beyond this set.) For full details, see Distributed Analysis: Plug-In Note.

Testing and Debugging Plug-Ins

When your plug-in is ready for live testing/debugging, install it as directed in the appropriate language-specific section (C++, Python, C) and analyze a sample project. Note that the plug-in mechanism provides close interaction with the CodeSonar analysis, so bugs in plug-ins can cause the entire analysis to crash. Similarly, terminating plug-in execution will terminate the entire analysis. The following hints may be useful.

More on Plug-Ins

General Information:

Visitors Plug-ins are based on visitors, which specify actions to be carried out on elements of the CodeSonar internal representation (IR) at various stages of the analysis.
Writing Plug-Ins
(this page)
General information about creating plug-ins to attach custom functionality to the CodeSonar analysis.
Custom Checks: Accounting for Incrementality Ensuring that custom checks implemented in plug-ins generate appropriate results in incremental analyses.
Plug-In Tutorial Two annotated example plug-ins (each provided in all API languages), with building and installation instructions.
AST API Tutorial The AST API tutorial (provided in all API languages) also uses plug-ins.

Specific API Language:

  Plug-In Guidelines Key API References
C++ Writing C++ Plug-Ins classes analysis, visitor, warningclass, project_metricclass, compunit_metricclass, sfile_metricclass, procedure_metricclass.
Python Writing Python Plug-Ins Visitor decorators, Metric decorators; classes analysis, warningclass, project_metricclass, compunit_metricclass, sfile_metricclass, procedure_metricclass.
C Writing C Plug-Ins CodeSonar Plug-In API: C Functions and Types for Visitors, Warnings, and Metrics

 

 

To report problems with this documentation, please visit https://support.codesecure.com/.