Skip to content

Commit

Permalink
in mono/mono/mini:
Browse files Browse the repository at this point in the history
2007-08-20  Jb Evain  <jbevain@novell.com>

	* mini.c (mono_method_to_ir): throw MethodAccessException
	and FieldAccessException instead of InvalidProgramException.

in mono/mono/metadata:
2007-08-20  Jb Evain  <jbevain@novell.com>

	* class-internals: add definitions for MONO_EXCEPTION_METHOD_ACCESS
	and MONO_EXCEPTION_FIELD_ACCESS.

	* debug-helpers.[c|h]: new mono_field_full_name function.

in mono/mono/tests:
2007-08-20  Jb Evain  <jbevain@novell.com>

	* field-access.il, method-access.il, Makefile.am: tests
	for FieldAccessException and MethodAccessException.

svn path=/trunk/mono/; revision=84470
  • Loading branch information
jbevain committed Aug 20, 2007
1 parent ca0903b commit 1f84cc2
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 3 deletions.
7 changes: 7 additions & 0 deletions mono/metadata/ChangeLog
@@ -1,3 +1,10 @@
2007-08-20 Jb Evain <jbevain@novell.com>

* class-internals: add definitions for MONO_EXCEPTION_METHOD_ACCESS
and MONO_EXCEPTION_FIELD_ACCESS.

* debug-helpers.[c|h]: new mono_field_full_name function.

2007-08-20 Mark Probst <mark.probst@gmail.com> 2007-08-20 Mark Probst <mark.probst@gmail.com>


* class.c: Removed class_security_level() and moved it to * class.c: Removed class_security_level() and moved it to
Expand Down
4 changes: 3 additions & 1 deletion mono/metadata/class-internals.h
Expand Up @@ -172,7 +172,9 @@ enum {
MONO_EXCEPTION_MISSING_METHOD = 5, MONO_EXCEPTION_MISSING_METHOD = 5,
MONO_EXCEPTION_MISSING_FIELD = 6, MONO_EXCEPTION_MISSING_FIELD = 6,
MONO_EXCEPTION_TYPE_LOAD = 7, MONO_EXCEPTION_TYPE_LOAD = 7,
MONO_EXCEPTION_FILE_NOT_FOUND = 8 MONO_EXCEPTION_FILE_NOT_FOUND = 8,
MONO_EXCEPTION_METHOD_ACCESS = 9,
MONO_EXCEPTION_FIELD_ACCESS = 10,
/* add other exception type */ /* add other exception type */
}; };


Expand Down
12 changes: 12 additions & 0 deletions mono/metadata/debug-helpers.c
Expand Up @@ -557,6 +557,18 @@ mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const
return result; return result;
} }


char *
mono_field_full_name (MonoClassField *field)
{
char *res;
const char *nspace = field->parent->name_space;

res = g_strdup_printf ("%s%s%s:%s", nspace, *nspace ? "." : "",
field->parent->name, field->name);

return res;
}

char * char *
mono_method_full_name (MonoMethod *method, gboolean signature) mono_method_full_name (MonoMethod *method, gboolean signature)
{ {
Expand Down
2 changes: 2 additions & 0 deletions mono/metadata/debug-helpers.h
Expand Up @@ -40,6 +40,8 @@ MonoMethod* mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImag


char* mono_method_full_name (MonoMethod *method, gboolean signature); char* mono_method_full_name (MonoMethod *method, gboolean signature);


char* mono_field_full_name (MonoClassField *field);

G_END_DECLS G_END_DECLS


#endif /* __MONO_DEBUG_HELPERS_H__ */ #endif /* __MONO_DEBUG_HELPERS_H__ */
Expand Down
5 changes: 5 additions & 0 deletions mono/mini/ChangeLog
@@ -1,3 +1,8 @@
2007-08-20 Jb Evain <jbevain@novell.com>

* mini.c (mono_method_to_ir): throw MethodAccessException
and FieldAccessException instead of InvalidProgramException.

2007-08-20 Mark Probst <mark.probst@gmail.com> 2007-08-20 Mark Probst <mark.probst@gmail.com>


* mini.c: CoreCLR security checks. * mini.c: CoreCLR security checks.
Expand Down
36 changes: 34 additions & 2 deletions mono/mini/mini.c
Expand Up @@ -88,6 +88,24 @@
if (cfg->exception_type != MONO_EXCEPTION_NONE)\ if (cfg->exception_type != MONO_EXCEPTION_NONE)\
goto exception_exit;\ goto exception_exit;\
} while (0) } while (0)
#define METHOD_ACCESS_FAILURE do { \
char *method_fname = mono_method_full_name (method, TRUE); \
char *cil_method_fname = mono_method_full_name (cil_method, TRUE); \
cfg->exception_type = MONO_EXCEPTION_METHOD_ACCESS; \
cfg->exception_message = g_strdup_printf ("Method `%s' is inaccessible from method `%s'\n", cil_method_fname, method_fname); \
g_free (method_fname); \
g_free (cil_method_fname); \
goto exception_exit; \
} while (0)
#define FIELD_ACCESS_FAILURE do { \
char *method_fname = mono_method_full_name (method, TRUE); \
char *field_fname = mono_field_full_name (field); \
cfg->exception_type = MONO_EXCEPTION_FIELD_ACCESS; \
cfg->exception_message = g_strdup_printf ("Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname); \
g_free (method_fname); \
g_free (field_fname); \
goto exception_exit; \
} while (0)


/* /*
* this is used to determine when some branch optimizations are possible: we exclude FP compares * this is used to determine when some branch optimizations are possible: we exclude FP compares
Expand Down Expand Up @@ -4653,7 +4671,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
if (!cmethod) if (!cmethod)
goto load_error; goto load_error;
if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cil_method)) if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cil_method))
UNVERIFIED; METHOD_ACCESS_FAILURE;


if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
ensure_method_is_allowed_to_call_method (cfg, method, cil_method, bblock, ip); ensure_method_is_allowed_to_call_method (cfg, method, cil_method, bblock, ip);
Expand Down Expand Up @@ -6062,7 +6080,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
goto load_error; goto load_error;
mono_class_init (klass); mono_class_init (klass);
if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field)) if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
UNVERIFIED; FIELD_ACCESS_FAILURE;


foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset; foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
/* FIXME: mark instructions for use in SSA */ /* FIXME: mark instructions for use in SSA */
Expand Down Expand Up @@ -6239,6 +6257,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
if (!field) if (!field)
goto load_error; goto load_error;
mono_class_init (klass); mono_class_init (klass);
if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
FIELD_ACCESS_FAILURE;


g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL)); g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));


