Skip to content

Commit 03f031a

Browse files
authored
Handle errors in jl_image_unpack_zstd better, especially on Windows (#59773)
Handle a failed allocation in `jl_image_unpack_zstd` better: - On Windows, if `GetLargePageMinimum()` reports that large pages are unsupported, don't try to allocate with `MEM_LARGE_PAGES`. - If they are supported but the large page allocation fails, try again without the option. - On all platforms, provide a better error message when the allocation fails. Ported from #59340.
1 parent 17e0df5 commit 03f031a

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/staticdata.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,23 +3534,37 @@ JL_DLLEXPORT void jl_image_unpack_zstd(void *handle, jl_image_buf_t *image)
35343534
image->size = ZSTD_getFrameContentSize(data, *plen);
35353535
size_t page_size = jl_getpagesize(); /* jl_page_size is not set yet when loading sysimg */
35363536
size_t aligned_size = LLT_ALIGN(image->size, page_size);
3537+
int fail = 0;
35373538
#if defined(_OS_WINDOWS_)
35383539
size_t large_page_size = GetLargePageMinimum();
3539-
if (image->size > 4 * large_page_size) {
3540+
image->data = NULL;
3541+
if (large_page_size > 0 && image->size > 4 * large_page_size) {
35403542
size_t aligned_size = LLT_ALIGN(image->size, large_page_size);
35413543
image->data = (char *)VirtualAlloc(
35423544
NULL, aligned_size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE);
35433545
}
3544-
else {
3546+
if (!image->data) {
3547+
/* Try small pages if large pages failed. */
35453548
image->data = (char *)VirtualAlloc(NULL, aligned_size, MEM_COMMIT | MEM_RESERVE,
35463549
PAGE_READWRITE);
35473550
}
3551+
fail = !image->data;
3552+
#else
3553+
image->data = (char *)mmap(NULL, aligned_size, PROT_READ | PROT_WRITE,
3554+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3555+
fail = image->data == (void *)-1;
3556+
#endif
3557+
if (fail) {
3558+
const char *err;
3559+
#if defined(_OS_WINDOWS_)
3560+
char err_buf[256];
3561+
win32_formatmessage(GetLastError(), err_buf, sizeof(err_buf));
3562+
err = err_buf;
35483563
#else
3549-
image->data =
3550-
(char *)mmap(NULL, aligned_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3564+
err = strerror(errno);
35513565
#endif
3552-
if (!image->data || image->data == (void *)-1) {
3553-
jl_printf(JL_STDERR, "ERROR: failed to allocate space for system image\n");
3566+
jl_printf(JL_STDERR, "ERROR: failed to allocate memory for system image: %s\n",
3567+
err);
35543568
jl_exit(1);
35553569
}
35563570

0 commit comments

Comments
 (0)