3 using System
.Runtime
.InteropServices
;
4 using System
.Reflection
;
6 [SecurityCriticalAttribute
]
11 //Console.WriteLine ("c ctor");
14 public virtual void Method ()
16 //Console.WriteLine ("c class");
19 public static void StaticMethod ()
21 //Console.WriteLine ("c class static");
25 [SecuritySafeCriticalAttribute
]
30 //Console.WriteLine ("sc ctor");
33 public virtual void Method ()
35 //Console.WriteLine ("sc class");
36 CClass cc
= new CClass ();
41 public class SCDevClass
: SCClass
45 Test
.error ("safe-critical-derived class instantiated");
48 public override void Method ()
51 Test
.error ("safe-critical-derived method called");
55 public class CMethodClass
57 public CMethodClass ()
59 //Console.WriteLine ("cmethod ctor");
62 [SecurityCriticalAttribute
]
63 public virtual void Method ()
65 //Console.WriteLine ("cmethod");
69 public class CMethodDevClass
: CMethodClass
71 public CMethodDevClass ()
73 Test
.error ("critical-derived constructor called");
76 public override void Method ()
79 Test
.error ("critical-derived method called");
83 public interface CMethodInterface
{
84 [SecurityCriticalAttribute
]
88 public class CInterfaceClass
: CMethodInterface
{
89 public CInterfaceClass () { }
93 Test
.error ("security-critical-interface-derived method called");
97 [SecurityCriticalAttribute
]
98 public class CriticalClass
{
100 public class NestedClassInsideCritical
{
102 static public void Method ()
104 Test
.error ("critical inner class method called");
109 public delegate void MethodDelegate ();
111 public delegate Object
InvokeDelegate (Object obj
, Object
[] parms
);
115 static bool haveError
= false;
117 public static void error (string text
)
119 Console
.WriteLine (text
);
123 [SecurityCriticalAttribute
]
124 static void CMethod ()
126 //Console.WriteLine ("c");
129 [SecuritySafeCriticalAttribute
]
130 static void SCMethod ()
132 //Console.WriteLine ("sc");
136 static void doSCDev ()
138 SCDevClass scdev
= new SCDevClass ();
142 static void doCMethodDev ()
144 CMethodDevClass cmdev
= new CMethodDevClass ();
145 error ("critical-derived object instantiated");
147 Console
.WriteLine ("critical-derived method called");
150 static void doSCInterfaceDev ()
152 CMethodInterface mi
= new CInterfaceClass ();
153 error ("safe-critical-interface-derived object instantiated");
155 error ("safe-critical-interface-derived method called");
159 static unsafe void unsafeMethod ()
162 error ("unsafe method called");
166 public static void TransparentReflectionCMethod ()
170 [SecurityCriticalAttribute
]
171 public static void ReflectionCMethod ()
173 error ("method called via reflection");
176 [DllImport ("/lib64/libc.so.6")]
177 static extern int getpid ();
179 public static int Main ()
185 error ("static critical method called");
186 } catch (MethodAccessException
) {
189 SCClass sc
= new SCClass ();
193 CClass c
= new CClass (); // Illegal
194 error ("critical object instantiated");
195 c
.Method (); // Illegal
196 error ("critical method called");
197 } catch (MethodAccessException
) {
202 error ("security-critical-derived class error");
203 } catch (TypeLoadException
) {
208 } catch (TypeLoadException
) {
213 error ("pinvoke called");
214 } catch (MethodAccessException
) {
218 MethodDelegate md
= new MethodDelegate (CClass
.StaticMethod
);
220 error ("critical method called via delegate");
221 } catch (MethodAccessException
) {
225 CriticalClass
.NestedClassInsideCritical
.Method ();
226 } catch (MethodAccessException
) {
231 } catch (TypeLoadException
) {
237 } catch (VerificationException) {
242 Type type
= Type
.GetType ("Test");
243 MethodInfo method
= type
.GetMethod ("TransparentReflectionCMethod");
245 method
.Invoke(null, null);
246 } catch (MethodAccessException
) {
247 error ("transparent method not called via reflection");
251 Type type
= Type
.GetType ("Test");
252 MethodInfo method
= type
.GetMethod ("ReflectionCMethod");
254 method
.Invoke(null, null);
255 } catch (MethodAccessException
) {
259 Type type
= Type
.GetType ("Test");
260 MethodInfo method
= type
.GetMethod ("TransparentReflectionCMethod");
261 InvokeDelegate id
= new InvokeDelegate (method
.Invoke
);
264 } catch (MethodAccessException
) {
265 error ("transparent method not called via reflection delegate");
269 Type type
= Type
.GetType ("Test");
270 MethodInfo method
= type
.GetMethod ("ReflectionCMethod");
271 InvokeDelegate id
= new InvokeDelegate (method
.Invoke
);
274 } catch (MethodAccessException
) {
277 //Console.WriteLine ("ok");