Merge qt-r1-dev-plus-aosp-without-vendor (5817612) into stage-aosp-master

No content change.

Bug: 135460123
Change-Id: I4996b9922f2f8079fd466e378bcb074405920cd9
Merged-In: I345a581282c3c24facefef95df3d57e898783b31
diff --git a/Android.bp b/Android.bp
index dbc9fa2..29f9b56 100644
--- a/Android.bp
+++ b/Android.bp
@@ -26,10 +26,14 @@
     name: "libhidl",
     required: [
         "libhidlbase",
-        "libhidltransport",
     ],
 }
 
+cc_library_headers {
+    name: "libhidl_gtest_helpers",
+    export_include_dirs: ["gtest_helpers"],
+}
+
 cc_test {
     name: "libhidl_test",
     defaults: ["libhidl-defaults"],
@@ -41,8 +45,6 @@
         "android.hidl.memory@1.0",
         "libbase",
         "libhidlbase",
-        "libhidltransport",
-        "libhwbinder",
         "liblog",
         "libutils",
         "libcutils",
@@ -97,6 +99,8 @@
     whole_static_libs: [
         "libhwbinder_pgo-impl-internal",
     ],
+
+    visibility: ["//system/libhwbinder:__subpackages__"],
 }
 
 // WARNING: deprecated
diff --git a/adapter/Android.bp b/adapter/Android.bp
index eb322bc..ae801bc 100644
--- a/adapter/Android.bp
+++ b/adapter/Android.bp
@@ -23,14 +23,11 @@
     shared_libs: [
         "libbase",
         "libhidlbase",
-        "libhidltransport",
-        "libhwbinder",
         "liblog",
         "libutils",
     ],
     export_shared_lib_headers: [
         "libhidlbase",
-        "libhidltransport",
         "libutils",
     ],
 }
diff --git a/base/Android.bp b/base/Android.bp
index 8fe2702..0bc3af0 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -53,4 +53,6 @@
             cflags: ["-DLIBHIDL_TARGET_DEBUGGABLE"],
         },
     },
+
+    visibility: ["//system/libhidl:__subpackages__"],
 }
diff --git a/gtest_helpers/hidl/GtestPrinter.h b/gtest_helpers/hidl/GtestPrinter.h
new file mode 100644
index 0000000..c9f5dd3
--- /dev/null
+++ b/gtest_helpers/hidl/GtestPrinter.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#include <gtest/gtest.h>
+
+namespace android {
+namespace hardware {
+
+static inline std::string PrintInstanceNameToString(
+        const testing::TestParamInfo<std::string>& info) {
+    // test names need to be unique -> index prefix
+    std::string name = std::to_string(info.index) + "/" + info.param;
+
+    for (size_t i = 0; i < name.size(); i++) {
+        // gtest test names must only contain alphanumeric characters
+        if (!std::isalnum(name[i])) name[i] = '_';
+    }
+
+    return name;
+}
+
+}  // namespace hardware
+}  // namespace android
diff --git a/libhidlmemory/Android.bp b/libhidlmemory/Android.bp
index 2135ef3..2f97d5d 100644
--- a/libhidlmemory/Android.bp
+++ b/libhidlmemory/Android.bp
@@ -26,7 +26,6 @@
         "libutils",
         "libcutils",
         "libhidlbase",
-        "libhidltransport",
         "android.hidl.memory@1.0",
         "android.hidl.memory.token@1.0",
     ],
diff --git a/transport/Android.bp b/transport/Android.bp
index 50f277e..29c820a 100644
--- a/transport/Android.bp
+++ b/transport/Android.bp
@@ -94,4 +94,6 @@
             cflags: ["-DENFORCE_VINTF_MANIFEST"]
         },
     },
+
+    visibility: ["//system/libhidl:__subpackages__"],
 }
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index 357bb30..2187740 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -559,6 +559,18 @@
     return manager;
 }
 
