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 describes two methods for implementing new CodeSonar checks using the Extension API and insert these checks into a project.
New CodeSonar checks can be implemented using the Extension API. CodeSonar provides two alternatives for users wishing to insert these checks into a project: inserting new code directly into the program file, or writing a special code wrapper in a separate file linked into the CodeSonar project. Each method has both advantages and disadvantages, as shown in the following table.
| [Method I] |
Insert a few lines of code directly into the program.
|
||||
|---|---|---|---|---|---|
| [Method II] |
Write a special code wrapper in a separate file, and link this
file into the CodeSonar project but not into real builds.
|
To illustrate the use of these two mechanisms, suppose you have two procedures foo() and bar(), and that procedure bar() should not be called while foo() is active
If the project is built using these modified versions of foo() and bar(), then CodeSonar will detect and report calls to bar() while foo() is active.
#ifdef __CODESONAR__
#include <stdlib.h>
#include "csonar.h"
#endif
static int foo_is_executing = 0;
int foo(void){
foo_is_executing = 1;
...
return 42;
}
void bar(void){
#ifdef __CODESONAR__
csonar_trigger(foo_is_executing, "==", 1, "bar should never be called while foo is active" );
#endif
}
EDG_FRONTEND_OPTIONS_APPEND += -Icsonar_libmodels_path
The behavior of csonar_trigger is described in the CodeSonar Extension API Functions and Macros section.
It is often desirable to implement a check without modifying existing code. To support this, CodeSonar provides a feature whereby any function p can be intercepted by a function called csonar_replace_p.
The following steps explain how to add a custom check to project MyProj.
/* file check_foo_bar.c */ #include <stdlib.h> #include "csonar.h" static int foo_is_executing = 0; int csonar_replace_foo(void){ int rv; foo_is_executing = 1; rv = foo(); return rv; } void csonar_replace_bar(void){ csonar_trigger(foo_is_executing, "==", 1, "bar should never be called while foo is active" ); bar(); }
CodeSonar will pretend that calls to foo() and bar() are actually calls to csonar_replace_foo() and csonar_replace_bar() because of the way the replacement functions are named.
EDG_FRONTEND_OPTIONS_APPEND += -Icsonar_libmodels_path
| host:port | is the hub location. |
|---|---|
| compiler_command | is the command for invoking your compiler. Example: gcc |
| include_libmodels | is the compiler option setting required to include the
codesonar/libmodels
subdirectory of your CodeSonar installation. Example: -I/path/to/codesonar/libmodels |
| options | are any compilation options that might be required. In
general, these should be similar to those used for the rest
of the project, including any preprocessor-related options
that might be required for compatibility. Example (for gcc and similar compilers): -c |
If you are automatically invoking the CodeSonar analysis as part of your build, it will usually make sense to edit the build to include the generation of CodeSonar representation for check_foo_bar.c. There are two possible approaches:
[...] codesonar analyze ProjectX alexmachine:7340 make
[...] codesonar build ProjectX alexmachine:7340 gcc -I/path/to/codesonar/libmodels -c check_foo_bar.c codesonar analyze ProjectX alexmachine:7340 make
CC = gcc
all: my_program
my_program: main_file.c secondary_file.c
$(CC) -o $@ $?
CC = gcc
CSONAR_INCLUDE = -I/path/to/codesonar/libmodels
all: csonar_check my_program
csonar_check: check_foo_bar.c
$(CC) $(CSONAR_INCLUDE) -c $?
my_program: main_file.c secondary_file.c
$(CC) -o $@ $?
The following examples add the custom checks in file codesonar_custom_check_file.c to project MyProj, using the hub at alex:7340.
To report problems with this documentation, please visit https://support.codesecure.com/.