Disable NativeAllocationRegisteryTest for native bridge

1. NativeAllocationRegistry API cannot be emulated properly when
executed in a natively bridged environment.

2. This is because the API assumes that a native callback address is
provided to the system in a jlong. Native bridge must wrap the callback,
which is of a guest CPU arch, with a host CPU arch runner (that the
system will actually be able to call). But jlong doesn't express function
prototype, so we don't know how to pass arguments and return the result.

3. At the same time, NativeAllocationRegistry APIs are internal only and
they are always compiled for native CPU architecture. Thus running test
under native bridge (with ABI translation) doesn't correspond to any real
life scenario. And so it is ok to exclude this case from CTS tests.

Test: cts-tradefed run cts -m CtsLibcoreTestCases \
-t libcore.libcore.util.NativeAllocationRegistryTest
(with and without native bridge)

Bug: 71386447
Bug: 72533441
Bug: 72740423

Change-Id: Iaf6d5d54cf7d5fc87223318382d3ca2b2fd588ad
Merged-Id: I9e77b65246df47c2e0adc2930ba7839a991a8985
(Cherry-picked from 3e4099c48010b76c871831d6eccec5b40629fabd)
diff --git a/luni/src/test/java/libcore/libcore/util/NativeAllocationRegistryTest.java b/luni/src/test/java/libcore/libcore/util/NativeAllocationRegistryTest.java
index a7b0cac..978ec37 100644
--- a/luni/src/test/java/libcore/libcore/util/NativeAllocationRegistryTest.java
+++ b/luni/src/test/java/libcore/libcore/util/NativeAllocationRegistryTest.java
@@ -46,6 +46,16 @@
     // Verify that NativeAllocations and their referents are freed before we run
     // out of space for new allocations.
     private void testNativeAllocation(TestConfig config) {
+        if (isNativeBridgedABI()) {
+            // 1. This test is intended to test platform internals, not public API.
+            // 2. The test would fail under native bridge as a side effect of how the tests work:
+            //  - The tests run using the app architecture instead of the platform architecture
+            //  - That scenario will never happen in practice due to (1)
+            // 3. This leaves a hole in testing for the case of native bridge, due to limitations
+            //    in the testing infrastructure from (2).
+            System.logI("Skipping test for native bridged ABI");
+            return;
+        }
         Runtime.getRuntime().gc();
         long max = Runtime.getRuntime().maxMemory();
         long total = Runtime.getRuntime().totalMemory();
@@ -119,6 +129,11 @@
     }
 
     public void testEarlyFree() {
+        if (isNativeBridgedABI()) {
+            // See the explanation in testNativeAllocation.
+            System.logI("Skipping test for native bridged ABI");
+            return;
+        }
         long size = 1234;
         NativeAllocationRegistry registry
             = new NativeAllocationRegistry(classLoader, getNativeFinalizer(), size);
@@ -193,6 +208,7 @@
         fail("Expected IllegalArgumentException, but no exception was thrown.");
     }
 
+    private static native boolean isNativeBridgedABI();
     private static native long getNativeFinalizer();
     private static native long doNativeAllocation(long size);
     private static native long getNumNativeBytesAllocated();
diff --git a/luni/src/test/native/libcore_libcore_util_NativeAllocationRegistryTest.cpp b/luni/src/test/native/libcore_libcore_util_NativeAllocationRegistryTest.cpp
index 149aae5..62982f4 100644
--- a/luni/src/test/native/libcore_libcore_util_NativeAllocationRegistryTest.cpp
+++ b/luni/src/test/native/libcore_libcore_util_NativeAllocationRegistryTest.cpp
@@ -15,12 +15,60 @@
  */
 
 #include <sys/stat.h>
+#include <stdio.h>
+#include <string.h>
+
 #include <string>
 
 #include <jni.h>
 #include <nativehelper/JNIHelp.h>
 #include <nativehelper/ScopedUtfChars.h>
 
+constexpr bool IsX86BuildArch() {
+#if defined(__i386__)
+  return true;
+#elif defined(__x86_64__)
+  return true;
+#else
+  return false;
+#endif
+}
+
+constexpr bool IsArmBuildArch() {
+#if defined(__arm__)
+  return true;
+#elif defined(__aarch64__)
+  return true;
+#else
+  return false;
+#endif
+}
+
+extern "C"
+jboolean Java_libcore_libcore_util_NativeAllocationRegistryTest_isNativeBridgedABI(JNIEnv*, jclass) {
+  FILE* fp = popen("uname -m", "re");
+  char buf[128];
+  memset(buf, '\0', sizeof(buf));
+  char* str = fgets(buf, sizeof(buf), fp);
+  pclose(fp);
+  if (!str) {
+    // Assume no native bridge if cannot do uname.
+    return static_cast<jboolean>(false);
+  }
+
+  std::string uname_string = buf;
+  bool is_native_bridged_abi;
+  if (IsX86BuildArch()) {
+    is_native_bridged_abi = uname_string.find("86") == std::string::npos;
+  } else if (IsArmBuildArch()) {
+    is_native_bridged_abi = uname_string.find("arm") == std::string::npos &&
+        uname_string.find("aarch64") == std::string::npos;
+  } else {
+    is_native_bridged_abi = false;
+  }
+  return static_cast<jboolean>(is_native_bridged_abi);
+}
+
 uint64_t gNumNativeBytesAllocated = 0;
 
 static void finalize(uint64_t* ptr) {