[automerger skipped] Make android.hardware.renderscript@1.0 buildable for native_bridge am: fd83b6b8cb -s ours

am skip reason: Change-Id Iaf5b1942a05617934e7f42d760c6aea5551d0b89 with SHA-1 538af7d4cc is in history

Original change: https://googleplex-android-review.googlesource.com/c/platform/system/libhidl/+/12127773

Change-Id: I40f4de869e74c3fc893ddbd3566e0d540bf27df2
diff --git a/Android.bp b/Android.bp
index e0ca9e2..f428bfb 100644
--- a/Android.bp
+++ b/Android.bp
@@ -110,13 +110,18 @@
 }
 
 // WARNING: deprecated
-// This library is no longer required, and dependencies should be taken
-// on libhidlbase instead.
+// This library is no longer required, and dependencies should be taken on libhidlbase instead.
+// This is automatically removed by bpfix. Once there are no makefiles, fixes can be automatically applied, and this can be removed.
 cc_library {
     name: "libhidltransport",
     vendor_available: true,
 
-    visibility: [":__subpackages__"],
+    visibility: [
+        ":__subpackages__",
+        "//hardware:__subpackages__",
+        "//test/sts:__subpackages__",
+        "//vendor:__subpackages__",
+    ],
 }
 
 cc_defaults {
diff --git a/MODULE_LICENSE_APACHE2 b/MODULE_LICENSE_APACHE2
deleted file mode 100644
index e69de29..0000000
--- a/MODULE_LICENSE_APACHE2
+++ /dev/null
diff --git a/TEST_MAPPING b/TEST_MAPPING
index 01179d5..22c9d36 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -8,6 +8,9 @@
     },
     {
       "name": "hal_implementation_test"
+    },
+    {
+      "name": "hidl_lazy_test"
     }
   ]
 }
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 1b91c26..1bb38e8 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -342,7 +342,6 @@
         memset(mPad, 0, sizeof(mPad));
     }
 
-    // Note, does not initialize primitive types.
     hidl_vec(size_t size) : hidl_vec() { resize(size); }
 
     hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
