@@ -1235,6 +1235,50 @@ Check calls to various UNIX/Posix functions: ``open, pthread_once, calloc, mallo
12351235.. literalinclude :: checkers/unix_api_example.c
12361236 :language: c
12371237
1238+ .. _unix-BlockInCriticalSection :
1239+
1240+ unix.BlockInCriticalSection (C, C++)
1241+ """"""""""""""""""""""""""""""""""""
1242+ Check for calls to blocking functions inside a critical section.
1243+ Blocking functions detected by this checker: ``sleep, getc, fgets, read, recv ``.
1244+ Critical section handling functions modeled by this checker:
1245+ ``lock, unlock, pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock, mtx_lock, mtx_timedlock, mtx_trylock, mtx_unlock, lock_guard, unique_lock ``.
1246+
1247+ .. code-block :: c
1248+
1249+ void pthread_lock_example(pthread_mutex_t *m) {
1250+ pthread_mutex_lock(m); // note: entering critical section here
1251+ sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
1252+ pthread_mutex_unlock(m);
1253+ }
1254+
1255+ .. code-block :: cpp
1256+
1257+ void overlapping_critical_sections(mtx_t *m1, std::mutex &m2) {
1258+ std::lock_guard lg{m2}; // note: entering critical section here
1259+ mtx_lock(m1); // note: entering critical section here
1260+ sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
1261+ mtx_unlock(m1);
1262+ sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
1263+ // still inside of the critical section of the std::lock_guard
1264+ }
1265+
1266+ **Limitations **
1267+
1268+ * The ``trylock `` and ``timedlock `` versions of acquiring locks are currently assumed to always succeed.
1269+ This can lead to false positives.
1270+
1271+ .. code-block :: c
1272+
1273+ void trylock_example(pthread_mutex_t *m) {
1274+ if (pthread_mutex_trylock(m) == 0) { // assume trylock always succeeds
1275+ sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
1276+ pthread_mutex_unlock(m);
1277+ } else {
1278+ sleep(10); // false positive: Incorrect warning about blocking function inside critical section.
1279+ }
1280+ }
1281+
12381282 .. _unix-Errno :
12391283
12401284unix.Errno (C)
@@ -3130,49 +3174,6 @@ For a more detailed description of configuration options, please see the
31303174alpha.unix
31313175^^^^^^^^^^
31323176
3133- .. _alpha-unix-BlockInCriticalSection :
3134-
3135- alpha.unix .BlockInCriticalSection (C)
3136- """""""""""""""""""""""""""""""""""""
3137- Check for calls to blocking functions inside a critical section.
3138- Blocking functions detected by this checker: ``sleep, getc, fgets, read, recv ``.
3139- Critical section handling functions modelled by this checker: ``lock, unlock, pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock, mtx_lock, mtx_timedlock, mtx_trylock, mtx_unlock, lock_guard, unique_lock ``.
3140-
3141- .. code-block :: c
3142-
3143- void pthread_lock_example(pthread_mutex_t *m) {
3144- pthread_mutex_lock(m); // note: entering critical section here
3145- sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
3146- pthread_mutex_unlock(m);
3147- }
3148-
3149- .. code-block :: cpp
3150-
3151- void overlapping_critical_sections(mtx_t *m1, std::mutex &m2) {
3152- std::lock_guard lg{m2}; // note: entering critical section here
3153- mtx_lock(m1); // note: entering critical section here
3154- sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
3155- mtx_unlock(m1);
3156- sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
3157- // still inside of the critical section of the std::lock_guard
3158- }
3159-
3160- **Limitations **
3161-
3162- * The ``trylock `` and ``timedlock `` versions of acquiring locks are currently assumed to always succeed.
3163- This can lead to false positives.
3164-
3165- .. code-block :: c
3166-
3167- void trylock_example(pthread_mutex_t *m) {
3168- if (pthread_mutex_trylock(m) == 0) { // assume trylock always succeeds
3169- sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
3170- pthread_mutex_unlock(m);
3171- } else {
3172- sleep(10); // false positive: Incorrect warning about blocking function inside critical section.
3173- }
3174- }
3175-
31763177.. _alpha-unix-Chroot :
31773178
31783179alpha.unix .Chroot (C)
0 commit comments