Skip to content

Commit 172ecdf

Browse files
authored
[mono] CodeQL fix set #1 (dotnet#99637)
CodeQL flagged various places where we're dereferencing pointers that could be NULL, this PR systematically cleans some of them up via g_assert. * g_assert result of g_build_path calls * Allocation failure handling * mono_class_inflate_generic_class_checked can return NULL
1 parent f529d5d commit 172ecdf

14 files changed

+110
-71
lines changed

src/mono/mono/eglib/gfile-posix.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **gerror)
142142
}
143143

144144
t = g_build_filename (g_get_tmp_dir (), tmpl, (const char*)NULL);
145+
g_assert (t);
145146

146147
#ifdef HOST_WASI
147148
g_critical ("g_file_open_tmp is not implemented for WASI");

src/mono/mono/metadata/appdomain.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ try_load_from (MonoAssembly **assembly,
612612

613613
*assembly = NULL;
614614
fullpath = g_build_filename (path1, path2, path3, path4, (const char*)NULL);
615+
g_assert (fullpath);
615616

616617
found = g_file_test (fullpath, G_FILE_TEST_IS_REGULAR);
617618

src/mono/mono/metadata/assembly.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ load_in_path (const char *basename, const char** search_path, const MonoAssembly
336336

337337
for (i = 0; search_path [i]; ++i) {
338338
fullpath = g_build_filename (search_path [i], basename, (const char*)NULL);
339+
g_assert (fullpath);
339340
result = mono_assembly_request_open (fullpath, req, status);
340341
g_free (fullpath);
341342
if (result)
@@ -1407,6 +1408,7 @@ absolute_dir (const gchar *filename)
14071408

14081409
cwd = g_get_current_dir ();
14091410
mixed = g_build_filename (cwd, filename, (const char*)NULL);
1411+
g_assert (mixed);
14101412
parts = g_strsplit (mixed, G_DIR_SEPARATOR_S, 0);
14111413
g_free (mixed);
14121414
g_free (cwd);

src/mono/mono/metadata/class.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6785,10 +6785,13 @@ mono_method_get_base_method (MonoMethod *method, gboolean definition, MonoError
67856785
if (mono_class_is_open_constructed_type (m_class_get_byval_arg (parent))) {
67866786
parent = mono_class_inflate_generic_class_checked (parent, generic_inst, error);
67876787
return_val_if_nok (error, NULL);
6788+
g_assert (parent);
67886789
}
6790+
67896791
if (mono_class_is_ginst (parent)) {
67906792
parent_inst = mono_class_get_context (parent);
67916793
parent = mono_class_get_generic_class (parent)->container_class;
6794+
g_assert (parent);
67926795
}
67936796

67946797
mono_class_setup_vtable (parent);
@@ -6808,6 +6811,7 @@ mono_method_get_base_method (MonoMethod *method, gboolean definition, MonoError
68086811
if (mono_class_is_open_constructed_type (m_class_get_byval_arg (klass))) {
68096812
klass = mono_class_inflate_generic_class_checked (klass, generic_inst, error);
68106813
return_val_if_nok (error, NULL);
6814+
g_assert (klass);
68116815

68126816
generic_inst = NULL;
68136817
}
@@ -6821,6 +6825,7 @@ mono_method_get_base_method (MonoMethod *method, gboolean definition, MonoError
68216825
if (generic_inst) {
68226826
klass = mono_class_inflate_generic_class_checked (klass, generic_inst, error);
68236827
return_val_if_nok (error, NULL);
6828+
g_assert (klass);
68246829
generic_inst = NULL;
68256830
}
68266831

@@ -6909,7 +6914,7 @@ mono_class_has_default_constructor (MonoClass *klass, gboolean public_only)
69096914
* \param klass class in which the failure was detected
69106915
* \param fmt \c printf -style error message string.
69116916
*
6912-
* Sets a deferred failure in the class and prints a warning message.
6917+
* Sets a deferred failure in the class and prints a warning message.
69136918
* The deferred failure allows the runtime to attempt setting up the class layout at runtime.
69146919
*
69156920
* LOCKING: Acquires the loader lock.

src/mono/mono/metadata/icall.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4564,6 +4564,7 @@ ves_icall_System_Reflection_RuntimeAssembly_GetInfo (MonoQCallAssemblyHandle ass
45644564
else
45654565
absolute = g_build_filename (assembly->basedir, filename, (const char*)NULL);
45664566

4567+
g_assert (absolute);
45674568
mono_icall_make_platform_path (absolute);
45684569

45694570
const gchar *prepend = mono_icall_get_file_path_prefix (absolute);

src/mono/mono/metadata/image.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,7 @@ mono_image_load_file_for_image_checked (MonoImage *image, uint32_t fileidx, Mono
25112511
fname = mono_metadata_string_heap (image, fname_id);
25122512
base_dir = g_path_get_dirname (image->name);
25132513
name = g_build_filename (base_dir, fname, (const char*)NULL);
2514+
g_assert (name);
25142515
res = mono_image_open (name, NULL);
25152516
if (!res)
25162517
goto done;

src/mono/mono/metadata/object.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4243,6 +4243,8 @@ prepare_run_main (MonoMethod *method, int argc, char *argv[])
42434243
basename,
42444244
(const char*)NULL);
42454245

4246+
g_assert (fullpath);
4247+
42464248
utf8_fullpath = utf8_from_external (fullpath);
42474249
if(utf8_fullpath == NULL) {
42484250
/* Printing the arg text will cause glib to
@@ -5355,7 +5357,7 @@ MonoObjectHandle
53555357
mono_object_new_handle (MonoClass *klass, MonoError *error)
53565358
{
53575359
MONO_REQ_GC_UNSAFE_MODE;
5358-
5360+
53595361
if (MONO_CLASS_IS_IMPORT(klass)) {
53605362
mono_error_set_not_supported (error, "Built-in COM interop is not supported on Mono.");
53615363
return MONO_HANDLE_NEW (MonoObject, NULL);

src/mono/mono/metadata/sgen-tarjan-bridge.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,10 @@ create_scc (ScanData *data)
819819
g_error ("Invalid state when building SCC %d", other->state);
820820
}
821821

822-
if (other->is_bridge)
822+
if (other->is_bridge) {
823+
g_assert (color_data);
823824
dyn_array_ptr_add (&color_data->bridges, other->obj);
825+
}
824826

825827
// Maybe we should make sure we are not adding duplicates here. It is not really a problem
826828
// since we will get rid of duplicates before submitting the SCCs to the client in gather_xrefs

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5508,10 +5508,10 @@ MONO_RESTORE_WARNING
55085508
if (decoded_args->named_args_info [j].field && !strcmp (decoded_args->named_args_info [j].field->name, "EntryPoint")) {
55095509
named = (const char *)decoded_args->named_args[j]->value.primitive;
55105510
slen = mono_metadata_decode_value (named, &named);
5511-
5511+
55125512
int prefix_len = (int)strlen (acfg->user_symbol_prefix);
55135513
g_assert (prefix_len < 2);
5514-
5514+
55155515
export_name = (char *)g_malloc (prefix_len + slen + 1);
55165516
if (prefix_len == 1)
55175517
export_name[0] = *acfg->user_symbol_prefix;
@@ -5851,12 +5851,14 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
58515851

58525852
icomparable_inst = mono_class_inflate_generic_class_checked (icomparable, &ctx, error);
58535853
mono_error_assert_ok (error); /* FIXME don't swallow the error */
5854+
g_assert (icomparable_inst);
58545855

58555856
if (mono_class_is_assignable_from_internal (icomparable_inst, tclass)) {
58565857
MonoClass *gcomparer_inst;
58575858
gcomparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericComparer`1");
58585859
gcomparer_inst = mono_class_inflate_generic_class_checked (gcomparer, &ctx, error);
58595860
mono_error_assert_ok (error); /* FIXME don't swallow the error */
5861+
g_assert (gcomparer_inst);
58605862

58615863
add_generic_class (acfg, gcomparer_inst, FALSE, "Comparer<T>");
58625864
}
@@ -5878,13 +5880,15 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
58785880

58795881
iface_inst = mono_class_inflate_generic_class_checked (iface, &ctx, error);
58805882
mono_error_assert_ok (error); /* FIXME don't swallow the error */
5883+
g_assert (iface_inst);
58815884

58825885
if (mono_class_is_assignable_from_internal (iface_inst, tclass)) {
58835886
MonoClass *gcomparer_inst;
58845887

58855888
gcomparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericEqualityComparer`1");
58865889
gcomparer_inst = mono_class_inflate_generic_class_checked (gcomparer, &ctx, error);
58875890
mono_error_assert_ok (error); /* FIXME don't swallow the error */
5891+
g_assert (gcomparer_inst);
58885892
add_generic_class (acfg, gcomparer_inst, FALSE, "EqualityComparer<T>");
58895893
}
58905894
}
@@ -5906,6 +5910,7 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
59065910
enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "EnumEqualityComparer`1");
59075911
enum_comparer_inst = mono_class_inflate_generic_class_checked (enum_comparer, &ctx, error);
59085912
mono_error_assert_ok (error); /* FIXME don't swallow the error */
5913+
g_assert (enum_comparer_inst);
59095914
add_generic_class (acfg, enum_comparer_inst, FALSE, "EqualityComparer<T>");
59105915
}
59115916
}
@@ -5927,6 +5932,7 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
59275932
comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "ObjectComparer`1");
59285933
comparer_inst = mono_class_inflate_generic_class_checked (comparer, &ctx, error);
59295934
mono_error_assert_ok (error); /* FIXME don't swallow the error */
5935+
g_assert (comparer_inst);
59305936
add_generic_class (acfg, comparer_inst, FALSE, "Comparer<T>");
59315937
}
59325938
}
@@ -5950,6 +5956,7 @@ add_instances_of (MonoAotCompile *acfg, MonoClass *klass, MonoType **insts, int
59505956
ctx.class_inst = mono_metadata_get_generic_inst (1, args);
59515957
generic_inst = mono_class_inflate_generic_class_checked (klass, &ctx, error);
59525958
mono_error_assert_ok (error); /* FIXME don't swallow the error */
5959+
g_assert (generic_inst);
59535960
add_generic_class (acfg, generic_inst, force, "");
59545961
}
59555962
}
@@ -11566,6 +11573,9 @@ emit_exception_info (MonoAotCompile *acfg)
1156611573
char *aot_file = g_strdup_printf("%s%s", image_basename, SEQ_POINT_AOT_EXT);
1156711574
char *aot_file_path = g_build_filename (dir, aot_file, (const char*)NULL);
1156811575

