[System.Core] Using more reference source files.
authorMarcos Henrich <marcos.henrich@xamarin.com>
Mon, 15 Jun 2015 09:15:41 +0000 (10:15 +0100)
committerMarcos Henrich <marcos.henrich@xamarin.com>
Mon, 15 Jun 2015 15:09:31 +0000 (16:09 +0100)
Changed MemoryMappedFile related code to use reference source
MemoryMappedViewAccessor and MemoryMappedViewStream.

This change was motivated by segmentations faults while dnmv code was
calling a CoreFX class that uses reflection to call MemoryMappedFile
code.

Now that we are using more code from reference sources there should be
no problem.

Fixes #30741 and #30825.

mcs/class/System.Core/ReferenceSources/SR.cs
mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedFile.cs
mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedView.cs [new file with mode: 0644]
mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedViewAccessor.cs [deleted file]
mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedViewStream.cs [deleted file]
mcs/class/System.Core/common_System.Core.dll.sources

index 3c4d8e7ebcfcc3889a3320246c43c45121f4702a..c52050141f7f7d6a3b8e8200fdd77ff4d93adf22 100644 (file)
@@ -100,4 +100,8 @@ partial class SR
        public const string Cryptography_UnknownEllipticCurve = "Cryptography_UnknownEllipticCurve";
        public const string Cryptography_UnknownEllipticCurveAlgorithm = "Cryptography_UnknownEllipticCurveAlgorithm";
        public const string Cryptography_UnknownPaddingMode = "Cryptography_UnknownPaddingMode";
+
+       public const string InvalidOperation_ViewIsNull = "InvalidOperation_ViewIsNull";
+       public const string ObjectDisposed_ViewAccessorClosed = "ObjectDisposed_ViewAccessorClosed";
+       public const string NotSupported_MMViewStreamsFixedLength = "NotSupported_MMViewStreamsFixedLength";
 }
index 5bd03a9fe3b3b9e4ed5b3b679eaee3f1872b1692..5bba4d9209b21555441b2a1620e3efbd62864ca5 100644 (file)
@@ -298,7 +298,8 @@ namespace System.IO.MemoryMappedFiles
 
                public MemoryMappedViewStream CreateViewStream (long offset, long size, MemoryMappedFileAccess access)
                {
-                       return new MemoryMappedViewStream (handle, offset, size, access);
+                       var view = MemoryMappedView.Create (handle, offset, size, access);
+                       return new MemoryMappedViewStream (view);
                }
 
                public MemoryMappedViewAccessor CreateViewAccessor ()
@@ -313,7 +314,8 @@ namespace System.IO.MemoryMappedFiles
 
                public MemoryMappedViewAccessor CreateViewAccessor (long offset, long size, MemoryMappedFileAccess access)
                {
-                       return new MemoryMappedViewAccessor (handle, offset, size, access);
+                       var view = MemoryMappedView.Create (handle, offset, size, access);
+                       return new MemoryMappedViewAccessor (view);
                }
 
                MemoryMappedFile ()
@@ -358,6 +360,34 @@ namespace System.IO.MemoryMappedFiles
                                throw new NotImplementedException ();
                        }
                }
+
+               // This converts a MemoryMappedFileAccess to a FileAccess. MemoryMappedViewStream and
+               // MemoryMappedViewAccessor subclass UnmanagedMemoryStream and UnmanagedMemoryAccessor, which both use
+               // FileAccess to determine whether they are writable and/or readable.
+               internal static FileAccess GetFileAccess (MemoryMappedFileAccess access) {
+
+                       if (access == MemoryMappedFileAccess.Read) {
+                               return FileAccess.Read;
+                       }
+                       if (access == MemoryMappedFileAccess.Write) {
+                               return FileAccess.Write;
+                       }
+                       else if (access == MemoryMappedFileAccess.ReadWrite) {
+                               return FileAccess.ReadWrite;
+                       }
+                       else if (access == MemoryMappedFileAccess.CopyOnWrite) {
+                               return FileAccess.ReadWrite;
+                       }
+                       else if (access == MemoryMappedFileAccess.ReadExecute) {
+                               return FileAccess.Read;
+                       }
+                       else if (access == MemoryMappedFileAccess.ReadWriteExecute) {
+                               return FileAccess.ReadWrite;
+                       }
+
+                       // If we reached here, access was invalid.
+                       throw new ArgumentOutOfRangeException ("access");
+               }
        }
 }
 
