Exemplo n.º 1
0
        public string[] PeVerifyModules(string[] modulesToVerify, bool throwOnError = true)
        {
            // For Windows RT (ARM) THE CLRHelper.Peverify appears to not work and will exclude this
            // for ARM testing at present.
            StringBuilder errors    = new StringBuilder();
            List <string> allOutput = new List <string>();

            foreach (var name in modulesToVerify)
            {
                var assemblyData = _fullNameToAssemblyDataMap[name];
                if (assemblyData.Kind != Kind.ModuleData)
                {
                    continue;
                }

                var      module = assemblyData.ModuleData;
                string[] output = CLRHelpers.PeVerify(module.Image);
                if (output.Length > 0)
                {
                    if (modulesToVerify.Length > 1)
                    {
                        errors.AppendLine();
                        errors.AppendLine("<<" + name + ">>");
                        errors.AppendLine();
                    }

                    foreach (var error in output)
                    {
                        errors.AppendLine(error);
                    }
                }

                if (!throwOnError)
                {
                    allOutput.AddRange(output);
                }
            }

            if (throwOnError && errors.Length > 0)
            {
                string dumpDir;
                RuntimeUtilities.DumpAssemblyData(ModuleDatas, out dumpDir);
                throw new RuntimePeVerifyException(errors.ToString(), dumpDir);
            }
            return(allOutput.ToArray());
        }
Exemplo n.º 2
0
        static int Main(string[] args)
        {
            string assemblyPath = null;
            bool   metadataOnly = false;

            foreach (var arg in args)
            {
                switch (arg.ToUpperInvariant())
                {
                case "/IL":
                case "/NOLOGO":
                case "/UNIQUE":
                    // ignore these options
                    break;

                case "/MD":
                    metadataOnly = true;
                    break;

                default:
                    if (assemblyPath != null)
                    {
                        Console.WriteLine("Assembly already specified or unknown option.");
                        return(-1);
                    }

                    assemblyPath = arg;
                    break;
                }
            }

            if (!Path.IsPathRooted(assemblyPath))
            {
                var workingDir = Directory.GetCurrentDirectory();
                assemblyPath = Path.Combine(workingDir, assemblyPath);
            }

            var errors = CLRHelpers.PeVerify(assemblyPath, metadataOnly);

            foreach (var error in errors)
            {
                Console.WriteLine(error);
            }

            return(errors.Length);
        }
Exemplo n.º 3
0
        private static bool ReflectionEmitInternal(
            Compilation compilation,
            string expectedOutput,
            bool peImageRequested,
            out ImmutableArray <byte> peImage,
            bool refEmitSupported,
            bool fallback,
            bool collectEmittedAssembly,
            bool peVerify,
            TempRoot tempRoot)
        {
            peImage = default(ImmutableArray <byte>);

            var success = false;
            var compilationDependencies = new List <ModuleData>();
            var diagnostics             = new DiagnosticBag();

            HostedRuntimeEnvironment.EmitReferences(compilation, compilationDependencies, diagnostics);

            // allow warnings
            if (diagnostics.HasAnyErrors())
            {
                // this will throw if there are errors
                diagnostics.Verify();
            }

            bool doExecute = expectedOutput != null;

            string fileName;
            string fileNameExt;
            string fileDir;

            TempFile outputFile;
            AssemblyBuilderAccess access;

            if (peImageRequested)
            {
                access = doExecute ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Save;

                // Until HostedExecutionEnvironment supports ref emit, we need to generate a unique temp file.
                // Otherwise, the file will be held open the next time we Emit this same compilation (done in CompileAndVerify).
                outputFile = tempRoot.CreateFile("RefEmit_", ".dll");

                fileNameExt = Path.GetFileName(outputFile.Path);
                fileName    = Path.GetFileNameWithoutExtension(fileNameExt);
                fileDir     = Path.GetDirectoryName(outputFile.Path);
            }
            else
            {
                outputFile = null;

                fileName    = CommonTestBase.GetUniqueName();
                fileNameExt = fileName + ".dll";
                fileDir     = null;

                access = collectEmittedAssembly ? AssemblyBuilderAccess.RunAndCollect : AssemblyBuilderAccess.Run;
            }

            var             assemblyName = new AssemblyIdentity(fileName).ToAssemblyName();
            AssemblyBuilder abuilder     = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, access, fileDir);
            ModuleBuilder   mbuilder     = abuilder.DefineDynamicModule("Module", fileNameExt, emitSymbolInfo: false);

            using (var assemblyManager = new RuntimeAssemblyManager())
            {
                assemblyManager.AddModuleData(compilationDependencies);

                DiagnosticBag emitDiagnostics = DiagnosticBag.GetInstance();
                MethodInfo    entryPoint;

                byte[] compiledAssemblyImage;

                success = compilation.Emit(
                    moduleBuilder: mbuilder,
                    assemblyLoader: new AssemblyLoader(assemblyManager),
                    assemblySymbolMapper: null,
                    recoverOnError: !refEmitSupported && fallback,
                    diagnostics: emitDiagnostics,
                    cancellationToken: default(CancellationToken),
                    entryPoint: out entryPoint,
                    compiledAssemblyImage: out compiledAssemblyImage
                    );

                emitDiagnostics.Free();

                if (success && peImageRequested)
                {
                    if (fallback)
                    {
                        peImage = compiledAssemblyImage.AsImmutableOrNull();
                    }
                    else
                    {
                        abuilder.Save(fileNameExt);
                        peImage = CommonTestBase.ReadFromFile(outputFile.Path);
                    }
                }

                if (refEmitSupported)
                {
                    Assert.True(success, "Expected Ref.Emit success");
                    Assert.Null(compiledAssemblyImage);
                }
                else if (fallback)
                {
                    Assert.True(success, "Expected fallback to CCI");
                    Assert.NotNull(compiledAssemblyImage);
                }
                else
                {
                    Assert.False(success, "Expected emit failure but it succeeded");
                    Assert.Null(compiledAssemblyImage);
                }

                if (success)
                {
                    if (peVerify)
                    {
                        Assert.False(peImage.IsDefault);

                        // Saving AssemblyBuilder to disk changes its manifest module MVID.
                        Guid mvid;
                        using (var metadata = ModuleMetadata.CreateFromImage(peImage))
                        {
                            mvid = metadata.GetModuleVersionId();
                        }

                        assemblyManager.AddMainModuleMvid(mvid);
#if !(ARM)
                        Assert.Equal(String.Empty, CLRHelpers.PeVerify(peImage).Concat());
#endif
                    }

                    if (doExecute)
                    {
                        Assert.NotNull(entryPoint);
                        assemblyManager.AddMainModuleMvid(entryPoint.Module.ModuleVersionId);

                        Action main = GetEntryPointAction(entryPoint);
                        ConsoleOutput.AssertEqual(main, expectedOutput, "");
                    }
                }

                return(success);
            }
        }