Merge "Merge cherrypicks of [2168845, 2175186, 2181977, 2181978, 2182297] into ndk-r25-release." into ndk-r25-release
diff --git a/build/cmake/android-legacy.toolchain.cmake b/build/cmake/android-legacy.toolchain.cmake
index 9d9ae35..3a0dd71 100644
--- a/build/cmake/android-legacy.toolchain.cmake
+++ b/build/cmake/android-legacy.toolchain.cmake
@@ -457,6 +457,7 @@
 list(APPEND ANDROID_LINKER_FLAGS_EXE -Wl,--gc-sections)
 
 # Debug and release flags.
+list(APPEND ANDROID_COMPILER_FLAGS_RELEASE -O3)
 list(APPEND ANDROID_COMPILER_FLAGS_RELEASE -DNDEBUG)
 if(ANDROID_TOOLCHAIN STREQUAL clang)
   list(APPEND ANDROID_COMPILER_FLAGS_DEBUG -fno-limit-debug-info)
diff --git a/docs/changelogs/Changelog-r25.md b/docs/changelogs/Changelog-r25.md
index 2267868..a83e15e 100644
--- a/docs/changelogs/Changelog-r25.md
+++ b/docs/changelogs/Changelog-r25.md
@@ -11,11 +11,28 @@
 [Android Studio site]: http://tools.android.com/filing-bugs
 [build system maintainers guide]: https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md
 
+
+## r25b
+
+* [Issue 1739]: Fixed C compatibility issue in `amidi/AMidi.h`.
+* [Issue 1740]: Fixed the legacy toolchain when using CMake's `Release` build
+  configuration. Since r23b it has not be receiving any optimization flag. It
+  will now receive `-O3`. If you're building with AGP and haven't overridden
+  AGP's default CMake modes, this change does not affect you, as AGP uses
+  `RelWithDebInfo` by default.
+* [Issue 1744]: Fixes ASan wrap.sh file to support 32-bit apps on 64-bit
+  devices.
+
+[Issue 1739]: https://github.com/android/ndk/issues/1739
+[Issue 1740]: https://github.com/android/ndk/issues/1740
+[Issue 1744]: https://github.com/android/ndk/issues/1744
+
 ## Changes
 
 * Includes Android 13 APIs.
 * Updated LLVM to clang-r450784d, based on LLVM 14 development.
   * [Issue 1455]: Improved display of Android API levels in Clang diagnostics.
+  * [Issue 1608]: Fixed crash in vector conversions.
   * [Issue 1710]: Fixed compiler crash caused by invalid `-march` values.
 * Eliminate duplicate static libraries in API-versioned sysroot directories.
   This reduces the uncompressed size of the NDK by 500 MB.
@@ -37,6 +54,7 @@
 [Issue 933]: https://github.com/android/ndk/issues/933
 [Issue 1334]: https://github.com/android/ndk/issues/1334
 [Issue 1455]: https://github.com/android/ndk/issues/1455
+[Issue 1608]: https://github.com/android/ndk/issues/1608
 [Issue 1634]: https://github.com/android/ndk/issues/1634
 [Issue 1693]: https://github.com/android/ndk/issues/1693
 [Issue 1710]: https://github.com/android/ndk/issues/1710
diff --git a/ndk/config.py b/ndk/config.py
index 43c04e4..92b44a2 100644
--- a/ndk/config.py
+++ b/ndk/config.py
@@ -2,7 +2,7 @@
 
 
 major = 25
-hotfix = 0
+hotfix = 1
 hotfix_str = chr(ord("a") + hotfix) if hotfix else ""
 beta = 0
 beta_str = "-beta{}".format(beta) if beta > 0 else ""
diff --git a/tests/build/cmake_default_flags/test.py b/tests/build/cmake_default_flags/test.py
index a7ead75..ca367a2 100644
--- a/tests/build/cmake_default_flags/test.py
+++ b/tests/build/cmake_default_flags/test.py
@@ -18,17 +18,38 @@
 from typing import Optional
 
 from ndk.test.spec import BuildConfiguration
