From: notaz Date: Fri, 27 Sep 2024 21:03:42 +0000 (+0300) Subject: lightrec: relax mem requirements so that asan works X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aa6257e409dd121267c855d8ef26b344e0d29196;p=pcsx_rearmed.git 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. --- diff --git a/Makefile b/Makefile index b0c56452..d1da30b6 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,8 @@ endif 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) \ @@ -48,9 +50,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; }