11576+
g_assert (dir);
11577+
g_assert (aot_file_path);
11578+
1156911579
if (g_ensure_directory_exists (aot_file_path) == FALSE) {
1157011580
fprintf (stderr, "AOT : failed to create msym directory: %s\n", aot_file_path);
1157111581
exit (1);
@@ -15345,6 +15355,8 @@ set_paths (MonoAotCompile *acfg)
1534515355
}
1534615356

1534715357
acfg->tmpbasename = g_build_filename (temp_path, "temp", (const char*)NULL);
15358+
g_assert (acfg->tmpbasename);
15359+
1534815360
acfg->asm_fname = g_strdup_printf ("%s.s", acfg->tmpbasename);
1534915361
acfg->llvm_sfile = g_strdup_printf ("%s-llvm.s", acfg->tmpbasename);
1535015362

@@ -15379,6 +15391,8 @@ set_paths (MonoAotCompile *acfg)
1537915391
/* Done later */
1538015392
} else {
1538115393
acfg->tmpbasename = g_build_filename (acfg->aot_opts.temp_path, "temp", (const char*)NULL);
15394+
g_assert (acfg->tmpbasename);
15395+
1538215396
acfg->asm_fname = g_strdup_printf ("%s.s", acfg->tmpbasename);
1538315397
}
1538415398
}
@@ -15624,6 +15638,7 @@ compile_assemblies_in_child (MonoAotOptions *aot_opts, MonoAssembly **assemblies
1562415638

1562515639
#ifdef HOST_WIN32
1562615640
response_fname = g_build_filename (aot_opts->temp_path, "temp.rsp", (const char*)NULL);
15641+
g_assert (response_fname);
1562715642
response = fopen (response_fname, "w");
1562815643
g_assert (response);
1562915644
#endif

src/mono/mono/mini/mini-generic-sharing.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,7 @@ get_wrapper_shared_vtype (MonoType *t)
13121312

13131313
MonoClass *tuple_inst = mono_class_inflate_generic_class_checked (tuple_class, &ctx, error);
13141314
mono_error_assert_ok (error);
1315+
g_assert (tuple_inst);
13151316

13161317
//printf ("T: %s\n", mono_class_full_name (tuple_inst));
13171318

@@ -1411,6 +1412,7 @@ get_wrapper_shared_type_full (MonoType *t, gboolean is_field)
14111412
}
14121413
klass = mono_class_inflate_generic_class_checked (mono_class_get_generic_class (klass)->container_class, &ctx, error);
14131414
mono_error_assert_ok (error); /* FIXME don't swallow the error */
1415+
g_assert (klass);
14141416

