예제 #1
0
        public static void DynamicCode()
        {
            Assert.Equal(RuntimeFeature.IsDynamicCodeSupported, RuntimeFeature.IsSupported("IsDynamicCodeSupported"));
            Assert.Equal(RuntimeFeature.IsDynamicCodeCompiled, RuntimeFeature.IsSupported("IsDynamicCodeCompiled"));

            if (RuntimeFeature.IsDynamicCodeCompiled)
            {
                Assert.True(RuntimeFeature.IsDynamicCodeSupported);
            }
        }
예제 #2
0
        public void NoNewFeaturesAdded()
        {
            var t        = typeof(RuntimeFeature);
            var features = from field in t.GetFields()
                           where field.FieldType == typeof(string)
                           let value = field.GetValue(null)
                                       select new KeyValuePair <string, bool> (
                field.Name,
                RuntimeFeature.IsSupported((string)value)
                );

            if (features.Count() == 0)
            {
                Assert.Inconclusive("No features found, this can happen when running the linker.");
            }

            CollectionAssert.AreEquivalent(ExpectedFeatures, features.ToDictionary(k => k.Key, v => v.Value));
        }
예제 #3
0
        internal ServiceProvider(IEnumerable <ServiceDescriptor> serviceDescriptors, ServiceProviderOptions options)
        {
            IServiceProviderEngineCallback callback = null;

            if (options.ValidateScopes)
            {
                callback           = this;
                _callSiteValidator = new CallSiteValidator();
            }

            switch (options.Mode)
            {
            case ServiceProviderMode.Default:
#if !NETCOREAPP
                _engine = new DynamicServiceProviderEngine(serviceDescriptors, callback);
#else
                if (RuntimeFeature.IsSupported("IsDynamicCodeCompiled"))
                {
                    _engine = new DynamicServiceProviderEngine(serviceDescriptors, callback);
                }
                else
                {
                    // Don't try to compile Expressions/IL if they are going to get interpreted
                    _engine = new RuntimeServiceProviderEngine(serviceDescriptors, callback);
                }
#endif
                break;

            case ServiceProviderMode.Dynamic:
                _engine = new DynamicServiceProviderEngine(serviceDescriptors, callback);
                break;

            case ServiceProviderMode.Runtime:
                _engine = new RuntimeServiceProviderEngine(serviceDescriptors, callback);
                break;

#if IL_EMIT
            case ServiceProviderMode.ILEmit:
                _engine = new ILEmitServiceProviderEngine(serviceDescriptors, callback);
                break;
#endif
            case ServiceProviderMode.Expressions:
                _engine = new ExpressionsServiceProviderEngine(serviceDescriptors, callback);
                break;

            default:
                throw new NotSupportedException(nameof(options.Mode));
            }

            if (options.ValidateOnBuild)
            {
                List <Exception> exceptions = null;
                foreach (var serviceDescriptor in serviceDescriptors)
                {
                    try
                    {
                        _engine.ValidateService(serviceDescriptor);
                    }
                    catch (Exception e)
                    {
                        exceptions = exceptions ?? new List <Exception>();
                        exceptions.Add(e);
                    }
                }

                if (exceptions != null)
                {
                    throw new AggregateException("Some services are not able to be constructed", exceptions.ToArray());
                }
            }
        }
 public static void PortablePdb()
 {
     Assert.True(RuntimeFeature.IsSupported("PortablePdb"));
 }
예제 #5
0
 public static void StaticDataMatchesDynamicProbing(string probedValue)
 {
     Assert.True(RuntimeFeature.IsSupported(probedValue));
 }
예제 #6
0
 public void NonExistingFeatureNotSupported()
 {
     Assert.IsFalse(RuntimeFeature.IsSupported("foo"));
 }
예제 #7
0
 public void DefaultImplementationsOfInterfacesSupported()
 {
     Assert.IsTrue(RuntimeFeature.IsSupported(RuntimeFeature.DefaultImplementationsOfInterfaces));
 }
예제 #8
0
 public void PortablePdbSupported()
 {
     Assert.IsTrue(RuntimeFeature.IsSupported(RuntimeFeature.PortablePdb));
 }
