1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
//------------------------------------------------------------------------------
// <copyright file="Compiler.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Xml.Serialization {
using System.Reflection;
using System.Reflection.Emit;
using System.Collections;
using System.IO;
using System;
using System.Text;
using System.ComponentModel;
using System.CodeDom.Compiler;
using System.Security;
using System.Security.Permissions;
using System.Diagnostics;
using System.Security.Principal;
using System.Security.Policy;
using System.Threading;
using System.Xml.Serialization.Configuration;
using System.Globalization;
using System.Runtime.Versioning;
using System.Runtime.CompilerServices;
internal class Compiler {
bool debugEnabled = DiagnosticsSwitches.KeepTempFiles.Enabled;
Hashtable imports = new Hashtable();
StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
[ResourceExposure(ResourceScope.Machine)]
protected string[] Imports {
get {
string[] array = new string[imports.Values.Count];
imports.Values.CopyTo(array, 0);
return array;
}
}
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
internal void AddImport(Type type, Hashtable types) {
if (type == null)
return;
if (TypeScope.IsKnownType(type))
return;
if (types[type] != null)
return;
types[type] = type;
Type baseType = type.BaseType;
if (baseType != null)
AddImport(baseType, types);
Type declaringType = type.DeclaringType;
if (declaringType != null)
AddImport(declaringType, types);
foreach (Type intf in type.GetInterfaces())
AddImport(intf, types);
ConstructorInfo[] ctors = type.GetConstructors();
for (int i = 0; i < ctors.Length; i++) {
ParameterInfo[] parms = ctors[i].GetParameters();
for (int j = 0; j < parms.Length; j++) {
AddImport(parms[j].ParameterType, types);
}
}
if (type.IsGenericType) {
Type[] arguments = type.GetGenericArguments();
for (int i = 0; i < arguments.Length; i++) {
AddImport(arguments[i], types);
}
}
TempAssembly.FileIOPermission.Assert();
Module module = type.Module;
Assembly assembly = module.Assembly;
if (DynamicAssemblies.IsTypeDynamic(type)) {
DynamicAssemblies.Add(assembly);
return;
}
object[] typeForwardedFromAttribute = type.GetCustomAttributes(typeof(TypeForwardedFromAttribute), false);
if (typeForwardedFromAttribute.Length > 0)
{
TypeForwardedFromAttribute originalAssemblyInfo = typeForwardedFromAttribute[0] as TypeForwardedFromAttribute;
Assembly originalAssembly = Assembly.Load(originalAssemblyInfo.AssemblyFullName);
imports[originalAssembly] = originalAssembly.Location;
}
imports[assembly] = assembly.Location;
}
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
internal void AddImport(Assembly assembly) {
TempAssembly.FileIOPermission.Assert();
imports[assembly] = assembly.Location;
}
internal TextWriter Source {
get { return writer; }
}
internal void Close() { }
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
internal static string GetTempAssemblyPath(string baseDir, Assembly assembly, string defaultNamespace) {
if (assembly.IsDynamic) {
throw new InvalidOperationException(Res.GetString(Res.XmlPregenAssemblyDynamic));
}
PermissionSet perms = new PermissionSet(PermissionState.None);
perms.AddPermission(new FileIOPermission(PermissionState.Unrestricted));
perms.AddPermission(new EnvironmentPermission(PermissionState.Unrestricted));
perms.Assert();
try {
if (baseDir != null && baseDir.Length > 0) {
// check that the dirsctory exists
if (!Directory.Exists(baseDir)) {
throw new UnauthorizedAccessException(Res.GetString(Res.XmlPregenMissingDirectory, baseDir));
}
}
else {
baseDir = Path.GetTempPath();
// check that the dirsctory exists
if (!Directory.Exists(baseDir)) {
throw new UnauthorizedAccessException(Res.GetString(Res.XmlPregenMissingTempDirectory));
}
}
#if MONO
baseDir = Path.Combine (baseDir, GetTempAssemblyName(assembly.GetName(), defaultNamespace));
#else
if (baseDir.EndsWith("\\", StringComparison.Ordinal))
baseDir += GetTempAssemblyName(assembly.GetName(), defaultNamespace);
else
baseDir += "\\" + GetTempAssemblyName(assembly.GetName(), defaultNamespace);
#endif
}
finally {
CodeAccessPermission.RevertAssert();
}
return baseDir + ".dll";
}
internal static string GetTempAssemblyName(AssemblyName parent, string ns) {
return parent.Name + ".XmlSerializers" + (ns == null || ns.Length == 0 ? "" : "." + ns.GetHashCode());
}
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
internal Assembly Compile(Assembly parent, string ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence) {
CodeDomProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
CompilerParameters parameters = xmlParameters.CodeDomParameters;
parameters.ReferencedAssemblies.AddRange(Imports);
if (debugEnabled) {
parameters.GenerateInMemory = false;
parameters.IncludeDebugInformation = true;
parameters.TempFiles.KeepFiles = true;
}
PermissionSet perms = new PermissionSet(PermissionState.None);
if (xmlParameters.IsNeedTempDirAccess) {
perms.AddPermission(TempAssembly.FileIOPermission);
}
perms.AddPermission(new EnvironmentPermission(PermissionState.Unrestricted));
perms.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));
perms.AddPermission(new SecurityPermission(SecurityPermissionFlag.ControlEvidence));
perms.Assert();
if (parent != null && (parameters.OutputAssembly == null || parameters.OutputAssembly.Length ==0)) {
string assemblyName = AssemblyNameFromOptions(parameters.CompilerOptions);
if (assemblyName == null)
assemblyName = GetTempAssemblyPath(parameters.TempFiles.TempDir, parent, ns);
//
parameters.OutputAssembly = assemblyName;
}
if (parameters.CompilerOptions == null || parameters.CompilerOptions.Length == 0)
parameters.CompilerOptions = "/nostdlib";
else
parameters.CompilerOptions += " /nostdlib";
parameters.CompilerOptions += " /D:_DYNAMIC_XMLSERIALIZER_COMPILATION";
#pragma warning disable 618
parameters.Evidence = evidence;
#pragma warning restore 618
CompilerResults results = null;
Assembly assembly = null;
try {
results = codeProvider.CompileAssemblyFromSource(parameters, writer.ToString());
// check the output for errors or a certain level-1 warning (1595)
if (results.Errors.Count > 0) {
StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
stringWriter.WriteLine(Res.GetString(Res.XmlCompilerError, results.NativeCompilerReturnValue.ToString(CultureInfo.InvariantCulture)));
bool foundOne = false;
foreach (CompilerError e in results.Errors) {
// clear filename. This makes ToString() print just error number and message.
e.FileName = "";
if (!e.IsWarning || e.ErrorNumber == "CS1595") {
foundOne = true;
stringWriter.WriteLine(e.ToString());
}
}
if (foundOne) {
throw new InvalidOperationException(stringWriter.ToString());
}
}
assembly = results.CompiledAssembly;
}
catch (UnauthorizedAccessException) {
// try to get the user token
string user = GetCurrentUser();
if (user == null || user.Length == 0) {
throw new UnauthorizedAccessException(Res.GetString(Res.XmlSerializerAccessDenied));
}
else {
throw new UnauthorizedAccessException(Res.GetString(Res.XmlIdentityAccessDenied, user));
}
}
catch (FileLoadException fle) {
throw new InvalidOperationException(Res.GetString(Res.XmlSerializerCompileFailed), fle);
}
finally {
CodeAccessPermission.RevertAssert();
}
// somehow we got here without generating an assembly
if (assembly == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalError));
return assembly;
}
static string AssemblyNameFromOptions(string options) {
if (options == null || options.Length == 0)
return null;
string outName = null;
string[] flags = options.ToLower(CultureInfo.InvariantCulture).Split(null);
for (int i = 0; i < flags.Length; i++) {
string val = flags[i].Trim();
if (val.StartsWith("/out:", StringComparison.Ordinal)) {
outName = val.Substring(5);
}
}
return outName;
}
internal static string GetCurrentUser()
{
#if !FEATURE_PAL
try {
WindowsIdentity id = WindowsIdentity.GetCurrent();
if (id != null && id.Name != null)
return id.Name;
}
catch (Exception e) {
if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
throw;
}
}
#endif // !FEATURE_PAL
return "";
}
}
}
|