Skip to content

Commit d1feca2

Browse files
committed
Throw MemberAccessException when abstract class is used with newobj instruction
1 parent 13e9761 commit d1feca2

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

mono/metadata/class-internals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ typedef enum {
225225
MONO_EXCEPTION_OUT_OF_MEMORY = 14,
226226
MONO_EXCEPTION_INLINE_FAILED = 15,
227227
MONO_EXCEPTION_MONO_ERROR = 16,
228+
MONO_EXCEPTION_MEMBER_ACCESS = 17,
228229
/* add other exception type */
229230
} MonoExceptionType;
230231

mono/mini/method-to-ir.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3653,6 +3653,14 @@ handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_
36533653
MonoInst *iargs [2];
36543654
void *alloc_ftn;
36553655

3656+
if (mono_class_get_flags (klass) & TYPE_ATTRIBUTE_ABSTRACT) {
3657+
char* full_name = mono_type_get_full_name (klass);
3658+
cfg->exception_message = g_strdup_printf ("Cannot create an abstract class: %s", full_name);
3659+
g_free (full_name);
3660+
mono_cfg_set_exception (cfg, MONO_EXCEPTION_MEMBER_ACCESS);
3661+
return NULL;
3662+
}
3663+
36563664
if (context_used) {
36573665
MonoInst *data;
36583666
MonoRgctxInfoType rgctx_info;

mono/mini/mini.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4187,6 +4187,7 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
41874187
case MONO_EXCEPTION_TYPE_LOAD:
41884188
case MONO_EXCEPTION_MISSING_FIELD:
41894189
case MONO_EXCEPTION_MISSING_METHOD:
4190+
case MONO_EXCEPTION_MEMBER_ACCESS:
41904191
case MONO_EXCEPTION_FILE_NOT_FOUND:
41914192
case MONO_EXCEPTION_BAD_IMAGE:
41924193
case MONO_EXCEPTION_INVALID_PROGRAM: {
@@ -4206,6 +4207,8 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
42064207
ex = mono_get_exception_bad_image_format (cfg->exception_message);
42074208
else if (cfg->exception_type == MONO_EXCEPTION_INVALID_PROGRAM)
42084209
ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", cfg->exception_message);
4210+
else if (cfg->exception_type == MONO_EXCEPTION_MEMBER_ACCESS)
4211+
ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MemberAccessException", cfg->exception_message);
42094212
else
42104213
g_assert_not_reached ();
42114214
}

mono/tests/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,8 @@ TESTS_IL_SRC= \
608608
gsharing-valuetype-layout.il \
609609
invalid_generic_instantiation.il \
610610
bug-45841-fpstack-exceptions.il \
611-
instance_tailrec.il
611+
instance_tailrec.il \
612+
newobj-abstract.il
612613

613614
TESTS_GSHARED_SRC = \
614615
generics-sharing.2.cs \

mono/tests/newobj-abstract.il

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
.assembly extern mscorlib {}
2+
3+
.assembly 'newobj-abstract' {}
4+
5+
.class private auto ansi beforefieldinit Program
6+
extends [mscorlib]System.Object
7+
{
8+
.method public hidebysig static int32 Main() cil managed
9+
{
10+
.entrypoint
11+
.locals init (int32 V_0)
12+
13+
ldc.i4.1
14+
stloc.0
15+
.try
16+
{
17+
call void Program::NewAbstract()
18+
leave.s leavetarget
19+
20+
} // end .try
21+
catch [mscorlib]System.MemberAccessException
22+
{
23+
pop
24+
ldc.i4.0
25+
stloc.0
26+
leave.s leavetarget
27+
}
28+
leavetarget:
29+
ldloc.0
30+
ret
31+
} // end of method Program::Main
32+
33+
.method public hidebysig static void NewAbstract() cil managed
34+
{
35+
newobj instance void Foo::.ctor()
36+
call void [mscorlib]System.GC::KeepAlive(object)
37+
ret
38+
}
39+
40+
.method public hidebysig specialname rtspecialname
41+
instance void .ctor() cil managed
42+
{
43+
ldarg.0
44+
call instance void [mscorlib]System.Object::.ctor()
45+
ret
46+
}
47+
48+
}
49+
50+
.class private abstract auto ansi beforefieldinit Foo
51+
extends [mscorlib]System.Object
52+
{
53+
.method public hidebysig specialname rtspecialname
54+
instance void .ctor() cil managed
55+
{
56+
ldarg.0
57+
call instance void [mscorlib]System.Object::.ctor()
58+
ret
59+
}
60+
61+
}

0 commit comments

Comments
 (0)