Skip to content

Commit

Permalink
apply review feedback
Browse files Browse the repository at this point in the history
Move mono_jit_init_version and mono_droid_load_assembly to mono_droid_runtime_init
type safety around strings
  • Loading branch information
matouskozak committed Jan 31, 2025
1 parent a6a5832 commit d2f611e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/tasks/AndroidAppBuilder/Templates/MonoRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static void initializeRuntime(String entryPointLibName, Context context)
if (rv != 0) {
Log.e("DOTNET", "Failed to initialize runtime, return-code=" + rv);
freeNativeResources();
System.exit(rv);
}
}

Expand Down
36 changes: 19 additions & 17 deletions src/tasks/AndroidAppBuilder/Templates/monodroid-coreclr.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ Java_net_dot_MonoRunner_freeNativeResources (JNIEnv* env, jobject thiz);

/********* implementation *********/

static char* g_bundle_path = NULL;
static char* g_executable_path = NULL;
static const char* g_bundle_path = NULL;
static const char* g_executable_path = NULL;
static unsigned int g_coreclr_domainId = 0;
static void* g_coreclr_handle = NULL;

Expand Down Expand Up @@ -129,7 +129,7 @@ get_tpas_from_path(const char* dir_path, const char** tpas)
}

static int
bundle_executable_path (const char* executable, const char* bundle_path, char** executable_path)
bundle_executable_path (const char* executable, const char* bundle_path, const char** executable_path)
{
size_t executable_path_len = strlen(bundle_path) + strlen(executable) + 1; // +1 for '/'
char* temp_path = (char*)malloc(sizeof(char) * executable_path_len + 1); // +1 for '\0'
Expand Down Expand Up @@ -168,7 +168,7 @@ free_resources ()
}

static int
mono_droid_execute_assembly (const char* executable_path, int managed_argc, const char** managed_argv, void* coreclr_handle, unsigned int coreclr_domainId)
mono_droid_execute_assembly (const char* executable_path, void* coreclr_handle, unsigned int coreclr_domainId, int managed_argc, const char** managed_argv)
{
unsigned int rv;
LOG_INFO ("Calling coreclr_execute_assembly");
Expand All @@ -178,10 +178,16 @@ mono_droid_execute_assembly (const char* executable_path, int managed_argc, cons
}

static int
mono_droid_runtime_init (const char* bundle_path, const char* executable, const char* executable_path, int local_date_time_offset)
mono_droid_runtime_init (const char* bundle_path, const char* executable, int local_date_time_offset)
{
LOG_INFO ("mono_droid_runtime_init (CoreCLR) called with executable: %s", executable);

if (bundle_executable_path(executable, bundle_path, &g_executable_path) < 0)
{
LOG_ERROR("Failed to resolve full path for: %s", executable);
return -1;
}

chdir (bundle_path);

// TODO: set TRUSTED_PLATFORM_ASSEMBLIES, APP_PATHS and NATIVE_DLL_SEARCH_DIRECTORIES
Expand All @@ -203,7 +209,7 @@ mono_droid_runtime_init (const char* bundle_path, const char* executable, const

LOG_INFO ("Calling coreclr_initialize");
int rv = coreclr_initialize (
executable_path,
g_executable_path,
executable,
3,
appctx_keys,
Expand Down Expand Up @@ -239,25 +245,21 @@ Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_
strncpy_str (env, testresults_dir, j_testresults_dir, sizeof(testresults_dir));
strncpy_str (env, entryPointLibName, j_entryPointLibName, sizeof(entryPointLibName));

g_bundle_path = (char*)malloc(sizeof(char) * (strlen(file_dir) + 1)); // +1 for '\0'
if (g_bundle_path == NULL)
size_t file_dir_len = strlen(file_dir);
char* bundle_path_tmp = (char*)malloc(sizeof(char) * (file_dir_len + 1)); // +1 for '\0'
if (bundle_path_tmp == NULL)
{
LOG_ERROR("Failed to allocate memory for bundle_path");
return -1;
}
strncpy(g_bundle_path, file_dir, strlen(file_dir) + 1);

if (bundle_executable_path(entryPointLibName, g_bundle_path, &g_executable_path) < 0)
{
LOG_ERROR("Failed to resolve full path for: %s", entryPointLibName);
return -1;
}
strncpy(bundle_path_tmp, file_dir, file_dir_len + 1);
g_bundle_path = bundle_path_tmp;

setenv ("HOME", g_bundle_path, true);
setenv ("TMPDIR", cache_dir, true);
setenv ("TEST_RESULTS_DIR", testresults_dir, true);

return mono_droid_runtime_init (g_bundle_path, entryPointLibName, g_executable_path, current_local_time);
return mono_droid_runtime_init (g_bundle_path, entryPointLibName, current_local_time);
}

int
Expand Down Expand Up @@ -293,7 +295,7 @@ Java_net_dot_MonoRunner_execEntryPoint (JNIEnv* env, jobject thiz, jstring j_ent
managed_argv[i + 1] = (char*)((*env)->GetStringUTFChars(env, j_arg, NULL));
}

int rv = mono_droid_execute_assembly (g_executable_path, managed_argc, managed_argv, g_coreclr_handle, g_coreclr_domainId);
int rv = mono_droid_execute_assembly (g_executable_path, g_coreclr_handle, g_coreclr_domainId, managed_argc, managed_argv);

for (int i = 0; i < args_len; ++i)
{
Expand Down
67 changes: 37 additions & 30 deletions src/tasks/AndroidAppBuilder/Templates/monodroid.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ invoke_external_native_api (void (*callback)(void));

/********* implementation *********/

static char* g_bundle_path = NULL;
static char* g_executable = NULL;

static const char* g_bundle_path = NULL;
static MonoDomain* g_domain = NULL;
static MonoAssembly* g_assembly = NULL;

#define LOG_INFO(fmt, ...) __android_log_print(ANDROID_LOG_DEBUG, "DOTNET", fmt, ##__VA_ARGS__)
#define LOG_ERROR(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, "DOTNET", fmt, ##__VA_ARGS__)
Expand Down Expand Up @@ -231,8 +231,9 @@ cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data)
}

static int
mono_droid_runtime_init (const char* bundle_path, int local_date_time_offset)
mono_droid_runtime_init (const char* bundle_path, const char* executable, int local_date_time_offset)
{
LOG_INFO ("mono_droid_runtime_init (Mono) called with executable: %s", executable);
// NOTE: these options can be set via command line args for adb or xharness, see AndroidSampleApp.csproj

// uncomment for debug output:
Expand Down Expand Up @@ -316,6 +317,18 @@ mono_droid_runtime_init (const char* bundle_path, int local_date_time_offset)
#endif // FULL_AOT
#endif // FORCE_INTERPRETER

g_domain = mono_jit_init_version ("dotnet.android", "mobile");
if (g_domain == NULL) {
LOG_ERROR ("mono_jit_init_version failed");
return -1;
}

g_assembly = mono_droid_load_assembly (executable, NULL);
if (g_assembly == NULL) {
LOG_ERROR ("mono_droid_load_assembly failed");
return -1;
}

return rv;
}

Expand All @@ -327,23 +340,17 @@ free_resources ()
free (g_bundle_path);
g_bundle_path = NULL;
}
if (g_executable)
if (g_assembly)
{
free (g_executable);
g_executable = NULL;
mono_assembly_close (g_assembly);
g_assembly = NULL;
}
}

