You can run a Polyspace® analysis on the custom C code in a C Function (Simulink) block from Simulink®. Polyspace checks the custom C code for errors and bugs while keeping the model specific information such as design range specification, nature and number of inputs that are specified in the Simulink model.
Before you run Polyspace from Simulink, you must link your Polyspace and MATLAB® installations. See Integrate Polyspace with MATLAB and Simulink.
To open the model used in this example, in the MATLAB Command Window, run:
openExample('polyspace_code_prover/CScriptDemoExample') open_system('psdemo_model_link_sl_cscript');
The model contains a C Function block called Command Strategy inside the controller subsystem.

The Command Strategy block implements a look-up table using custom C code and outputs a value result based on two inputs x and y.
Click the Apps tab and select Polyspace Code Verifier to open the Polyspace tab.
Select Bug Finder or Code Prover from the drop-down list located at the leftmost corner of the Polyspace tab.
To run a Polyspace analysis on the custom C code in the C Function block, select Custom Code Used in Model from the drop-down list in the Analyze section.

To start the Polyspace analysis, click the Run Analysis button. The MATLAB Command Window displays the progress of the analysis.
After the analysis, the Polyspace user interface opens with the results. You can choose to not open the results automatically after the analysis by unselecting Open results automatically after verification in Settings. To open the results after the analysis is finished, click the Analysis Results button.
To see all results of the Polyspace analysis, click Clear active filters from the Showing drop-down list in the Results List pane. If you run a Code Prover analysis, the results for the controller subsystem contain two red checks and an orange check.
To organize the results by family, click
and select
Family.

