Skip to content

Commit ec2763b

Browse files
gbaraldiKristoffer Carlsson
authored andcommitted
Make build_id.lo more random (#58258)
This does not fix the underlying issue that can occur here which is a collision of build_ids.lo between modules in IR decompression. Fixing that requires a somewhat significant overhaul to the serialization of IR (probably using the module identity as a key). This does mean we use a lot more of the bits available here so it makes collisions a lot less likely( they were already extremely rare) but hrtime does tend to only use the lower bits of a 64 bit integer and this will hopefully add some more randomness and make this even less likely (cherry picked from commit 7157407)
1 parent 4715983 commit ec2763b

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/module.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ JL_DLLEXPORT jl_module_t *jl_new_module_(jl_sym_t *name, jl_module_t *parent, ui
2525
m->istopmod = 0;
2626
m->uuid = uuid_zero;
2727
static unsigned int mcounter; // simple counter backup, in case hrtime is not incrementing
28-
m->build_id.lo = jl_hrtime() + (++mcounter);
28+
// TODO: this is used for ir decompression and is liable to hash collisions so use more of the bits
29+
m->build_id.lo = bitmix(jl_hrtime() + (++mcounter), jl_rand());
2930
if (!m->build_id.lo)
3031
m->build_id.lo++; // build id 0 is invalid
3132
m->build_id.hi = ~(uint64_t)0;

src/staticdata.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3673,7 +3673,11 @@ static jl_value_t *jl_restore_package_image_from_stream(void* pkgimage_handle, i
36733673

36743674
// No special processing of `new_ext_cis` is required because recaching handled it
36753675
// Add roots to methods
3676-
jl_copy_roots(method_roots_list, jl_worklist_key((jl_array_t*)restored));
3676+
int failed = jl_copy_roots(method_roots_list, jl_worklist_key((jl_array_t*)restored));
3677+
if (failed != 0) {
3678+
jl_printf(JL_STDERR, "Error copying roots to methods from Module: %s\n", pkgname);
3679+
abort();
3680+
}
36773681
// Insert method extensions and handle edges
36783682
JL_LOCK(&world_counter_lock);
36793683
// allocate a world for the new methods, and insert them there, invalidating content as needed

src/staticdata_utils.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,17 +947,31 @@ static void jl_activate_methods(jl_array_t *external, jl_array_t *internal, size
947947
}
948948
}
949949

950-
static void jl_copy_roots(jl_array_t *method_roots_list, uint64_t key)
950+
static int jl_copy_roots(jl_array_t *method_roots_list, uint64_t key)
951951
{
952952
size_t i, l = jl_array_nrows(method_roots_list);
953+
int failed = 0;
953954
for (i = 0; i < l; i+=2) {
954955
jl_method_t *m = (jl_method_t*)jl_array_ptr_ref(method_roots_list, i);
955956
jl_array_t *roots = (jl_array_t*)jl_array_ptr_ref(method_roots_list, i+1);
956957
if (roots) {
957958
assert(jl_is_array(roots));
959+
if (m->root_blocks) {
960+
// check for key collision
961+
uint64_t *blocks = jl_array_data(m->root_blocks, uint64_t);
962+
size_t nx2 = jl_array_nrows(m->root_blocks);
963+
for (size_t i = 0; i < nx2; i+=2) {
964+
if (blocks[i] == key) {
965+
// found duplicate block
966+
failed = -1;
967+
}
968+
}
969+
}
970+
958971
jl_append_method_roots(m, key, roots);
959972
}
960973
}
974+
return failed;
961975
}
962976

963977

0 commit comments

Comments
 (0)