简体   繁体   中英

Calling method_getImplementation throws ExecutionEngineException on device

I'm trying to implement swizzling from the answer to the question here . I have a example on github . The full crash is here .

[DllImport("/usr/lib/libobjc.dylib")]
extern static Func<IntPtr,IntPtr,IntPtr> method_getImplementation(IntPtr method);

delegate void CaptureDelegate(IntPtr block,IntPtr self,IntPtr uiView);

[MonoPInvokeCallback(typeof(CaptureDelegate))]
static void MyCapture(IntPtr block, IntPtr self, IntPtr uiView)
{
}

static void HijackWillMoveToSuperView()
{
    var method = class_getInstanceMethod(new UIView().ClassHandle, new Selector("willMoveToSuperview:").Handle);

    original_impl = method_getImplementation(method);

    var block_value = new BlockLiteral();
    CaptureDelegate d = MyCapture;
    block_value.SetupBlock(d, null);
    var imp = imp_implementationWithBlock(ref block_value);
    method_setImplementation(method, imp);
}

The example works on the simulator but not on the device. On the device,

 original_impl = method_getImplementation(method);

throws an ExecutionEngineException

Attempting to JIT compile method '(wrapper managed-to-native) System.Func`3:wrapper_aot_native (intptr&,intptr,intptr)' while running with --aot-only.

What do I need to do in order for this to be AOT compiled? Or, is this an issue with calling method_getImplementation(method) on ARM64? I've found some information about that here .

It seems like using an imp_implementationWithBlock crashes on arm64.

And more here :

Note the cast to objc_msgSend. While this (by luck) worked without casting in the earlier days, this will probably crash your arm64 build if you don't cast this correctly, because the variable argument casting specifications changed.

There is an example in objective-c here , where the architecture is checked and the behavior is different for 32 vs 64. I'm wondering if I have the same problem.

返回值是“ IntPtr”,而不是“ Func”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM