libbinder_ndk: distinguish exception/status_t

Previously here, I had erroneously used EX_* values instead of using
values corresponding to status_t.

This CL defines the set of errors that can be returned from APIs and
constrains the return set to be exactly and only those values.

Exception values are currently not used, but they will be used for
a Status-like object since they are required for communicating with
the SDK (Java). This is also why they were not removed.

Bug: 111445392
Test: ./ndk/runtests.sh
Change-Id: I24bbebae60c167d7e050ee608c24cc1ffc8a9101
diff --git a/libs/binder/ndk/Android.bp b/libs/binder/ndk/Android.bp
index e5c68b5..459b6b5 100644
--- a/libs/binder/ndk/Android.bp
+++ b/libs/binder/ndk/Android.bp
@@ -27,6 +27,7 @@
         "ibinder.cpp",
         "parcel.cpp",
         "process.cpp",
+        "status.cpp",
         "service_manager.cpp",
     ],
 
diff --git a/libs/binder/ndk/ibinder.cpp b/libs/binder/ndk/ibinder.cpp
index 8a1ec05..23d84c2 100644
--- a/libs/binder/ndk/ibinder.cpp
+++ b/libs/binder/ndk/ibinder.cpp
@@ -19,6 +19,7 @@
 
 #include <android/binder_status.h>
 #include "parcel_internal.h"
+#include "status_internal.h"
 
 #include <android-base/logging.h>
 
@@ -27,6 +28,7 @@
 using ::android::IBinder;
 using ::android::Parcel;
 using ::android::sp;
+using ::android::status_t;
 using ::android::String16;
 using ::android::wp;
 
@@ -116,17 +118,18 @@
     return getClass()->getInterfaceDescriptor();
 }
 
-binder_status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
-                                     binder_flags_t flags) {
+status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
+                              binder_flags_t flags) {
     if (isUserCommand(code)) {
         if (!data.checkInterface(this)) {
-            return EX_ILLEGAL_STATE;
+            return STATUS_PERMISSION_DENIED;
         }
 
         const AParcel in = AParcel::readOnly(this, &data);
         AParcel out = AParcel(this, reply, false /*owns*/);
 
-        return getClass()->onTransact(this, code, &in, &out);
+        binder_status_t status = getClass()->onTransact(this, code, &in, &out);
+        return PruneStatusT(status);
     } else {
         return BBinder::onTransact(code, data, reply, flags);
     }
@@ -253,13 +256,13 @@
     sp<TransferDeathRecipient> recipient =
             new TransferDeathRecipient(binder->getBinder(), cookie, mOnDied);
 
-    binder_status_t status = binder->getBinder()->linkToDeath(recipient, cookie, 0 /*flags*/);
-    if (status != EX_NONE) {
-        return status;
+    status_t status = binder->getBinder()->linkToDeath(recipient, cookie, 0 /*flags*/);
+    if (status != STATUS_OK) {
+        return PruneStatusT(status);
     }
 
     mDeathRecipients.push_back(recipient);
-    return EX_NONE;
+    return STATUS_OK;
 }
 
 binder_status_t AIBinder_DeathRecipient::unlinkToDeath(AIBinder* binder, void* cookie) {
@@ -270,22 +273,19 @@
     for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
         sp<TransferDeathRecipient> recipient = *it;
 
-        if (recipient->getCookie() == cookie &&
-
-            recipient->getWho() == binder->getBinder()) {
+        if (recipient->getCookie() == cookie && recipient->getWho() == binder->getBinder()) {
             mDeathRecipients.erase(it.base() - 1);
 
-            binder_status_t status =
-                    binder->getBinder()->unlinkToDeath(recipient, cookie, 0 /*flags*/);
-            if (status != EX_NONE) {
+            status_t status = binder->getBinder()->unlinkToDeath(recipient, cookie, 0 /*flags*/);
+            if (status != ::android::OK) {
                 LOG(ERROR) << __func__
                            << ": removed reference to death recipient but unlink failed.";
             }
-            return status;
+            return PruneStatusT(status);
         }
     }
 
-    return -ENOENT;
+    return STATUS_NAME_NOT_FOUND;
 }
 
 // start of C-API methods
@@ -323,19 +323,20 @@
 
 binder_status_t AIBinder_ping(AIBinder* binder) {
     if (binder == nullptr) {
-        return EX_NULL_POINTER;
+        return STATUS_UNEXPECTED_NULL;
     }
 
-    return binder->getBinder()->pingBinder();
+    return PruneStatusT(binder->getBinder()->pingBinder());
 }
 
 binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
                                      void* cookie) {
     if (binder == nullptr || recipient == nullptr) {
         LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
-        return EX_NULL_POINTER;
+        return STATUS_UNEXPECTED_NULL;
     }
 
+    // returns binder_status_t
     return recipient->linkToDeath(binder, cookie);
 }
 
@@ -343,9 +344,10 @@
                                        void* cookie) {
     if (binder == nullptr || recipient == nullptr) {
         LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
-        return EX_NULL_POINTER;
+        return STATUS_UNEXPECTED_NULL;
     }
 
+    // returns binder_status_t
     return recipient->unlinkToDeath(binder, cookie);
 }
 