-from ndk.testing.flag_verifier import FlagVerifier
+from ndk.testing.flag_verifier import FlagVerifier, FlagVerifierResult
 
 
-def run_test(ndk_path: str,
-             config: BuildConfiguration) -> tuple[bool, Optional[str]]:
-    """Check that the CMake toolchain uses the correct default flags.
+def check_configuration(
+    ndk_path: str,
+    build_config: BuildConfiguration,
+    cmake_config: str,
+    expected_flags: list[str],
+    unexpected_flags: list[str],
+) -> FlagVerifierResult:
+    verifier = FlagVerifier(Path("project"), Path(ndk_path), build_config)
+    for flag in expected_flags:
+        verifier.expect_flag(flag)
+    for flag in unexpected_flags:
+        verifier.expect_not_flag(flag)
+    return verifier.verify_cmake([f"-DCMAKE_BUILD_TYPE={cmake_config}"])
 
-    Currently this only tests the optimization flags for RelWithDebInfo, but
-    it's probably worth expanding in the future.
-    """
-    verifier = FlagVerifier(Path('project'), Path(ndk_path), config)
-    verifier.expect_flag('-O2')
-    return verifier.verify_cmake(['-DCMAKE_BUILD_TYPE=RelWithDebInfo'
-                                  ]).make_test_result_tuple()
+
+def run_test(ndk_path: str, config: BuildConfiguration) -> tuple[bool, Optional[str]]:
+    """Check that the CMake toolchain uses the correct default flags."""
+    verify_configs: dict[str, tuple[list[str], list[str]]] = {
+        # No flag is the same as -O0. As long as no other opt flag is used, the default
+        # is fine.
+        "Debug": ([], ["-O1", "-O2", "-O3", "-Os", "-Oz"]),
+        "MinSizeRel": (["-Os"], ["-O0", "-O1", "-O2", "-O3", "-Oz"]),
+        "Release": (["-O3"], ["-O0", "-O1", "-O2", "-Os", "-Oz"]),
+        "RelWithDebInfo": (["-O2"], ["-O0", "-O1", "-O3", "-Os", "-Oz"]),
+    }
+    for cmake_config, (expected_flags, unexpected_flags) in verify_configs.items():
+        result = check_configuration(
+            ndk_path, config, cmake_config, expected_flags, unexpected_flags
+        )
+        if result.failed():
+            return result.make_test_result_tuple()
+    return result.make_test_result_tuple()
diff --git a/wrap.sh/asan.sh b/wrap.sh/asan.sh
index 1e0fa20..3929540 100644
--- a/wrap.sh/asan.sh
+++ b/wrap.sh/asan.sh
@@ -4,15 +4,10 @@
 cmd=$1
 shift
 
-export ASAN_OPTIONS=log_to_syslog=false,allow_user_segv_handler=1
-ASAN_LIB=$(ls "$HERE"/libclang_rt.asan-*-android.so)
-if [ -f "$HERE/libc++_shared.so" ]; then
-    # Workaround for https://github.com/android-ndk/ndk/issues/988.
-    export LD_PRELOAD="$ASAN_LIB $HERE/libc++_shared.so"
-else
-    export LD_PRELOAD="$ASAN_LIB"
-fi
-
+# This must be called *before* `LD_PRELOAD` is set. Otherwise, if this is a 32-
+# bit app running on a 64-bit device, the 64-bit getprop will fail to load
+# because it will preload a 32-bit ASan runtime.
+# https://github.com/android/ndk/issues/1744
 os_version=$(getprop ro.build.version.sdk)
 
 if [ "$os_version" -eq "27" ]; then
@@ -23,4 +18,13 @@
   cmd="$cmd -XjdwpProvider:adbconnection -XjdwpOptions:suspend=n,server=y $@"
 fi
 
+export ASAN_OPTIONS=log_to_syslog=false,allow_user_segv_handler=1
+ASAN_LIB=$(ls "$HERE"/libclang_rt.asan-*-android.so)
+if [ -f "$HERE/libc++_shared.so" ]; then
+    # Workaround for https://github.com/android-ndk/ndk/issues/988.
+    export LD_PRELOAD="$ASAN_LIB $HERE/libc++_shared.so"
+else
+    export LD_PRELOAD="$ASAN_LIB"
+fi
+
 exec $cmd