libhidl: export cutils headers am: 686f611ebb am: 912b0d6a88
am: 1cd8ee3242

Change-Id: Ic37af1b04b17b9fcbd0bdbbcc380f3dfe91b04db
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index 7cc8ef5..8223599 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -99,6 +99,7 @@
 }
 
 void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) {
+    freeHandle();
     mHandle = handle;
     mOwnsHandle = shouldOwn;
 }
diff --git a/manifest.xml b/manifest.xml
index c258bce..693bcbe 100644
--- a/manifest.xml
+++ b/manifest.xml
@@ -35,4 +35,31 @@
             <instance>default</instance>
         </interface>
     </hal>
+    <hal>
+        <name>android.frameworks.schedulerservice</name>
+        <transport>hwbinder</transport>
+        <version>1.0</version>
+        <interface>
+            <name>ISchedulingPolicyService</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
+    <hal>
+        <name>android.frameworks.sensorservice</name>
+        <transport>hwbinder</transport>
+        <version>1.0</version>
+        <interface>
+            <name>ISensorManager</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
+    <hal>
+        <name>android.system.wifi.keystore</name>
+        <transport>hwbinder</transport>
+        <version>1.0</version>
+        <interface>
+            <name>IKeystore</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
 </manifest>
diff --git a/transport/HidlBinderSupport.cpp b/transport/HidlBinderSupport.cpp
index c0601ca..f421953 100644
--- a/transport/HidlBinderSupport.cpp
+++ b/transport/HidlBinderSupport.cpp
@@ -33,7 +33,7 @@
 static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset");
 static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset");
 