diff --git a/mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedView.cs b/mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedView.cs
new file mode 100644 (file)
index 0000000..9362f00
--- /dev/null
@@ -0,0 +1,118 @@
+//
+// MemoryMappedView.cs
+//
+// Authors:
+//     Marcos Henrich (marcos.henrich@gmail.com)
+//
+// Copyright (C) 2015, Xamarin, Inc
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+
+using System;
+using System.IO;
+using System.Diagnostics;
+using System.Collections.Generic;
+using Microsoft.Win32.SafeHandles;
+
+namespace System.IO.MemoryMappedFiles
+{
+       internal class MemoryMappedView : IDisposable {
+               private SafeMemoryMappedViewHandle m_viewHandle;
+               private Int64 m_pointerOffset;
+               private Int64 m_size;
+               private MemoryMappedFileAccess m_access;
+
+               [System.Security.SecurityCritical]
+               private unsafe MemoryMappedView(SafeMemoryMappedViewHandle viewHandle, Int64 pointerOffset, 
+                                                                                       Int64 size, MemoryMappedFileAccess access) {
+
+                       m_viewHandle = viewHandle;
+                       m_pointerOffset = pointerOffset;
+                       m_size = size;
+                       m_access = access;
+               }
+
+               internal SafeMemoryMappedViewHandle ViewHandle {
+                       [System.Security.SecurityCritical]
+                       get {
+                               return m_viewHandle;
+                       }
+               }
+
+               internal Int64 PointerOffset {
+                       get {
+                               return m_pointerOffset;
+                       }
+               }
+
+               internal Int64 Size {
+                       get {
+                               return m_size;
+                       }
+               }
+
+               internal MemoryMappedFileAccess Access {
+                       get {
+                               return m_access;
+                       }
+               }
+
+               internal unsafe static MemoryMappedView Create (IntPtr handle, long offset, long size, MemoryMappedFileAccess access)
+               {
+                       IntPtr base_address;
+                       IntPtr mmap_handle;
+
+                       MemoryMapImpl.Map (handle, offset, ref size, access, out mmap_handle, out base_address);
+
+                       var safe_handle = new SafeMemoryMappedViewHandle (mmap_handle, base_address, size);
+
+                       // MemoryMapImpl.Map returns a base_address to the offset so MemoryMappedView is initiated
+                       // no offset.
+                       return new MemoryMappedView (safe_handle, 0, size, access);
+               }
+
+               public void Flush (IntPtr capacity)
+               {
+                       MemoryMapImpl.Flush (m_viewHandle.DangerousGetHandle ());
+               }
+               
+               protected virtual void Dispose (bool disposing)
+               {
+                       if (m_viewHandle != null && !m_viewHandle.IsClosed) {
+                               m_viewHandle.Dispose ();
+                       }
+               }
+               public void Dispose ()
+               {
+                       Dispose (true);
+                       GC.SuppressFinalize (this);
+               }
+               internal bool IsClosed {
+                       get {
+                               return (m_viewHandle == null || m_viewHandle.IsClosed);
+                       }
+               }
+       }
+}
+
diff --git a/mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedViewAccessor.cs b/mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedViewAccessor.cs
deleted file mode 100644 (file)
index 8bb8fc3..0000000
+++ /dev/null
@@ -1,102 +0,0 @@
-//
-// MemoryMappedViewAccessor.cs
-//
-// Authors:
-//     Zoltan Varga (vargaz@gmail.com)
-//
-// Copyright (C) 2009, Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-
-using System;
-using System.IO;
-using System.Collections.Generic;
-using Microsoft.Win32.SafeHandles;
-
-namespace System.IO.MemoryMappedFiles
-{
-       public sealed class MemoryMappedViewAccessor : UnmanagedMemoryAccessor, IDisposable {
-               IntPtr mmap_handle;
-               SafeMemoryMappedViewHandle safe_handle;
-               long pointerOffset;
-
-               internal MemoryMappedViewAccessor (IntPtr handle, long offset, long size, MemoryMappedFileAccess access)
-               {
-                       pointerOffset = offset;
-                       Create (handle, offset, size, access);
-               }
-
-               public long PointerOffset
-               {
-                       get { return pointerOffset; }
-               }
-
-               static FileAccess ToFileAccess (MemoryMappedFileAccess access)
-               {
-                       switch (access){
-                       case MemoryMappedFileAccess.CopyOnWrite:
-                       case MemoryMappedFileAccess.ReadWrite:
-                       case MemoryMappedFileAccess.ReadWriteExecute:
-                               return FileAccess.ReadWrite;
-                               
-                       case MemoryMappedFileAccess.Write:
-                               return FileAccess.Write;
-                               
-                       case MemoryMappedFileAccess.ReadExecute:
-                       case MemoryMappedFileAccess.Read:
-                       default:
-                               return FileAccess.Read;
-                       }
-               }
-               
-               unsafe void Create (IntPtr handle, long offset, long size, MemoryMappedFileAccess access)
-               {
-                       IntPtr base_address;
-
-                       MemoryMapImpl.Map (handle, offset, ref size, access, out mmap_handle, out base_address);
-                       safe_handle = new SafeMemoryMappedViewHandle (mmap_handle, base_address, size);
-
-                       Initialize (safe_handle, 0, size, ToFileAccess (access));
-               }
-
-               public SafeMemoryMappedViewHandle SafeMemoryMappedViewHandle {
-                       get {
-                               return safe_handle;
-                       }
-               }
-
-               protected override void Dispose (bool disposing)
-               {
-                       base.Dispose (disposing);
-               }
-
-               void IDisposable.Dispose () {
-                       Dispose (true);
-               }
-
-               public void Flush ()
-               {
-                       MemoryMapImpl.Flush (mmap_handle);
-               }
-       }
-}
-
diff --git a/mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedViewStream.cs b/mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedViewStream.cs
deleted file mode 100644 (file)
index 6fbaca6..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-//
-// MemoryMappedViewStream.cs
-//
-// Authors:
-//     Zoltan Varga (vargaz@gmail.com)
-//
-// Copyright (C) 2009, Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-
-using System;
-using System.IO;
-using Microsoft.Win32.SafeHandles;
-
-namespace System.IO.MemoryMappedFiles
-{
-       public sealed class MemoryMappedViewStream : UnmanagedMemoryStream {
-               IntPtr mmap_handle;
-               object monitor;
-               long pointerOffset;
-
-               internal MemoryMappedViewStream (IntPtr handle, long offset, long size, MemoryMappedFileAccess access) {
-                       pointerOffset = offset;
-                       monitor = new Object ();
-                       CreateStream (handle, offset, size, access);
-               }
-
-               public long PointerOffset
-               {
-                       get { return pointerOffset; }
-               }
-
-               public SafeMemoryMappedViewHandle SafeMemoryMappedViewHandle { 
-                       get {
-                               throw new NotImplementedException ();
-                       }
-               }
-
-               unsafe void CreateStream (IntPtr handle, long offset, long size, MemoryMappedFileAccess access)
-               {
-                       IntPtr base_address;
-
-                       MemoryMapImpl.Map (handle, offset, ref size, access, out mmap_handle, out base_address);
-
-                       FileAccess faccess;
-                       switch (access) {
-                       case MemoryMappedFileAccess.ReadWrite:
-                               faccess = FileAccess.ReadWrite;
-                               break;
-                       case MemoryMappedFileAccess.Read:
-                               faccess = FileAccess.Read;
-                               break;
-                       case MemoryMappedFileAccess.Write:
-                               faccess = FileAccess.Write;
-                               break;
-                       default:
-                               throw new NotImplementedException ("access mode " + access + " not supported.");
-                       }
-                       Initialize ((byte*)base_address, size, size, faccess);
-               }
-                
-               protected override void Dispose (bool disposing)
-               {
-                       base.Dispose (disposing);
-                       lock (monitor) {
-                               if (mmap_handle != (IntPtr)(-1)) {
-                                       MemoryMapImpl.Unmap (mmap_handle);
-                                       mmap_handle = (IntPtr)(-1);
-                               }
-                       }
-               }
-
-               public override void Flush ()
-               {
-                       MemoryMapImpl.Flush (mmap_handle);
-               }
-       }
-}
-
index a90d09f09b80542aa643045b2c713b10943c121d..c3524281a3f3cf21bf5834eae295f1c51533e3d1 100644 (file)
@@ -3,8 +3,7 @@
 Assembly/AssemblyInfo.cs
 System/Util.cs
 System.IO.MemoryMappedFiles/MemoryMappedFile.cs
-System.IO.MemoryMappedFiles/MemoryMappedViewStream.cs
-System.IO.MemoryMappedFiles/MemoryMappedViewAccessor.cs
+System.IO.MemoryMappedFiles/MemoryMappedView.cs
 Microsoft.Win32.SafeHandles/SafeMemoryMappedFileHandle.cs
 Microsoft.Win32.SafeHandles/SafeMemoryMappedViewHandle.cs
 
@@ -34,6 +33,8 @@ ReferenceSources/Strings.cs
 
 ../../../external/referencesource/System.Core/System/IO/MemoryMappedFiles/Enums.cs
 ../../../external/referencesource/System.Core/System/IO/MemoryMappedFiles/MemoryMappedFileSecurity.cs
+../../../external/referencesource/System.Core/System/IO/MemoryMappedFiles/MemoryMappedViewAccessor.cs
+../../../external/referencesource/System.Core/System/IO/MemoryMappedFiles/MemoryMappedViewStream.cs
 
 ../../../external/referencesource/System.Core/System/Linq/Enumerable.cs
 ../../../external/referencesource/System.Core/System/Linq/IQueryable.cs