Potentially unprotected variable

Global variables shared between multiple tasks but not protected from concurrent access by the tasks

Description

A shared unprotected global variable has the following properties:

  • The variable is used in more than one task.

  • Polyspace® determines that at least one operation on the variable is not protected from interruption by operations in other tasks.

In code that is not intended for multitasking, all global variables are non-shared.

In your verification results, these variables are colored orange on the Source, Results List and Variable Access panes. On the Source pane, the coloring is applied to the variable only during declaration.

Examples

expand all

#include <limits.h>
int shared_var;

void inc() {
    shared_var+=2;
}

void reset() {
    shared_var = 0;
}

void task() {
    volatile int randomValue = 0;
    while(randomValue) {
        reset();
        inc();
        inc();
    }
}

void interrupt() {
    shared_var = INT_MAX;
}

void interrupt_handler() {
    volatile int randomValue = 0;
    while(randomValue) {
        interrupt();
    }
}

void main() {
}

In this example, shared_var is an unprotected shared variable if you specify the following multitasking options:

OptionValue
Configure multitasking manually
Tasks

task

interrupt_handler

You do not specify protection mechanisms such as critical sections.

The operation shared_var = INT_MAX can interrupt the other operations on shared_var and cause unpredictable behavior.

If you click the (graph) icon on the Result Details pane, you see the two concurrent tasks (threads).

The first graph shows how the tasks access the variable. For instance, the task interrupt_handler calls a function interrupt that writes to the shared variable shared_var.

The second graph shows how the tasks are created. In this example, both tasks are created after main completes. In other cases, tasks might be created within functions called from main.

Check Information

Language: C | C++