Merge "Fix bionic unit tests with HWASan after Fortify+HWASan workaround." into main
diff --git a/android-changes-for-ndk-developers.md b/android-changes-for-ndk-developers.md
index 2430447..e094a1d 100644
--- a/android-changes-for-ndk-developers.md
+++ b/android-changes-for-ndk-developers.md
@@ -108,7 +108,7 @@
 to dlopen(3) (as opposed to being referenced by DT_NEEDED entries).
 
 
-## GNU hashes (Availible in API level >= 23)
+## GNU hashes (Available in API level >= 23)
 
 The GNU hash style available with `--hash-style=gnu` allows faster
 symbol lookup and is supported by Android's dynamic linker in API level 23 and
@@ -437,6 +437,8 @@
 adb shell setprop debug.ld.all dlerror,dlopen
 ```
 
+See also `LD_DEBUG`.
+
 
 ## dlclose interacts badly with thread local variables with non-trivial destructors
 
@@ -544,3 +546,20 @@
 _not_ apply if your app was debuggable. To be compatible with all API levels,
 always give files that need to be extracted a "lib" prefix and ".so" suffix,
 or avoid using `extractNativeLibs`.
+
+
+## The LD_DEBUG environment variable.
+
+On devices running API level 37 or later you can also use the `LD_DEBUG`
+environment variable when running a stand-alone executable such as a unit test.
+The syntax is broadly similar to glibc, and you can get help for the specific
+version of Android you're on by using `LD_DEBUG=help`.
+You can also enable everything by using `LD_DEBUG=all`.
+
+(Older versions of Android also supported `LD_DEBUG`,
+but used integers instead of strings.
+The meaning of those integers varied by release,
+and some releases compiled support for `LD_DEBUG` out of released builds,
+so the best advice is either "look at the corresponding source" or
+"start with `1` and keep increasing the number until you see what you want,
+or see no change in output from the previous value".)
diff --git a/libc/Android.bp b/libc/Android.bp
index 2a18ec8..a59ba03 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1627,6 +1627,7 @@
         export_headers_as_system: true,
         export_llndk_headers: ["libc_headers"],
     },
+    afdo: true,
 }
 
 cc_library {
diff --git a/libc/bionic/ndk_cruft.cpp b/libc/bionic/ndk_cruft.cpp
index a69b77f..bc06d9d 100644
--- a/libc/bionic/ndk_cruft.cpp
+++ b/libc/bionic/ndk_cruft.cpp
@@ -31,6 +31,8 @@
 // LP64 doesn't need to support any legacy cruft.
 #if !defined(__LP64__)
 
+#define __BIONIC_DISABLE_MALLOC_USABLE_SIZE_FORTIFY_WARNINGS
+
 #include <ctype.h>
 #include <dirent.h>
 #include <errno.h>
diff --git a/libc/include/malloc.h b/libc/include/malloc.h
index bb4916a..70a5ea2 100644
--- a/libc/include/malloc.h
+++ b/libc/include/malloc.h
@@ -121,10 +121,17 @@
 /**
  * [malloc_usable_size(3)](https://man7.org/linux/man-pages/man3/malloc_usable_size.3.html)
  * returns the actual size of the given heap block.
+ *
+ * malloc_usable_size() and _FORTIFY_SOURCE=3 are incompatible if you are using more of the
+ * allocation than originally requested. However, malloc_usable_size() can be used to keep track
+ * of allocation/deallocation byte counts and this is an exception to the incompatible rule. In this
+ * case, you can define __BIONIC_DISABLE_MALLOC_USABLE_SIZE_FORTIFY_WARNINGS to disable the
+ * compiler error.
  */
 __nodiscard size_t malloc_usable_size(const void* _Nullable __ptr)
-#if defined(_FORTIFY_SOURCE)
-    __clang_error_if(_FORTIFY_SOURCE == 3, "malloc_usable_size() and _FORTIFY_SOURCE=3 are incompatible")
+#if defined(_FORTIFY_SOURCE) && !defined(__BIONIC_DISABLE_MALLOC_USABLE_SIZE_FORTIFY_WARNINGS)
+    __clang_error_if(_FORTIFY_SOURCE == 3,
+      "malloc_usable_size() and _FORTIFY_SOURCE=3 are incompatible: see malloc_usable_size() documentation")
 #endif
 ;