#include <stdio.h>
#include <stdlib.h>
#define SIZE_ARRAY 3
typedef unsigned int uint32;
// Defining three functions of which
// only one accesses a global variable
uint32 var;
void Write_to_var(void) {
var+=1;
}
void No_write_read_1(void) {
}
void No_write_read_2(void) {
}
// Defining a 3-element array of function pointers
// that point to the three functions
typedef void (*FuncPtr)(void);
typedef struct {
FuncPtr pFuncPtr;
}FuncPtrStruct;
const FuncPtrStruct FuncPtrArray[SIZE_ARRAY] =
{
{ &Write_to_var },
{ &No_write_read_1 },
{ &No_write_read_2 }
};
void main() {}
// Defining a function that calls one of the three functions
// via the function pointer
void function(int uiSignalType) {
FuncPtrArray[uiSignalType].pFuncPtr();
}
// Entry-point functions in void(void) format for Polyspace analysis
// Analyze this example with
// -D POLYSPACE -entry-points task_10ms,task_20ms,task_50ms
#ifdef POLYSPACE
void task_10ms(void) {
int signalType;
signalType = 0;
function(signalType);
}
void task_20ms(void) {
int signalType;
signalType = 1;
function(signalType);
}
void task_50ms(void) {
int signalType;
signalType = 2;
function(signalType);
}
#endif
In this example, var
correctly appears as an non-shared
variable if you specify the following options:
However, the message on the variable shows that it can be
potentially written or read by one of the tasks task_10ms
,
task_20ms
, or task_50ms
.
var
is not a shared variable at all since it is accessed only
in the task, task_10ms
. However, the analysis reports all three
tasks as accessing the variable because of an approximation on the
line:
FuncPtrArray[uiSignalType].pFuncPtr();
FuncPtrArray
is an array of function pointers. The pointer at index 0 points to the function
Write_to_var
, which updates the variable
var
. The pointers at indices 1 and 2 point to the functions
No_write_read_1
and
No_write_read_2
, which
do not access
var
at all. However, the analysis does not store
information on which array access calls which function. Instead, the analysis makes
the sound approximation that whatever the index, each array access potentially calls
one of the three functions.
As a result of this approximation, the analysis considers that the tasks
task_10ms
, task_20ms
and
task_50ms
can potentially call each of the functions
Write_to_var
, No_write_read_1
and
No_write_read_2
. A different analysis route determines
correctly that the variable is not shared.