Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix illegal delegate usage
Cache the delegates to native code we've created so that when we need to
call that delegate, we don't ask for a delegate from a native pointer
(that is a delegate to managed code.)
  • Loading branch information
BadSingleton committed Dec 17, 2020
commit 5d9f76f4815f8c2c178b76f1bc6644363d6df2d4
4 changes: 4 additions & 0 deletions src/runtime/interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,9 @@ internal static Type GetPrototype(string name)
return pmap[name] as Type;
}


internal static Dictionary<IntPtr, Delegate> allocatedThunks = new Dictionary<IntPtr, Delegate>();

internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null)
{
Type dt;
Expand All @@ -505,6 +508,7 @@ internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null)
}
Delegate d = Delegate.CreateDelegate(dt, method);
var info = new ThunkInfo(d);
allocatedThunks[info.Address] = d;
return info;
}

Expand Down
7 changes: 4 additions & 3 deletions src/runtime/managedtype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ internal static int PyVisit(IntPtr ob, IntPtr visit, IntPtr arg)
{
return 0;
}
var visitFunc = (Interop.ObjObjFunc)Marshal.GetDelegateForFunctionPointer(visit, typeof(Interop.ObjObjFunc));
var visitFunc = NativeCall.GetDelegate<Interop.ObjObjFunc>(visit);
return visitFunc(ob, arg);
}

Expand All @@ -196,7 +196,7 @@ internal void CallTypeClear()
{
return;
}
var clearFunc = (Interop.InquiryFunc)Marshal.GetDelegateForFunctionPointer(clearPtr, typeof(Interop.InquiryFunc));
var clearFunc = NativeCall.GetDelegate<Interop.InquiryFunc>(clearPtr);
clearFunc(pyHandle);
}

Expand All @@ -214,7 +214,8 @@ internal void CallTypeTraverse(Interop.ObjObjFunc visitproc, IntPtr arg)
{
return;
}
var traverseFunc = (Interop.ObjObjArgFunc)Marshal.GetDelegateForFunctionPointer(traversePtr, typeof(Interop.ObjObjArgFunc));
var traverseFunc = NativeCall.GetDelegate<Interop.ObjObjArgFunc>(traversePtr);

var visiPtr = Marshal.GetFunctionPointerForDelegate(visitproc);
traverseFunc(pyHandle, visiPtr, arg);
}
Expand Down
11 changes: 8 additions & 3 deletions src/runtime/nativecall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
return d(a1, a2, a3);
}

private static T GetDelegate<T>(IntPtr fp) where T: Delegate
internal static T GetDelegate<T>(IntPtr fp) where T: Delegate
{
// Use Marshal.GetDelegateForFunctionPointer<> directly after upgrade the framework
return (T)Marshal.GetDelegateForFunctionPointer(fp, typeof(T));
Delegate d = null;
if (!Interop.allocatedThunks.TryGetValue(fp, out d))
Comment on lines +45 to +46
Copy link
Member

@lostmsu lostmsu Dec 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: you don't need to assign an initial value to d. You could also do TryGetValue(fp, out var d).

{
// Use Marshal.GetDelegateForFunctionPointer<> directly after upgrade the framework
d = Marshal.GetDelegateForFunctionPointer(fp, typeof(T));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also apply the comment here? The generic function was introduced in .NET 4.5.1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, maybe just remove the comment. You are not writing this into allocatedThunks right now, btw.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I don't cache these because they truly point to unmanaged code. We should only cache the function pointers to managed code.

}
return (T)d;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd save a couple instructions with:

Delegate d; // no init
if (trygetvalue(out d)) {
  return (T)d;
} else {
  return Marshal.GetDelegate...(); // no cast
}

}
}
}