static int
mono_droid_execute_assembly (const char *executable, int managed_argc, char* managed_argv[])
{
MonoDomain *domain = mono_jit_init_version ("dotnet.android", "mobile");
assert (domain);

MonoAssembly *assembly = mono_droid_load_assembly (executable, NULL);
assert (assembly);

LOG_INFO ("Calling mono_jit_exec: %s", executable);
mono_droid_execute_assembly (MonoDomain* domain, MonoAssembly* assembly, int managed_argc, char* managed_argv[])
{
LOG_INFO ("Calling mono_jit_exec");
int rv = mono_jit_exec (domain, assembly, managed_argc, managed_argv);
LOG_INFO ("Exit code: %d.", rv);

Expand Down Expand Up @@ -386,40 +393,40 @@ Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_
strncpy_str (env, testresults_dir, j_testresults_dir, sizeof(testresults_dir));
strncpy_str (env, entryPointLibName, j_entryPointLibName, sizeof(entryPointLibName));

g_bundle_path = (char*)malloc(sizeof(char) * (strlen(file_dir) + 1)); // +1 for '\0'
if (g_bundle_path == NULL)
size_t file_dir_len = strlen(file_dir);
char* bundle_path_tmp = (char*)malloc(sizeof(char) * (file_dir_len + 1)); // +1 for '\0'
if (bundle_path_tmp == NULL)
{
LOG_ERROR("Failed to allocate memory for bundle_path");
return -1;
}
strncpy(g_bundle_path, file_dir, strlen(file_dir) + 1);

g_executable = (char*)malloc(sizeof(char) * (strlen(entryPointLibName) + 1)); // +1 for '\0'
if (g_executable == NULL)
{
LOG_ERROR("Failed to allocate memory for executable");
return -1;
}
strncpy(g_executable, entryPointLibName, strlen(entryPointLibName) + 1);
strncpy(bundle_path_tmp, file_dir, file_dir_len + 1);
g_bundle_path = bundle_path_tmp;

setenv ("HOME", g_bundle_path, true);
setenv ("TMPDIR", cache_dir, true);
setenv ("TEST_RESULTS_DIR", testresults_dir, true);

return mono_droid_runtime_init (g_bundle_path, current_local_time);
return mono_droid_runtime_init (g_bundle_path, entryPointLibName, current_local_time);
}

int
Java_net_dot_MonoRunner_execEntryPoint (JNIEnv* env, jobject thiz, jstring j_entryPointLibName, jobjectArray j_args)
{
LOG_INFO("Java_net_dot_MonoRunner_execEntryPoint (Mono):");

if ((g_bundle_path == NULL) || (g_executable == NULL))
if (g_bundle_path == NULL)
{
LOG_ERROR("Bundle path or executable name not set");
return -1;
}

if (g_domain == NULL || g_assembly == NULL)
{
LOG_ERROR("Mono domain or assembly not initialized");
return -1;
}

int args_len = (*env)->GetArrayLength(env, j_args);
int managed_argc = args_len + 1;
char** managed_argv = (char**)malloc(managed_argc * sizeof(char*));
Expand All @@ -430,7 +437,7 @@ Java_net_dot_MonoRunner_execEntryPoint (JNIEnv* env, jobject thiz, jstring j_ent
managed_argv[i + 1] = (char*)((*env)->GetStringUTFChars(env, j_arg, NULL));
}

int rv = mono_droid_execute_assembly (g_executable, managed_argc, managed_argv);
int rv = mono_droid_execute_assembly (g_domain, g_assembly, managed_argc, managed_argv);

for (int i = 0; i < args_len; ++i)
{
Expand Down

0 comments on commit d2f611e

Please sign in to comment.