Esempio n. 1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="InstrumentHelper" /> class.
        /// </summary>
        /// <param name="targetAssembly">The target assembly.</param>
        /// <param name="bootstrapAssembly">The bootstrap assembly (optional).</param>
        public InstrumentHelper(
            AssemblyDefinition targetAssembly,
            AssemblyDefinition bootstrapAssembly = null)
        {
            var frameworkVersion = MsilUtilities.GetFrameworkVersion(targetAssembly);

            _sourceAssemblyImage = GetInjectedAssemblyImage(frameworkVersion);
            _sourceAssembly      = MsilUtilities.LoadAssembly(_sourceAssemblyImage);

            if (bootstrapAssembly == null)
            {
                _bootstrapAssemblyImage = GetBootstrapAssemblyImage(frameworkVersion);
                _bootstrapAssembly      = MsilUtilities.LoadAssembly(_bootstrapAssemblyImage);
            }
            else
            {
                _bootstrapAssembly = bootstrapAssembly;
                // TODO:MAK it should not be needed, but it would be nice if it gets populated in the future
                _bootstrapAssemblyImage = null;
            }

            if (_sourceAssembly == null || _bootstrapAssembly == null)
            {
                throw new ArgumentException(string.Format(
                                                "Instrumentation assembly could not be found for framework version '{0}'", frameworkVersion));
            }

            _targetAssembly = targetAssembly;
        }
Esempio n. 2
0
        /// <summary>Validates if LibZResolver can be injected.</summary>
        /// <param name="assembly">The target assembly.</param>
        /// <exception cref="System.ArgumentException">If assembly is targeting unsupported version.</exception>
        protected static void ValidateLibZInstrumentation(AssemblyDefinition assembly)
        {
            var version = MsilUtilities.GetFrameworkVersion(assembly);

            if (version >= new Version("4.0.0.0"))
            {
                return;
            }
            if (version < new Version("2.0.0.0") || version == new Version("2.0.5.0"))
            {
                throw new ArgumentException(
                          string.Format("Cannot inject code into assemblies targeting '{0}'", version));
            }
            if (version < new Version("3.5.0.0"))
            {
                Log.Warn(string.Format("Attempting to inject assemblies into assembly targeting '{0}'.", version));
                Log.Warn("LibZResolver will work only if .NET 3.5 is also installed on target machine");
            }
        }
Esempio n. 3
0
        /// <summary>Validates if AsmZResolver can be injected.</summary>
        /// <param name="assembly">The target assembly.</param>
        /// <exception cref="System.ArgumentException">If assembly is targeting unsupported version.</exception>
        protected static void ValidateAsmZInstrumentation(AssemblyDefinition assembly)
        {
            var version = MsilUtilities.GetFrameworkVersion(assembly);

            if (version >= new Version("4.0.0.0"))
            {
                return;
            }
            if (version < new Version("2.0.0.0") || version == new Version("2.0.5.0"))
            {
                throw new ArgumentException(
                          string.Format("Cannot inject code into assemblies targeting '{0}'", version));
            }
            if (version < new Version("3.5.0.0"))
            {
                Log.Warn(string.Format("Attempting to inject AsmZResolver into assembly targeting framework '{0}'.", version));
                Log.Warn("AsmZResolver should work but is neither designed nor tested with this framework.");
            }
        }
Esempio n. 4
0
        /// <summary>Executes the task.</summary>
        /// <param name="mainFileName">Name of the main file.</param>
        /// <param name="allLibZResources">
        ///     if set to <c>true</c> loads all LibZ files in resources on startup.
        /// </param>
        /// <param name="libzFiles">The LibZ files to be loaded on startup.</param>
        /// <param name="libzPatterns">The libz file patterns to be loaded on startup.</param>
        /// <param name="keyFileName">Name of the key file.</param>
        /// <param name="keyFilePassword">The key file password.</param>
        public virtual void Execute(
            string mainFileName,
            bool allLibZResources,
            string[] libzFiles,
            string[] libzPatterns,
            string keyFileName, string keyFilePassword)
        {
            if (!File.Exists(mainFileName))
            {
                throw FileNotFound(mainFileName);
            }
            if (libzFiles == null)
            {
                libzFiles = new string[0];
            }
            if (libzPatterns == null)
            {
                libzPatterns = new string[0];
            }

            var targetAssembly = MsilUtilities.LoadAssembly(mainFileName);

            ValidateLibZInstrumentation(targetAssembly);

            var keyPair = MsilUtilities.LoadKeyPair(keyFileName, keyFilePassword);
            var requiresAsmZResolver = false;

            var bootstrapAssembly =
                FindBootstrapAssembly(targetAssembly, mainFileName);

            if (bootstrapAssembly == null)
            {
                var version = MsilUtilities.GetFrameworkVersion(targetAssembly);

                var bootstrapAssemblyImage = InstrumentHelper.GetBootstrapAssemblyImage(version);
                bootstrapAssembly = MsilUtilities.LoadAssembly(bootstrapAssemblyImage);

                if (bootstrapAssembly == null)
                {
                    throw new ArgumentException("LibZ.Bootstrap has not been found");
                }

                Log.Info("Using built in LibZResolver");

                InjectDll(
                    targetAssembly,
                    bootstrapAssembly,
                    bootstrapAssemblyImage,
                    true);
                requiresAsmZResolver = true;
            }

            _instrumentHelper = new InstrumentHelper(
                targetAssembly,
                bootstrapAssembly);

            _instrumentHelper.InjectLibZInitializer();
            if (requiresAsmZResolver)
            {
                _instrumentHelper.InjectAsmZResolver();
            }
            _instrumentHelper.InjectLibZStartup(allLibZResources, libzFiles, libzPatterns);

            MsilUtilities.SaveAssembly(targetAssembly, mainFileName, keyPair);
        }