예제 #9
0
        internal void InitializeSourceInfo(int iSkip, bool fNeedFileInfo, Exception exception)
        {
            StackTrace.GetStackFramesInternal(this, iSkip, fNeedFileInfo, exception);
            if (!fNeedFileInfo)
            {
                return;
            }

            // For back compat we opt-out of using Portable PDBs before 4.7.2. See the comments in
            // RuntimeFeature for more details.
            //
            // Even if our compat policy for enabling the feature changes, make sure that
            // RuntimeFeature.IsSupported accurately encapsulates that policy. Our API contract with
            // tools is that we will accurately tell them whether or not Portable PDB is supported.
            if (!RuntimeFeature.IsSupported(RuntimeFeature.PortablePdb))
            {
                return;
            }

            // Check if this function is being reentered because of an exception in the code below
            if (t_reentrancy > 0)
            {
                return;
            }

            t_reentrancy++;
            try
            {
                // need private reflection below + unmanaged code for the portable PDB access itself
                // PERF: these demands are somewhat expensive so do the quick check first. We are aiming for
                // ~50k traces/s at 5 frames/trace on decent 2017 era hardware to maintain rough performance
                // parity with 4.7 implementation that didn't have Portable PDB support
                if (!CodeAccessSecurityEngine.QuickCheckForAllDemands())
                {
                    new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert();
                    new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
                }

                if (s_getSourceLineInfo == null)
                {
                    Type symbolsType = Type.GetType(
                        "System.Diagnostics.StackTraceSymbols, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                        throwOnError: false);

                    if (symbolsType == null)
                    {
                        return;
                    }

                    MethodInfo symbolsMethodInfo = symbolsType.GetMethod("GetSourceLineInfoWithoutCasAssert",
                                                                         new Type[] { typeof(string),
                                                                                      typeof(IntPtr),
                                                                                      typeof(int),
                                                                                      typeof(IntPtr),
                                                                                      typeof(int),
                                                                                      typeof(int),
                                                                                      typeof(int),
                                                                                      typeof(string).MakeByRefType(),
                                                                                      typeof(int).MakeByRefType(),
                                                                                      typeof(int).MakeByRefType() });

                    // We can't take a servicing dependency that System.Core.dll has been upgraded. If for whatever
                    // wacky reason we still have the old version of System.Core.dll fallback to the original less
                    // performant implementation of the method.
                    if (symbolsMethodInfo == null)
                    {
                        symbolsMethodInfo = symbolsType.GetMethod("GetSourceLineInfo",
                                                                  new Type[] { typeof(string),
                                                                               typeof(IntPtr),
                                                                               typeof(int),
                                                                               typeof(IntPtr),
                                                                               typeof(int),
                                                                               typeof(int),
                                                                               typeof(int),
                                                                               typeof(string).MakeByRefType(),
                                                                               typeof(int).MakeByRefType(),
                                                                               typeof(int).MakeByRefType() });
                    }

                    if (symbolsMethodInfo == null)
                    {
                        return;
                    }

                    // Create an instance of System.Diagnostics.Stacktrace.Symbols
                    object target = Activator.CreateInstance(symbolsType);

                    // Create an instance delegate for the GetSourceLineInfo method
                    GetSourceLineInfoDelegate getSourceLineInfo = (GetSourceLineInfoDelegate)symbolsMethodInfo.CreateDelegate(typeof(GetSourceLineInfoDelegate), target);

                    // We could ---- with another thread. It doesn't matter if we win or lose, the losing instance will be GC'ed and all threads including this one will
                    // use the winning instance
                    Interlocked.CompareExchange(ref s_getSourceLineInfo, getSourceLineInfo, null);
                }

                for (int index = 0; index < iFrameCount; index++)
                {
                    // If there was some reason not to try get get the symbols from the portable PDB reader like the module was
                    // ENC or the source/line info was already retrieved, the method token is 0.
                    if (rgiMethodToken[index] != 0)
                    {
                        s_getSourceLineInfo(rgAssemblyPath[index], rgLoadedPeAddress[index], rgiLoadedPeSize[index],
                                            rgInMemoryPdbAddress[index], rgiInMemoryPdbSize[index], rgiMethodToken[index],
                                            rgiILOffset[index], out rgFilename[index], out rgiLineNumber[index], out rgiColumnNumber[index]);
                    }
                }
            }
            catch
            {
            }
            finally
            {
                t_reentrancy--;
            }
        }
예제 #10
0
 internal void InitializeSourceInfo(int iSkip, bool fNeedFileInfo, Exception exception)
 {
     StackTrace.GetStackFramesInternal(this, iSkip, fNeedFileInfo, exception);
     if (!fNeedFileInfo)
     {
         return;
     }
     if (!RuntimeFeature.IsSupported("PortablePdb"))
     {
         return;
     }
     if (StackFrameHelper.t_reentrancy > 0)
     {
         return;
     }
     StackFrameHelper.t_reentrancy++;
     try
     {
         if (!CodeAccessSecurityEngine.QuickCheckForAllDemands())
         {
             new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert();
             new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
         }
         if (StackFrameHelper.s_getSourceLineInfo == null)
         {
             Type type = Type.GetType("System.Diagnostics.StackTraceSymbols, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false);
             if (type == null)
             {
                 return;
             }
             MethodInfo method = type.GetMethod("GetSourceLineInfoWithoutCasAssert", new Type[]
             {
                 typeof(string),
                 typeof(IntPtr),
                 typeof(int),
                 typeof(IntPtr),
                 typeof(int),
                 typeof(int),
                 typeof(int),
                 typeof(string).MakeByRefType(),
                 typeof(int).MakeByRefType(),
                 typeof(int).MakeByRefType()
             });
             if (method == null)
             {
                 method = type.GetMethod("GetSourceLineInfo", new Type[]
                 {
                     typeof(string),
                     typeof(IntPtr),
                     typeof(int),
                     typeof(IntPtr),
                     typeof(int),
                     typeof(int),
                     typeof(int),
                     typeof(string).MakeByRefType(),
                     typeof(int).MakeByRefType(),
                     typeof(int).MakeByRefType()
                 });
             }
             if (method == null)
             {
                 return;
             }
             object target = Activator.CreateInstance(type);
             StackFrameHelper.GetSourceLineInfoDelegate value = (StackFrameHelper.GetSourceLineInfoDelegate)method.CreateDelegate(typeof(StackFrameHelper.GetSourceLineInfoDelegate), target);
             Interlocked.CompareExchange <StackFrameHelper.GetSourceLineInfoDelegate>(ref StackFrameHelper.s_getSourceLineInfo, value, null);
         }
         for (int i = 0; i < this.iFrameCount; i++)
         {
             if (this.rgiMethodToken[i] != 0)
             {
                 StackFrameHelper.s_getSourceLineInfo(this.rgAssemblyPath[i], this.rgLoadedPeAddress[i], this.rgiLoadedPeSize[i], this.rgInMemoryPdbAddress[i], this.rgiInMemoryPdbSize[i], this.rgiMethodToken[i], this.rgiILOffset[i], out this.rgFilename[i], out this.rgiLineNumber[i], out this.rgiColumnNumber[i]);
             }
         }
     }
     catch
     {
     }
     finally
     {
         StackFrameHelper.t_reentrancy--;
     }
 }