From 5589ecd29fc7d38507c715feedf445179cf0f937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E5=AE=AB=E9=9B=AA=E7=8F=8A?= Date: Wed, 3 Nov 2021 10:18:57 +0800 Subject: [PATCH] Revert "[zygisk] Use ASharedMemory (#1350)" (#1355) This reverts commit 449b48e77f2306bf3c3772c865ff79df24c81ba5. --- core/src/main/cpp/main/Android.mk | 2 +- core/src/main/cpp/main/api/zygisk_main.cpp | 30 ++++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/core/src/main/cpp/main/Android.mk b/core/src/main/cpp/main/Android.mk index 74774520..bf47e448 100644 --- a/core/src/main/cpp/main/Android.mk +++ b/core/src/main/cpp/main/Android.mk @@ -15,7 +15,7 @@ else ifeq ($(API), zygisk) LOCAL_SRC_FILES += api/zygisk_main.cpp endif LOCAL_CFLAGS += -DINJECTED_AID=${INJECTED_AID} -LOCAL_LDLIBS := -llog -landroid +LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY) $(LOCAL_PATH)/api/config.cpp : FORCE diff --git a/core/src/main/cpp/main/api/zygisk_main.cpp b/core/src/main/cpp/main/api/zygisk_main.cpp index 26346ec3..4e4dc0ba 100644 --- a/core/src/main/cpp/main/api/zygisk_main.cpp +++ b/core/src/main/cpp/main/api/zygisk_main.cpp @@ -19,8 +19,7 @@ #include #include -#include -#include +#include #include "jni/zygisk.h" #include "logging.h" @@ -174,6 +173,24 @@ namespace lspd { int *allowUnload = &allow_unload; class SharedMem { + inline static void *cutils = nullptr; + + inline static int (*ashmem_create_region)(const char *name, std::size_t size) = nullptr; + + inline static int (*ashmem_set_prot_region)(int fd, int prot) = nullptr; + + inline static bool init = false; + + static void Init() { + if (init) return; + cutils = dlopen("/system/lib" LP_SELECT("", "64") "/libcutils.so", 0); + ashmem_create_region = cutils ? reinterpret_cast( + dlsym(cutils, "ashmem_create_region")) : nullptr; + ashmem_set_prot_region = cutils ? reinterpret_cast( + dlsym(cutils, "ashmem_set_prot_region")) : nullptr; + init = true; + } + int fd_ = -1; std::size_t size_ = 0; @@ -228,7 +245,8 @@ namespace lspd { constexpr bool ok() const { return fd_ > 0 && size_ > 0; } SharedMem(std::string_view name, std::size_t size) { - if ((fd_ = ASharedMemory_create(name.data(), size)) > 0) { + Init(); + if (ashmem_create_region && (fd_ = ashmem_create_region(name.data(), size)) > 0) { size_ = size; LOGD("using memfd"); } else { @@ -243,10 +261,12 @@ namespace lspd { } void SetProt(int prot) { - ASharedMemory_setProt(fd_, prot); + ashmem_set_prot_region(fd_, prot); } - SharedMem() : fd_(-1), size_(0) {} + SharedMem() : fd_(-1), size_(0) { + Init(); + } constexpr auto get() const { return fd_; }