A request for help (@John_McCall or @Joe_Groff , this might be your area if you have time?):
I've got a project with C++ interop up and mostly working, but I'm running into random EXC_BAD_ACCESS (code=1, address=something non-pointer-like like 0x46b) issues when calling methods on C++ types – sometimes the methods will work, and at other times they won't. I'm guessing that arguments are being passed incorrectly or that the stack is somehow being corrupted, but I haven't been able to properly track down the problem. I'm posting the SIL and LLVM-IR in the hope that someone else might be able to spot it.
SIL around the function call (to int MFnDagNode::objectColor(MStatus *)
) that's causing the problem:
%465 = alloc_stack $MFnDagNode // users: %466, %476, %474
store %463 to %465 : $*MFnDagNode // id: %466
%467 = begin_access [modify] [static] %52 : $*MStatus // users: %475, %468
%468 = address_to_pointer %467 : $*MStatus to $Builtin.RawPointer // user: %469
%469 = struct $UnsafeMutablePointer<MStatus> (%468 : $Builtin.RawPointer) // user: %472
%470 = tuple ()
%471 = tuple ()
%472 = enum $Optional<UnsafeMutablePointer<MStatus>>, #Optional.some!enumelt.1, %469 : $UnsafeMutablePointer<MStatus> // user: %474
// function_ref _ZNK8Autodesk4Maya16OpenMaya2019000010MFnDagNode11objectColorEPNS1_7MStatusE
%473 = function_ref @_ZNK8Autodesk4Maya16OpenMaya2019000010MFnDagNode11objectColorEPNS1_7MStatusE : $@convention(c) (@in MFnDagNode, Optional<UnsafeMutablePointer<MStatus>>) -> Int32 // user: %474
%474 = apply %473(%465, %472) : $@convention(c) (@in MFnDagNode, Optional<UnsafeMutablePointer<MStatus>>) -> Int32 // users: %509, %478
end_access %467 : $*MStatus // id: %475
dealloc_stack %465 : $*MFnDagNode // id: %476
LLVM IR:
%14 = alloca %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV, align 8
...
%315 = bitcast %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV* %14 to i8*
call void @llvm.lifetime.start.p0i8(i64 64, i8* %315)
%.f_path26 = getelementptr inbounds %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV, %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV* %14, i32 0, i32 1
%316 = bitcast %TSvSg* %.f_path26 to i64*
store i64 %308, i64* %316, align 8
%.f_xform27 = getelementptr inbounds %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV, %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV* %14, i32 0, i32 2
%317 = bitcast %TSvSg* %.f_xform27 to i64*
store i64 %310, i64* %317, align 8
%.f_data128 = getelementptr inbounds %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV, %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV* %14, i32 0, i32 3
%318 = bitcast %TSvSg* %.f_data128 to i64*
store i64 %312, i64* %318, align 8
%.f_data229 = getelementptr inbounds %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV, %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV* %14, i32 0, i32 4
%319 = bitcast %TSvSg* %.f_data229 to i64*
store i64 %314, i64* %319, align 8
%320 = bitcast %TSo8AutodeskJ4MayaJ16OpenMaya20190000J7MStatusV* %status to i8*
%321 = ptrtoint i8* %320 to i64
%322 = bitcast %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV* %14 to %"class.Autodesk::Maya::OpenMaya20190000::MFnDagNode"*
%323 = inttoptr i64 %321 to %"class.Autodesk::Maya::OpenMaya20190000::MStatus"*
%324 = call i32 @_ZNK8Autodesk4Maya16OpenMaya2019000010MFnDagNode11objectColorEPNS1_7MStatusE(%"class.Autodesk::Maya::OpenMaya20190000::MFnDagNode"* %322, %"class.Autodesk::Maya::OpenMaya20190000::MStatus"* %323)
%325 = bitcast %TSo8AutodeskJ4MayaJ16OpenMaya20190000J10MFnDagNodeV* %14 to i8*
call void @llvm.lifetime.end.p0i8(i64 64, i8* %325)
%objectColor._value30 = getelementptr inbounds %Ts5Int32V, %Ts5Int32V* %objectColor, i32 0, i32 0
store i32 %324, i32* %objectColor._value30, align 4
Registers at the time of the crash:
General Purpose Registers:
rax = 0x0000000000000003
rbx = 0x0000000000000000
rcx = 0x00007ffeefbfaf50
rdx = 0x0000000000000000
rdi = 0x00007ffeefbfaf50
rsi = 0x00007ffeefbfb040
rbp = 0x00007ffeefbfb070
rsp = 0x00007ffeefbfaba8
r8 = 0x0000000000000036
r9 = 0x0000000000000000
r10 = 0x0000000000000037
r11 = 0x0000000000000037
r12 = 0x00007ffeefbfb090
r13 = 0x00007ffeefbfafb0
r14 = 0x0000000138312c00
r15 = 0x00000001c5db01c0 llamaPlugin.bundle`$s11llamaPlugin9nodeAdded0C010clientDataySpySo8AutodeskJ4MayaJ16OpenMaya20190000J7MObjectVGSg_SvSgtFTo
rip = 0x000000016d71a37b libOpenMaya.dylib`Autodesk::Maya::OpenMaya20190000::MFnDagNode::isInstanceable(Autodesk::Maya::OpenMaya20190000::MStatus*) const + 11
rflags = 0x0000000000010206
cs = 0x000000000000002b
fs = 0x0000000000000000
gs = 0x0000000000000000
Call-site assembly:
0x1c5daf0e2 <+3138>: movq -0x58(%rbp), %rax
0x1c5daf0e6 <+3142>: movq -0x50(%rbp), %rcx
0x1c5daf0ea <+3146>: movq -0x48(%rbp), %rdx
0x1c5daf0ee <+3150>: movq -0x40(%rbp), %rsi
0x1c5daf0f2 <+3154>: movq %rax, -0x160(%rbp)
0x1c5daf0f9 <+3161>: movq %rcx, -0x158(%rbp)
0x1c5daf100 <+3168>: movq %rdx, -0x150(%rbp)
0x1c5daf107 <+3175>: movq %rsi, -0x148(%rbp)
0x1c5daf10e <+3182>: leaq -0x30(%rbp), %rax
0x1c5daf112 <+3186>: leaq -0x180(%rbp), %rcx
0x1c5daf119 <+3193>: movq %rcx, %rdi
0x1c5daf11c <+3196>: movq %rax, %rsi
0x1c5daf11f <+3199>: callq 0x1c69efb32 ; symbol stub for: Autodesk::Maya::OpenMaya20190000::MFnDagNode::objectColor(Autodesk::Maya::OpenMaya20190000::MStatus*) const
0x1c5daf124 <+3204>: movl %eax, -0x128(%rbp)
0x1c5daf12a <+3210>: movl $0x1, %edi
0x1c5daf12f <+3215>: movl %eax, -0x41c(%rbp)
I know that the MFnDagNode
is a valid type and I can intermittently call other methods on it successfully. Any ideas as to what's going on here and how I might work around it? I'm fine with hacky solutions at this stage – I realise a lot of the C++ metadata isn't getting properly propagated through yet.