Move g(Bn|Bs)ConstructorMap's into cpp.
These were originally used to store IFoo::descriptor ->
constructor maps for HIDL code in order to create the
appropriate objects when receiving a binder in HIDL,
but due to static linking of these libraries in recovery
mode, they were moved into functions which create the
static variables and return them (getBnConstructorMap
and getBsConstructorMap). However, old prebuilts still
reference these old variables. In order to remove them,
prebuilt rebuilds need to not reference them, so here,
I have removed all references to them in headers.
Bug: 69122224
Bug: 69408124 # in preparation for this bug
Test: boot Pixel 2, hidl_test
Change-Id: I681d4895f7784bc080e01f4fae2be318ba8d6c0f
diff --git a/transport/HidlBinderSupport.cpp b/transport/HidlBinderSupport.cpp
index f6c7bff..015d607 100644
--- a/transport/HidlBinderSupport.cpp
+++ b/transport/HidlBinderSupport.cpp
@@ -18,6 +18,8 @@
#include <hidl/HidlBinderSupport.h>
+#include <InternalStatic.h> // TODO(b/69122224): remove this include, for getOrCreateCachedBinder
+
// C includes
#include <unistd.h>
@@ -196,6 +198,42 @@
return status;
}
+sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr) {
+ LOG_ALWAYS_FATAL_IF(ifacePtr->isRemote(),
+ "getOrCreateCachedBinder does not have a way to construct remote binders");
+
+ std::string descriptor = details::getDescriptor(ifacePtr);
+ if (descriptor.empty()) {
+ // interfaceDescriptor fails
+ return nullptr;
+ }
+
+ // for get + set
+ std::unique_lock<std::mutex> _lock = details::gBnMap.lock();
+
+ wp<BHwBinder> wBnObj = details::gBnMap.getLocked(ifacePtr, nullptr);
+ sp<IBinder> sBnObj = wBnObj.promote();
+
+ if (sBnObj == nullptr) {
+ auto func = details::getBnConstructorMap().get(descriptor, nullptr);
+ if (!func) {
+ // TODO(b/69122224): remove this static variable when prebuilts updated
+ func = details::gBnConstructorMap.get(descriptor, nullptr);
+ if (!func) {
+ return nullptr;
+ }
+ }
+
+ sBnObj = sp<IBinder>(func(static_cast<void*>(ifacePtr)));
+
+ if (sBnObj != nullptr) {
+ details::gBnMap.setLocked(ifacePtr, static_cast<BHwBinder*>(sBnObj.get()));
+ }
+ }
+
+ return sBnObj;
+}
+
static bool gThreadPoolConfigured = false;
void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
diff --git a/transport/HidlPassthroughSupport.cpp b/transport/HidlPassthroughSupport.cpp
index e5eb164..ff68a1e 100644
--- a/transport/HidlPassthroughSupport.cpp
+++ b/transport/HidlPassthroughSupport.cpp
@@ -16,6 +16,8 @@
#include <hidl/HidlPassthroughSupport.h>
+#include <InternalStatic.h> // TODO(b/69122224): remove this include, for tryWrap
+
#include <hidl/HidlTransportUtils.h>
#include <hidl/Static.h>
@@ -28,6 +30,7 @@
static sp<IBase> tryWrap(const std::string& descriptor, sp<IBase> iface) {
auto func = getBsConstructorMap().get(descriptor, nullptr);
if (!func) {
+ // TODO(b/69122224): remove this when prebuilts don't reference it
func = gBsConstructorMap.get(descriptor, nullptr);
}
if (func) {
diff --git a/transport/InternalStatic.h b/transport/InternalStatic.h
new file mode 100644
index 0000000..b0fefa9
--- /dev/null
+++ b/transport/InternalStatic.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+// This file is for legacy static variables that we are trying to get rid of.
+// TODO(b/69122224): remove this file
+
+#ifndef ANDROID_HARDWARE_HIDL_INTERNAL_STATIC_H
+#define ANDROID_HARDWARE_HIDL_INTERNAL_STATIC_H
+
+#include <hidl/Static.h>
+
+namespace android {
+namespace hardware {
+namespace details {
+
+// deprecated; use getBnConstructorMap instead.
+extern BnConstructorMap gBnConstructorMap;
+// deprecated; use getBsConstructorMap instead.
+extern BsConstructorMap gBsConstructorMap;
+
+} // namespace details
+} // namespace hardware
+} // namespace android
+
+#endif
diff --git a/transport/Static.cpp b/transport/Static.cpp
index 9875311..45ae27c 100644
--- a/transport/Static.cpp
+++ b/transport/Static.cpp
@@ -17,6 +17,7 @@
// All static variables go here, to control initialization and
// destruction order in the library.
+#include <InternalStatic.h>
#include <hidl/Static.h>
#include <android/hidl/manager/1.0/IServiceManager.h>
diff --git a/transport/include/hidl/HidlBinderSupport.h b/transport/include/hidl/HidlBinderSupport.h
index 1545946..6d45f39 100644
--- a/transport/include/hidl/HidlBinderSupport.h
+++ b/transport/include/hidl/HidlBinderSupport.h
@@ -301,6 +301,10 @@
// ---------------------- support for casting interfaces
+// Constructs a binder for this interface and caches it. If it has already been created
+// then it returns it. ifacePtr must be a local object.
+sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr);
+
// Construct a smallest possible binder from the given interface.
// If it is remote, then its remote() will be retrieved.
// Otherwise, the smallest possible BnChild is found where IChild is a subclass of IType
@@ -317,35 +321,7 @@
return ::android::hardware::IInterface::asBinder(
static_cast<BpInterface<IType>*>(ifacePtr));
} else {
- std::string myDescriptor = details::getDescriptor(ifacePtr);
- if (myDescriptor.empty()) {
- // interfaceDescriptor fails
- return nullptr;
- }
-
- // for get + set
- std::unique_lock<std::mutex> _lock = details::gBnMap.lock();
-
- wp<BHwBinder> wBnObj = details::gBnMap.getLocked(ifacePtr, nullptr);
- sp<IBinder> sBnObj = wBnObj.promote();
-
- if (sBnObj == nullptr) {
- auto func = details::getBnConstructorMap().get(myDescriptor, nullptr);
- if (!func) {
- func = details::gBnConstructorMap.get(myDescriptor, nullptr);
- if (!func) {
- return nullptr;
- }
- }
-
- sBnObj = sp<IBinder>(func(static_cast<void*>(ifacePtr)));
-
- if (sBnObj != nullptr) {
- details::gBnMap.setLocked(ifacePtr, static_cast<BHwBinder*>(sBnObj.get()));
- }
- }
-
- return sBnObj;
+ return getOrCreateCachedBinder(ifacePtr);
}
}
diff --git a/transport/include/hidl/Static.h b/transport/include/hidl/Static.h
index 6ff6a9f..b50c173 100644
--- a/transport/include/hidl/Static.h
+++ b/transport/include/hidl/Static.h
@@ -47,8 +47,6 @@
// For HidlBinderSupport and autogenerated code
// value function receives reinterpret_cast<void *>(static_cast<IFoo *>(foo)),
// returns sp<IBinder>
-// deprecated; use getBnConstructorMap instead.
-extern BnConstructorMap gBnConstructorMap;
BnConstructorMap& getBnConstructorMap();
using BsConstructorMap = ConcurrentMap<std::string,
@@ -56,8 +54,6 @@
// For HidlPassthroughSupport and autogenerated code
// value function receives reinterpret_cast<void *>(static_cast<IFoo *>(foo)),
// returns sp<IBase>
-// deprecated; use getBsConstructorMap instead.
-extern BsConstructorMap gBsConstructorMap;
BsConstructorMap& getBsConstructorMap();
} // namespace details
} // namespace hardware