Skip to content

Commit 8c4036e

Browse files
committed
Use uniquing target table to map gvars to uniqued values
1 parent 37110f4 commit 8c4036e

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/staticdata.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
4040
`uniquing_list` also holds the serialized location of external DataTypes, MethodInstances, and singletons
4141
in the serialized blob (i.e., new-at-the-time-of-serialization specializations).
42+
`uniquing_target` is a hash table for which `uniquing_target[obj] -> chosen_target`.
4243
4344
Most of step 2 is handled by `jl_write_values`, followed by special handling of the dedicated parallel streams.
4445
@@ -1837,7 +1838,7 @@ static void write_gvars(jl_serializer_state *s, arraylist_t *globals) JL_NOTSAFE
18371838
}
18381839

18391840
// Pointer relocation for native-code referenced global variables
1840-
static void jl_update_all_gvars(jl_serializer_state *s)
1841+
static void jl_update_all_gvars(jl_serializer_state *s, htable_t *uniquing_target)
18411842
{
18421843
if (sysimg_gvars_base == NULL)
18431844
return;
@@ -1851,7 +1852,11 @@ static void jl_update_all_gvars(jl_serializer_state *s)
18511852
uintptr_t offset = *gvars;
18521853
if (offset) {
18531854
uintptr_t v = get_item_for_reloc(s, base, size, offset, s->link_ids_gvars, &link_index);
1854-
// TODO(required): if (incremental) v = jl_as_global_root((jl_value_t*)v);
1855+
uintptr_t unique_value = (uintptr_t)ptrhash_get(uniquing_target, (void*)v);
1856+
if ((void*)unique_value != HT_NOTFOUND)
1857+
v = unique_value;
1858+
// if (s->incremental)
1859+
// v = (uintptr_t) jl_as_global_root((jl_value_t*)v);
18551860
*sysimg_gvars(sysimg_gvars_base, gvname_index) = (uintptr_t)v;
18561861
}
18571862
gvname_index += 1;
@@ -2779,21 +2784,10 @@ static void jl_restore_system_image_from_stream_(ios_t *f, jl_array_t *depmods,
27792784
*pfld = (uintptr_t)newobj | GC_OLD;
27802785
else
27812786
*pfld = (uintptr_t)newobj;
2787+
ptrhash_put(&uniquing_target, (void*)obj, (void*)newobj);
27822788
assert(!(image_base < (char*)newobj && (char*)newobj <= image_base + sizeof_sysimg + sizeof(uintptr_t)));
27832789
assert(jl_typeis(obj, otyp));
27842790
}
2785-
// Write junk in place of the source data we used during uniquing, to catch inadvertent references to
2786-
// it from elsewhere.
2787-
for (size_t i = 0; i < s.uniquing_list.len; i++) {
2788-
void *item = s.uniquing_list.items[i];
2789-
jl_taggedvalue_t *o = jl_astaggedvalue(item);
2790-
if (o->type == (jl_value_t*)jl_method_instance_type)
2791-
memset(o, 0xba, sizeof(jl_value_t*) + sizeof(jl_method_instance_t));
2792-
else if (o->type == (jl_value_t*)jl_datatype_type)
2793-
memset(o, 0xba, sizeof(jl_value_t*) + sizeof(jl_datatype_t));
2794-
else
2795-
memset(o, 0xba, sizeof(jl_value_t*));
2796-
}
27972791
// Perform fixups: things like updating world ages, inserting methods & specializations, etc.
27982792
size_t world = jl_atomic_load_acquire(&jl_world_counter);
27992793
for (size_t i = 0; i < s.fixup_list.len; i++) {
@@ -2872,14 +2866,29 @@ static void jl_restore_system_image_from_stream_(ios_t *f, jl_array_t *depmods,
28722866
r->bnd_cache = b && b->value ? b : NULL;
28732867
}
28742868
}
2875-
arraylist_free(&s.uniquing_list);
28762869
arraylist_free(&s.fixup_list);
28772870

28782871
// s.link_ids_gvars will be processed in `jl_update_all_gvars`
28792872
ios_close(&relocs);
28802873
ios_close(&const_data);
2881-
jl_update_all_gvars(&s); // gvars relocs
2874+
jl_update_all_gvars(&s, &uniquing_target); // gvars relocs
28822875
ios_close(&gvar_record);
2876+
2877+
// Write junk in place of the source data we used during uniquing, to catch inadvertent references to
2878+
// it from elsewhere. This needs to be delayed after update_all_gvars so that we can rewire gvars as well.
2879+
for (size_t i = 0; i < s.uniquing_list.len; i++) {
2880+
void *item = s.uniquing_list.items[i];
2881+
jl_taggedvalue_t *o = jl_astaggedvalue(item);
2882+
if (o->type == (jl_value_t*)jl_method_instance_type)
2883+
memset(o, 0xba, sizeof(jl_value_t*) + sizeof(jl_method_instance_t));
2884+
else if (o->type == (jl_value_t*)jl_datatype_type)
2885+
memset(o, 0xba, sizeof(jl_value_t*) + sizeof(jl_datatype_t));
2886+
else
2887+
memset(o, 0xba, sizeof(jl_value_t*));
2888+
}
2889+
arraylist_free(&s.uniquing_list);
2890+
htable_free(&uniquing_target);
2891+
28832892
s.s = NULL;
28842893

28852894
if (0) {

0 commit comments

Comments
 (0)