Skip to content

Commit 323e725

Browse files
authored
serialization: fix relocatability bug (#54738)
1 parent 7cdbf74 commit 323e725

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
@@ -478,6 +478,7 @@ void *native_functions; // opaque jl_native_code_desc_t blob used for fetching
478478

479479
// table of struct field addresses to rewrite during saving
480480
static htable_t field_replace;
481+
static htable_t relocatable_ext_cis;
481482

482483
// array of definitions for the predefined function pointers
483484
// (reverse of fptr_to_id)
@@ -710,7 +711,8 @@ static int needs_uniquing(jl_value_t *v) JL_NOTSAFEPOINT
710711

711712
static void record_field_change(jl_value_t **addr, jl_value_t *newval) JL_NOTSAFEPOINT
712713
{
713-
ptrhash_put(&field_replace, (void*)addr, newval);
714+
if (*addr != newval)
715+
ptrhash_put(&field_replace, (void*)addr, newval);
714716
}
715717

716718
static jl_value_t *get_replaceable_field(jl_value_t **addr, int mutabl) JL_GC_DISABLED
@@ -852,6 +854,8 @@ static void jl_insert_into_serialization_queue(jl_serializer_state *s, jl_value_
852854
// TODO: if (ci in ci->defs->cache)
853855
record_field_change((jl_value_t**)&ci->next, NULL);
854856
}
857+
if (jl_atomic_load_relaxed(&ci->inferred) && !is_relocatable_ci(&relocatable_ext_cis, ci))
858+
record_field_change((jl_value_t**)&ci->inferred, jl_nothing);
855859
}
856860

857861
if (immediate) // must be things that can be recursively handled, and valid as type parameters
@@ -1647,6 +1651,7 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
16471651
jl_atomic_store_release(&newci->min_world, 1);
16481652
jl_atomic_store_release(&newci->max_world, 0);
16491653
}
1654+
newci->relocatability = 0;
16501655
}
16511656
jl_atomic_store_relaxed(&newci->invoke, NULL);
16521657
jl_atomic_store_relaxed(&newci->specsigflags, 0);
@@ -2603,7 +2608,7 @@ static void jl_prepare_serialization_data(jl_array_t *mod_array, jl_array_t *new
26032608
*edges = jl_alloc_vec_any(0);
26042609
*method_roots_list = jl_alloc_vec_any(0);
26052610
// Collect the new method roots for external specializations
2606-
jl_collect_new_roots(*method_roots_list, *new_ext_cis, worklist_key);
2611+
jl_collect_new_roots(&relocatable_ext_cis, *method_roots_list, *new_ext_cis, worklist_key);
26072612
jl_collect_edges(*edges, *ext_targets, *new_ext_cis, world);
26082613
}
26092614
assert(edges_map == NULL); // jl_collect_edges clears this when done
@@ -3004,6 +3009,7 @@ JL_DLLEXPORT void jl_create_system_image(void **_native_data, jl_array_t *workli
30043009
assert((ct->reentrant_timing & 0b1110) == 0);
30053010
ct->reentrant_timing |= 0b1000;
30063011
if (worklist) {
3012+
htable_new(&relocatable_ext_cis, 0);
30073013
jl_prepare_serialization_data(mod_array, newly_inferred, jl_worklist_key(worklist),
30083014
&extext_methods, &new_ext_cis, &method_roots_list, &ext_targets, &edges);
30093015
if (!emit_split) {
@@ -3020,6 +3026,8 @@ JL_DLLEXPORT void jl_create_system_image(void **_native_data, jl_array_t *workli
30203026
jl_save_system_image_to_stream(ff, mod_array, worklist, extext_methods, new_ext_cis, method_roots_list, ext_targets, edges);
30213027
if (_native_data != NULL)
30223028
native_functions = NULL;
3029+
if (worklist)
3030+
htable_free(&relocatable_ext_cis);
30233031
// make sure we don't run any Julia code concurrently before this point
30243032
// Re-enable running julia code for postoutput hooks, atexit, etc.
30253033
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
@@ -789,7 +789,7 @@ precompile_test_harness("code caching") do dir
789789
mi = minternal.specializations::Core.MethodInstance
790790
@test mi.specTypes == Tuple{typeof(M.getelsize),Vector{Int32}}
791791
ci = mi.cache
792-
@test ci.relocatability == 1
792+
@test ci.relocatability == 0
793793
@test ci.inferred !== nothing
794794
# ...and that we can add "untracked" roots & non-relocatable CodeInstances to them too
795795
Base.invokelatest() do

0 commit comments

Comments
 (0)