Expand Down Expand Up @@ -10865,6 +10885,18 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
mono_raise_exception (ex); mono_raise_exception (ex);
break; break;
} }
case MONO_EXCEPTION_METHOD_ACCESS: {
MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MethodAccessException", cfg->exception_message);
mono_destroy_compile (cfg);
mono_raise_exception (ex);
break;
}
case MONO_EXCEPTION_FIELD_ACCESS: {
MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "FieldAccessException", cfg->exception_message);
mono_destroy_compile (cfg);
mono_raise_exception (ex);
break;
}
/* this can only be set if the security manager is active */ /* this can only be set if the security manager is active */
case MONO_EXCEPTION_SECURITY_LINKDEMAND: { case MONO_EXCEPTION_SECURITY_LINKDEMAND: {
MonoAssembly *assembly = mono_image_get_assembly (method->klass->image); MonoAssembly *assembly = mono_image_get_assembly (method->klass->image);
Expand Down
5 changes: 5 additions & 0 deletions mono/tests/ChangeLog
@@ -1,3 +1,8 @@
2007-08-20 Jb Evain <jbevain@novell.com>

* field-access.il, method-access.il, Makefile.am: tests
for FieldAccessException and MethodAccessException.

2007-08-16 Rodrigo Kumpera <rkumpera@novell.com> 2007-08-16 Rodrigo Kumpera <rkumpera@novell.com>


* ldtoken_with_byref_typespec.2.il: Test for (in pseudo c#) "typeof (int&)" expression * ldtoken_with_byref_typespec.2.il: Test for (in pseudo c#) "typeof (int&)" expression
Expand Down
2 changes: 2 additions & 0 deletions mono/tests/Makefile.am
Expand Up @@ -253,6 +253,8 @@ TEST_CSC_SRC= \
vararg.cs vararg.cs


TEST_IL_SRC= \ TEST_IL_SRC= \
field-access.il \
method-access.il \
cpblkTest.il \ cpblkTest.il \
vbinterface.il \ vbinterface.il \
jmpTest.il \ jmpTest.il \
Expand Down
79 changes: 79 additions & 0 deletions mono/tests/field-access.il
@@ -0,0 +1,79 @@
.assembly extern mscorlib {}

.assembly 'field-access' {}

.class public FieldFail extends [mscorlib]System.Object {

.field private static string _sfield

.field private string _field

.method public specialname rtspecialname instance void .ctor()
{
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
ret
}
}

.class Test {

.method static void read_sfield ()
{
ldsfld string FieldFail::_sfield
pop
ret
}

.method static void read_field ()
{
.locals (FieldFail ff)

newobj instance void FieldFail::.ctor()
stloc ff
ldloc ff
ldfld string FieldFail::_field
pop
ret
}

.method static int32 Main ()
{
.entrypoint

.try {
call void Test::read_sfield ()
br fail
} catch [mscorlib]System.FieldAccessException {
pop
br continue
} catch [mscorlib]System.Exception {
pop
br fail
}

continue:
.try {
call void Test::read_field ()
br fail
} catch [mscorlib]System.FieldAccessException {
pop
br success
} catch [mscorlib]System.Exception {
pop
br fail
}

success:
ldstr "PASS"
call void [mscorlib]System.Console::WriteLine(string)
ldc.i4.0
ret

fail:
ldstr "FAIL"
call void [mscorlib]System.Console::WriteLine(string)
ldc.i4.1
ret
}
}
83 changes: 83 additions & 0 deletions mono/tests/method-access.il
@@ -0,0 +1,83 @@
.assembly extern mscorlib {}

.assembly 'method-access' {}

.class public MethFail extends [mscorlib]System.Object {

.method private void foo ()
{
ret
}

.method static private void sfoo ()
{
ret
}

.method public specialname rtspecialname instance void .ctor()
{
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
ret
}
}

.class Test {

.method static void call_sfoo ()
{
call void MethFail::sfoo ()
ret
}

.method static void call_foo ()
{
.locals (MethFail mf)

newobj instance void MethFail::.ctor()
stloc mf
ldloc mf
call instance void MethFail::foo ()
ret
}

.method static int32 Main ()
{
.entrypoint

.try {
call void Test::call_sfoo ()
br fail
} catch [mscorlib]System.MethodAccessException {
pop
br continue
} catch [mscorlib]System.Exception {
pop
br fail
}

continue:
.try {
call void Test::call_foo ()
br fail
} catch [mscorlib]System.MethodAccessException {
pop
br success
} catch [mscorlib]System.Exception {
pop
br fail
}

success:
ldstr "PASS"
call void [mscorlib]System.Console::WriteLine(string)
ldc.i4.0
ret

fail:
ldstr "FAIL"
call void [mscorlib]System.Console::WriteLine(string)
ldc.i4.1
ret
}
}

0 comments on commit 1f84cc2

Please sign in to comment.