Skip to content

Commit 7f6aedb

Browse files
authored
[mono] Fix g_str_hash skipping the first byte and remove mono_metadata_str_hash (#119208)
Port of mono/mono#21844 `g_str_hash` was skipping the first byte because the `*p++` increment happens before the hash calculation, so when `*p` is referenced inside the loop, it's actually looking at the character after the one that was just tested. We can also remove `mono_metadata_str_hash` which has similar logic (but isn't technically affected since it was already accounting for the first byte in initializating `guint hash = *p`) since we no longer support linking to a different glib - we always link to eglib - and so the original reason for having a separate function no longer applies.
1 parent 1c82b73 commit 7f6aedb

File tree

8 files changed

+17
-43
lines changed

8 files changed

+17
-43
lines changed

src/mono/mono/eglib/ghashtable.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,8 @@ g_str_hash (gconstpointer v1)
675675
guint hash = 0;
676676
unsigned char *p = (unsigned char *) v1;
677677

678-
while (*p++)
679-
hash = (hash << 5) - (hash + *p);
678+
while (*p)
679+
hash = (hash << 5) - (hash + *p++);
680680

681681
return hash;
682682
}

src/mono/mono/metadata/class-init.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token, MonoError
511511
}
512512

513513
klass->name = name;
514-
klass->name_hash = mono_metadata_str_hash (name);
514+
klass->name_hash = g_str_hash (name);
515515
klass->name_space = nspace;
516516

