CWE Rule 833
Description
Rule Description
The product contains multiple threads or executable segments that are waiting for each other to release a necessary lock, resulting in deadlock.
Polyspace Implementation
The rule checker checks for Deadlock.
Examples
Deadlock
This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).
This issue occurs when multiple tasks are stuck in their critical sections (CS) because:
Each CS waits for another CS to end.
The critical sections (CS) form a closed cycle. For example:
CS #1 waits for CS #2 to end, and CS #2 waits for CS #1 to end.
CS #1 waits for CS #2 to end, CS #2 waits for CS #3 to end and CS #3 waits for CS #1 to end.
Polyspace expects critical sections of code to follow a specific format. A critical section lies between a call to a lock function and a call to an unlock function. When a task my_task
calls a lock function my_lock
, other tasks calling my_lock
must wait until my_task
calls the corresponding unlock function.
To find this defect, specify your lock and unlock functions using one of these methods:
Invoke one of the concurrency primitives that Polyspace Bug Finder™ can detect automatically. See Auto-Detection of Thread Creation and Critical Section in Polyspace.
Specify lock and unlock functions explicitly before analysis as configuration options. Polyspace requires that both lock and unlock functions must have the form
void func(void)
. SeeCritical section details (-critical-section-begin -critical-section-end)
.
Each task waits for a critical section in another task to end and is unable to proceed. The program can freeze indefinitely.
The fix depends on the root cause of the defect. You can try to break the cyclic order between the tasks in one of these ways:
Write down all critical sections involved in the deadlock in a certain sequence. Whenever you call the lock functions of the critical sections within a task, respect the order in that sequence. See an example below.
If one of the critical sections involved in a deadlock occurs in an interrupt, try to disable all interrupts during critical sections in all tasks. See
Disabling all interrupts (-routine-disable-interrupts -routine-enable-interrupts)
.
Reviewing this defect is an opportunity to check if all operations in your critical section are really meant to be executed as an atomic block. It is a good practice to keep critical sections at a bare minimum.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
You might be using multithreading functions that are not supported by Polyspace. Extend this checker by mapping the functions of your multithreading functions to their known POSIX® equivalent. See Extend Concurrency Defect Checkers to Unsupported Multithreading Environments.
void task1(void); void task2(void); int var; void perform_task_cycle(void) { var++; } void begin_critical_section_1(void); void end_critical_section_1(void); void begin_critical_section_2(void); void end_critical_section_2(void); void task1() { while(1) { begin_critical_section_1(); begin_critical_section_2(); //Noncompliant perform_task_cycle(); end_critical_section_2(); end_critical_section_1(); } } void task2() { while(1) { begin_critical_section_2(); begin_critical_section_1(); perform_task_cycle(); end_critical_section_1(); end_critical_section_2(); } }
In this example, to emulate multitasking behavior, you must specify the following options:
Option | Specification | |
---|---|---|
Configure multitasking manually | ||
Tasks (-entry-points) |
| |
Critical section details (-critical-section-begin -critical-section-end) | Starting routine | Ending routine |
begin_critical_section_1 | end_critical_section_1 | |
begin_critical_section_2 | end_critical_section_2 |
A Deadlock occurs because the instructions can execute in the following sequence:
task1
callsbegin_critical_section_1
.task2
callsbegin_critical_section_2
.task1
reaches the instructionbegin_critical_section_2();
. Sincetask2
has already calledbegin_critical_section_2
,task1
waits fortask2
to callend_critical_section_2
.task2
reaches the instructionbegin_critical_section_1();
. Sincetask1
has already calledbegin_critical_section_1
,task2
waits fortask1
to callend_critical_section_1
.
One possible correction is to follow the same sequence of calls
to lock and unlock functions in both task1
and task2
.
void task1(void); void task2(void); void perform_task_cycle(void); void begin_critical_section_1(void); void end_critical_section_1(void); void begin_critical_section_2(void); void end_critical_section_2(void); void task1() { while(1) { begin_critical_section_1(); begin_critical_section_2(); perform_task_cycle(); end_critical_section_2(); end_critical_section_1(); } } void task2() { while(1) { begin_critical_section_1(); begin_critical_section_2(); perform_task_cycle(); end_critical_section_2(); end_critical_section_1(); } }
int var; void performTaskCycle() { var++; } void lock1(void); void lock2(void); void lock3(void); void unlock1(void); void unlock2(void); void unlock3(void); void task1() { while(1) { lock1(); lock2(); //Noncompliant performTaskCycle(); unlock2(); unlock1(); } } void task2() { while(1) { lock2(); lock3(); performTaskCycle(); unlock3(); unlock2(); } } void task3() { while(1) { lock3(); lock1(); performTaskCycle(); unlock1(); unlock3(); } }
In this example, to emulate multitasking behavior, you must specify the following options:
Option | Specification | |
---|---|---|
Configure multitasking manually | ||
Entry points |
| |
Critical section details | Starting routine | Ending routine |
lock1 | unlock1 | |
lock2 | unlock2 | |
lock3 | unlock3 |
A Deadlock occurs because the instructions can execute in the following sequence:
task1
callslock1
.task2
callslock2
.task3
callslock3
.task1
reaches the instructionlock2();
. Sincetask2
has already calledlock2
,task1
waits for call tounlock2
.task2
reaches the instructionlock3();
. Sincetask3
has already calledlock3
,task2
waits for call tounlock3
.task3
reaches the instructionlock1();
. Sincetask1
has already calledlock1
,task3
waits for call tounlock1
.
To break the cyclic order between critical sections, note every lock function in your code in a certain sequence, for example:
lock1
lock2
lock3
If you use more than one lock function in a task, use
them in the order in which they appear in the sequence. For example,
you can use lock1
followed by lock2
but
not lock2
followed by lock1
.
int var; void performTaskCycle() { var++; } void lock1(void); void lock2(void); void lock3(void); void unlock1(void); void unlock2(void); void unlock3(void); void task1() { while(1) { lock1(); lock2(); performTaskCycle(); unlock2(); unlock1(); } } void task2() { while(1) { lock2(); lock3(); performTaskCycle(); unlock3(); unlock2(); } } void task3() { while(1) { lock1(); lock3(); performTaskCycle(); unlock3(); unlock1(); } }
Check Information
Category: Resource Locking Problems |
Version History
Introduced in R2024a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)