@@ -406,14 +408,14 @@
 binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
     if (binder == nullptr || in == nullptr) {
         LOG(ERROR) << __func__ << ": requires non-null parameters.";
-        return EX_NULL_POINTER;
+        return STATUS_UNEXPECTED_NULL;
     }
     const AIBinder_Class* clazz = binder->getClass();
     if (clazz == nullptr) {
         LOG(ERROR) << __func__
                    << ": Class must be defined for a remote binder transaction. See "
                       "AIBinder_associateClass.";
-        return EX_ILLEGAL_STATE;
+        return STATUS_INVALID_OPERATION;
     }
 
     if (!binder->isRemote()) {
@@ -424,20 +426,22 @@
     }
 
     *in = new AParcel(binder);
-    binder_status_t status = (**in)->writeInterfaceToken(clazz->getInterfaceDescriptor());
-    if (status != EX_NONE) {
+    status_t status = (**in)->writeInterfaceToken(clazz->getInterfaceDescriptor());
+    binder_status_t ret = PruneStatusT(status);
+
+    if (ret != STATUS_OK) {
         delete *in;
         *in = nullptr;
     }
 
-    return status;
+    return ret;
 }
 
 binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
                                   AParcel** out, binder_flags_t flags) {
     if (in == nullptr) {
         LOG(ERROR) << __func__ << ": requires non-null in parameter";
-        return EX_NULL_POINTER;
+        return STATUS_UNEXPECTED_NULL;
     }
 
     using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
@@ -447,36 +451,37 @@
 
     if (!isUserCommand(code)) {
         LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
-        return EX_UNSUPPORTED_OPERATION;
+        return STATUS_UNKNOWN_TRANSACTION;
     }
 
     if ((flags & ~FLAG_ONEWAY) != 0) {
         LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
-        return EX_ILLEGAL_ARGUMENT;
+        return STATUS_BAD_VALUE;
     }
 
     if (binder == nullptr || *in == nullptr || out == nullptr) {
         LOG(ERROR) << __func__ << ": requires non-null parameters.";
-        return EX_NULL_POINTER;
+        return STATUS_UNEXPECTED_NULL;
     }
 
     if ((*in)->getBinder() != binder) {
         LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
                    << " but called with " << (*in)->getBinder();
-        return EX_ILLEGAL_STATE;
+        return STATUS_BAD_VALUE;
     }
 
     *out = new AParcel(binder);
 
-    binder_status_t parcelStatus =
+    status_t status =
             binder->getBinder()->transact(code, *(*in)->operator->(), (*out)->operator->(), flags);
+    binder_status_t ret = PruneStatusT(status);
 
-    if (parcelStatus != EX_NONE) {
+    if (ret != STATUS_OK) {
         delete *out;
         *out = nullptr;
     }
 
-    return parcelStatus;
+    return ret;
 }
 
 AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
diff --git a/libs/binder/ndk/ibinder_internal.h b/libs/binder/ndk/ibinder_internal.h
index fcf1b0b..5b6bc94 100644
--- a/libs/binder/ndk/ibinder_internal.h
+++ b/libs/binder/ndk/ibinder_internal.h
@@ -66,8 +66,8 @@
     ABBinder* asABBinder() override { return this; }
 
     const ::android::String16& getInterfaceDescriptor() const override;
-    binder_status_t onTransact(uint32_t code, const ::android::Parcel& data,
-                               ::android::Parcel* reply, binder_flags_t flags) override;
+    ::android::status_t onTransact(uint32_t code, const ::android::Parcel& data,
+                                   ::android::Parcel* reply, binder_flags_t flags) override;
 
 private:
     ABBinder(const AIBinder_Class* clazz, void* userData);
diff --git a/libs/binder/ndk/include_ndk/android/binder_ibinder.h b/libs/binder/ndk/include_ndk/android/binder_ibinder.h
index 7dca5a4..5081248 100644
--- a/libs/binder/ndk/include_ndk/android/binder_ibinder.h
+++ b/libs/binder/ndk/include_ndk/android/binder_ibinder.h
@@ -97,7 +97,7 @@
  *
  * If the process containing an AIBinder dies, it is possible to be holding a strong reference to
  * an object which does not exist. In this case, transactions to this binder will return
- * EX_DEAD_OBJECT. See also AIBinder_linkToDeath, AIBinder_unlinkToDeath, and AIBinder_isAlive.
+ * STATUS_DEAD_OBJECT. See also AIBinder_linkToDeath, AIBinder_unlinkToDeath, and AIBinder_isAlive.
  *
  * Once an AIBinder is created, anywhere it is passed (remotely or locally), there is a 1-1
  * correspondence between the address of an AIBinder and the object it represents. This means that
@@ -211,7 +211,7 @@
 /**
  * Stops registration for the associated binder dying. Does not delete the recipient. This function
  * may return a binder transaction failure and in case the death recipient cannot be found, it
- * returns -ENOENT.
+ * returns STATUS_NAME_NOT_FOUND.
  */
 binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
                                        void* cookie);