517517
MONO_PROFILER_RAISE (class_loading, (klass));
@@ -1176,7 +1176,7 @@ mono_class_create_bounded_array (MonoClass *eclass, guint32 rank, gboolean bound
11761176
name [nsize + maxrank + bounded] = ']';
11771177
name [nsize + maxrank + bounded + 1] = 0;
11781178
klass->name = mm ? mono_mem_manager_strdup (mm, name) : mono_image_strdup (image, name);
1179-
klass->name_hash = mono_metadata_str_hash (klass->name);
1179+
klass->name_hash = g_str_hash (klass->name);
11801180
g_free (name);
11811181

11821182
klass->type_token = 0;
@@ -1534,7 +1534,7 @@ mono_class_create_ptr (MonoType *type)
15341534
result->name_space = el_class->name_space;
15351535
name = g_strdup_printf ("%s*", el_class->name);
15361536
result->name = mm ? mono_mem_manager_strdup (mm, name) : mono_image_strdup (image, name);
1537-
result->name_hash = mono_metadata_str_hash (result->name);
1537+
result->name_hash = g_str_hash (result->name);
15381538
result->class_kind = MONO_CLASS_POINTER;
15391539
g_free (name);
15401540

@@ -1613,7 +1613,7 @@ mono_class_create_fnptr (MonoMethodSignature *sig)
16131613
result->parent = NULL; /* no parent for PTR types */
16141614
result->name_space = "System";
16151615
result->name = "MonoFNPtrFakeClass";
1616-
result->name_hash = mono_metadata_str_hash (result->name);
1616+
result->name_hash = g_str_hash (result->name);
16171617
result->class_kind = MONO_CLASS_POINTER;
16181618

16191619
result->image = mono_defaults.corlib; /* need to fix... */

src/mono/mono/metadata/metadata-internals.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,8 +1070,6 @@ mono_metadata_get_corresponding_property_from_generic_type_definition (MonoPrope
10701070
guint32
10711071
mono_metadata_signature_size (MonoMethodSignature *sig);
10721072

1073-
guint mono_metadata_str_hash (gconstpointer v1);
1074-
10751073
gboolean mono_image_load_pe_data (MonoImage *image);
10761074

10771075
gboolean mono_image_load_cli_data (MonoImage *image);

src/mono/mono/metadata/metadata.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5598,29 +5598,6 @@ mono_metadata_generic_context_equal (const MonoGenericContext *g1, const MonoGen
55985598
return g1->class_inst == g2->class_inst && g1->method_inst == g2->method_inst;
55995599
}
56005600

5601-
/*
5602-
* mono_metadata_str_hash:
5603-
*
5604-
* This should be used instead of g_str_hash for computing hash codes visible
5605-
* outside this module, since g_str_hash () is not guaranteed to be stable
5606-
* (its not the same in eglib for example).
5607-
*/
5608-
guint
5609-
mono_metadata_str_hash (gconstpointer v1)
5610-
{
5611-
/* Same as g_str_hash () in glib */
5612-
/* note: signed/unsigned char matters - we feed UTF-8 to this function, so the high bit will give diferent results if we don't match. */
5613-
unsigned char *p = (unsigned char *) v1;
5614-
guint hash = *p;
5615-
5616-
while (*p++) {
5617-
if (*p)
5618-
hash = (hash << 5) - hash + *p;
5619-
}
5620-
5621-
return hash;
5622-
}
5623-
56245601
/**
56255602
* mono_metadata_type_hash:
56265603
* \param t1 a type

src/mono/mono/metadata/object.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,6 @@ field_is_special_static (MonoClass *fklass, MonoClassField *field)
13111311
* The IMT slot is embedded into AOTed code, so this must return the same value
13121312
* for the same method across all executions. This means:
13131313
* - pointers shouldn't be used as hash values.
1314-
* - mono_metadata_str_hash () should be used for hashing strings.
13151314
*/
13161315
guint32
13171316
mono_method_get_imt_slot (MonoMethod *method)
@@ -1348,8 +1347,8 @@ mono_method_get_imt_slot (MonoMethod *method)
13481347

13491348
/* Initialize hashes */
13501349
hashes [0] = m_class_get_name_hash (method->klass);
1351-
hashes [1] = mono_metadata_str_hash (m_class_get_name_space (method->klass));
1352-
hashes [2] = mono_metadata_str_hash (method->name);
1350+
hashes [1] = g_str_hash (m_class_get_name_space (method->klass));
1351+
hashes [2] = g_str_hash (method->name);
13531352
hashes [3] = mono_metadata_type_hash (sig->ret);
13541353
for (i = 0; i < sig->param_count; i++) {
13551354
hashes [4 + i] = mono_metadata_type_hash (sig->params [i]);

src/mono/mono/metadata/sre.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2558,7 +2558,7 @@ reflection_setup_internal_class_internal (MonoReflectionTypeBuilderHandle ref_tb
25582558
klass->inited = 1; /* we lie to the runtime */
25592559
klass->name = mono_string_to_utf8_image (klass->image, ref_name, error);
25602560
goto_if_nok (error, leave);
2561-
klass->name_hash = mono_metadata_str_hash (klass->name);
2561+
klass->name_hash = g_str_hash (klass->name);
25622562
klass->name_space = mono_string_to_utf8_image (klass->image, ref_nspace, error);
25632563
goto_if_nok (error, leave);
25642564
klass->type_token = MONO_TOKEN_TYPE_DEF | table_idx;

src/mono/mono/mini/aot-compiler.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11284,18 +11284,18 @@ mono_aot_method_hash (MonoMethod *method)
1128411284
else
1128511285
full_name = mono_type_full_name (m_class_get_byval_arg (klass));
1128611286

11287-
hashes [0] = mono_metadata_str_hash (full_name);
11287+
hashes [0] = g_str_hash (full_name);
1128811288
hashes [1] = 0;
1128911289
g_free (full_name);
1129011290
} else {
1129111291
hashes [0] = m_class_get_name_hash (klass);
11292-
hashes [1] = mono_metadata_str_hash (m_class_get_name_space (klass));
11292+
hashes [1] = g_str_hash (m_class_get_name_space (klass));
1129311293
}
1129411294
if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && mono_marshal_get_wrapper_info (method)->subtype == WRAPPER_SUBTYPE_ICALL_WRAPPER)
1129511295
/* The name might not be set correctly if DISABLE_JIT is set */
1129611296
hashes [2] = mono_marshal_get_wrapper_info (method)->d.icall.jit_icall_id;
1129711297
else
11298-
hashes [2] = mono_metadata_str_hash (method->name);
11298+
hashes [2] = g_str_hash (method->name);
1129911299

1130011300
if (method->wrapper_type == MONO_WRAPPER_OTHER) {
1130111301
if (info && (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG || info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG))
@@ -11710,7 +11710,7 @@ static uint32_t
1171011710
hash_for_class (MonoClass *klass)
1171111711
{
1171211712
char *full_name = get_class_full_name_for_hash (klass);
11713-
uint32_t hash = mono_metadata_str_hash (full_name);
11713+
uint32_t hash = g_str_hash (full_name);
1171411714
g_free (full_name);
1171511715
return hash;
1171611716
}
@@ -12094,7 +12094,7 @@ emit_globals (MonoAotCompile *acfg)
1209412094
for (guint i = 0; i < acfg->globals->len; ++i) {
1209512095
char *name = (char *)g_ptr_array_index (acfg->globals, i);
1209612096

12097-
hash = mono_metadata_str_hash (name) % table_size;
12097+
hash = g_str_hash (name) % table_size;
1209812098

1209912099
/* FIXME: Allocate from the mempool */
1210012100
new_entry = g_new0 (GlobalsTableEntry, 1);

src/mono/mono/mini/aot-runtime.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *valu
15451545
table_size = table [0];
15461546
table ++;
15471547

1548-
hash = mono_metadata_str_hash (symbol) % table_size;
1548+
hash = g_str_hash (symbol) % table_size;
15491549

15501550
entry = &table [hash * 2];
15511551

@@ -2716,9 +2716,9 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch
27162716
}
27172717
#ifdef DEBUG_AOT_NAME_TABLE
27182718
debug_full_name = g_strdup (full_name);
2719-
debug_hash = mono_metadata_str_hash (full_name) % table_size;
2719+
debug_hash = g_str_hash (full_name) % table_size;
27202720
#endif
2721-
hash = mono_metadata_str_hash (full_name) % table_size;
2721+
hash = g_str_hash (full_name) % table_size;
27222722
if (full_name != full_name_buf)
27232723
g_free (full_name);
27242724

0 commit comments

Comments
 (0)