Merge "Make android_mallopt weak for native bridge"
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index 542c4a5..d64a6bd 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -42,6 +42,7 @@
 #include <unistd.h>
 
 #include <async_safe/log.h>
+#include <platform/bionic/mte_kernel.h>
 
 #include "private/WriteProtected.h"
 #include "private/bionic_defs.h"
@@ -109,8 +110,18 @@
 #if defined(__aarch64__)
 #define PR_SET_TAGGED_ADDR_CTRL 55
 #define PR_TAGGED_ADDR_ENABLE   (1UL << 0)
+#ifdef ANDROID_EXPERIMENTAL_MTE
+  // First, try enabling MTE in asynchronous mode, with tag 0 excluded. This will fail if the kernel
+  // or hardware doesn't support MTE, and we will fall back to just enabling tagged pointers in
+  // syscall arguments.
+  if (prctl(PR_SET_TAGGED_ADDR_CTRL,
+            PR_TAGGED_ADDR_ENABLE | PR_MTE_TCF_ASYNC | (1 << PR_MTE_EXCL_SHIFT), 0, 0, 0)) {
+    prctl(PR_SET_TAGGED_ADDR_CTRL, PR_TAGGED_ADDR_ENABLE, 0, 0, 0);
+  }
+#else
   prctl(PR_SET_TAGGED_ADDR_CTRL, PR_TAGGED_ADDR_ENABLE, 0, 0, 0);
 #endif
+#endif
 }
 
 void __libc_init_fork_handler() {
diff --git a/libc/kernel/README.md b/libc/kernel/README.md
index 9036b9f..6db08d6 100644
--- a/libc/kernel/README.md
+++ b/libc/kernel/README.md
@@ -53,14 +53,16 @@
 NOT BREAK THE KERNEL <-> USER ABI, FOR EXAMPLE BY CHANGING THE SIZE
 OF A GIVEN TYPE. THIS TASK CANNOT BE EASILY AUTOMATED AT THE MOMENT.
 
-Download the Linux kernel source code:
+Download the Android mainline kernel source code:
 ```
   > mkdir kernel_src
   > cd kernel_src
-  kernel_src> git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
+  kernel_src> git clone https://android.googlesource.com/kernel/common/ -b android-mainline
 ```
 
-Then checkout the stable tag for the new kernel headers to import:
+For now, there are no tags, take the top of tree version. To find the
+version of the linux stable kernel headers the mainline source code is
+tracking, read the uapi/linux/version.h that is generated.
 ```
   kernel_src> cd linux-stable
   kernel_src/linux-stable> git checkout tags/vXXX
@@ -71,11 +73,17 @@
 to determine which directory to use as the destination directory.
 
 After running lunch, run this command to import the headers into the android
-source tree:
+source tree if there is a kernel source tree already checked out:
 ```
   bionic/libc/kernel/tools/generate_uapi_headers.sh --use-kernel-dir kernel_src
 ```
 
+Run this command to automatically download the latest version of the headers
+and import them if there is no checked out kernel source tree:
+```
+  bionic/libc/kernel/tools/generate_uapi_headers.sh --download-kernel
+```
+
 Next, run this command to copy the parsed files to bionic/libc/kernel/uapi:
 ```
   bionic/libc/kernel/tools/update_all.py
diff --git a/libc/platform/bionic/mte_kernel.h b/libc/platform/bionic/mte_kernel.h
index 04f2bb6..804311c 100644
--- a/libc/platform/bionic/mte_kernel.h
+++ b/libc/platform/bionic/mte_kernel.h
@@ -36,6 +36,16 @@
 // This interface should not be considered to be stable.
 
 #ifdef ANDROID_EXPERIMENTAL_MTE
-#define HWCAP2_MTE (1UL << 31)
-#define PROT_MTE 0x10
+
+#define HWCAP2_MTE (1 << 10)
+#define PROT_MTE 0x20
+
+#define PR_MTE_TCF_SHIFT 1
+#define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
+#define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
+#define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
+#define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
+#define PR_MTE_EXCL_SHIFT 3
+#define PR_MTE_EXCL_MASK (0xffffUL << PR_MTE_EXCL_SHIFT)
+
 #endif
diff --git a/tests/signal_test.cpp b/tests/signal_test.cpp
index 6db7751..3bc3135 100644
--- a/tests/signal_test.cpp
+++ b/tests/signal_test.cpp
@@ -176,7 +176,7 @@
 
   pid_t pid = getpid();
   std::thread thread([&pid]() {
-    usleep(5000);
+    sleep(1);
     kill(pid, SIGRTMIN);
   });