diff --git a/libs/binder/ndk/include_ndk/android/binder_status.h b/libs/binder/ndk/include_ndk/android/binder_status.h
index e97e181..bc8e44a 100644
--- a/libs/binder/ndk/include_ndk/android/binder_status.h
+++ b/libs/binder/ndk/include_ndk/android/binder_status.h
@@ -25,12 +25,42 @@
 
 #pragma once
 
+#include <errno.h>
 #include <stdint.h>
 #include <sys/cdefs.h>
 
 __BEGIN_DECLS
 
-// Keep the exception codes in sync with android/os/Parcel.java.
+enum {
+    STATUS_OK = 0,
+
+    STATUS_UNKNOWN_ERROR = (-2147483647 - 1), // INT32_MIN value
+    STATUS_NO_MEMORY = -ENOMEM,
+    STATUS_INVALID_OPERATION = -ENOSYS,
+    STATUS_BAD_VALUE = -EINVAL,
+    STATUS_BAD_TYPE = (STATUS_UNKNOWN_ERROR + 1),
+    STATUS_NAME_NOT_FOUND = -ENOENT,
+    STATUS_PERMISSION_DENIED = -EPERM,
+    STATUS_NO_INIT = -ENODEV,
+    STATUS_ALREADY_EXISTS = -EEXIST,
+    STATUS_DEAD_OBJECT = -EPIPE,
+    STATUS_FAILED_TRANSACTION = (STATUS_UNKNOWN_ERROR + 2),
+    STATUS_BAD_INDEX = -EOVERFLOW,
+    STATUS_NOT_ENOUGH_DATA = -ENODATA,
+    STATUS_WOULD_BLOCK = -EWOULDBLOCK,
+    STATUS_TIMED_OUT = -ETIMEDOUT,
+    STATUS_UNKNOWN_TRANSACTION = -EBADMSG,
+    STATUS_FDS_NOT_ALLOWED = (STATUS_UNKNOWN_ERROR + 7),
+    STATUS_UNEXPECTED_NULL = (STATUS_UNKNOWN_ERROR + 8),
+};
+
+/**
+ * One of the STATUS_* values.
+ *
+ * All unrecognized values are coerced into STATUS_UNKNOWN_ERROR.
+ */
+typedef int32_t binder_status_t;
+
 enum {
     EX_NONE = 0,
     EX_SECURITY = -1,
@@ -44,12 +74,6 @@
     EX_PARCELABLE = -9,
 
     /**
-     * This is special and Java specific; see Parcel.java.
-     * This should be considered a success, and the next readInt32 bytes can be ignored.
-     */
-    EX_HAS_REPLY_HEADER = -128,
-
-    /**
      * This is special, and indicates to native binder proxies that the
      * transaction has failed at a low level.
      */
@@ -57,10 +81,13 @@
 };
 
 /**
- * One of the above values or -errno.
- * By convention, positive values are considered to mean service-specific exceptions.
+ * One of the EXCEPTION_* types.
+ *
+ * All unrecognized values are coerced into EXCEPTION_TRANSACTION_FAILED.
+ *
+ * These exceptions values are used by the SDK for parcelables. Also see Parcel.java.
  */
