Snap for 14099086 from 5f076439f419d72056b8c39a08eb77b2d5c851c1 to 25Q4-release

Change-Id: I515862481206f5888157365a5b16bf12c473ca04
diff --git a/libcutils/ashmem-dev.cpp b/libcutils/ashmem-dev.cpp
index c61e377..11aedb1 100644
--- a/libcutils/ashmem-dev.cpp
+++ b/libcutils/ashmem-dev.cpp
@@ -124,6 +124,27 @@
     return "/dev/ashmem" + boot_id;
 }
 
+static bool __init_ashmem_rdev() {
+    const std::string ashmem_device_path = get_ashmem_device_path();
+    if (ashmem_device_path.empty()) {
+        return false;
+    }
+
+    struct stat st;
+    if (TEMP_FAILURE_RETRY(stat(ashmem_device_path.c_str(), &st)) == -1) {
+        ALOGE("Unable to stat ashmem device: %m");
+        return false;
+    }
+    if (!S_ISCHR(st.st_mode)) {
+        ALOGE("ashmem device is not a character device");
+        errno = ENOTTY;
+        return false;
+    }
+
+    __ashmem_rdev = st.st_rdev;
+    return true;
+}
+
 int __ashmem_open() {
     static const std::string ashmem_device_path = get_ashmem_device_path();
 
@@ -131,40 +152,25 @@
         return -1;
     }
 
+    if (__ashmem_rdev == 0 && !__init_ashmem_rdev()) {
+        return -1;
+    }
+
     android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(ashmem_device_path.c_str(), O_RDWR | O_CLOEXEC)));
     if (!fd.ok()) {
         ALOGE("Unable to open ashmem device: %m");
         return -1;
     }
 
-    struct stat st;
-    if (TEMP_FAILURE_RETRY(fstat(fd, &st)) == -1) {
-        ALOGE("Unable to fstat ashmem device: %m");
-        return -1;
-    }
-    if (!S_ISCHR(st.st_mode) || !st.st_rdev) {
-        ALOGE("ashmem device is not a character device");
-        errno = ENOTTY;
-        return -1;
-    }
-
-    __ashmem_rdev = st.st_rdev;
     return fd.release();
 }
 
-static void __init_ashmem_rdev() {
-    // If __ashmem_rdev hasn't been initialized yet,
-    // create an ashmem fd for that side effect.
-    // This shouldn't happen if all ashmem fds come from us,
-    // but we know that the libcutils code has been copy & pasted.
-    // (Chrome, for example, contains a copy of an old version.)
-    android::base::unique_fd fd(__ashmem_open());
-}
-
 /* Make sure file descriptor references ashmem, negative number means false */
 // TODO: return bool
 static int __ashmem_is_ashmem(int fd, bool fatal) {
-    if (__ashmem_rdev == 0) __init_ashmem_rdev();
+    if (__ashmem_rdev == 0 && !__init_ashmem_rdev()) {
+        return -1;
+    }
 
     struct stat st;
     if (fstat(fd, &st) == -1) return -1;
diff --git a/trusty/test/binder/aidl/com/android/trusty/binder/test/ITestService.aidl b/trusty/test/binder/aidl/com/android/trusty/binder/test/ITestService.aidl
index cfbb246..53f229d 100644
--- a/trusty/test/binder/aidl/com/android/trusty/binder/test/ITestService.aidl
+++ b/trusty/test/binder/aidl/com/android/trusty/binder/test/ITestService.aidl
@@ -22,6 +22,7 @@
 
 interface ITestService {
     const @utf8InCpp String PORT = "com.android.trusty.binder.test.service";
+    const @utf8InCpp String HND_PORT = "com.android.trusty.binder.test.handover";
 
     const int TEST_CONSTANT = 42;
     const int TEST_CONSTANT2 = -42;
@@ -67,4 +68,7 @@
     ByteEnum[] ReverseByteEnum(in ByteEnum[] input, out ByteEnum[] repeated);
     IntEnum[] ReverseIntEnum(in IntEnum[] input, out IntEnum[] repeated);
     LongEnum[] ReverseLongEnum(in LongEnum[] input, out LongEnum[] repeated);
+
+    // Test that we can pass connections.
+    void Handover(in ParcelFileDescriptor handle);
 }