Skip to content

Commit d2f611e

Browse files
committed
apply review feedback
Move mono_jit_init_version and mono_droid_load_assembly to mono_droid_runtime_init type safety around strings
1 parent a6a5832 commit d2f611e

File tree

3 files changed

+57
-47
lines changed

3 files changed

+57
-47
lines changed

src/tasks/AndroidAppBuilder/Templates/MonoRunner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public static void initializeRuntime(String entryPointLibName, Context context)
9696
if (rv != 0) {
9797
Log.e("DOTNET", "Failed to initialize runtime, return-code=" + rv);
9898
freeNativeResources();
99+
System.exit(rv);
99100
}
100101
}
101102

src/tasks/AndroidAppBuilder/Templates/monodroid-coreclr.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ Java_net_dot_MonoRunner_freeNativeResources (JNIEnv* env, jobject thiz);
3434

3535
/********* implementation *********/
3636

37-
static char* g_bundle_path = NULL;
38-
static char* g_executable_path = NULL;
37+
static const char* g_bundle_path = NULL;
38+
static const char* g_executable_path = NULL;
3939
static unsigned int g_coreclr_domainId = 0;
4040
static void* g_coreclr_handle = NULL;
4141

@@ -129,7 +129,7 @@ get_tpas_from_path(const char* dir_path, const char** tpas)
129129
}
130130

131131
static int
132-
bundle_executable_path (const char* executable, const char* bundle_path, char** executable_path)
132+
bundle_executable_path (const char* executable, const char* bundle_path, const char** executable_path)
133133
{
134134
size_t executable_path_len = strlen(bundle_path) + strlen(executable) + 1; // +1 for '/'
135135
char* temp_path = (char*)malloc(sizeof(char) * executable_path_len + 1); // +1 for '\0'
@@ -168,7 +168,7 @@ free_resources ()
168168
}
169169

170170
static int
171-
mono_droid_execute_assembly (const char* executable_path, int managed_argc, const char** managed_argv, void* coreclr_handle, unsigned int coreclr_domainId)
171+
mono_droid_execute_assembly (const char* executable_path, void* coreclr_handle, unsigned int coreclr_domainId, int managed_argc, const char** managed_argv)
172172
{
173173
unsigned int rv;
174174
LOG_INFO ("Calling coreclr_execute_assembly");
@@ -178,10 +178,16 @@ mono_droid_execute_assembly (const char* executable_path, int managed_argc, cons
178178
}
179179

180180
static int
181-
mono_droid_runtime_init (const char* bundle_path, const char* executable, const char* executable_path, int local_date_time_offset)
181+
mono_droid_runtime_init (const char* bundle_path, const char* executable, int local_date_time_offset)
182182
{
183183
LOG_INFO ("mono_droid_runtime_init (CoreCLR) called with executable: %s", executable);
184184

185+
if (bundle_executable_path(executable, bundle_path, &g_executable_path) < 0)
186+
{
187+
LOG_ERROR("Failed to resolve full path for: %s", executable);
188+
return -1;
189+
}
190+
185191
chdir (bundle_path);
186192

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

204210
LOG_INFO ("Calling coreclr_initialize");
205211
int rv = coreclr_initialize (
206-
executable_path,
212+
g_executable_path,
207213
executable,
208214
3,
209215
appctx_keys,
@@ -239,25 +245,21 @@ Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_
239245
strncpy_str (env, testresults_dir, j_testresults_dir, sizeof(testresults_dir));
240246
strncpy_str (env, entryPointLibName, j_entryPointLibName, sizeof(entryPointLibName));
241247

242-
g_bundle_path = (char*)malloc(sizeof(char) * (strlen(file_dir) + 1)); // +1 for '\0'
243-
if (g_bundle_path == NULL)
248+
size_t file_dir_len = strlen(file_dir);
249+
char* bundle_path_tmp = (char*)malloc(sizeof(char) * (file_dir_len + 1)); // +1 for '\0'
250+
if (bundle_path_tmp == NULL)
244251
{
245252
LOG_ERROR("Failed to allocate memory for bundle_path");
246253
return -1;
247254
}
248-
strncpy(g_bundle_path, file_dir, strlen(file_dir) + 1);
249-
250-
if (bundle_executable_path(entryPointLibName, g_bundle_path, &g_executable_path) < 0)
251-
{
252-
LOG_ERROR("Failed to resolve full path for: %s", entryPointLibName);
253-
return -1;
254-
}
255+
strncpy(bundle_path_tmp, file_dir, file_dir_len + 1);
256+
g_bundle_path = bundle_path_tmp;
255257

256258
setenv ("HOME", g_bundle_path, true);
257259
setenv ("TMPDIR", cache_dir, true);
258260
setenv ("TEST_RESULTS_DIR", testresults_dir, true);
259261

260-
return mono_droid_runtime_init (g_bundle_path, entryPointLibName, g_executable_path, current_local_time);
262+
return mono_droid_runtime_init (g_bundle_path, entryPointLibName, current_local_time);
261263
}
262264

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

296-
int rv = mono_droid_execute_assembly (g_executable_path, managed_argc, managed_argv, g_coreclr_handle, g_coreclr_domainId);
298+
int rv = mono_droid_execute_assembly (g_executable_path, g_coreclr_handle, g_coreclr_domainId, managed_argc, managed_argv);
297299

298300
for (int i = 0; i < args_len; ++i)
299301
{

src/tasks/AndroidAppBuilder/Templates/monodroid.c

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ invoke_external_native_api (void (*callback)(void));
4848

4949
/********* implementation *********/
5050

51-
static char* g_bundle_path = NULL;
52-
static char* g_executable = NULL;
53-
51+
static const char* g_bundle_path = NULL;
52+
static MonoDomain* g_domain = NULL;
53+
static MonoAssembly* g_assembly = NULL;
5454

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

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

238239
// uncomment for debug output:
@@ -316,6 +317,18 @@ mono_droid_runtime_init (const char* bundle_path, int local_date_time_offset)
316317
#endif // FULL_AOT
317318
#endif // FORCE_INTERPRETER
318319

320+
g_domain = mono_jit_init_version ("dotnet.android", "mobile");
321+
if (g_domain == NULL) {
322+
LOG_ERROR ("mono_jit_init_version failed");
323+
return -1;
324+
}
325+
326+
g_assembly = mono_droid_load_assembly (executable, NULL);
327+
if (g_assembly == NULL) {
328+
LOG_ERROR ("mono_droid_load_assembly failed");
329+
return -1;
330+
}
331+
319332
return rv;
320333
}
321334

@@ -327,23 +340,17 @@ free_resources ()
327340
free (g_bundle_path);
328341
g_bundle_path = NULL;
329342
}
330-
if (g_executable)
343+
if (g_assembly)
331344
{
332-
free (g_executable);
333-
g_executable = NULL;
345+
mono_assembly_close (g_assembly);
346+
g_assembly = NULL;
334347
}
335348
}
336349

