CWE Rule 832
Description
Rule Description
The product attempts to unlock a resource that is not locked.
Polyspace Implementation
The rule checker checks for Missing lock.
Examples
Missing lock
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 a task calls an unlock function before calling the corresponding lock function.
In multitasking code, a lock function begins a critical section
of code and an unlock function ends it. When a task my_task
calls
a lock function my_lock
, other tasks calling my_lock
must
wait till my_task
calls the corresponding unlock
function. Polyspace requires that both lock and unlock functions
must have the form void func(void)
.
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)
.
A call to an unlock function without a corresponding lock function can indicate a coding error. For instance, perhaps the unlock function does not correspond to the lock function that begins the critical section.
The fix depends on the root cause of the defect. For instance, if the defect occurs because of a mismatch between lock and unlock function, check the lock-unlock function pair in your Polyspace analysis configuration and fix the mismatch.
See examples of fixes below. To avoid the issue, you can follow the practice of
calling the lock and unlock functions in the same module at the same level of
abstraction. For instance, in this example, func
calls the lock
and unlock function at the same level but func2
does
not.
void func() { my_lock(); { ... } my_unlock(); } void func2() { { my_lock(); ... } my_unlock(); }
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 locking and unlocking functions that are not supported by Polyspace. Extend this checker by mapping these functions to their known POSIX® equivalent. See Extend Concurrency Defect Checkers to Unsupported Multithreading Environments.
void begin_critical_section(void); void end_critical_section(void); int global_var; void reset(void) { begin_critical_section(); global_var = 0; end_critical_section(); } void my_task(void) { global_var += 1; end_critical_section(); //Noncompliant }
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 | end_critical_section |
On the command-line, you can use the following:
polyspace-bug-finder -entry-points my_task,reset -critical-section-begin begin_critical_section:cs1 -critical-section-end end_critical_section:cs1
The example has two entry points, my_task
and reset
. my_task
calls end_critical_section
before
calling begin_critical_section
.
One possible correction is to call the lock function begin_critical_section
before
the instructions in the critical section.
void begin_critical_section(void); void end_critical_section(void); int global_var; void reset(void) { begin_critical_section(); global_var = 0; end_critical_section(); } void my_task(void) { begin_critical_section(); global_var += 1; end_critical_section(); }
void begin_critical_section(void); void end_critical_section(void); int global_var; void reset() { begin_critical_section(); global_var=0; end_critical_section(); } void my_task(void) { int index=0; volatile int numCycles; while(numCycles) { if(index%10==0) { begin_critical_section(); global_var ++; } end_critical_section(); //Noncompliant index++; } }
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 | end_critical_section |
On the command-line, you can use the following:
polyspace-bug-finder -entry-points my_task,reset -critical-section-begin begin_critical_section:cs1 -critical-section-end end_critical_section:cs1
The example has two entry points, my_task
and reset
.
In the while
loop, my_task
leaves
a critical section through the call end_critical_section();
.
In an iteration of the while
loop:
If
my_task
enters theif
condition branch, the critical section begins through a call tobegin_critical_section
.If
my_task
does not enter theif
condition branch and leaves thewhile
loop, the critical section does not begin. Therefore, a Missing lock defect occurs.If
my_task
does not enter theif
condition branch and continues to the next iteration of thewhile
loop, the unlock functionend_critical_section
is called again. A Double unlock defect occurs.
Because numCycles
is a volatile
variable,
it can take any value. Any
of the cases above are possible. Therefore, a Missing lock defect
and a Double unlock defect appear on the call end_critical_section
.
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 (한국어)