To switch between a Bug Finder and Code Prover analysis, return to the Simulink Editor from the Polyspace user interface. Select Bug Finder or Code Prover from the drop-down list located at the leftmost corner of the Polyspace tab and rerun the analysis.
You can run a Polyspace Code Prover™ analysis on the custom code for this model from MATLAB Editor or the Command Window using this code:
% Load the model 'psdemo_model_link_sl_cscript' load_system('psdemo_model_link_sl_cscript'); % Create a 'pslinkoptions' object mlopts = pslinkoptions('psdemo_model_link_sl_cscript'); % Specify whether to run 'CodeProver' or 'BugFinder' Analysis mlopts.VerificationMode = 'CodeProver'; % Specify custom code as analysis target and run the analysis pslinkrun('-slcc','psdemo_model_link_sl_cscript',mlopts);
To identify issues in the custom C code, use the information in the Result Details pane and the Source pane of the Polyspace user interface. If you do not see these panes, go to Window > Show/Hide View and select the missing pane. For details on the panes, see Result Details and Source.
Polyspace wraps the code in the C Function block in a custom code wrapper. The inputs and outputs of the C Function block are declared as global variables. The custom C code is called as a function.
/* Variables corresponding to inputs ..*/
// global In...
/* Variables corresponding to outputs*/
// global Out...
/* Wrapper functions for code in block */
// void ...(void){
//...
}
The global variables corresponding to inputs start with
In, such as
In1_psdemo_model_link_sl_cscript_98_Command_strategy.
The global variables corresponding to outputs start with
Out, such as
Out1_psdemo_model_link_sl_cscript_98_Command_strategy.
The void-void function contains the custom C
code with the input and output variables replaced by the global
variables. If you have multiple C Function blocks,
then the code in each block is wrapped in separate functions.
The global variables reflect all properties of the input and output of the C Function block, including their data range, data type, and size. If you have multiple inputs, then the order of the global variables is the same as the order of the input defined in the C Function block. This table shows the input and output variables of the block in this example and their corresponding global variables in the Source pane.
| Global Variable Name in Source Pane | Scope | Variable Name in C Function Block |
|---|---|---|
In1_psdemo_model_link_sl_cscript_98_Command_strategy | Input | x |
In2_psdemo_model_link_sl_cscript_98_Command_strategy | Input | y |
Out1_psdemo_model_link_sl_cscript_98_Command_strategy | Output | result |
Identify issues in the custom code by reviewing the wrapped code in the Source pane. Use the tooltip in the Source pane and the information in the Result Details pane to fix the issues. This workflow applies to Code Prover and Bug Finder analyses.
The red check Illegally dereferenced pointer highlights
the dereferencing operation after the for
loop.
tmp = *p + 5;
*p is outside its bounds. To find the root cause of the
check, follow the life cycle of the pointer leading to the illegal dereferencing.
At the start of its life cycle, the pointer *p
points to the first element of array which has
100 elements.
Then p is incremented 100 times, pointing
*p to the nonexistent location
array[100].
The dereferencing operation in tmp = *p+5;
becomes illegal, causing a red check.
The red check Out of Bounds array index highlights the
array indexing operation in the if
condition.
if (another_array[return_val - i + 9] != 0)
another_array is 2 while the index value
return_val-i+9 ranges from 2 to 18. To find the root
cause of the check, track the values of the variables
return_val and i using the tooltip.
When you hover over any instance of the variables in the
Source pane, the tooltip is displayed.
The value of i is 100.
The value of return_val ranges from 93 to 109
because of the prevailing condition: if ((return_val > 92)
&& (return_val < 110)).
The index value (return_val-i+9) evaluates to a
range of 2 to 18.
The index values are out of bounds for the array
another_array, causing a red check.
The orange Overflow check highlights the assignment to
return_val. The Result Details pane
states that the check is related to bounded input values. To find the root cause
of the check, check the data type and corresponding range of the variables by
using the tooltip.
The input values x and y
correspond to these respective global variables
In1_psdemo_model_link_sl_cscript_98_Command_strategy
In2_psdemo_model_link_sl_cscript_98_Command_strategy
The first input x is an unbound unsigned
integer. Because x is unbound, it has the full
range of an unsigned integer, which is from 0 to 65535.
The second input y is a bounded unsigned
integer ranging from 0 to 1023.
x-y is assigned to the unbound signed integer
return_val. Because
return_val is unbound, it has full range from
-32768 to 32767.
The range of x-y is 1023 to 65535, while the
range of return_val is -32768 to 32767.
Some possible values of x-y cannot fit into
return_val, causing the orange check.
For details about interpreting results of a Polyspace Bug Finder™ analysis, see Interpret Bug Finder Results in Polyspace Desktop User Interface.
Modify the custom C code or the model to fix the issues. You can fix a Polyspace check in several ways. The examples here illustrate the general workflow of fixing Polyspace checks.
You can address this check in several ways. Modify the C code so that a nonexistent memory address is not accessed.
Return to the Simulink Editor and double-click on the C Function block to open the custom code.
Use the index operator on array to access a
valid array index. You can access indices from 0 to 99 because
array has 100 elements. Accessing indices
beyond this range results in a run-time error in Simulink.
// access any index between 0 to 99 tmp = array[50] + 5;
p before the dereferencing operation. For
example, *p can point to the
51st element in
array.// After the for loop, point p to a valid memory location p = &(array[50]); // ... tmp = *p + 5;
You can address this check in several ways. Modify the code so that the size
of another_array[] remains larger than or equal to the index
value return_val-i+9.
Return to the Simulink Editor and double-click on the C Function block to open the custom code.
Modify the prevailing condition on return_val
so that the index value return_val-i+9 always
evaluates to 0 or
1.
if ((return_val > 91) && (return_val < 92)) //...
another_array with size
19.int another_array[19];
You can address this check in several ways as well. Modify the C code or the model so that the range of the right side of the assignment operation remains equal to or larger than that of the left side.
Return to the Simulink Editor.
Saturate the input variables x and
y in the model so that their difference can
fit into a 16-bit integer. The workflow for fixing
Overflow by using saturation blocks is
described in Run Polyspace Analysis on Custom Code in C Caller Blocks and Stateflow Charts.
Alternatively, increase the size of return_val
in the custom C code to accommodate x-y.
Return to the Simulink Editor and double-click on the C Function block to open the custom code.
Declare return_val as a 32-bit
integer.
int32_T return_val;
For details about addressing Polyspace results, see Address Polyspace Results Through Bug Fixes or Justifications.