From 4a4d7f5e2f7c3a03faf8cef8a099b4c5fa2fa883 Mon Sep 17 00:00:00 2001 From: Ryan 'Swingflip' Hamlin Date: Thu, 25 Oct 2018 18:48:10 +0100 Subject: [PATCH] Revert MM dynamic code for seperate PR and rework --- libretro-common/dynamic/dylib.c | 278 +------------------------------- 1 file changed, 2 insertions(+), 276 deletions(-) diff --git a/libretro-common/dynamic/dylib.c b/libretro-common/dynamic/dylib.c index e57ef62678..bc6be9fc40 100644 --- a/libretro-common/dynamic/dylib.c +++ b/libretro-common/dynamic/dylib.c @@ -34,270 +34,6 @@ #include #endif -#if defined(__linux__) && !defined(ANDROID) -#define soramLoader -#endif - -#ifdef soramLoader -// stolen from here: https://x-c3ll.github.io/posts/fileless-memfd_create/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define debug_printf(...) //printf(__VA_ARGS__) - -typedef struct _soramHandle -{ - dylib_t handle; - char*soname; - int32_t ref; -} soramHandle_t; - -#define VECTOR_LIST_TYPE soramHandle_t -#define VECTOR_LIST_NAME soram -#include <../lists/vector_list.c> -typedef struct TYPE_NAME() soramList_t; -#undef VECTOR_LIST_TYPE -#undef VECTOR_LIST_NAME - -static soramList_t *soramList = 0; -static pthread_mutex_t soramMutex = PTHREAD_MUTEX_INITIALIZER; - -static soramHandle_t*soramFindName(const char*soname) -{ - size_t i; - if (soramList == 0) - return 0; - for (i = 0; i < soramList->count; ++i) - if (strcmp(soname, soramList->data[i].soname) == 0) - return &soramList->data[i]; - return 0; -} - -static soramHandle_t*soramFindHandle(dylib_t handle) -{ - size_t i; - if (soramList == 0) - return 0; - for (i = 0; i < soramList->count; ++i) - if (handle == soramList->data[i].handle) - return &soramList->data[i]; - return 0; -} - -static void soramAdd(const char*soname,dylib_t handle) -{ - soramHandle_t *so, _so; - if (soramList == 0) - soramList = soram_vector_list_new(); - so = soramFindHandle(0); - if (so == 0) - so = &_so; - so->handle = handle; - so->soname = strdup(soname); - so->ref = 1; - soram_vector_list_append(soramList, *so); -} - -static bool soramRemove(dylib_t handle) -{ - size_t i,count; - soramHandle_t *so = soramFindHandle(handle); - if (so == 0) - return 0; - if (--so->ref > 0) - return 1; -#ifndef NO_DLCLOSE - dlclose(so->handle); - free(so->soname); - so->handle = 0; - so->soname = 0; - count = 0; - for (i = 0; i < soramList->count; ++i) - if (soramList->data[i].ref > 0) - ++count; - if (count) - return 1; - soram_vector_list_free(soramList); - soramList = 0; -#endif - return 1; -} - -static void soramLock() -{ - pthread_mutex_lock(&soramMutex); -} - -static void soramUnlock() -{ - pthread_mutex_unlock(&soramMutex); -} - -// Wrapper to call memfd_create_cust syscall -static inline int memfd_create_cust(const char *pathname, int flags) -{ - return syscall(319, pathname, flags); -} - -// Returns a file descriptor where we can write our shared object -static int open_ramfs(const char *bname) -{ - int fd = memfd_create_cust(bname, 1); - if (fd < 0) - fd = shm_open(bname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); - return fd; -} - -static int is_xz(const char *pathname) -{ - uint32_t buffer[2]; - FILE*hf=fopen(pathname, "rb"); - buffer[0] = 0; - if (hf) - { - if (fread(buffer, 1, 4, hf) != 4) - { - buffer[0] = 0; - } - fclose(hf); - } - return (ntohl(buffer[0]) == 0xfd377a58) ? 0 : -1; -} - -static int open_xz(const char *pathname,const char *bname) -{ - int status = -1; - size_t size = 0x1000, rchunk = 0, wchunk = 0; - char *buffer; - FILE *fp; - int fd; - - fd = open_ramfs(bname); - if (fd < 0) - return fd; - - buffer = (char*)malloc(size+8); - if (buffer) - { - snprintf(buffer, size, "xz -cd '%s'", pathname); - fp = popen(buffer, "re"); - if (fp != 0) - { - while (!feof(fp) && !ferror(fp)) - { - rchunk=TEMP_FAILURE_RETRY(fread(buffer, 1, size, fp)); - if (rchunk > 0) - { - wchunk=TEMP_FAILURE_RETRY(write(fd, buffer, rchunk)); - if (wchunk != rchunk) - break; - } - } - status = pclose(fp); - } - - free(buffer); - if ((status == 0) && (wchunk == rchunk)) - return fd; - } - - close(fd); - return -1; -} - -static int dlcallback(struct dl_phdr_info *info, size_t size, void *data) -{ - if (info && info->dlpi_name) - debug_printf("\t[+] %s\n", info->dlpi_name); - return 0; -} - -// Load the shared object -static dylib_t soramLoad(const char *pathname, int flag) -{ - char path[1024]; - char shmp[1024]; - void *handle; - ssize_t size; - char *dname, *bname; - soramHandle_t *so; - - if (is_xz(pathname) < 0) - return 0; - - dname = strdup(pathname); - if (dname == 0) - return 0; - bname = basename(dname); - - soramLock(); - so = soramFindName(bname); - if (so) - { - ++so->ref; - soramUnlock(); - free(dname); - return so->handle; - } - - debug_printf("[INFO] [dylib] soramLoad(%s)\n", pathname); - int fd = open_xz(pathname, bname); - if (fd < 0) - { - soramUnlock(); - free(dname); - return 0; - } - - snprintf(path, 1024, "/proc/self/fd/%d", fd); - handle = dlopen(path, flag); - size = readlink(path, shmp, 1024); - close(fd); - dl_iterate_phdr(dlcallback, 0); - if ((size > 9) && (memcmp(shmp, "/dev/shm/", 9) == 0)) - { - debug_printf("\t\tshm_unlink(%s) - ", bname); - errno = 0; - flag = shm_unlink(bname); - debug_printf("%d\n", errno); - if (flag < 0) - { - debug_printf("\t\t unlink(%s) - ", shmp); - errno = 0; - unlink(shmp); - debug_printf("%d\n", errno); - } - } - soramAdd(bname, handle); - soramUnlock(); - free(dname); - debug_printf("[INFO] [dylib] soramLoad(%s) - %s\n", pathname, handle?"ok":"fail"); - return handle; -} - -static bool soramUnload(dylib_t handle) -{ - bool ret; - soramLock(); - ret = soramRemove(handle); - soramUnlock(); - return ret; -} -#endif //soramLoader - /* Assume W-functions do not work below Win2K and Xbox platforms */ #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX) @@ -357,12 +93,7 @@ dylib_t dylib_load(const char *path) } last_dyn_error[0] = 0; #else - dylib_t lib; -#ifdef soramLoader - lib = soramLoad(path, RTLD_LAZY); - if (!lib) -#endif - lib = dlopen(path, RTLD_LAZY); + dylib_t lib = dlopen(path, RTLD_LAZY); #endif return lib; } @@ -427,13 +158,8 @@ void dylib_close(dylib_t lib) set_dl_error(); last_dyn_error[0] = 0; #else -#ifdef soramLoader - if (soramUnload(lib) == 0) -#endif #ifndef NO_DLCLOSE - dlclose(lib); -#else - ; + dlclose(lib); #endif #endif }