GeneralPurpose-BlockingCallsInCriticalSection¶
There shall be no calls to blocking functions in a critical region
Required inputs: IR
Bad code (blocking call in critical section):
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *Process(void *) {
pthread_mutex_lock(&lock);
char buffer[100];
fgets(buffer, sizeof(buffer), stdin); // ERROR: blocking I/O while locked
pthread_mutex_unlock(&lock);
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, Process, NULL);
}
Good code (release lock before blocking):
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *Process(void *) {
char buffer[100];
fgets(buffer, sizeof(buffer), stdin); // OK: blocking I/O outside lock
pthread_mutex_lock(&lock);
// Process buffer with lock held
pthread_mutex_unlock(&lock);
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, Process, NULL);
}
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
blocking_call_in_critical_section |
Call to possible blocking function ‘{}’ in critical section. |
None |
False |
Options¶
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
blocking_function_calls¶
blocking_function_calls : set[bauhaus.analysis.config.QualifiedName] = {'fgets', 'fopen', 'getc', 'read', 'recv', 'sleep'}
enter_critical_functions¶
enter_critical_functions
Set of function names to enter a critical region.Type: set[bauhaus.analysis.config.QualifiedName]
Default:
{'EnterCriticalSection', 'mtx_lock', 'pthread_mutex_lock', 'std::_Mutex_base::lock', 'std::mutex::lock'}
enter_critical_macros¶
enter_critical_macros : set[bauhaus.analysis.config.MacroName] = set()
exit_critical_functions¶
exit_critical_functions
Set of function names to exit a critical region.Type: set[bauhaus.analysis.config.QualifiedName]
Default:
{'ExitCriticalSection', 'mtx_unlock', 'pthread_mutex_unlock', 'std::_Mutex_base::unlock', 'std::mutex::unlock'}
exit_critical_macros¶
exit_critical_macros : set[bauhaus.analysis.config.MacroName] = set()
nested_critical_regions¶
nested_critical_regions : bool = True