Skip to content

Commit f61bc39

Browse files
kumperaakoeplinger
authored andcommitted
Simplify MonoError and managed Exception creation (mono#6123)
MonoError is simplified for MME by having the error message eagerly created, which simplifies things A LOT. Managed Exception creation is simplified by only setting Message, as it's done by other runtimes. * [mini] Fix mini_emit_memory_copy_internal with ref types and remove workaround from mono_method_to_ir * [mini] Remove unused argument and rename function to better reflect its use. * [utils] Fix swapped member and signature fields in error message. * [runtime] Simplify MME by only setting the message field since ClassName/MemberName don't need to be set. * [runtime] Add mono_error_set_method_missing to exceptions.c and collapse setting MME to a single function. This implements the cleanup discussed in the PR. * [runtime] Cleanup MonoError for BadImageFormatException. Move mono_error_set_bad_image & mono_error_set_bad_image_name to exception.c. Rename mono_error_set_bad_image_name to mono_error_set_bad_image_by_name. Introduce mono_corlib_exception_new_with_args to be used when creating exception objects. It's easier to consume than mono_exception_from_name_two_strings_checked as it takes string arguments as char*. Fix usage of mono_error_set_bad_image_by_name to never dup the supplied image name as the callee does it. Fix some empty error messages. Finally, reduce the cruft in mono_error_prepare_exception by using the new exception creation function from above. * [runtime] Cleanup MonoError of FileNotFoundException. Move it to use the generic first_argument and make it a simple error - IE, get_message doesn't format anything. * [runtime] Rework MonoError for MissingFieldException. * Fix build issues * [runtime] Ensure memory is freed using new MonoError set functions.
1 parent 60d4907 commit f61bc39

File tree

17 files changed

+276
-360
lines changed

17 files changed

+276
-360
lines changed

mcs/class/corlib/LinkerDescriptor/mscorlib.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,6 @@
248248
<type fullname="System.MissingMethodException">
249249
<!-- mini.c (mono_jit_compiler_method_inner) mono_exception_from_name_msg -->
250250
<method signature="System.Void .ctor(System.String)" />
251-
<!-- exception.c (mono_get_exception_type_load) mono_exception_from_name_two_strings -->
252-
<method signature="System.Void .ctor(System.String,System.String)" />
253-
<!-- exception.c mono_exception_from_name_four_strings -->
254-
<method signature="System.Void .ctor(System.String,System.String,System.String,System.String)" />
255251
</type>
256252

257253
<!-- threadpool.c: mono_thread_pool_init (assert) -->

mcs/class/referencesource/mscorlib/system/missingmethodexception.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,10 @@ public override String Message
4848
if (ClassName == null) {
4949
return base.Message;
5050
} else {
51-
#if MONO
52-
string res = ClassName + "." + MemberName;
53-
if (!string.IsNullOrEmpty(signature))
54-
res = string.Format (CultureInfo.InvariantCulture, signature, res);
55-
if (!string.IsNullOrEmpty(_message))
56-
res += " Due to: " + _message;
57-
return res;
58-
#else
5951
// do any desired fixups to classname here.
6052
return Environment.GetResourceString("MissingMethod_Name",
6153
ClassName + "." + MemberName +
6254
(Signature != null ? " " + FormatSignature(Signature) : ""));
63-
#endif
6455
}
6556
}
6657
}
@@ -82,17 +73,5 @@ public MissingMethodException(String className, String methodName)
8273
// If ClassName != null, Message will construct on the fly using it
8374
// and the other variables. This allows customization of the
8475
// format depending on the language environment.
85-
#if MONO
86-
// Called from the EE
87-
private MissingMethodException(String className, String methodName, String signature, String message) : base (message)
88-
{
89-
ClassName = className;
90-
MemberName = methodName;
91-
this.signature = signature;
92-
}
93-
94-
[NonSerialized]
95-
string signature;
96-
#endif
9776
}
9877
}

