/////////////////////////////////////////////////////////////////////// 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); }
/////////////////////////////////////////////////////////////////////// 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); } }