예제 #1
0
        internal Exception GetClasslibException(ExceptionIDs id)
        {
#if INPLACE_RUNTIME
            return RuntimeExceptionHelpers.GetRuntimeException(id);
#else
            return EH.GetClasslibException(id, GetAssociatedModuleAddress());
#endif
        }
예제 #2
0
        public static Exception GetRuntimeException(ExceptionIDs id)
        {
            // This method is called by the runtime's EH dispatch code and is not allowed to leak exceptions
            // back into the dispatcher.
            try
            {
                // @TODO: this function should return pre-allocated exception objects, either frozen in the image
                // or preallocated during DllMain(). In particular, this function will be called when out of memory,
                // and failure to create an exception will result in infinite recursion and therefore a stack overflow.
                switch (id)
                {
                    case ExceptionIDs.OutOfMemory:
                        return PreallocatedOutOfMemoryException.Instance;

                    case ExceptionIDs.Arithmetic:
                        return new ArithmeticException();

                    case ExceptionIDs.ArrayTypeMismatch:
                        return new ArrayTypeMismatchException();

                    case ExceptionIDs.DivideByZero:
                        return new DivideByZeroException();

                    case ExceptionIDs.IndexOutOfRange:
                        return new IndexOutOfRangeException();

                    case ExceptionIDs.InvalidCast:
                        return new InvalidCastException();

                    case ExceptionIDs.Overflow:
                        return new OverflowException();

                    case ExceptionIDs.NullReference:
                        return new NullReferenceException();

                    case ExceptionIDs.AccessViolation:
                        FailFast("Access Violation: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. The application will be terminated since this platform does not support throwing an AccessViolationException.");
                        return null;

                    case ExceptionIDs.DataMisaligned:
                        return new DataMisalignedException();

                    default:
                        FailFast("The runtime requires an exception for a case that this class library does not understand.");
                        return null;
                }
            }
            catch
            {
                return null; // returning null will cause the runtime to FailFast via the class library.
            }
        }
예제 #3
0
        public static Exception GetRuntimeException(ExceptionIDs id)
        {
            // This method is called by the runtime's EH dispatch code and is not allowed to leak exceptions
            // back into the dispatcher.
            try
            {
                // @TODO: this function should return pre-allocated exception objects, either frozen in the image
                // or preallocated during DllMain(). In particular, this function will be called when out of memory,
                // and failure to create an exception will result in infinite recursion and therefore a stack overflow.
                switch (id)
                {
                    case ExceptionIDs.OutOfMemory:
                        return PreallocatedOutOfMemoryException.Instance;

                    case ExceptionIDs.Arithmetic:
                        return new ArithmeticException();

                    case ExceptionIDs.ArrayTypeMismatch:
                        return new ArrayTypeMismatchException();

                    case ExceptionIDs.DivideByZero:
                        return new DivideByZeroException();

                    case ExceptionIDs.IndexOutOfRange:
                        return new IndexOutOfRangeException();

                    case ExceptionIDs.InvalidCast:
                        return new InvalidCastException();

                    case ExceptionIDs.Overflow:
                        return new OverflowException();

                    case ExceptionIDs.NullReference:
                        return new NullReferenceException();

                    case ExceptionIDs.DataMisaligned:
                        // We don't have this in Test.CoreLib
                        return new PlatformNotSupportedException();

                    default:
                        Debug.Assert(false, "unexpected ExceptionID");
                        RuntimeImports.RhpFallbackFailFast();
                        return null;
                }
            }
            catch
            {
                return null; // returning null will cause the runtime to FailFast via the class library.
            }
        }
예제 #4
0
        internal Exception GetClasslibException(ExceptionIDs id)
        {
#if INPLACE_RUNTIME
            return RuntimeExceptionHelpers.GetRuntimeException(id);
#else
            DynamicModule* dynamicModule = this.DynamicModule;
            if (dynamicModule != null)
            {
                IntPtr getRuntimeException = dynamicModule->GetRuntimeException;
                if (getRuntimeException != IntPtr.Zero)
                {
                    return CalliIntrinsics.Call<Exception>(getRuntimeException, id);
                }
            }
            if (IsParameterizedType)
            {
                return RelatedParameterType->GetClasslibException(id);
            }

            return EH.GetClasslibException(id, GetAssociatedModuleAddress());
#endif
        }
