public static unsafe byte[] GetSourceLinkData(Stream pdbStream) { pdbStream.Position = 0; var symReader = SymReaderFactory.CreateReader(pdbStream); try { Marshal.ThrowExceptionForHR( symReader.GetSourceServerData(out byte *data, out int size) ); if (size == 0) { return(Array.Empty <byte>()); } var result = new byte[size]; Marshal.Copy((IntPtr)data, result, 0, result.Length); return(result); } finally { symReader.Dispose(); } }
private static ImmutableArray <ImmutableArray <string> > GetGroupedImportStrings(Compilation compilation, string methodName, out ImmutableArray <string> externAliasStrings) { Assert.NotNull(compilation); Assert.NotNull(methodName); using (var exebits = new MemoryStream()) { using (var pdbbits = new MemoryStream()) { compilation.Emit(exebits, pdbbits); exebits.Position = 0; using (var metadata = ModuleMetadata.CreateFromStream(exebits, leaveOpen: true)) { var module = metadata.Module; var metadataReader = module.MetadataReader; MethodDefinitionHandle methodHandle = metadataReader.MethodDefinitions.Single(mh => metadataReader.GetString(metadataReader.GetMethodDefinition(mh).Name) == methodName); int methodToken = metadataReader.GetToken(methodHandle); // Create a SymReader, rather than a raw COM object, because // SymReader implements ISymUnmanagedReader3 and the COM object // might not. pdbbits.Position = 0; var reader = SymReaderFactory.CreateReader(pdbbits); return(reader.GetCSharpGroupedImportStrings(methodToken, methodVersion: 1, externAliasStrings: out externAliasStrings)); } } } }
internal static ModuleInstance GetModuleInstanceForIL(string ilSource) { ImmutableArray <byte> peBytes; ImmutableArray <byte> pdbBytes; CommonTestBase.EmitILToArray(ilSource, appendDefaultHeader: true, includePdb: true, assemblyBytes: out peBytes, pdbBytes: out pdbBytes); return(ModuleInstance.Create(peBytes, SymReaderFactory.CreateReader(pdbBytes), includeLocalSignatures: true)); }
public void EmbeddedType() { var sourcePIA = @"using System.Runtime.InteropServices; [assembly: PrimaryInteropAssembly(0, 0)] [assembly: Guid(""863D5BC0-46A1-49AC-97AA-A5F0D441A9DA"")] [ComImport] [Guid(""863D5BC0-46A1-49AD-97AA-A5F0D441A9DA"")] public interface I { object F(); }"; var source = @"class C { static void M() { var o = (I)null; } }"; var compilationPIA = CreateCompilationWithMscorlib(sourcePIA, options: TestOptions.DebugDll); var referencePIA = compilationPIA.EmitToImageReference(embedInteropTypes: true); var compilation0 = CreateCompilationWithMscorlib( source, options: TestOptions.DebugDll, assemblyName: Guid.NewGuid().ToString("D"), references: new MetadataReference[] { referencePIA }); byte[] exeBytes; byte[] pdbBytes; ImmutableArray <MetadataReference> references; compilation0.EmitAndGetReferences(out exeBytes, out pdbBytes, out references); // References should not include PIA. Assert.Equal(references.Length, 1); Assert.True(references[0].Display.StartsWith("mscorlib", StringComparison.Ordinal)); var runtime = CreateRuntimeInstance( Guid.NewGuid().ToString("D"), references, exeBytes, SymReaderFactory.CreateReader(pdbBytes)); var context = CreateMethodContext(runtime, "C.M"); string error; var testData = new CompilationTestData(); var result = context.CompileExpression("o", out error, testData); Assert.Null(error); testData.GetMethodData("<>x.<>m0").VerifyIL( @"{ // Code size 2 (0x2) .maxstack 1 .locals init (I V_0) //o IL_0000: ldloc.0 IL_0001: ret }"); }
public static ISymUnmanagedReader3 CreateSymReader(this CompilationVerifier verifier) { var pdbStream = new ImmutableMemoryStream(verifier.EmittedAssemblyPdb); return(SymReaderFactory.CreateReader( pdbStream, metadataReaderOpt: null, metadataMemoryOwnerOpt: null )); }
internal static ModuleInstance ToModuleInstance( this Compilation compilation, DebugInformationFormat debugFormat = DebugInformationFormat.Pdb, bool includeLocalSignatures = true) { var pdbStream = (debugFormat != 0) ? new MemoryStream() : null; var peImage = compilation.EmitToArray(new EmitOptions(debugInformationFormat: debugFormat), pdbStream: pdbStream); var symReader = (debugFormat != 0) ? SymReaderFactory.CreateReader(pdbStream, new PEReader(peImage)) : null; return(ModuleInstance.Create(peImage, symReader, includeLocalSignatures)); }
public void AccessType() { var ilSource = @" .class public auto ansi beforefieldinit C extends [mscorlib]System.Object { .field private int32 Private .field family int32 Protected .field assembly int32 Internal .field public int32 Public .field famorassem int32 ProtectedInternal .field famandassem int32 ProtectedAndInternal .method public hidebysig instance void Test() cil managed { ret } .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { ldarg.0 call instance void [mscorlib]System.Object::.ctor() ret } // end of method C::.ctor } // end of class C "; ImmutableArray <byte> exeBytes; ImmutableArray <byte> pdbBytes; EmitILToArray(ilSource, appendDefaultHeader: true, includePdb: true, assemblyBytes: out exeBytes, pdbBytes: out pdbBytes); var runtime = CreateRuntimeInstance( assemblyName: GetUniqueName(), references: ImmutableArray.Create(MscorlibRef), exeBytes: exeBytes.ToArray(), symReader: SymReaderFactory.CreateReader(pdbBytes)); var context = CreateMethodContext(runtime, methodName: "C.Test"); Assert.Equal(DkmEvaluationResultAccessType.Private, GetResultProperties(context, "Private").AccessType); Assert.Equal(DkmEvaluationResultAccessType.Protected, GetResultProperties(context, "Protected").AccessType); Assert.Equal(DkmEvaluationResultAccessType.Internal, GetResultProperties(context, "Internal").AccessType); Assert.Equal(DkmEvaluationResultAccessType.Public, GetResultProperties(context, "Public").AccessType); // As in dev12. Assert.Equal(DkmEvaluationResultAccessType.Internal, GetResultProperties(context, "ProtectedInternal").AccessType); Assert.Equal(DkmEvaluationResultAccessType.Internal, GetResultProperties(context, "ProtectedAndInternal").AccessType); Assert.Equal(DkmEvaluationResultAccessType.None, GetResultProperties(context, "null").AccessType); }
internal RuntimeInstance CreateRuntimeInstance( Compilation compilation, bool includeSymbols = true) { byte[] exeBytes; byte[] pdbBytes; ImmutableArray <MetadataReference> references; compilation.EmitAndGetReferences(out exeBytes, out pdbBytes, out references); return(CreateRuntimeInstance( ExpressionCompilerUtilities.GenerateUniqueName(), references.AddIntrinsicAssembly(), exeBytes, includeSymbols ? SymReaderFactory.CreateReader(pdbBytes, exeBytes) : null)); }
public void OldStyleNonCapturingLambda() { var ilSource = @" .class public auto ansi beforefieldinit C extends [mscorlib]System.Object { .method public hidebysig instance void M() cil managed { ldnull throw } .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { ldarg.0 call instance void [mscorlib]System.Object::.ctor() ret } .method private hidebysig static int32 '<M>b__0'() cil managed { ldnull throw } } // end of class C "; ImmutableArray <byte> ilBytes; ImmutableArray <byte> ilPdbBytes; EmitILToArray(ilSource, appendDefaultHeader: true, includePdb: true, assemblyBytes: out ilBytes, pdbBytes: out ilPdbBytes); var runtime = CreateRuntimeInstance( assemblyName: GetUniqueName(), references: ImmutableArray.Create(MscorlibRef), exeBytes: ilBytes.ToArray(), symReader: SymReaderFactory.CreateReader(ilPdbBytes.ToArray())); var context = CreateMethodContext(runtime, "C.<M>b__0"); VerifyNoThis(context); }
public void Win8RuntimeAssemblies() { var source = @"class C { static void M(Windows.Storage.StorageFolder f, Windows.Foundation.Collections.PropertySet p) { } }"; var compilation0 = CreateCompilationWithMscorlib( source, options: TestOptions.DebugDll, assemblyName: ExpressionCompilerUtilities.GenerateUniqueName(), references: WinRtRefs); var runtimeAssemblies = ExpressionCompilerTestHelpers.GetRuntimeWinMds("Windows.Storage", "Windows.Foundation.Collections"); Assert.True(runtimeAssemblies.Length >= 2); byte[] exeBytes; byte[] pdbBytes; ImmutableArray <MetadataReference> references; compilation0.EmitAndGetReferences(out exeBytes, out pdbBytes, out references); var runtime = CreateRuntimeInstance( ExpressionCompilerUtilities.GenerateUniqueName(), ImmutableArray.Create(MscorlibRef).Concat(runtimeAssemblies), // no reference to Windows.winmd exeBytes, SymReaderFactory.CreateReader(pdbBytes)); var context = CreateMethodContext(runtime, "C.M"); string error; var testData = new CompilationTestData(); context.CompileExpression("(p == null) ? f : null", out error, testData); Assert.Null(error); testData.GetMethodData("<>x.<>m0").VerifyIL( @"{ // Code size 7 (0x7) .maxstack 1 IL_0000: ldarg.1 IL_0001: brfalse.s IL_0005 IL_0003: ldnull IL_0004: ret IL_0005: ldarg.0 IL_0006: ret }"); }
public void Win8RuntimeAssemblies_ExternAlias() { var source = @"extern alias X; class C { static void M(X::Windows.Storage.StorageFolder f) { } }"; var compilation0 = CreateCompilationWithMscorlib( source, options: TestOptions.DebugDll, assemblyName: ExpressionCompilerUtilities.GenerateUniqueName(), references: WinRtRefs.Select(r => r.Display == "Windows" ? r.WithAliases(new[] { "X" }) : r)); var runtimeAssemblies = ExpressionCompilerTestHelpers.GetRuntimeWinMds("Windows.Storage"); Assert.True(runtimeAssemblies.Length >= 1); byte[] exeBytes; byte[] pdbBytes; ImmutableArray <MetadataReference> references; compilation0.EmitAndGetReferences(out exeBytes, out pdbBytes, out references); var runtime = CreateRuntimeInstance( ExpressionCompilerUtilities.GenerateUniqueName(), ImmutableArray.Create(MscorlibRef).Concat(runtimeAssemblies), // no reference to Windows.winmd exeBytes, SymReaderFactory.CreateReader(pdbBytes)); var context = CreateMethodContext(runtime, "C.M"); string error; var testData = new CompilationTestData(); context.CompileExpression("X::Windows.Storage.FileProperties.PhotoOrientation.Unspecified", out error, testData); Assert.Null(error); testData.GetMethodData("<>x.<>m0").VerifyIL( @"{ // Code size 2 (0x2) .maxstack 1 IL_0000: ldc.i4.0 IL_0001: ret }"); }
private RuntimeInstance CreateRuntime( string source, ImmutableArray <MetadataReference> compileReferences, ImmutableArray <MetadataReference> runtimeReferences) { var compilation0 = CreateCompilationWithMscorlib( source, options: TestOptions.DebugDll, assemblyName: ExpressionCompilerUtilities.GenerateUniqueName(), references: compileReferences); byte[] exeBytes; byte[] pdbBytes; ImmutableArray <MetadataReference> references; compilation0.EmitAndGetReferences(out exeBytes, out pdbBytes, out references); return(CreateRuntimeInstance( ExpressionCompilerUtilities.GenerateUniqueName(), runtimeReferences.AddIntrinsicAssembly(), exeBytes, SymReaderFactory.CreateReader(pdbBytes))); }
public ISymUnmanagedReader3 CreateSymReader() { var pdbStream = new MemoryStream(EmittedAssemblyPdb.ToArray()); return(SymReaderFactory.CreateReader(pdbStream, metadataReaderOpt: null, metadataMemoryOwnerOpt: null)); }
public void InstanceIterator_ExplicitInterfaceImplementation_OldName() { var ilSource = @" .class interface public abstract auto ansi I`1<T> { .method public hidebysig newslot abstract virtual instance class [mscorlib]System.Collections.IEnumerable F() cil managed { } // end of method I`1::F } // end of class I`1 .class public auto ansi beforefieldinit C extends [mscorlib]System.Object implements class I`1<int32> { .class auto ansi sealed nested private beforefieldinit '<I<System.Int32>'.'F>d__0' extends [mscorlib]System.Object implements class [mscorlib]System.Collections.Generic.IEnumerable`1<object>, [mscorlib]System.Collections.IEnumerable, class [mscorlib]System.Collections.Generic.IEnumerator`1<object>, [mscorlib]System.Collections.IEnumerator, [mscorlib]System.IDisposable { .field private object '<>2__current' .field private int32 '<>1__state' .field private int32 '<>l__initialThreadId' .field public class C '<>4__this' .method private hidebysig newslot virtual final instance class [mscorlib]System.Collections.Generic.IEnumerator`1<object> 'System.Collections.Generic.IEnumerable<System.Object>.GetEnumerator'() cil managed { ldnull throw } .method private hidebysig newslot virtual final instance class [mscorlib]System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() cil managed { ldnull throw } .method private hidebysig newslot virtual final instance bool MoveNext() cil managed { ldnull throw } .method private hidebysig newslot specialname virtual final instance object 'System.Collections.Generic.IEnumerator<System.Object>.get_Current'() cil managed { ldnull throw } .method private hidebysig newslot virtual final instance void System.Collections.IEnumerator.Reset() cil managed { ldnull throw } .method private hidebysig newslot virtual final instance void System.IDisposable.Dispose() cil managed { ldnull throw } .method private hidebysig newslot specialname virtual final instance object System.Collections.IEnumerator.get_Current() cil managed { ldnull throw } .method public hidebysig specialname rtspecialname instance void .ctor(int32 '<>1__state') cil managed { ldarg.0 call instance void [mscorlib]System.Object::.ctor() ret } .property instance object 'System.Collections.Generic.IEnumerator<System.Object>.Current'() { .get instance object C/'<I<System.Int32>'.'F>d__0'::'System.Collections.Generic.IEnumerator<System.Object>.get_Current'() } .property instance object System.Collections.IEnumerator.Current() { .get instance object C/'<I<System.Int32>'.'F>d__0'::System.Collections.IEnumerator.get_Current() } } // end of class '<I<System.Int32>'.'F>d__0' .method private hidebysig newslot virtual final instance class [mscorlib]System.Collections.IEnumerable 'I<System.Int32>.F'() cil managed { ldnull throw } .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { ldarg.0 call instance void [mscorlib]System.Object::.ctor() ret } } // end of class C "; ImmutableArray <byte> ilBytes; ImmutableArray <byte> ilPdbBytes; EmitILToArray(ilSource, appendDefaultHeader: true, includePdb: true, assemblyBytes: out ilBytes, pdbBytes: out ilPdbBytes); var runtime = CreateRuntimeInstance( assemblyName: GetUniqueName(), references: ImmutableArray.Create(MscorlibRef), exeBytes: ilBytes.ToArray(), symReader: SymReaderFactory.CreateReader(ilPdbBytes)); var context = CreateMethodContext(runtime, "C.<I<System.Int32>.F>d__0.MoveNext"); VerifyHasThis(context, "C", @" { // Code size 7 (0x7) .maxstack 1 IL_0000: ldarg.0 IL_0001: ldfld ""C C.<I<System.Int32>.F>d__0.<>4__this"" IL_0006: ret }"); }
public void AssemblyQualifiedNameResolutionWithUnification() { var source1 = @" using SI = System.Int32; public class C1 { void M() { } } "; var source2 = @" public class C2 : C1 { } "; ImmutableArray <MetadataReference> unused; var comp1 = CreateCompilation(source1, new[] { MscorlibRef_v20 }, TestOptions.DebugDll, assemblyName: "A"); byte[] dllBytes1; byte[] pdbBytes1; comp1.EmitAndGetReferences(out dllBytes1, out pdbBytes1, out unused); var ref1 = AssemblyMetadata.CreateFromImage(dllBytes1).GetReference(display: "A"); var comp2 = CreateCompilation(source2, new[] { MscorlibRef_v4_0_30316_17626, ref1 }, TestOptions.DebugDll, assemblyName: "B"); byte[] dllBytes2; byte[] pdbBytes2; comp2.EmitAndGetReferences(out dllBytes2, out pdbBytes2, out unused); var ref2 = AssemblyMetadata.CreateFromImage(dllBytes2).GetReference(display: "B"); var modulesBuilder = ArrayBuilder <ModuleInstance> .GetInstance(); modulesBuilder.Add(ref1.ToModuleInstance(dllBytes1, SymReaderFactory.CreateReader(pdbBytes1))); modulesBuilder.Add(ref2.ToModuleInstance(dllBytes2, SymReaderFactory.CreateReader(pdbBytes2))); modulesBuilder.Add(MscorlibRef_v4_0_30316_17626.ToModuleInstance(fullImage: null, symReader: null)); modulesBuilder.Add(ExpressionCompilerTestHelpers.IntrinsicAssemblyReference.ToModuleInstance(fullImage: null, symReader: null)); using (var runtime = new RuntimeInstance(modulesBuilder.ToImmutableAndFree())) { var context = CreateMethodContext(runtime, "C1.M"); string error; var testData = new CompilationTestData(); context.CompileExpression("typeof(SI)", out error, testData); Assert.Null(error); testData.GetMethodData("<>x.<>m0").VerifyIL(@" { // Code size 11 (0xb) .maxstack 1 IL_0000: ldtoken ""int"" IL_0005: call ""System.Type System.Type.GetTypeFromHandle(System.RuntimeTypeHandle)"" IL_000a: ret } "); } }
public static ISymUnmanagedReader CreateSymReaderFromResource(KeyValuePair <byte[], byte[]> peAndPdb) { return(SymReaderFactory.CreateReader(new MemoryStream(peAndPdb.Value), new SymMetadataImport(new MemoryStream(peAndPdb.Key)))); }
public void IntrinsicMethods() { var sourceA = @"public class A { }"; var sourceB = @"public class A { } public class B { static void M(A a) { } }"; var assemblyNameA = ExpressionCompilerUtilities.GenerateUniqueName(); var compilationA = CreateCompilationWithMscorlibAndSystemCore(sourceA, options: TestOptions.DebugDll, assemblyName: assemblyNameA); byte[] exeBytesA; byte[] pdbBytesA; ImmutableArray <MetadataReference> referencesA; compilationA.EmitAndGetReferences(out exeBytesA, out pdbBytesA, out referencesA); var referenceA = AssemblyMetadata.CreateFromImage(exeBytesA).GetReference(display: assemblyNameA); var identityA = referenceA.GetAssemblyIdentity(); var moduleA = referenceA.ToModuleInstance(exeBytesA, SymReaderFactory.CreateReader(pdbBytesA)); var assemblyNameB = ExpressionCompilerUtilities.GenerateUniqueName(); var compilationB = CreateCompilationWithMscorlibAndSystemCore(sourceB, options: TestOptions.DebugDll, assemblyName: assemblyNameB, references: new[] { referenceA }); byte[] exeBytesB; byte[] pdbBytesB; ImmutableArray <MetadataReference> referencesB; compilationB.EmitAndGetReferences(out exeBytesB, out pdbBytesB, out referencesB); var referenceB = AssemblyMetadata.CreateFromImage(exeBytesB).GetReference(display: assemblyNameB); var moduleB = referenceB.ToModuleInstance(exeBytesB, SymReaderFactory.CreateReader(pdbBytesB)); var moduleBuilder = ArrayBuilder <ModuleInstance> .GetInstance(); moduleBuilder.AddRange(referencesA.Select(r => r.ToModuleInstance(null, null))); moduleBuilder.Add(moduleA); moduleBuilder.Add(moduleB); moduleBuilder.Add(ExpressionCompilerTestHelpers.IntrinsicAssemblyReference.ToModuleInstance(fullImage: null, symReader: null)); var modules = moduleBuilder.ToImmutableAndFree(); using (var runtime = new RuntimeInstance(modules)) { ImmutableArray <MetadataBlock> blocks; Guid moduleVersionId; ISymUnmanagedReader symReader; int methodToken; int localSignatureToken; GetContextState(runtime, "B.M", out blocks, out moduleVersionId, out symReader, out methodToken, out localSignatureToken); var aliases = ImmutableArray.Create( ExceptionAlias(typeof(ArgumentException)), ReturnValueAlias(2, typeof(string)), ObjectIdAlias(1, typeof(object))); int attempts = 0; ExpressionCompiler.CreateContextDelegate contextFactory = (b, u) => { attempts++; return(EvaluationContext.CreateMethodContext( ToCompilation(b, u, moduleVersionId), symReader, moduleVersionId, methodToken, methodVersion: 1, ilOffset: 0, localSignatureToken: localSignatureToken)); }; string errorMessage; CompilationTestData testData; ExpressionCompilerTestHelpers.CompileExpressionWithRetry( blocks, "(object)new A() ?? $exception ?? $1 ?? $ReturnValue2", aliases, contextFactory, getMetaDataBytesPtr: null, errorMessage: out errorMessage, testData: out testData); Assert.Null(errorMessage); Assert.Equal(2, attempts); var methodData = testData.GetMethodData("<>x.<>m0"); methodData.VerifyIL( @"{ // Code size 49 (0x31) .maxstack 2 IL_0000: newobj ""A..ctor()"" IL_0005: dup IL_0006: brtrue.s IL_0030 IL_0008: pop IL_0009: call ""System.Exception Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetException()"" IL_000e: castclass ""System.ArgumentException"" IL_0013: dup IL_0014: brtrue.s IL_0030 IL_0016: pop IL_0017: ldstr ""$1"" IL_001c: call ""object Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetObjectByAlias(string)"" IL_0021: dup IL_0022: brtrue.s IL_0030 IL_0024: pop IL_0025: ldc.i4.2 IL_0026: call ""object Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetReturnValue(int)"" IL_002b: castclass ""string"" IL_0030: ret }"); } }
public void MissingMscorlib() { var sourceA = @"public class A { } class B { } class C { }"; var sourceB = @"public class B : A { }"; var assemblyNameA = ExpressionCompilerUtilities.GenerateUniqueName(); var compilationA = CreateCompilation( sourceA, references: new MetadataReference[] { SystemRuntimePP7Ref }, options: TestOptions.DebugDll, assemblyName: assemblyNameA); byte[] exeBytesA; byte[] pdbBytesA; ImmutableArray <MetadataReference> referencesA; compilationA.EmitAndGetReferences(out exeBytesA, out pdbBytesA, out referencesA); var referenceA = AssemblyMetadata.CreateFromImage(exeBytesA).GetReference(display: assemblyNameA); var identityA = referenceA.GetAssemblyIdentity(); var moduleA = referenceA.ToModuleInstance(exeBytesA, SymReaderFactory.CreateReader(pdbBytesA)); var assemblyNameB = ExpressionCompilerUtilities.GenerateUniqueName(); var compilationB = CreateCompilation( sourceB, references: new MetadataReference[] { SystemRuntimePP7Ref, referenceA }, options: TestOptions.DebugDll, assemblyName: assemblyNameB); byte[] exeBytesB; byte[] pdbBytesB; ImmutableArray <MetadataReference> referencesB; compilationB.EmitAndGetReferences(out exeBytesB, out pdbBytesB, out referencesB); var referenceB = AssemblyMetadata.CreateFromImage(exeBytesB).GetReference(display: assemblyNameB); var moduleB = referenceB.ToModuleInstance(exeBytesB, SymReaderFactory.CreateReader(pdbBytesB)); // Include an empty assembly to verify that not all assemblies // with no references are treated as mscorlib. var referenceC = AssemblyMetadata.CreateFromImage(Resources.Empty).GetReference(); // At runtime System.Runtime.dll contract assembly is replaced // by mscorlib.dll and System.Runtime.dll facade assemblies. var moduleBuilder = ArrayBuilder <ModuleInstance> .GetInstance(); moduleBuilder.Add(MscorlibFacadeRef.ToModuleInstance(null, null)); moduleBuilder.Add(SystemRuntimeFacadeRef.ToModuleInstance(null, null)); moduleBuilder.Add(moduleA); moduleBuilder.Add(moduleB); moduleBuilder.Add(referenceC.ToModuleInstance(null, null)); var modules = moduleBuilder.ToImmutableAndFree(); using (var runtime = new RuntimeInstance(modules)) { ImmutableArray <MetadataBlock> blocks; Guid moduleVersionId; ISymUnmanagedReader symReader; int typeToken; int localSignatureToken; GetContextState(runtime, "C", out blocks, out moduleVersionId, out symReader, out typeToken, out localSignatureToken); string errorMessage; CompilationTestData testData; int attempts = 0; ExpressionCompiler.CreateContextDelegate contextFactory = (b, u) => { attempts++; return(EvaluationContext.CreateTypeContext( ToCompilation(b, u, moduleVersionId), moduleVersionId, typeToken)); }; // Compile: [DebuggerDisplay("{new B()}")] const string expr = "new B()"; ExpressionCompilerTestHelpers.CompileExpressionWithRetry(blocks, expr, ImmutableArray <Alias> .Empty, contextFactory, getMetaDataBytesPtr: null, errorMessage: out errorMessage, testData: out testData); Assert.Null(errorMessage); Assert.Equal(2, attempts); var methodData = testData.GetMethodData("<>x.<>m0"); methodData.VerifyIL( @"{ // Code size 6 (0x6) .maxstack 1 IL_0000: newobj ""B..ctor()"" IL_0005: ret }"); } }
public void DuplicateTypesAndMethodsDifferentAssemblies() { var sourceA = @"using N; namespace N { class C1 { } public static class E { public static A F(this A o) { return o; } } } class C2 { } public class A { public static void M() { var x = new A(); var y = x.F(); } }"; var sourceB = @"using N; namespace N { class C1 { } public static class E { public static int F(this A o) { return 2; } } } class C2 { } public class B { static void M() { var x = new A(); } }"; var assemblyNameA = ExpressionCompilerUtilities.GenerateUniqueName(); var compilationA = CreateCompilationWithMscorlibAndSystemCore(sourceA, options: TestOptions.DebugDll, assemblyName: assemblyNameA); byte[] exeBytesA; byte[] pdbBytesA; ImmutableArray <MetadataReference> referencesA; compilationA.EmitAndGetReferences(out exeBytesA, out pdbBytesA, out referencesA); var referenceA = AssemblyMetadata.CreateFromImage(exeBytesA).GetReference(display: assemblyNameA); var identityA = referenceA.GetAssemblyIdentity(); var moduleA = referenceA.ToModuleInstance(exeBytesA, SymReaderFactory.CreateReader(pdbBytesA)); var assemblyNameB = ExpressionCompilerUtilities.GenerateUniqueName(); var compilationB = CreateCompilationWithMscorlibAndSystemCore(sourceB, options: TestOptions.DebugDll, assemblyName: assemblyNameB, references: new[] { referenceA }); byte[] exeBytesB; byte[] pdbBytesB; ImmutableArray <MetadataReference> referencesB; compilationB.EmitAndGetReferences(out exeBytesB, out pdbBytesB, out referencesB); var referenceB = AssemblyMetadata.CreateFromImage(exeBytesB).GetReference(display: assemblyNameB); var moduleB = referenceB.ToModuleInstance(exeBytesB, SymReaderFactory.CreateReader(pdbBytesB)); var moduleBuilder = ArrayBuilder <ModuleInstance> .GetInstance(); moduleBuilder.AddRange(referencesA.Select(r => r.ToModuleInstance(null, null))); moduleBuilder.Add(moduleA); moduleBuilder.Add(moduleB); var modules = moduleBuilder.ToImmutableAndFree(); using (var runtime = new RuntimeInstance(modules)) { ImmutableArray <MetadataBlock> blocks; Guid moduleVersionId; ISymUnmanagedReader symReader; int typeToken; int methodToken; int localSignatureToken; GetContextState(runtime, "B", out blocks, out moduleVersionId, out symReader, out typeToken, out localSignatureToken); string errorMessage; CompilationTestData testData; var contextFactory = CreateTypeContextFactory(moduleVersionId, typeToken); // Duplicate type in namespace, at type scope. ExpressionCompilerTestHelpers.CompileExpressionWithRetry(blocks, "new N.C1()", ImmutableArray <Alias> .Empty, contextFactory, getMetaDataBytesPtr: null, errorMessage: out errorMessage, testData: out testData); Assert.True(errorMessage.StartsWith("error CS0433: The type 'C1' exists in both ")); GetContextState(runtime, "B.M", out blocks, out moduleVersionId, out symReader, out methodToken, out localSignatureToken); contextFactory = CreateMethodContextFactory(moduleVersionId, symReader, methodToken, localSignatureToken); // Duplicate type in namespace, at method scope. ExpressionCompilerTestHelpers.CompileExpressionWithRetry(blocks, "new C1()", ImmutableArray <Alias> .Empty, contextFactory, getMetaDataBytesPtr: null, errorMessage: out errorMessage, testData: out testData); Assert.True(errorMessage.StartsWith("error CS0433: The type 'C1' exists in both ")); // Duplicate type in global namespace, at method scope. ExpressionCompilerTestHelpers.CompileExpressionWithRetry(blocks, "new C2()", ImmutableArray <Alias> .Empty, contextFactory, getMetaDataBytesPtr: null, errorMessage: out errorMessage, testData: out testData); Assert.True(errorMessage.StartsWith("error CS0433: The type 'C2' exists in both ")); // Duplicate extension method, at method scope. ExpressionCompilerTestHelpers.CompileExpressionWithRetry(blocks, "x.F()", ImmutableArray <Alias> .Empty, contextFactory, getMetaDataBytesPtr: null, errorMessage: out errorMessage, testData: out testData); Assert.Equal(errorMessage, "error CS0121: The call is ambiguous between the following methods or properties: 'N.E.F(A)' and 'N.E.F(A)'"); // Same tests as above but in library that does not directly reference duplicates. GetContextState(runtime, "A", out blocks, out moduleVersionId, out symReader, out typeToken, out localSignatureToken); contextFactory = CreateTypeContextFactory(moduleVersionId, typeToken); // Duplicate type in namespace, at type scope. ExpressionCompilerTestHelpers.CompileExpressionWithRetry(blocks, "new N.C1()", ImmutableArray <Alias> .Empty, contextFactory, getMetaDataBytesPtr: null, errorMessage: out errorMessage, testData: out testData); Assert.Null(errorMessage); var methodData = testData.GetMethodData("<>x.<>m0"); methodData.VerifyIL( @"{ // Code size 6 (0x6) .maxstack 1 IL_0000: newobj ""N.C1..ctor()"" IL_0005: ret }"); Assert.Equal(methodData.Method.ReturnType.ContainingAssembly.ToDisplayString(), identityA.GetDisplayName()); GetContextState(runtime, "A.M", out blocks, out moduleVersionId, out symReader, out methodToken, out localSignatureToken); contextFactory = CreateMethodContextFactory(moduleVersionId, symReader, methodToken, localSignatureToken); // Duplicate type in global namespace, at method scope. ExpressionCompilerTestHelpers.CompileExpressionWithRetry(blocks, "new C2()", ImmutableArray <Alias> .Empty, contextFactory, getMetaDataBytesPtr: null, errorMessage: out errorMessage, testData: out testData); Assert.Null(errorMessage); methodData = testData.GetMethodData("<>x.<>m0"); methodData.VerifyIL( @"{ // Code size 6 (0x6) .maxstack 1 .locals init (A V_0, //x A V_1) //y IL_0000: newobj ""C2..ctor()"" IL_0005: ret }"); Assert.Equal(methodData.Method.ReturnType.ContainingAssembly.ToDisplayString(), identityA.GetDisplayName()); // Duplicate extension method, at method scope. ExpressionCompilerTestHelpers.CompileExpressionWithRetry(blocks, "x.F()", ImmutableArray <Alias> .Empty, contextFactory, getMetaDataBytesPtr: null, errorMessage: out errorMessage, testData: out testData); Assert.Null(errorMessage); methodData = testData.GetMethodData("<>x.<>m0"); methodData.VerifyIL( @"{ // Code size 7 (0x7) .maxstack 1 .locals init (A V_0, //x A V_1) //y IL_0000: ldloc.0 IL_0001: call ""A N.E.F(A)"" IL_0006: ret }"); Assert.Equal(methodData.Method.ReturnType.ContainingAssembly.ToDisplayString(), identityA.GetDisplayName()); } }
public void AssemblyDuplicateReferences() { var sourceA = @"public class A { }"; var sourceB = @"public class B { public A F = new A(); }"; var sourceC = @"class C { private B F = new B(); static void M() { } }"; // Assembly A, multiple versions, strong name. var assemblyNameA = ExpressionCompilerUtilities.GenerateUniqueName(); var publicKeyA = ImmutableArray.CreateRange(new byte[] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x06, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x31, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0xED, 0xD3, 0x22, 0xCB, 0x6B, 0xF8, 0xD4, 0xA2, 0xFC, 0xCC, 0x87, 0x37, 0x04, 0x06, 0x04, 0xCE, 0xE7, 0xB2, 0xA6, 0xF8, 0x4A, 0xEE, 0xF3, 0x19, 0xDF, 0x5B, 0x95, 0xE3, 0x7A, 0x6A, 0x28, 0x24, 0xA4, 0x0A, 0x83, 0x83, 0xBD, 0xBA, 0xF2, 0xF2, 0x52, 0x20, 0xE9, 0xAA, 0x3B, 0xD1, 0xDD, 0xE4, 0x9A, 0x9A, 0x9C, 0xC0, 0x30, 0x8F, 0x01, 0x40, 0x06, 0xE0, 0x2B, 0x95, 0x62, 0x89, 0x2A, 0x34, 0x75, 0x22, 0x68, 0x64, 0x6E, 0x7C, 0x2E, 0x83, 0x50, 0x5A, 0xCE, 0x7B, 0x0B, 0xE8, 0xF8, 0x71, 0xE6, 0xF7, 0x73, 0x8E, 0xEB, 0x84, 0xD2, 0x73, 0x5D, 0x9D, 0xBE, 0x5E, 0xF5, 0x90, 0xF9, 0xAB, 0x0A, 0x10, 0x7E, 0x23, 0x48, 0xF4, 0xAD, 0x70, 0x2E, 0xF7, 0xD4, 0x51, 0xD5, 0x8B, 0x3A, 0xF7, 0xCA, 0x90, 0x4C, 0xDC, 0x80, 0x19, 0x26, 0x65, 0xC9, 0x37, 0xBD, 0x52, 0x81, 0xF1, 0x8B, 0xCD }); var compilationAS1 = CreateCompilation( new AssemblyIdentity(assemblyNameA, new Version(1, 1, 1, 1), cultureName: "", publicKeyOrToken: publicKeyA, hasPublicKey: true), new[] { sourceA }, references: new[] { MscorlibRef }, options: TestOptions.DebugDll.WithDelaySign(true)); var referenceAS1 = compilationAS1.EmitToImageReference(); var identityAS1 = referenceAS1.GetAssemblyIdentity(); var compilationAS2 = CreateCompilation( new AssemblyIdentity(assemblyNameA, new Version(2, 1, 1, 1), cultureName: "", publicKeyOrToken: publicKeyA, hasPublicKey: true), new[] { sourceA }, references: new[] { MscorlibRef }, options: TestOptions.DebugDll.WithDelaySign(true)); var referenceAS2 = compilationAS2.EmitToImageReference(); var identityAS2 = referenceAS2.GetAssemblyIdentity(); // Assembly B, multiple versions, not strong name. var assemblyNameB = ExpressionCompilerUtilities.GenerateUniqueName(); var compilationBN1 = CreateCompilation( new AssemblyIdentity(assemblyNameB, new Version(1, 1, 1, 1)), new[] { sourceB }, references: new[] { MscorlibRef, referenceAS1 }, options: TestOptions.DebugDll); var referenceBN1 = compilationBN1.EmitToImageReference(); var identityBN1 = referenceBN1.GetAssemblyIdentity(); var compilationBN2 = CreateCompilation( new AssemblyIdentity(assemblyNameB, new Version(2, 2, 2, 1)), new[] { sourceB }, references: new[] { MscorlibRef, referenceAS1 }, options: TestOptions.DebugDll); var referenceBN2 = compilationBN2.EmitToImageReference(); var identityBN2 = referenceBN2.GetAssemblyIdentity(); // Assembly B, multiple versions, strong name. var publicKeyB = ImmutableArray.CreateRange(new byte[] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x06, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x53, 0x52, 0x41, 0x31, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0xED, 0xD3, 0x22, 0xCB, 0x6B, 0xF8, 0xD4, 0xA2, 0xFC, 0xCC, 0x87, 0x37, 0x04, 0x06, 0x04, 0xCE, 0xE7, 0xB2, 0xA6, 0xF8, 0x4A, 0xEE, 0xF3, 0x19, 0xDF, 0x5B, 0x95, 0xE3, 0x7A, 0x6A, 0x28, 0x24, 0xA4, 0x0A, 0x83, 0x83, 0xBD, 0xBA, 0xF2, 0xF2, 0x52, 0x20, 0xE9, 0xAA, 0x3B, 0xD1, 0xDD, 0xE4, 0x9A, 0x9A, 0x9C, 0xC0, 0x30, 0x8F, 0x01, 0x40, 0x06, 0xE0, 0x2B, 0x95, 0x62, 0x89, 0x2A, 0x34, 0x75, 0x22, 0x68, 0x64, 0x6E, 0x7C, 0x2E, 0x83, 0x50, 0x5A, 0xCE, 0x7B, 0x0B, 0xE8, 0xF8, 0x71, 0xE6, 0xF7, 0x73, 0x8E, 0xEB, 0x84, 0xD2, 0x73, 0x5D, 0x9D, 0xBE, 0x5E, 0xF5, 0x90, 0xF9, 0xAB, 0x0A, 0x10, 0x7E, 0x23, 0x48, 0xF4, 0xAD, 0x70, 0x2E, 0xF7, 0xD4, 0x51, 0xD5, 0x8B, 0x3A, 0xF7, 0xCA, 0x90, 0x4C, 0xDC, 0x80, 0x19, 0x26, 0x65, 0xC9, 0x37, 0xBD, 0x52, 0x81, 0xF1, 0x8B, 0xCD }); var compilationBS1 = CreateCompilation( new AssemblyIdentity(assemblyNameB, new Version(1, 1, 1, 1), cultureName: "", publicKeyOrToken: publicKeyB, hasPublicKey: true), new[] { sourceB }, references: new[] { MscorlibRef, referenceAS1 }, options: TestOptions.DebugDll.WithDelaySign(true)); var referenceBS1 = compilationBS1.EmitToImageReference(); var identityBS1 = referenceBS1.GetAssemblyIdentity(); var compilationBS2 = CreateCompilation( new AssemblyIdentity(assemblyNameB, new Version(2, 2, 2, 1), cultureName: "", publicKeyOrToken: publicKeyB, hasPublicKey: true), new[] { sourceB }, references: new[] { MscorlibRef, referenceAS2 }, options: TestOptions.DebugDll.WithDelaySign(true)); var referenceBS2 = compilationBS2.EmitToImageReference(); var identityBS2 = referenceBS2.GetAssemblyIdentity(); var mscorlibIdentity = MscorlibRef.GetAssemblyIdentity(); var mscorlib20Identity = MscorlibRef_v20.GetAssemblyIdentity(); var systemRefIdentity = SystemRef.GetAssemblyIdentity(); var systemRef20Identity = SystemRef_v20.GetAssemblyIdentity(); // No duplicates. VerifyAssemblyReferences( referenceBN1, ImmutableArray.Create(MscorlibRef, referenceAS1, referenceBN1), ImmutableArray.Create(mscorlibIdentity, identityAS1, identityBN1)); // No duplicates, extra references. VerifyAssemblyReferences( referenceAS1, ImmutableArray.Create(MscorlibRef, referenceBN1, referenceAS1, referenceBS2), ImmutableArray.Create(mscorlibIdentity, identityAS1)); // Strong-named, non-strong-named, and framework duplicates, same version (no aliases). VerifyAssemblyReferences( referenceBN2, ImmutableArray.Create(MscorlibRef, referenceAS1, MscorlibRef, referenceBN2, referenceBN2, referenceAS1, referenceAS1), ImmutableArray.Create(mscorlibIdentity, identityAS1, identityBN2)); // Strong-named, non-strong-named, and framework duplicates, different versions. VerifyAssemblyReferences( referenceBN1, ImmutableArray.Create(MscorlibRef, referenceAS1, MscorlibRef_v20, referenceAS2, referenceBN2, referenceBN1, referenceAS2, referenceAS1, referenceBN1), ImmutableArray.Create(mscorlibIdentity, identityAS2, identityBN2)); VerifyAssemblyReferences( referenceBN2, ImmutableArray.Create(MscorlibRef, referenceAS1, MscorlibRef_v20, referenceAS2, referenceBN2, referenceBN1, referenceAS2, referenceAS1, referenceBN1), ImmutableArray.Create(mscorlibIdentity, identityAS2, identityBN2)); // Strong-named, different versions. VerifyAssemblyReferences( referenceBS1, ImmutableArray.Create(MscorlibRef, referenceAS1, referenceAS2, referenceBS2, referenceBS1, referenceAS2, referenceAS1, referenceBS1), ImmutableArray.Create(mscorlibIdentity, identityAS2, identityBS2)); VerifyAssemblyReferences( referenceBS2, ImmutableArray.Create(MscorlibRef, referenceAS1, referenceAS2, referenceBS2, referenceBS1, referenceAS2, referenceAS1, referenceBS1), ImmutableArray.Create(mscorlibIdentity, identityAS2, identityBS2)); // Assembly C, multiple versions, not strong name. var assemblyNameC = ExpressionCompilerUtilities.GenerateUniqueName(); var compilationCN1 = CreateCompilation( new AssemblyIdentity(assemblyNameC, new Version(1, 1, 1, 1)), new[] { sourceC }, references: new[] { MscorlibRef, referenceBS1 }, options: TestOptions.DebugDll); byte[] exeBytesC1; byte[] pdbBytesC1; ImmutableArray <MetadataReference> references; compilationCN1.EmitAndGetReferences(out exeBytesC1, out pdbBytesC1, out references); var compilationCN2 = CreateCompilation( new AssemblyIdentity(assemblyNameC, new Version(2, 1, 1, 1)), new[] { sourceC }, references: new[] { MscorlibRef, referenceBS2 }, options: TestOptions.DebugDll); byte[] exeBytesC2; byte[] pdbBytesC2; compilationCN1.EmitAndGetReferences(out exeBytesC2, out pdbBytesC2, out references); // Duplicate assemblies, target module referencing BS1. using (var runtime = CreateRuntimeInstance( assemblyNameC, ImmutableArray.Create(MscorlibRef, referenceAS1, referenceAS2, referenceBS2, referenceBS1, referenceBS2), exeBytesC1, SymReaderFactory.CreateReader(pdbBytesC1))) { ImmutableArray <MetadataBlock> typeBlocks; ImmutableArray <MetadataBlock> methodBlocks; Guid moduleVersionId; ISymUnmanagedReader symReader; int typeToken; int methodToken; int localSignatureToken; GetContextState(runtime, "C", out typeBlocks, out moduleVersionId, out symReader, out typeToken, out localSignatureToken); GetContextState(runtime, "C.M", out methodBlocks, out moduleVersionId, out symReader, out methodToken, out localSignatureToken); uint ilOffset = ExpressionCompilerTestHelpers.GetOffset(methodToken, symReader); // Compile expression with type context with all modules. var context = EvaluationContext.CreateTypeContext( default(CSharpMetadataContext), typeBlocks, moduleVersionId, typeToken); string error; // A is ambiguous. var testData = new CompilationTestData(); context.CompileExpression("new A()", out error, testData); Assert.True(error.StartsWith("error CS0433: The type 'A' exists in both ")); testData = new CompilationTestData(); // B is ambiguous. context.CompileExpression("new B()", out error, testData); Assert.True(error.StartsWith("error CS0433: The type 'B' exists in both ")); var previous = new CSharpMetadataContext(typeBlocks, context); // Compile expression with type context with referenced modules only. context = EvaluationContext.CreateTypeContext( typeBlocks.ToCompilationReferencedModulesOnly(moduleVersionId), moduleVersionId, typeToken); // A is unrecognized since there were no direct references to AS1 or AS2. testData = new CompilationTestData(); context.CompileExpression("new A()", out error, testData); Assert.Equal(error, "error CS0246: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)"); testData = new CompilationTestData(); // B should be resolved to BS2. context.CompileExpression("new B()", out error, testData); var methodData = testData.GetMethodData("<>x.<>m0"); methodData.VerifyIL( @"{ // Code size 6 (0x6) .maxstack 1 IL_0000: newobj ""B..ctor()"" IL_0005: ret }"); Assert.Equal(methodData.Method.ReturnType.ContainingAssembly.ToDisplayString(), identityBS2.GetDisplayName()); // B.F should result in missing assembly AS2 since there were no direct references to AS2. ResultProperties resultProperties; ImmutableArray <AssemblyIdentity> missingAssemblyIdentities; testData = new CompilationTestData(); context.CompileExpression( "(new B()).F", DkmEvaluationFlags.None, NoAliases, DebuggerDiagnosticFormatter.Instance, out resultProperties, out error, out missingAssemblyIdentities, EnsureEnglishUICulture.PreferredOrNull, testData); AssertEx.Equal(missingAssemblyIdentities, ImmutableArray.Create(identityAS2)); // Compile expression with method context with all modules. context = EvaluationContext.CreateMethodContext( previous, methodBlocks, symReader, moduleVersionId, methodToken: methodToken, methodVersion: 1, ilOffset: ilOffset, localSignatureToken: localSignatureToken); Assert.Equal(previous.Compilation, context.Compilation); // re-use type context compilation testData = new CompilationTestData(); // A is ambiguous. testData = new CompilationTestData(); context.CompileExpression("new A()", out error, testData); Assert.True(error.StartsWith("error CS0433: The type 'A' exists in both ")); testData = new CompilationTestData(); // B is ambiguous. context.CompileExpression("new B()", out error, testData); Assert.True(error.StartsWith("error CS0433: The type 'B' exists in both ")); // Compile expression with method context with referenced modules only. context = EvaluationContext.CreateMethodContext( methodBlocks.ToCompilationReferencedModulesOnly(moduleVersionId), symReader, moduleVersionId, methodToken: methodToken, methodVersion: 1, ilOffset: ilOffset, localSignatureToken: localSignatureToken); // A is unrecognized since there were no direct references to AS1 or AS2. testData = new CompilationTestData(); context.CompileExpression("new A()", out error, testData); Assert.Equal(error, "error CS0246: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)"); testData = new CompilationTestData(); // B should be resolved to BS2. context.CompileExpression("new B()", out error, testData); methodData = testData.GetMethodData("<>x.<>m0"); methodData.VerifyIL( @"{ // Code size 6 (0x6) .maxstack 1 IL_0000: newobj ""B..ctor()"" IL_0005: ret }"); Assert.Equal(methodData.Method.ReturnType.ContainingAssembly.ToDisplayString(), identityBS2.GetDisplayName()); // B.F should result in missing assembly AS2 since there were no direct references to AS2. testData = new CompilationTestData(); context.CompileExpression( "(new B()).F", DkmEvaluationFlags.None, NoAliases, DebuggerDiagnosticFormatter.Instance, out resultProperties, out error, out missingAssemblyIdentities, EnsureEnglishUICulture.PreferredOrNull, testData); AssertEx.Equal(missingAssemblyIdentities, ImmutableArray.Create(identityAS2)); } }
public void DifferentAssemblyVersion() { var sourceA = @"public class A<T> { }"; var sourceB = @"class B<T> { } class C { static void M() { var o = new A<object>(); } }"; var assemblyNameA = "397300B0-A"; var publicKeyA = ImmutableArray.CreateRange(new byte[] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x06, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x31, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0xED, 0xD3, 0x22, 0xCB, 0x6B, 0xF8, 0xD4, 0xA2, 0xFC, 0xCC, 0x87, 0x37, 0x04, 0x06, 0x04, 0xCE, 0xE7, 0xB2, 0xA6, 0xF8, 0x4A, 0xEE, 0xF3, 0x19, 0xDF, 0x5B, 0x95, 0xE3, 0x7A, 0x6A, 0x28, 0x24, 0xA4, 0x0A, 0x83, 0x83, 0xBD, 0xBA, 0xF2, 0xF2, 0x52, 0x20, 0xE9, 0xAA, 0x3B, 0xD1, 0xDD, 0xE4, 0x9A, 0x9A, 0x9C, 0xC0, 0x30, 0x8F, 0x01, 0x40, 0x06, 0xE0, 0x2B, 0x95, 0x62, 0x89, 0x2A, 0x34, 0x75, 0x22, 0x68, 0x64, 0x6E, 0x7C, 0x2E, 0x83, 0x50, 0x5A, 0xCE, 0x7B, 0x0B, 0xE8, 0xF8, 0x71, 0xE6, 0xF7, 0x73, 0x8E, 0xEB, 0x84, 0xD2, 0x73, 0x5D, 0x9D, 0xBE, 0x5E, 0xF5, 0x90, 0xF9, 0xAB, 0x0A, 0x10, 0x7E, 0x23, 0x48, 0xF4, 0xAD, 0x70, 0x2E, 0xF7, 0xD4, 0x51, 0xD5, 0x8B, 0x3A, 0xF7, 0xCA, 0x90, 0x4C, 0xDC, 0x80, 0x19, 0x26, 0x65, 0xC9, 0x37, 0xBD, 0x52, 0x81, 0xF1, 0x8B, 0xCD }); var compilationA1 = CreateCompilation( new AssemblyIdentity(assemblyNameA, new Version(1, 1, 1, 1), cultureName: "", publicKeyOrToken: publicKeyA, hasPublicKey: true), new[] { sourceA }, references: new[] { MscorlibRef_v20 }, options: TestOptions.DebugDll.WithDelaySign(true)); var referenceA1 = compilationA1.EmitToImageReference(); var assemblyNameB = "397300B0-B"; var compilationB1 = CreateCompilation( new AssemblyIdentity(assemblyNameB, new Version(1, 2, 2, 2)), new[] { sourceB }, references: new[] { MscorlibRef_v20, referenceA1 }, options: TestOptions.DebugDll); // Use mscorlib v4.0.0.0 and A v2.1.2.1 at runtime. byte[] exeBytes; byte[] pdbBytes; ImmutableArray <MetadataReference> references; compilationB1.EmitAndGetReferences(out exeBytes, out pdbBytes, out references); var compilationA2 = CreateCompilation( new AssemblyIdentity(assemblyNameA, new Version(2, 1, 2, 1), cultureName: "", publicKeyOrToken: publicKeyA, hasPublicKey: true), new[] { sourceA }, references: new[] { MscorlibRef_v20 }, options: TestOptions.DebugDll.WithDelaySign(true)); var referenceA2 = compilationA2.EmitToImageReference(); var runtime = CreateRuntimeInstance( assemblyNameB, ImmutableArray.Create(MscorlibRef, referenceA2).AddIntrinsicAssembly(), exeBytes, SymReaderFactory.CreateReader(pdbBytes)); //// typeof(Exception), typeof(A<B<object>>), typeof(B<A<object>[]>) var context = CreateMethodContext( runtime, "C.M"); var aliases = ImmutableArray.Create( ExceptionAlias("System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"), ObjectIdAlias(1, "A`1[[B`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], 397300B0-B, Version=1.2.2.2, Culture=neutral, PublicKeyToken=null]], 397300B0-A, Version=2.1.2.1, Culture=neutral, PublicKeyToken=1f8a32457d187bf3"), ObjectIdAlias(2, "B`1[[A`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]][], 397300B0-A, Version=2.1.2.1, Culture=neutral, PublicKeyToken=1f8a32457d187bf3]], 397300B0-B, Version=1.2.2.2, Culture=neutral, PublicKeyToken=null")); string error; var testData = new CompilationTestData(); context.CompileExpression( "(object)$exception ?? (object)$1 ?? $2", DkmEvaluationFlags.TreatAsExpression, aliases, out error, testData); testData.GetMethodData("<>x.<>m0").VerifyIL( @"{ // Code size 44 (0x2c) .maxstack 2 .locals init (A<object> V_0) //o IL_0000: call ""System.Exception Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetException()"" IL_0005: dup IL_0006: brtrue.s IL_002b IL_0008: pop IL_0009: ldstr ""$1"" IL_000e: call ""object Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetObjectByAlias(string)"" IL_0013: castclass ""A<B<object>>"" IL_0018: dup IL_0019: brtrue.s IL_002b IL_001b: pop IL_001c: ldstr ""$2"" IL_0021: call ""object Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetObjectByAlias(string)"" IL_0026: castclass ""B<A<object>[]>"" IL_002b: ret }"); }
private EvaluationContext CreateMethodContextWithReferences(Compilation comp, string methodName, ImmutableArray <MetadataReference> references) { byte[] exeBytes; byte[] pdbBytes; ImmutableArray <MetadataReference> unusedReferences; var result = comp.EmitAndGetReferences(out exeBytes, out pdbBytes, out unusedReferences); Assert.True(result); var runtime = CreateRuntimeInstance(GetUniqueName(), references, exeBytes, SymReaderFactory.CreateReader(pdbBytes)); return(CreateMethodContext(runtime, methodName)); }
public void PIATypeAndEmbeddedType() { var sourcePIA = @"using System.Runtime.InteropServices; [assembly: PrimaryInteropAssembly(0, 0)] [assembly: Guid(""863D5BC0-46A1-49AC-97AA-A5F0D441A9DC"")] [ComImport] [Guid(""863D5BC0-46A1-49AD-97AA-A5F0D441A9DC"")] public interface I { object F(); }"; var sourceA = @"public class A { public static void M(I x) { } }"; var sourceB = @"class B { static void Main() { I y = null; A.M(y); } }"; var compilationPIA = CreateCompilationWithMscorlib(sourcePIA, options: TestOptions.DebugDll); byte[] exePIA; byte[] pdbPIA; ImmutableArray <MetadataReference> referencesPIA; compilationPIA.EmitAndGetReferences(out exePIA, out pdbPIA, out referencesPIA); var metadataPIA = AssemblyMetadata.CreateFromImage(exePIA); var referencePIA = metadataPIA.GetReference(); // csc /t:library /l:PIA.dll A.cs var compilationA = CreateCompilationWithMscorlib( sourceA, options: TestOptions.DebugDll, assemblyName: ExpressionCompilerUtilities.GenerateUniqueName(), references: new MetadataReference[] { metadataPIA.GetReference(embedInteropTypes: true) }); byte[] exeA; byte[] pdbA; ImmutableArray <MetadataReference> referencesA; compilationA.EmitAndGetReferences(out exeA, out pdbA, out referencesA); var metadataA = AssemblyMetadata.CreateFromImage(exeA); var referenceA = metadataA.GetReference(); // csc /r:A.dll /r:PIA.dll B.cs var compilationB = CreateCompilationWithMscorlib( sourceB, options: TestOptions.DebugExe, assemblyName: Guid.NewGuid().ToString("D"), references: new MetadataReference[] { metadataA.GetReference(), metadataPIA.GetReference() }); byte[] exeB; byte[] pdbB; ImmutableArray <MetadataReference> referencesB; compilationB.EmitAndGetReferences(out exeB, out pdbB, out referencesB); var metadataB = AssemblyMetadata.CreateFromImage(exeB); var referenceB = metadataB.GetReference(); // Create runtime from modules { mscorlib, PIA, A, B }. var modulesBuilder = ArrayBuilder <ModuleInstance> .GetInstance(); modulesBuilder.Add(MscorlibRef.ToModuleInstance(fullImage: null, symReader: null)); modulesBuilder.Add(referenceA.ToModuleInstance(fullImage: exeA, symReader: SymReaderFactory.CreateReader(pdbA))); modulesBuilder.Add(referencePIA.ToModuleInstance(fullImage: null, symReader: null)); modulesBuilder.Add(referenceB.ToModuleInstance(fullImage: exeB, symReader: SymReaderFactory.CreateReader(pdbB))); using (var runtime = new RuntimeInstance(modulesBuilder.ToImmutableAndFree())) { var context = CreateMethodContext(runtime, "A.M"); ResultProperties resultProperties; string error; // Bind to local of embedded PIA type. var testData = new CompilationTestData(); context.CompileExpression("x", out error, testData); Assert.Null(error); testData.GetMethodData("<>x.<>m0").VerifyIL( @"{ // Code size 2 (0x2) .maxstack 1 IL_0000: ldarg.0 IL_0001: ret }"); // Binding to method on original PIA should fail // since it was not included in embedded type. ImmutableArray <AssemblyIdentity> missingAssemblyIdentities; context.CompileExpression( "x.F()", DkmEvaluationFlags.TreatAsExpression, NoAliases, DebuggerDiagnosticFormatter.Instance, out resultProperties, out error, out missingAssemblyIdentities, EnsureEnglishUICulture.PreferredOrNull, testData: null); AssertEx.SetEqual(missingAssemblyIdentities, EvaluationContextBase.SystemCoreIdentity); Assert.Equal(error, "error CS1061: 'I' does not contain a definition for 'F' and no extension method 'F' accepting a first argument of type 'I' could be found (are you missing a using directive or an assembly reference?)"); // Binding to method on original PIA should succeed // in assembly referencing PIA.dll. context = CreateMethodContext(runtime, "B.Main"); testData = new CompilationTestData(); context.CompileExpression("y.F()", out error, testData); Assert.Null(error); testData.GetMethodData("<>x.<>m0").VerifyIL( @"{ // Code size 7 (0x7) .maxstack 1 .locals init (I V_0) //y IL_0000: ldloc.0 IL_0001: callvirt ""object I.F()"" IL_0006: ret }"); } }
public void CorLibWithAssemblyReferences_Pdb() { string sourceLib = @"namespace Namespace { public class Private { } }"; var compLib = CreateStandardCompilation(sourceLib, assemblyName: "System.Private.Library"); compLib.VerifyDiagnostics(); var refLib = compLib.EmitToImageReference(aliases: ImmutableArray.Create("A")); string sourceCorLib = @"extern alias A; #pragma warning disable 8019 using N = A::Namespace; namespace System { public class Object { public void F() { } } #pragma warning disable 0436 public class Void : Object { } #pragma warning restore 0436 }"; // Create a custom corlib with a reference to compilation // above and a reference to the actual mscorlib. var compCorLib = CreateCompilation(sourceCorLib, assemblyName: CorLibAssemblyName, references: new[] { MscorlibRef, refLib }); compCorLib.VerifyDiagnostics(); var objectType = compCorLib.SourceAssembly.GlobalNamespace.GetMember <NamedTypeSymbol>("System.Object"); Assert.NotNull(objectType.BaseType); var pdbPath = Temp.CreateDirectory().Path; ImmutableArray <byte> peBytes; ImmutableArray <byte> pdbBytes; ExpressionCompilerTestHelpers.EmitCorLibWithAssemblyReferences( compCorLib, pdbPath, (moduleBuilder, emitOptions) => new PEAssemblyBuilderWithAdditionalReferences(moduleBuilder, emitOptions, objectType), out peBytes, out pdbBytes); var symReader = SymReaderFactory.CreateReader(pdbBytes); using (var reader = new PEReader(peBytes)) { var metadata = reader.GetMetadata(); var module = metadata.ToModuleMetadata(ignoreAssemblyRefs: true); var metadataReader = metadata.ToMetadataReader(); var moduleInstance = ModuleInstance.Create(metadata, metadataReader.GetModuleVersionIdOrThrow(), symReader); // Verify the module declares System.Object. Assert.True(metadataReader.DeclaresTheObjectClass()); // Verify the PEModule has no assembly references. Assert.Equal(0, module.Module.ReferencedAssemblies.Length); // Verify the underlying metadata has the expected assembly references. var actualReferences = metadataReader.AssemblyReferences.Select(r => metadataReader.GetString(metadataReader.GetAssemblyReference(r).Name)).ToImmutableArray(); AssertEx.Equal(new[] { "mscorlib", "System.Private.Library" }, actualReferences); using (var runtime = RuntimeInstance.Create(new[] { moduleInstance })) { string error; var context = CreateMethodContext(runtime, "System.Object.F"); var testData = new CompilationTestData(); // Invalid import: "using N = A::Namespace;". context.CompileExpression( "new N.Private()", out error, testData); Assert.Equal("error CS0246: The type or namespace name 'N' could not be found (are you missing a using directive or an assembly reference?)", error); } } }
public void TypeOutsideModule() { var sourceA = @"using System; public class A<T> { public static void M(Action f) { object o; try { f(); } catch (Exception) { } } }"; var sourceB = @"using System; class E : Exception { internal object F; } class B { static void Main() { A<int>.M(() => { throw new E(); }); } }"; var assemblyNameA = "0A93FF0B-31A2-47C8-B24D-16A2D77AB5C5"; var compilationA = CreateCompilationWithMscorlib(sourceA, options: TestOptions.DebugDll, assemblyName: assemblyNameA); byte[] exeA; byte[] pdbA; ImmutableArray <MetadataReference> referencesA; compilationA.EmitAndGetReferences(out exeA, out pdbA, out referencesA); var metadataA = AssemblyMetadata.CreateFromImage(exeA); var referenceA = metadataA.GetReference(); var assemblyNameB = "9BAC6622-86EB-4EC5-94A1-9A1E6D0C24B9"; var compilationB = CreateCompilationWithMscorlib(sourceB, options: TestOptions.DebugExe, references: new[] { referenceA }, assemblyName: assemblyNameB); byte[] exeB; byte[] pdbB; ImmutableArray <MetadataReference> referencesB; compilationB.EmitAndGetReferences(out exeB, out pdbB, out referencesB); var metadataB = AssemblyMetadata.CreateFromImage(exeB); var referenceB = metadataB.GetReference(); var modulesBuilder = ArrayBuilder <ModuleInstance> .GetInstance(); modulesBuilder.Add(MscorlibRef.ToModuleInstance(fullImage: null, symReader: null)); modulesBuilder.Add(referenceA.ToModuleInstance(fullImage: exeA, symReader: SymReaderFactory.CreateReader(pdbA))); modulesBuilder.Add(referenceB.ToModuleInstance(fullImage: exeB, symReader: SymReaderFactory.CreateReader(pdbB))); modulesBuilder.Add(ExpressionCompilerTestHelpers.IntrinsicAssemblyReference.ToModuleInstance(fullImage: null, symReader: null)); using (var runtime = new RuntimeInstance(modulesBuilder.ToImmutableAndFree())) { var context = CreateMethodContext( runtime, "A.M"); var aliases = ImmutableArray.Create( ExceptionAlias("E, 9BAC6622-86EB-4EC5-94A1-9A1E6D0C24B9, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"), ObjectIdAlias(1, "A`1[[B, 9BAC6622-86EB-4EC5-94A1-9A1E6D0C24B9, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], 0A93FF0B-31A2-47C8-B24D-16A2D77AB5C5, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")); string error; var testData = new CompilationTestData(); context.CompileExpression( "$exception", DkmEvaluationFlags.TreatAsExpression, aliases, out error, testData); Assert.Null(error); testData.GetMethodData("<>x<T>.<>m0").VerifyIL( @"{ // Code size 11 (0xb) .maxstack 1 .locals init (object V_0) //o IL_0000: call ""System.Exception Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetException()"" IL_0005: castclass ""E"" IL_000a: ret }"); ResultProperties resultProperties; ImmutableArray <AssemblyIdentity> missingAssemblyIdentities; testData = new CompilationTestData(); context.CompileAssignment( "o", "$1", aliases, DebuggerDiagnosticFormatter.Instance, out resultProperties, out error, out missingAssemblyIdentities, EnsureEnglishUICulture.PreferredOrNull, testData); Assert.Empty(missingAssemblyIdentities); Assert.Null(error); testData.GetMethodData("<>x<T>.<>m0").VerifyIL( @"{ // Code size 17 (0x11) .maxstack 1 .locals init (object V_0) //o IL_0000: ldstr ""$1"" IL_0005: call ""object Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetObjectByAlias(string)"" IL_000a: castclass ""A<B>"" IL_000f: stloc.0 IL_0010: ret }"); } }