Merge changes Ie92399c9,Ic6f05388

* changes:
  Change default block size alignment to be 4 for memory saving on 32-bit arch
  Keep allocation of `tail_` outside of LinkedList
diff --git a/libc/arch-arm/bionic/kuser_helper_on.S b/libc/arch-arm/bionic/kuser_helper_on.S
index 2a1d86d..08291f7 100644
--- a/libc/arch-arm/bionic/kuser_helper_on.S
+++ b/libc/arch-arm/bionic/kuser_helper_on.S
@@ -32,10 +32,10 @@
   .balign 4
   .type kuser_helper_on, %object
 kuser_helper_on:
-  .long 2f-1f         // int32_t namesz
-  .long 3f-2f         // int32_t descsz
-  .long NT_TYPE_KUSER // int32_t type
-1:.ascii "Android\0"  // char name[]
-2:.long 1             // int32_t on
+  .long 2f-1f                 // int32_t namesz
+  .long 3f-2f                 // int32_t descsz
+  .long NT_ANDROID_TYPE_KUSER // int32_t type
+1:.ascii "Android\0"          // char name[]
+2:.long 1                     // int32_t on
 3:
   .size kuser_helper_on, .-kuser_helper_on
diff --git a/libc/arch-arm64/bionic/note_memtag_heap_async.S b/libc/arch-arm64/bionic/note_memtag_heap_async.S
index 208115c..931f40b 100644
--- a/libc/arch-arm64/bionic/note_memtag_heap_async.S
+++ b/libc/arch-arm64/bionic/note_memtag_heap_async.S
@@ -33,11 +33,11 @@
 
   .section ".note.android.memtag", "a", %note
   .p2align 2
-  .long 1f - 0f         // int32_t namesz
-  .long 3f - 2f         // int32_t descsz
-  .long NT_TYPE_MEMTAG  // int32_t type
+  .long 1f - 0f                 // int32_t namesz
+  .long 3f - 2f                 // int32_t descsz
+  .long NT_ANDROID_TYPE_MEMTAG  // int32_t type
 0:
-  .asciz "Android"      // char name[]
+  .asciz "Android"              // char name[]
 1:
   .p2align 2
 2:
diff --git a/libc/arch-arm64/bionic/note_memtag_heap_sync.S b/libc/arch-arm64/bionic/note_memtag_heap_sync.S
index d71ad02..77ab03a 100644
--- a/libc/arch-arm64/bionic/note_memtag_heap_sync.S
+++ b/libc/arch-arm64/bionic/note_memtag_heap_sync.S
@@ -33,11 +33,11 @@
 
   .section ".note.android.memtag", "a", %note
   .p2align 2
-  .long 1f - 0f         // int32_t namesz
-  .long 3f - 2f         // int32_t descsz
-  .long NT_TYPE_MEMTAG  // int32_t type
+  .long 1f - 0f                 // int32_t namesz
+  .long 3f - 2f                 // int32_t descsz
+  .long NT_ANDROID_TYPE_MEMTAG  // int32_t type
 0:
-  .asciz "Android"      // char name[]
+  .asciz "Android"              // char name[]
 1:
   .p2align 2
 2:
diff --git a/libc/arch-common/bionic/crtbrand.S b/libc/arch-common/bionic/crtbrand.S
index 307ef2e..b7540e9 100644
--- a/libc/arch-common/bionic/crtbrand.S
+++ b/libc/arch-common/bionic/crtbrand.S
@@ -40,7 +40,7 @@
 abitag:
   .long 2f-1f                 // int32_t namesz
   .long 3f-2f                 // int32_t descsz
-  .long NT_TYPE_IDENT         // int32_t type
+  .long NT_ANDROID_TYPE_IDENT // int32_t type
 #ifdef __ANDROID__
 1:.ascii "Android\0"          // char name[]
 2:.long PLATFORM_SDK_VERSION  // int32_t android_api
diff --git a/libc/bionic/bionic_systrace.cpp b/libc/bionic/bionic_systrace.cpp
index cf5cd82..227cb84 100644
--- a/libc/bionic/bionic_systrace.cpp
+++ b/libc/bionic/bionic_systrace.cpp
@@ -20,8 +20,10 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "bionic/pthread_internal.h"
 #include "private/bionic_lock.h"
 #include "private/bionic_systrace.h"
+#include "private/bionic_tls.h"
 #include "private/CachedProperty.h"
 
 #include <async_safe/log.h>
@@ -55,7 +57,7 @@
   return g_trace_marker_fd;
 }
 