예제 #5
0
        public static Exception GetRuntimeException(ExceptionIDs id)
        {
            switch (id)
            {
                case ExceptionIDs.OutOfMemory:
                    // Throw a preallocated exception to avoid infinite recursion.
                    return s_theOOMException;

                case ExceptionIDs.Overflow:
                    return new OverflowException();

                case ExceptionIDs.InvalidCast:
                    return new InvalidCastException();
                    
                default:
                    Debug.Assert(false, "unexpected ExceptionID");
                    FallbackFailFast(RhFailFastReason.InternalError, null);
                    return null;
            }
        }
예제 #6
0
        // Given an ExceptionID and an address pointing somewhere into a managed module, get
        // an exception object of a type that the module containing the given address will understand.
        // This finds the classlib-defined GetRuntimeException function and asks it for the exception object.
        internal static Exception GetClasslibException(ExceptionIDs id, IntPtr address)
        {
            // Find the classlib function that will give us the exception object we want to throw. This
            // is a RuntimeExport function from the classlib module, and is therefore managed-callable.
            IntPtr pGetRuntimeExceptionFunction =
                (IntPtr)InternalCalls.RhpGetClasslibFunction(address, ClassLibFunctionId.GetRuntimeException);

            // Return the exception object we get from the classlib.
            Exception e = null;
            try
            {
                e = CalliIntrinsics.Call<Exception>(pGetRuntimeExceptionFunction, id);
            }
            catch
            {
                // disallow all exceptions leaking out of callbacks
            }

            // If the helper fails to yield an object, then we fail-fast.
            if (e == null)
            {
                FailFastViaClasslib(
                    RhFailFastReason.ClassLibDidNotTranslateExceptionID,
                    null,
                    address);
            }

            return e;
        }
예제 #7
0
        // Given an ExceptionID and an address pointing somewhere into a managed module, get
        // an exception object of a type that the module contianing the given address will understand.
        // This finds the classlib-defined GetRuntimeException function and asks it for the exception object.
        internal static Exception GetClasslibException(ExceptionIDs id, IntPtr address)
        {
            unsafe
            {
                // Find the classlib function that will give us the exception object we want to throw. This
                // is a RuntimeExport function from the classlib module, and is therefore managed-callable.
                void* pGetRuntimeExceptionFunction =
                    InternalCalls.RhpGetClasslibFunction(address, ClassLibFunctionId.GetRuntimeException);

                // Return the exception object we get from the classlib.
                Exception e = null;
                try
                {
                    e = CalliIntrinsics.Call<Exception>((IntPtr)pGetRuntimeExceptionFunction, id);
                }
                catch
                {
                    // Unfortunately, this catch turns into "catch (System.Object)", which will not catch 
                    // exceptions thrown from the class library because their objects do not derive from our 
                    // System.Object. 
                    //
                    // @TODO: Use a filtered catch whose filter always returns 'true'.
                }

                // If the helper fails to yield an object, then we fail-fast.
                if (e == null)
                {
                    FailFastViaClasslib(
                        RhFailFastReason.ClassLibDidNotTranslateExceptionID,
                        null,
                        address);
                }

                return e;
            }
        }
예제 #8
0
 internal static T Call <T>(IntPtr pfn, ExceptionIDs arg0)
 {
     throw new NotImplementedException();
 }