@@ -412,7 +411,7 @@
     }
 
     T *releaseData() {
-        if (!mOwnsBuffer && mSize > 0) {
+        if (!mOwnsBuffer && mBuffer != nullptr) {
             resize(mSize);
         }
         mOwnsBuffer = false;
@@ -507,7 +506,7 @@
         return mBuffer[index];
     }
 
-    // Does not initialize primitive types if new size > old size.
+    // Copies over old elements fitting in new size. Value initializes the rest.
     void resize(size_t size) {
         if (size > UINT32_MAX) {
             details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
@@ -1044,6 +1043,9 @@
  */
 template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
 struct hidl_enum_range {
+    // Container-like associated type.
+    using value_type = T;
+
     constexpr auto begin() const { return std::begin(details::hidl_enum_values<T>); }
     constexpr auto cbegin() const { return begin(); }
     constexpr auto rbegin() const { return std::rbegin(details::hidl_enum_values<T>); }
diff --git a/base/include/hidl/MQDescriptor.h b/base/include/hidl/MQDescriptor.h
index 0f61cb5..eb244e5 100644
--- a/base/include/hidl/MQDescriptor.h
+++ b/base/include/hidl/MQDescriptor.h
@@ -164,14 +164,10 @@
 template<typename T>
 using MQDescriptorUnsync = MQDescriptor<T, kUnsynchronizedWrite>;
 
-template<typename T, MQFlavor flavor>
-MQDescriptor<T, flavor>::MQDescriptor(
-        const std::vector<GrantorDescriptor>& grantors,
-        native_handle_t* nhandle,
-        size_t size)
-    : mHandle(nhandle),
-      mQuantum(size),
-      mFlags(flavor) {
+template <typename T, MQFlavor flavor>
+MQDescriptor<T, flavor>::MQDescriptor(const std::vector<GrantorDescriptor>& grantors,
+                                      native_handle_t* nhandle, size_t size)
+    : mHandle(nhandle), mQuantum(static_cast<uint32_t>(size)), mFlags(flavor) {
     mGrantors.resize(grantors.size());
     for (size_t i = 0; i < grantors.size(); ++i) {
         if (isAlignedToWordBoundary(grantors[i].offset) == false) {
@@ -181,10 +177,10 @@
     }
 }
 
-template<typename T, MQFlavor flavor>
-MQDescriptor<T, flavor>::MQDescriptor(size_t bufferSize, native_handle_t *nHandle,
-                                   size_t messageSize, bool configureEventFlag)
-    : mHandle(nHandle), mQuantum(messageSize), mFlags(flavor) {
+template <typename T, MQFlavor flavor>
+MQDescriptor<T, flavor>::MQDescriptor(size_t bufferSize, native_handle_t* nHandle,
+                                      size_t messageSize, bool configureEventFlag)
+    : mHandle(nHandle), mQuantum(static_cast<uint32_t>(messageSize)), mFlags(flavor) {
     /*
      * If configureEventFlag is true, allocate an additional spot in mGrantor
      * for containing the fd and offset for mmapping the EventFlag word.
@@ -234,9 +230,8 @@
             mHandle->data[i] = dup(other.mHandle->data[i]);
         }
 
-        memcpy(&mHandle->data[other.mHandle->numFds],
-               &other.mHandle->data[other.mHandle->numFds],
-               other.mHandle->numInts * sizeof(int));
+        memcpy(&mHandle->data[other.mHandle->numFds], &other.mHandle->data[other.mHandle->numFds],
+               static_cast<size_t>(other.mHandle->numInts) * sizeof(int));
     }
 
     return *this;
@@ -258,7 +253,7 @@
 
 template<typename T, MQFlavor flavor>
 size_t MQDescriptor<T, flavor>::getSize() const {
-  return mGrantors[DATAPTRPOS].extent;
+    return static_cast<size_t>(mGrantors[DATAPTRPOS].extent);
 }
 
 template<typename T, MQFlavor flavor>
diff --git a/libhidlmemory/Android.bp b/libhidlmemory/Android.bp
index 41da614..d3bef74 100644
--- a/libhidlmemory/Android.bp
+++ b/libhidlmemory/Android.bp
@@ -15,8 +15,15 @@
 cc_library {
     name: "libhidlmemory",
     vendor_available: true,
+    // Host support is needed for testing only
+    host_supported: true,
     // TODO(b/153609531): remove when no longer needed.
     native_bridge_supported: true,
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
     vndk: {
         enabled: true,
         support_system_process: true,
diff --git a/minijail/Android.bp b/minijail/Android.bp
index e025033..83db2d9 100644
--- a/minijail/Android.bp
+++ b/minijail/Android.bp
@@ -13,5 +13,9 @@
         "libbase",
         "libminijail",
     ],
-    visibility: [":__subpackages__"],
+    visibility: [
+        ":__subpackages__",
+        "//hardware/interfaces/configstore/1.1/default",
+        "//vendor:__subpackages__",
+    ],
 }
diff --git a/test_main.cpp b/test_main.cpp
index e4cdd29..0a1e97b 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -222,6 +222,24 @@
     EXPECT_ARRAYEQ(v3, array, v3.size());
 }
 
+TEST_F(LibHidlTest, VecReleaseTest) {
+    // this test indicates an inconsistency of behaviors which is undesirable.
+    // Perhaps hidl-vec should always allocate an empty vector whenever it
+    // exposes its data. Alternatively, perhaps it should always free/reject
+    // empty vectors and always return nullptr for this state. While this second
+    // alternative is faster, it makes client code harder to write, and it would
+    // break existing client code.
+    using android::hardware::hidl_vec;
+
+    hidl_vec<int32_t> empty;
+    EXPECT_EQ(nullptr, empty.releaseData());
+
+    empty.resize(0);
+    int32_t* data = empty.releaseData();
+    EXPECT_NE(nullptr, data);
+    delete data;
+}
+
 TEST_F(LibHidlTest, VecIterTest) {
     int32_t array[] = {5, 6, 7};
     android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
diff --git a/transport/HidlLazyUtils.cpp b/transport/HidlLazyUtils.cpp
index 08ed676..d00b461 100644
--- a/transport/HidlLazyUtils.cpp
+++ b/transport/HidlLazyUtils.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "HidlLazyUtils"
+
 #include <hidl/HidlLazyUtils.h>
 #include <hidl/HidlTransportSupport.h>
 
@@ -29,15 +31,27 @@
 using ::android::hidl::base::V1_0::IBase;
 
 class ClientCounterCallback : public ::android::hidl::manager::V1_2::IClientCallback {
-   public:
-    ClientCounterCallback() : mNumConnectedServices(0) {}
+  public:
+    ClientCounterCallback() {}
 
     bool addRegisteredService(const sp<IBase>& service, const std::string& name);
 
-   protected:
+  protected:
     Return<void> onClients(const sp<IBase>& service, bool clients) override;
 
-   private:
+  private:
+    struct Service {
+        sp<IBase> service;
+        std::string name;
+        bool clients = false;
+    };
+
+    /**
+     * Looks up service that is guaranteed to be registered (service from
+     * onClients).
+     */
+    Service& assertRegisteredService(const sp<IBase>& service);
+
     /**
      * Registers or re-registers services. Returns whether successful.
      */
@@ -50,28 +64,19 @@
     void tryShutdown();
 
     /**
-     * Counter of the number of services that currently have at least one client.
-     */
-    size_t mNumConnectedServices;
-
-    struct Service {
-        sp<IBase> service;
-        std::string name;
-    };
-    /**
      * Number of services that have been registered.
      */
     std::vector<Service> mRegisteredServices;
 };
 
 class LazyServiceRegistrarImpl {
-   public:
+  public:
     LazyServiceRegistrarImpl() : mClientCallback(new ClientCounterCallback) {}
 
     status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service,
                              const std::string& name);
 
-   private:
+  private:
     sp<ClientCounterCallback> mClientCallback;
 };
 
@@ -86,6 +91,17 @@
     return success;
 }
 
+ClientCounterCallback::Service& ClientCounterCallback::assertRegisteredService(
+        const sp<IBase>& service) {
+    for (Service& registered : mRegisteredServices) {
+        if (registered.service != service) continue;
+        return registered;
+    }
+    LOG(FATAL) << "Got callback on service " << getDescriptor(service.get())
+               << " which we did not register.";
+    __builtin_unreachable();
+}
+
 bool ClientCounterCallback::registerService(const sp<IBase>& service, const std::string& name) {
     auto manager = hardware::defaultServiceManager1_2();
 
@@ -114,17 +130,24 @@
  */
 Return<void> ClientCounterCallback::onClients(const sp<::android::hidl::base::V1_0::IBase>& service,
                                               bool clients) {
-    if (clients) {
-        mNumConnectedServices++;
-    } else {
-        mNumConnectedServices--;
+    Service& registered = assertRegisteredService(service);
+    if (registered.clients == clients) {
+        LOG(FATAL) << "Process already thought " << getDescriptor(service.get()) << "/"
+                   << registered.name << " had clients: " << registered.clients
+                   << " but hwservicemanager has notified has clients: " << clients;
+    }
+    registered.clients = clients;
+
+    size_t numWithClients = 0;
+    for (const Service& registered : mRegisteredServices) {
+        if (registered.clients) numWithClients++;
     }
 
-    LOG(INFO) << "Process has " << mNumConnectedServices << " (of " << mRegisteredServices.size()
+    LOG(INFO) << "Process has " << numWithClients << " (of " << mRegisteredServices.size()
               << " available) client(s) in use after notification " << getDescriptor(service.get())
-              << " has clients: " << clients;
+              << "/" << registered.name << " has clients: " << clients;
 
-    if (mNumConnectedServices == 0) {
+    if (numWithClients == 0) {
         tryShutdown();
     }
 
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index d7faa6d..1c30909 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -153,6 +153,36 @@
 
 namespace details {
 
+#ifdef ENFORCE_VINTF_MANIFEST
+static constexpr bool kEnforceVintfManifest = true;
+#else
+static constexpr bool kEnforceVintfManifest = false;
+#endif
+
+#ifdef LIBHIDL_TARGET_DEBUGGABLE
+static constexpr bool kDebuggable = true;
+#else
+static constexpr bool kDebuggable = false;
+#endif
+
+static bool* getTrebleTestingOverridePtr() {
+    static bool gTrebleTestingOverride = false;
+    return &gTrebleTestingOverride;
+}
+
+void setTrebleTestingOverride(bool testingOverride) {
+    *getTrebleTestingOverridePtr() = testingOverride;
+}
+
+static inline bool isTrebleTestingOverride() {
+    if (kEnforceVintfManifest && !kDebuggable) {
+        // don't allow testing override in production
+        return false;
+    }
+
+    return *getTrebleTestingOverridePtr();
+}
+
 /*
  * Returns the age of the current process by reading /proc/self/stat and comparing starttime to the
  * current time. This is useful for measuring how long it took a HAL to register itself.
@@ -192,6 +222,7 @@
     tryShortenProcessName(descriptor);
 }
 
+// only used by prebuilts - should be able to remove
 void onRegistration(const std::string& packageName, const std::string& interfaceName,
                     const std::string& instanceName) {
     return onRegistrationImpl(packageName + "::" + interfaceName, instanceName);
@@ -372,10 +403,7 @@
 #endif
         };
 
-#ifdef LIBHIDL_TARGET_DEBUGGABLE
-        const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
-        const bool trebleTestingOverride = env && !strcmp(env, "true");
-        if (trebleTestingOverride) {
+        if (details::isTrebleTestingOverride()) {
             // Load HAL implementations that are statically linked
             handle = dlopen(nullptr, dlMode);
             if (handle == nullptr) {
@@ -386,7 +414,6 @@
                 return;
             }
         }
-#endif
 
         for (const std::string& path : paths) {
             std::vector<std::string> libs = findFiles(path, prefix, ".so");
@@ -737,28 +764,6 @@
     return false;
 }
 
-#ifdef ENFORCE_VINTF_MANIFEST
-static constexpr bool kEnforceVintfManifest = true;
-#else
-static constexpr bool kEnforceVintfManifest = false;
-#endif
-
-#ifdef LIBHIDL_TARGET_DEBUGGABLE
-static constexpr bool kDebuggable = true;
-#else
-static constexpr bool kDebuggable = false;
-#endif
-
-static inline bool isTrebleTestingOverride() {
-    if (kEnforceVintfManifest && !kDebuggable) {
-        // don't allow testing override in production
-        return false;
-    }
-
-    const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
-    return env && !strcmp(env, "true");
-}
-
 sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
                                                              const std::string& instance,
                                                              bool retry, bool getStub) {
diff --git a/transport/allocator/1.0/Android.bp b/transport/allocator/1.0/Android.bp
index 6da3793..4196216 100644
--- a/transport/allocator/1.0/Android.bp
+++ b/transport/allocator/1.0/Android.bp
@@ -5,9 +5,6 @@
     root: "android.hidl",
     // TODO(b/153609531): remove when no longer needed.
     native_bridge_supported: true,
-    vndk: {
-        enabled: true,
-    },
     srcs: [
         "IAllocator.hal",
     ],
diff --git a/transport/allocator/1.0/vts/functional/Android.bp b/transport/allocator/1.0/vts/functional/Android.bp
index 31d9821..ba7aa70 100644
--- a/transport/allocator/1.0/vts/functional/Android.bp
+++ b/transport/allocator/1.0/vts/functional/Android.bp
@@ -27,3 +27,6 @@
     test_suites: ["general-tests", "vts"],
 }
 
+vts_config {
+    name: "VtsHidlAllocatorV1_0Target",
+}
diff --git a/transport/base/1.0/Android.bp b/transport/base/1.0/Android.bp
index 8c796b2..2ce883b 100644
--- a/transport/base/1.0/Android.bp
+++ b/transport/base/1.0/Android.bp
@@ -5,9 +5,6 @@
     root: "android.hidl",
     // TODO(b/153609531): remove when no longer needed.
     native_bridge_supported: true,
-    vndk: {
-        enabled: true,
-    },
     srcs: [
         "types.hal",
         "IBase.hal",
diff --git a/transport/base/1.0/vts/functional/Android.bp b/transport/base/1.0/vts/functional/Android.bp
index 0c814ae..1c6e7ad 100644
--- a/transport/base/1.0/vts/functional/Android.bp
+++ b/transport/base/1.0/vts/functional/Android.bp
@@ -40,3 +40,7 @@
     require_root: true,
     auto_gen_config: true,
 }
+
+vts_config {
+    name: "VtsHalBaseV1_0TargetTest",
+}
diff --git a/transport/base/1.0/vts/functional/Android.mk b/transport/base/1.0/vts/functional/Android.mk
deleted file mode 100644
index 61c6e31..0000000
--- a/transport/base/1.0/vts/functional/Android.mk
+++ /dev/null
@@ -1,22 +0,0 @@
-#
-# Copyright (C) 2017 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := VtsHalBaseV1_0TargetTest
--include test/vts/tools/build/Android.host_config.mk
diff --git a/transport/include/hidl/ServiceManagement.h b/transport/include/hidl/ServiceManagement.h
index 4573a25..c8e8f6c 100644
--- a/transport/include/hidl/ServiceManagement.h
+++ b/transport/include/hidl/ServiceManagement.h
@@ -47,6 +47,10 @@
 // e.x.: android.hardware.foo@1.0::IFoo, default
 void waitForHwService(const std::string &interface, const std::string &instanceName);
 
+// Only works on userdebug/eng builds. This allows getService to bypass the
+// VINTF manifest for testing only.
+void setTrebleTestingOverride(bool testingOverride);
+
 void preloadPassthroughService(const std::string &descriptor);
 
 // Returns a service with the following constraints:
diff --git a/transport/manager/1.0/Android.bp b/transport/manager/1.0/Android.bp
index c91dcd2..6f1bc20 100644
--- a/transport/manager/1.0/Android.bp
+++ b/transport/manager/1.0/Android.bp
@@ -3,9 +3,6 @@
 hidl_interface {
     name: "android.hidl.manager@1.0",
     root: "android.hidl",
-    vndk: {
-        enabled: true,
-    },
     srcs: [
         "IServiceManager.hal",
         "IServiceNotification.hal",
diff --git a/transport/manager/1.1/Android.bp b/transport/manager/1.1/Android.bp
index 82545e5..1358f38 100644
--- a/transport/manager/1.1/Android.bp
+++ b/transport/manager/1.1/Android.bp
@@ -3,9 +3,6 @@
 hidl_interface {
     name: "android.hidl.manager@1.1",
     root: "android.hidl",
-    vndk: {
-        enabled: true,
-    },
     srcs: [
         "IServiceManager.hal",
     ],
diff --git a/transport/manager/1.2/Android.bp b/transport/manager/1.2/Android.bp
index e7ee143..9c77035 100644
--- a/transport/manager/1.2/Android.bp
+++ b/transport/manager/1.2/Android.bp
@@ -3,9 +3,6 @@
 hidl_interface {
     name: "android.hidl.manager@1.2",
     root: "android.hidl",
-    vndk: {
-        enabled: true,
-    },
     srcs: [
         "IClientCallback.hal",
         "IServiceManager.hal",
diff --git a/transport/memory/block/1.0/Android.bp b/transport/memory/block/1.0/Android.bp
index f26a6d3..86c29bc 100644
--- a/transport/memory/block/1.0/Android.bp
+++ b/transport/memory/block/1.0/Android.bp
@@ -3,9 +3,6 @@
 hidl_interface {
     name: "android.hidl.memory.block@1.0",
     root: "android.hidl",
-    vndk: {
-        enabled: true,
-    },
     srcs: [
         "types.hal",
     ],
diff --git a/transport/token/1.0/utils/Android.bp b/transport/token/1.0/utils/Android.bp
index 5ccbe75..5f62804 100644
--- a/transport/token/1.0/utils/Android.bp
+++ b/transport/token/1.0/utils/Android.bp
@@ -16,6 +16,13 @@
     name: "android.hidl.token@1.0-utils",
     defaults: ["libhidl-defaults"],
     vendor_available: true,
+    // Host support is needed for testing only
+    host_supported: true,
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
     vndk: {
         enabled: true,
     },