Beispiel #1
0
        ///////////////////////////////////////////////////////////////////////

        private static bool UnloadNativeLibrary(
            Interpreter interpreter /* NOT USED */
            )
        {
            lock (syncRoot) /* TRANSACTIONAL */
            {
                if (nativeModule == IntPtr.Zero)
                {
                    return(true);
                }

                try
                {
                    UnsetNativeDelegates();

                    int lastError;

                    if (NativeOps.FreeLibrary(
                            nativeModule, out lastError)) /* throw */
                    {
                        nativeModule   = IntPtr.Zero;
                        nativeFileName = null;

                        TraceOps.DebugTrace(
                            "UnloadNativeLibrary: successfully unloaded",
                            typeof(NativeUtility).Name,
                            TracePriority.NativeDebug);

                        return(true);
                    }
                    else
                    {
                        TraceOps.DebugTrace(String.Format(
                                                "FreeLibrary(0x{1:X}) failed with error {0}: {2}",
                                                lastError, nativeModule,
                                                NativeOps.GetDynamicLoadingError(lastError).Trim()),
                                            typeof(NativeUtility).Name,
                                            TracePriority.NativeError);
                    }
                }
                catch (Exception e)
                {
                    TraceOps.DebugTrace(
                        e, typeof(NativeUtility).Name,
                        TracePriority.NativeError);
                }

                return(false);
            }
        }
Beispiel #2
0
        ///////////////////////////////////////////////////////////////////////

        #region Public Methods (Internal Use Only)
        public ReturnCode UnloadNoThrow( /* EXEMPT: object-15.11 */
            ref int loaded,
            ref Result error
            )
        {
            // CheckDisposed(); /* EXEMPT */

            lock (syncRoot) /* TRANSACTIONAL */
            {
                //
                // NOTE: If the module was already loaded previously,
                //       do nothing.
                //
                if (module == IntPtr.Zero)
                {
                    return(ReturnCode.Ok);
                }

                //
                // NOTE: If there are still outstanding references to
                //       the native module, do nothing.
                //
                if (Interlocked.Decrement(ref referenceCount) > 0)
                {
                    return(ReturnCode.Ok);
                }

                //
                // NOTE: If the native module has been locked in place
                //       (because it cannot be cleanly unloaded?), then
                //       leave it alone.
                //
                if (FlagOps.HasFlags(
                        flags, ModuleFlags.NoUnload, true))
                {
                    return(ReturnCode.Ok);
                }

                try
                {
                    int lastError;

                    if (NativeOps.FreeLibrary(
                            module, out lastError)) /* throw */
                    {
                        Interlocked.Decrement(ref loaded);

                        module = IntPtr.Zero;
                        return(ReturnCode.Ok);
                    }
                    else
                    {
                        error = String.Format(
                            "FreeLibrary(0x{1:X}) failed with error {0}: {2}",
                            lastError, module, NativeOps.GetDynamicLoadingError(
                                lastError));
                    }
                }
                catch (Exception e)
                {
                    error = e;
                }
            }

            return(ReturnCode.Error);
        }
Beispiel #3
0
        ///////////////////////////////////////////////////////////////////////

        public ReturnCode Load(
            ref int loaded,
            ref Result error
            )
        {
            CheckDisposed();

            lock (syncRoot) /* TRANSACTIONAL */
            {
                try
                {
                    if (module != IntPtr.Zero)
                    {
                        return(ReturnCode.Ok);
                    }

                    if (String.IsNullOrEmpty(fileName))
                    {
                        error = "invalid file name";
                        return(ReturnCode.Error);
                    }

                    int lastError;

                    module = NativeOps.LoadLibrary(
                        fileName, out lastError); /* throw */

                    if (NativeOps.IsValidHandle(module))
                    {
                        Interlocked.Increment(ref loaded);
                        return(ReturnCode.Ok);
                    }
                    else
                    {
                        error = String.Format(
                            "LoadLibrary({1}) failed with error {0}: {2}",
                            lastError, FormatOps.WrapOrNull(fileName),
                            NativeOps.GetDynamicLoadingError(lastError));
                    }
                }
                catch (Exception e)
                {
                    error = e;
                }
                finally
                {
                    //
                    // NOTE: If the module handle is valid then we know
                    //       the module was loaded successfully -OR- was
                    //       already loaded; therefore, increment the
                    //       reference count.
                    //
                    if (module != IntPtr.Zero)
                    {
                        Interlocked.Increment(ref referenceCount);
                    }
                }
            }

            return(ReturnCode.Error);
        }
