From 9ce90315ab53cf8505c201e398d883d2ed6418da Mon Sep 17 00:00:00 2001 From: khanhduytran0 Date: Wed, 16 Apr 2025 18:28:35 +0700 Subject: [PATCH] Feat: pre-validate if there is sufficient memory --- Natives/JavaLauncher.m | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Natives/JavaLauncher.m b/Natives/JavaLauncher.m index c46bd5a4..50a3510c 100644 --- a/Natives/JavaLauncher.m +++ b/Natives/JavaLauncher.m @@ -20,6 +20,14 @@ extern char **environ; +BOOL validateVirtualMemorySpace(int size) { + void *map = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + // check if process successfully maps and unmaps a contiguous range + if(map == MAP_FAILED || munmap(map, size) != 0) + return NO; + return YES; +} + void init_loadDefaultEnv() { /* Define default env */ @@ -167,6 +175,11 @@ int launchJVM(NSString *username, id launchTarget, int width, int height, int mi allocmem = getPrefInt(@"java.allocated_memory"); } NSLog(@"[JavaLauncher] Max RAM allocation is set to %d MB", allocmem); + if (!validateVirtualMemorySpace(allocmem)) { + UIKit_returnToSplitView(); + showDialog(localize(@"Error", nil), @"Insufficient contiguous virtual memory space. Lower memory allocation and try again."); + return 1; + } int margc = -1; const char *margv[1000];