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/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