mono/metadata/appdomain.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ mono_try_assembly_resolve_handle (MonoDomain *domain, MonoStringHandle fname, Mo
12301230

12311231
if (ret && !refonly && ret->ref_only) {
12321232
/* .NET Framework throws System.IO.FileNotFoundException in this case */
1233-
mono_error_set_file_not_found (error, "AssemblyResolveEvent handlers cannot return Assemblies loaded for reflection only");
1233+
mono_error_set_file_not_found (error, NULL, "AssemblyResolveEvent handlers cannot return Assemblies loaded for reflection only");
12341234
ret = NULL;
12351235
goto leave;
12361236
}
@@ -2172,9 +2172,9 @@ ves_icall_System_Reflection_Assembly_LoadFrom (MonoStringHandle fname, MonoBoole
21722172

21732173
if (!ass) {
21742174
if (status == MONO_IMAGE_IMAGE_INVALID)
2175-
mono_error_set_bad_image_name (error, g_strdup (name), "");
2175+
mono_error_set_bad_image_by_name (error, name, "Invalid Image");
21762176
else
2177-
mono_error_set_assembly_load (error, g_strdup (name), "%s", "");
2177+
mono_error_set_file_not_found (error, name, "Invalid Image");
21782178
goto leave;
21792179
}
21802180

@@ -2214,7 +2214,7 @@ ves_icall_System_AppDomain_LoadAssemblyRaw (MonoAppDomainHandle ad,
22142214
MonoImage *image = mono_image_open_from_data_full (assembly_data, raw_assembly_len, FALSE, NULL, refonly);
22152215

22162216
if (!image) {
2217-
mono_error_set_bad_image_name (error, g_strdup (""), "%s", "");
2217+
mono_error_set_bad_image_by_name (error, "In memory assembly", "0x%p", raw_data);
22182218
return refass;
22192219
}
22202220

@@ -2231,7 +2231,7 @@ ves_icall_System_AppDomain_LoadAssemblyRaw (MonoAppDomainHandle ad,
22312231

22322232
if (!ass) {
22332233
mono_image_close (image);
2234-
mono_error_set_bad_image_name (error, g_strdup (""), "%s", "");
2234+
mono_error_set_bad_image_by_name (error, "In Memory assembly", "0x%p", assembly_data);
22352235
return refass;
22362236
}
22372237

mono/metadata/class-init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <mono/metadata/class-internals.h>
1313
#include <mono/metadata/custom-attrs-internals.h>
1414
#include <mono/metadata/debug-helpers.h>
15+
#include <mono/metadata/exception-internals.h>
1516
#include <mono/metadata/gc-internals.h>
1617
#include <mono/metadata/object-internals.h>
1718
#include <mono/metadata/profiler-private.h>

mono/metadata/class.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <mono/metadata/image-internals.h>
2323
#include <mono/metadata/assembly.h>
2424
#include <mono/metadata/assembly-internals.h>
25+
#include <mono/metadata/exception-internals.h>
2526
#include <mono/metadata/metadata.h>
2627
#include <mono/metadata/metadata-internals.h>
2728
#include <mono/metadata/profiler-private.h>
@@ -191,7 +192,8 @@ mono_class_from_typeref_checked (MonoImage *image, guint32 type_token, MonoError
191192

192193
mono_assembly_get_assemblyref (image, idx - 1, &aname);
193194
human_name = mono_stringify_assembly_name (&aname);
194-
mono_error_set_assembly_load_simple (error, human_name, image->assembly ? image->assembly->ref_only : FALSE);
195+
mono_error_set_simple_file_not_found (error, human_name, image->assembly ? image->assembly->ref_only : FALSE);
196+
g_free (human_name);
195197
return NULL;
196198
}
197199

mono/metadata/debug-helpers.c

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -287,49 +287,6 @@ mono_signature_full_name (MonoMethodSignature *sig)
287287
return result;
288288
}
289289

290-
/*
291-
* Returns a string ready to be consumed by managed code when formating a string to include class + method name.
292-
* IE, say you have void Foo:Bar(int). It will return "void {0}(int)".
293-
* The reason for this is that managed exception constructors for missing members require a both class and member names to be provided independently of the signature.
294-
*/
295-
char*
296-
mono_signature_get_managed_fmt_string (MonoMethodSignature *sig)
297-
{
298-
int i;
299-
char *result;
300-
GString *res;
301-
302-
if (!sig)
303-
return g_strdup ("<invalid signature>");
304-
305-
res = g_string_new ("");
306-
307-
mono_type_get_desc (res, sig->ret, TRUE);
308-
309-
g_string_append (res, " {0}");
310-
311-
if (sig->generic_param_count) {
312-
g_string_append_c (res, '<');
313-
for (i = 0; i < sig->generic_param_count; ++i) {
314-
if (i > 0)
315-
g_string_append (res, ",");
316-
g_string_append_printf (res, "!%d", i);
317-
}
318-
g_string_append_c (res, '>');
319-
}
320-
321-
g_string_append_c (res, '(');
322-
for (i = 0; i < sig->param_count; ++i) {
323-
if (i > 0)
324-
g_string_append_c (res, ',');
325-
mono_type_get_desc (res, sig->params [i], TRUE);
326-
}
327-
g_string_append_c (res, ')');
328-
result = res->str;
329-
g_string_free (res, FALSE);
330-
return result;
331-
}
332-
333290
void
334291
mono_ginst_get_desc (GString *str, MonoGenericInst *ginst)
335292
{

mono/metadata/exception-internals.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,30 @@ mono_exception_from_token_two_strings_checked (MonoImage *image, uint32_t token,
3030
MonoString *a1, MonoString *a2,
3131
MonoError *error);
3232

33-
MonoException *
34-
mono_exception_from_name_four_strings_checked (MonoImage *image, const char *name_space,
35-
const char *name, MonoString *a1, MonoString *a2, MonoString *a3, MonoString *a4,
36-
MonoError *error);
37-
38-
3933
typedef int (*MonoGetSeqPointFunc) (MonoDomain *domain, MonoMethod *method, gint32 native_offset);
4034

4135
void
4236
mono_install_get_seq_point (MonoGetSeqPointFunc func);
4337

38+
void
39+
mono_error_set_method_missing (MonoError *error, MonoClass *klass, const char *method_name, MonoMethodSignature *sig, const char *reason, ...) MONO_ATTR_FORMAT_PRINTF(5,6);
40+
41+
void
42+
mono_error_set_field_missing (MonoError *oerror, MonoClass *klass, const char *field_name, MonoType *sig, const char *reason, ...) MONO_ATTR_FORMAT_PRINTF(5,6);
43+
44+
void
45+
mono_error_set_bad_image (MonoError *error, MonoImage *image, const char *msg_format, ...) MONO_ATTR_FORMAT_PRINTF(3,4);
46+
47+
void
48+
mono_error_set_bad_image_by_name (MonoError *error, const char *image_name, const char *msg_format, ...) MONO_ATTR_FORMAT_PRINTF(3,4);
49+
50+
void
51+
mono_error_set_file_not_found (MonoError *oerror, const char *file_name, const char *msg_format, ...) MONO_ATTR_FORMAT_PRINTF(3,4);
52+
53+
void
54+
mono_error_set_simple_file_not_found (MonoError *oerror, const char *assembly_name, gboolean refection_only);
55+
56+
MonoException *
57+
mono_corlib_exception_new_with_args (const char *name_space, const char *name, const char *arg_0, const char *arg_1, MonoError *error);
58+
4459
#endif

0 commit comments

Comments
 (0)