Beispiel #4
0
        ///////////////////////////////////////////////////////////////////////

        private ReturnCode PrivateResolve(
            ref Result error,
            ref Exception exception
            )
        {
            lock (syncRoot) /* TRANSACTIONAL */
            {
                if (type == null)
                {
                    error = "invalid type";
                    return(ReturnCode.Error);
                }

                if (!ConversionOps.IsDelegateType(type, false))
                {
                    error = "type is not a delegate type";
                    return(ReturnCode.Error);
                }

                if (module == null)
                {
                    error = "invalid module";
                    return(ReturnCode.Error);
                }

                if (String.IsNullOrEmpty(functionName))
                {
                    error = "invalid export name";
                    return(ReturnCode.Error);
                }

                if (module.Load(ref moduleLoaded, ref error) == ReturnCode.Ok)
                {
                    try
                    {
                        int lastError;

                        address = NativeOps.GetProcAddress(
                            module.Module, functionName,
                            out lastError); /* throw */

                        if (address != IntPtr.Zero)
                        {
                            //
                            // NOTE: The GetDelegateForFunctionPointer method
                            //       of the Marshal class is how we get the
                            //       delegate we need to invoke the library
                            //       function itself and this is why we went
                            //       through all the trouble to creating and
                            //       populating the delegate type dynamically.
                            //       To see exactly how this is accomplished,
                            //       please refer to CreateNativeDelegateType
                            //       in DelegateOps).
                            //
                            @delegate = Marshal.GetDelegateForFunctionPointer(
                                address, type); /* throw */

                            return(ReturnCode.Ok);
                        }
                        else
                        {
                            error = String.Format(
                                "GetProcAddress({1}, \"{2}\") failed with " +
                                "error {0}: {3}", lastError, module,
                                functionName, NativeOps.GetDynamicLoadingError(
                                    lastError));
                        }
                    }
                    catch (Exception e)
                    {
                        error = e;

                        exception = e;
                    }
                }
            }

            return(ReturnCode.Error);
        }
Beispiel #5
0
        ///////////////////////////////////////////////////////////////////////

        private static bool LoadNativeLibrary(
            Interpreter interpreter
            )
        {
            lock (syncRoot) /* TRANSACTIONAL */
            {
                if (nativeModule != IntPtr.Zero)
                {
                    return(true);
                }

                try
                {
                    string fileName = GetNativeLibraryFileName(interpreter);

                    if (!String.IsNullOrEmpty(fileName))
                    {
                        TraceOps.DebugTrace(String.Format(
                                                "LoadNativeLibrary: using file name {0}",
                                                FormatOps.WrapOrNull(fileName)),
                                            typeof(NativeUtility).Name,
                                            TracePriority.NativeDebug);
                    }
                    else
                    {
                        TraceOps.DebugTrace(String.Format(
                                                "LoadNativeLibrary: file name {0} is invalid",
                                                FormatOps.WrapOrNull(fileName)),
                                            typeof(NativeUtility).Name,
                                            TracePriority.NativeError);

                        return(false);
                    }

                    //
                    // NOTE: Check if the native library file name actually
                    //       exists.  If not, do nothing and return failure
                    //       after tracing the issue.
                    //
                    if (!File.Exists(fileName))
                    {
                        TraceOps.DebugTrace(String.Format(
                                                "LoadNativeLibrary: file name {0} does not exist",
                                                FormatOps.WrapOrNull(fileName)),
                                            typeof(NativeUtility).Name,
                                            TracePriority.NativeError);

                        return(false);
                    }

                    //
                    // BUGFIX: Stop loading "untrusted" native libraries
                    //         when running with a "trusted" core library.
                    //
                    if (!RuntimeOps.ShouldLoadNativeLibrary(fileName))
                    {
                        TraceOps.DebugTrace(String.Format(
                                                "LoadNativeLibrary: file name {0} is untrusted",
                                                FormatOps.WrapOrNull(fileName)),
                                            typeof(NativeUtility).Name,
                                            TracePriority.NativeError);

                        return(false);
                    }

                    int lastError;

                    nativeModule = NativeOps.LoadLibrary(
                        fileName, out lastError); /* throw */

                    if (nativeModule != IntPtr.Zero)
                    {
                        InitializeNativeDelegates(true);

                        Result error = null;

                        if (SetNativeDelegates(ref error))
                        {
                            nativeFileName = fileName;

                            TraceOps.DebugTrace(
                                "LoadNativeLibrary: successfully loaded",
                                typeof(NativeUtility).Name,
                                TracePriority.NativeDebug);

                            return(true);
                        }
                        else
                        {
                            TraceOps.DebugTrace(String.Format(
                                                    "LoadNativeLibrary: file name {0} delegate " +
                                                    "setup error: {1}",
                                                    FormatOps.WrapOrNull(fileName), error),
                                                typeof(NativeUtility).Name,
                                                TracePriority.NativeError);

                            /* IGNORED */
                            UnloadNativeLibrary(interpreter);
                        }
                    }
                    else
                    {
                        TraceOps.DebugTrace(String.Format(
                                                "LoadLibrary({1}) failed with error {0}: {2}",
                                                lastError, FormatOps.WrapOrNull(fileName),
                                                NativeOps.GetDynamicLoadingError(lastError).Trim()),
                                            typeof(NativeUtility).Name,
                                            TracePriority.NativeError);
                    }
                }
                catch (Exception e)
                {
                    TraceOps.DebugTrace(
                        e, typeof(NativeUtility).Name,
                        TracePriority.NativeError);
                }

                return(false);
            }
        }