예제 #9
0
        public static void RhThrowHwEx(uint exceptionCode, ref ExInfo exInfo)
        {
            // trigger a GC (only if gcstress) to ensure we can stackwalk at this point
            GCStress.TriggerGC();

            InternalCalls.RhpValidateExInfoStack();

            IntPtr       faultingCodeAddress = exInfo._pExContext->IP;
            bool         instructionFault    = true;
            ExceptionIDs exceptionId         = default(ExceptionIDs);
            Exception    exceptionToThrow    = null;

            switch (exceptionCode)
            {
            case (uint)HwExceptionCode.STATUS_REDHAWK_NULL_REFERENCE:
                exceptionId = ExceptionIDs.NullReference;
                break;

            case (uint)HwExceptionCode.STATUS_REDHAWK_WRITE_BARRIER_NULL_REFERENCE:
                // The write barrier where the actual fault happened has been unwound already.
                // The IP of this fault needs to be treated as return address, not as IP of
                // faulting instruction.
                instructionFault = false;
                exceptionId      = ExceptionIDs.NullReference;
                break;

            case (uint)HwExceptionCode.STATUS_REDHAWK_THREAD_ABORT:
                exceptionToThrow = InternalCalls.RhpGetThreadAbortException();
                break;

            case (uint)HwExceptionCode.STATUS_DATATYPE_MISALIGNMENT:
                exceptionId = ExceptionIDs.DataMisaligned;
                break;

            // N.B. -- AVs that have a read/write address lower than 64k are already transformed to
            //         HwExceptionCode.REDHAWK_NULL_REFERENCE prior to calling this routine.
            case (uint)HwExceptionCode.STATUS_ACCESS_VIOLATION:
                exceptionId = ExceptionIDs.AccessViolation;
                break;

            case (uint)HwExceptionCode.STATUS_INTEGER_DIVIDE_BY_ZERO:
                exceptionId = ExceptionIDs.DivideByZero;
                break;

            case (uint)HwExceptionCode.STATUS_INTEGER_OVERFLOW:
                exceptionId = ExceptionIDs.Overflow;
                break;

            default:
                // We don't wrap SEH exceptions from foreign code like CLR does, so we believe that we
                // know the complete set of HW faults generated by managed code and do not need to handle
                // this case.
                FailFastViaClasslib(RhFailFastReason.InternalError, null, faultingCodeAddress);
                break;
            }

            if (exceptionId != default(ExceptionIDs))
            {
                exceptionToThrow = GetClasslibException(exceptionId, faultingCodeAddress);
            }

            exInfo.Init(exceptionToThrow, instructionFault);
            DispatchEx(ref exInfo._frameIter, ref exInfo, MaxTryRegionIdx);
            FallbackFailFast(RhFailFastReason.InternalError, null);
        }
예제 #10
0
        public static Exception?GetRuntimeException(ExceptionIDs id)
        {
            if (!SafeToPerformRichExceptionSupport)
            {
                return(null);
            }

            // This method is called by the runtime's EH dispatch code and is not allowed to leak exceptions
            // back into the dispatcher.
            try
            {
                // @TODO: this function should return pre-allocated exception objects, either frozen in the image
                // or preallocated during DllMain(). In particular, this function will be called when out of memory,
                // and failure to create an exception will result in infinite recursion and therefore a stack overflow.
                switch (id)
                {
                case ExceptionIDs.OutOfMemory:
                    Exception outOfMemoryException = PreallocatedOutOfMemoryException.Instance;

                    // If possible, try to allocate proper out-of-memory exception with error message and stack trace
                    if (!t_allocatingOutOfMemoryException)
                    {
                        t_allocatingOutOfMemoryException = true;
                        try
                        {
                            outOfMemoryException = new OutOfMemoryException();
                        }
                        catch
                        {
                        }
                        t_allocatingOutOfMemoryException = false;
                    }

                    return(outOfMemoryException);

                case ExceptionIDs.Arithmetic:
                    return(new ArithmeticException());

                case ExceptionIDs.ArrayTypeMismatch:
                    return(new ArrayTypeMismatchException());

                case ExceptionIDs.DivideByZero:
                    return(new DivideByZeroException());

                case ExceptionIDs.IndexOutOfRange:
                    return(new IndexOutOfRangeException());

                case ExceptionIDs.InvalidCast:
                    return(new InvalidCastException());

                case ExceptionIDs.Overflow:
                    return(new OverflowException());

                case ExceptionIDs.NullReference:
                    return(new NullReferenceException());

                case ExceptionIDs.AccessViolation:
                    FailFast("Access Violation: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. The application will be terminated since this platform does not support throwing an AccessViolationException.");
                    return(null);

                case ExceptionIDs.DataMisaligned:
                    return(new DataMisalignedException());

                case ExceptionIDs.EntrypointNotFound:
                    return(new EntryPointNotFoundException());

                case ExceptionIDs.AmbiguousImplementation:
                    return(new AmbiguousImplementationException());

                default:
                    FailFast("The runtime requires an exception for a case that this class library does not understand.");
                    return(null);
                }
            }
            catch
            {
                return(null); // returning null will cause the runtime to FailFast via the class library.
            }
        }