mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-17 18:16:42 +00:00
[clang][analyzer][NFC] Add test for a limitation of alpha.unix.BlockInCriticalSection checker (#93799)
Updated the documentation in `checkers.rst` to include an example of how `trylock` function is handled. Added a new test for a scenario where `pthread_mutex_trylock` is used, demonstrating the current limitation.
This commit is contained in:
parent
196dca7561
commit
46b3145b7c
@ -3157,6 +3157,22 @@ Critical section handling functions modelled by this checker: ``lock, unlock, pt
|
||||
// still inside of the critical section of the std::lock_guard
|
||||
}
|
||||
|
||||
**Limitations**
|
||||
|
||||
* The ``trylock`` and ``timedlock`` versions of acquiring locks are currently assumed to always succeed.
|
||||
This can lead to false positives.
|
||||
|
||||
.. code-block:: c
|
||||
|
||||
void trylock_example(pthread_mutex_t *m) {
|
||||
if (pthread_mutex_trylock(m) == 0) { // assume trylock always succeeds
|
||||
sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
|
||||
pthread_mutex_unlock(m);
|
||||
} else {
|
||||
sleep(10); // false positive: Incorrect warning about blocking function inside critical section.
|
||||
}
|
||||
}
|
||||
|
||||
.. _alpha-unix-Chroot:
|
||||
|
||||
alpha.unix.Chroot (C)
|
||||
|
@ -36,15 +36,15 @@ ssize_t read(int fd, void *buf, size_t count);
|
||||
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
|
||||
|
||||
struct pthread_mutex_t;
|
||||
void pthread_mutex_lock(pthread_mutex_t *mutex);
|
||||
void pthread_mutex_trylock(pthread_mutex_t *mutex);
|
||||
void pthread_mutex_unlock(pthread_mutex_t *mutex);
|
||||
int pthread_mutex_lock(pthread_mutex_t *mutex);
|
||||
int pthread_mutex_trylock(pthread_mutex_t *mutex);
|
||||
int pthread_mutex_unlock(pthread_mutex_t *mutex);
|
||||
|
||||
struct mtx_t;
|
||||
void mtx_lock(mtx_t *mutex);
|
||||
void mtx_timedlock(mtx_t *mutex);
|
||||
void mtx_trylock(mtx_t *mutex);
|
||||
void mtx_unlock(mtx_t *mutex);
|
||||
int mtx_lock(mtx_t *mutex);
|
||||
int mtx_timedlock(mtx_t *mutex);
|
||||
int mtx_trylock(mtx_t *mutex);
|
||||
int mtx_unlock(mtx_t *mutex);
|
||||
|
||||
// global params for dummy function calls
|
||||
FILE *stream;
|
||||
@ -292,3 +292,20 @@ void testBlockInCriticalSectionUniqueLockNested() {
|
||||
testBlockInCriticalSectionUniqueLock(); // expected-note {{Calling 'testBlockInCriticalSectionUniqueLock'}}
|
||||
sleep(1); // no-warning
|
||||
}
|
||||
|
||||
void testTrylockCurrentlyFalsePositive(pthread_mutex_t *m) {
|
||||
// expected-note@+4 {{Assuming the condition is true}}
|
||||
// expected-note@+3 {{Taking true branch}}
|
||||
// expected-note@+2 {{Assuming the condition is false}}
|
||||
// expected-note@+1 {{Taking false branch}}
|
||||
if (pthread_mutex_trylock(m) == 0) { // expected-note 2 {{Entering critical section here}}
|
||||
// FIXME: we are entering the critical section only in the true branch
|
||||
sleep(10); // expected-warning {{Call to blocking function 'sleep' inside of critical section}}
|
||||
// expected-note@-1 {{Call to blocking function 'sleep' inside of critical section}}
|
||||
pthread_mutex_unlock(m);
|
||||
} else {
|
||||
sleep(10); // expected-warning {{Call to blocking function 'sleep' inside of critical section}}
|
||||
// expected-note@-1 {{Call to blocking function 'sleep' inside of critical section}}
|
||||
// FIXME: this is a false positive, the lock was not acquired
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user