Skip to content

Commit ce2364c

Browse files
authored
[mono][wasm] Refactor mono_fixup_symbol_name (dotnet#101681)
Refactor mono_fixup_symbol_name, apply @lambdageek's feedback
1 parent 9be6287 commit ce2364c

File tree

6 files changed

+64
-62
lines changed

6 files changed

+64
-62
lines changed

src/mono/browser/runtime/runtime.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
int mono_wasm_enable_gc = 1;
6666

6767
/* Missing from public headers */
68-
char *mono_fixup_symbol_name (char *key);
68+
char *mono_fixup_symbol_name (const char *prefix, const char *key, const char *suffix);
6969
void mono_icall_table_init (void);
7070
void mono_wasm_enable_debugging (int);
7171
void mono_ee_interp_init (const char *opts);
@@ -214,8 +214,9 @@ get_native_to_interp (MonoMethod *method, void *extra_arg)
214214

215215
assert (strlen (name) < 100);
216216
snprintf (key, sizeof(key), "%s_%s_%s", name, class_name, method_name);
217-
char* fixedName = mono_fixup_symbol_name(key);
217+
char *fixedName = mono_fixup_symbol_name ("", key, "");
218218
addr = wasm_dl_get_native_to_interp (fixedName, extra_arg);
219+
free (fixedName);
219220
MONO_EXIT_GC_UNSAFE;
220221
return addr;
221222
}

src/mono/mono/metadata/native-library.c

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,31 +1223,62 @@ mono_loader_install_pinvoke_override (PInvokeOverrideFn override_fn)
12231223
pinvoke_override = override_fn;
12241224
}
12251225