-typedef int32_t binder_status_t;
+typedef int32_t binder_exception_t;
 
 __END_DECLS
 
diff --git a/libs/binder/ndk/parcel.cpp b/libs/binder/ndk/parcel.cpp
index ccdfc7b..a896d10 100644
--- a/libs/binder/ndk/parcel.cpp
+++ b/libs/binder/ndk/parcel.cpp
@@ -18,12 +18,14 @@
 #include "parcel_internal.h"
 
 #include "ibinder_internal.h"
+#include "status_internal.h"
 
 #include <binder/Parcel.h>
 
 using ::android::IBinder;
 using ::android::Parcel;
 using ::android::sp;
+using ::android::status_t;
 
 void AParcel_delete(AParcel** parcel) {
     if (parcel == nullptr) {
@@ -40,100 +42,118 @@
 }
 binder_status_t AParcel_readStrongBinder(const AParcel* parcel, AIBinder** binder) {
     sp<IBinder> readBinder = nullptr;
-    binder_status_t status = (*parcel)->readStrongBinder(&readBinder);
-    if (status != EX_NONE) {
-        return status;
+    status_t status = (*parcel)->readStrongBinder(&readBinder);
+    if (status != STATUS_OK) {
+        return PruneStatusT(status);
     }
     sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(readBinder);
     AIBinder_incStrong(ret.get());
     *binder = ret.get();
-    return status;
+    return PruneStatusT(status);
 }
 binder_status_t AParcel_readNullableStrongBinder(const AParcel* parcel, AIBinder** binder) {
     sp<IBinder> readBinder = nullptr;
-    binder_status_t status = (*parcel)->readNullableStrongBinder(&readBinder);
-    if (status != EX_NONE) {
-        return status;
+    status_t status = (*parcel)->readNullableStrongBinder(&readBinder);
+    if (status != STATUS_OK) {
+        return PruneStatusT(status);
     }
     sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(readBinder);
     AIBinder_incStrong(ret.get());
     *binder = ret.get();
-    return status;
+    return PruneStatusT(status);
 }
 
 // See gen_parcel_helper.py. These auto-generated read/write methods use the same types for
 // libbinder and this library.
 // @START
 binder_status_t AParcel_writeInt32(AParcel* parcel, int32_t value) {
-    return (*parcel)->writeInt32(value);
+    status_t status = (*parcel)->writeInt32(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_writeUint32(AParcel* parcel, uint32_t value) {
-    return (*parcel)->writeUint32(value);
+    status_t status = (*parcel)->writeUint32(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_writeInt64(AParcel* parcel, int64_t value) {
-    return (*parcel)->writeInt64(value);
+    status_t status = (*parcel)->writeInt64(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_writeUint64(AParcel* parcel, uint64_t value) {
-    return (*parcel)->writeUint64(value);
+    status_t status = (*parcel)->writeUint64(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_writeFloat(AParcel* parcel, float value) {
-    return (*parcel)->writeFloat(value);
+    status_t status = (*parcel)->writeFloat(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_writeDouble(AParcel* parcel, double value) {
-    return (*parcel)->writeDouble(value);
+    status_t status = (*parcel)->writeDouble(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_writeBool(AParcel* parcel, bool value) {
-    return (*parcel)->writeBool(value);
+    status_t status = (*parcel)->writeBool(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_writeChar(AParcel* parcel, char16_t value) {
-    return (*parcel)->writeChar(value);
+    status_t status = (*parcel)->writeChar(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_writeByte(AParcel* parcel, int8_t value) {
-    return (*parcel)->writeByte(value);
+    status_t status = (*parcel)->writeByte(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_readInt32(const AParcel* parcel, int32_t* value) {
-    return (*parcel)->readInt32(value);
+    status_t status = (*parcel)->readInt32(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_readUint32(const AParcel* parcel, uint32_t* value) {
-    return (*parcel)->readUint32(value);
+    status_t status = (*parcel)->readUint32(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_readInt64(const AParcel* parcel, int64_t* value) {
-    return (*parcel)->readInt64(value);
+    status_t status = (*parcel)->readInt64(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_readUint64(const AParcel* parcel, uint64_t* value) {
-    return (*parcel)->readUint64(value);
+    status_t status = (*parcel)->readUint64(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_readFloat(const AParcel* parcel, float* value) {
-    return (*parcel)->readFloat(value);
+    status_t status = (*parcel)->readFloat(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_readDouble(const AParcel* parcel, double* value) {
-    return (*parcel)->readDouble(value);
+    status_t status = (*parcel)->readDouble(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_readBool(const AParcel* parcel, bool* value) {
-    return (*parcel)->readBool(value);
+    status_t status = (*parcel)->readBool(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_readChar(const AParcel* parcel, char16_t* value) {
-    return (*parcel)->readChar(value);
+    status_t status = (*parcel)->readChar(value);
+    return PruneStatusT(status);
 }
 
 binder_status_t AParcel_readByte(const AParcel* parcel, int8_t* value) {
-    return (*parcel)->readByte(value);
+    status_t status = (*parcel)->readByte(value);
+    return PruneStatusT(status);
 }
 
 // @END
diff --git a/libs/binder/ndk/scripts/gen_parcel_helper.py b/libs/binder/ndk/scripts/gen_parcel_helper.py
index 794afe2..c4c9ad4 100755
--- a/libs/binder/ndk/scripts/gen_parcel_helper.py
+++ b/libs/binder/ndk/scripts/gen_parcel_helper.py
@@ -68,7 +68,8 @@
         header += " */\n"
         header += "binder_status_t AParcel_write" + pretty + "(AParcel* parcel, " + cpp + " value);\n\n"
         source += "binder_status_t AParcel_write" + pretty + "(AParcel* parcel, " + cpp + " value) {\n"
-        source += "    return (*parcel)->write" + pretty + "(value);\n"
+        source += "    status_t status = (*parcel)->write" + pretty + "(value);\n"
+        source += "    return PruneStatusT(status);\n"
         source += "}\n\n"
 
     for pretty, cpp in data_types:
@@ -77,7 +78,8 @@
         header += " */\n"
         header += "binder_status_t AParcel_read" + pretty + "(const AParcel* parcel, " + cpp + "* value);\n\n"
         source += "binder_status_t AParcel_read" + pretty + "(const AParcel* parcel, " + cpp + "* value) {\n"
-        source += "    return (*parcel)->read" + pretty + "(value);\n"
+        source += "    status_t status = (*parcel)->read" + pretty + "(value);\n"
+        source += "    return PruneStatusT(status);\n"
         source += "}\n\n"
 
     replaceFileTags(ROOT + "include_ndk/android/binder_parcel.h", header)
diff --git a/libs/binder/ndk/service_manager.cpp b/libs/binder/ndk/service_manager.cpp
index 5bc69b0..9ddc555 100644
--- a/libs/binder/ndk/service_manager.cpp
+++ b/libs/binder/ndk/service_manager.cpp
@@ -15,7 +15,9 @@
  */
 
 #include <android/binder_manager.h>
+
 #include "ibinder_internal.h"
+#include "status_internal.h"
 
 #include <binder/IServiceManager.h>
 
@@ -23,15 +25,17 @@
 using ::android::IBinder;
 using ::android::IServiceManager;
 using ::android::sp;
+using ::android::status_t;
 using ::android::String16;
 
 binder_status_t AServiceManager_addService(AIBinder* binder, const char* instance) {
     if (binder == nullptr || instance == nullptr) {
-        return EX_NULL_POINTER;
+        return STATUS_UNEXPECTED_NULL;
     }
 
     sp<IServiceManager> sm = defaultServiceManager();
-    return sm->addService(String16(instance), binder->getBinder());
+    status_t status = sm->addService(String16(instance), binder->getBinder());
+    return PruneStatusT(status);
 }
 AIBinder* AServiceManager_getService(const char* instance) {
     if (instance == nullptr) {
diff --git a/libs/binder/ndk/status.cpp b/libs/binder/ndk/status.cpp
new file mode 100644
index 0000000..626a9b2
--- /dev/null
+++ b/libs/binder/ndk/status.cpp
@@ -0,0 +1,105 @@
+/*
+ * 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.
+ */
+
+#include <android/binder_status.h>
+#include "status_internal.h"
+
+#include <android-base/logging.h>
+
+using ::android::status_t;
+using ::android::binder::Status;
+
+binder_status_t PruneStatusT(status_t status) {
+    if (status > 0) return status;
+
+    switch (status) {
+        case ::android::OK:
+            return STATUS_OK;
+        case ::android::NO_MEMORY:
+            return STATUS_NO_MEMORY;
+        case ::android::INVALID_OPERATION:
+            return STATUS_INVALID_OPERATION;
+        case ::android::BAD_VALUE:
+            return STATUS_BAD_VALUE;
+        case ::android::BAD_TYPE:
+            return STATUS_BAD_TYPE;
+        case ::android::NAME_NOT_FOUND:
+            return STATUS_NAME_NOT_FOUND;
+        case ::android::PERMISSION_DENIED:
+            return STATUS_PERMISSION_DENIED;
+        case ::android::NO_INIT:
+            return STATUS_NO_INIT;
+        case ::android::ALREADY_EXISTS:
+            return STATUS_ALREADY_EXISTS;
+        case ::android::DEAD_OBJECT:
+            return STATUS_DEAD_OBJECT;
+        case ::android::FAILED_TRANSACTION:
+            return STATUS_FAILED_TRANSACTION;
+        case ::android::BAD_INDEX:
+            return STATUS_BAD_INDEX;
+        case ::android::NOT_ENOUGH_DATA:
+            return STATUS_NOT_ENOUGH_DATA;
+        case ::android::WOULD_BLOCK:
+            return STATUS_WOULD_BLOCK;
+        case ::android::TIMED_OUT:
+            return STATUS_TIMED_OUT;
+        case ::android::UNKNOWN_TRANSACTION:
+            return STATUS_UNKNOWN_TRANSACTION;
+        case ::android::FDS_NOT_ALLOWED:
+            return STATUS_FDS_NOT_ALLOWED;
+        case ::android::UNEXPECTED_NULL:
+            return STATUS_UNEXPECTED_NULL;
+        case ::android::UNKNOWN_ERROR:
+            return STATUS_UNKNOWN_ERROR;
+
+        default:
+            LOG(WARNING) << __func__
+                         << ": Unknown status_t pruned into STATUS_UNKNOWN_ERROR: " << status;
+            return STATUS_UNKNOWN_ERROR;
+    }
+}
+
+binder_exception_t PruneException(int32_t exception) {
+    switch (exception) {
+        case Status::EX_NONE:
+            return EX_NONE;
+        case Status::EX_SECURITY:
+            return EX_SECURITY;
+        case Status::EX_BAD_PARCELABLE:
+            return EX_BAD_PARCELABLE;
+        case Status::EX_ILLEGAL_ARGUMENT:
+            return EX_ILLEGAL_ARGUMENT;
+        case Status::EX_NULL_POINTER:
+            return EX_NULL_POINTER;
+        case Status::EX_ILLEGAL_STATE:
+            return EX_ILLEGAL_STATE;
+        case Status::EX_NETWORK_MAIN_THREAD:
+            return EX_NETWORK_MAIN_THREAD;
+        case Status::EX_UNSUPPORTED_OPERATION:
+            return EX_UNSUPPORTED_OPERATION;
+        case Status::EX_SERVICE_SPECIFIC:
+            return EX_SERVICE_SPECIFIC;
+        case Status::EX_PARCELABLE:
+            return EX_PARCELABLE;
+        case Status::EX_TRANSACTION_FAILED:
+            return EX_TRANSACTION_FAILED;
+
+        default:
+            LOG(WARNING) << __func__
+                         << ": Unknown status_t pruned into EX_TRANSACTION_FAILED: " << exception;
+            return EX_TRANSACTION_FAILED;
+    }
+}
diff --git a/libs/binder/ndk/status_internal.h b/libs/binder/ndk/status_internal.h
new file mode 100644
index 0000000..41b124c
--- /dev/null
+++ b/libs/binder/ndk/status_internal.h
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <android/binder_status.h>
+
+#include <binder/Status.h>
+#include <utils/Errors.h>
+
+// This collapses the statuses into the declared range.
+binder_status_t PruneStatusT(android::status_t status);
+
+// This collapses the exception into the declared range.
+binder_exception_t PruneException(int32_t exception);
diff --git a/libs/binder/ndk/test/iface.cpp b/libs/binder/ndk/test/iface.cpp
index a1aa0fa..80700df 100644
--- a/libs/binder/ndk/test/iface.cpp
+++ b/libs/binder/ndk/test/iface.cpp
@@ -41,7 +41,7 @@
 
 binder_status_t IFoo_Class_onTransact(AIBinder* binder, transaction_code_t code, const AParcel* in,
                                       AParcel* out) {
-    binder_status_t stat = EX_UNSUPPORTED_OPERATION;
+    binder_status_t stat = STATUS_FAILED_TRANSACTION;
 
     sp<IFoo> foo = static_cast<IFoo_Class_Data*>(AIBinder_getUserData(binder))->foo;
     CHECK(foo != nullptr) << "Transaction made on already deleted object";
@@ -50,7 +50,7 @@
         case IFoo::DOFOO: {
             int32_t valueIn;
             stat = AParcel_readInt32(in, &valueIn);
-            if (stat != EX_NONE) break;
+            if (stat != STATUS_OK) break;
             int32_t valueOut = foo->doubleNumber(valueIn);
             stat = AParcel_writeInt32(out, valueOut);
             break;
@@ -70,16 +70,16 @@
 
     virtual int32_t doubleNumber(int32_t in) {
         AParcel* parcelIn;
-        CHECK(EX_NONE == AIBinder_prepareTransaction(mBinder, &parcelIn));
+        CHECK(STATUS_OK == AIBinder_prepareTransaction(mBinder, &parcelIn));
 
-        CHECK(EX_NONE == AParcel_writeInt32(parcelIn, in));
+        CHECK(STATUS_OK == AParcel_writeInt32(parcelIn, in));
 
         AParcel* parcelOut;
-        CHECK(EX_NONE ==
+        CHECK(STATUS_OK ==
               AIBinder_transact(mBinder, IFoo::DOFOO, &parcelIn, &parcelOut, 0 /*flags*/));
 
         int32_t out;
-        CHECK(EX_NONE == AParcel_readInt32(parcelOut, &out));
+        CHECK(STATUS_OK == AParcel_readInt32(parcelOut, &out));
 
         AParcel_delete(&parcelOut);
 
diff --git a/libs/binder/ndk/test/main_client.cpp b/libs/binder/ndk/test/main_client.cpp
index b8518d7..2dcccfe 100644
--- a/libs/binder/ndk/test/main_client.cpp
+++ b/libs/binder/ndk/test/main_client.cpp
@@ -41,7 +41,7 @@
     ASSERT_NE(nullptr, binder);
     EXPECT_TRUE(AIBinder_isRemote(binder));
     EXPECT_TRUE(AIBinder_isAlive(binder));
-    EXPECT_EQ(EX_NONE, AIBinder_ping(binder));
+    EXPECT_EQ(STATUS_OK, AIBinder_ping(binder));
 
     AIBinder_decStrong(binder);
 }
@@ -60,9 +60,11 @@
     AIBinder_DeathRecipient* recipient = AIBinder_DeathRecipient_new(OnBinderDeath);
     ASSERT_NE(nullptr, recipient);
 
-    EXPECT_EQ(EX_NONE, AIBinder_linkToDeath(binder, recipient, nullptr));
-    EXPECT_EQ(EX_NONE, AIBinder_unlinkToDeath(binder, recipient, nullptr));
-    EXPECT_EQ(-ENOENT, AIBinder_unlinkToDeath(binder, recipient, nullptr));
+    EXPECT_EQ(STATUS_OK, AIBinder_linkToDeath(binder, recipient, nullptr));
+    EXPECT_EQ(STATUS_OK, AIBinder_linkToDeath(binder, recipient, nullptr));
+    EXPECT_EQ(STATUS_OK, AIBinder_unlinkToDeath(binder, recipient, nullptr));
+    EXPECT_EQ(STATUS_OK, AIBinder_unlinkToDeath(binder, recipient, nullptr));
+    EXPECT_EQ(STATUS_NAME_NOT_FOUND, AIBinder_unlinkToDeath(binder, recipient, nullptr));
 
     AIBinder_DeathRecipient_delete(&recipient);
     AIBinder_decStrong(binder);
@@ -79,7 +81,7 @@
     static const char* kInstanceName = "test-get-service-in-process";
 
     sp<IFoo> foo = new MyTestFoo;
-    EXPECT_EQ(EX_NONE, foo->addService(kInstanceName));
+    EXPECT_EQ(STATUS_OK, foo->addService(kInstanceName));
 
     sp<IFoo> getFoo = IFoo::getService(kInstanceName);
     EXPECT_EQ(foo.get(), getFoo.get());
@@ -119,7 +121,7 @@
     static const char* kInstanceName1 = "test-multi-1";
     static const char* kInstanceName2 = "test-multi-2";
     sp<IFoo> foo = new MyTestFoo;
-    EXPECT_EQ(EX_NONE, foo->addService(kInstanceName1));
-    EXPECT_EQ(EX_NONE, foo->addService(kInstanceName2));
+    EXPECT_EQ(STATUS_OK, foo->addService(kInstanceName1));
+    EXPECT_EQ(STATUS_OK, foo->addService(kInstanceName2));
     EXPECT_EQ(IFoo::getService(kInstanceName1), IFoo::getService(kInstanceName2));
 }
diff --git a/libs/binder/ndk/test/main_server.cpp b/libs/binder/ndk/test/main_server.cpp
index f3da7da..0718a69 100644
--- a/libs/binder/ndk/test/main_server.cpp
+++ b/libs/binder/ndk/test/main_server.cpp
@@ -33,7 +33,7 @@
     // Strong reference to MyFoo kept by service manager.
     binder_status_t status = (new MyFoo)->addService(IFoo::kSomeInstanceName);
 
-    if (status != EX_NONE) {
+    if (status != STATUS_OK) {
         LOG(FATAL) << "Could not register: " << status;
     }