Skip to content

Commit

Permalink
fix: misaligned pointer dereference.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Jun 7, 2024
1 parent c3073f7 commit d53ef6b
Show file tree
Hide file tree
Showing 5 changed files with 2,697 additions and 1,419 deletions.
12 changes: 6 additions & 6 deletions Runtime/Scripts/Internal/FFIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,17 @@ public FfiResponse SendRequest(FfiRequest request)
using var memory = memoryPool.Memory(request);
var data = memory.Span();
request.WriteTo(data);

UIntPtr dataInLen = new UIntPtr((UInt64)data.Length);
fixed (byte* requestDataPtr = data)
{
var handle = NativeMethods.FfiNewRequest(
requestDataPtr,
data.Length,
dataInLen,
out byte* dataPtr,
out int dataLen
out UIntPtr dataLen
);

var dataSpan = new Span<byte>(dataPtr, dataLen);
var dataSpan = new Span<byte>(dataPtr, (int)dataLen.ToUInt32());
var response = responseParser.ParseFrom(dataSpan)!;
NativeMethods.FfiDropHandle(handle);
return response;
Expand All @@ -198,15 +198,15 @@ out int dataLen


[AOT.MonoPInvokeCallback(typeof(FFICallbackDelegate))]
static unsafe void FFICallback(IntPtr data, int size)
static unsafe void FFICallback(IntPtr data, UIntPtr size)
{
#if NO_LIVEKIT_MODE
return;
#endif

if (_isDisposed) return;

var respData = new Span<byte>(data.ToPointer()!, size);
var respData = new Span<byte>(data.ToPointer()!, (int)size.ToUInt32());
var response = FfiEvent.Parser!.ParseFrom(respData);

// Run on the main thread, the order of execution is guaranteed by Unity
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/Internal/FFIClients/FFIEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace LiveKit.Internal
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void FFICallbackDelegate(IntPtr data, int size);
internal delegate void FFICallbackDelegate(IntPtr data, UIntPtr size);

// Callbacks
internal delegate void PublishTrackDelegate(PublishTrackCallback e);
Expand Down
4 changes: 3 additions & 1 deletion Runtime/Scripts/Internal/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;

using FfiHandleId = System.UInt64;

namespace LiveKit.Internal
{
[SuppressUnmanagedCodeSecurity]
Expand All @@ -19,7 +21,7 @@ internal static class NativeMethods
internal extern static bool FfiDropHandle(IntPtr handleId);

[DllImport(Lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "livekit_ffi_request")]
internal extern static unsafe IntPtr FfiNewRequest(byte* data, int len, out byte* dataPtr, out int dataLen);
internal extern static unsafe IntPtr FfiNewRequest(byte* data, UIntPtr len, out byte* dataPtr, out UIntPtr dataLen);

//TODO optimise FfiHandle, can be replaced by FfiHandleId = uint64_t
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "livekit_ffi_initialize")]
Expand Down
Loading

0 comments on commit d53ef6b

Please sign in to comment.