1226-
// Keep synced with FixupSymbolName from src/tasks/Common/Utils.cs
1227-
char* mono_fixup_symbol_name (char *key) {
1228-
char* fixedName = malloc(256);
1229-
int sb_index = 0;
1230-
int len = (int)strlen (key);
1226+
static gboolean
1227+
is_symbol_char_verbatim (unsigned char b)
1228+
{
1229+
return ((b >= '0' && b <= '9') || (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z'));
1230+
}
12311231

1232-
for (int i = 0; i < len; ++i) {
1232+
static gboolean
1233+
is_symbol_char_underscore (unsigned char c)
1234+
{
1235+
switch (c) {
1236+
case '_':
1237+
case '.':
1238+
case '-':
1239+
case '+':
1240+
case '<':
1241+
case '>':
1242+
return TRUE;
1243+
default:
1244+
return FALSE;
1245+
}
1246+
}
1247+
1248+
static size_t mono_precompute_size (const char *key)
1249+
{
1250+
size_t size = 1; // Null terminator
1251+
size_t len = (int)strlen (key);
1252+
for (size_t i = 0; i < len; ++i) {
12331253
unsigned char b = key[i];
1234-
if ((b >= '0' && b <= '9') ||
1235-
(b >= 'a' && b <= 'z') ||
1236-
(b >= 'A' && b <= 'Z') ||
1237-
(b == '_')) {
1238-
fixedName[sb_index++] = b;
1239-
}
1240-
else if (b == '.' || b == '-' || b == '+' || b == '<' || b == '>') {
1241-
fixedName[sb_index++] = '_';
1254+
if (is_symbol_char_verbatim (b) || is_symbol_char_underscore (b)) {
1255+
size++;
12421256
}
12431257
else {
1244-
// Append the hexadecimal representation of b between underscores
1245-
sprintf(&fixedName[sb_index], "_%X_", b);
1246-
sb_index += 4; // Move the index after the appended hexadecimal characters
1258+
size += 4;
12471259
}
12481260
}
1261+
return size;
1262+
}
12491263

1250-
// Null-terminate the fixedName string
1251-
fixedName[sb_index] = '\0';
1252-
return fixedName;
1264+
// Keep synced with FixupSymbolName from src/tasks/Common/Utils.cs
1265+
char* mono_fixup_symbol_name (const char *prefix, const char *key, const char *suffix) {
1266+
size_t size = mono_precompute_size (key) + strlen (prefix) + strlen (suffix);
1267+
GString *str = g_string_sized_new (size);
1268+
size_t len = (int)strlen (key);
1269+
g_string_append_printf (str, "%s", prefix);
1270+
1271+
for (size_t i = 0; i < len; ++i) {
1272+
unsigned char b = key[i];
1273+
if (is_symbol_char_verbatim (b)) {
1274+
g_string_append_c (str, b);
1275+
} else if (is_symbol_char_underscore (b)) {
1276+
g_string_append_c (str, '_');
1277+
} else {
1278+
// Append the hex representation of b between underscores
1279+
g_string_append_printf (str, "_%X_", b);
1280+
}
1281+
}
1282+
g_string_append_printf (str, "%s", suffix);
1283+
return g_string_free (str, FALSE);
12531284
}

src/mono/mono/metadata/native-library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ void
3636
mono_loader_install_pinvoke_override (PInvokeOverrideFn override_fn);
3737

3838
char *
39-
mono_fixup_symbol_name (char *key);
39+
mono_fixup_symbol_name (const char *prefix, const char *key, const char *suffix);
4040

4141
#endif

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12414,18 +12414,8 @@ emit_file_info (MonoAotCompile *acfg)
1241412414
* mono_aot_register_module (). The symbol points to a pointer to the file info
1241512415
* structure.
1241612416
*/
12417-
sprintf (symbol, "%smono_aot_module_%s_info", acfg->user_symbol_prefix, acfg->image->assembly->aname.name);
12418-
#ifdef TARGET_WASM
12419-
acfg->static_linking_symbol = g_strdup (mono_fixup_symbol_name(symbol));
12420-
#else
12421-
/* Get rid of characters which cannot occur in symbols */
12422-
char *p = symbol;
12423-
for (p = symbol; *p; ++p) {
12424-
if (!(isalnum (*p) || *p == '_'))
12425-
*p = '_';
12426-
}
12427-
acfg->static_linking_symbol = g_strdup (symbol);
12428-
#endif
12417+
snprintf (symbol, MAX_SYMBOL_SIZE, "%smono_aot_module_%s", acfg->user_symbol_prefix, acfg->image->assembly->aname.name);
12418+
acfg->static_linking_symbol = mono_fixup_symbol_name ("", symbol, "_info");
1242912419
}
1243012420

1243112421
if (acfg->llvm)
@@ -14294,6 +14284,8 @@ acfg_free (MonoAotCompile *acfg)
1429414284
g_free (acfg->static_linking_symbol);
1429514285
g_free (acfg->got_symbol);
1429614286
g_free (acfg->plt_symbol);
14287+
g_free (acfg->global_prefix);
14288+
g_free (acfg->assembly_name_sym);
1429714289
g_ptr_array_free (acfg->methods, TRUE);
1429814290
g_ptr_array_free (acfg->image_table, TRUE);
1429914291
g_ptr_array_free (acfg->globals, TRUE);
@@ -15121,18 +15113,8 @@ aot_assembly (MonoAssembly *ass, guint32 jit_opts, MonoAotOptions *aot_options)
1512115113
if (acfg->aot_opts.llvm_only)
1512215114
acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_LLVM_ONLY);
1512315115

15124-
acfg->assembly_name_sym = g_strdup (get_assembly_prefix (acfg->image));
15125-
#ifdef TARGET_WASM
15126-
acfg->global_prefix = g_strdup_printf ("mono_aot_%s", g_strdup(mono_fixup_symbol_name (acfg->assembly_name_sym)));
15127-
#else
15128-
char *p;
15129-
/* Get rid of characters which cannot occur in symbols */
15130-
for (p = acfg->assembly_name_sym; *p; ++p) {
15131-
if (!(isalnum (*p) || *p == '_'))
15132-
*p = '_';
15133-
}
15116+
acfg->assembly_name_sym = mono_fixup_symbol_name ("", get_assembly_prefix (acfg->image), "");
1513415117
acfg->global_prefix = g_strdup_printf ("mono_aot_%s", acfg->assembly_name_sym);
15135-
#endif
1513615118
acfg->plt_symbol = g_strdup_printf ("%s_plt", acfg->global_prefix);
1513715119
acfg->got_symbol = g_strdup_printf ("%s_got", acfg->global_prefix);
1513815120
if (acfg->llvm) {

src/mono/mono/mini/mini-llvm.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14542,22 +14542,10 @@ emit_aot_file_info (MonoLLVMModule *module)
1454214542
LLVMSetInitializer (info_var, LLVMConstNamedStruct (module->info_var_type, fields, nfields));
1454314543

1454414544
if (module->static_link) {
14545-
char *s;
1454614545
LLVMValueRef var;
14547-
14548-
s = g_strdup_printf ("mono_aot_module_%s_info", module->assembly->aname.name);
14549-
#ifdef TARGET_WASM
14550-
var = LLVMAddGlobal (module->lmodule, pointer_type (LLVMInt8Type ()), g_strdup (mono_fixup_symbol_name(s)));
14551-
#else
14552-
/* Get rid of characters which cannot occur in symbols */
14553-
char *p = s;
14554-
for (p = s; *p; ++p) {
14555-
if (!(isalnum (*p) || *p == '_'))
14556-
*p = '_';
14557-
}
14558-
var = LLVMAddGlobal (module->lmodule, pointer_type (LLVMInt8Type ()), s);
14559-
#endif
14560-
g_free (s);
14546+
char *fixedName = mono_fixup_symbol_name ("mono_aot_module_", module->assembly->aname.name, "_info");
14547+
var = LLVMAddGlobal (module->lmodule, pointer_type (LLVMInt8Type ()), fixedName);
14548+
free (fixedName);
1456114549
LLVMSetInitializer (var, LLVMConstBitCast (LLVMGetNamedGlobal (module->lmodule, "mono_aot_file_info"), pointer_type (LLVMInt8Type ())));
1456214550
LLVMSetLinkage (var, LLVMExternalLinkage);
1456314551
}

src/tasks/AotCompilerTask/MonoAOTCompiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ private PrecompileArguments GetPrecompileArgumentsFor(ITaskItem assemblyItem, st
752752

753753
if (CollectTrimmingEligibleMethods)
754754
{
755-
string assemblyName = assemblyFilename.Replace(".", "_");
755+
string assemblyName = FixupSymbolName(assemblyFilename);
756756
string outputFileName = assemblyName + "_compiled_methods.txt";
757757
string outputFilePath;
758758
if (string.IsNullOrEmpty(TrimmingEligibleMethodsOutputDirectory))

0 commit comments

Comments
 (0)