From 5dd884df9640a3be38507be60832c3b7d323b8dc Mon Sep 17 00:00:00 2001 From: notaz Date: Sat, 28 Sep 2024 00:03:42 +0300 Subject: [PATCH] lightrec: relax mem requirements so that asan works Seems to work anyway with suboptimal mappings, and MAP_FIXED_NOREPLACE is unnecessary as the kernel will honour the address if it's free and return something else if it's not. --- Makefile | 5 ++--- libpcsxcore/lightrec/mem.c | 46 ++++++++++++++++++++++++-------------- libpcsxcore/psxmem.c | 15 +++++++------ 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index 5b890600..9c27f06c 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ CFLAGS += -O2 -DNDEBUG endif ifeq ($(DEBUG_ASAN), 1) CFLAGS += -fsanitize=address +LDFLAGS += -fsanitize=address +#LDFLAGS += -static-libasan endif CFLAGS += -DP_HAVE_MMAP=$(if $(NO_MMAP),0,1) \ -DP_HAVE_PTHREAD=$(if $(NO_PTHREAD),0,1) \ @@ -39,9 +41,6 @@ endif CC_LINK ?= $(CC) CC_AS ?= $(CC) LDFLAGS += $(MAIN_LDFLAGS) -ifeq ($(DEBUG_ASAN), 1) -LDFLAGS += -static-libasan -endif EXTRA_LDFLAGS ?= -Wl,-Map=$@.map LDLIBS += $(MAIN_LDLIBS) ifdef PCNT diff --git a/libpcsxcore/lightrec/mem.c b/libpcsxcore/lightrec/mem.c index 5cd86b4f..4b582583 100644 --- a/libpcsxcore/lightrec/mem.c +++ b/libpcsxcore/lightrec/mem.c @@ -82,15 +82,15 @@ static int lightrec_mmap_ram(bool hugetlb) memfd = syscall(SYS_memfd_create, "/lightrec_memfd", flags); if (memfd < 0) { + SysMessage("Failed to create memfd: %d", errno); err = -errno; - fprintf(stderr, "Failed to create memfd: %d\n", err); return err; } err = ftruncate(memfd, 0x200000); if (err < 0) { + SysMessage("Could not trim memfd: %d", errno); err = -errno; - fprintf(stderr, "Could not trim memfd: %d\n", err); goto err_close_memfd; } @@ -139,61 +139,73 @@ err_close_memfd: int lightrec_init_mmap(void) { unsigned int i; - uintptr_t base; + s8 *base, *target; void *map; int err = lightrec_mmap_ram(true); if (err) { err = lightrec_mmap_ram(false); if (err) { - fprintf(stderr, "Unable to mmap RAM and mirrors\n"); + SysMessage("Unable to mmap RAM and mirrors"); return err; } } - base = (uintptr_t) psxM; + base = psxM; - map = mmap((void *)(base + 0x1f000000), 0x10000, + target = base + 0x1f000000; + map = mmap(target, 0x10000, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS, -1, 0); + MAP_PRIVATE | /*MAP_FIXED_NOREPLACE |*/ MAP_ANONYMOUS, -1, 0); if (map == MAP_FAILED) { + SysMessage("Unable to mmap parallel port: %d", errno); err = -EINVAL; - fprintf(stderr, "Unable to mmap parallel port\n"); goto err_unmap; } + if (map != target) + SysMessage("lightrec: mapped parallel port at %p, wanted %p", map, target); psxP = (s8 *)map; - map = mmap_huge((void *)(base + 0x1fc00000), 0x200000, + target = base + 0x1fc00000; + map = mmap_huge(target, 0x200000, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS, -1, 0); + MAP_PRIVATE | /*MAP_FIXED_NOREPLACE |*/ MAP_ANONYMOUS, -1, 0); if (map == MAP_FAILED) { + SysMessage("Unable to mmap BIOS: %d", errno); err = -EINVAL; - fprintf(stderr, "Unable to mmap BIOS\n"); goto err_unmap_parallel; } + if (map != target) + SysMessage("lightrec: mapped bios at %p, wanted %p", map, target); psxR = (s8 *)map; - map = mmap((void *)(base + 0x1f800000), 0x10000, + target = base + 0x1f800000; + map = mmap(target, 0x10000, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS, 0, 0); + MAP_PRIVATE | /*MAP_FIXED_NOREPLACE |*/ MAP_ANONYMOUS, 0, 0); if (map == MAP_FAILED) { + SysMessage("Unable to mmap scratchpad: %d", errno); err = -EINVAL; - fprintf(stderr, "Unable to mmap scratchpad\n"); goto err_unmap_bios; } + if (map != target) + SysMessage("lightrec: mapped scratchpad at %p, wanted %p", map, target); psxH = (s8 *)map; - map = mmap_huge((void *)(base + 0x800000), CODE_BUFFER_SIZE, + target = base + 0x800000; + map = mmap_huge(target, CODE_BUFFER_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS, + MAP_PRIVATE | /*MAP_FIXED_NOREPLACE |*/ MAP_ANONYMOUS, -1, 0); if (map == MAP_FAILED) { + SysMessage("Unable to mmap code buffer: %d", errno); err = -EINVAL; - fprintf(stderr, "Unable to mmap code buffer\n"); goto err_unmap_scratch; } + if (map != target) + SysMessage("lightrec: mapped code at %p, wanted %p", map, target); code_buffer = map; diff --git a/libpcsxcore/psxmem.c b/libpcsxcore/psxmem.c index 4e03b24b..0e28b72c 100644 --- a/libpcsxcore/psxmem.c +++ b/libpcsxcore/psxmem.c @@ -159,7 +159,7 @@ static int psxMemInitMap(void) if (psxM == MAP_FAILED) psxM = psxMap(0x77000000, 0x00210000, 0, MAP_TAG_RAM); if (psxM == MAP_FAILED) { - SysMessage(_("mapping main RAM failed")); + SysMessage("mapping main RAM failed"); psxM = NULL; return -1; } @@ -167,15 +167,15 @@ static int psxMemInitMap(void) psxH = psxMap(0x1f800000, 0x10000, 0, MAP_TAG_OTHER); if (psxH == MAP_FAILED) { - SysMessage(_("Error allocating memory!")); - psxMemShutdown(); + SysMessage("Error allocating psxH"); + psxH = NULL; return -1; } psxR = psxMap(0x1fc00000, 0x80000, 0, MAP_TAG_OTHER); if (psxR == MAP_FAILED) { - SysMessage(_("Error allocating memory!")); - psxMemShutdown(); + SysMessage("Error allocating psxR"); + psxR = NULL; return -1; } @@ -201,7 +201,8 @@ int psxMemInit(void) else ret = psxMemInitMap(); if (ret) { - SysMessage(_("Error allocating memory!")); + if (LIGHTREC_CUSTOM_MAP) + SysMessage("lightrec_init_mmap failed"); psxMemShutdown(); return -1; } @@ -213,7 +214,7 @@ int psxMemInit(void) psxMemWLUT = (u8 **)malloc(0x10000 * sizeof(void *)); if (psxMemRLUT == NULL || psxMemWLUT == NULL) { - SysMessage(_("Error allocating memory!")); + SysMessage("Error allocating psxMem LUTs"); psxMemShutdown(); return -1; } -- 2.39.5