Multiple threads perform unprotected operations on adjacent bit fields of a shared data structure
This defect occurs when:
Multiple tasks perform unprotected operations on bit fields that are part of the same structure.
For instance, a task operates on field errorFlag1 and another
task on field errorFlag2 in a variable of this
type:
struct errorFlags {
unsigned int errorFlag1 : 1;
unsigned int errorFlag2 : 1;
...
}At least one of the unprotected operations is a write operation.
To find this defect, before analysis, you must specify the multitasking options. To specify these options, on the Configuration pane, select Multitasking. For more information, see Configuring Polyspace Multitasking Analysis Manually.
Adjacent bit fields that are part of the same structure might be stored in one byte in the same memory location. Read or write operations on all variables including bit fields occur one byte or word at a time. To modify only specific bits in a byte, steps similar to these steps occur in sequence:
The byte is loaded into RAM.
A mask is created so that only specific bits are modified to the intended value and the remaining bits remain unchanged.
A bitwise OR operation is performed between the copy of the byte in RAM and the mask.
The byte with specific bits modified is copied back from RAM.
When you access two different bit fields, these four steps have to be performed for each
bit field. If the accesses are not protected, all four steps for one bit field might not be
completed before the four steps for the other bit field begin. As a result, the modification
of one bit field might undo the modification of an adjacent bit field. For instance, in the
preceding example, the modification of errorFlag1 and
errorFlag2 can occur in the following sequence. Steps 1,2 and 5 relate
to modification of errorFlag1 and while steps 3,4 and 6 relate to that of
errorFlag2.
The byte with both errorFlag1 and errorFlag2
unmodified is copied into RAM, for purposes of modifying
errorFlag1.
A mask that modifies only errorFlag1 is bitwise OR-ed with this
copy.
The byte containing both errorFlag1 and
errorFlag2 unmodified is copied into RAM a second time, for
purposes of modifying errorFlag2.
A mask that modifies only errorFlag2 is bitwise OR-ed with this
second copy.
The version with errorFlag1 modified is copied back. This version
has errorFlag2 unmodified.
The version with errorFlag2 modified is copied back. This version
has errorFlag1 unmodified and overwrites the previous
modification.
To fix this defect, protect the operations on bit fields that are part of the same structure by using critical sections, temporal exclusion, or another means. See Protections for Shared Variables in Multitasking Code.
To identify existing protections that you can reuse, see the table and graphs associated
with the result. The table shows each pair of conflicting calls. The Access
Protections column shows existing protections on the calls. To see the function
call sequence leading to the conflicts, click the
icon.
| Group: Concurrency |
| Language: C | C++ |
| Default: On |
Command-Line Syntax:
DATA_RACE_BIT_FIELDS |
| Impact: High |
Configure
multitasking manually | Critical section
details (-critical-section-begin -critical-section-end) | Data race | Data race
including atomic operations | Data race through
standard library function call | Disabling all
interrupts (-routine-disable-interrupts -routine-enable-interrupts) | Find defects
(-checkers) | Target processor
type (-target) | Tasks
(-entry-points) | Temporally
exclusive tasks (-temporal-exclusions-file)