@@ -71,6 +71,7 @@ External links:
7171*/
7272#include <stdlib.h>
7373#include <string.h>
74+ #include <stdbool.h>
7475#include <stdio.h> // printf
7576#include <inttypes.h> // PRIxPTR
7677
@@ -3297,7 +3298,7 @@ static jl_value_t *jl_validate_cache_file(ios_t *f, jl_array_t *depmods, uint64_
32973298}
32983299
32993300// TODO?: refactor to make it easier to create the "package inspector"
3300- static jl_value_t * jl_restore_package_image_from_stream (ios_t * f , jl_image_t * image , jl_array_t * depmods , int completeinfo )
3301+ static jl_value_t * jl_restore_package_image_from_stream (ios_t * f , jl_image_t * image , jl_array_t * depmods , int completeinfo , bool needs_permalloc )
33013302{
33023303 uint64_t checksum = 0 ;
33033304 int64_t dataendpos = 0 ;
@@ -3308,7 +3309,7 @@ static jl_value_t *jl_restore_package_image_from_stream(ios_t *f, jl_image_t *im
33083309 return verify_fail ;
33093310
33103311 assert (datastartpos > 0 && datastartpos < dataendpos );
3311-
3312+ needs_permalloc = jl_options . permalloc_pkgimg || needs_permalloc ;
33123313 jl_value_t * restored = NULL ;
33133314 jl_array_t * init_order = NULL , * extext_methods = NULL , * new_specializations = NULL , * method_roots_list = NULL , * ext_targets = NULL , * edges = NULL ;
33143315 jl_svec_t * cachesizes_sv = NULL ;
@@ -3320,14 +3321,22 @@ static jl_value_t *jl_restore_package_image_from_stream(ios_t *f, jl_image_t *im
33203321 ios_bufmode (f , bm_none );
33213322 JL_SIGATOMIC_BEGIN ();
33223323 size_t len = dataendpos - datastartpos ;
3323- char * sysimg = (char * )jl_gc_perm_alloc (len , 0 , 64 , 0 );
3324+ char * sysimg ;
3325+ bool success = !needs_permalloc ;
33243326 ios_seek (f , datastartpos );
3325- if (ios_readall (f , sysimg , len ) != len || jl_crc32c (0 , sysimg , len ) != (uint32_t )checksum ) {
3326- restored = jl_get_exceptionf (jl_errorexception_type , "Error reading system image file." );
3327+ if (needs_permalloc )
3328+ sysimg = (char * )jl_gc_perm_alloc (len , 0 , 64 , 0 );
3329+ else
3330+ sysimg = & f -> buf [f -> bpos ];
3331+ if (needs_permalloc )
3332+ success = ios_readall (f , sysimg , len ) == len ;
3333+ if (!success || jl_crc32c (0 , sysimg , len ) != (uint32_t )checksum ) {
3334+ restored = jl_get_exceptionf (jl_errorexception_type , "Error reading package image file." );
33273335 JL_SIGATOMIC_END ();
33283336 }
33293337 else {
3330- ios_close (f );
3338+ if (needs_permalloc )
3339+ ios_close (f );
33313340 ios_static_buffer (f , sysimg , len );
33323341 pkgcachesizes cachesizes ;
33333342 jl_restore_system_image_from_stream_ (f , image , depmods , checksum , (jl_array_t * * )& restored , & init_order , & extext_methods , & new_specializations , & method_roots_list , & ext_targets , & edges , & base , & ccallable_list , & cachesizes );
@@ -3372,11 +3381,11 @@ static void jl_restore_system_image_from_stream(ios_t *f, jl_image_t *image, uin
33723381 jl_restore_system_image_from_stream_ (f , image , NULL , checksum | ((uint64_t )0xfdfcfbfa << 32 ), NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL );
33733382}
33743383
3375- JL_DLLEXPORT jl_value_t * jl_restore_incremental_from_buf (const char * buf , jl_image_t * image , size_t sz , jl_array_t * depmods , int completeinfo )
3384+ JL_DLLEXPORT jl_value_t * jl_restore_incremental_from_buf (const char * buf , jl_image_t * image , size_t sz , jl_array_t * depmods , int completeinfo , bool needs_permalloc )
33763385{
33773386 ios_t f ;
33783387 ios_static_buffer (& f , (char * )buf , sz );
3379- jl_value_t * ret = jl_restore_package_image_from_stream (& f , image , depmods , completeinfo );
3388+ jl_value_t * ret = jl_restore_package_image_from_stream (& f , image , depmods , completeinfo , needs_permalloc );
33803389 ios_close (& f );
33813390 return ret ;
33823391}
@@ -3389,7 +3398,7 @@ JL_DLLEXPORT jl_value_t *jl_restore_incremental(const char *fname, jl_array_t *d
33893398 "Cache file \"%s\" not found.\n" , fname );
33903399 }
33913400 jl_image_t pkgimage = {};
3392- jl_value_t * ret = jl_restore_package_image_from_stream (& f , & pkgimage , depmods , completeinfo );
3401+ jl_value_t * ret = jl_restore_package_image_from_stream (& f , & pkgimage , depmods , completeinfo , true );
33933402 ios_close (& f );
33943403 return ret ;
33953404}
@@ -3491,7 +3500,7 @@ JL_DLLEXPORT jl_value_t *jl_restore_package_image_from_file(const char *fname, j
34913500 }
34923501 #endif
34933502
3494- jl_value_t * mod = jl_restore_incremental_from_buf (pkgimg_data , & pkgimage , * plen , depmods , completeinfo );
3503+ jl_value_t * mod = jl_restore_incremental_from_buf (pkgimg_data , & pkgimage , * plen , depmods , completeinfo , false );
34953504
34963505 return mod ;
34973506}
0 commit comments