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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
//------------------------------------------------------------------------------
// <copyright file="Scripts.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <spec>http://devdiv/Documents/Whidbey/CLR/CurrentSpecs/BCL/CodeDom%20Activation.doc</spec>
//------------------------------------------------------------------------------
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Security.Permissions;
using System.Threading;
using System.Xml.Xsl.IlGen;
using System.Xml.Xsl.Runtime;
using System.Runtime.Versioning;
namespace System.Xml.Xsl.Xslt {
using Res = System.Xml.Utils.Res;
internal class ScriptClass {
public string ns;
public CompilerInfo compilerInfo;
public StringCollection refAssemblies;
public StringCollection nsImports;
public CodeTypeDeclaration typeDecl;
public bool refAssembliesByHref;
public Dictionary<string, string> scriptUris;
// These two fields are used to report a compile error when its position is outside
// of all user code snippets in the generated temporary file
public string endUri;
public Location endLoc;
public ScriptClass(string ns, CompilerInfo compilerInfo) {
this.ns = ns;
this.compilerInfo = compilerInfo;
this.refAssemblies = new StringCollection();
this.nsImports = new StringCollection();
this.typeDecl = new CodeTypeDeclaration(GenerateUniqueClassName());
this.refAssembliesByHref = false;
this.scriptUris = new Dictionary<string, string>(
#if !FEATURE_CASE_SENSITIVE_FILESYSTEM
StringComparer.OrdinalIgnoreCase
#endif
);
}
private static long scriptClassCounter = 0;
private static string GenerateUniqueClassName() {
return "Script" + Interlocked.Increment(ref scriptClassCounter);
}
public void AddScriptBlock(string source, string uriString, int lineNumber, Location end) {
CodeSnippetTypeMember scriptSnippet = new CodeSnippetTypeMember(source);
string fileName = SourceLineInfo.GetFileName(uriString);
if (lineNumber > 0) {
scriptSnippet.LinePragma = new CodeLinePragma(fileName, lineNumber);
scriptUris[fileName] = uriString;
}
typeDecl.Members.Add(scriptSnippet);
this.endUri = uriString;
this.endLoc = end;
}
public ISourceLineInfo EndLineInfo {
get {
return new SourceLineInfo(this.endUri, this.endLoc, this.endLoc);
}
}
}
internal class Scripts {
private const string ScriptClassesNamespace = "System.Xml.Xsl.CompiledQuery";
private Compiler compiler;
private List<ScriptClass> scriptClasses = new List<ScriptClass>();
private Dictionary<string, Type> nsToType = new Dictionary<string, Type>();
private XmlExtensionFunctionTable extFuncs = new XmlExtensionFunctionTable();
public Scripts(Compiler compiler) {
this.compiler = compiler;
}
public Dictionary<string, Type> ScriptClasses {
get { return nsToType; }
}
public XmlExtensionFunction ResolveFunction(string name, string ns, int numArgs, IErrorHelper errorHelper) {
Type type;
if (nsToType.TryGetValue(ns, out type)) {
try {
return extFuncs.Bind(name, ns, numArgs, type, XmlQueryRuntime.EarlyBoundFlags);
}
catch (XslTransformException e) {
errorHelper.ReportError(e.Message);
}
}
return null;
}
public ScriptClass GetScriptClass(string ns, string language, IErrorHelper errorHelper) {
#if CONFIGURATION_DEP
CompilerInfo compilerInfo;
try {
compilerInfo = CodeDomProvider.GetCompilerInfo(language);
Debug.Assert(compilerInfo != null);
}
catch (ConfigurationException) {
// There is no CodeDom provider defined for this language
errorHelper.ReportError(/*[XT_010]*/Res.Xslt_ScriptInvalidLanguage, language);
return null;
}
foreach (ScriptClass scriptClass in scriptClasses) {
if (ns == scriptClass.ns) {
// Use object comparison because CompilerInfo.Equals may throw
if (compilerInfo != scriptClass.compilerInfo) {
errorHelper.ReportError(/*[XT_011]*/Res.Xslt_ScriptMixedLanguages, ns);
return null;
}
return scriptClass;
}
}
ScriptClass newScriptClass = new ScriptClass(ns, compilerInfo);
newScriptClass.typeDecl.TypeAttributes = TypeAttributes.Public;
scriptClasses.Add(newScriptClass);
return newScriptClass;
#else
return null;
#endif
}
//------------------------------------------------
// Compilation
//------------------------------------------------
public void CompileScripts() {
List<ScriptClass> scriptsForLang = new List<ScriptClass>();
for (int i = 0; i < scriptClasses.Count; i++) {
// If the script is already compiled, skip it
if (scriptClasses[i] == null)
continue;
// Group together scripts with the same CompilerInfo
CompilerInfo compilerInfo = scriptClasses[i].compilerInfo;
scriptsForLang.Clear();
for (int j = i; j < scriptClasses.Count; j++) {
// Use object comparison because CompilerInfo.Equals may throw
if (scriptClasses[j] != null && scriptClasses[j].compilerInfo == compilerInfo) {
scriptsForLang.Add(scriptClasses[j]);
scriptClasses[j] = null;
}
}
Assembly assembly = CompileAssembly(scriptsForLang);
if (assembly != null) {
foreach (ScriptClass script in scriptsForLang) {
Type clrType = assembly.GetType(ScriptClassesNamespace + Type.Delimiter + script.typeDecl.Name);
if (clrType != null) {
nsToType.Add(script.ns, clrType);
}
}
}
}
}
// Namespaces we always import when compiling
private static readonly string[] defaultNamespaces = new string[] {
"System",
"System.Collections",
"System.Text",
"System.Text.RegularExpressions",
"System.Xml",
"System.Xml.Xsl",
"System.Xml.XPath",
};
// 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.
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
private Assembly CompileAssembly(List<ScriptClass> scriptsForLang) {
TempFileCollection allTempFiles = compiler.CompilerResults.TempFiles;
CompilerErrorCollection allErrors = compiler.CompilerResults.Errors;
ScriptClass lastScript = scriptsForLang[scriptsForLang.Count - 1];
CodeDomProvider provider;
bool isVB = false;
try {
provider = lastScript.compilerInfo.CreateProvider();
}
catch (ConfigurationException e) {
// The CodeDom provider type could not be located, or some error in machine.config
allErrors.Add(compiler.CreateError(lastScript.EndLineInfo, /*[XT_041]*/Res.Xslt_ScriptCompileException, e.Message));
return null;
}
#if !FEATURE_PAL // visualbasic
isVB = provider is Microsoft.VisualBasic.VBCodeProvider;
#endif // !FEATURE_PAL
CodeCompileUnit[] codeUnits = new CodeCompileUnit[scriptsForLang.Count];
CompilerParameters compilParams = lastScript.compilerInfo.CreateDefaultCompilerParameters();
//
compilParams.ReferencedAssemblies.Add(typeof(System.Xml.Res).Assembly.Location);
compilParams.ReferencedAssemblies.Add("System.dll");
if (isVB) {
compilParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");
}
bool refAssembliesByHref = false;
for (int idx = 0; idx < scriptsForLang.Count; idx++) {
ScriptClass script = scriptsForLang[idx];
CodeNamespace scriptNs = new CodeNamespace(ScriptClassesNamespace);
// Add imported namespaces
foreach (string ns in defaultNamespaces) {
scriptNs.Imports.Add(new CodeNamespaceImport(ns));
}
if (isVB) {
scriptNs.Imports.Add(new CodeNamespaceImport("Microsoft.VisualBasic"));
}
foreach (string ns in script.nsImports) {
scriptNs.Imports.Add(new CodeNamespaceImport(ns));
}
scriptNs.Types.Add(script.typeDecl);
CodeCompileUnit unit = new CodeCompileUnit(); {
unit.Namespaces.Add(scriptNs);
if (isVB) {
// This settings have sense for Visual Basic only. In future releases we may allow to specify
// them explicitly in the msxsl:script element.
unit.UserData["AllowLateBound"] = true; // Allow variables to be declared untyped
unit.UserData["RequireVariableDeclaration"] = false; // Allow variables to be undeclared
}
// Put SecurityTransparentAttribute and SecurityRulesAttribute on the first CodeCompileUnit only
if (idx == 0) {
unit.AssemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Security.SecurityTransparentAttribute"));
// We want the assemblies generated for scripts to stick to the old security model
unit.AssemblyCustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(typeof(System.Security.SecurityRulesAttribute)),
new CodeAttributeArgument(
new CodeFieldReferenceExpression(
new CodeTypeReferenceExpression(typeof(System.Security.SecurityRuleSet)), "Level1"))));
}
}
codeUnits[idx] = unit;
foreach (string name in script.refAssemblies) {
compilParams.ReferencedAssemblies.Add(name);
}
refAssembliesByHref |= script.refAssembliesByHref;
}
XsltSettings settings = compiler.Settings;
compilParams.WarningLevel = settings.WarningLevel >= 0 ? settings.WarningLevel : compilParams.WarningLevel;
compilParams.TreatWarningsAsErrors = settings.TreatWarningsAsErrors;
compilParams.IncludeDebugInformation = compiler.IsDebug;
string asmPath = compiler.ScriptAssemblyPath;
if (asmPath != null && scriptsForLang.Count < scriptClasses.Count) {
asmPath = Path.ChangeExtension(asmPath, "." + GetLanguageName(lastScript.compilerInfo) + Path.GetExtension(asmPath));
}
compilParams.OutputAssembly = asmPath;
string tempDir = (settings.TempFiles != null) ? settings.TempFiles.TempDir : null;
compilParams.TempFiles = new TempFileCollection(tempDir);
// We need only .dll and .pdb, but there is no way to specify that
bool keepFiles = (compiler.IsDebug && asmPath == null);
#if DEBUG
keepFiles = keepFiles || XmlILTrace.IsEnabled;
#endif
keepFiles = keepFiles && !settings.CheckOnly;
compilParams.TempFiles.KeepFiles = keepFiles;
// If GenerateInMemory == true, then CodeDom loads the compiled assembly using Assembly.Load(byte[])
// instead of Assembly.Load(AssemblyName). That means the assembly will be loaded in the anonymous
// context (http://blogs.msdn.com/Microsoft/archive/2003/05/29/57143.aspx), and its dependencies can only
// be loaded from the Load context or using AssemblyResolve event. However we want to use the LoadFrom
// context to preload all dependencies specified by <ms:assembly href="uri-reference"/>, so we turn off
// GenerateInMemory here.
compilParams.GenerateInMemory = (asmPath == null && !compiler.IsDebug && !refAssembliesByHref) || settings.CheckOnly;
CompilerResults results;
try {
results = provider.CompileAssemblyFromDom(compilParams, codeUnits);
}
catch (ExternalException e) {
// Compiler might have created temporary files
results = new CompilerResults(compilParams.TempFiles);
results.Errors.Add(compiler.CreateError(lastScript.EndLineInfo, /*[XT_041]*/Res.Xslt_ScriptCompileException, e.Message));
}
if (!settings.CheckOnly) {
foreach (string fileName in results.TempFiles) {
allTempFiles.AddFile(fileName, allTempFiles.KeepFiles);
}
}
foreach (CompilerError error in results.Errors) {
FixErrorPosition(error, scriptsForLang);
compiler.AddModule(error.FileName);
}
allErrors.AddRange(results.Errors);
return results.Errors.HasErrors ? null : results.CompiledAssembly;
}
private int assemblyCounter = 0;
private string GetLanguageName(CompilerInfo compilerInfo) {
Regex alphaNumeric = new Regex("^[0-9a-zA-Z]+$");
foreach (string name in compilerInfo.GetLanguages()) {
if (alphaNumeric.IsMatch(name))
return name;
}
return "script" + (++assemblyCounter).ToString(CultureInfo.InvariantCulture);
}
// The position of a compile error may be outside of all user code snippets (for example, in case of
// unclosed '{'). In that case filename would be the name of the temporary file, and not the name
// of the stylesheet file. Exposing the path of the temporary file is considered to be a security issue,
// so here we check that filename is amongst user files.
private static void FixErrorPosition(CompilerError error, List<ScriptClass> scriptsForLang) {
string fileName = error.FileName;
string uri;
foreach (ScriptClass script in scriptsForLang) {
// We assume that CodeDom provider returns absolute paths (VSWhidbey 289665).
// Note that casing may be different.
if (script.scriptUris.TryGetValue(fileName, out uri)) {
// The error position is within one of user stylesheets, its URI may be reported
error.FileName = uri;
return;
}
}
// Error is outside user code snippeets, we should hide filename for security reasons.
// Return filename and position of the end of the last script block for the given class.
int idx, scriptNumber;
ScriptClass errScript = scriptsForLang[scriptsForLang.Count - 1];
// Normally temporary source files are named according to the scheme "<random name>.<script number>.
// <language extension>". Try to extract the middle part to find the relevant script class. In case
// of a non-standard CodeDomProvider, use the last script class.
fileName = Path.GetFileNameWithoutExtension(fileName);
if ((idx = fileName.LastIndexOf('.')) >= 0)
if (int.TryParse(fileName.Substring(idx + 1), NumberStyles.None, NumberFormatInfo.InvariantInfo, out scriptNumber))
if ((uint)scriptNumber < scriptsForLang.Count) {
errScript = scriptsForLang[scriptNumber];
}
error.FileName = errScript.endUri;
error.Line = errScript.endLoc.Line;
error.Column = errScript.endLoc.Pos;
}
}
}
|