337350
static int
338-
mono_droid_execute_assembly (const char *executable, int managed_argc, char* managed_argv[])
339-
{
340-
MonoDomain *domain = mono_jit_init_version ("dotnet.android", "mobile");
341-
assert (domain);
342-
343-
MonoAssembly *assembly = mono_droid_load_assembly (executable, NULL);
344-
assert (assembly);
345-
346-
LOG_INFO ("Calling mono_jit_exec: %s", executable);
351+
mono_droid_execute_assembly (MonoDomain* domain, MonoAssembly* assembly, int managed_argc, char* managed_argv[])
352+
{
353+
LOG_INFO ("Calling mono_jit_exec");
347354
int rv = mono_jit_exec (domain, assembly, managed_argc, managed_argv);
348355
LOG_INFO ("Exit code: %d.", rv);
349356

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

389-
g_bundle_path = (char*)malloc(sizeof(char) * (strlen(file_dir) + 1)); // +1 for '\0'
390-
if (g_bundle_path == NULL)
396+
size_t file_dir_len = strlen(file_dir);
397+
char* bundle_path_tmp = (char*)malloc(sizeof(char) * (file_dir_len + 1)); // +1 for '\0'
398+
if (bundle_path_tmp == NULL)
391399
{
392400
LOG_ERROR("Failed to allocate memory for bundle_path");
393401
return -1;
394402
}
395-
strncpy(g_bundle_path, file_dir, strlen(file_dir) + 1);
396-
397-
g_executable = (char*)malloc(sizeof(char) * (strlen(entryPointLibName) + 1)); // +1 for '\0'
398-
if (g_executable == NULL)
399-
{
400-
LOG_ERROR("Failed to allocate memory for executable");
401-
return -1;
402-
}
403-
strncpy(g_executable, entryPointLibName, strlen(entryPointLibName) + 1);
403+
strncpy(bundle_path_tmp, file_dir, file_dir_len + 1);
404+
g_bundle_path = bundle_path_tmp;
404405

405406
setenv ("HOME", g_bundle_path, true);
406407
setenv ("TMPDIR", cache_dir, true);
407408
setenv ("TEST_RESULTS_DIR", testresults_dir, true);
408409

409-
return mono_droid_runtime_init (g_bundle_path, current_local_time);
410+
return mono_droid_runtime_init (g_bundle_path, entryPointLibName, current_local_time);
410411
}
411412

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

417-
if ((g_bundle_path == NULL) || (g_executable == NULL))
418+
if (g_bundle_path == NULL)
418419
{
419420
LOG_ERROR("Bundle path or executable name not set");
420421
return -1;
421422
}
422423

424+
if (g_domain == NULL || g_assembly == NULL)
425+
{
426+
LOG_ERROR("Mono domain or assembly not initialized");
427+
return -1;
428+
}
429+
423430
int args_len = (*env)->GetArrayLength(env, j_args);
424431
int managed_argc = args_len + 1;
425432
char** managed_argv = (char**)malloc(managed_argc * sizeof(char*));
@@ -430,7 +437,7 @@ Java_net_dot_MonoRunner_execEntryPoint (JNIEnv* env, jobject thiz, jstring j_ent
430437
managed_argv[i + 1] = (char*)((*env)->GetStringUTFChars(env, j_arg, NULL));
431438
}
432439

433-
int rv = mono_droid_execute_assembly (g_executable, managed_argc, managed_argv);
440+
int rv = mono_droid_execute_assembly (g_domain, g_assembly, managed_argc, managed_argv);
434441

435442
for (int i = 0; i < args_len; ++i)
436443
{

0 commit comments

Comments
 (0)