Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mono][wasm] Refactor mono_fixup_symbol_name #101681

Merged
merged 12 commits into from
May 9, 2024
4 changes: 2 additions & 2 deletions src/mono/browser/runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
int mono_wasm_enable_gc = 1;

/* Missing from public headers */
char *mono_fixup_symbol_name (char *key);
char *mono_fixup_symbol_name (const char *prefix, const char *key, const char *suffix);
void mono_icall_table_init (void);
void mono_wasm_enable_debugging (int);
void mono_ee_interp_init (const char *opts);
Expand Down Expand Up @@ -214,7 +214,7 @@ get_native_to_interp (MonoMethod *method, void *extra_arg)

assert (strlen (name) < 100);
snprintf (key, sizeof(key), "%s_%s_%s", name, class_name, method_name);
char* fixedName = mono_fixup_symbol_name(key);
char* fixedName = mono_fixup_symbol_name("", key, "");
addr = wasm_dl_get_native_to_interp (fixedName, extra_arg);
MONO_EXIT_GC_UNSAFE;
return addr;
Expand Down
31 changes: 27 additions & 4 deletions src/mono/mono/metadata/native-library.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,11 +1223,32 @@ mono_loader_install_pinvoke_override (PInvokeOverrideFn override_fn)
pinvoke_override = override_fn;
}

static int mono_precompute_size (const char *key)
{
int size = 1; // Null terminator
int len = (int)strlen (key);
mkhamoyan marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < len; ++i) {
mkhamoyan marked this conversation as resolved.
Show resolved Hide resolved
unsigned char b = key[i];
if ((b >= '0' && b <= '9') ||
(b >= 'a' && b <= 'z') ||
(b >= 'A' && b <= 'Z') ||
b == '_' || b == '.' || b == '-' || b == '+' || b == '<' || b == '>') {
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
size++;
}
else {
size += 4;
}
}
return size;
}

// Keep synced with FixupSymbolName from src/tasks/Common/Utils.cs
char* mono_fixup_symbol_name (char *key) {
char* fixedName = malloc(256);
char* mono_fixup_symbol_name (const char *prefix, const char *key, const char *suffix) {
int size = mono_precompute_size (key) + strlen (prefix) + strlen (suffix);
mkhamoyan marked this conversation as resolved.
Show resolved Hide resolved
char fixedName[size];
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
int sb_index = 0;
int len = (int)strlen (key);
strcpy(fixedName, prefix);

for (int i = 0; i < len; ++i) {
mkhamoyan marked this conversation as resolved.
Show resolved Hide resolved
unsigned char b = key[i];
Expand All @@ -1242,12 +1263,14 @@ char* mono_fixup_symbol_name (char *key) {
}
else {
// Append the hexadecimal representation of b between underscores
sprintf(&fixedName[sb_index], "_%X_", b);
snprintf(&fixedName[sb_index], sizeof(fixedName) - sb_index - 1, "_%X_", b);
sb_index += 4; // Move the index after the appended hexadecimal characters
}
}

strcpy(&fixedName[sb_index], suffix);

// Null-terminate the fixedName string
fixedName[sb_index] = '\0';
fixedName[size-1] = '\0';
return fixedName;
}
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/native-library.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ void
mono_loader_install_pinvoke_override (PInvokeOverrideFn override_fn);

char *
mono_fixup_symbol_name (char *key);
mono_fixup_symbol_name (const char *prefix, const char *key, const char *suffix);

#endif
28 changes: 4 additions & 24 deletions src/mono/mono/mini/aot-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -12407,25 +12407,15 @@ emit_file_info (MonoAotCompile *acfg)
init_aot_file_info (acfg, info);

if (acfg->aot_opts.static_link) {
char symbol [MAX_SYMBOL_SIZE];
char prefix [MAX_SYMBOL_SIZE];

/*
* Emit a global symbol which can be passed by an embedding app to
* mono_aot_register_module (). The symbol points to a pointer to the file info
* structure.
*/
sprintf (symbol, "%smono_aot_module_%s_info", acfg->user_symbol_prefix, acfg->image->assembly->aname.name);
#ifdef TARGET_WASM
acfg->static_linking_symbol = g_strdup (mono_fixup_symbol_name(symbol));
#else
/* Get rid of characters which cannot occur in symbols */
char *p = symbol;
for (p = symbol; *p; ++p) {
if (!(isalnum (*p) || *p == '_'))
*p = '_';
}
acfg->static_linking_symbol = g_strdup (symbol);
#endif
sprintf (prefix, "%smono_aot_module_", acfg->user_symbol_prefix);
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
acfg->static_linking_symbol = g_strdup (mono_fixup_symbol_name(prefix, acfg->image->assembly->aname.name, "_info"));
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
}

if (acfg->llvm)
Expand Down Expand Up @@ -15122,17 +15112,7 @@ aot_assembly (MonoAssembly *ass, guint32 jit_opts, MonoAotOptions *aot_options)
acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_LLVM_ONLY);

acfg->assembly_name_sym = g_strdup (get_assembly_prefix (acfg->image));
#ifdef TARGET_WASM
acfg->global_prefix = g_strdup_printf ("mono_aot_%s", g_strdup(mono_fixup_symbol_name (acfg->assembly_name_sym)));
#else
mkhamoyan marked this conversation as resolved.
Show resolved Hide resolved
char *p;
/* Get rid of characters which cannot occur in symbols */
for (p = acfg->assembly_name_sym; *p; ++p) {
if (!(isalnum (*p) || *p == '_'))
*p = '_';
}
acfg->global_prefix = g_strdup_printf ("mono_aot_%s", acfg->assembly_name_sym);
#endif
acfg->global_prefix = mono_fixup_symbol_name("mono_aot_", acfg->assembly_name_sym, "");
acfg->plt_symbol = g_strdup_printf ("%s_plt", acfg->global_prefix);
acfg->got_symbol = g_strdup_printf ("%s_got", acfg->global_prefix);
if (acfg->llvm) {
Expand Down
15 changes: 1 addition & 14 deletions src/mono/mono/mini/mini-llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -14542,22 +14542,9 @@ emit_aot_file_info (MonoLLVMModule *module)
LLVMSetInitializer (info_var, LLVMConstNamedStruct (module->info_var_type, fields, nfields));

if (module->static_link) {
char *s;
LLVMValueRef var;

s = g_strdup_printf ("mono_aot_module_%s_info", module->assembly->aname.name);
#ifdef TARGET_WASM
var = LLVMAddGlobal (module->lmodule, pointer_type (LLVMInt8Type ()), g_strdup (mono_fixup_symbol_name(s)));
#else
/* Get rid of characters which cannot occur in symbols */
char *p = s;
for (p = s; *p; ++p) {
if (!(isalnum (*p) || *p == '_'))
*p = '_';
}
var = LLVMAddGlobal (module->lmodule, pointer_type (LLVMInt8Type ()), s);
#endif
g_free (s);
var = LLVMAddGlobal (module->lmodule, pointer_type (LLVMInt8Type ()), g_strdup (mono_fixup_symbol_name("mono_aot_module_", module->assembly->aname.name, "_info")));
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
LLVMSetInitializer (var, LLVMConstBitCast (LLVMGetNamedGlobal (module->lmodule, "mono_aot_file_info"), pointer_type (LLVMInt8Type ())));
LLVMSetLinkage (var, LLVMExternalLinkage);
}
Expand Down