14151417
t = m_class_get_byval_arg (klass);
14161418
MonoType *shared_type = get_wrapper_shared_vtype (t);
@@ -4349,6 +4351,7 @@ get_shared_type (MonoType *t, MonoType *type)
43494351

43504352
k = mono_class_inflate_generic_class_checked (gclass->container_class, &context, error);
43514353
mono_error_assert_ok (error); /* FIXME don't swallow the error */
4354+
g_assert (k);
43524355

43534356
return mini_get_shared_gparam (t, m_class_get_byval_arg (k));
43544357
} else if (MONO_TYPE_ISSTRUCT (type)) {

src/mono/mono/mini/mini-runtime.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,17 @@ void *(mono_global_codeman_reserve) (int size)
404404
global_codeman = mono_code_manager_new ();
405405
else
406406
global_codeman = mono_code_manager_new_aot ();
407-
return mono_code_manager_reserve (global_codeman, size);
407+
ptr = mono_code_manager_reserve (global_codeman, size);
408408
}
409409
else {
410410
mono_jit_lock ();
411411
ptr = mono_code_manager_reserve (global_codeman, size);
412412
mono_jit_unlock ();
413-
return ptr;
414413
}
414+
415+
/* Virtually all call sites for this API assume it can't return NULL. */
416+
g_assert (ptr);
417+
return ptr;
415418
}
416419

