Add worksource support to binder calls.

Unlike android.os.WorkSource, we only support the leaf UID of the work
chain here for performance reasons.

We use a similar mechanism to how we propagate the StrictMode flags. The
enforceInterface and writeInterfaceToken are used to pass the worksource
through the parcel. enforceInterface/writeInterfaceToken is at least
used by all the services using AIDL.

It might break code which is reading/writing raw parcels. For instance,
a client calling an AIDL service and replicating what enforceInterface
is doing, e.g. service_manager.c. This is the only client which seems to
do that.

Here is an example of how this API is supposed to be used:
Binder.setThreadWorkSource(uid);
try {
  // Call an API.
} finally {
  Binder.clearThreadWorkSource();
}

An alternative would be to clear the work source automatically on behalf
of the caller. Manually setting the work source has been chosen to give
more control to callers of the API:
- they might choose to call setThreadWorkSource once and do multiple binder calls
- callers can also decide whether they want to restore the work source
to the orginal
value.

We do not throw an exception when #setThreadWorkSource overrides an
existing work source. It would be really hard to figure when it is safe
to call #setThreadWorkSource. For instance, adding a binder transaction
might make some code down the line that calls #setThreadWorkSource throw
an exception (because the binder transaction would set the work source
on the thread).

Ran the binder performance test suite
(test/vts-testcase/performance/binder_benchmark/binder_performance_test/).

There is no difference in terms of cpu/real time:
- without patch
{
  "name": "BM_sendVec_binder/4",
  "iterations": 43088,
  "real_time": 3.0151086752702871e+04, // yes, time is higher without patch
  "cpu_time": 1.4715999326958785e+04,
  "time_unit": "ns"
{
  "name": "BM_sendVec_binder/8",
  "iterations": 48335,
  "real_time": 2.8371138740033064e+04,
  "cpu_time": 1.3710703486086690e+04,
  "time_unit": "ns"
},

- with patch
{
  "name": "BM_sendVec_binder/4",
  "iterations": 48214,
  "real_time": 2.8613625191853276e+04,
  "cpu_time": 1.4305279856473226e+04,
  "time_unit": "ns"
},
{
  "name": "BM_sendVec_binder/8",
  "iterations": 49998,
  "real_time": 2.8749066382655215e+04,
  "cpu_time": 1.4422315992639707e+04,
  "time_unit": "ns"
},


Test: unit tests and manually tested on my phone
Change-Id: I167abbbfa6e7c4b0933c4cafa04df810f4814e2b
diff --git a/libs/binder/IPCThreadState.cpp b/libs/binder/IPCThreadState.cpp
index f052bcb..7a47724 100644
--- a/libs/binder/IPCThreadState.cpp
+++ b/libs/binder/IPCThreadState.cpp
@@ -109,6 +109,10 @@
     "BC_DEAD_BINDER_DONE"
 };
 
+// The work source represents the UID of the process we should attribute the transaction to.
+// We use -1 to specify that the work source was not set using #setWorkSource.
+static const int kUnsetWorkSource = -1;
+
 static const char* getReturnString(uint32_t cmd)
 {
     size_t idx = cmd & 0xff;
@@ -383,6 +387,25 @@
     return mStrictModePolicy;
 }
 
+uid_t IPCThreadState::setWorkSource(uid_t uid)
+{
+    uid_t returnValue = mWorkSource;
+    mWorkSource = uid;
+    return returnValue;
+}
+
+uid_t IPCThreadState::getWorkSource() const
+{
+    return mWorkSource;
+}
+
+uid_t IPCThreadState::clearWorkSource()
+{
+    uid_t returnValue = mWorkSource;
+    mWorkSource = kUnsetWorkSource;
+    return returnValue;
+}
+
 void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
 {
     mLastTransactionBinderFlags = flags;
@@ -736,6 +759,7 @@
 
 IPCThreadState::IPCThreadState()
     : mProcess(ProcessState::self()),
+      mWorkSource(kUnsetWorkSource),
       mStrictModePolicy(0),
       mLastTransactionBinderFlags(0)
 {