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 |
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.
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.
Plug-ins can:
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() |
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. |
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.
The API provides functions for checking properties of warning classes.
It also provides functionality for retrieving warning classes by ID or name.
Plug-ins can:
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.
|
| C | cs_metricclass_t | cs_metricclass_flags | ...a setup visitor. | Granularity-specific creation functions csonar_metric_create_class_*(). |
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:
| API Language | Explicitly report metric values with |
|---|---|
| C++ | Metric class report() method (details). |
| Python | Metric class report() method (details). |
| C | Granularity-specific csonar_report_metric_*() function (details). |
For incremental analyses, metric retraction is handled during the drop traversal.
Both automatic and manual retraction can take place.
| API language | Explicitly retract metric values with |
|---|---|
| C++ | Metric class retract() method (details) . |
| Python | Metric class retract() method (details). |
| C | Granularity-specific csonar_metric_retract_*() function (details). |
The CodeSonar plug-in API provides additional functionality for metric values and metric classes.
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.
| 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() |
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() |
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.
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.
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() |
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.
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.
By default, the master will retry any failed unit of work (usually the analysis of a single procedure) three times before giving up and moving on to the next unit of work. This often means that bugs can be reproduced instantly and repeatedly without needing to restart the master: the slave will attempt the unit of work that previously failed immediately upon starting. If you want more retries, change the setting of UNIT_OF_WORK_RETRIES. In particular, you may wish to set UNIT_OF_WORK_RETRIES=-1 so that crashes can be reproduced as many times as you want. The plug-in can even be modified in between retries in order to fix the issue while the analysis is live.
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/.