Remove dep to libbinderthreadstate.

Instead of having this library, libbinder/libhwbinder can keep track of
stack pointers so that when they recurse, we know which one was visited
most recently.

As with the original implementation of libbinderthreadstate, this is
somewhat of a hack. An explanation of why this is and what to do instead
is added in CallerUtils.h.

Bug: 148692216
Test: boot
Change-Id: I6cb780099229e1fce55c1f767b81822c06a605c1
Merged-In: I6cb780099229e1fce55c1f767b81822c06a605c1
diff --git a/Android.bp b/Android.bp
index 2d04396..a4bfd33 100644
--- a/Android.bp
+++ b/Android.bp
@@ -47,14 +47,6 @@
 
 cc_defaults {
     name: "libhwbinder-impl-shared-libs",
-    defaults: ["libhwbinder-impl-shared-libs-no-vndk-private"],
-    shared_libs: [
-        "libbinderthreadstate",
-    ],
-}
-
-cc_defaults {
-    name: "libhwbinder-impl-shared-libs-no-vndk-private",
     shared_libs: [
         "libbase",
         "liblog",
@@ -80,10 +72,6 @@
 cc_library_static {
     name: "libhwbinder-impl-internal",
     include_dirs: [
-        // TODO(b/135299443): allow this library to link against vndk-private libs
-        // and instead rely on the fact that users of this static library must be
-        // vndk (since they must use libbinderthreadstate).
-        "frameworks/native/libs/binderthreadstate/include/",
         // TODO(b/31559095): get headers from bionic on host
         "bionic/libc/kernel/android/uapi/",
         "bionic/libc/kernel/uapi/",
@@ -91,7 +79,7 @@
 
     defaults: [
         "libhwbinder_defaults",
-        "libhwbinder-impl-shared-libs-no-vndk-private",
+        "libhwbinder-impl-shared-libs",
         "hwbinder_pgo",
         "hwbinder_lto",
     ],
diff --git a/IPCThreadState.cpp b/IPCThreadState.cpp
index 47486ca..4baa5cb 100644
--- a/IPCThreadState.cpp
+++ b/IPCThreadState.cpp
@@ -17,7 +17,6 @@
 #define LOG_TAG "hw-IPCThreadState"
 
 #include <hwbinder/IPCThreadState.h>
-#include <binderthreadstate/IPCThreadStateBase.h>
 
 #include <hwbinder/Binder.h>
 #include <hwbinder/BpHwBinder.h>
@@ -771,6 +770,7 @@
 
 IPCThreadState::IPCThreadState()
     : mProcess(ProcessState::self()),
+      mServingStackPointer(nullptr),
       mStrictModePolicy(0),
       mLastTransactionBinderFlags(0),
       mIsLooper(false),
@@ -780,8 +780,6 @@
     clearCaller();
     mIn.setDataCapacity(256);
     mOut.setDataCapacity(256);
-
-    mIPCThreadStateBase = IPCThreadStateBase::self();
 }
 
 IPCThreadState::~IPCThreadState()
@@ -1143,9 +1141,6 @@
                 "Not enough command data for brTRANSACTION");
             if (result != NO_ERROR) break;
 
-            // Record the fact that we're in a hwbinder call
-            mIPCThreadStateBase->pushCurrentState(
-                IPCThreadStateBase::CallState::HWBINDER);
             Parcel buffer;
             buffer.ipcSetDataReference(
                 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
@@ -1153,6 +1148,9 @@
                 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
                 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
 
+            const void* origServingStackPointer = mServingStackPointer;
+            mServingStackPointer = &origServingStackPointer; // anything on the stack
+
             const pid_t origPid = mCallingPid;
             const char* origSid = mCallingSid;
             const uid_t origUid = mCallingUid;
@@ -1212,7 +1210,6 @@
                 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags, reply_callback);
             }
 
-            mIPCThreadStateBase->popCurrentState();
             if ((tr.flags & TF_ONE_WAY) == 0) {
                 if (!reply_sent) {
                     // Should have been a reply but there wasn't, so there
@@ -1233,6 +1230,7 @@
             //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n",
             //     mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid);
 
+            mServingStackPointer = origServingStackPointer;
             mCallingPid = origPid;
             mCallingSid = origSid;
             mCallingUid = origUid;
@@ -1285,9 +1283,8 @@
     return result;
 }
 
-bool IPCThreadState::isServingCall() const
-{
-    return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::HWBINDER;
+const void* IPCThreadState::getServingStackPointer() const {
+    return mServingStackPointer;
 }
 
 void IPCThreadState::threadDestructor(void *st)
diff --git a/TEST_MAPPING b/TEST_MAPPING
new file mode 100644
index 0000000..2bd0463
--- /dev/null
+++ b/TEST_MAPPING
@@ -0,0 +1,7 @@
+{
+  "presubmit": [
+    {
+      "name": "libbinderthreadstateutils_test"
+    }
+  ]
+}
diff --git a/include/hwbinder/IPCThreadState.h b/include/hwbinder/IPCThreadState.h
index 759aef3..ca99591 100644
--- a/include/hwbinder/IPCThreadState.h
+++ b/include/hwbinder/IPCThreadState.h
@@ -31,8 +31,6 @@
 // ---------------------------------------------------------------------------
 namespace android {
 
-class IPCThreadStateBase;
-
 namespace hardware {
 
 class IPCThreadState
@@ -99,31 +97,12 @@
             bool                isLooperThread();
             bool                isOnlyBinderThread();
 
-            // Is this thread currently serving a hwbinder call. This method
-            // returns true if while traversing backwards from the function call
-            // stack for this thread, we encounter a function serving a hwbinder
-            // call before encountering a binder call / hitting the end of the
-            // call stack.
-            // Eg: If thread T1 went through the following call pattern
-            //     1) T1 receives and executes binder call B1.
-            //     2) While handling B1, T1 makes hwbinder call H1.
-            //     3) The handler of H1, calls into T1 with a callback H2.
-            // If isServingCall() is called during B1 before 3), this method
-            // will return false, else true.
+            // WARNING: DO NOT USE THIS API
             //
-            //  ----
-            // | H2 | ---> While callback H2 is being handled during 3).
-            //  ----
-            // | B1 | ---> While B1 is being handled, hwbinder call H1 made.
-            //  ----
-            // Fig: Thread Call stack while handling H2.
-            //
-            // This is since after 3), while traversing the thread call stack,
-            // we hit a hwbinder call before a binder call / end of stack.
-            // This method may be typically used to determine whether to use
-            // hardware::IPCThreadState methods or IPCThreadState methods to
-            // infer information about thread state.
-            bool isServingCall() const;
+            // Returns a pointer to the stack from the last time a transaction
+            // was initiated by the kernel. Used to compare when making nested
+            // calls between multiple different transports.
+            const void*         getServingStackPointer() const;
 
             // Tasks which are done on the binder thread after the thread returns to the
             // threadpool.
@@ -164,6 +143,7 @@
             Parcel              mIn;
             Parcel              mOut;
             status_t            mLastError;
+            const void*         mServingStackPointer;
             pid_t               mCallingPid;
             const char*         mCallingSid;
             uid_t               mCallingUid;
@@ -173,7 +153,6 @@
             bool mIsPollingThread;
 
             std::vector<std::function<void(void)>> mPostCommandTasks;
-            IPCThreadStateBase *mIPCThreadStateBase;
 
             ProcessState::CallRestriction mCallRestriction;
 };