From b010f770a060ab3075189b0c667c211251381178 Mon Sep 17 00:00:00 2001 From: "lehao.yxl" Date: Mon, 27 Jul 2020 11:28:34 +0800 Subject: [PATCH 1/2] fix: android targetSdkVersion 29 ashmem issue --- weex_core/Source/third_party/IPC/ashmem.c | 85 ++++++++++++++++++++--- 1 file changed, 76 insertions(+), 9 deletions(-) diff --git a/weex_core/Source/third_party/IPC/ashmem.c b/weex_core/Source/third_party/IPC/ashmem.c index 3c976edb56..5e64a2c240 100644 --- a/weex_core/Source/third_party/IPC/ashmem.c +++ b/weex_core/Source/third_party/IPC/ashmem.c @@ -31,9 +31,45 @@ #include "ashmem.h" #include +#include +#include +#include +#include #define ASHMEM_DEVICE "/dev/ashmem" + +static int system_property_get_int(const char* name) { + int result = 0; + char value[PROP_VALUE_MAX] = {}; + if (__system_property_get(name, value) >= 1) + result = atoi(value); + return result; +} + +static int device_api_level() { + static int s_api_level = -1; + if (s_api_level < 0) + s_api_level = system_property_get_int("ro.build.version.sdk"); + return s_api_level; +} + +typedef int(*ASharedMemory_createFunc)(const char *, size_t); +typedef size_t(*ASharedMemory_getSizeFunc)(int fd); +typedef int(*ASharedMemory_setProtFunc)(int fd, int prot); + +// Function pointers to shared memory functions. +typedef struct { + ASharedMemory_createFunc create; + ASharedMemory_getSizeFunc getSize; + ASharedMemory_setProtFunc setProt; +} ASharedMemoryFuncs; + +static ASharedMemoryFuncs s_ashmem_funcs = { + NULL, NULL, NULL +}; +static pthread_once_t s_ashmem_funcs_once = PTHREAD_ONCE_INIT; + /* * ashmem_create_region - creates a new ashmem region and returns the file * descriptor, or <0 on error @@ -41,7 +77,7 @@ * `name' is an optional label to give the region (visible in /proc/pid/maps) * `size' is the size of the region, in page-aligned bytes */ -int ashmem_create_region(const char* name, size_t size) +static int ashmem_dev_create_region(const char* name, size_t size) { int fd, ret; @@ -69,26 +105,57 @@ int ashmem_create_region(const char* name, size_t size) return ret; } -int ashmem_set_prot_region(int fd, int prot) +static int ashmem_dev_set_prot_region(int fd, int prot) { return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); } -int ashmem_pin_region(int fd, size_t offset, size_t len) +static size_t ashmem_dev_get_size_region(int fd) { - struct ashmem_pin pin = { offset, len }; - return ioctl(fd, ASHMEM_PIN, &pin); + return (size_t)ioctl(fd, ASHMEM_GET_SIZE, NULL); } -int ashmem_unpin_region(int fd, size_t offset, size_t len) +static void ashmem_init_funcs() { + ASharedMemoryFuncs* funcs = &s_ashmem_funcs; + // ANDROID_API_Q + if (device_api_level() >= 29) { + void * lib = dlopen("libandroid.so", RTLD_NOW); + funcs->create = + (ASharedMemory_createFunc)dlsym(lib, "ASharedMemory_create"); + funcs->getSize = + (ASharedMemory_getSizeFunc)dlsym(lib, "ASharedMemory_getSize"); + funcs->setProt = + (ASharedMemory_setProtFunc)dlsym(lib, "ASharedMemory_setProt"); + } + + if(funcs->create == NULL) { + funcs->create = &ashmem_dev_create_region; + } + if(funcs->getSize == NULL) { + funcs->getSize = &ashmem_dev_get_size_region; + } + if(funcs->setProt == NULL) { + funcs->setProt = &ashmem_dev_set_prot_region; + } +} + +static const ASharedMemoryFuncs* ashmem_get_funcs() { + pthread_once(&s_ashmem_funcs_once, ashmem_init_funcs); + return &s_ashmem_funcs; +} +int ashmem_create_region(const char *name, size_t size) + { + return ashmem_get_funcs()->create(name, size); + } + +int ashmem_set_prot_region(int fd, int prot) { - struct ashmem_pin pin = { offset, len }; - return ioctl(fd, ASHMEM_UNPIN, &pin); + return ashmem_get_funcs()->setProt(fd, prot); } int ashmem_get_size_region(int fd) { - return ioctl(fd, ASHMEM_GET_SIZE, NULL); + return (int)ashmem_get_funcs()->getSize(fd); } int ashmem_purge_all(void) From 8c5d8489430a5c8ea0028e941ac5b7639a1867e2 Mon Sep 17 00:00:00 2001 From: "lehao.yxl" Date: Mon, 27 Jul 2020 14:29:59 +0800 Subject: [PATCH 2/2] fix: targetSdkVersion 29 issue --- android/build.gradle | 4 ++-- weex_core/Source/third_party/IPC/ashmem.c | 22 ++++------------------ weex_core/Source/third_party/IPC/ashmem.h | 3 --- 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 8dea6a00e6..fbcc9e7afa 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -53,9 +53,9 @@ subprojects { } } ext { - compileSdkVersion=28 + compileSdkVersion=29 minSdkVersion=14 - targetSdkVersion=28 + targetSdkVersion=29 supportLibVersion="28.0.0" fastjsonLibVersion="1.1.70.android" //Default value for disableCov is false diff --git a/weex_core/Source/third_party/IPC/ashmem.c b/weex_core/Source/third_party/IPC/ashmem.c index 5e64a2c240..d8755a6c76 100644 --- a/weex_core/Source/third_party/IPC/ashmem.c +++ b/weex_core/Source/third_party/IPC/ashmem.c @@ -16,11 +16,6 @@ * specific language governing permissions and limitations * under the License. */ -/* - * Implementation of the user-space ashmem API for devices, which have our - * ashmem-enabled kernel. See ashmem-sim.c for the "fake" tmp-based version, - * used by the simulator. - */ #include #include @@ -38,7 +33,6 @@ #define ASHMEM_DEVICE "/dev/ashmem" - static int system_property_get_int(const char* name) { int result = 0; char value[PROP_VALUE_MAX] = {}; @@ -70,6 +64,7 @@ static ASharedMemoryFuncs s_ashmem_funcs = { }; static pthread_once_t s_ashmem_funcs_once = PTHREAD_ONCE_INIT; + /* * ashmem_create_region - creates a new ashmem region and returns the file * descriptor, or <0 on error @@ -143,10 +138,11 @@ static const ASharedMemoryFuncs* ashmem_get_funcs() { pthread_once(&s_ashmem_funcs_once, ashmem_init_funcs); return &s_ashmem_funcs; } + int ashmem_create_region(const char *name, size_t size) { return ashmem_get_funcs()->create(name, size); - } +} int ashmem_set_prot_region(int fd, int prot) { @@ -156,14 +152,4 @@ int ashmem_set_prot_region(int fd, int prot) int ashmem_get_size_region(int fd) { return (int)ashmem_get_funcs()->getSize(fd); -} - -int ashmem_purge_all(void) -{ - const int fd = open(ASHMEM_DEVICE, O_RDWR); - if (fd < 0) - return fd; - const int ret = ioctl(fd, ASHMEM_PURGE_ALL_CACHES, 0); - close(fd); - return ret; -} +} \ No newline at end of file diff --git a/weex_core/Source/third_party/IPC/ashmem.h b/weex_core/Source/third_party/IPC/ashmem.h index cec94f1d92..20718e673f 100644 --- a/weex_core/Source/third_party/IPC/ashmem.h +++ b/weex_core/Source/third_party/IPC/ashmem.h @@ -33,10 +33,7 @@ extern "C" { int ashmem_create_region(const char* name, size_t size); int ashmem_set_prot_region(int fd, int prot); -int ashmem_pin_region(int fd, size_t offset, size_t len); -int ashmem_unpin_region(int fd, size_t offset, size_t len); int ashmem_get_size_region(int fd); -int ashmem_purge_all(void); #ifdef __cplusplus }