setDefaultImpl aborts on a second call
The actual problem is that default implementation is set globally.
setDefaultImpl might not work as expected when it is called twice with
different instances.
Because we don't have a proper solution for the problem, we prevent
calling setDefaultImpl() twice by aborting.
Bug: 140139809
Test: ./runtests.sh (in /system/tools/aidl)
Change-Id: I659d3eaad3a45dcba608fa79a08f083f84bc4d58
diff --git a/libs/binder/include/binder/IInterface.h b/libs/binder/include/binder/IInterface.h
index 79d9b79..cabfc7f 100644
--- a/libs/binder/include/binder/IInterface.h
+++ b/libs/binder/include/binder/IInterface.h
@@ -20,6 +20,8 @@
#include <binder/Binder.h>
+#include <assert.h>
+
namespace android {
// ----------------------------------------------------------------------
@@ -155,7 +157,11 @@
std::unique_ptr<I##INTERFACE> I##INTERFACE::default_impl; \
bool I##INTERFACE::setDefaultImpl(std::unique_ptr<I##INTERFACE> impl)\
{ \
- if (!I##INTERFACE::default_impl && impl) { \
+ /* Only one user of this interface can use this function */ \
+ /* at a time. This is a heuristic to detect if two different */ \
+ /* users in the same process use this function. */ \
+ assert(!I##INTERFACE::default_impl); \
+ if (impl) { \
I##INTERFACE::default_impl = std::move(impl); \
return true; \
} \