417420
/* The callback shouldn't take any locks */
@@ -2167,7 +2170,7 @@ mono_emit_jit_dump (MonoJitInfo *jinfo, gpointer code)
21672170
int i;
21682171

21692172
memset (&rec, 0, sizeof (rec));
2170-
2173+
21712174
// populating info relating debug methods
21722175
dmji = mono_debug_find_method (jinfo->d.method, NULL);
21732176

@@ -4540,20 +4543,20 @@ mini_llvm_init (void)
45404543
}
45414544

45424545
#ifdef ENSURE_PRIMARY_STACK_SIZE
4543-
/*++
4544-
Function:
4545-
EnsureStackSize
4546-
4547-
Abstract:
4548-
This fixes a problem on MUSL where the initial stack size reported by the
4549-
pthread_attr_getstack is about 128kB, but this limit is not fixed and
4550-
the stack can grow dynamically. The problem is that it makes the
4551-
functions ReflectionInvocation::[Try]EnsureSufficientExecutionStack
4552-
to fail for real life scenarios like e.g. compilation of corefx.
4553-
Since there is no real fixed limit for the stack, the code below
4554-
ensures moving the stack limit to a value that makes reasonable
4555-
real life scenarios work.
4556-
4546+
/*++
4547+
Function:
4548+
EnsureStackSize
4549+
4550+
Abstract:
4551+
This fixes a problem on MUSL where the initial stack size reported by the
4552+
pthread_attr_getstack is about 128kB, but this limit is not fixed and
4553+
the stack can grow dynamically. The problem is that it makes the
4554+
functions ReflectionInvocation::[Try]EnsureSufficientExecutionStack
4555+
to fail for real life scenarios like e.g. compilation of corefx.
4556+
Since there is no real fixed limit for the stack, the code below
4557+
ensures moving the stack limit to a value that makes reasonable
4558+
real life scenarios work.
4559+
45574560
--*/
45584561
static MONO_NO_OPTIMIZATION MONO_NEVER_INLINE void
45594562
ensure_stack_size (size_t size)
@@ -4737,7 +4740,7 @@ mini_init (const char *filename)
47374740
mono_w32handle_init ();
47384741
#endif
47394742

4740-
#ifdef ENSURE_PRIMARY_STACK_SIZE
4743+
#ifdef ENSURE_PRIMARY_STACK_SIZE
47414744
ensure_stack_size (5 * 1024 * 1024);
47424745
#endif // ENSURE_PRIMARY_STACK_SIZE
47434746

src/mono/mono/mini/mini.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4322,6 +4322,7 @@ mini_handle_call_res_devirt (MonoMethod *cmethod)
43224322

43234323
inst = mono_class_inflate_generic_class_checked (mono_class_get_iequatable_class (), &ctx, error);
43244324
mono_error_assert_ok (error);
4325+
g_assert (inst);
43254326

43264327
// EqualityComparer<T>.Default returns specific types depending on T
43274328
// FIXME: Special case more types: byte, string, nullable, enum ?

0 commit comments

Comments
 (0)