Skip to content

Commit cde8038

Browse files
xal-0gbaraldi
authored andcommitted
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 b406292 commit cde8038

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
@@ -3661,23 +3661,37 @@ JL_DLLEXPORT void jl_image_unpack_zstd(void *handle, jl_image_buf_t *image)
36613661
image->size = ZSTD_getFrameContentSize(data, *plen);
36623662
size_t page_size = jl_getpagesize(); /* jl_page_size is not set yet when loading sysimg */
36633663
size_t aligned_size = LLT_ALIGN(image->size, page_size);
3664+
int fail = 0;
36643665
#if defined(_OS_WINDOWS_)
36653666
size_t large_page_size = GetLargePageMinimum();
3666-
if (image->size > 4 * large_page_size) {
3667+
image->data = NULL;
3668+
if (large_page_size > 0 && image->size > 4 * large_page_size) {
36673669
size_t aligned_size = LLT_ALIGN(image->size, large_page_size);
36683670
image->data = (char *)VirtualAlloc(
36693671
NULL, aligned_size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE);
36703672
}
3671-
else {
3673+
if (!image->data) {
3674+
/* Try small pages if large pages failed. */
36723675
image->data = (char *)VirtualAlloc(NULL, aligned_size, MEM_COMMIT | MEM_RESERVE,
36733676
PAGE_READWRITE);
36743677
}
3678+
fail = !image->data;
3679+
#else
3680+
image->data = (char *)mmap(NULL, aligned_size, PROT_READ | PROT_WRITE,
3681+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3682+
fail = image->data == (void *)-1;
3683+
#endif
3684+
if (fail) {
3685+
const char *err;
3686+
#if defined(_OS_WINDOWS_)
3687+
char err_buf[256];
3688+
win32_formatmessage(GetLastError(), err_buf, sizeof(err_buf));
3689+
err = err_buf;
36753690
#else
3676-
image->data =
3677-
(char *)mmap(NULL, aligned_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3691+
err = strerror(errno);
36783692
#endif
3679-
if (!image->data || image->data == (void *)-1) {
3680-
jl_printf(JL_STDERR, "ERROR: failed to allocate space for system image\n");
3693+
jl_printf(JL_STDERR, "ERROR: failed to allocate memory for system image: %s\n",
3694+
err);
36813695
jl_exit(1);
36823696
}
36833697

0 commit comments

Comments
 (0)