-status_t readEmbeddedFromParcel(hidl_memory * /* memory */,
+status_t readEmbeddedFromParcel(const hidl_memory& memory,
         const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
     const native_handle_t *handle;
     ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
@@ -43,7 +43,7 @@
 
     if (_hidl_err == ::android::OK) {
         _hidl_err = readEmbeddedFromParcel(
-                (hidl_string*) nullptr,
+                memory.name(),
                 parcel,
                 parentHandle,
                 parentOffset + hidl_memory::kOffsetOfName);
@@ -73,14 +73,28 @@
 const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
 static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset");
 
-status_t readEmbeddedFromParcel(hidl_string * /* string */,
+status_t readEmbeddedFromParcel(const hidl_string &string ,
         const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
     const void *out;
-    return parcel.readEmbeddedBuffer(
+
+    status_t status = parcel.readEmbeddedBuffer(
+            string.size() + 1,
             nullptr /* buffer_handle */,
             parentHandle,
             parentOffset + hidl_string::kOffsetOfBuffer,
             &out);
+
+    if (status != OK) {
+        return status;
+    }
+
+    // Always safe to access out[string.size()] because we read size+1 bytes
+    if (static_cast<const char *>(out)[string.size()] != '\0') {
+        ALOGE("Received unterminated hidl_string buffer.");
+        return BAD_VALUE;
+    }
+
+    return OK;
 }
 
 status_t writeEmbeddedToParcel(const hidl_string &string,
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index 0e56d18..9c692da 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -16,6 +16,7 @@
 
 #define LOG_TAG "ServiceManagement"
 
+#include <android/dlext.h>
 #include <condition_variable>
 #include <dlfcn.h>
 #include <dirent.h>
@@ -43,6 +44,10 @@
 #define RE_PATH         RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
 static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
 
+extern "C" {
+    android_namespace_t* android_get_exported_namespace(const char*);
+}
+
 using android::base::WaitForProperty;
 
 using android::hidl::manager::V1_0::IServiceManager;
@@ -227,6 +232,7 @@
         const std::string prefix = packageAndVersion + "-impl";
         const std::string sym = "HIDL_FETCH_" + ifaceName;
 
+        const android_namespace_t* sphal_namespace = android_get_exported_namespace("sphal");
         const int dlMode = RTLD_LAZY;
         void *handle = nullptr;
 
@@ -243,7 +249,29 @@
             for (const std::string &lib : libs) {
                 const std::string fullPath = path + lib;
 
-                handle = dlopen(fullPath.c_str(), dlMode);
+                // If sphal namespace is available, try to load from the
+                // namespace first. If it fails, fall back to the original
+                // dlopen, which loads from the current namespace.
+                if (sphal_namespace != nullptr && path != HAL_LIBRARY_PATH_SYSTEM) {
+                    const android_dlextinfo dlextinfo = {
+                        .flags = ANDROID_DLEXT_USE_NAMESPACE,
+                        // const_cast is dirty but required because
+                        // library_namespace field is non-const.
+                        .library_namespace = const_cast<android_namespace_t*>(sphal_namespace),
+                    };
+                    handle = android_dlopen_ext(fullPath.c_str(), dlMode, &dlextinfo);
+                    if (handle == nullptr) {
+                        const char* error = dlerror();
+                        LOG(WARNING) << "Failed to dlopen " << lib << " from sphal namespace:"
+                                     << (error == nullptr ? "unknown error" : error);
+                    } else {
+                        LOG(DEBUG) << lib << " loaded from sphal namespace.";
+                    }
+                }
+                if (handle == nullptr) {
+                    handle = dlopen(fullPath.c_str(), dlMode);
+                }
+
                 if (handle == nullptr) {
                     const char* error = dlerror();
                     LOG(ERROR) << "Failed to dlopen " << lib << ": "
diff --git a/transport/current.txt b/transport/current.txt
new file mode 100644
index 0000000..9b41870
--- /dev/null
+++ b/transport/current.txt
@@ -0,0 +1,13 @@
+# Do not change this file except to add new interfaces. Changing
+# pre-existing interfaces will fail VTS and break framework-only OTAs
+
+# HALs released in Android O
+
+43a52a3777b4d6ec1bb0f5c371bbcecc51898e76695834e0971fef424659f66a android.hidl.allocator@1.0::IAllocator
+bddab6184d7a346da6a07dc0828cf19a696f4caa3611c51f2e14565a14b40fd9 android.hidl.base@1.0::IBase
+500ec34f1b0826a93c4abe45b23c4d85565d8041acaf3cf9fb23c09702967567 android.hidl.base@1.0::types
+234feb5f59498c482639151de0b4bdfa0e3af56f2eb581050cbc5d3557cc88ec android.hidl.manager@1.0::IServiceManager
+50552b700ef67c7ed8c8d776d323f8c629a9b43b965e10a916a66f3c946c50fb android.hidl.manager@1.0::IServiceNotification
+2b885b5dec97391c82f35e64180686dc4c8f78b2b0a01732f8536385654f27c8 android.hidl.memory@1.0::IMapper
+4632246017013e75536fa6ee47db286b24a323fb92c37c6b14bb0ab796b7a16b android.hidl.memory@1.0::IMemory
+7c9fe352af04af659bd51ab6f5495115575bc063ddf684fc6d0dec1f4a4b4b7c android.hidl.token@1.0::ITokenManager
diff --git a/transport/include/hidl/HidlBinderSupport.h b/transport/include/hidl/HidlBinderSupport.h
index 86ac74c..302e289 100644
--- a/transport/include/hidl/HidlBinderSupport.h
+++ b/transport/include/hidl/HidlBinderSupport.h
@@ -60,7 +60,7 @@
 
 // ---------------------- hidl_memory
 
-status_t readEmbeddedFromParcel(hidl_memory *memory,
+status_t readEmbeddedFromParcel(const hidl_memory &memory,
         const Parcel &parcel, size_t parentHandle, size_t parentOffset);
 
 status_t writeEmbeddedToParcel(const hidl_memory &memory,
@@ -68,7 +68,7 @@
 
 // ---------------------- hidl_string
 
-status_t readEmbeddedFromParcel(hidl_string *string,
+status_t readEmbeddedFromParcel(const hidl_string &string,
         const Parcel &parcel, size_t parentHandle, size_t parentOffset);
 
 status_t writeEmbeddedToParcel(const hidl_string &string,
@@ -92,13 +92,14 @@
 
 template<typename T>
 status_t readEmbeddedFromParcel(
-        hidl_vec<T> * /*vec*/,
+        const hidl_vec<T> &vec,
         const Parcel &parcel,
         size_t parentHandle,
         size_t parentOffset,
         size_t *handle) {
     const void *out;
     return parcel.readNullableEmbeddedBuffer(
+            vec.size() * sizeof(T),
             handle,
             parentHandle,
             parentOffset + hidl_vec<T>::kOffsetOfBuffer,
@@ -129,7 +130,7 @@
 
 template<typename T, MQFlavor flavor>
 ::android::status_t readEmbeddedFromParcel(
-        MQDescriptor<T, flavor> *obj,
+        MQDescriptor<T, flavor> &obj,
         const ::android::hardware::Parcel &parcel,
         size_t parentHandle,
         size_t parentOffset) {
@@ -138,7 +139,7 @@
     size_t _hidl_grantors_child;
 
     _hidl_err = ::android::hardware::readEmbeddedFromParcel(
-                &obj->grantors(),
+                obj.grantors(),
                 parcel,
                 parentHandle,
                 parentOffset + MQDescriptor<T, flavor>::kOffsetOfGrantors,