blob: 1bc9021f603dc3ad8ef72f011731bae5103623d6 [file] [log] [blame]
Jackeaglef4f8d462019-05-03 14:45:18 +02001From b3343e687842ad54ca1621f6dcb1d99e6826d65d Mon Sep 17 00:00:00 2001
Jon West99571ca2019-04-17 20:33:29 -04002From: Ethan Chen <intervigil@gmail.com>
3Date: Tue, 25 Sep 2018 00:11:05 -0700
Jackeaglef4f8d462019-05-03 14:45:18 +02004Subject: [PATCH 1/2] Actually restore pre-P mutex behavior
Jon West99571ca2019-04-17 20:33:29 -04005
6Apps built against versions < P may not actually expect the EBUSY return
7code, and may crash or otherwise misbehave. Check for target SDK
8versions earlier than P when performing the IsMutexDestroyed check so
9any invocation of HandleUsingDestroyedMutex is bypassed and pre-P mutex
10behavior is restored.
11
12See 9e989f12d1186231d97dac6d038db7955acebdf3 for the change that
13introduced this new behavior.
14
15Change-Id: I45f8882c9527c63eed1ef5820a5004b8958d58ea
16---
17 libc/bionic/pthread_mutex.cpp | 19 ++++++++++++-------
18 1 file changed, 12 insertions(+), 7 deletions(-)
19
20diff --git a/libc/bionic/pthread_mutex.cpp b/libc/bionic/pthread_mutex.cpp
21index bc7bd653f..517e52688 100644
22--- a/libc/bionic/pthread_mutex.cpp
23+++ b/libc/bionic/pthread_mutex.cpp
24@@ -782,17 +782,22 @@ static int MutexLockWithTimeout(pthread_mutex_internal_t* mutex, bool use_realti
25
26 } // namespace NonPI
27
28-static inline __always_inline bool IsMutexDestroyed(uint16_t mutex_state) {
29- return mutex_state == 0xffff;
30-}
31-
32 // Inlining this function in pthread_mutex_lock() adds the cost of stack frame instructions on
33 // ARM64. So make it noinline.
34-static int __attribute__((noinline)) HandleUsingDestroyedMutex(pthread_mutex_t* mutex,
35- const char* function_name) {
36+static inline __attribute__((noinline)) bool IsMutexDestroyed(uint16_t mutex_state) {
37+ // Checking for mutex destruction is a P-specific behavior. Bypass the
38+ // check if the SDK version precedes P, so that no change in behavior
39+ // that may cause crashes is introduced.
40 if (bionic_get_application_target_sdk_version() >= __ANDROID_API_P__) {
41- __fortify_fatal("%s called on a destroyed mutex (%p)", function_name, mutex);
42+ return mutex_state == 0xffff;
43+ } else {
44+ return false;
45 }
46+}
47+
48+static int __always_inline HandleUsingDestroyedMutex(pthread_mutex_t* mutex,
49+ const char* function_name) {
50+ __fortify_fatal("%s called on a destroyed mutex (%p)", function_name, mutex);
51 return EBUSY;
52 }
53
54--
Jackeaglef4f8d462019-05-03 14:45:18 +0200552.21.0
Jon West99571ca2019-04-17 20:33:29 -040056