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
5 changes: 3 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,8 +214,9 @@ 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);
free (fixedName);
MONO_EXIT_GC_UNSAFE;
return addr;
}
Expand Down
69 changes: 50 additions & 19 deletions src/mono/mono/metadata/native-library.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,31 +1223,62 @@ mono_loader_install_pinvoke_override (PInvokeOverrideFn override_fn)
pinvoke_override = override_fn;
}

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

static gboolean
is_symbol_char_underscore (unsigned char c)
{
switch (c) {
case '_':
case '.':
case '-':
case '+':
case '<':
case '>':
return TRUE;
default:
return FALSE;
}
}

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 == '_')) {
fixedName[sb_index++] = b;
}
else if (b == '.' || b == '-' || b == '+' || b == '<' || b == '>') {
fixedName[sb_index++] = '_';
if (is_symbol_char_verbatim (b) || is_symbol_char_underscore (b)) {
size++;
}
else {
// Append the hexadecimal representation of b between underscores
sprintf(&fixedName[sb_index], "_%X_", b);
sb_index += 4; // Move the index after the appended hexadecimal characters
size += 4;
}
}
return size;
}

// Null-terminate the fixedName string
fixedName[sb_index] = '\0';
return fixedName;
// Keep synced with FixupSymbolName from src/tasks/Common/Utils.cs
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
GString *str = g_string_sized_new (size);
int len = (int)strlen (key);
g_string_append_printf (str, "%s", prefix);

for (int i = 0; i < len; ++i) {
mkhamoyan marked this conversation as resolved.
Show resolved Hide resolved
unsigned char b = key[i];
if (is_symbol_char_verbatim (b)) {
g_string_append_c (str, b);
} else if (is_symbol_char_underscore (b)) {
g_string_append_c (str, '_');
} else {
// Append the hex representation of b between underscores
g_string_append_printf (str, "_%X_", b);
}
}
g_string_append_printf (str, "%s", suffix);
return g_string_free (str, FALSE);
}
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
snprintf (prefix, MAX_SYMBOL_SIZE, "%smono_aot_module_", acfg->user_symbol_prefix);
acfg->static_linking_symbol = mono_fixup_symbol_name (prefix, acfg->image->assembly->aname.name, "_info");
}

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, "");
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
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
18 changes: 3 additions & 15 deletions src/mono/mono/mini/mini-llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -14542,22 +14542,10 @@ 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);
char *fixedName = mono_fixup_symbol_name ("mono_aot_module_", module->assembly->aname.name, "_info");
var = LLVMAddGlobal (module->lmodule, pointer_type (LLVMInt8Type ()), fixedName);
free (fixedName);
LLVMSetInitializer (var, LLVMConstBitCast (LLVMGetNamedGlobal (module->lmodule, "mono_aot_file_info"), pointer_type (LLVMInt8Type ())));
LLVMSetLinkage (var, LLVMExternalLinkage);
}
Expand Down