2 // MoonlightA11yProcessor.cs
5 // Andrés G. Aragoneses (aaragoneses@novell.com)
7 // (C) 2009 Novell, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 namespace Mono.Tuner {
39 public class MoonlightA11yProcessor : InjectSecurityAttributes {
41 protected override bool ConditionToProcess ()
46 protected override void ProcessAssembly (AssemblyDefinition assembly)
48 if (Annotations.GetAction (assembly) != AssemblyAction.Link)
53 // remove existing [SecurityCritical] and [SecuritySafeCritical]
54 RemoveSecurityAttributes ();
56 // add [SecurityCritical]
57 AddSecurityAttributes ();
59 // convert all public members into internal
63 void MakeApiInternal ()
65 foreach (TypeDefinition type in _assembly.MainModule.Types) {
67 type.IsPublic = false;
69 if (type.HasMethods && !type.Name.EndsWith ("Adapter"))
70 foreach (MethodDefinition ctor in type.Methods.Where (m => m.IsConstructor))
72 ctor.IsAssembly = true;
75 foreach (MethodDefinition method in type.Methods.Where (m => !m.IsConstructor))
77 method.IsAssembly = true;
81 void AddSecurityAttributes ()
83 foreach (TypeDefinition type in _assembly.MainModule.Types) {
84 AddCriticalAttribute (type);
87 foreach (MethodDefinition ctor in type.Methods.Where (m => m.IsConstructor))
88 AddCriticalAttribute (ctor);
91 foreach (MethodDefinition method in type.Methods.Where (m => !m.IsConstructor)) {
92 MethodDefinition parent = null;
94 //TODO: take in account generic params
95 if (!method.HasGenericParameters) {
98 * we need to scan base methods because the CoreCLR complains about SC attribs added
99 * to overriden methods whose base (virtual or interface) method is not marked as SC
100 * with TypeLoadExceptions
102 parent = GetBaseMethod (type, method);
105 //if there's no base method
106 if (parent == null ||
108 //if it's our bridge assembly, we're sure it will (finally, at the end of the linking process) have the SC attrib
109 _assembly.MainModule.Types.Contains (parent.DeclaringType) ||
111 //if the type is in the moonlight assemblies, check if it has the SC attrib
112 HasSecurityAttribute (parent, AttributeType.Critical))
114 AddCriticalAttribute (method);
120 MethodDefinition GetBaseMethod (TypeDefinition finalType, MethodDefinition final)
122 // both GetOverridenMethod and GetInterfaceMethod return null if there is no base method
123 return GetOverridenMethod (finalType, final) ?? GetInterfaceMethod (finalType, final);
126 //note: will not return abstract methods
127 MethodDefinition GetOverridenMethod (TypeDefinition finalType, MethodDefinition final)
129 TypeReference baseType = finalType.BaseType;
130 while (baseType != null && baseType.Resolve () != null) {
131 foreach (MethodDefinition method in baseType.Resolve ().Methods) {
132 if (!method.IsVirtual || method.Name != final.Name)
135 //TODO: should we discard them?
136 if (method.IsAbstract)
139 if (HasSameSignature (method, final))
142 baseType = baseType.Resolve().BaseType;
147 MethodDefinition GetInterfaceMethod (TypeDefinition finalType, MethodDefinition final)
149 TypeDefinition baseType = finalType;
150 while (baseType != null) {
151 if (baseType.HasInterfaces)
152 foreach (var @interface in baseType.Interfaces)
153 foreach (MethodDefinition method in @interface.InterfaceType.Resolve ().Methods)
154 if (method.Name == final.Name && HasSameSignature (method, final))
157 baseType = baseType.BaseType == null ? null : baseType.BaseType.Resolve ();
162 bool HasSameSignature (MethodDefinition method1, MethodDefinition method2)
164 if (method1.ReturnType.FullName != method2.ReturnType.FullName)
167 if (method1.Parameters.Count != method2.Parameters.Count)
170 for (int i = 0; i < method1.Parameters.Count; i++) {
171 if (method1.Parameters [i].ParameterType.FullName !=
172 method2.Parameters [i].ParameterType.FullName)