-void bionic_trace_begin(const char* message) {
+static void trace_begin_internal(const char* message) {
   if (!should_trace()) {
     return;
   }
@@ -76,7 +78,22 @@
   TEMP_FAILURE_RETRY(write(trace_marker_fd, buf, len));
 }
 
-void bionic_trace_end() {
+void bionic_trace_begin(const char* message) {
+  // Some functions called by trace_begin_internal() can call
+  // bionic_trace_begin(). Prevent infinite recursion and non-recursive mutex
+  // deadlock by using a flag in the thread local storage.
+  bionic_tls& tls = __get_bionic_tls();
+  if (tls.bionic_systrace_disabled) {
+    return;
+  }
+  tls.bionic_systrace_disabled = true;
+
+  trace_begin_internal(message);
+
+  tls.bionic_systrace_disabled = false;
+}
+
+static void trace_end_internal() {
   if (!should_trace()) {
     return;
   }
@@ -103,6 +120,21 @@
   TEMP_FAILURE_RETRY(write(trace_marker_fd, const_cast<const char*>(buf), 2));
 }
 
+void bionic_trace_end() {
+  // Some functions called by trace_end_internal() can call
+  // bionic_trace_begin(). Prevent infinite recursion and non-recursive mutex
+  // deadlock by using a flag in the thread local storage.
+  bionic_tls& tls = __get_bionic_tls();
+  if (tls.bionic_systrace_disabled) {
+    return;
+  }
+  tls.bionic_systrace_disabled = true;
+
+  trace_end_internal();
+
+  tls.bionic_systrace_disabled = false;
+}
+
 ScopedTrace::ScopedTrace(const char* message) : called_end_(false) {
   bionic_trace_begin(message);
 }
diff --git a/libc/bionic/libc_init_dynamic.cpp b/libc/bionic/libc_init_dynamic.cpp
index 4625fa1..24efbf5 100644
--- a/libc/bionic/libc_init_dynamic.cpp
+++ b/libc/bionic/libc_init_dynamic.cpp
@@ -93,6 +93,15 @@
   __libc_init_common();
   __libc_init_scudo();
 
+#if __has_feature(hwaddress_sanitizer)
+  // Notify the HWASan runtime library whenever a library is loaded or unloaded
+  // so that it can update its shadow memory.
+  // This has to happen before _libc_init_malloc which might dlopen to load
+  // profiler libraries.
+  __libc_shared_globals()->load_hook = __hwasan_library_loaded;
+  __libc_shared_globals()->unload_hook = __hwasan_library_unloaded;
+#endif
+
   // Hooks for various libraries to let them know that we're starting up.
   __libc_globals.mutate(__libc_init_malloc);
 
@@ -101,13 +110,6 @@
 
   __libc_init_fork_handler();
 
-#if __has_feature(hwaddress_sanitizer)
-  // Notify the HWASan runtime library whenever a library is loaded or unloaded
-  // so that it can update its shadow memory.
-  __libc_shared_globals()->load_hook = __hwasan_library_loaded;
-  __libc_shared_globals()->unload_hook = __hwasan_library_unloaded;
-#endif
-
   __libc_shared_globals()->set_target_sdk_version_hook = __libc_set_target_sdk_version;
 
   netdClientInit();
diff --git a/libc/bionic/libc_init_static.cpp b/libc/bionic/libc_init_static.cpp
index 67e692c..ef2e56a 100644
--- a/libc/bionic/libc_init_static.cpp
+++ b/libc/bionic/libc_init_static.cpp
@@ -194,7 +194,7 @@
   if (note->n_namesz != 8 || strncmp(name, "Android", 8) != 0) {
     return false;
   }
-  if (note->n_type != NT_TYPE_MEMTAG) {
+  if (note->n_type != NT_ANDROID_TYPE_MEMTAG) {
     return false;
   }
   if (note->n_descsz != 4) {
diff --git a/libc/include/bits/fortify/stdio.h b/libc/include/bits/fortify/stdio.h
index 42698dd..77bdbb4 100644
--- a/libc/include/bits/fortify/stdio.h
+++ b/libc/include/bits/fortify/stdio.h
@@ -40,6 +40,7 @@
 /* No diag -- clang diagnoses misuses of this on its own.  */
 __BIONIC_FORTIFY_INLINE __printflike(3, 0)
 int vsnprintf(char* const __pass_object_size dest, size_t size, const char* format, va_list ap)
+        __diagnose_as_builtin(__builtin_vsnprintf, 1, 2, 3, 4)
         __overloadable {
     return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
 }
@@ -70,6 +71,7 @@
 /* No diag -- clang diagnoses misuses of this on its own.  */
 __BIONIC_FORTIFY_VARIADIC __printflike(3, 4)
 int snprintf(char* const __pass_object_size dest, size_t size, const char* format, ...)
+        __diagnose_as_builtin(__builtin_snprintf, 1, 2, 3)
         __overloadable {
     va_list va;
     va_start(va, format);
diff --git a/libc/include/bits/fortify/string.h b/libc/include/bits/fortify/string.h
index beb5ff5..08bce2d 100644
--- a/libc/include/bits/fortify/string.h
+++ b/libc/include/bits/fortify/string.h
@@ -44,13 +44,16 @@
 /* No diag -- clang diagnoses misuses of this on its own.  */
 __BIONIC_FORTIFY_INLINE
 void* memcpy(void* const dst __pass_object_size0, const void* src, size_t copy_amount)
+        __diagnose_as_builtin(__builtin_memcpy, 1, 2, 3)
         __overloadable {
     return __builtin___memcpy_chk(dst, src, copy_amount, __bos0(dst));
 }
 
 /* No diag -- clang diagnoses misuses of this on its own.  */
 __BIONIC_FORTIFY_INLINE
-void* memmove(void* const dst __pass_object_size0, const void* src, size_t len) __overloadable {
+void* memmove(void* const dst __pass_object_size0, const void* src, size_t len)
+        __diagnose_as_builtin(__builtin_memmove, 1, 2, 3)
+        __overloadable {
     return __builtin___memmove_chk(dst, src, len, __bos0(dst));
 }
 #endif
@@ -59,6 +62,7 @@
 #if __ANDROID_API__ >= 30
 __BIONIC_FORTIFY_INLINE
 void* mempcpy(void* const dst __pass_object_size0, const void* src, size_t copy_amount)
+        __diagnose_as_builtin(__builtin_mempcpy, 1, 2, 3)
         __overloadable
         __clang_error_if(__bos_unevaluated_lt(__bos0(dst), copy_amount),
                          "'mempcpy' called with size bigger than buffer") {
@@ -87,6 +91,7 @@
 
 __BIONIC_FORTIFY_INLINE
 char* strcpy(char* const dst __pass_object_size, const char* src)
+        __diagnose_as_builtin(__builtin_strcpy, 1, 2)
         __overloadable
         __clang_error_if(__bos_unevaluated_le(__bos(dst), __builtin_strlen(src)),
                          "'strcpy' called with string bigger than buffer") {
@@ -112,7 +117,9 @@
 #if __ANDROID_API__ >= 17 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
 /* No diag -- clang diagnoses misuses of this on its own.  */
 __BIONIC_FORTIFY_INLINE
-char* strncat(char* const dst __pass_object_size, const char* src, size_t n) __overloadable {
+char* strncat(char* const dst __pass_object_size, const char* src, size_t n)
+       __diagnose_as_builtin(__builtin_strncat, 1, 2, 3)
+       __overloadable {
     return __builtin___strncat_chk(dst, src, n, __bos(dst));
 }
 #endif
@@ -120,6 +127,7 @@
 /* No diag -- clang diagnoses misuses of this on its own.  */
 __BIONIC_FORTIFY_INLINE
 void* memset(void* const s __pass_object_size0, int c, size_t n) __overloadable
+        __diagnose_as_builtin(__builtin_memset, 1, 2, 3)
         /* If you're a user who wants this warning to go away: use `(&memset)(foo, bar, baz)`. */
         __clang_warning_if(c && !n, "'memset' will set 0 bytes; maybe the arguments got flipped?") {
 #if __ANDROID_API__ >= 17 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
@@ -157,6 +165,7 @@
 /* No diag -- clang diagnoses misuses of this on its own.  */
 __BIONIC_FORTIFY_INLINE
 char* stpncpy(char* const dst __pass_object_size, const char* const src __pass_object_size, size_t n)
+        __diagnose_as_builtin(__builtin_stpncpy, 1, 2, 3)
         __overloadable {
     size_t bos_dst = __bos(dst);
     size_t bos_src = __bos(src);
@@ -172,6 +181,7 @@
 /* No diag -- clang diagnoses misuses of this on its own.  */
 __BIONIC_FORTIFY_INLINE
 char* strncpy(char* const dst __pass_object_size, const char* const src __pass_object_size, size_t n)
+        __diagnose_as_builtin(__builtin_strncpy, 1, 2, 3)
         __overloadable {
     size_t bos_dst = __bos(dst);
     size_t bos_src = __bos(src);
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index 2556d11..99a200a 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -327,6 +327,13 @@
 
 #define __overloadable __attribute__((overloadable))
 
+// TODO(pirama) Remove this version check after switching to clang-r445002
+#if __clang_major__ == 14 && __clang_patchlevel__ >= 2
+#define __diagnose_as_builtin(...) __attribute__((diagnose_as_builtin(__VA_ARGS__)))
+#else
+#define __diagnose_as_builtin(...)
+#endif
+
 /* Used to tag non-static symbols that are private and never exposed by the shared library. */
 #define __LIBC_HIDDEN__ __attribute__((visibility("hidden")))
 
diff --git a/libc/malloc_debug/UnwindBacktrace.cpp b/libc/malloc_debug/UnwindBacktrace.cpp
index dbaebb3..a7036d9 100644
--- a/libc/malloc_debug/UnwindBacktrace.cpp
+++ b/libc/malloc_debug/UnwindBacktrace.cpp
@@ -90,11 +90,13 @@
     std::shared_ptr<unwindstack::MapInfo> map_info = info->map_info;
 
     std::string line = android::base::StringPrintf("          #%0zd  pc %" PAD_PTR "  ", i, info->rel_pc);
-    if (map_info->offset() != 0) {
+    if (map_info != nullptr && map_info->offset() != 0) {
       line += android::base::StringPrintf("(offset 0x%" PRIx64 ") ", map_info->offset());
     }
 
-    if (map_info->name().empty()) {
+    if (map_info == nullptr) {
+      line += "<unknown>";
+    } else if (map_info->name().empty()) {
       line += android::base::StringPrintf("<anonymous:%" PRIx64 ">", map_info->start());
     } else {
       line += map_info->name();
diff --git a/libc/malloc_hooks/tests/malloc_hooks_tests.cpp b/libc/malloc_hooks/tests/malloc_hooks_tests.cpp
index 1858781..ca064c2 100644
--- a/libc/malloc_hooks/tests/malloc_hooks_tests.cpp
+++ b/libc/malloc_hooks/tests/malloc_hooks_tests.cpp
@@ -350,6 +350,9 @@
   RunTest("*.DISABLED_aligned_alloc_hook_error");
 }
 
+// Allow deliberate call with non-power-of-two alignment in test code.
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnon-power-of-two-alignment"
 TEST_F(MallocHooksTest, DISABLED_aligned_alloc_hook_error) {
   Init();
   ASSERT_TRUE(__memalign_hook != nullptr);
@@ -365,6 +368,7 @@
   EXPECT_FALSE(void_arg_ != nullptr)
       << "The memalign hook was called with a nullptr with an error.";
 }
+#pragma clang diagnostic pop
 
 #if !defined(__LP64__)
 TEST_F(MallocHooksTest, pvalloc_hook) {
diff --git a/libc/private/bionic_asm_note.h b/libc/private/bionic_asm_note.h
index 80b8f01..d8559ff 100644
--- a/libc/private/bionic_asm_note.h
+++ b/libc/private/bionic_asm_note.h
@@ -28,6 +28,6 @@
 
 #pragma once
 
-#define NT_TYPE_IDENT 1
-#define NT_TYPE_KUSER 3
-#define NT_TYPE_MEMTAG 4
+#define NT_ANDROID_TYPE_IDENT 1
+#define NT_ANDROID_TYPE_KUSER 3
+#define NT_ANDROID_TYPE_MEMTAG 4
diff --git a/libc/private/bionic_tls.h b/libc/private/bionic_tls.h
index 80d645a..53fe3d5 100644
--- a/libc/private/bionic_tls.h
+++ b/libc/private/bionic_tls.h
@@ -129,7 +129,8 @@
   passwd_state_t passwd;
 
   char fdtrack_disabled;
-  char padding[3];
+  char bionic_systrace_disabled;
+  char padding[2];
 
   // Initialize the main thread's final object using its bootstrap object.
   void copy_from_bootstrap(const bionic_tls* boot __attribute__((unused))) {
diff --git a/tests/struct_layout_test.cpp b/tests/struct_layout_test.cpp
index 00fd4d5..0123ed9 100644
--- a/tests/struct_layout_test.cpp
+++ b/tests/struct_layout_test.cpp
@@ -69,7 +69,8 @@
   CHECK_OFFSET(bionic_tls, group, 11952);
   CHECK_OFFSET(bionic_tls, passwd, 12040);
   CHECK_OFFSET(bionic_tls, fdtrack_disabled, 12192);
-  CHECK_OFFSET(bionic_tls, padding, 12193);
+  CHECK_OFFSET(bionic_tls, bionic_systrace_disabled, 12193);
+  CHECK_OFFSET(bionic_tls, padding, 12194);
 #else
   CHECK_SIZE(pthread_internal_t, 668);
   CHECK_OFFSET(pthread_internal_t, next, 0);
@@ -110,7 +111,8 @@
   CHECK_OFFSET(bionic_tls, group, 10892);
   CHECK_OFFSET(bionic_tls, passwd, 10952);
   CHECK_OFFSET(bionic_tls, fdtrack_disabled, 11076);
-  CHECK_OFFSET(bionic_tls, padding, 11077);
+  CHECK_OFFSET(bionic_tls, bionic_systrace_disabled, 11077);
+  CHECK_OFFSET(bionic_tls, padding, 11078);
 #endif  // __LP64__
 #undef CHECK_SIZE
 #undef CHECK_OFFSET