From 57a1d3a487ea0ee0c0df44d46a21d0a95897bb25 Mon Sep 17 00:00:00 2001 From: gbaraldi Date: Mon, 28 Apr 2025 17:18:06 -0300 Subject: [PATCH 1/2] Make build_id.lo more random --- src/module.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/module.c b/src/module.c index 4c685ca574523..e7cb3918c7352 100644 --- a/src/module.c +++ b/src/module.c @@ -481,7 +481,8 @@ static jl_module_t *jl_new_module__(jl_sym_t *name, jl_module_t *parent) m->istopmod = 0; m->uuid = uuid_zero; static unsigned int mcounter; // simple counter backup, in case hrtime is not incrementing - m->build_id.lo = jl_hrtime() + (++mcounter); + // TODO: this is used for ir decompression and is liable to hash collisions so use more of the bits + m->build_id.lo = bitmix(jl_hrtime() + (++mcounter), jl_rand()); if (!m->build_id.lo) m->build_id.lo++; // build id 0 is invalid m->build_id.hi = ~(uint64_t)0; From eb401f365f33b1def3b91a7e61d5d76b40b32ab6 Mon Sep 17 00:00:00 2001 From: gbaraldi Date: Wed, 30 Apr 2025 18:33:00 -0300 Subject: [PATCH 2/2] Make loading fail if it detects a buildid collision --- src/staticdata.c | 6 +++++- src/staticdata_utils.c | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/staticdata.c b/src/staticdata.c index a8ebf1d9bc345..df38ad1fc0277 100644 --- a/src/staticdata.c +++ b/src/staticdata.c @@ -4410,7 +4410,11 @@ static jl_value_t *jl_restore_package_image_from_stream(ios_t *f, jl_image_t *im JL_SIGATOMIC_END(); // Add roots to methods - jl_copy_roots(method_roots_list, jl_worklist_key((jl_array_t*)restored)); + int failed = jl_copy_roots(method_roots_list, jl_worklist_key((jl_array_t*)restored)); + if (failed != 0) { + jl_printf(JL_STDERR, "Error copying roots to methods from Module: %s\n", pkgname); + abort(); + } // Insert method extensions and handle edges int new_methods = jl_array_nrows(extext_methods) > 0; if (!new_methods) { diff --git a/src/staticdata_utils.c b/src/staticdata_utils.c index 95a64964eccba..c3f6a7e98a550 100644 --- a/src/staticdata_utils.c +++ b/src/staticdata_utils.c @@ -736,17 +736,31 @@ static void jl_activate_methods(jl_array_t *external, jl_array_t *internal, size } } -static void jl_copy_roots(jl_array_t *method_roots_list, uint64_t key) +static int jl_copy_roots(jl_array_t *method_roots_list, uint64_t key) { size_t i, l = jl_array_nrows(method_roots_list); + int failed = 0; for (i = 0; i < l; i+=2) { jl_method_t *m = (jl_method_t*)jl_array_ptr_ref(method_roots_list, i); jl_array_t *roots = (jl_array_t*)jl_array_ptr_ref(method_roots_list, i+1); if (roots) { assert(jl_is_array(roots)); + if (m->root_blocks) { + // check for key collision + uint64_t *blocks = jl_array_data(m->root_blocks, uint64_t); + size_t nx2 = jl_array_nrows(m->root_blocks); + for (size_t i = 0; i < nx2; i+=2) { + if (blocks[i] == key) { + // found duplicate block + failed = -1; + } + } + } + jl_append_method_roots(m, key, roots); } } + return failed; } static jl_value_t *read_verify_mod_list(ios_t *s, jl_array_t *depmods)