From 3bcc7742d02a31a99c2b3e67836be393f0949595 Mon Sep 17 00:00:00 2001 From: Krzysztof Dynowski Date: Tue, 6 Mar 2018 10:57:20 +0100 Subject: [PATCH] [TEEC] Make SharedMemory disposable (#121) --- .../Tizen.Security.TEEC/Libteec.cs | 91 +++++++++++++++++----- 1 file changed, 70 insertions(+), 21 deletions(-) diff --git a/src/Tizen.Security.TEEC/Tizen.Security.TEEC/Libteec.cs b/src/Tizen.Security.TEEC/Tizen.Security.TEEC/Libteec.cs index ad93795..6965415 100644 --- a/src/Tizen.Security.TEEC/Tizen.Security.TEEC/Libteec.cs +++ b/src/Tizen.Security.TEEC/Tizen.Security.TEEC/Libteec.cs @@ -109,20 +109,61 @@ namespace Tizen.Security.TEEC /// with the implementation or allocated by it. /// /// 3 - public sealed class SharedMemory + public sealed class SharedMemory : IDisposable { + private bool disposed = false; + private bool initialized = false; + private Context context; // keep reference for correct finalizers order internal Interop.TEEC_SharedMemory shm; internal IntPtr shmptr; - internal SharedMemory(ref Interop.TEEC_SharedMemory shm) + internal SharedMemory(Context context) + { + this.context = context; + shmptr = Marshal.AllocHGlobal(Marshal.SizeOf()); + } + internal void setShm(ref Interop.TEEC_SharedMemory shm) { - this.shm=shm; - shmptr = Marshal.AllocHGlobal(Marshal.SizeOf(shm)); + this.shm = shm; Marshal.StructureToPtr(shm, shmptr, false); + initialized = true; + ++context.shmcnt; } - ~SharedMemory() + private void Dispose(bool disposing) { + if (disposed) { + return ; + } + if (context == null) { + throw new Exception("internal error: context is null"); + } + if (initialized) { + Interop.Libteec.ReleaseSharedMemory(ref shm); + initialized = false; + --context.shmcnt; + } Marshal.FreeHGlobal(shmptr); + shmptr = IntPtr.Zero; + context = null; + disposed = true; } + + /// + /// Destructor of the class. + /// + ~SharedMemory() + { + Dispose(false); + } + + /// + /// Disposable interface implememtation. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + /// /// This property represents the shared memory size in bytes. /// @@ -344,8 +385,7 @@ namespace Tizen.Security.TEEC for (int i=0; i < 4; ++i) shm[i] = null; } - // internal since class is sealed - internal void Dispose(bool disposing) + private void Dispose(bool disposing) { if (disposed) { return ; @@ -374,7 +414,8 @@ namespace Tizen.Security.TEEC /// public void Dispose() { - Close(); + Dispose(true); + GC.SuppressFinalize(this); } internal UInt32 InitParam(ref Interop.TEEC_Parameter32[] dst, int i, Parameter src) @@ -570,7 +611,7 @@ namespace Tizen.Security.TEEC for (int i=0; i < 4; ++i) { if (shm[i] != null) { - context.ReleaseSharedMemory(shm[i]); + shm[i].Dispose(); shm[i] = null; } } @@ -603,7 +644,7 @@ namespace Tizen.Security.TEEC for (int i=0; i < 4; ++i) { if (shm[i] != null) { - context.ReleaseSharedMemory(shm[i]); + shm[i].Dispose(); shm[i] = null; } } @@ -625,8 +666,7 @@ namespace Tizen.Security.TEEC /// The required feature is not supported. /// The operation is invalid. public void Close() { - Dispose(true); - GC.SuppressFinalize(this); + Dispose(); } /// @@ -682,7 +722,7 @@ namespace Tizen.Security.TEEC for (int i=0; i < 4; ++i) { if (shm[i] != null) { - context.ReleaseSharedMemory(shm[i]); + shm[i].Dispose(); shm[i] = null; } } @@ -722,6 +762,7 @@ namespace Tizen.Security.TEEC private bool disposed = false; private bool initialized = false; internal IntPtr context_imp; + internal uint shmcnt = 0; /// /// This function (constructor) initializes a new TEE Context, forming a connection between this client application and the @@ -743,7 +784,7 @@ namespace Tizen.Security.TEEC try { int ret = Interop.Libteec.InitializeContext(name, context_imp); Interop.CheckNThrowException(ret, string.Format("InititalizeContext('{0}')", name)); - initialized = true; + initialized = true; } catch (global::System.DllNotFoundException e) { @@ -753,14 +794,14 @@ namespace Tizen.Security.TEEC } } - // internal since class is sealed - internal void Dispose(bool disposing) + private void Dispose(bool disposing) { if (disposed) { return ; } if (initialized) { Interop.Libteec.FinalizeContext(context_imp); + initialized = false; } Marshal.FreeHGlobal(context_imp); context_imp = IntPtr.Zero; @@ -783,6 +824,10 @@ namespace Tizen.Security.TEEC /// partner /// http://tizen.org/feature/security.tee public void Dispose() { + if (shmcnt != 0) { + Tizen.Log.Error("TZ_CLIENTAPI", "Context.Dispose not all shm released yet!"); + return ; + } Dispose(true); GC.SuppressFinalize(this); } @@ -906,13 +951,15 @@ namespace Tizen.Security.TEEC /// The argument is wrong. public SharedMemory RegisterSharedMemory(IntPtr memaddr, UInt32 size, SharedMemoryFlags flags) { + SharedMemory sharedmem = new SharedMemory(this); Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory(); shm.buffer = memaddr; - shm.size = new UIntPtr(size); + shm.size = (UIntPtr)size; shm.flags = (UInt32)flags; int ret = Interop.Libteec.RegisterSharedMemory(context_imp, ref shm); Interop.CheckNThrowException(ret, "RegisterSharedMemory"); - return new SharedMemory(ref shm); + sharedmem.setShm(ref shm); + return sharedmem; } /// @@ -931,12 +978,14 @@ namespace Tizen.Security.TEEC /// The operation is invalid. public SharedMemory AllocateSharedMemory(UInt32 size, SharedMemoryFlags flags) { + SharedMemory sharedmem = new SharedMemory(this); Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory(); - shm.size = new UIntPtr(size); + shm.size = (UIntPtr)size; shm.flags = (UInt32)flags; int ret = Interop.Libteec.AllocateSharedMemory(context_imp, ref shm); Interop.CheckNThrowException(ret, "AllocateSharedMemory"); - return new SharedMemory(ref shm); + sharedmem.setShm(ref shm); + return sharedmem; } /// @@ -962,7 +1011,7 @@ namespace Tizen.Security.TEEC /// The argument is wrong. public void ReleaseSharedMemory(SharedMemory shm) { - Interop.Libteec.ReleaseSharedMemory(ref shm.shm); + shm.Dispose(); } }; -- 2.7.4