GSI updates for bionic

Change-Id: Ic39dcb8921a089286d840eb18043ced7e8b2d490
diff --git a/patches/bionic/0001-Implement-per-process-target-SDK-version-override.patch b/patches/bionic/0001-Implement-per-process-target-SDK-version-override.patch
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/patches/bionic/0001-Implement-per-process-target-SDK-version-override.patch
diff --git a/patches/bionic/0002-Actually-restore-pre-P-mutex-behavior.patch b/patches/bionic/0002-Actually-restore-pre-P-mutex-behavior.patch
new file mode 100644
index 0000000..a85063f
--- /dev/null
+++ b/patches/bionic/0002-Actually-restore-pre-P-mutex-behavior.patch
@@ -0,0 +1,56 @@
+From 1117121c17d82e5d65a4c59a8bff60bd79c6d223 Mon Sep 17 00:00:00 2001
+From: Ethan Chen <intervigil@gmail.com>
+Date: Tue, 25 Sep 2018 00:11:05 -0700
+Subject: [PATCH 2/4] Actually restore pre-P mutex behavior
+
+Apps built against versions < P may not actually expect the EBUSY return
+code, and may crash or otherwise misbehave. Check for target SDK
+versions earlier than P when performing the IsMutexDestroyed check so
+any invocation of HandleUsingDestroyedMutex is bypassed and pre-P mutex
+behavior is restored.
+
+See 9e989f12d1186231d97dac6d038db7955acebdf3 for the change that
+introduced this new behavior.
+
+Change-Id: I45f8882c9527c63eed1ef5820a5004b8958d58ea
+---
+ libc/bionic/pthread_mutex.cpp | 19 ++++++++++++-------
+ 1 file changed, 12 insertions(+), 7 deletions(-)
+
+diff --git a/libc/bionic/pthread_mutex.cpp b/libc/bionic/pthread_mutex.cpp
+index bc7bd653f..517e52688 100644
+--- a/libc/bionic/pthread_mutex.cpp
++++ b/libc/bionic/pthread_mutex.cpp
+@@ -782,17 +782,22 @@ static int MutexLockWithTimeout(pthread_mutex_internal_t* mutex, bool use_realti
+ 
+ }  // namespace NonPI
+ 
+-static inline __always_inline bool IsMutexDestroyed(uint16_t mutex_state) {
+-    return mutex_state == 0xffff;
+-}
+-
+ // Inlining this function in pthread_mutex_lock() adds the cost of stack frame instructions on
+ // ARM64. So make it noinline.
+-static int __attribute__((noinline)) HandleUsingDestroyedMutex(pthread_mutex_t* mutex,
+-                                                               const char* function_name) {
++static inline __attribute__((noinline)) bool IsMutexDestroyed(uint16_t mutex_state) {
++    // Checking for mutex destruction is a P-specific behavior. Bypass the
++    // check if the SDK version precedes P, so that no change in behavior
++    // that may cause crashes is introduced.
+     if (bionic_get_application_target_sdk_version() >= __ANDROID_API_P__) {
+-        __fortify_fatal("%s called on a destroyed mutex (%p)", function_name, mutex);
++        return mutex_state == 0xffff;
++    } else {
++        return false;
+     }
++}
++
++static int __always_inline HandleUsingDestroyedMutex(pthread_mutex_t* mutex,
++                                                               const char* function_name) {
++    __fortify_fatal("%s called on a destroyed mutex (%p)", function_name, mutex);
+     return EBUSY;
+ }
+ 
+-- 
+2.17.1
+
diff --git a/patches/bionic/0003-bionic-Use-legacy-pthread_mutex_init-behavior-on-pre.patch b/patches/bionic/0003-bionic-Use-legacy-pthread_mutex_init-behavior-on-pre.patch
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/patches/bionic/0003-bionic-Use-legacy-pthread_mutex_init-behavior-on-pre.patch
diff --git a/patches/bionic/0004-Read-SDK-version-override-from-property.patch b/patches/bionic/0004-Read-SDK-version-override-from-property.patch
new file mode 100644
index 0000000..6d9ab29
--- /dev/null
+++ b/patches/bionic/0004-Read-SDK-version-override-from-property.patch
@@ -0,0 +1,37 @@
+From bc49de4e6f2bcf1deed8fb6dcd8b65d5e0a30570 Mon Sep 17 00:00:00 2001
+From: Pierre-Hugues Husson <phh@phh.me>
+Date: Thu, 3 Jan 2019 17:50:03 +0100
+Subject: [PATCH 4/4] Read SDK version override from property
+
+Change-Id: I88ca5d0bde15ee4f2b2bd1255e98f9592973dbf9
+---
+ linker/linker.cpp | 6 ++++--
+ 1 file changed, 4 insertions(+), 2 deletions(-)
+
+diff --git a/linker/linker.cpp b/linker/linker.cpp
+index 951351c32..0283abb19 100644
+--- a/linker/linker.cpp
++++ b/linker/linker.cpp
+@@ -3923,7 +3923,9 @@ std::vector<android_namespace_t*> init_default_namespaces(const char* executable
+   }
+ 
+   uint32_t target_sdk = config->target_sdk_version();
+-#ifdef SDK_VERSION_OVERRIDES
++
++  std::string sdkVersionOverrides = android::base::GetProperty("persist.sys.phh.sdk_override", "");
++  static const char *SDK_VERSION_OVERRIDES = sdkVersionOverrides.c_str();
+   for (const auto& entry : android::base::Split(SDK_VERSION_OVERRIDES, " ")) {
+     auto splitted = android::base::Split(entry, "=");
+     if (splitted.size() == 2 && splitted[0] == executable_path) {
+@@ -3932,7 +3934,7 @@ std::vector<android_namespace_t*> init_default_namespaces(const char* executable
+     }
+   }
+   DEBUG("Target SDK for %s = %d", executable_path, target_sdk);
+-#endif
++
+   set_application_target_sdk_version(target_sdk);
+ 
+   std::vector<android_namespace_t*> created_namespaces;
+-- 
+2.17.1
+