Data race on adjacent bit fields
Multiple threads perform unprotected operations on adjacent bit fields of a shared data structure
Since R2020b
Description
This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).
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 fielderrorFlag2
in a variable of this type:Suppose that the operations are not atomic with respect to each other. In other words, you have not implemented protection mechanisms to ensure that one operation is completed before another operation begins.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.
Risk
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
anderrorFlag2
unmodified is copied into RAM, for purposes of modifyingerrorFlag1
.A mask that modifies only
errorFlag1
is bitwise OR-ed with this copy.The byte containing both
errorFlag1
anderrorFlag2
unmodified is copied into RAM a second time, for purposes of modifyingerrorFlag2
.A mask that modifies only
errorFlag2
is bitwise OR-ed with this second copy.The version with
errorFlag1
modified is copied back. This version haserrorFlag2
unmodified.The version with
errorFlag2
modified is copied back. This version haserrorFlag1
unmodified and overwrites the previous modification.
Fix
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.
Examples
Result Information
Group: Concurrency |
Language: C | C++ |
Default: On |
Command-Line Syntax:
DATA_RACE_BIT_FIELDS |
Impact: High |
Version History
Introduced in R2020b
See Also
Data race
| Disabling all interrupts (-routine-disable-interrupts -routine-enable-interrupts)
| Temporally exclusive tasks (-temporal-exclusions-file)
| Critical section details (-critical-section-begin -critical-section-end)
| Tasks (-entry-points)
| Configure multitasking manually
| Target processor type (-target)
| Find defects (-checkers)
| Data race through standard library function call
Topics
- Analyze Multitasking Programs in Polyspace
- Protections for Shared Variables in Multitasking Code
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)
- Define Atomic Operations in Multitasking Code
- Extend Concurrency Defect Checkers to Unsupported Multithreading Environments