ashmemd: daemon that provides /dev/ashmem fds

Motivation: we want to replace /dev/ashmem with memfd. To do so we need
all usage of /dev/ashmem to go through libcutils. Once that happens, we
migrate libcutils to use memfd.

ashmemd is our way to enforce that apps are using the existing
ASharedMemory_create API and not bypassing it to go directly to
/dev/ashmem.

ashmemd serves opened file descriptors. The following way should be the
only way for apps to open an fd to /dev/ashmem:
app -> ASharedMemory_create -> libcutils -> ashmemd -> /dev/ashmem

Bug: 113362644
Test: ashmemd_test
Change-Id: I9068cefa950f91dba0f1b75daca23f02d933b1c8
diff --git a/Android.bp b/Android.bp
new file mode 100644
index 0000000..37699f8
--- /dev/null
+++ b/Android.bp
@@ -0,0 +1,60 @@
+// 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.
+
+aidl_interface {
+    name: "ashmemd_aidl_interface",
+    local_include_dir: "aidl",
+    srcs: [
+        "aidl/android/ashmemd/IAshmemDeviceService.aidl",
+    ],
+}
+
+cc_defaults {
+    name: "ashmemd_defaults",
+    shared_libs: [
+        "ashmemd_aidl_interface-cpp",
+        "libbase",
+        "libbinder",
+        "libutils",
+    ],
+    cflags: [
+        "-Wall",
+        "-Werror",
+    ],
+}
+
+cc_binary {
+    name: "ashmemd",
+    defaults: ["ashmemd_defaults"],
+    srcs: ["ashmemd.cpp"],
+    init_rc: ["ashmemd.rc"],
+}
+
+// This library is used to communicate with ashmemd using dlopen/dlsym. We do
+// this to avoid shared library dependecy cycles.
+cc_library {
+    name: "libashmemd_client",
+    defaults: ["ashmemd_defaults"],
+    srcs: ["ashmemd_client.cpp"],
+    cflags: [
+        "-Wexit-time-destructors",
+        "-fno-c++-static-destructors",
+    ],
+}
+
+cc_test {
+    name: "ashmemd_test",
+    defaults: ["ashmemd_defaults"],
+    srcs: ["tests/ashmemd_test.cpp"],
+}
diff --git a/OWNERS b/OWNERS
new file mode 100644
index 0000000..5f8199f
--- /dev/null
+++ b/OWNERS
@@ -0,0 +1 @@
+trong@google.com
diff --git a/aidl/android/ashmemd/IAshmemDeviceService.aidl b/aidl/android/ashmemd/IAshmemDeviceService.aidl
new file mode 100644
index 0000000..283db13
--- /dev/null
+++ b/aidl/android/ashmemd/IAshmemDeviceService.aidl
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+package android.ashmemd;
+
+/** {@hide} */
+interface IAshmemDeviceService {
+    /* Opens a file descriptor to /dev/ashmem */
+    ParcelFileDescriptor open();
+}
diff --git a/ashmemd.cpp b/ashmemd.cpp
new file mode 100644
index 0000000..9d12ed3
--- /dev/null
+++ b/ashmemd.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2019 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-base/unique_fd.h>
+#include <binder/BinderService.h>
+#include <binder/IPCThreadState.h>
+#include <binder/IServiceManager.h>
+#include <binder/ProcessState.h>
+#include <binder/Status.h>
+#include <utils/String16.h>
+
+#include <android/ashmemd/BnAshmemDeviceService.h>
+
+using android::String16;
+using android::base::unique_fd;
+
+namespace android {
+namespace ashmemd {
+
+class AshmemDeviceService : public BnAshmemDeviceService {
+  public:
+    binder::Status open(os::ParcelFileDescriptor* ashmemFd) override {
+        ashmemFd->reset(unique_fd(TEMP_FAILURE_RETRY(::open("/dev/ashmem", O_RDWR | O_CLOEXEC))));
+        return binder::Status::ok();
+    }
+};
+
+void CreateAndRegisterService() {
+    sp<AshmemDeviceService> ashmemService = new AshmemDeviceService();
+    defaultServiceManager()->addService(String16("ashmem_device_service"), ashmemService,
+                                        true /* allowIsolated */);
+}
+
+void JoinThreadPool() {
+    sp<ProcessState> ps = ProcessState::self();
+    IPCThreadState::self()->joinThreadPool();  // should not return
+}
+
+}  // namespace ashmemd
+}  // namespace android
+
+int main() {
+    android::ashmemd::CreateAndRegisterService();
+    android::ashmemd::JoinThreadPool();
+    std::abort();  // unreachable
+}
diff --git a/ashmemd.rc b/ashmemd.rc
new file mode 100644
index 0000000..ee16d95
--- /dev/null
+++ b/ashmemd.rc
@@ -0,0 +1,4 @@
+service ashmemd /system/bin/ashmemd
+    class core
+    user nobody
+    group nobody
diff --git a/ashmemd_client.cpp b/ashmemd_client.cpp
new file mode 100644
index 0000000..1984358
--- /dev/null
+++ b/ashmemd_client.cpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2019 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/ashmemd/IAshmemDeviceService.h>
+#include <binder/IServiceManager.h>
+
+using android::IBinder;
+using android::IServiceManager;
+using android::String16;
+using android::ashmemd::IAshmemDeviceService;
+using android::os::ParcelFileDescriptor;
+
+namespace android {
+namespace ashmemd {
+
+sp<IAshmemDeviceService> getAshmemService() {
+    sp<IServiceManager> sm = android::defaultServiceManager();
+    sp<IBinder> binder = sm->getService(String16("ashmem_device_service"));
+    return interface_cast<IAshmemDeviceService>(binder);
+}
+
+extern "C" int openAshmemdFd() {
+    static sp<IAshmemDeviceService> ashmemService = getAshmemService();
+    if (!ashmemService) return -1;
+
+    ParcelFileDescriptor fd;
+    auto status = ashmemService->open(&fd);
+    if (!status.isOk()) return -1;
+
+    // unique_fd is the underlying type of ParcelFileDescriptor, i.e. fd is
+    // closed when it falls out of scope, so we make a dup.
+    return dup(fd.get());
+}
+
+}  // namespace ashmemd
+}  // namespace android
diff --git a/tests/ashmemd_test.cpp b/tests/ashmemd_test.cpp
new file mode 100644
index 0000000..ed3cfe3
--- /dev/null
+++ b/tests/ashmemd_test.cpp
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2019 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 <dlfcn.h>
+
+#include <binder/IServiceManager.h>
+#include <gtest/gtest.h>
+#include <linux/ashmem.h>
+#include <sys/mman.h>
+
+#include <android/ashmemd/IAshmemDeviceService.h>
+
+using android::IBinder;
+using android::IServiceManager;
+using android::String16;
+using android::ashmemd::IAshmemDeviceService;
+using android::os::ParcelFileDescriptor;
+
+namespace android {
+namespace ashmemd {
+
+class AshmemdTest : public ::testing::Test {
+  public:
+    virtual void SetUp() override {
+        sp<IServiceManager> sm = android::defaultServiceManager();
+        sp<IBinder> binder = sm->getService(String16("ashmem_device_service"));
+        ASSERT_NE(binder, nullptr);
+
+        ashmemService = android::interface_cast<IAshmemDeviceService>(binder);
+        ASSERT_NE(ashmemService, nullptr);
+    }
+
+    void openFd(ParcelFileDescriptor* fd) {
+        auto status = ashmemService->open(fd);
+        ASSERT_TRUE(status.isOk());
+        ASSERT_GE(fd->get(), 0);
+    }
+
+    sp<IAshmemDeviceService> ashmemService;
+};
+
+TEST_F(AshmemdTest, OpenFd) {
+    ParcelFileDescriptor fd;
+    openFd(&fd);
+}
+
+TEST_F(AshmemdTest, OpenMultipleFds) {
+    ParcelFileDescriptor fd1;
+    ParcelFileDescriptor fd2;
+    openFd(&fd1);
+    openFd(&fd2);
+    ASSERT_NE(fd1.get(), fd2.get());
+}
+
+TEST_F(AshmemdTest, MmapFd) {
+    ParcelFileDescriptor pfd;
+    openFd(&pfd);
+    int fd = pfd.get();
+    size_t testSize = 2097152;
+
+    ASSERT_EQ(ioctl(fd, ASHMEM_SET_NAME, "AshmemdTest"), 0);
+    ASSERT_EQ(ioctl(fd, ASHMEM_SET_SIZE, testSize), 0);
+
+    void* data = mmap(NULL, testSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+    ASSERT_NE(data, MAP_FAILED) << "Failed to mmap() ashmem fd";
+    ASSERT_EQ(munmap(data, testSize), 0) << "Failed to munmap() ashmem fd";
+}
+
+TEST(LibAshmemdClientTest, OpenFd) {
+    void* handle = dlopen("libashmemd_client.so", RTLD_NOW);
+    ASSERT_NE(handle, nullptr) << "Failed to dlopen() libashmemd_client.so: " << dlerror();
+
+    auto function = (int (*)())dlsym(handle, "openAshmemdFd");
+    ASSERT_NE(function, nullptr) << "Failed to dlsym() openAshmemdFd() function: " << dlerror();
+
+    int fd = function();
+    ASSERT_GE(fd, 0) << "Failed to open /dev/ashmem";
+}
+
+}  // namespace ashmemd
+}  // namespace android