Skip to content

Commit

Permalink
sqAllocateMemory should return null if failed, not killing the applic…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
tesonep committed Oct 24, 2022
1 parent 0b4fd58 commit 358106e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
7 changes: 1 addition & 6 deletions extracted/vm/src/win/sqWin32SpurAlloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,7 @@ sqAllocateMemory(usqInt minHeapSize, usqInt desiredHeapSize, usqInt desiredBaseA

alloc = sqAllocateMemorySegmentOfSizeAboveAllocatedSizeInto
(roundUpToPage(desiredHeapSize), address, &allocBytes);
if (!alloc) {
exit(errno);
sqMessageBox(MB_OK | MB_ICONSTOP, TEXT("VM Error:"),
TEXT("sqAllocateMemory: initial alloc failed!\n"));
exit(1);
}

return alloc;
}

Expand Down
18 changes: 7 additions & 11 deletions src/memoryUnix.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ sqAllocateMemory(usqInt minHeapSize, usqInt desiredHeapSize, usqInt desiredBaseA

usqInt desiredBaseAddressAligned = valign(desiredBaseAddress);

logDebug("Aligned Requested Size %d", heapLimit);
logDebug("Aligned Requested Size %"PRIdSQINT, heapLimit);

logDebug("Trying to load the image in %p\n",
(void* )desiredBaseAddressAligned);
Expand All @@ -152,19 +152,19 @@ sqAllocateMemory(usqInt minHeapSize, usqInt desiredHeapSize, usqInt desiredBaseA
* To avoid it, we force to use the required base address
*/
#ifndef __APPLE__
if(heap != MAP_FAILED && (usqInt)heap != desiredBaseAddressAligned){
if(heap != 0 && (usqInt)heap != desiredBaseAddressAligned){

desiredBaseAddressAligned = valign(desiredBaseAddressAligned + pageSize);

if((usqInt)heap < desiredBaseAddress){
logError("I cannot find a good memory address starting from: %p", (void*)desiredBaseAddress);
exit(-1);
return 0;
}

//If I overflow.
if(desiredBaseAddress > desiredBaseAddressAligned){
logError("I cannot find a good memory address starting from: %p", (void*)desiredBaseAddress);
exit(-1);
return 0;
}

munmap(heap, heapLimit);
Expand All @@ -173,15 +173,11 @@ sqAllocateMemory(usqInt minHeapSize, usqInt desiredHeapSize, usqInt desiredBaseA
#endif
}

if (!heap) {
logError("Failed to allocate at least %lld bytes)\n",
(long long )minHeapSize);
exit(-1);
}

heapSize = heapLimit;

logDebug("Loading the image in %p\n", (void* )heap);
if(heap){
logDebug("Loading the image in %p\n", (void* )heap);
}

return (usqInt) heap;
}
Expand Down

0 comments on commit 358106e

Please sign in to comment.