From: Krzysztof Dynowski Date: Mon, 11 Dec 2017 16:00:06 +0000 (+0100) Subject: [TEEC] Fix C# struct Marshaling, binding for null operations X-Git-Tag: 5.0.0-preview1-00420^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=511684b5165e0aed265b88cdc7c39ce78d82235c;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [TEEC] Fix C# struct Marshaling, binding for null operations Change-Id: I28608df6d02d5d3cf853c88f8e0effb100638b55 --- diff --git a/src/Tizen.Security.TEEC/Interop/Interop.Libteec.cs b/src/Tizen.Security.TEEC/Interop/Interop.Libteec.cs index 3700b90..d01fa05 100644 --- a/src/Tizen.Security.TEEC/Interop/Interop.Libteec.cs +++ b/src/Tizen.Security.TEEC/Interop/Interop.Libteec.cs @@ -93,6 +93,9 @@ internal static partial class Interop [DllImport(Libraries.Libteec, EntryPoint = "TEEC_OpenSession", CallingConvention = CallingConvention.Cdecl)] static public extern int OpenSession(ref TEEC_Context context, ref TEEC_Session session, ref TEEC_UUID destination, uint connectionMethod, byte[] connectionData, ref TEEC_Operation operation, out uint returnOrigin); + [DllImport(Libraries.Libteec, EntryPoint = "TEEC_OpenSession", CallingConvention = CallingConvention.Cdecl)] + static public extern int OpenSession(ref TEEC_Context context, ref TEEC_Session session, ref TEEC_UUID destination, uint connectionMethod, byte[] connectionData, IntPtr operation, out uint returnOrigin); + /// /// This function closes a session which has been opened with a trusted application. /// All commands within the session must have completed before this function can be called. @@ -115,6 +118,9 @@ internal static partial class Interop [DllImport(Libraries.Libteec, EntryPoint = "TEEC_InvokeCommand", CallingConvention = CallingConvention.Cdecl)] static public extern int InvokeCommand(ref TEEC_Session session, uint commandID, ref TEEC_Operation operation, out uint returnOrigin); + [DllImport(Libraries.Libteec, EntryPoint = "TEEC_InvokeCommand", CallingConvention = CallingConvention.Cdecl)] + static public extern int InvokeCommand(ref TEEC_Session session, uint commandID, IntPtr operation, out uint returnOrigin); + /// /// This function requests the cancelation of a pending open session operation or a command invocation /// operation. As this is a synchronous API, this function must be called from a thread other than the one diff --git a/src/Tizen.Security.TEEC/Interop/Interop.Types.cs b/src/Tizen.Security.TEEC/Interop/Interop.Types.cs index ece1f6d..bd0ab94 100644 --- a/src/Tizen.Security.TEEC/Interop/Interop.Types.cs +++ b/src/Tizen.Security.TEEC/Interop/Interop.Types.cs @@ -56,38 +56,38 @@ internal static partial class Interop } } - [StructLayout(LayoutKind.Sequential)] - internal class TEEC_SharedMemory + [StructLayout(LayoutKind.Sequential,Pack=8)] + internal struct TEEC_SharedMemory { public IntPtr buffer; - public UInt32 size; + public UIntPtr size; public UInt32 flags; public IntPtr imp; } - [StructLayout(LayoutKind.Sequential)] + [StructLayout(LayoutKind.Sequential,Pack=8)] internal struct TEEC_Value { public UInt32 a; public UInt32 b; } - [StructLayout(LayoutKind.Sequential)] + [StructLayout(LayoutKind.Sequential, Pack=8)] internal struct TEEC_TempMemoryReference { public IntPtr buffer; - public UInt32 size; + public UIntPtr size; } - [StructLayout(LayoutKind.Sequential)] + [StructLayout(LayoutKind.Sequential, Pack=8)] internal struct TEEC_RegisteredMemoryReference { - public TEEC_SharedMemory parent; - public UInt32 size; - public UInt32 offset; + public IntPtr parent; + public UIntPtr size; + public UIntPtr offset; } - [StructLayout(LayoutKind.Explicit)] + [StructLayout(LayoutKind.Explicit, Pack=8)] internal struct TEEC_Parameter { [FieldOffset(0)] @@ -98,7 +98,7 @@ internal static partial class Interop public TEEC_Value value; } - [StructLayout(LayoutKind.Sequential)] + [StructLayout(LayoutKind.Sequential, Pack=8)] internal struct TEEC_Operation { public UInt32 started; diff --git a/src/Tizen.Security.TEEC/Tizen.Security.TEEC/Libteec.cs b/src/Tizen.Security.TEEC/Tizen.Security.TEEC/Libteec.cs index a9e7290..b5c875e 100644 --- a/src/Tizen.Security.TEEC/Tizen.Security.TEEC/Libteec.cs +++ b/src/Tizen.Security.TEEC/Tizen.Security.TEEC/Libteec.cs @@ -110,9 +110,16 @@ namespace Tizen.Security.TEEC public sealed class SharedMemory { internal Interop.TEEC_SharedMemory shm; - internal SharedMemory(Interop.TEEC_SharedMemory shm) + internal IntPtr shmptr; + internal SharedMemory(ref Interop.TEEC_SharedMemory shm) { this.shm=shm; + shmptr = Marshal.AllocHGlobal(Marshal.SizeOf(shm)); + Marshal.StructureToPtr(shm, shmptr, false); + } + ~SharedMemory() + { + Marshal.FreeHGlobal(shmptr); } /// /// This property represents the shared memory size in bytes. @@ -122,7 +129,7 @@ namespace Tizen.Security.TEEC /// partner public UInt32 Size { - get { return shm.size; } + get { return shm.size.ToUInt32(); } } /// /// This property represents the start address of the shared memory block. @@ -251,9 +258,16 @@ namespace Tizen.Security.TEEC base(type) { this.Parent = parent; - this.Size = size; - this.Offset = offset; + if (type == TEFRegisteredMemoryType.Whole) { + this.Size = parent.Size; + this.Offset = 0; + } + else { + this.Size = size; + this.Offset = offset; + } } + /// /// This property represents the shared memory that is referred to. /// @@ -332,82 +346,95 @@ namespace Tizen.Security.TEEC Close(); } - internal UInt32 InitParam(ref Interop.TEEC_Parameter dst, Parameter src) + static internal UInt32 InitParam(ref Interop.TEEC_Parameter[] dst, int i, Parameter src) { switch (src.NativeType) { - case (int)TEFValueType.Input: - case (int)TEFValueType.Output: - case (int)TEFValueType.InOut: - dst.value.a = ((Value)src).A; - dst.value.b = ((Value)src).B; - break; - - case (int)TEFTempMemoryType.Input: - case (int)TEFTempMemoryType.Output: - case (int)TEFTempMemoryType.InOut: - dst.tmpref.buffer = ((TempMemoryReference)src).Buffer; - dst.tmpref.size = ((TempMemoryReference)src).Size; - break; - - case (int)TEFRegisteredMemoryType.Whole: - case (int)TEFRegisteredMemoryType.PartialInput: - case (int)TEFRegisteredMemoryType.PartialOutput: - case (int)TEFRegisteredMemoryType.PartialInOut: - dst.memref.parent = ((RegisteredMemoryReference)src).Parent.shm; - dst.memref.size = ((RegisteredMemoryReference)src).Size; - dst.memref.offset = ((RegisteredMemoryReference)src).Offset; - break; - - default: return 0; + case (int)TEFValueType.Input: + case (int)TEFValueType.Output: + case (int)TEFValueType.InOut: + dst[i].value.a = ((Value)src).A; + dst[i].value.b = ((Value)src).B; + break; + + case (int)TEFTempMemoryType.Input: + case (int)TEFTempMemoryType.Output: + case (int)TEFTempMemoryType.InOut: + dst[i].tmpref.buffer = ((TempMemoryReference)src).Buffer; + dst[i].tmpref.size = new UIntPtr(((TempMemoryReference)src).Size); + break; + + case (int)TEFRegisteredMemoryType.Whole: + case (int)TEFRegisteredMemoryType.PartialInput: + case (int)TEFRegisteredMemoryType.PartialOutput: + case (int)TEFRegisteredMemoryType.PartialInOut: + RegisteredMemoryReference rmr = (RegisteredMemoryReference)src; + dst[i].memref.parent = rmr.Parent.shmptr; + dst[i].memref.size = new UIntPtr(rmr.Size); + dst[i].memref.offset = new UIntPtr(rmr.Offset); + break; + + default: return 0; } return src.NativeType; } - internal void UpdateParam(Interop.TEEC_Parameter src, ref Parameter dst) + static internal void UpdateParam(Interop.TEEC_Parameter src, ref Parameter[] dst, int i) { - switch (dst.NativeType) { + switch (dst[i].NativeType) { case (int)TEFValueType.Input: case (int)TEFValueType.Output: case (int)TEFValueType.InOut: - ((Value)dst).A = src.value.a; - ((Value)dst).B = src.value.b; + ((Value)dst[i]).A = src.value.a; + ((Value)dst[i]).B = src.value.b; break; case (int)TEFTempMemoryType.Input: case (int)TEFTempMemoryType.Output: case (int)TEFTempMemoryType.InOut: - ((TempMemoryReference)dst).Size = src.tmpref.size; + ((TempMemoryReference)dst[i]).Size = src.tmpref.size.ToUInt32(); break; case (int)TEFRegisteredMemoryType.Whole: case (int)TEFRegisteredMemoryType.PartialInput: case (int)TEFRegisteredMemoryType.PartialOutput: case (int)TEFRegisteredMemoryType.PartialInOut: - ((RegisteredMemoryReference)dst).Size = src.memref.size; + ((RegisteredMemoryReference)dst[i]).Size = src.memref.size.ToUInt32(); break; default: break; } } + static internal Interop.TEEC_Operation Create_TEEC_Operation() { + Interop.TEEC_Operation op = new Interop.TEEC_Operation(); + op.started=0; + op.paramTypes=0; + op.paramlist = new Interop.TEEC_Parameter[4]; + return op; + } + internal void Open(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist) { Interop.TEEC_UUID uuid = Interop.TEEC_UUID.ToTeecUuid(destination); - Interop.TEEC_Operation op = new Interop.TEEC_Operation(); - op.started=0; - op.paramTypes=0; - for (int i=0; i < 4 && i < paramlist.Length; ++i) { - op.paramTypes |= InitParam(ref op.paramlist[i], paramlist[i]) << (4*i); + int ret; + uint ro; + if (paramlist != null) { + Interop.TEEC_Operation op = Create_TEEC_Operation(); + for (int i=0; i < 4 && i < paramlist.Length; ++i) { + op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (4*i); + } + ret = Interop.Libteec.OpenSession(ref context, ref session, ref uuid, loginMethod, connectionData, ref op, out ro); + for (int i=0; i < 4 && i < paramlist.Length; ++i) { + UpdateParam(op.paramlist[i], ref paramlist, i); + } + } + else { + ret = Interop.Libteec.OpenSession(ref context, ref session, ref uuid, loginMethod, connectionData, IntPtr.Zero, out ro); } - uint ro; - int ret = Interop.Libteec.OpenSession(ref context, ref session, ref uuid, loginMethod, connectionData, ref op, out ro); //MAYBE map origin of return code to specyfic Exception Interop.CheckNThrowException(ret, string.Format("OpenSession('{0}')", destination)); - for (int i=0; i < 4 && i < paramlist.Length; ++i) { - UpdateParam(op.paramlist[i], ref paramlist[i]); - } } /// @@ -423,7 +450,6 @@ namespace Tizen.Security.TEEC /// The operation is invalid. public void Close() { Interop.Libteec.CloseSession(ref session); - //session = null; } /// @@ -445,20 +471,24 @@ namespace Tizen.Security.TEEC /// The argument is wrong. public void InvokeCommand(uint commandID, Parameter[] paramlist) { - Interop.TEEC_Operation op = new Interop.TEEC_Operation(); - op.started=0; - op.paramTypes=0; - for (int i=0; i < 4 && i < paramlist.Length; ++i) { - op.paramTypes |= InitParam(ref op.paramlist[i], paramlist[i]) << (4*i); + int ret; + uint ro; + if (paramlist != null) { + Interop.TEEC_Operation op = Create_TEEC_Operation(); + for (int i=0; i < 4 && i < paramlist.Length; ++i) { + op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (4*i); + } + ret = Interop.Libteec.InvokeCommand(ref session, commandID, ref op, out ro); + for (int i=0; i < 4 && i < paramlist.Length; ++i) { + UpdateParam(op.paramlist[i], ref paramlist, i); + } + } + else { + ret = Interop.Libteec.InvokeCommand(ref session, commandID, IntPtr.Zero, out ro); } - uint ro; - int ret = Interop.Libteec.InvokeCommand(ref session, commandID, ref op, out ro); //MAYBE map origin of return code to specific Exception Interop.CheckNThrowException(ret, string.Format("InvokeCommand({0})", commandID)); - for (int i=0; i < 4 && i < paramlist.Length; ++i) { - UpdateParam(op.paramlist[i], ref paramlist[i]); - } } /// @@ -655,11 +685,11 @@ namespace Tizen.Security.TEEC { Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory(); shm.buffer = memaddr; - shm.size = size; + shm.size = new UIntPtr(size); shm.flags = (UInt32)flags; int ret = Interop.Libteec.RegisterSharedMemory(ref context, ref shm); Interop.CheckNThrowException(ret, "RegisterSharedMemory"); - return new SharedMemory(shm); + return new SharedMemory(ref shm); } /// @@ -679,11 +709,11 @@ namespace Tizen.Security.TEEC public SharedMemory AllocateSharedMemory(UInt32 size, SharedMemoryFlags flags) { Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory(); - shm.size = size; + shm.size = new UIntPtr(size); shm.flags = (UInt32)flags; int ret = Interop.Libteec.AllocateSharedMemory(ref context, ref shm); Interop.CheckNThrowException(ret, "AllocateSharedMemory"); - return new SharedMemory(shm); + return new SharedMemory(ref shm); } ///