Skip to content

Commit cf4f1ba

Browse files
vtjnashKristofferC
authored andcommitted
serialization: fix relocatability bug (#54738)
(cherry picked from commit 323e725)
1 parent 82d961b commit cf4f1ba

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/staticdata.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ void *native_functions; // opaque jl_native_code_desc_t blob used for fetching
461461

462462
// table of struct field addresses to rewrite during saving
463463
static htable_t field_replace;
464+
static htable_t relocatable_ext_cis;
464465

465466
// array of definitions for the predefined function pointers
466467
// (reverse of fptr_to_id)
@@ -693,7 +694,8 @@ static int needs_uniquing(jl_value_t *v) JL_NOTSAFEPOINT
693694

694695
static void record_field_change(jl_value_t **addr, jl_value_t *newval) JL_NOTSAFEPOINT
695696
{
696-
ptrhash_put(&field_replace, (void*)addr, newval);
697+
if (*addr != newval)
698+
ptrhash_put(&field_replace, (void*)addr, newval);
697699
}
698700

699701
static jl_value_t *get_replaceable_field(jl_value_t **addr, int mutabl) JL_GC_DISABLED
@@ -836,6 +838,8 @@ static void jl_insert_into_serialization_queue(jl_serializer_state *s, jl_value_
836838
// TODO: if (ci in ci->defs->cache)
837839
record_field_change((jl_value_t**)&ci->next, NULL);
838840
}
841+
if (jl_atomic_load_relaxed(&ci->inferred) && !is_relocatable_ci(&relocatable_ext_cis, ci))
842+
record_field_change((jl_value_t**)&ci->inferred, jl_nothing);
839843
}
840844

841845
if (immediate) // must be things that can be recursively handled, and valid as type parameters
@@ -1631,6 +1635,7 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
16311635
jl_atomic_store_release(&newci->min_world, 1);
16321636
jl_atomic_store_release(&newci->max_world, 0);
16331637
}
1638+
newci->relocatability = 0;
16341639
}
16351640
jl_atomic_store_relaxed(&newci->invoke, NULL);
16361641
jl_atomic_store_relaxed(&newci->specsigflags, 0);
@@ -2573,7 +2578,7 @@ static void jl_prepare_serialization_data(jl_array_t *mod_array, jl_array_t *new
25732578
*edges = jl_alloc_vec_any(0);
25742579
*method_roots_list = jl_alloc_vec_any(0);
25752580
// Collect the new method roots for external specializations
2576-
jl_collect_new_roots(*method_roots_list, *new_ext_cis, worklist_key);
2581+
jl_collect_new_roots(&relocatable_ext_cis, *method_roots_list, *new_ext_cis, worklist_key);
25772582
jl_collect_edges(*edges, *ext_targets, *new_ext_cis, world);
25782583
}
25792584
assert(edges_map == NULL); // jl_collect_edges clears this when done
@@ -2974,6 +2979,7 @@ JL_DLLEXPORT void jl_create_system_image(void **_native_data, jl_array_t *workli
29742979
assert((ct->reentrant_timing & 0b1110) == 0);
29752980
ct->reentrant_timing |= 0b1000;
29762981
if (worklist) {
2982+
htable_new(&relocatable_ext_cis, 0);
29772983
jl_prepare_serialization_data(mod_array, newly_inferred, jl_worklist_key(worklist),
29782984
&extext_methods, &new_ext_cis, &method_roots_list, &ext_targets, &edges);
29792985
if (!emit_split) {
@@ -2990,6 +2996,8 @@ JL_DLLEXPORT void jl_create_system_image(void **_native_data, jl_array_t *workli
29902996
jl_save_system_image_to_stream(ff, mod_array, worklist, extext_methods, new_ext_cis, method_roots_list, ext_targets, edges);
29912997
if (_native_data != NULL)
29922998
native_functions = NULL;
2999+
if (worklist)
3000+
htable_free(&relocatable_ext_cis);
29933001
// make sure we don't run any Julia code concurrently before this point
29943002
// Re-enable running julia code for postoutput hooks, atexit, etc.
29953003
jl_gc_enable_finalizers(ct, 1);

src/staticdata_utils.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ static int has_backedge_to_worklist(jl_method_instance_t *mi, htable_t *visited,
209209
return found;
210210
}
211211

212+
static int is_relocatable_ci(htable_t *relocatable_ext_cis, jl_code_instance_t *ci)
213+
{
214+
if (!ci->relocatability)
215+
return 0;
216+
jl_method_instance_t *mi = ci->def;
217+
jl_method_t *m = mi->def.method;
218+
if (!ptrhash_has(relocatable_ext_cis, ci) && jl_object_in_image((jl_value_t*)m) && (!jl_is_method(m) || jl_object_in_image((jl_value_t*)m->module)))
219+
return 0;
220+
return 1;
221+
}
222+
212223
// Given the list of CodeInstances that were inferred during the build, select
213224
// those that are (1) external, (2) still valid, (3) are inferred to be called
214225
// from the worklist or explicitly added by a `precompile` statement, and
@@ -258,7 +269,7 @@ static jl_array_t *queue_external_cis(jl_array_t *list)
258269
}
259270

260271
// New roots for external methods
261-
static void jl_collect_new_roots(jl_array_t *roots, jl_array_t *new_ext_cis, uint64_t key)
272+
static void jl_collect_new_roots(htable_t *relocatable_ext_cis, jl_array_t *roots, jl_array_t *new_ext_cis, uint64_t key)
262273
{
263274
htable_t mset;
264275
htable_new(&mset, 0);
@@ -269,6 +280,7 @@ static void jl_collect_new_roots(jl_array_t *roots, jl_array_t *new_ext_cis, uin
269280
jl_method_t *m = ci->def->def.method;
270281
assert(jl_is_method(m));
271282
ptrhash_put(&mset, (void*)m, (void*)m);
283+
ptrhash_put(relocatable_ext_cis, (void*)ci, (void*)ci);
272284
}
273285
int nwithkey;
274286
void *const *table = mset.table;

test/precompile.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ precompile_test_harness("code caching") do dir
806806
mi = minternal.specializations::Core.MethodInstance
807807
@test mi.specTypes == Tuple{typeof(M.getelsize),Vector{Int32}}
808808
ci = mi.cache
809-
@test ci.relocatability == 1
809+
@test ci.relocatability == 0
810810
@test ci.inferred !== nothing
811811
# ...and that we can add "untracked" roots & non-relocatable CodeInstances to them too
812812
Base.invokelatest() do

0 commit comments

Comments
 (0)