From 7ab8496f561bf1281f8d8ad3bc69e610492a9047 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sat, 24 Feb 2024 14:17:15 -0500 Subject: [PATCH 1/2] Prevent tainting native code loading from propagating When we use options like code coverage, we can't use the native code present in the cache file since it is not instrumented. PR #52123 introduced the capability of skipping the native code during loading, but created the issue that subsequent packages could have an explicit or implicit dependency on the native code. PR #53439 tainted the current process by setting `use_sysimage_native_code`, but this flag is propagated to subprocesses and lead to a regression in test time. Move this to a process local flag to avoid the regression. In the future we might be able to change the calling convention for cross-image calls to `invoke(ci::CodeInstance, args...)` instead of `ci.fptr(args...)` to handle native code not being present. --- src/staticdata.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/staticdata.c b/src/staticdata.c index c8fd4053aaf73..3ef2b395f26ab 100644 --- a/src/staticdata.c +++ b/src/staticdata.c @@ -3066,6 +3066,11 @@ JL_DLLEXPORT void jl_set_sysimg_so(void *handle) extern void rebuild_image_blob_tree(void); extern void export_jl_small_typeof(void); +// When an image is loaded with ignore_native, all subsequent image loads must ignore +// native code in the cache-file since we can't gurantuee that there are no call edges +// into the native code of the image. See https://github.com/JuliaLang/julia/pull/52123#issuecomment-1959965395. +int IMAGE_NATIVE_CODE_TAINTED = 0; + static void jl_restore_system_image_from_stream_(ios_t *f, jl_image_t *image, jl_array_t *depmods, uint64_t checksum, /* outputs */ jl_array_t **restored, jl_array_t **init_order, jl_array_t **extext_methods, jl_array_t **internal_methods, @@ -3092,7 +3097,7 @@ static void jl_restore_system_image_from_stream_(ios_t *f, jl_image_t *image, jl // in --build mode only use sysimg data, not precompiled native code int imaging_mode = jl_generating_output() && !jl_options.incremental; - if (imaging_mode || jl_options.use_sysimage_native_code != JL_OPTIONS_USE_SYSIMAGE_NATIVE_CODE_YES) { + if (imaging_mode || jl_options.use_sysimage_native_code != JL_OPTIONS_USE_SYSIMAGE_NATIVE_CODE_YES || IMAGE_NATIVE_CODE_TAINTED) { memset(&image->fptrs, 0, sizeof(image->fptrs)); image->gvars_base = NULL; } @@ -3772,7 +3777,7 @@ JL_DLLEXPORT jl_value_t *jl_restore_package_image_from_file(const char *fname, j // Must disable using native code in possible downstream users of this code: // https://github.com/JuliaLang/julia/pull/52123#issuecomment-1959965395. // The easiest way to do that is to disable it in all of them. - jl_options.use_sysimage_native_code = JL_OPTIONS_USE_SYSIMAGE_NATIVE_CODE_NO; + IMAGE_NATIVE_CODE_TAINTED = 1; } jl_value_t* mod = jl_restore_incremental_from_buf(pkgimg_handle, pkgimg_data, &pkgimage, *plen, depmods, completeinfo, pkgname, 0); From b3860912c72bcfb7d4e45bafc728a9a50da00a8c Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Sat, 24 Feb 2024 15:45:10 -0500 Subject: [PATCH 2/2] Update src/staticdata.c --- src/staticdata.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/staticdata.c b/src/staticdata.c index 3ef2b395f26ab..261042b775c14 100644 --- a/src/staticdata.c +++ b/src/staticdata.c @@ -3100,6 +3100,7 @@ static void jl_restore_system_image_from_stream_(ios_t *f, jl_image_t *image, jl if (imaging_mode || jl_options.use_sysimage_native_code != JL_OPTIONS_USE_SYSIMAGE_NATIVE_CODE_YES || IMAGE_NATIVE_CODE_TAINTED) { memset(&image->fptrs, 0, sizeof(image->fptrs)); image->gvars_base = NULL; + IMAGE_NATIVE_CODE_TAINTED = 1; } // step 1: read section map