+std::vector<std::string> getAllHalInstanceNames(const std::string& descriptor) {
+    std::vector<std::string> ret;
+    auto sm = defaultServiceManager1_2();
+    sm->listManifestByInterface(descriptor, [&](const auto& instances) {
+        ret.reserve(instances.size());
+        for (const auto& i : instances) {
+            ret.push_back(i);
+        }
+    });
+    return ret;
+}
+
 namespace details {
 
 void preloadPassthroughService(const std::string &descriptor) {
@@ -723,11 +735,32 @@
     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) {
-    using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport;
-    using ::android::hidl::manager::V1_0::IServiceManager;
+    using Transport = IServiceManager1_0::Transport;
     sp<Waiter> waiter;
 
     sp<IServiceManager1_1> sm;
@@ -753,30 +786,18 @@
 
     const bool vintfHwbinder = (transport == Transport::HWBINDER);
     const bool vintfPassthru = (transport == Transport::PASSTHROUGH);
+    const bool trebleTestingOverride = isTrebleTestingOverride();
+    const bool allowLegacy = !kEnforceVintfManifest || (trebleTestingOverride && kDebuggable);
+    const bool vintfLegacy = (transport == Transport::EMPTY) && allowLegacy;
 
-#ifdef ENFORCE_VINTF_MANIFEST
-
-#ifdef LIBHIDL_TARGET_DEBUGGABLE
-    const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
-    const bool trebleTestingOverride = env && !strcmp(env, "true");
-    const bool vintfLegacy = (transport == Transport::EMPTY) && trebleTestingOverride;
-#else   // ENFORCE_VINTF_MANIFEST but not LIBHIDL_TARGET_DEBUGGABLE
-    const bool trebleTestingOverride = false;
-    const bool vintfLegacy = false;
-#endif  // LIBHIDL_TARGET_DEBUGGABLE
-
-#else   // not ENFORCE_VINTF_MANIFEST
-    const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
-    const bool trebleTestingOverride = env && !strcmp(env, "true");
-    const bool vintfLegacy = (transport == Transport::EMPTY);
-
-    ALOGE("getService: Potential race detected. The VINTF manifest is not being enforced. If a HAL "
-          "server has a delay in starting and it is not in the manifest, it will not be retrieved. "
-          "Please make sure all HALs on this device are in the VINTF manifest and enable "
-          "PRODUCT_ENFORCE_VINTF_MANIFEST on this device (this is also enabled by "
-          "PRODUCT_FULL_TREBLE). PRODUCT_ENFORCE_VINTF_MANIFEST will ensure that no race condition "
-          "is possible here.");
-#endif  // ENFORCE_VINTF_MANIFEST
+    if (!kEnforceVintfManifest) {
+        ALOGE("getService: Potential race detected. The VINTF manifest is not being enforced. If "
+              "a HAL server has a delay in starting and it is not in the manifest, it will not be "
+              "retrieved. Please make sure all HALs on this device are in the VINTF manifest and "
+              "enable PRODUCT_ENFORCE_VINTF_MANIFEST on this device (this is also enabled by "
+              "PRODUCT_FULL_TREBLE). PRODUCT_ENFORCE_VINTF_MANIFEST will ensure that no race "
+              "condition is possible here.");
+    }
 
     for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
         if (waiter == nullptr && tries > 0) {
@@ -820,7 +841,7 @@
     }
 
     if (getStub || vintfPassthru || vintfLegacy) {
-        const sp<IServiceManager> pm = getPassthroughServiceManager();
+        const sp<IServiceManager1_0> pm = getPassthroughServiceManager();
         if (pm != nullptr) {
             sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr);
             if (!getStub || trebleTestingOverride) {
@@ -843,6 +864,19 @@
         return INVALID_OPERATION;
     }
 
+    const std::string descriptor = getDescriptor(service.get());
+
+    if (kEnforceVintfManifest && !isTrebleTestingOverride()) {
+        using Transport = IServiceManager1_0::Transport;
+        Transport transport = sm->getTransport(descriptor, name);
+
+        if (transport != Transport::HWBINDER) {
+            LOG(ERROR) << "Service " << descriptor << "/" << name
+                       << " must be in VINTF manifest in order to register/get.";
+            return UNKNOWN_ERROR;
+        }
+    }
+
     bool registered = false;
     Return<void> ret = service->interfaceChain([&](const auto& chain) {
         registered = sm->addWithChain(name.c_str(), service, chain).withDefault(false);
@@ -853,7 +887,7 @@
     }
 
     if (registered) {
-        onRegistrationImpl(getDescriptor(service.get()), name);
+        onRegistrationImpl(descriptor, name);
     }
 
     return registered ? OK : UNKNOWN_ERROR;
diff --git a/transport/allocator/1.0/default/Android.bp b/transport/allocator/1.0/default/Android.bp
index 1fdfb26..1116f1d 100644
--- a/transport/allocator/1.0/default/Android.bp
+++ b/transport/allocator/1.0/default/Android.bp
@@ -26,8 +26,6 @@
     shared_libs: [
         "android.hidl.allocator@1.0",
         "libhidlbase",
-        "libhidltransport",
-        "libhwbinder",
         "libbase",
         "liblog",
         "libutils",
diff --git a/transport/allocator/1.0/utils/Android.bp b/transport/allocator/1.0/utils/Android.bp
index 9f70963..b324ef1 100644
--- a/transport/allocator/1.0/utils/Android.bp
+++ b/transport/allocator/1.0/utils/Android.bp
@@ -24,7 +24,6 @@
         "libbinder",
         "libcutils",
         "libhidlbase",
-        "libhidltransport",
         "android.hidl.memory@1.0"
     ],
     export_include_dirs: ["include"],
diff --git a/transport/base/1.0/vts/functional/Android.bp b/transport/base/1.0/vts/functional/Android.bp
index fb9af33..38b03f7 100644
--- a/transport/base/1.0/vts/functional/Android.bp
+++ b/transport/base/1.0/vts/functional/Android.bp
@@ -24,8 +24,6 @@
     shared_libs: [
         "libbase",
         "libhidlbase",
-        "libhidltransport",
-        "libhwbinder",
         "liblog",
         "libutils",
         "libprotobuf-cpp-lite",
diff --git a/transport/include/hidl/ServiceManagement.h b/transport/include/hidl/ServiceManagement.h
index a962034..4573a25 100644
--- a/transport/include/hidl/ServiceManagement.h
+++ b/transport/include/hidl/ServiceManagement.h
@@ -18,6 +18,7 @@
 #define ANDROID_HARDWARE_ISERVICE_MANAGER_H
 
 #include <string>
+#include <vector>
 
 #include <android/hidl/base/1.0/IBase.h>
 #include <utils/StrongPointer.h>
@@ -71,6 +72,14 @@
 sp<::android::hidl::manager::V1_1::IServiceManager> getPassthroughServiceManager1_1();
 
 /**
+ * Given a descriptor (e.g. from IFoo::descriptor), return a list of all instance names
+ * on a device (e.g. the VINTF manifest). These HALs may not be currently running, but
+ * the expectation is that if they aren't running, they should start as lazy HALs.
+ * So, getService should return for each of these instance names.
+ */
+std::vector<std::string> getAllHalInstanceNames(const std::string& descriptor);
+
+/**
  * Given a service that is in passthrough mode, this function will go ahead and load the
  * required passthrough module library (but not call HIDL_FETCH_I* functions to instantiate it).
  *
diff --git a/transport/memory/1.0/default/Android.bp b/transport/memory/1.0/default/Android.bp
index d242ddf..f56ee95 100644
--- a/transport/memory/1.0/default/Android.bp
+++ b/transport/memory/1.0/default/Android.bp
@@ -29,12 +29,10 @@
     ],
     shared_libs: [
         "libcutils",
-        "libhwbinder",
         "libbase",
         "liblog",
         "libutils",
         "libhidlbase",
-        "libhidltransport",
         "android.hidl.memory@1.0",
     ],
 }
diff --git a/transport/token/1.0/utils/include/hidl/HybridInterface.h b/transport/token/1.0/utils/include/hidl/HybridInterface.h
index 125d5e8..5c54d85 100644
--- a/transport/token/1.0/utils/include/hidl/HybridInterface.h
+++ b/transport/token/1.0/utils/include/hidl/HybridInterface.h
@@ -389,7 +389,10 @@
         return getHalVariant().index();                                   \
     }                                                                     \
     constexpr uint32_t I##INTERFACE::sGetHalTokenTransactionCode;         \
-    const ::android::String16 I##INTERFACE::descriptor(NAME);             \
+    static const ::android::StaticString16 I##INTERFACE##_desc_str16(     \
+        u##NAME);                                                         \
+    const ::android::String16 I##INTERFACE::descriptor(                   \
+        I##INTERFACE##_desc_str16);                                       \
     const ::android::String16&                                            \
             I##INTERFACE::getInterfaceDescriptor() const {                \
         return I##INTERFACE::descriptor;                                  \