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.
36 namespace Mono.Tuner {
38 public class MoonlightA11yProcessor : InjectSecurityAttributes {
40 protected override bool ConditionToProcess ()
45 protected override void ProcessAssembly (AssemblyDefinition assembly)
47 if (Annotations.GetAction (assembly) != AssemblyAction.Link)
52 // remove existing [SecurityCritical] and [SecuritySafeCritical]
53 RemoveSecurityAttributes ();
55 // add [SecurityCritical]
56 AddSecurityAttributes ();
58 // convert all public members into internal
62 void MakeApiInternal ()
64 foreach (TypeDefinition type in _assembly.MainModule.Types) {
66 type.IsPublic = false;
68 if (type.HasConstructors && !type.Name.EndsWith ("Adapter"))
69 foreach (MethodDefinition ctor in type.Constructors)
71 ctor.IsAssembly = true;
74 foreach (MethodDefinition method in type.Methods)
76 method.IsAssembly = true;
80 void AddSecurityAttributes ()
82 foreach (TypeDefinition type in _assembly.MainModule.Types) {
83 AddCriticalAttribute (type);
85 if (type.HasConstructors)
86 foreach (MethodDefinition ctor in type.Constructors)
87 AddCriticalAttribute (ctor);
90 foreach (MethodDefinition method in type.Methods) {
91 MethodDefinition parent = null;
93 //TODO: take in account generic params
94 if (!method.HasGenericParameters) {
97 * we need to scan base methods because the CoreCLR complains about SC attribs added
98 * to overriden methods whose base (virtual or interface) method is not marked as SC
99 * with TypeLoadExceptions
101 parent = GetBaseMethod (type, method);
104 //if there's no base method
105 if (parent == null ||
107 //if it's our bridge assembly, we're sure it will (finally, at the end of the linking process) have the SC attrib
108 _assembly.MainModule.Types.Contains (parent.DeclaringType) ||
110 //if the type is in the moonlight assemblies, check if it has the SC attrib
111 HasSecurityAttribute (parent, AttributeType.Critical))
113 AddCriticalAttribute (method);
119 MethodDefinition GetBaseMethod (TypeDefinition finalType, MethodDefinition final)
121 // both GetOverridenMethod and GetInterfaceMethod return null if there is no base method
122 return GetOverridenMethod (finalType, final) ?? GetInterfaceMethod (finalType, final);
125 //note: will not return abstract methods
126 MethodDefinition GetOverridenMethod (TypeDefinition finalType, MethodDefinition final)
128 TypeReference baseType = finalType.BaseType;
129 while (baseType != null && baseType.Resolve () != null) {
130 foreach (MethodDefinition method in baseType.Resolve ().Methods) {
131 if (!method.IsVirtual || method.Name != final.Name)
134 //TODO: should we discard them?
135 if (method.IsAbstract)
138 if (HasSameSignature (method, final))
141 baseType = baseType.Resolve().BaseType;
146 MethodDefinition GetInterfaceMethod (TypeDefinition finalType, MethodDefinition final)
148 TypeDefinition baseType = finalType;
149 while (baseType != null) {
150 if (baseType.HasInterfaces)
151 foreach (TypeReference @interface in baseType.Interfaces)
152 foreach (MethodDefinition method in @interface.Resolve ().Methods)
153 if (method.Name == final.Name && HasSameSignature (method, final))
156 baseType = baseType.BaseType == null ? null : baseType.BaseType.Resolve ();
161 bool HasSameSignature (MethodDefinition method1, MethodDefinition method2)
163 if (method1.ReturnType.ReturnType.FullName != method2.ReturnType.ReturnType.FullName)
166 if (method1.Parameters.Count != method2.Parameters.Count)
169 for (int i = 0; i < method1.Parameters.Count; i++) {
170 if (method1.Parameters [i].ParameterType.FullName !=
171 method2.Parameters [i].ParameterType.FullName)