From aa53b1e5efce690ac8607b7dc0d33bf4cb66193e Mon Sep 17 00:00:00 2001 From: Bill Zissimopoulos Date: Sun, 9 Apr 2017 21:37:04 -0700 Subject: [PATCH] src: dotnet: fix UnmanagedFunctionPointer delegate lifetime --- src/dotnet/FileSystem.cs | 59 ++++++++++++++++++++-------------------- src/dotnet/Interop.cs | 5 ++-- src/dotnet/Service.cs | 4 ++- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/dotnet/FileSystem.cs b/src/dotnet/FileSystem.cs index bf829a89..884e7b34 100644 --- a/src/dotnet/FileSystem.cs +++ b/src/dotnet/FileSystem.cs @@ -158,7 +158,7 @@ namespace Fsp Int32 Result; Result = Api.FspFileSystemCreate( _VolumeParams.IsPrefixEmpty() ? "WinFsp.Disk" : "WinFsp.Net", - ref _VolumeParams, _FileSystemInterface, out _FileSystem); + ref _VolumeParams, _FileSystemInterfacePtr, out _FileSystem); if (0 <= Result) { Api.SetUserContext(_FileSystem, this); @@ -1192,38 +1192,37 @@ namespace Fsp static FileSystem() { - FileSystemInterface FileSystemInterface; - FileSystemInterface = default(FileSystemInterface); - FileSystemInterface.GetVolumeInfo = GetVolumeInfo; - FileSystemInterface.SetVolumeLabel = SetVolumeLabel; - FileSystemInterface.GetSecurityByName = GetSecurityByName; - FileSystemInterface.Create = Create; - FileSystemInterface.Open = Open; - FileSystemInterface.Overwrite = Overwrite; - FileSystemInterface.Cleanup = Cleanup; - FileSystemInterface.Close = Close; - FileSystemInterface.Read = Read; - FileSystemInterface.Write = Write; - FileSystemInterface.Flush = Flush; - FileSystemInterface.GetFileInfo = GetFileInfo; - FileSystemInterface.SetBasicInfo = SetBasicInfo; - FileSystemInterface.SetFileSize = SetFileSize; - FileSystemInterface.CanDelete = CanDelete; - FileSystemInterface.Rename = Rename; - FileSystemInterface.GetSecurity = GetSecurity; - FileSystemInterface.SetSecurity = SetSecurity; - FileSystemInterface.ReadDirectory = ReadDirectory; - FileSystemInterface.ResolveReparsePoints = ResolveReparsePoints; - FileSystemInterface.GetReparsePoint = GetReparsePoint; - FileSystemInterface.SetReparsePoint = SetReparsePoint; - FileSystemInterface.DeleteReparsePoint = DeleteReparsePoint; - FileSystemInterface.GetStreamInfo = GetStreamInfo; + _FileSystemInterface.GetVolumeInfo = GetVolumeInfo; + _FileSystemInterface.SetVolumeLabel = SetVolumeLabel; + _FileSystemInterface.GetSecurityByName = GetSecurityByName; + _FileSystemInterface.Create = Create; + _FileSystemInterface.Open = Open; + _FileSystemInterface.Overwrite = Overwrite; + _FileSystemInterface.Cleanup = Cleanup; + _FileSystemInterface.Close = Close; + _FileSystemInterface.Read = Read; + _FileSystemInterface.Write = Write; + _FileSystemInterface.Flush = Flush; + _FileSystemInterface.GetFileInfo = GetFileInfo; + _FileSystemInterface.SetBasicInfo = SetBasicInfo; + _FileSystemInterface.SetFileSize = SetFileSize; + _FileSystemInterface.CanDelete = CanDelete; + _FileSystemInterface.Rename = Rename; + _FileSystemInterface.GetSecurity = GetSecurity; + _FileSystemInterface.SetSecurity = SetSecurity; + _FileSystemInterface.ReadDirectory = ReadDirectory; + _FileSystemInterface.ResolveReparsePoints = ResolveReparsePoints; + _FileSystemInterface.GetReparsePoint = GetReparsePoint; + _FileSystemInterface.SetReparsePoint = SetReparsePoint; + _FileSystemInterface.DeleteReparsePoint = DeleteReparsePoint; + _FileSystemInterface.GetStreamInfo = GetStreamInfo; - _FileSystemInterface = Marshal.AllocHGlobal(Marshal.SizeOf(FileSystemInterface)); - Marshal.StructureToPtr(FileSystemInterface, _FileSystemInterface, false); + _FileSystemInterfacePtr = Marshal.AllocHGlobal(FileSystemInterface.Size); + Marshal.StructureToPtr(_FileSystemInterface, _FileSystemInterfacePtr, false); } - private static IntPtr _FileSystemInterface; + private static FileSystemInterface _FileSystemInterface; + private static IntPtr _FileSystemInterfacePtr; private VolumeParams _VolumeParams; private IntPtr _FileSystem; } diff --git a/src/dotnet/Interop.cs b/src/dotnet/Interop.cs index ba586707..bff71a7b 100644 --- a/src/dotnet/Interop.cs +++ b/src/dotnet/Interop.cs @@ -374,6 +374,8 @@ namespace Fsp.Interop out UInt32 PBytesTransferred); } + internal static int Size = IntPtr.Size * 64; + internal Proto.GetVolumeInfo GetVolumeInfo; internal Proto.SetVolumeLabel SetVolumeLabel; internal Proto.GetSecurityByName GetSecurityByName; @@ -398,8 +400,7 @@ namespace Fsp.Interop internal Proto.SetReparsePoint SetReparsePoint; internal Proto.DeleteReparsePoint DeleteReparsePoint; internal Proto.GetStreamInfo GetStreamInfo; - internal unsafe fixed long/*IntPtr*/ Reserved[40]; - /* NTSTATUS (*Reserved[40])(); */ + /* NTSTATUS (*Reserved[40])(); */ } [SuppressUnmanagedCodeSecurity] diff --git a/src/dotnet/Service.cs b/src/dotnet/Service.cs index 9aaa04e3..af8b88df 100644 --- a/src/dotnet/Service.cs +++ b/src/dotnet/Service.cs @@ -33,7 +33,7 @@ namespace Fsp /* ctor/dtor */ public Service(String ServiceName) { - Api.FspServiceCreate(ServiceName, OnStart, OnStop, null, out _Service); + Api.FspServiceCreate(ServiceName, _OnStart, _OnStop, null, out _Service); if (IntPtr.Zero != _Service) Api.SetUserContext(_Service, this); } @@ -150,6 +150,8 @@ namespace Fsp } } + private static Api.Proto.ServiceStart _OnStart = OnStart; + private static Api.Proto.ServiceStop _OnStop = OnStop; private IntPtr _Service; }